From a94573fdf6fa36f33fffb6202d6cd35d4fff0d5b Mon Sep 17 00:00:00 2001 From: Evan Harmon Date: Fri, 20 Oct 2023 16:31:55 -0500 Subject: [PATCH] Add settings etc. --- main.ts | 154 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 89 insertions(+), 65 deletions(-) diff --git a/main.ts b/main.ts index 2a904b0..be992ff 100644 --- a/main.ts +++ b/main.ts @@ -3,39 +3,64 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Set // Remember to rename these classes and interfaces! interface WikipediaDataSettings { - mySetting: string; + template: string; + shouldBoldSearchTerm: boolean; + language: string; } +interface WikiData { + title: string; + text: string; + url: string; + image: string; +} + +const extractApiUrl = "wikipedia.org/api/rest_v1/page/summary/"; + const DEFAULT_SETTINGS: WikipediaDataSettings = { - mySetting: 'default' + template: `{{text}}\n> [Wikipedia]({{url}})`, + shouldBoldSearchTerm: true, + language: "en", } export default class WikipediaData extends Plugin { settings: WikipediaDataSettings; + getLanguage(): string { + return this.settings.language ? this.settings.language : "en"; + } + + getUrl(title: string): string { + return `https://${this.getLanguage()}.wikipedia.org/wiki/${encodeURI( + title + )}`; + } + + getApiUrl(): string { + return `https://${this.getLanguage()}.` + extractApiUrl; + } + + async getWikipediaText(title: string): Promise { + const url = this.getApiUrl() + encodeURIComponent(title); + const requestParam: RequestParam = { + url: url, + }; + const resp = await request(requestParam) + .then((r) => JSON.parse(r)) + .catch( + () => + new Notice( + "Failed to get Wikipedia. Check your internet connection or language prefix." + ) + ); + const extract = this.parseResponse(resp); + return extract; + } + async onload() { + console.log("Loading Wikipedia data plugin"); await this.loadSettings(); - // This creates an icon in the left ribbon. - const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => { - // Called when the user clicks the icon. - new Notice('This is a notice Hello World!'); - }); - // Perform additional things with the ribbon - ribbonIconEl.addClass('my-plugin-ribbon-class'); - - // This adds a status bar item to the bottom of the app. Does not work on mobile apps. - const statusBarItemEl = this.addStatusBarItem(); - statusBarItemEl.setText('Status Bar Text'); - - // This adds a simple command that can be triggered anywhere - this.addCommand({ - id: 'open-sample-modal-simple', - name: 'Open sample modal (simple)', - callback: () => { - new SampleModal(this.app).open(); - } - }); // This adds an editor command that can perform some operation on the current editor instance this.addCommand({ id: 'sample-editor-command', @@ -45,37 +70,10 @@ export default class WikipediaData extends Plugin { editor.replaceSelection('Sample Editor Command'); } }); - // This adds a complex command that can check whether the current state of the app allows execution of the command - this.addCommand({ - id: 'open-sample-modal-complex', - name: 'Open sample modal (complex)', - checkCallback: (checking: boolean) => { - // Conditions to check - const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); - if (markdownView) { - // If checking is true, we're simply "checking" if the command can be run. - // If checking is false, then we want to actually perform the operation. - if (!checking) { - new SampleModal(this.app).open(); - } - - // This command will only show up in Command Palette when the check function returns true - return true; - } - } - }); // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new WikipediaDataSettingTab(this.app, this)); - // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) - // Using this function will automatically remove the event listener when this plugin is disabled. - this.registerDomEvent(document, 'click', (evt: MouseEvent) => { - console.log('click', evt); - }); - - // When registering intervals, this function will automatically clear the interval when the plugin is disabled. - this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); } onunload() { @@ -91,22 +89,6 @@ export default class WikipediaData extends Plugin { } } -class SampleModal extends Modal { - constructor(app: App) { - super(app); - } - - onOpen() { - const {contentEl} = this; - contentEl.setText('Woah!'); - } - - onClose() { - const {contentEl} = this; - contentEl.empty(); - } -} - class WikipediaDataSettingTab extends PluginSettingTab { plugin: WikipediaData; @@ -130,5 +112,47 @@ class WikipediaDataSettingTab extends PluginSettingTab { this.plugin.settings.mySetting = value; await this.plugin.saveSettings(); })); + + new Setting(containerEl) + .setName("Wikipedia Language Prefix") + .setDesc(`Choose Wikipedia language prefix to use (ex. en for English)`) + .addText((textField) => { + textField + .setValue(this.plugin.settings.language) + .onChange(async (value) => { + this.plugin.settings.language = value; + await this.plugin.saveSettings(); + }); + }); + + new Setting(containerEl) + .setName("Wikipedia Extract Template") + .setDesc( + `Set markdown template for extract to be inserted.\n + Available template variables are {{text}}, {{searchTerm}} and {{url}}. + ` + ) + .addTextArea((textarea) => + textarea + .setValue(this.plugin.settings.template) + .onChange(async (value) => { + this.plugin.settings.template = value; + await this.plugin.saveSettings(); + }) + ); + + new Setting(containerEl) + .setName("Bold Search Term?") + .setDesc( + "If set to true, the first instance of the search term will be **bolded**" + ) + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.shouldBoldSearchTerm) + .onChange(async (value) => { + this.plugin.settings.shouldBoldSearchTerm = value; + await this.plugin.saveSettings(); + }) + ); } }