Add example for settings.

This commit is contained in:
lishid 2020-12-22 11:24:00 -05:00
parent 318cb2b055
commit cfe4d17ce7

41
main.ts
View File

@ -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();
}));
}
}