obsidian-semantic

Log | Files | Refs | README

commit cfe4d17ce7dd3346af83e5578d87556f97285951
parent 318cb2b055e06748f56c25c4d4c624eabcababb2
Author: lishid <lishid@gmail.com>
Date:   Tue, 22 Dec 2020 11:24:00 -0500

Add example for settings.

Diffstat:
Mmain.ts | 41+++++++++++++++++++++++++++++++++++------
1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/main.ts b/main.ts @@ -1,9 +1,21 @@ import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; +interface MyPluginSettings { + mySetting: string; +} + +const DEFAULT_SETTINGS: MyPluginSettings = { + mySetting: 'default' +} + export default class MyPlugin extends Plugin { - onload() { + settings: MyPluginSettings; + + async onload() { console.log('loading plugin'); + await this.loadSettings(); + this.addRibbonIcon('dice', 'Sample Plugin', () => { new Notice('This is a notice!'); }); @@ -30,9 +42,9 @@ export default class MyPlugin extends Plugin { this.addSettingTab(new SampleSettingTab(this.app, this)); - this.registerEvent(this.app.on('codemirror', (cm: CodeMirror.Editor) => { + this.registerCodeMirror((cm: CodeMirror.Editor) => { console.log('codemirror', cm); - })); + }); this.registerDomEvent(document, 'click', (evt: MouseEvent) => { console.log('click', evt); @@ -44,6 +56,14 @@ export default class MyPlugin extends Plugin { onunload() { console.log('unloading plugin'); } + + async loadSettings() { + this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData()); + } + + async saveSettings() { + await this.saveData(this.settings); + } } class SampleModal extends Modal { @@ -63,6 +83,13 @@ class SampleModal extends Modal { } class SampleSettingTab extends PluginSettingTab { + plugin: MyPlugin; + + constructor(app: App, plugin: MyPlugin) { + super(app, plugin); + this.plugin = plugin; + } + display(): void { let {containerEl} = this; @@ -73,11 +100,13 @@ class SampleSettingTab extends PluginSettingTab { new Setting(containerEl) .setName('Setting #1') .setDesc('It\'s a secret') - .addText(text => text.setPlaceholder('Enter your secret') + .addText(text => text + .setPlaceholder('Enter your secret') .setValue('') - .onChange((value) => { + .onChange(async (value) => { console.log('Secret: ' + value); + this.plugin.settings.mySetting = value; + await this.plugin.saveSettings(); })); - } }