forked from nqthqn/obsidian-wordy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordPanel.ts
50 lines (41 loc) · 1.53 KB
/
wordPanel.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
import { Plugin } from "obsidian";
class SearchableWordsPanel extends Plugin {
private containerEl: HTMLElement;
private similarWords: string[];
constructor(app: App, similarWords: string[]) {
super(app);
this.similarWords = similarWords;
}
async onload() {
// create the container element
this.containerEl = document.createElement("div");
this.containerEl.addClass("tlf-synonym-panel");
// create the list element and populate it with clickable items
const listEl = document.createElement("ul");
listEl.addClass("tlf-synonym-list");
this.containerEl.appendChild(listEl);
for (const word of this.similarWords) {
const itemEl = document.createElement("li");
itemEl.addClass("tlf-synonym-item");
itemEl.setText(word);
itemEl.addEventListener("click", () => {
this.replaceSelectionWithWord(word);
});
listEl.appendChild(itemEl);
}
// create the markdown preview
const preview = this.app.workspace.createMarkdownPreview(
"Synonyms",
this.containerEl
);
// register the unload function to remove the preview
this.register(() => preview && preview.destroy());
}
private replaceSelectionWithWord(word: string) {
const editor = this.app.workspace.getActiveTextEditor();
if (editor) {
editor.replaceSelection(word);
}
}
}
export default SearchableWordsPanel;