-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
478 lines (406 loc) · 14.1 KB
/
main.ts
File metadata and controls
478 lines (406 loc) · 14.1 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import { Editor, MarkdownFileInfo, MarkdownView, Notice, Plugin, PluginSettingTab, Setting, Menu, Modal, App } from 'obsidian';
import { SynonymerSettings, DEFAULT_SETTINGS } from './settings';
import { SynonymService } from './synonymService';
// Import without .js extension for TypeScript compatibility
import { AssetDictionaryLoader } from './assetDictionaryLoader';
import { CustomDictionaryManager } from './customDictionaryManager';
import { t, Translations } from './i18n';
// Add a new modal class for adding synonyms
class AddSynonymModal extends Modal {
word: string;
onSubmit: (synonym: string) => void;
tr: Translations;
constructor(app: App, word: string, tr: Translations, onSubmit: (synonym: string) => void) {
super(app);
this.word = word;
this.tr = tr;
this.onSubmit = onSubmit;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h2', { text: this.tr.addSynonymTitle(this.word) });
const inputEl = contentEl.createEl('input', {
type: 'text',
placeholder: this.tr.addSynonymPlaceholder
});
inputEl.style.width = '100%';
inputEl.style.marginBottom = '10px';
const buttonContainer = contentEl.createDiv();
buttonContainer.style.display = 'flex';
buttonContainer.style.justifyContent = 'flex-end';
buttonContainer.style.gap = '10px';
const cancelBtn = buttonContainer.createEl('button', { text: this.tr.cancel });
cancelBtn.onclick = () => this.close();
const submitBtn = buttonContainer.createEl('button', {
text: this.tr.add,
cls: 'mod-cta'
});
submitBtn.onclick = () => {
const synonym = inputEl.value.trim();
if (synonym) {
this.onSubmit(synonym);
this.close();
}
};
inputEl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
const synonym = inputEl.value.trim();
if (synonym) {
this.onSubmit(synonym);
this.close();
}
}
});
inputEl.focus();
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
export default class SynonymerPlugin extends Plugin {
settings!: SynonymerSettings;
synonymService!: SynonymService;
customManager!: CustomDictionaryManager;
async onload() {
this.addStyles();
await this.loadSettings();
// Initialize custom dictionary manager - pass app instead of vault
this.customManager = new CustomDictionaryManager(this.app, this.manifest.dir!);
await this.customManager.load();
// Load assets dictionary - pass app instead of vault
const assetLoader = new AssetDictionaryLoader(this.app, this.manifest.dir!);
const assetDict = await assetLoader.loadDictionary(this.settings.selectedLanguage);
this.synonymService = new SynonymService(this.settings, assetDict, this.customManager);
// Add ribbon icon to the left sidebar - using Obsidian's built-in ClipboardType icon
this.addRibbonIcon('clipboard-list', t(this.settings.uiLanguage).ribbonTooltip, (evt: MouseEvent) => {
const tr = t(this.settings.uiLanguage);
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view) {
const editor = view.editor;
const selection = editor.getSelection();
if (selection) {
this.showSynonyms(selection, editor);
} else {
new Notice(tr.selectWordNotice);
}
} else {
new Notice(tr.markdownOnlyNotice);
}
});
// Add a command to show synonyms for the selected word
this.addCommand({
id: 'show-synonyms',
name: t(this.settings.uiLanguage).commandShowSynonyms,
editorCallback: (editor: Editor, ctx: MarkdownView | MarkdownFileInfo) => {
const tr = t(this.settings.uiLanguage);
if (ctx instanceof MarkdownView) {
const selection = editor.getSelection();
if (selection) {
this.showSynonyms(selection, editor);
} else {
new Notice(tr.selectWordNotice);
}
} else {
new Notice(tr.markdownOnlyNotice);
}
}
});
// Add editor context menu item (right-click menu)
this.registerEvent(
this.app.workspace.on('editor-menu', (menu: Menu, editor: Editor) => {
const tr = t(this.settings.uiLanguage);
let selection = editor.getSelection();
// If no selection, try to get word under cursor
if (!selection || selection.trim() === '') {
selection = this.getWordAtCursor(editor);
}
if (selection && selection.trim() !== '') {
// Add option to add custom synonym
menu.addItem((item) => {
item.setTitle(tr.contextAddSynonym)
.setIcon('pencil')
.onClick(() => {
this.promptAddSynonym(selection);
});
});
menu.addItem((item) => {
item.setTitle(tr.contextFindSynonyms)
.setIcon('search')
.onClick(() => {
this.showSynonyms(selection, editor);
});
});
}
})
);
// Add settings tab
this.addSettingTab(new SynonymerSettingTab(this.app, this));
}
getWordAtCursor(editor: Editor): string {
const cursor = editor.getCursor();
const line = editor.getLine(cursor.line);
let start = cursor.ch;
let end = cursor.ch;
// Word characters including Swedish/European characters and hyphens
const wordChar = /[\w\u00C0-\u00ff\-]/;
// Walk backwards
while (start > 0 && wordChar.test(line.charAt(start - 1))) {
start--;
}
// Walk forwards
while (end < line.length && wordChar.test(line.charAt(end))) {
end++;
}
return line.slice(start, end);
}
async promptAddSynonym(word: string) {
const tr = t(this.settings.uiLanguage);
new AddSynonymModal(this.app, word, tr, async (synonym) => {
try {
await this.customManager.addSynonym(word, synonym);
// Reload the synonym service to include the new custom synonym
const assetLoader = new AssetDictionaryLoader(this.app, this.manifest.dir!);
const assetDict = await assetLoader.loadDictionary(this.settings.selectedLanguage);
this.synonymService = new SynonymService(this.settings, assetDict, this.customManager);
new Notice(tr.synonymAddedNotice(synonym, word));
} catch (error) {
console.error('Error adding synonym:', error);
new Notice(tr.couldNotAddSynonym + (error instanceof Error ? error.message : tr.unknownError));
}
}).open();
}
// Replace loadStyles with addStyles that adds CSS directly
addStyles() {
// Add styles directly to document
const styleEl = document.createElement('style');
styleEl.id = 'synonym-plugin-styles';
styleEl.textContent = `
/* Synonym Plugin Custom Styles */
.synonymer-menu-item {
padding: 8px 12px;
cursor: pointer;
display: flex;
align-items: center;
font-size: 14px;
width: 100%;
text-align: left;
}
.synonymer-menu-item:hover {
background-color: var(--background-modifier-hover);
color: var(--text-accent);
}
.synonymer-notice {
font-size: 14px;
padding: 8px;
background-color: var(--background-primary);
border-left: 4px solid var(--text-accent);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
/* Make the toolbar icon black and white */
.side-dock-ribbon-action[aria-label="Synonym"] svg {
color: var(--icon-color) !important;
}
`;
document.head.appendChild(styleEl);
}
async showSynonyms(word: string, editor: Editor) {
const tr = t(this.settings.uiLanguage);
try {
new Notice(tr.searchingNotice(word), 2000);
const synonyms = await this.synonymService.getSynonyms(word);
if (synonyms.length === 0) {
new Notice(tr.noSynonymsNotice(word), 3000);
return;
}
const menu = this.createSynonymMenu(synonyms, (synonym) => {
editor.replaceSelection(synonym);
});
// Fix for iOS: Use safer selection coordinate detection
let rect: { left: number; bottom: number; };
try {
const selection = window.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
const domRect = range.getBoundingClientRect();
rect = { left: domRect.left, bottom: domRect.bottom };
} else {
throw new Error("No selection ranges available");
}
} catch (e) {
// Safe fallback without using getScrollerElement
const pos = editor.getCursor();
const lineHeight = 20;
rect = {
left: 100,
bottom: 200 + ((pos.line + 1) * lineHeight)
};
}
menu.showAtPosition({ x: rect.left, y: rect.bottom });
} catch (error) {
console.error('Error fetching synonyms:', error);
// Better error handling for network errors
let errorMessage: string;
if (error instanceof Error) {
// Check for DNS or network errors
if (error.message.includes('ERR_NAME_NOT_RESOLVED') ||
error.message.includes('ERR_CONNECTION_REFUSED') ||
error.message.includes('NetworkError')) {
errorMessage = tr.connectionError;
} else {
errorMessage = error.message;
}
} else {
errorMessage = tr.unknownError;
}
new Notice(`${tr.couldNotFetchSynonyms}${errorMessage}`, 4000);
}
}
createSynonymMenu(synonyms: string[], onSelect: (synonym: string) => void) {
const menu = new Menu();
const tr = t(this.settings.uiLanguage);
// Show a header with count
menu.addItem((item) => {
item.setTitle(tr.foundSynonymsHeader(synonyms.length))
.setDisabled(true);
});
menu.addSeparator();
if (synonyms.length > 10) {
// Create submenu for better organization when many synonyms
const submenuSize = Math.ceil(synonyms.length / 2);
const firstHalf = synonyms.slice(0, submenuSize);
const secondHalf = synonyms.slice(submenuSize);
firstHalf.forEach((synonym) => {
menu.addItem((item) => {
// Fix: Only set title and onClick once to avoid errors
item.setTitle(synonym)
.onClick(() => onSelect(synonym));
});
});
if (secondHalf.length > 0) {
menu.addSeparator();
secondHalf.forEach((synonym) => {
menu.addItem((item) => {
// Fix: Only set title and onClick once to avoid errors
item.setTitle(synonym)
.onClick(() => onSelect(synonym));
});
});
}
} else {
// Simpler menu for fewer synonyms
synonyms.forEach((synonym) => {
menu.addItem((item) => {
item.setTitle(synonym)
.onClick(() => onSelect(synonym));
});
});
}
return menu;
}
onunload() {
// Clean up styles when plugin is disabled
const styleEl = document.getElementById('synonym-plugin-styles');
if (styleEl) styleEl.remove();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SynonymerSettingTab extends PluginSettingTab {
plugin: SynonymerPlugin;
constructor(app: any, plugin: SynonymerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
const tr = t(this.plugin.settings.uiLanguage);
containerEl.createEl('h2', {text: tr.settingsHeading});
// UI Language selection
new Setting(containerEl)
.setName(tr.uiLanguageName)
.setDesc(tr.uiLanguageDesc)
.addDropdown(dropdown => dropdown
.addOption('en', tr.uiLanguageEnglish)
.addOption('sv', tr.uiLanguageSwedish)
.setValue(this.plugin.settings.uiLanguage)
.onChange(async (value: string) => {
this.plugin.settings.uiLanguage = value as 'en' | 'sv';
// Also switch online source to match UI language
this.plugin.settings.apiSource = value === 'sv' ? 'svenska_se' : 'thesaurus_com';
await this.plugin.saveSettings();
// Re-render settings tab with new language
this.display();
}));
// Dictionary language selection
new Setting(containerEl)
.setName(tr.dictLanguageName)
.setDesc(tr.dictLanguageDesc)
.addDropdown(async (dropdown) => {
const loader = new AssetDictionaryLoader(this.app, this.plugin.manifest.dir!);
const languages = await loader.getAvailableLanguages();
// Add language options with better display names
if (languages.length === 0) {
dropdown.addOption('', tr.noDictionariesFound);
} else {
languages.forEach(lang => {
// Display language code as-is (e.g., sv_SE, en_US)
dropdown.addOption(lang, lang);
});
}
dropdown.setValue(this.plugin.settings.selectedLanguage)
.onChange(async (value) => {
this.plugin.settings.selectedLanguage = value;
await this.plugin.saveSettings();
new Notice(tr.reloadPluginNotice);
});
});
new Setting(containerEl)
.setName(tr.enableOnlineName)
.setDesc(tr.enableOnlineDesc)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableOnlineLookup)
.onChange(async (value) => {
this.plugin.settings.enableOnlineLookup = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(tr.onlineSourceName)
.setDesc(tr.onlineSourceDesc)
.addDropdown(dropdown => dropdown
.addOption('thesaurus_com', tr.onlineSourceThesaurus)
.addOption('svenska_se', tr.onlineSourceSwedish)
.setValue(this.plugin.settings.apiSource)
.onChange(async (value) => {
this.plugin.settings.apiSource = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(tr.apiKeyName)
.setDesc(tr.apiKeyDesc)
.addText(text => text
.setPlaceholder(tr.apiKeyPlaceholder)
.setValue(this.plugin.settings.apiKey)
.onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(tr.maxSynonymsName)
.setDesc(tr.maxSynonymsDesc)
.addSlider(slider => slider
.setLimits(3, 25, 1)
.setValue(this.plugin.settings.maxSynonyms)
.setDynamicTooltip()
.onChange(async (value) => {
this.plugin.settings.maxSynonyms = value;
await this.plugin.saveSettings();
}));
// Custom synonyms section removed - see README for file location
}
}