rmellmer_obsidian-link-range/main.ts

87 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-03-15 04:04:41 +00:00
import { Plugin } from 'obsidian';
2023-03-01 23:43:50 +00:00
import { around } from "monkey-around";
2023-03-15 04:04:41 +00:00
import { ViewPlugin } from "@codemirror/view";
import { DEFAULT_SETTINGS, LinkRangeSettings, LinkRangeSettingTab } from 'src/settings';
import { linkRangePostProcessor } from 'src/markdownPostProcessor';
import { checkLink } from 'src/utils';
import { LinkRangeView } from 'src/linkRangeView';
2023-03-01 23:43:50 +00:00
export default class LinkRange extends Plugin {
settings: LinkRangeSettings;
2023-03-01 17:25:25 +00:00
async onload() {
await this.loadSettings();
2023-03-01 23:43:50 +00:00
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new LinkRangeSettingTab(this.app, this));
const settings = this.settings;
// on page load, update hrefs to strip off second header to handle clickthrough, and add new range-href field
2023-03-15 04:04:41 +00:00
this.registerMarkdownPostProcessor((el, ctx) => {
linkRangePostProcessor(el, ctx, settings)
2023-03-01 17:25:25 +00:00
});
2023-03-02 00:12:05 +00:00
// wait for layout to be ready
this.app.workspace.onLayoutReady(() => {
this.registerEditorExtension(ViewPlugin.define((v) => {
return new LinkRangeView(this.settings)
}));
2023-03-02 00:12:05 +00:00
const pagePreviewPlugin = this.app.internalPlugins.plugins["page-preview"];
console.log("LinkRange: Hooking into page-preview onHover calls")
2023-03-15 04:04:41 +00:00
2023-03-02 00:12:05 +00:00
// intercept page-preview plugin
const uninstaller = around(pagePreviewPlugin.instance.constructor.prototype, {
onHoverLink(old: Function) {
return function (options: { event: MouseEvent }, ...args: unknown[]) {
return old.call(this, options, ...args);
};
},
onLinkHover(old: Function) {
return function (
parent: any,
targetEl: HTMLElement,
linkText: string,
path: string,
state: any,
...args: unknown[]
) {
// parse link using the added range-href field
2023-03-15 16:26:36 +00:00
const res = checkLink(targetEl, settings, false, "range-href")
2023-03-15 05:50:40 +00:00
if (res !== null) {
2023-03-02 00:12:05 +00:00
old.call(this, parent, targetEl, res.note, path, {scroll:res.h1Line}, ...args)
} else {
old.call(this, parent, targetEl, linkText, path, state, ...args);
}
};
},
});
this.register(uninstaller);
// This will recycle the event handlers so that they pick up the patched onLinkHover method
2023-03-01 23:43:50 +00:00
pagePreviewPlugin.disable();
pagePreviewPlugin.enable();
2023-03-02 00:12:05 +00:00
this.register(function () {
if (!pagePreviewPlugin.enabled) return;
pagePreviewPlugin.disable();
pagePreviewPlugin.enable();
});
2023-03-01 17:25:25 +00:00
});
}
onunload() {
2023-03-15 04:04:41 +00:00
2023-03-01 17:25:25 +00:00
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}