Add example for settings.
This commit is contained in:
parent
318cb2b055
commit
cfe4d17ce7
1 changed files with 35 additions and 6 deletions
41
main.ts
41
main.ts
|
@ -1,9 +1,21 @@
|
||||||
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
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 {
|
export default class MyPlugin extends Plugin {
|
||||||
onload() {
|
settings: MyPluginSettings;
|
||||||
|
|
||||||
|
async onload() {
|
||||||
console.log('loading plugin');
|
console.log('loading plugin');
|
||||||
|
|
||||||
|
await this.loadSettings();
|
||||||
|
|
||||||
this.addRibbonIcon('dice', 'Sample Plugin', () => {
|
this.addRibbonIcon('dice', 'Sample Plugin', () => {
|
||||||
new Notice('This is a notice!');
|
new Notice('This is a notice!');
|
||||||
});
|
});
|
||||||
|
@ -30,9 +42,9 @@ export default class MyPlugin extends Plugin {
|
||||||
|
|
||||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
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);
|
console.log('codemirror', cm);
|
||||||
}));
|
});
|
||||||
|
|
||||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||||
console.log('click', evt);
|
console.log('click', evt);
|
||||||
|
@ -44,6 +56,14 @@ export default class MyPlugin extends Plugin {
|
||||||
onunload() {
|
onunload() {
|
||||||
console.log('unloading plugin');
|
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 {
|
class SampleModal extends Modal {
|
||||||
|
@ -63,6 +83,13 @@ class SampleModal extends Modal {
|
||||||
}
|
}
|
||||||
|
|
||||||
class SampleSettingTab extends PluginSettingTab {
|
class SampleSettingTab extends PluginSettingTab {
|
||||||
|
plugin: MyPlugin;
|
||||||
|
|
||||||
|
constructor(app: App, plugin: MyPlugin) {
|
||||||
|
super(app, plugin);
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
display(): void {
|
display(): void {
|
||||||
let {containerEl} = this;
|
let {containerEl} = this;
|
||||||
|
|
||||||
|
@ -73,11 +100,13 @@ class SampleSettingTab extends PluginSettingTab {
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Setting #1')
|
.setName('Setting #1')
|
||||||
.setDesc('It\'s a secret')
|
.setDesc('It\'s a secret')
|
||||||
.addText(text => text.setPlaceholder('Enter your secret')
|
.addText(text => text
|
||||||
|
.setPlaceholder('Enter your secret')
|
||||||
.setValue('')
|
.setValue('')
|
||||||
.onChange((value) => {
|
.onChange(async (value) => {
|
||||||
console.log('Secret: ' + value);
|
console.log('Secret: ' + value);
|
||||||
|
this.plugin.settings.mySetting = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue