swarnam-obsidian/main.ts

113 lines
2.4 KiB
TypeScript
Raw Normal View History

import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
2020-10-25 20:55:59 +00:00
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() {
2020-10-25 20:55:59 +00:00
console.log('loading plugin');
2020-12-22 16:24:00 +00:00
await this.loadSettings();
2020-10-25 20:55:59 +00:00
this.addRibbonIcon('dice', 'Sample Plugin', () => {
new Notice('This is a notice!');
});
this.addStatusBarItem().setText('Status Bar Text');
this.addCommand({
id: 'open-sample-modal',
name: 'Open Sample Modal',
// callback: () => {
// console.log('Simple Callback');
// },
checkCallback: (checking: boolean) => {
let leaf = this.app.workspace.activeLeaf;
if (leaf) {
if (!checking) {
new SampleModal(this.app).open();
}
return true;
}
return false;
}
});
this.addSettingTab(new SampleSettingTab(this.app, this));
2020-10-29 21:36:03 +00:00
2020-12-22 16:24:00 +00:00
this.registerCodeMirror((cm: CodeMirror.Editor) => {
2020-10-29 22:02:57 +00:00
console.log('codemirror', cm);
2020-12-22 16:24:00 +00:00
});
2020-10-29 22:02:57 +00:00
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
2020-10-25 20:55:59 +00:00
}
onunload() {
console.log('unloading plugin');
}
2020-12-22 16:24:00 +00:00
async loadSettings() {
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData());
}
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() {
let {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
let {contentEl} = this;
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 {
let {containerEl} = this;
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')
2020-10-25 20:55:59 +00:00
.setValue('')
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
}));
}
}