forked from nqthqn/obsidian-wordy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
217 lines (192 loc) · 4.95 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
import {
App,
Editor,
MarkdownView,
Notice,
Plugin,
PluginManifest,
PluginSettingTab,
SuggestModal,
Modal,
} from "obsidian";
import { DatamuseApi } from "./DatamuseApi";
import { SearchableWordsPanel } from "./wordPanel";
import { coundSelected } from "./countSelected";
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 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) => {
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) => {
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) => {
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) => {
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 loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
// 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!");
}
}