Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/include notes: fix some issues

Adorian Doran68afc5940619
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.ts
index 59917374a9..a0a56b253a 100644
--- a/apps/client/src/menus/link_context_menu.spec.ts
+++ b/apps/client/src/menus/link_context_menu.spec.ts
@@ -295,14 +295,15 @@ describe("openContextMenu", () => {
describe("from the title row of an attachment embed", () => {
const execute = vi.fn();
const selectIncludeAt = vi.fn();
- const convertCommand = { isEnabled: true };
let hasPlugin = true;
+ let hasCommand = true;
let editable: HTMLElement;
beforeEach(() => {
- selectIncludeAt.mockReturnValue(true);
- convertCommand.isEnabled = true;
+ selectIncludeAt.mockReset().mockReturnValue(true);
+ execute.mockClear();
hasPlugin = true;
+ hasCommand = true;
mocks.getAttachmentOfNote.mockResolvedValue({ attachmentId: "att-1" });
mocks.getAttachmentActionGroups.mockReturnValue([
[ { title: "Download", icon: "bx bx-download", run: vi.fn() } ]
@@ -310,8 +311,8 @@ describe("openContextMenu", () => {
mocks.getTextEditorContaining.mockResolvedValue({
plugins: { has: () => hasPlugin, get: () => ({ selectIncludeAt }) },
commands: {
- get: (name: string) => (name === "convertEmbedToLink"
- ? convertCommand
+ get: (name: string) => (hasCommand && name === "convertEmbedToLink"
+ ? { isEnabled: true }
: undefined)
},
execute
@@ -319,7 +320,7 @@ describe("openContextMenu", () => {
editable = document.createElement("div");
editable.className = "ck-editor__editable";
editable.setAttribute("contenteditable", "true");
- editable.innerHTML = `<section class="include-note">`
+ editable.innerHTML = `<section class="include-note" data-attachment-id="att-1">`
+ `<div class="include-note-title-row">`
+ `<h4 class="include-note-title"><span><a href="#">report.pdf</a></span></h4>`
+ `<button class="include-note-menu"></button></div></section>`;
@@ -332,42 +333,54 @@ describe("openContextMenu", () => {
return mocks.show.mock.lastCall?.[0].items;
}
- it("ends the menu with converting the embed to a link, selecting it first", async () => {
+ it("ends the menu with converting the embed, selected only once chosen", async () => {
+ const titleRow = editable.querySelector(".include-note-title-row");
+
// Right-clicking the title, then pressing the menu button beside it.
for (const selector of [ "a", "button.include-note-menu" ]) {
selectIncludeAt.mockClear();
execute.mockClear();
const items = await openOnTitle(selector);
- const titleRow = editable.querySelector(".include-note-title-row");
- expect(selectIncludeAt).toHaveBeenCalledWith(titleRow);
expect(items.slice(4)).toMatchObject([
{ kind: "separator" },
{ title: "Download" },
{ title: "link_context_menu.convert_embed_to_link", uiIcon: "bx bx-link" }
]);
+ expect(selectIncludeAt).not.toHaveBeenCalled();
+
items.at(-1).handler();
+ expect(selectIncludeAt).toHaveBeenCalledWith(titleRow);
expect(execute).toHaveBeenCalledWith("convertEmbedToLink");
}
+
+ // An embed the editor cannot select is left as it is.
+ selectIncludeAt.mockReturnValue(false);
+ execute.mockClear();
+ (await openOnTitle()).at(-1).handler();
+ expect(execute).not.toHaveBeenCalled();
});
- it("leaves it out in a read-only note, or for an embed it cannot convert", async () => {
+ it("leaves it out in a read-only note, or outside an attachment embed", async () => {
hasPlugin = false;
expect(await openOnTitle()).toHaveLength(6);
- expect(selectIncludeAt).not.toHaveBeenCalled();
hasPlugin = true;
- selectIncludeAt.mockReturnValue(false);
+ hasCommand = false;
expect(await openOnTitle()).toHaveLength(6);
- selectIncludeAt.mockReturnValue(true);
- convertCommand.isEnabled = false;
+ // A title row outside an attachment embed is not looked up in the editor at all.
+ hasCommand = true;
+ const section = editable.querySelector("section");
+ section?.removeAttribute("data-attachment-id");
expect(await openOnTitle()).toHaveLength(6);
+ expect(mocks.getTextEditorContaining).toHaveBeenCalledTimes(2);
- convertCommand.isEnabled = true;
+ section?.setAttribute("data-attachment-id", "att-1");
editable.removeAttribute("contenteditable");
expect(await openOnTitle()).toHaveLength(6);
- expect(mocks.getTextEditorContaining).toHaveBeenCalledTimes(3);
+ expect(mocks.getTextEditorContaining).toHaveBeenCalledTimes(2);
+ expect(selectIncludeAt).not.toHaveBeenCalled();
});
});
diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.ts
index 6db69a97fb..ac912c244e 100644
--- a/apps/client/src/menus/link_context_menu.ts
+++ b/apps/client/src/menus/link_context_menu.ts
@@ -161,18 +161,23 @@ async function getConvertToLinkItem(e: ContextMenuEvent): Promise<MenuItem<Comma
const titleRow = e.target instanceof Element
? e.target.closest<HTMLElement>(".include-note-title-row")
: null;
- const editor = await getEditingTextEditor(titleRow);
- // `convertEmbedToLink` acts on the selected embed, so the embed of the menu is selected first.
+ const editor = titleRow?.closest("section.include-note[data-attachment-id]")
+ ? await getEditingTextEditor(titleRow)
+ : null;
if (!titleRow || !editor?.plugins.has("IncludeNote")
- || !editor.plugins.get("IncludeNote").selectIncludeAt(titleRow)
- || !editor.commands.get("convertEmbedToLink")?.isEnabled) {
+ || !editor.commands.get("convertEmbedToLink")) {
return null;
}
return {
title: t("link_context_menu.convert_embed_to_link"),
uiIcon: "bx bx-link",
- handler: () => editor.execute("convertEmbedToLink")
+ handler: () => {
+ // `convertEmbedToLink` acts on the selected embed.
+ if (editor.plugins.get("IncludeNote").selectIncludeAt(titleRow)) {
+ editor.execute("convertEmbedToLink");
+ }
+ }
};
}
diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.ts
index 9b08b4e3a7..c7ac017834 100644
--- a/packages/ckeditor5/src/plugins/includenote.spec.ts
+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts
@@ -681,6 +681,28 @@ describe("IncludeNote with attachments", () => {
.toBe(`<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`));
});
+ it("converts the embed that selectIncludeAt() selects from its title row", async () => {
+ editor.setData("<p>before</p>" + embedHtml());
+ const command = editor.commands.get(CONVERT_EMBED_TO_LINK_COMMAND);
+ const wrapper = renderEmbeds()?.[0];
+ if (!wrapper) {
+ throw new Error("Expected a rendered embed.");
+ }
+ const titleRow = document.createElement("div");
+ titleRow.className = "include-note-title-row";
+ wrapper.append(titleRow);
+ expect(command?.isEnabled).toBe(false);
+
+ expect(editor.plugins.get("IncludeNote").selectIncludeAt(titleRow)).toBe(true);
+ expect(command?.isEnabled).toBe(true);
+
+ await editor.execute(CONVERT_EMBED_TO_LINK_COMMAND);
+ expect(getModelData(editor.model, { withoutSelection: true })).toBe(
+ "<paragraph>before</paragraph>"
+ + `<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`
+ );
+ });
+
it("converts nothing when no attachment embed is selected", async () => {
editor.setData(embedHtml() + "<p>after</p>");
setModelData(editor.model, "<paragraph>foo[]bar</paragraph>");