|
| 1 | +import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; |
| 2 | + |
| 3 | +interface MyPluginSettings { |
| 4 | + mySetting: string; |
| 5 | +} |
| 6 | + |
| 7 | +const DEFAULT_SETTINGS: MyPluginSettings = { |
| 8 | + mySetting: 'default' |
| 9 | +} |
| 10 | + |
| 11 | +export default class MyPlugin extends Plugin { |
| 12 | + settings: MyPluginSettings; |
| 13 | + |
| 14 | + async onload() { |
| 15 | + console.log('loading plugin'); |
| 16 | + |
| 17 | + await this.loadSettings(); |
| 18 | + |
| 19 | + this.addRibbonIcon('dice', 'Sample Plugin', () => { |
| 20 | + new Notice('This is a notice!'); |
| 21 | + }); |
| 22 | + |
| 23 | + this.addStatusBarItem().setText('Status Bar Text'); |
| 24 | + |
| 25 | + this.addCommand({ |
| 26 | + id: 'open-sample-modal', |
| 27 | + name: 'Open Sample Modal', |
| 28 | + // callback: () => { |
| 29 | + // console.log('Simple Callback'); |
| 30 | + // }, |
| 31 | + checkCallback: (checking: boolean) => { |
| 32 | + let leaf = this.app.workspace.activeLeaf; |
| 33 | + if (leaf) { |
| 34 | + if (!checking) { |
| 35 | + new SampleModal(this.app).open(); |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + this.addSettingTab(new SampleSettingTab(this.app, this)); |
| 44 | + |
| 45 | + this.registerCodeMirror((cm: CodeMirror.Editor) => { |
| 46 | + console.log('codemirror', cm); |
| 47 | + }); |
| 48 | + |
| 49 | + this.registerDomEvent(document, 'click', (evt: MouseEvent) => { |
| 50 | + console.log('click', evt); |
| 51 | + }); |
| 52 | + |
| 53 | + this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); |
| 54 | + } |
| 55 | + |
| 56 | + onunload() { |
| 57 | + console.log('unloading plugin'); |
| 58 | + } |
| 59 | + |
| 60 | + async loadSettings() { |
| 61 | + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); |
| 62 | + } |
| 63 | + |
| 64 | + async saveSettings() { |
| 65 | + await this.saveData(this.settings); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class SampleModal extends Modal { |
| 70 | + constructor(app: App) { |
| 71 | + super(app); |
| 72 | + } |
| 73 | + |
| 74 | + onOpen() { |
| 75 | + let {contentEl} = this; |
| 76 | + contentEl.setText('Woah!'); |
| 77 | + } |
| 78 | + |
| 79 | + onClose() { |
| 80 | + let {contentEl} = this; |
| 81 | + contentEl.empty(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class SampleSettingTab extends PluginSettingTab { |
| 86 | + plugin: MyPlugin; |
| 87 | + |
| 88 | + constructor(app: App, plugin: MyPlugin) { |
| 89 | + super(app, plugin); |
| 90 | + this.plugin = plugin; |
| 91 | + } |
| 92 | + |
| 93 | + display(): void { |
| 94 | + let {containerEl} = this; |
| 95 | + |
| 96 | + containerEl.empty(); |
| 97 | + |
| 98 | + containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); |
| 99 | + |
| 100 | + new Setting(containerEl) |
| 101 | + .setName('Setting #1') |
| 102 | + .setDesc('It\'s a secret') |
| 103 | + .addText(text => text |
| 104 | + .setPlaceholder('Enter your secret') |
| 105 | + .setValue('') |
| 106 | + .onChange(async (value) => { |
| 107 | + console.log('Secret: ' + value); |
| 108 | + this.plugin.settings.mySetting = value; |
| 109 | + await this.plugin.saveSettings(); |
| 110 | + })); |
| 111 | + } |
| 112 | +} |
0 commit comments