import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, EditorChange, EditorPosition, EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, TFile } from 'obsidian'; // Remember to rename these classes and interfaces! interface MyPluginSettings { mySetting: string; } const DEFAULT_SETTINGS: MyPluginSettings = { mySetting: 'default' } export default class MyPlugin extends Plugin { settings: MyPluginSettings; suggester: DataviewSuggester async onload() { await this.loadSettings(); // register suggester this.suggester = new DataviewSuggester(this); this.registerEditorSuggest(this.suggester); this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); } onunload() { } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } async saveSettings() { await this.saveData(this.settings); } } class SampleSettingTab extends PluginSettingTab { plugin: MyPlugin; constructor(app: App, plugin: MyPlugin) { super(app, plugin); this.plugin = plugin; } display(): void { const {containerEl} = this; containerEl.empty(); new Setting(containerEl) .setName('Setting #1') .setDesc('It\'s a secret') .addText(text => text .setPlaceholder('Enter your secret') .setValue(this.plugin.settings.mySetting) .onChange(async (value) => { this.plugin.settings.mySetting = value; await this.plugin.saveSettings(); })); } } class DataviewSuggester extends EditorSuggest { constructor(plugin: Plugin) { super(plugin.app) } onTrigger( cursor: EditorPosition, editor: Editor, file: TFile, ): EditorSuggestTriggerInfo | null { const line = editor.getLine(cursor.line); const regex = /\((.*?)\)/g let match while((match = regex.exec(line)) !== null) { // check if cursor is positioned inside the match const cursorInMatch = cursor.ch > match.index && cursor.ch <= match.index + match[1].length + 1 if (cursorInMatch) { return { start: { line: cursor.line, ch: match.index+1 }, end: { line: cursor.line, ch: match.index + match[1].length+1 }, query: match[1], }; } } return null; } getSuggestions(context: EditorSuggestContext): string[] | Promise { return ["hello", "world"] } renderSuggestion(value: string, el: HTMLElement): void { el.setText(value) } selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void { console.log("selected", value) const { editor, start, end } = this.context!; editor.replaceRange(value, start, end); const newCursorPos = { line: end.line + 1, ch: 0, }; editor.setCursor(newCursorPos); } }