Skip to content

Build your personal knowledge base with Trilium Notes

chore(text): read the anchor link's note from editor config

Elian Doranb8d25d4f1f09
diff --git a/.claude/skills/ckeditor5-testing/references/patterns.md b/.claude/skills/ckeditor5-testing/references/patterns.md
index cbe8ce6b39..5b6390f87b 100644
--- a/.claude/skills/ckeditor5-testing/references/patterns.md
+++ b/.claude/skills/ckeditor5-testing/references/patterns.md
@@ -45,7 +45,7 @@ assert on it can omit it. A spec sharing one editor across tests can swap an ent
`editor.config.set()`, which the plugins read at call time.
Many in-aggregate plugins still reach for a global `glob` (the Trilium bridge — typed in
-`packages/ckeditor5/src/augmentation.ts` with `getComponentByEl` and `getActiveContextNote`), some
+`packages/ckeditor5/src/augmentation.ts` with `getComponentByEl`, which finds the host component), some
buttons hit `navigator.clipboard`, and some converters call jQuery `$(...)` (e.g. the `editingDowncast`
converter in `packages/ckeditor5/src/plugins/referencelink.ts`). Use the globals kit
(`test/globals-test-kit.ts`) so each stub **tears itself down** after the test — never hand-roll
diff --git a/apps/client/src/services/glob.ts b/apps/client/src/services/glob.ts
index de9b8c87e4..328e708f72 100644
--- a/apps/client/src/services/glob.ts
+++ b/apps/client/src/services/glob.ts
@@ -15,7 +15,7 @@ function setupGlobs() {
window.glob.getReferenceLinkTitleSync = (href, storedTitle) =>
linkService.getReferenceLinkTitleSync(href, storedTitle);
- // required for ESLint plugin and CKEditor
+ // required for ESLint plugin
window.glob.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
window.glob.appContext = appContext; // for debugging
window.glob.froca = froca;
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index 9f196aec23..d11b1f3cd9 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -319,9 +319,6 @@ export default function EditableText({
return convertImageToDrawing(note.noteId, src, parentComponent?.componentId);
},
getAttachmentHref,
- getNoteId() {
- return note.noteId;
- },
openContentEmbedMenu,
openBlockHandleMenu(event: MouseEvent, count: number) {
const shareLink = getNoteShareLink(note);
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index 5b8e93f4e7..1fe9a3eabd 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -137,8 +137,6 @@ declare global {
): void;
/** The href of a reference link to the attachment, or `null` once it is deleted. */
getAttachmentHref(attachmentId: string): Promise<string | null>;
- /** The note the editor holds. Hosts without a note of their own leave it out. */
- getNoteId?(): string | undefined;
/**
* Opens the context menu of what `embed` shows, below `anchor`. Hosts without embeds
* leave it out.
@@ -178,9 +176,6 @@ declare global {
var glob: {
getComponentByEl<T extends Component>(el: unknown): T;
- getActiveContextNote(): {
- noteId: string;
- };
};
}
diff --git a/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts b/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
index 37d4f06cd7..5c1a417fce 100644
--- a/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
+++ b/packages/ckeditor5/src/plugins/copy_anchor_link.spec.ts
@@ -2,28 +2,21 @@ import { _setModelData as setModelData, Bold, Bookmark, ClassicEditor, Essential
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
-import { installGlobMock } from "../../test/globals-test-kit.js";
import CopyAnchorLinkButton from "./copy_anchor_link.js";
describe("CopyAnchorLinkButton", () => {
let editor: ClassicEditor;
- let getActiveContextNote: ReturnType<typeof vi.fn>;
+ let getNoteId: ReturnType<typeof vi.fn<() => string | undefined>>;
let getReferenceLinkTitleSync: ReturnType<typeof vi.fn>;
- let getComponentByEl: ReturnType<typeof vi.fn>;
let copyHtml: ReturnType<typeof vi.fn>;
beforeEach(async () => {
- getActiveContextNote = vi.fn(() => ({ noteId: "noteAbc" }));
+ getNoteId = vi.fn<() => string | undefined>(() => "noteAbc");
getReferenceLinkTitleSync = vi.fn(() => "Some title");
- getComponentByEl = vi.fn(() => ({}));
- installGlobMock({
- getActiveContextNote,
- getComponentByEl
- });
-
copyHtml = vi.fn();
editor = await createTestEditor([Essentials, Paragraph, Bold, Bookmark, CopyAnchorLinkButton], {
+ getNoteId,
clipboard: { copy: vi.fn(), copyHtml },
referenceLink: { getTitle: async () => "Some title", getTitleSync: getReferenceLinkTitleSync }
});
@@ -48,14 +41,13 @@ describe("CopyAnchorLinkButton", () => {
expect(copyHtml).toHaveBeenCalledWith(`<a class="reference-link" href="${href}">Some title</a>`, href);
});
- it("links to the note of the editor, which can differ from the active one", () => {
- getComponentByEl.mockReturnValue({ getNoteId: () => "includedNote" });
+ it("links to the note the editor holds when the button is pressed", () => {
+ getNoteId.mockReturnValue("includedNote");
setModelData(editor.model, "<paragraph>[<bookmark bookmarkId=\"anchor\"></bookmark>]</paragraph>");
getButton().fire("execute");
const href = "#root/includedNote?bookmark=anchor";
- expect(getComponentByEl).toHaveBeenCalledWith(editor.editing.view.getDomRoot());
expect(copyHtml).toHaveBeenCalledWith(`<a class="reference-link" href="${href}">Some title</a>`, href);
});
@@ -77,7 +69,7 @@ describe("CopyAnchorLinkButton", () => {
getButton().fire("execute");
- expect(getActiveContextNote).not.toHaveBeenCalled();
+ expect(getNoteId).not.toHaveBeenCalled();
expect(copyHtml).not.toHaveBeenCalled();
});
@@ -86,12 +78,12 @@ describe("CopyAnchorLinkButton", () => {
getButton().fire("execute");
- expect(getActiveContextNote).not.toHaveBeenCalled();
+ expect(getNoteId).not.toHaveBeenCalled();
expect(copyHtml).not.toHaveBeenCalled();
});
- it("does nothing when there is no active context note", () => {
- getActiveContextNote.mockReturnValue(undefined);
+ it("does nothing when the editor names no note", () => {
+ getNoteId.mockReturnValue(undefined);
setModelData(editor.model, "<paragraph>[<bookmark bookmarkId=\"anchor\"></bookmark>]</paragraph>");
getButton().fire("execute");
diff --git a/packages/ckeditor5/src/plugins/copy_anchor_link.ts b/packages/ckeditor5/src/plugins/copy_anchor_link.ts
index 1ca7282d2c..478464c005 100644
--- a/packages/ckeditor5/src/plugins/copy_anchor_link.ts
+++ b/packages/ckeditor5/src/plugins/copy_anchor_link.ts
@@ -31,11 +31,7 @@ export default class CopyAnchorLinkButton extends Plugin {
if (selectedElement?.name === "bookmark") {
const bookmarkId = selectedElement.getAttribute("bookmarkId") as string;
- // The note the editor holds, which is not the active tab's in an included note.
- const component = glob.getComponentByEl<EditorComponent>(
- editor.editing.view.getDomRoot()
- );
- const noteId = component?.getNoteId?.() ?? glob.getActiveContextNote()?.noteId;
+ const noteId = editor.config.get("getNoteId")?.();
if (noteId && bookmarkId) {
const href = `#root/${noteId}?bookmark=${encodeURIComponent(bookmarkId)}`;
diff --git a/packages/ckeditor5/src/plugins/uploadimage.ts b/packages/ckeditor5/src/plugins/uploadimage.ts
index 85d5e26a05..6915b5e896 100644
--- a/packages/ckeditor5/src/plugins/uploadimage.ts
+++ b/packages/ckeditor5/src/plugins/uploadimage.ts
@@ -265,7 +265,7 @@ export interface AttachmentUploadHost {
declare module "ckeditor5" {
interface EditorConfig {
- /** The ID of the note the editor holds, which uploads attach to. */
+ /** The ID of the note the editor holds, which uploads attach to and anchor links point to. */
getNoteId?(): string | undefined;
attachmentUpload?: Partial<AttachmentUploadHost>;
}