swarnam-obsidian/main.ts

138 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-10-19 17:34:19 +00:00
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
2020-10-25 20:55:59 +00:00
// Remember to rename these classes and interfaces!
2020-12-22 16:24:00 +00:00
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}
export default class MyPlugin extends Plugin {
2020-12-22 16:24:00 +00:00
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
2021-10-19 17:34:19 +00:00
// This creates an icon in the left ribbon.
2021-10-30 18:04:07 +00:00
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
2021-10-19 17:34:19 +00:00
// Called when the user clicks the icon.
2020-10-25 20:55:59 +00:00
new Notice('This is a notice!');
});
2021-10-19 17:34:19 +00:00
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
2020-10-25 20:55:59 +00:00
2021-10-19 17:34:19 +00:00
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
2021-10-30 18:04:07 +00:00
const statusBarItemEl = this.addStatusBarItem();
2021-10-19 17:34:19 +00:00
statusBarItemEl.setText('Status Bar Text');
2020-10-25 20:55:59 +00:00
2021-10-19 17:34:19 +00:00
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new SampleModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
2020-10-25 20:55:59 +00:00
this.addCommand({
2021-10-19 17:34:19 +00:00
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'open-sample-modal-complex',
name: 'Open sample modal (complex)',
2020-10-25 20:55:59 +00:00
checkCallback: (checking: boolean) => {
2021-10-19 17:34:19 +00:00
// Conditions to check
2021-10-30 18:04:07 +00:00
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
2021-10-19 17:34:19 +00:00
if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation.
2020-10-25 20:55:59 +00:00
if (!checking) {
new SampleModal(this.app).open();
}
2021-10-19 17:34:19 +00:00
// This command will only show up in Command Palette when the check function returns true
2020-10-25 20:55:59 +00:00
return true;
}
}
});
2021-10-19 17:34:19 +00:00
// This adds a settings tab so the user can configure various aspects of the plugin
2020-10-25 20:55:59 +00:00
this.addSettingTab(new SampleSettingTab(this.app, this));
2020-10-29 21:36:03 +00:00
2021-10-19 17:34:19 +00:00
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
2020-10-29 22:02:57 +00:00
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});
2021-10-19 17:34:19 +00:00
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
2020-10-29 22:02:57 +00:00
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
2020-10-25 20:55:59 +00:00
}
onunload() {
2021-10-19 17:34:19 +00:00
2020-10-25 20:55:59 +00:00
}
2020-12-22 16:24:00 +00:00
async loadSettings() {
2021-01-24 20:41:47 +00:00
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
2020-12-22 16:24:00 +00:00
}
async saveSettings() {
await this.saveData(this.settings);
}
2020-10-25 20:55:59 +00:00
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
2021-10-30 18:04:07 +00:00
const {contentEl} = this;
2020-10-25 20:55:59 +00:00
contentEl.setText('Woah!');
}
onClose() {
2021-10-30 18:04:07 +00:00
const {contentEl} = this;
2020-10-25 20:55:59 +00:00
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
2020-12-22 16:24:00 +00:00
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
2020-10-25 20:55:59 +00:00
display(): void {
2021-10-30 18:04:07 +00:00
const {containerEl} = this;
2020-10-25 20:55:59 +00:00
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
2020-12-22 16:24:00 +00:00
.addText(text => text
.setPlaceholder('Enter your secret')
2021-10-10 09:13:28 +00:00
.setValue(this.plugin.settings.mySetting)
2020-12-22 16:24:00 +00:00
.onChange(async (value) => {
2020-10-25 20:55:59 +00:00
console.log('Secret: ' + value);
2020-12-22 16:24:00 +00:00
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
2020-10-25 20:55:59 +00:00
}));
}
}