-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.ts
More file actions
104 lines (87 loc) · 2.99 KB
/
Copy pathmain.ts
File metadata and controls
104 lines (87 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { GeneratorModal } from 'editor/GeneratorModal';
import { MarkdownView, Notice, Plugin, Editor } from 'obsidian';
import { InlineGeneratorSuggester } from "editor/InlineGenerator";
import { FantasyPluginSettings, possibleOptions } from 'settings/Datatypes';
import { DEFAULT_SETTINGS } from 'settings/DefaultSetting';
import { SettingTab } from 'settings/SettingsTab';
export default class FantasyPlugin extends Plugin {
settings: FantasyPluginSettings;
currentEditor: Editor | null = null;
//Function used to return the array of options for the suggester.
getOptionsForSuggest(): string[] {
return possibleOptions;
}
async onload() {
await this.loadSettings();
this.app.workspace.on('active-leaf-change', (leaf) => {
if (leaf) {
const view = leaf.view;
if (view instanceof MarkdownView) {
this.currentEditor = view.editor;
} else {
this.currentEditor = null;
}
} else {
this.currentEditor = null;
}
});
//Command to open Modal dialog
this.addCommand({
id: 'open-fantasy-generator',
name: 'Open Fantasy Generator',
callback: () => {
new GeneratorModal(this.app, (result) => {
const copyContent = async () => {
//Try to see if any generators spit out an Error or if copying the string fails.
try {
if (result instanceof Error) {
new Notice(`${result}`);
} else {
await navigator.clipboard.writeText(result);
new Notice(`${result} was copied to the clipboard.`);
}
} catch (err) {
console.error('Failed to copy: ', err);
new Notice("Failed to copy, Check error in console.");
}
}
copyContent();
}, this).open();
},
});
//Register the InlineGeneratorSuggester to the Editor suggester.
this.registerEditorSuggest(new InlineGeneratorSuggester(this.getOptionsForSuggest, this));
// This creates an icon in the left ribbon to access the modal for the Fantasy Content Generator.
this.addRibbonIcon('book', 'Fantasy Generators', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new GeneratorModal(this.app, (result) => {
const copyContent = async () => {
//Try to see if any generators spit out an Error or if copying the string fails.
try {
if (result instanceof Error) {
new Notice(`${result}`);
} else {
await navigator.clipboard.writeText(result);
new Notice(`${result} was copied to the clipboard.`);
}
} catch (err) {
console.error('Failed to copy: ', err);
new Notice("Failed to copy, Check error in console.");
}
}
copyContent();
}, this).open();
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
console.log("loaded Fantasy Content Generator");
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}