|
| 1 | +import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; |
| 2 | + |
| 3 | +// Remember to rename these classes and interfaces! |
| 4 | + |
| 5 | +interface MyPluginSettings { |
| 6 | + mySetting: string; |
| 7 | +} |
| 8 | + |
| 9 | +const DEFAULT_SETTINGS: MyPluginSettings = { |
| 10 | + mySetting: 'default' |
| 11 | +} |
| 12 | + |
| 13 | +export default class MyPlugin extends Plugin { |
| 14 | + settings: MyPluginSettings; |
| 15 | + |
| 16 | + async onload() { |
| 17 | + await this.loadSettings(); |
| 18 | + |
| 19 | + // This creates an icon in the left ribbon. |
| 20 | + const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => { |
| 21 | + // Called when the user clicks the icon. |
| 22 | + new Notice('This is a notice!'); |
| 23 | + }); |
| 24 | + // Perform additional things with the ribbon |
| 25 | + ribbonIconEl.addClass('my-plugin-ribbon-class'); |
| 26 | + |
| 27 | + // This adds a status bar item to the bottom of the app. Does not work on mobile apps. |
| 28 | + const statusBarItemEl = this.addStatusBarItem(); |
| 29 | + statusBarItemEl.setText('Status Bar Text'); |
| 30 | + |
| 31 | + // This adds a simple command that can be triggered anywhere |
| 32 | + this.addCommand({ |
| 33 | + id: 'open-sample-modal-simple', |
| 34 | + name: 'Open sample modal (simple)', |
| 35 | + callback: () => { |
| 36 | + new SampleModal(this.app).open(); |
| 37 | + } |
| 38 | + }); |
| 39 | + // This adds an editor command that can perform some operation on the current editor instance |
| 40 | + this.addCommand({ |
| 41 | + id: 'sample-editor-command', |
| 42 | + name: 'Sample editor command', |
| 43 | + editorCallback: (editor: Editor, view: MarkdownView) => { |
| 44 | + console.log(editor.getSelection()); |
| 45 | + editor.replaceSelection('Sample Editor Command'); |
| 46 | + } |
| 47 | + }); |
| 48 | + // This adds a complex command that can check whether the current state of the app allows execution of the command |
| 49 | + this.addCommand({ |
| 50 | + id: 'open-sample-modal-complex', |
| 51 | + name: 'Open sample modal (complex)', |
| 52 | + checkCallback: (checking: boolean) => { |
| 53 | + // Conditions to check |
| 54 | + const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); |
| 55 | + if (markdownView) { |
| 56 | + // If checking is true, we're simply "checking" if the command can be run. |
| 57 | + // If checking is false, then we want to actually perform the operation. |
| 58 | + if (!checking) { |
| 59 | + new SampleModal(this.app).open(); |
| 60 | + } |
| 61 | + |
| 62 | + // This command will only show up in Command Palette when the check function returns true |
| 63 | + return true; |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + // This adds a settings tab so the user can configure various aspects of the plugin |
| 69 | + this.addSettingTab(new SampleSettingTab(this.app, this)); |
| 70 | + |
| 71 | + // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) |
| 72 | + // Using this function will automatically remove the event listener when this plugin is disabled. |
| 73 | + this.registerDomEvent(document, 'click', (evt: MouseEvent) => { |
| 74 | + console.log('click', evt); |
| 75 | + }); |
| 76 | + |
| 77 | + // When registering intervals, this function will automatically clear the interval when the plugin is disabled. |
| 78 | + this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); |
| 79 | + } |
| 80 | + |
| 81 | + onunload() { |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + async loadSettings() { |
| 86 | + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); |
| 87 | + } |
| 88 | + |
| 89 | + async saveSettings() { |
| 90 | + await this.saveData(this.settings); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +class SampleModal extends Modal { |
| 95 | + constructor(app: App) { |
| 96 | + super(app); |
| 97 | + } |
| 98 | + |
| 99 | + onOpen() { |
| 100 | + const {contentEl} = this; |
| 101 | + contentEl.setText('Woah!'); |
| 102 | + } |
| 103 | + |
| 104 | + onClose() { |
| 105 | + const {contentEl} = this; |
| 106 | + contentEl.empty(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class SampleSettingTab extends PluginSettingTab { |
| 111 | + plugin: MyPlugin; |
| 112 | + |
| 113 | + constructor(app: App, plugin: MyPlugin) { |
| 114 | + super(app, plugin); |
| 115 | + this.plugin = plugin; |
| 116 | + } |
| 117 | + |
| 118 | + display(): void { |
| 119 | + const {containerEl} = this; |
| 120 | + |
| 121 | + containerEl.empty(); |
| 122 | + |
| 123 | + containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); |
| 124 | + |
| 125 | + new Setting(containerEl) |
| 126 | + .setName('Setting #1') |
| 127 | + .setDesc('It\'s a secret') |
| 128 | + .addText(text => text |
| 129 | + .setPlaceholder('Enter your secret') |
| 130 | + .setValue(this.plugin.settings.mySetting) |
| 131 | + .onChange(async (value) => { |
| 132 | + console.log('Secret: ' + value); |
| 133 | + this.plugin.settings.mySetting = value; |
| 134 | + await this.plugin.saveSettings(); |
| 135 | + })); |
| 136 | + } |
| 137 | +} |
0 commit comments