generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.ts
273 lines (238 loc) · 6.07 KB
/
main.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {
App,
Editor,
MarkdownView,
Notice,
Plugin,
PluginSettingTab,
SuggestModal,
} from "obsidian";
import type { MarkdownFileInfo, PluginManifest }from "obsidian";
import { DatamuseApi } from "./DatamuseApi";
interface WordyPluginSettings {
enumeratedWords: boolean;
}
const DEFAULT_SETTINGS: WordyPluginSettings = {
enumeratedWords: true,
};
export default class WordyPlugin extends Plugin {
settings: WordyPluginSettings = {
enumeratedWords: true,
};
datamuseApi: DatamuseApi;
constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.datamuseApi = new DatamuseApi();
}
// Initial plugin setup, configure all the resources needed by the plugin
async onload() {
await this.loadSettings();
this.registerView(
VIEW_ID,
(leaf) => new WordyView(leaf)
);
this.addRibbonIcon("pilcrow", "Wordy view", () => {
this.activateView();
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: "wordy-syn",
name: "Synonyms",
editorCallback: async (editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
const rootWord = editor.getSelection();
if (rootWord != "") {
const similarWords = await this.datamuseApi.wordsSimilarTo(
rootWord
);
if (similarWords.length == 0) {
new Notice(`Oops — No synonyms found.`);
return;
}
new SearchableWordsModal(
this.app,
similarWords,
(selectedWord: string) => {
editor.replaceSelection(selectedWord);
}
).open();
} else {
new Notice(`Oops — Select a word first.`);
}
},
});
this.addCommand({
id: "wordy-ant",
name: "Antonyms",
editorCallback: async (editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
const rootWord = editor.getSelection();
if (rootWord != "") {
const oppositeWords =
await this.datamuseApi.wordsOppositeTo(rootWord, true);
if (oppositeWords.length == 0) {
new Notice(`Oops — No antonyms found.`);
return;
}
new SearchableWordsModal(
this.app,
oppositeWords,
(selectedWord: string) => {
editor.replaceSelection(selectedWord);
}
).open();
} else {
new Notice(`Oops — Select a word first.`);
}
},
});
this.addCommand({
id: "wordy-rhy",
name: "Rhymes",
editorCallback: async (editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
const rootWord = editor.getSelection();
if (rootWord != "") {
const rhymes = await this.datamuseApi.wordsThatRhymeWith(
rootWord
);
if (rhymes.length == 0) {
new Notice(`Oops — No rhymes found.`);
return;
}
new SearchableWordsModal(
this.app,
rhymes,
(selectedWord: string) => {
editor.replaceSelection(selectedWord);
}
).open();
} else {
new Notice(`Oops — Select a word first.`);
}
},
});
this.addCommand({
id: "wordy-asyn",
name: "Alliterative Synonyms",
editorCallback: async (editor: Editor, view: MarkdownView | MarkdownFileInfo) => {
const [priorWord, rootWord] = editor.getSelection().split(" ");
if (rootWord != "") {
const alliterativeSynonyms =
await this.datamuseApi.alliterativeSynonyms(
priorWord,
rootWord
);
if (alliterativeSynonyms.length == 0) {
new Notice(`Oops — No rhymes found.`);
return;
}
new SearchableWordsModal(
this.app,
alliterativeSynonyms,
(selectedWord: string) => {
editor.replaceSelection(`${selectedWord}`);
}
).open();
} else {
new Notice(`Oops — Select a word first.`);
}
},
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new WordyPluginSettingTab(this.app, this));
}
async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_ID);
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_ID,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_ID)[0]
);
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
// Wordy View
import { ItemView, WorkspaceLeaf } from "obsidian";
import Component from "./Component.svelte";
export const VIEW_ID = "wordy-view";
export class WordyView extends ItemView {
component: Component;
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
getViewType() {
return VIEW_ID;
}
getDisplayText() {
return "Wordy";
}
async onOpen() {
// const container = this.containerEl.children[1];
// container.empty();
// container.createEl("h4", { text: "Wordy" });
// debugger;
this.component = new Component({
target: this.containerEl.children[1],
props: {
variable: 42
}
})
}
async onClose() {
// Kill the svelte app
this.component.$destroy();
}
}
// Suggestion modal
type Word = string;
export class SearchableWordsModal extends SuggestModal<Word> {
words: string[];
replaceFn: any;
constructor(app: App, words: string[], replaceFn: any) {
super(app);
this.words = words;
this.replaceFn = replaceFn;
if (words.length == 0) {
return;
}
}
// Returns all available suggestions.
getSuggestions(query: string): Word[] {
return this.words.filter((word: string) =>
word.toLowerCase().includes(query.toLowerCase())
);
}
// Renders each suggestion item.
renderSuggestion(word: Word, el: HTMLElement) {
el.createEl("div", { text: word });
}
// Perform action on the selected suggestion.
onChooseSuggestion(word: Word, evt: MouseEvent | KeyboardEvent) {
this.replaceFn(word);
}
}
/**
* Setting Pane
*/
class WordyPluginSettingTab extends PluginSettingTab {
plugin: WordyPlugin;
constructor(app: App, plugin: WordyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings" });
containerEl.createEl("p").setText("Nothing to configure yet!");
}
}