Skip to content

Commit

Permalink
Close #49
Browse files Browse the repository at this point in the history
  • Loading branch information
phibr0 committed Sep 16, 2021
1 parent 68650bd commit 21c1d24
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const DEFAULT_CACHE: DictionaryCache = {
}

export const DEFAULT_SETTINGS: DictionarySettings = {
getLangFromFile: true,
defaultLanguage: "en_US",
normalLang: 'en_US',
shouldShowSynonymPopover: true,
shouldShowCustomContextMenu: false,
apiSettings: {
Expand Down
18 changes: 17 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT_CACHE } from './_constants';
import { DEFAULT_CACHE, LANGUAGES } from './_constants';
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { DictionaryCache, DictionarySettings } from 'src/types';
Expand Down Expand Up @@ -129,6 +129,22 @@ export default class DictionaryPlugin extends Plugin {
// Editor mode
// @ts-ignore
this.registerEvent(this.app.workspace.on('editor-menu', this.handleContextMenuHelper));

this.registerEvent(this.app.workspace.on('file-open', async (file) => {
if (this.settings.getLangFromFile) {
let lang = this.app.metadataCache.getFileCache(file).frontmatter?.lang;
if (!lang) {
lang = this.app.metadataCache.getFileCache(file).frontmatter?.language;
}
if (lang && Object.keys(LANGUAGES).contains(lang)) {
this.settings.defaultLanguage = lang;
await this.saveSettings();
} else {
this.settings.defaultLanguage = this.settings.normalLang;
await this.saveSettings();
}
}
}));
}

onunload(): void {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { DictionaryWord, Synonym } from "src/integrations/types";

export interface DictionarySettings {
defaultLanguage: keyof APISettings;
normalLang: keyof APISettings;
getLangFromFile: boolean;
apiSettings: APISettings;
partOfSpeechApiName: string;
shouldShowSynonymPopover: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/ui/modals/languageChooser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class LanguageChooser extends FuzzySuggestModal<string>{

async onChooseItem(item: string): Promise<void> {
this.plugin.settings.defaultLanguage = item as keyof typeof LANGUAGES;
this.plugin.settings.normalLang = item as keyof typeof LANGUAGES;
await this.plugin.saveSettings();
this.close();
}
Expand Down
11 changes: 11 additions & 0 deletions src/ui/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ export default class SettingsTab extends PluginSettingTab {
dropdown.setValue(plugin.settings.defaultLanguage)
.onChange(async (value) => {
plugin.settings.defaultLanguage = value as keyof typeof LANGUAGES;
plugin.settings.normalLang = value as keyof typeof LANGUAGES;
await this.save();
this.display();
});
});
new Setting(containerEl)
.setName('Get Language from File')
.setDesc('Use the lang or language key in the frontmatter to set the Language dependent on your open File. Example: lang: "en_US" or language: "de".')
.addToggle(toggle => {
toggle.setValue(this.plugin.settings.getLangFromFile).onChange(async value => {
this.plugin.settings.getLangFromFile = value;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName(t('Definition Provider'))
.setDesc(t('The API the Plugin will use to search for Definitions.'))
Expand Down

0 comments on commit 21c1d24

Please sign in to comment.