From 6917ed83093b7a7b75912faa88c4f471c34a37a0 Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Wed, 6 Dec 2023 21:56:51 +0900 Subject: [PATCH] Fixed the incompatibility issue with Hover Editor --- README.md | 3 +-- src/main.ts | 28 +++++++++++++++++---- src/settings.ts | 6 ----- src/typings/{suggest.d.ts => obsidian.d.ts} | 7 +++++- 4 files changed, 30 insertions(+), 14 deletions(-) rename src/typings/{suggest.d.ts => obsidian.d.ts} (63%) diff --git a/README.md b/README.md index 92db37d..15567d1 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,7 @@ This is an [Obsidian.md](https://obsidian.md) plugin that enhances the built-in 2. Render markdown in block link suggestions (i.e. `[[note title^` or `[[^`) > [!note] -> - The first feature requires the [page preview](https://help.obsidian.md/Plugins/Page+preview) core plugin enabled. Unfortunately, it's currently incompatible with [Hover Editor](https://github.com/nothingislost/obsidian-hover-editor). -> - The second functionality works whether Hover Editor is enabled or not. +> The first feature requires the [page preview](https://help.obsidian.md/Plugins/Page+preview) core plugin enabled. ## Installation diff --git a/src/main.ts b/src/main.ts index 5ff29e8..2a5ddc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,12 +10,26 @@ type BuiltInAutocompletion = EditorSuggest & { component: Component }; export default class MyPlugin extends Plugin { settings: MyPluginSettings; + originalOnLinkHover: (hoverParent: HoverParent, targetEl: HTMLElement | null, linktext: string, sourcePath: string, state?: any) => any; async onload() { await this.loadSettings(); await this.saveSettings(); this.addSettingTab(new SampleSettingTab(this)); + /** + * Hover Editor completely replaces the core Page Preview plugin's onLinkHover method with its own. + * But Hover Editor's version is incompatible with this plugin, so we need to store the original method + * and call it instead. + */ + if (this.app.plugins.enabledPlugins.has('obsidian-hover-editor')) { + await this.app.plugins.disablePlugin('obsidian-hover-editor'); + this.originalOnLinkHover = this.app.internalPlugins.getPluginById('page-preview').instance.onLinkHover + await this.app.plugins.enablePlugin('obsidian-hover-editor'); + } else { + this.originalOnLinkHover = this.app.internalPlugins.getPluginById('page-preview').instance.onLinkHover + } + this.app.workspace.onLayoutReady(() => { this.patch(); }) @@ -29,6 +43,12 @@ export default class MyPlugin extends Plugin { await this.saveData(this.settings); } + /** Call the core Page Preview plugin's (potentially) original onLinkHover method. */ + onLinkHover(...args: any[]) { + const self = this.app.internalPlugins.getPluginById('page-preview').instance; + return this.originalOnLinkHover.call(self, ...args); + } + patch() { // @ts-ignore const suggest = this.app.workspace.editorSuggest.suggests[0] as BuiltInAutocompletion; @@ -47,17 +67,15 @@ export default class MyPlugin extends Plugin { self.component.load(); self.component.registerDomEvent(window, 'keydown', (event) => { if (suggest.isOpen && Keymap.isModifier(event, plugin.settings.modifierToPreview)) { - if (app.plugins.enabledPlugins.has('obsidian-hover-editor')) return; - const item = suggest.suggestions.values[suggest.suggestions.selectedItem]; const parent = new KeyEventAwareHoverParent(plugin, suggest); self.component.addChild(parent); if (item.type === 'file') { - app.workspace.trigger('link-hover', parent, null, item.file.path, "") + plugin.onLinkHover(parent, null, item.file.path, "") } else if (item.type === 'heading') { - app.workspace.trigger('link-hover', parent, null, item.file.path + '#' + stripHeadingForLink(item.heading), "") + plugin.onLinkHover(parent, null, item.file.path + '#' + stripHeadingForLink(item.heading), "") } else if (item.type === 'block') { - app.workspace.trigger('link-hover', parent, null, item.file.path, "", { scroll: item.node.position.start.line }) + plugin.onLinkHover(parent, null, item.file.path, "", { scroll: item.node.position.start.line }) } } }); diff --git a/src/settings.ts b/src/settings.ts index 4d70fd4..2f0321f 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -108,12 +108,6 @@ export class SampleSettingTab extends PluginSettingTab { display(): void { this.containerEl.empty(); - if (this.app.plugins.enabledPlugins.has('obsidian-hover-editor')) { - new Setting(this.containerEl) - .setDesc('Quick preview is not available because Hover Editor is enabled.') - .then((setting) => setting.settingEl.addClass('alert-hover-editor')); - } - this.addDropdowenSetting('modifierToPreview', ['Mod', 'Ctrl', 'Meta', 'Shift', 'Alt'], getModifierNameInPlatform) .setName('Modifier key for quick preview') .setDesc('Hold down this key to preview the link without clicking.'); diff --git a/src/typings/suggest.d.ts b/src/typings/obsidian.d.ts similarity index 63% rename from src/typings/suggest.d.ts rename to src/typings/obsidian.d.ts index 6db4aab..0fd1112 100644 --- a/src/typings/suggest.d.ts +++ b/src/typings/obsidian.d.ts @@ -4,6 +4,11 @@ declare module "obsidian" { interface App { plugins: { enabledPlugins: Set; + enablePlugin(id: string): Promise; + disablePlugin(id: string): Promise; + } + internalPlugins: { + getPluginById(id: string): Plugin & { instance: any }; } } @@ -14,7 +19,7 @@ declare module "obsidian" { values: T[]; containerEl: HTMLElement; moveUp(event: KeyboardEvent): void; - moveDown(event: KeyboardEvent): void; + moveDown(event: KeyboardEvent): void; }; suggestEl: HTMLElement; isOpen: boolean;