Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: change the default action of the "attach file" button from link to embed

Adorian Dorandbfbe6035ce9
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
index ebe1d35ca2..addf0fa84c 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html
@@ -35,14 +35,15 @@
in a dedicated section (<em>Generated by Trilium</em>).</p>
<h2>Attaching files from a text note</h2>
<p>To attach files to a&nbsp;<a class="reference-link" href="#root/_help_iPIMuisry3hd">Text</a>&nbsp;note
- and link them in its content, press the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button
+ and embed them in its content, press the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button
in the&nbsp;<a class="reference-link" href="#root/_help_nRhnJkTT8cPs">Formatting toolbar</a>,
next to the <span class="tn-icon cke cke-image-upload"></span>image upload
- button, and select one or more files. The arrow beside the button lists
- the same action as <em>Attach file as a link</em>, followed by <em>Attach and embed file</em>,
- which embeds the files instead (see below). Both actions are also available
- as <em>Attach file as a link</em> and <em>Attach and embed file</em> in the&nbsp;<a
- class="reference-link" href="#root/_help_ZlN4nump6EbW">Slash Commands</a>.</p>
+ button, and select one or more files (see <em>Embedding an attachment</em> below).
+ The arrow beside the button lists the same action as <em>Attach and embed file</em>,
+ followed by <em>Attach file as a link</em>, which links the files instead.
+ Both actions are also available as <em>Attach file as a link</em> and <em>Attach and embed file</em> in
+ the&nbsp;<a class="reference-link" href="#root/_help_ZlN4nump6EbW">Slash Commands</a>.</p>
+<p>When files are attached as links:</p>
<ul>
<li>Each file becomes an attachment of the note, and a link to it is inserted
at the cursor. Several files produce several links, separated by a space.</li>
@@ -71,8 +72,7 @@
code file can also be edited in place (see <em>Editing an include in place</em> in&nbsp;<a
class="reference-link" href="#root/_help_nBAXQFj20hS1">Include Note</a>).</p>
<ul>
- <li>To attach files and embed them straight away, select <em>Attach and embed file</em> from
- the arrow beside the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button.
+ <li>To attach files and embed them straight away, press the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button.
Each file gets an embed of its own, which shows the file name while the
file uploads.</li>
<li>To embed an attachment that is already linked, right-click a link to it
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Images.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Images.html
index 5dd18b45a9..d13111f951 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Images.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Images.html
@@ -14,7 +14,8 @@
<li>You can also copy and paste an image from web (see section below).</li>
</ul>
<aside class="admonition tip">
- <p>To link a picture instead of displaying it, attach it with the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button.
+ <p>To link a picture instead of displaying it, attach it with <em>Attach file as a link</em>,
+ from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> button.
See&nbsp;<a class="reference-link" href="#root/_help_0vhv7lsOLy82">Attachments</a>.</p>
</aside>
<h2>Clipboard &amp; automatic download of images</h2>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
index fa4f8d9dfb..85eeee4435 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md
@@ -21,7 +21,9 @@ The system attachments are displayed at the end of the attachments list, in a de
## Attaching files from a text note
-To attach files to a <a class="reference-link" href="../../Note%20Types/Text.md">Text</a> note and link them in its content, press the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button in the <a class="reference-link" href="../../Note%20Types/Text/Formatting%20toolbar.md">Formatting toolbar</a>, next to the <span class="tn-icon cke cke-image-upload"></span>image upload button, and select one or more files. The arrow beside the button lists the same action as _Attach file as a link_, followed by _Attach and embed file_, which embeds the files instead (see below). Both actions are also available as _Attach file as a link_ and _Attach and embed file_ in the <a class="reference-link" href="../../Note%20Types/Text/Slash%20Commands.md">Slash Commands</a>.
+To attach files to a <a class="reference-link" href="../../Note%20Types/Text.md">Text</a> note and embed them in its content, press the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button in the <a class="reference-link" href="../../Note%20Types/Text/Formatting%20toolbar.md">Formatting toolbar</a>, next to the <span class="tn-icon cke cke-image-upload"></span>image upload button, and select one or more files (see _Embedding an attachment_ below). The arrow beside the button lists the same action as _Attach and embed file_, followed by _Attach file as a link_, which links the files instead. Both actions are also available as _Attach file as a link_ and _Attach and embed file_ in the <a class="reference-link" href="../../Note%20Types/Text/Slash%20Commands.md">Slash Commands</a>.
+
+When files are attached as links:
* Each file becomes an attachment of the note, and a link to it is inserted at the cursor. Several files produce several links, separated by a space.
* While a file uploads, its link shows the file name and a toast shows the progress.
@@ -39,7 +41,7 @@ Links follow changes to their attachment: renaming it updates the title they sho
An attachment can be shown in the text instead of linked, the way <a class="reference-link" href="../../Note%20Types/Text/Include%20Note.md">Include Note</a> shows a note: a picture is displayed, a PDF, a video or a document is previewed, and a code or text file is shown with syntax highlighting. An embedded code file can also be edited in place (see _Editing an include in place_ in <a class="reference-link" href="../../Note%20Types/Text/Include%20Note.md">Include Note</a>).
-* To attach files and embed them straight away, select _Attach and embed file_ from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button. Each file gets an embed of its own, which shows the file name while the file uploads.
+* To attach files and embed them straight away, press the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button. Each file gets an embed of its own, which shows the file name while the file uploads.
* To embed an attachment that is already linked, right-click a link to it in a text note being edited and select _Convert link to an embed_, at the end of the menu.
* A drawing canvas, inserted with <span class="tn-icon bx bx-pen"></span>_Drawing canvas_ in the <span class="tn-icon cke cke-plus"></span>_Insert_ menu, is an embed of a `Canvas.excalidraw` attachment that can be drawn on in place (see _Drawing canvases_ in <a class="reference-link" href="../../Note%20Types/Text/Include%20Note.md">Include Note</a>).
* An embed gets the box size that suits the attachment, as a new included note does (see _Box sizes_ there). To change its size, select the embed and use the box size menu in its toolbar, or the _Size_ submenu of its <span class="tn-icon bx bx-dots-vertical-rounded"></span>_More actions_ menu. A _Small_, _Medium_ or _Expandable_ embed can also be resized by dragging its handles (see _Resizing_ there).
diff --git a/docs/User Guide/User Guide/Note Types/Text/Images.md b/docs/User Guide/User Guide/Note Types/Text/Images.md
index 810f801331..66ac643605 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Images.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Images.md
@@ -12,7 +12,7 @@ To add an image to the note:
* You can also copy and paste an image from web (see section below).
> [!TIP]
-> To link a picture instead of displaying it, attach it with the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button. See <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/Notes/Attachments.md">Attachments</a>.
+> To link a picture instead of displaying it, attach it with _Attach file as a link_, from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button. See <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/Notes/Attachments.md">Attachments</a>.
## Clipboard & automatic download of images
diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts
index bc1459a5d9..eab511b8cc 100644
--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts
@@ -77,7 +77,7 @@ describe("FileUploadUI", () => {
expect(actionView.acceptedType).toBeUndefined();
});
- it("links the files picked through the button and returns the focus to the editor", () => {
+ it("embeds the files picked through the button and returns the focus to the editor", () => {
const { actionView } = createDropdown();
const execute = vi.spyOn(editor, "execute").mockImplementation(() => undefined);
const focus = vi.spyOn(editor.editing.view, "focus");
@@ -85,11 +85,13 @@ describe("FileUploadUI", () => {
actionView.fire("done", files);
- expect(execute).toHaveBeenCalledWith("fileUpload", { file: files });
+ expect(execute).toHaveBeenCalledExactlyOnceWith("fileUpload", {
+ file: files, asEmbed: true
+ });
expect(focus).toHaveBeenCalled();
});
- it("lists attaching as links and as embeds, each closing the dropdown when picked", () => {
+ it("lists attaching as embeds and as links, each closing the dropdown when picked", () => {
const { dropdown, items } = createDropdown();
const execute = vi.spyOn(editor, "execute").mockImplementation(() => undefined);
const focus = vi.spyOn(editor.editing.view, "focus");
@@ -100,11 +102,11 @@ describe("FileUploadUI", () => {
expect(items.map((item) => [
item.label, item.icon, item.withText, item.allowMultipleFiles
])).toEqual([
- [ "Attach file as a link", bxLink, true, true ],
- [ "Attach and embed file", bxShapeSquare, true, true ]
+ [ "Attach and embed file", bxShapeSquare, true, true ],
+ [ "Attach file as a link", bxLink, true, true ]
]);
- const [ linkItem, embedItem ] = items;
+ const [ embedItem, linkItem ] = items;
dropdown.render();
dropdown.isOpen = true;
linkItem.fire("execute");
@@ -112,33 +114,41 @@ describe("FileUploadUI", () => {
expect(openDialog).toHaveBeenCalledOnce();
openDialog.mockRestore();
- linkItem.fire("done", files);
embedItem.fire("done", files);
+ linkItem.fire("done", files);
expect(execute.mock.calls).toEqual([
- [ "fileUpload", { file: files } ],
- [ "fileUpload", { file: files, asEmbed: true } ]
+ [ "fileUpload", { file: files, asEmbed: true } ],
+ [ "fileUpload", { file: files } ]
]);
expect(focus).toHaveBeenCalledTimes(2);
dropdown.destroy();
});
- it("follows the `fileUpload` command, and offers embeds only where an embed can go", () => {
- const { dropdown, actionView, items: [ linkItem, embedItem ] } = createDropdown();
+ it("follows the `fileUpload` command, and embeds only where an embed can go", () => {
+ const { dropdown, actionView, items: [ embedItem, linkItem ] } = createDropdown();
expect([ dropdown.isEnabled, actionView.isEnabled ]).toEqual([ true, true ]);
- expect([ linkItem.isEnabled, embedItem.isEnabled ]).toEqual([ true, true ]);
+ expect([ embedItem.isEnabled, linkItem.isEnabled ]).toEqual([ true, true ]);
embedCommand.forceDisabled("spec");
- expect([ linkItem.isEnabled, embedItem.isEnabled ]).toEqual([ true, false ]);
+ expect([ dropdown.isEnabled, actionView.isEnabled ]).toEqual([ true, false ]);
+ expect([ embedItem.isEnabled, linkItem.isEnabled ]).toEqual([ false, true ]);
+ embedCommand.clearForceDisabled("spec");
command.forceDisabled("spec");
expect([ dropdown.isEnabled, actionView.isEnabled ]).toEqual([ false, false ]);
});
- it("builds the button in an editor without the commands, listing no embeds", async () => {
+ it("builds a linking button in an editor without the commands", async () => {
editor = await createTestEditor([Essentials, Paragraph, FileUploadUI]);
+ const execute = vi.spyOn(editor, "execute").mockImplementation(() => undefined);
+ const files = pickFiles();
+
+ const { actionView, items } = createDropdown();
+ actionView.fire("done", files);
- const labels = createDropdown().items.map((item) => item.label);
- expect(labels).toEqual([ "Attach file as a link" ]);
+ expect(items.map((item) => item.label)).toEqual([ "Attach file as a link" ]);
+ expect(actionView.isEnabled).toBe(true);
+ expect(execute).toHaveBeenCalledExactlyOnceWith("fileUpload", { file: files });
});
describe("drawingCanvas", () => {
diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts
index c429d1f00d..e3823fe514 100644
--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts
+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts
@@ -24,8 +24,8 @@ import bxShapeSquare from "boxicons/svg/regular/bx-shape-square.svg?raw";
import type { FileUploadOptions } from "./fileuploadcommand.js";
/**
- * Registers the `fileUpload` split button. The button attaches the picked files and links them;
- * the list also offers to embed them.
+ * Registers the `fileUpload` split button. The button attaches the picked files and embeds them;
+ * the list also offers to link them. In an editor without embeds, the button links them.
*
* Also registers the `drawingCanvas` button, which attaches an empty canvas drawing and embeds it,
* editable.
@@ -47,22 +47,18 @@ export default class FileUploadUI extends Plugin {
tooltip: true,
allowMultipleFiles: true
});
- this.attachOnDone(actionView, {});
const dropdownView = createDropdown(locale, new SplitButtonView(locale, actionView));
dropdownView.buttonView.set({ label: t("Attach file"), tooltip: true });
- actionView.bind("isEnabled").to(dropdownView);
const command = editor.commands.get("fileUpload");
if (command) {
dropdownView.bind("isEnabled").to(command);
}
- const buttons = [
- this.createListButton(locale, t("Attach file as a link"), bxLink, {})
- ];
+ const buttons: FileDialogListItemButtonView[] = [];
- // Embedding goes wherever the "Include note" button can insert an embed.
+ // Embedding, the default, goes wherever the "Include note" button can insert an embed.
const embedCommand = editor.commands.get("insertContentEmbed");
if (embedCommand) {
const embedButton = this.createListButton(
@@ -70,8 +66,19 @@ export default class FileUploadUI extends Plugin {
);
embedButton.bind("isEnabled").to(embedCommand);
buttons.push(embedButton);
+
+ this.attachOnDone(actionView, { asEmbed: true });
+ actionView.bind("isEnabled").to(
+ dropdownView, "isEnabled", embedCommand, "isEnabled",
+ (canUpload, canEmbed) => canUpload && canEmbed
+ );
+ } else {
+ this.attachOnDone(actionView, {});
+ actionView.bind("isEnabled").to(dropdownView);
}
+ buttons.push(this.createListButton(locale, t("Attach file as a link"), bxLink, {}));
+
const listView = dropdownView.listView = new ListView(locale);
for (const button of buttons) {
const listItem = new ListItemView(locale);