generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
248 lines (215 loc) · 7.85 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
import cyrillicToTranslit from 'cyrillic-to-translit-js';
import { App, Notice, Plugin, PluginSettingTab, SearchComponent, Setting, View } from 'obsidian';
interface TransliteratedSearchPluginSettings {
ukrainian: boolean;
russian: boolean;
turnOnByDefault: boolean;
}
const DEFAULT_SETTINGS: TransliteratedSearchPluginSettings = {
ukrainian: true,
russian: true,
turnOnByDefault: true,
}
export default class TransliteratedSearchPlugin extends Plugin {
settings: TransliteratedSearchPluginSettings;
statusBarItemEl: HTMLElement;
ribbonIconEl: HTMLElement;
isTransliterationEnabled: boolean;
async onload() {
await this.loadSettings();
// This adds a settings tab so the user can configure language settings
this.addSettingTab(new TransliteratedSearchSettingTab(this.app, this));
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
this.statusBarItemEl = this.addStatusBarItem();
// This creates an icon in the left ribbon.
this.ribbonIconEl = this.addRibbonIcon('languages', 'Transliterated Search', (evt: MouseEvent) => {
if (this.isTransliterationEnabled) {
this.removeSettingsFromSearchComponent();
this.defaultGetterOfSearchComponent();
new Notice('Transliteration is disabled!');
} else {
this.applySettingsToSearchComponent();
this.overrideGetterOfSearchComponent();
new Notice('Transliteration is enabled!');
}
});
// Load plugin on startup if required
this.observeSearchPanelLoaded();
}
onunload() {
// Returning everything to the default
this.removeSettingsFromSearchComponent();
this.defaultGetterOfSearchComponent();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
this.applySettingsToSearchComponent();
}
/**
* Loads transliteration plugin on startup if required.
* Uses observer to detect when the search panel is loaded.
*/
private observeSearchPanelLoaded(): void {
const observer = new MutationObserver(mutations => {
if (document.querySelector('.search-input-container')) {
observer.disconnect();
if (this.settings.turnOnByDefault) {
console.log("Enabling transliteration on startup");
this.getSearchComponent().then((searchComponent) => {
searchComponent.inputEl.value = "";
});
this.applySettingsToSearchComponent();
this.overrideGetterOfSearchComponent();
}
}
});
observer.observe(document.body, { attributes: true, childList: true, subtree: true });
}
/**
* Overrides search component getter to add transliteration.
* Method {@code getValue} is called by the core Search plugin to get the search query.
* This method is overridden and appended with transliterated terms.
* Enables transliteration.
*/
private overrideGetterOfSearchComponent(): void {
this.getSearchComponent().then((searchComponent) => {
searchComponent.getValue = function () {
const t = this as ModifiedSearchComponent;
const userInput: string = t.inputEl.value ?? "";
if (userInput == "") return "";
const termSet: Set<string> = new Set();
termSet.add(userInput);
if (t.translitRU) {
const ru_en = t.translitRU.transform(userInput).toLowerCase();
const en_ru = t.translitRU.reverse(userInput).toLowerCase();
termSet.add(ru_en);
termSet.add(en_ru);
}
if (t.translitUK) {
const uk_en = t.translitUK.transform(userInput).toLowerCase();
const en_uk = t.translitUK.reverse(userInput).toLowerCase();
termSet.add(uk_en);
termSet.add(en_uk);
}
const transliteratedInput = Array.from(termSet).join(" OR ");
return transliteratedInput;
}
this.updateTransliterationStatus(true);
});
}
/**
* Returns the search component getter to the default.
* Disables transliteration.
*/
private defaultGetterOfSearchComponent(): void {
this.getSearchComponent().then((searchComponent) => {
searchComponent.getValue = function () {
const t = this as ModifiedSearchComponent;
return t.inputEl.value;
}
this.updateTransliterationStatus(false);
});
}
/**
* This function initializes search component with transliteration settings.
*/
private applySettingsToSearchComponent(): void {
this.getSearchComponent().then((searchComponent) => {
searchComponent.translitRU = this.settings.russian ? cyrillicToTranslit({ preset: 'ru' }) : undefined;
searchComponent.translitUK = this.settings.ukrainian ? cyrillicToTranslit({ preset: 'uk' }) : undefined;
});
}
/**
* This function removes transliteration settings from search component.
* This is needed to return the search component to the default state.
*/
private removeSettingsFromSearchComponent(): void {
this.getSearchComponent().then((searchComponent) => {
searchComponent.translitRU = undefined;
searchComponent.translitUK = undefined;
});
}
/**
* Function to update the status of the transliteration.
* Also updates the status bar and ribbon icon.
* @param isEnabled - boolean value to set the status of the transliteration
*/
private updateTransliterationStatus(isEnabled: boolean): void {
this.isTransliterationEnabled = isEnabled;
this.updateStatusBar(isEnabled ? 'Transliteration On' : 'Transliteration Off');
if (isEnabled)
this.ribbonIconEl.addClass('is-active');
else
this.ribbonIconEl.removeClass('is-active');
}
private updateStatusBar(text: string): void {
this.statusBarItemEl.setText(text);
}
/**
* Function to get the native searchComponent from the core Search panel
* @returns searchComponent from search panel
*/
private getSearchComponent(): ModifiedSearchComponent {
const searchPanel: CoreSearchPanel = this.app.workspace.getLeavesOfType('search')[0]?.view as CoreSearchPanel;
return searchPanel.searchComponent;
}
// TODO: try to make this work in async way
// private getSearchComponent(): Promise<ModifiedSearchComponent> {
// return new Promise<ModifiedSearchComponent>((resolve) => {
// const searchPanel: CoreSearchPanel = this.app.workspace.getLeavesOfType('search')[0]?.view as CoreSearchPanel;
// if (searchPanel && searchPanel.searchComponent) {
// return resolve(searchPanel.searchComponent);
// }
// });
// }
}
class TransliteratedSearchSettingTab extends PluginSettingTab {
plugin: TransliteratedSearchPlugin;
constructor(app: App, plugin: TransliteratedSearchPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Settings for transliterated search plugin.' });
new Setting(containerEl)
.setName('Transliterate Ukrainian')
.setDesc('If checked, the plugin will transliterate Ukrainian characters.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.ukrainian)
.onChange(async (value) => {
this.plugin.settings.ukrainian = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Transliterate russian')
.setDesc('If checked, the plugin will transliterate russian characters.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.russian)
.onChange(async (value) => {
this.plugin.settings.russian = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Turn on by default')
.setDesc('If checked, the plugin will be activated each time you open obsidian. NOTE: will clear search input on startup.')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.turnOnByDefault)
.onChange(async (value) => {
this.plugin.settings.turnOnByDefault = value;
await this.plugin.saveSettings();
}));
}
}
// OBSIDIAN UI COMPONENT INTERFACES
interface CoreSearchPanel extends View {
searchComponent: ModifiedSearchComponent;
}
interface ModifiedSearchComponent extends SearchComponent {
translitRU: undefined | any;
translitUK: undefined | any;
}