chore(react/type_widgets): port and fix follow link under cursor

This commit is contained in:
Elian Doran 2025-09-25 14:35:52 +03:00
parent a975576214
commit d443d79685
No known key found for this signature in database
2 changed files with 30 additions and 31 deletions

View File

@ -11,6 +11,8 @@ import Component from "../../../components/component";
import options from "../../../services/options";
import { loadIncludedNote, refreshIncludedNote } from "./utils";
import getTemplates, { updateTemplateCache } from "./snippets.js";
import appContext from "../../../components/app_context";
import link, { parseNavigationStateFromUrl } from "../../../services/link";
/**
* The editor can operate into two distinct modes:
@ -88,7 +90,34 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
editorApi: editorApiRef.current,
});
},
loadIncludedNote
loadIncludedNote,
async followLinkUnderCursorCommand() {
const editor = await waitForEditor();
const selection = editor?.model.document.selection;
const selectedElement = selection?.getSelectedElement();
if (selectedElement?.name === "reference") {
const { notePath } = parseNavigationStateFromUrl(selectedElement.getAttribute("href") as string | undefined);
if (notePath) {
await appContext.tabManager.getActiveContext()?.setNote(notePath);
return;
}
}
if (!selection?.hasAttribute("linkHref")) {
return;
}
const selectedLinkUrl = selection.getAttribute("linkHref") as string;
const notePath = link.getNotePathFromUrl(selectedLinkUrl);
if (notePath) {
await appContext.tabManager.getActiveContext()?.setNote(notePath);
} else {
window.open(selectedLinkUrl, "_blank");
}
}
});
useTriliumEvent("refreshIncludedNote", ({ noteId }) => {

View File

@ -71,36 +71,6 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
resolve(this.watchdog.editor as CKTextEditor);
}
async followLinkUnderCursorCommand() {
await this.initialized;
const selection = this.watchdog.editor?.model.document.selection;
const selectedElement = selection?.getSelectedElement();
if (selectedElement?.name === "reference") {
// reference link
const notePath = selectedElement.getAttribute("notePath") as string | undefined;
if (notePath) {
await appContext.tabManager.getActiveContext()?.setNote(notePath);
return;
}
}
if (!selection?.hasAttribute("linkHref")) {
return;
}
const selectedLinkUrl = selection.getAttribute("linkHref") as string;
const notePath = link.getNotePathFromUrl(selectedLinkUrl);
if (notePath) {
await appContext.tabManager.getActiveContext()?.setNote(notePath);
} else {
window.open(selectedLinkUrl, "_blank");
}
}
async createNoteForReferenceLink(title: string) {
if (!this.notePath) {
return;