Skip to content

Commit

Permalink
Add settings for footnotes, subheads, and blocks
Browse files Browse the repository at this point in the history
You can now configure Hover Editor to *not* replace the built-in
preview functions for links to footnotes, subheadings, and/or
blocks.

By default, blocks and footnotes will now be displayed
with the built-in preview (with Obsidian 1.6 and higher),
but subheadings will still be displayed in Hover Editor.
(For older Obsidians, the default will be to use Hover Editor
for everything, since before 1.6 the built-in preview didn't
support editing.)

You can change these settings from the defaults using the plugin's
settings page.
  • Loading branch information
pjeby committed Oct 13, 2024
1 parent e37daec commit 8e44ed8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian-hover-editor",
"name": "Hover Editor",
"version": "0.11.20",
"minAppVersion": "1.4.16",
"version": "0.11.21",
"minAppVersion": "1.5.8",
"description": "Transform the Page Preview hover popover into a fully working editor instance",
"author": "NothingIsLost",
"authorUrl": "https://github.com/nothingislost",
Expand Down
19 changes: 18 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MarkdownPreviewView,
MarkdownView,
Menu,
parseLinktext,
Platform,
Plugin,
PopoverState,
Expand Down Expand Up @@ -337,7 +338,7 @@ export default class HoverEditorPlugin extends Plugin {
getDropLocation(old) {
return function getDropLocation(event: MouseEvent) {
for (const popover of HoverEditor.activePopovers()) {
const dropLoc = this.recursiveGetTarget(event, popover.rootSplit);
const dropLoc: any = this.recursiveGetTarget(event, popover.rootSplit);
if (dropLoc) {
if (requireApiVersion && requireApiVersion("0.15.3")) {
// getDropLocation's return signature changed in 0.15.3
Expand Down Expand Up @@ -412,6 +413,22 @@ export default class HoverEditorPlugin extends Plugin {
state: EphemeralState,
...args: unknown[]
) {
const {subpath} = parseLinktext(linkText);
if (subpath && subpath[0] === "#") {
if (subpath.startsWith("#[^")) {
if (plugin.settings.footnotes !== "always") {
return old.call(this, parent, targetEl, linkText, path, state, ...args);
}
} else if (subpath.startsWith("#^")) {
if (plugin.settings.blocks !== "always") {
return old.call(this, parent, targetEl, linkText, path, state, ...args);
}
} else {
if (plugin.settings.headings !== "always") {
return old.call(this, parent, targetEl, linkText, path, state, ...args);
}
}
}
onLinkHover(plugin, parent, targetEl, linkText, path, state, ...args);
};
},
Expand Down
44 changes: 43 additions & 1 deletion src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import { App, PluginSettingTab, Setting, requireApiVersion } from "obsidian";

import HoverEditorPlugin from "../main";
import { parseCssUnitValue } from "../utils/misc";
Expand All @@ -16,6 +16,9 @@ export interface HoverEditorSettings {
showViewHeader: boolean;
imageZoom: boolean;
hoverEmbeds: boolean;
footnotes: "always" | "never";
headings: "always" | "never";
blocks: "always" | "never";
}

export const DEFAULT_SETTINGS: HoverEditorSettings = {
Expand All @@ -31,6 +34,9 @@ export const DEFAULT_SETTINGS: HoverEditorSettings = {
showViewHeader: false,
imageZoom: true,
hoverEmbeds: false,
footnotes: requireApiVersion("1.6") ? "never" : "always",
headings: "always",
blocks: requireApiVersion("1.6") ? "never" : "always",
};

export const modeOptions = {
Expand Down Expand Up @@ -89,6 +95,42 @@ export class SettingTab extends PluginSettingTab {
}),
);

new Setting(containerEl)
.setName("Trigger hover preview on sub-heading links")
.setDesc(
"Use hover editor for links to subheadings, instead of the built-in preview/editor",
)
.addToggle(toggle =>
toggle.setValue(this.plugin.settings.headings === "always").onChange(value => {
this.plugin.settings.headings = value ? "always" : "never";
this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName("Trigger hover preview on block links")
.setDesc(
"Use hover editor for links to blocks, instead of the built-in preview/editor",
)
.addToggle(toggle =>
toggle.setValue(this.plugin.settings.blocks === "always").onChange(value => {
this.plugin.settings.blocks = value ? "always" : "never";
this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName("Trigger hover preview on footnotes")
.setDesc(
"Use hover editor for footnotes, instead of the built-in preview/editor",
)
.addToggle(toggle =>
toggle.setValue(this.plugin.settings.footnotes === "always").onChange(value => {
this.plugin.settings.footnotes = value ? "always" : "never";
this.plugin.saveSettings();
}),
);

new Setting(containerEl)
.setName("Auto Focus")
.setDesc("Set the hover editor as the active pane when opened")
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"0.11.20": "1.5.8",
"0.11.21": "1.5.8",
"0.11.17": "1.4.16",
"0.11.15": "1.3.5",
"0.11.12": "0.15.9",
Expand Down

0 comments on commit 8e44ed8

Please sign in to comment.