Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/include notes: fix an issue
Adorian Doranf5c8e9ed207b
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.tsindex af9d1b5a7d..91d4e6a3b1 100644--- a/apps/client/src/menus/link_context_menu.spec.ts+++ b/apps/client/src/menus/link_context_menu.spec.ts@@ -308,6 +308,7 @@ describe("openContextMenu", () => {]);mocks.getTextEditorContaining.mockResolvedValue({commands: { get: () => ({ isEnabled: true }) },+ plugins: { get: () => ({ canConvertLinkToEmbed: () => true }) },execute});const editable = document.createElement("div");@@ -339,24 +340,26 @@ describe("openContextMenu", () => {expect(mocks.getTextEditorContaining).toHaveBeenCalledTimes(1);});- it("ends with converting a note link to an embed, but not one inside an embed", async () => {+ it("ends with converting a note link to an embed, if the editor can convert it", async () => {const execute = vi.fn();+ const canConvertLinkToEmbed = vi.fn(() => true);const note = { noteId: "n1" };mocks.getNote.mockResolvedValue(note);mocks.getEmbedBoxSize.mockReturnValue("full");mocks.getTextEditorContaining.mockResolvedValue({commands: { get: () => ({ isEnabled: true }) },+ plugins: { get: () => ({ canConvertLinkToEmbed }) },execute});const editable = document.createElement("div");editable.className = "ck-editor__editable";editable.setAttribute("contenteditable", "true");- editable.innerHTML = `<p><a class="reference-link" href="#root/n1">Note</a></p>`- + `<figure class="include-note"><div class="include-note-content">`- + `<a class="reference-link" href="#root/n1">Note</a></div></figure>`;- const [ link, embeddedLink ] = editable.querySelectorAll("a");+ editable.innerHTML = `<p><a class="reference-link" href="#root/n1">Note</a></p>`;+ const link = editable.querySelector("a");+ if (!link) return;await linkContextMenu.openContextMenu("root/n1", contextMenuEvent(link));+ expect(canConvertLinkToEmbed).toHaveBeenCalledWith(link);const { items } = mocks.show.mock.calls[0][0];expect(items.slice(4)).toMatchObject([{ kind: "separator" },@@ -370,7 +373,9 @@ describe("openContextMenu", () => {boxSize: "full"});- await linkContextMenu.openContextMenu("root/n1", contextMenuEvent(embeddedLink));+ // Such as one with a query, which names a part of the note.+ canConvertLinkToEmbed.mockReturnValue(false);+ await linkContextMenu.openContextMenu("root/n1", contextMenuEvent(link));expect(mocks.show.mock.calls[1][0].items).toHaveLength(4);});diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.tsindex 453e2f2353..b70ce34f21 100644--- a/apps/client/src/menus/link_context_menu.ts+++ b/apps/client/src/menus/link_context_menu.ts@@ -284,8 +284,7 @@ function runEmbedCommand(/*** The item titled `title` that converts a link to a note or an attachment into an embed, in a- * text note open for editing. A link in the content of an embed is not part of the note, and- * offers none.+ * text note open for editing, for a link that the editor can convert.*/async function getConvertToEmbedItem(e: LinkMenuOrigin,@@ -294,8 +293,8 @@ async function getConvertToEmbedItem(getLinkedEntity: () => Promise<FNote | FAttachment | null>): Promise<MenuItem<CommandNames> | null> {const link = getTarget(e)?.closest<HTMLElement>("a.reference-link");- if (!link || link.closest(".include-note")- || !editor?.commands.get("convertLinkToEmbed")?.isEnabled) {+ if (!link || !editor?.commands.get("convertLinkToEmbed")?.isEnabled+ || !editor.plugins.get("ContentEmbed").canConvertLinkToEmbed(link)) {return null;}diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.tsindex 975ce12605..09a33c407e 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts@@ -738,26 +738,49 @@ describe("ContentEmbed with attachments", () => {});it("turns a link to a note into an embed of the note, and leaves anything else alone", () => {+ const cardHref = "#root/boardAbc?card=cardAbc";editor.setData(`<p>Before <a class="reference-link" href="#root/parentAbc/noteAbc">x</a> after</p>`+ + `<p><a class="reference-link" href="${cardHref}">card</a></p>`+ + `<p><a class="reference-link" href="${LINK_HREF}">file</a></p>`);const before = getModelData(editor.model, { withoutSelection: true });const domRoot = editor.editing.view.getDomRoot();+ const [ link, cardLink, attachmentLink ] =+ domRoot?.querySelectorAll<HTMLElement>("a.reference-link") ?? [];const paragraph = domRoot?.querySelector("p");const detached = document.createElement("a");+ if (!link || !cardLink || !attachmentLink || !paragraph) {+ throw new Error("Expected the links and their paragraph.");+ }+ const plugin = editor.plugins.get(ContentEmbed);- for (const domElement of [ paragraph, detached ]) {+ // A note link with a query names a part of the note, which an embed does not show.+ expect([ link, cardLink, attachmentLink, paragraph, detached ]+ .map((domElement) => plugin.canConvertLinkToEmbed(domElement)))+ .toEqual([ true, false, true, false, false ]);+ for (const domElement of [ cardLink, paragraph, detached ]) {editor.execute(CONVERT_LINK_TO_EMBED_COMMAND, { domElement });}expect(getModelData(editor.model, { withoutSelection: true })).toBe(before);- const link = domRoot?.querySelector("a.reference-link");editor.execute(CONVERT_LINK_TO_EMBED_COMMAND, { domElement: link, boxSize: "full" });expect(getModelData(editor.model, { withoutSelection: true })).toBe("<paragraph>Before </paragraph>" +"<contentEmbed boxSize=\"full\" noteId=\"noteAbc\"></contentEmbed>" +- "<paragraph> after</paragraph>"+ "<paragraph> after</paragraph>" ++ `<paragraph><reference href="${cardHref}"></reference></paragraph>` ++ `<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`);++ // A link in the content of an embed is not part of the note.+ const wrapper = domRoot?.querySelector("div.include-note-wrapper");+ if (!wrapper) {+ throw new Error("Expected the wrapper of the embed.");+ }+ wrapper.innerHTML = `<a class="reference-link" href="#root/otherAbc">other</a>`;+ const embeddedLink = wrapper.querySelector("a");+ expect(embeddedLink && plugin.canConvertLinkToEmbed(embeddedLink)).toBe(false);});it("loads an embed that names nothing, as one saved mid-upload, as an empty box", () => {diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex cf59de9b5b..7fa21d0620 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -124,6 +124,11 @@ export default class ContentEmbed extends Plugin {};}+ /** Whether `domElement` is a reference link that `convertLinkToEmbed` turns into an embed. */+ canConvertLinkToEmbed( domElement: HTMLElement ): boolean {+ return !!getLinkToEmbed( this.editor, domElement );+ }+/** The box sizes, with the labels the toolbar gives them. */getBoxSizes(): { value: BoxSizeValue; label: string }[] {return BOX_SIZES.map( value => ( { value, label: getBoxSizeLabel( this.editor.t, value ) } ) );@@ -688,22 +693,15 @@ class ConvertLinkToEmbedCommand extends Command {*/override execute( { domElement, boxSize }: { domElement: HTMLElement; boxSize?: string } ) {const editor = this.editor;- const viewElement = editor.editing.view.domConverter.mapDomToView( domElement );- const reference = viewElement?.is( 'element' )- ? editor.editing.mapper.toModelElement( viewElement )- : undefined;- const href = reference?.is( 'element', 'reference' )- ? reference.getAttribute( 'href' )- : null;- const attachmentId = getAttachmentId( href );- const noteId = getNoteId( href );- if ( !reference || !noteId ) {+ const link = getLinkToEmbed( editor, domElement );+ if ( !link ) {return;}+ const { reference, embedded } = link;editor.model.change( writer => {const embed = writer.createElement( 'contentEmbed', {- ...( attachmentId ? { attachmentId } : { noteId } ),+ ...embedded,boxSize: boxSize ?? 'medium'} );const range = writer.createRangeOn( reference );@@ -752,6 +750,30 @@ class ConvertEmbedToLinkCommand extends Command {}}+/**+ * The reference link that `domElement` renders, and the attributes of an embed showing what it+ * points to, or `null`. A link to a note with a query points to a part of the note, such as a+ * board card, which an embed does not show.+ */+function getLinkToEmbed( editor: Editor, domElement: HTMLElement ) {+ const viewElement = editor.editing.view.domConverter.mapDomToView( domElement );+ const reference = viewElement?.is( 'element' )+ ? editor.editing.mapper.toModelElement( viewElement )+ : undefined;+ if ( !reference?.is( 'element', 'reference' ) ) {+ return null;+ }++ const href = reference.getAttribute( 'href' );+ const attachmentId = getAttachmentId( href );+ if ( attachmentId ) {+ return { reference, embedded: { attachmentId } };+ }++ const noteId = typeof href === 'string' && !href.includes( '?' ) ? getNoteId( href ) : null;+ return noteId ? { reference, embedded: { noteId } } : null;+}+/** The embed the selection is on or inside, or `null`. */function getSelectedContentEmbed( editor: Editor ) {const selection = editor.model.document.selection;