Skip to content

Commit

Permalink
Fixed the incompatibility issue with Hover Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 6, 2023
1 parent e73b6e8 commit 6917ed8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 23 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ type BuiltInAutocompletion = EditorSuggest<Item> & { 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();
})
Expand All @@ -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;
Expand All @@ -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 })
}
}
});
Expand Down
6 changes: 0 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
7 changes: 6 additions & 1 deletion src/typings/suggest.d.ts → src/typings/obsidian.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ declare module "obsidian" {
interface App {
plugins: {
enabledPlugins: Set<string>;
enablePlugin(id: string): Promise<void>;
disablePlugin(id: string): Promise<void>;
}
internalPlugins: {
getPluginById(id: string): Plugin & { instance: any };
}
}

Expand All @@ -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;
Expand Down

0 comments on commit 6917ed8

Please sign in to comment.