Skip to content

Build your personal knowledge base with Trilium Notes

feat(bulk_actions): show the selected notes as chips

Elian Doran429b937ac60e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 401fa47d99..d99ef8c36f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -84,6 +84,9 @@
},
"bulk_actions": {
"bulk_actions": "Bulk actions",
+ "selected_notes": "Selected notes",
+ "more_notes": "+{{count}} more",
+ "remove_from_selection": "Remove from the selection",
"affected_notes": "Affected notes",
"include_descendants": "Include descendants of the selected notes",
"available_actions": "Available actions",
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.css b/apps/client/src/widgets/dialogs/bulk_actions.css
index d6c8aec20e..adc8cdfc1a 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.css
+++ b/apps/client/src/widgets/dialogs/bulk_actions.css
@@ -2,6 +2,24 @@
margin-top: 20px;
}
+.bulk-actions-dialog .bulk-actions-selected-notes {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 6px;
+ max-height: 30vh;
+ overflow-y: auto;
+
+ .tn-chip {
+ max-width: 100%;
+ min-width: 0;
+ }
+
+ .bulk-actions-more-notes {
+ padding-inline: 4px;
+ }
+}
+
.bulk-actions-dialog .bulk-available-action-list button {
padding: 2px 7px;
margin-inline-end: 10px;
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx
new file mode 100644
index 0000000000..a18dc31c91
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx
@@ -0,0 +1,80 @@
+import { act } from "preact/test-utils";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import Component from "../../components/component";
+import { buildNote } from "../../test/easy-froca";
+import { renderInto } from "../../test/render";
+import { ParentComponent } from "../react/react_utils";
+import BulkActionsDialog from "./bulk_actions";
+
+const server = vi.hoisted(() => ({
+ get: vi.fn(async () => []),
+ post: vi.fn()
+}));
+
+vi.mock("../../services/i18n", async (importOriginal) => ({
+ ...await importOriginal<typeof import("../../services/i18n")>(),
+ t: (key: string, opts?: { count?: number }) => (opts?.count !== undefined ? `${key}:${opts.count}` : key)
+}));
+vi.mock("../../services/server", () => ({ default: server }));
+vi.mock("../react/NoteLink", () => ({
+ default: ({ notePath }: { notePath: string }) => <span className="note-link">{notePath}</span>
+}));
+
+describe("BulkActionsDialog", () => {
+ let host: Component;
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ server.post.mockResolvedValue({ affectedNoteCount: 0 });
+ buildNote({ id: "_bulkAction", title: "Bulk action" });
+ host = new Component();
+ container = renderInto(
+ <ParentComponent.Provider value={host}>
+ <BulkActionsDialog />
+ </ParentComponent.Provider>
+ );
+ });
+
+ async function open(selectedOrActiveNoteIds: string[]) {
+ await act(async () => {
+ await host.handleEventInChildren("openBulkActionsDialog", { selectedOrActiveNoteIds });
+ });
+ }
+
+ function chipNoteIds() {
+ return [ ...container.querySelectorAll(".bulk-actions-selected-notes .tn-chip .note-link") ]
+ .map((el) => el.textContent);
+ }
+
+ function lastAffectedNotesRequest() {
+ return server.post.mock.calls.filter(([ url ]) => url === "bulk-action/affected-notes").at(-1)?.[1];
+ }
+
+ it("shows the first selected notes as chips and the rest on demand", async () => {
+ const noteIds = [ "n1", "n2", "n3", "n4", "n5", "n6", "n7" ];
+ await open(noteIds);
+ expect(chipNoteIds()).toStrictEqual([ "n1", "n2", "n3", "n4", "n5" ]);
+
+ const more = container.querySelector<HTMLElement>(".bulk-actions-selected-notes .bulk-actions-more-notes");
+ expect(more?.textContent).toBe("bulk_actions.more_notes:2");
+ await act(async () => more?.click());
+
+ expect(chipNoteIds()).toStrictEqual(noteIds);
+ expect(container.querySelector(".bulk-actions-more-notes")).toBeNull();
+ });
+
+ it("drops a removed chip from the notes the actions apply to, keeping the last one", async () => {
+ await open([ "n1", "n2" ]);
+ expect(lastAffectedNotesRequest()).toStrictEqual({ noteIds: [ "n1", "n2" ], includeDescendants: false });
+
+ const removeButtons = container.querySelectorAll<HTMLButtonElement>(".bulk-actions-selected-notes .tn-chip-remove");
+ expect(removeButtons).toHaveLength(2);
+ await act(async () => removeButtons[0].click());
+
+ expect(chipNoteIds()).toStrictEqual([ "n2" ]);
+ expect(lastAffectedNotesRequest()).toStrictEqual({ noteIds: [ "n2" ], includeDescendants: false });
+ expect(container.querySelector<HTMLButtonElement>(".tn-chip-remove")?.disabled).toBe(true);
+ });
+});
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.tsx b/apps/client/src/widgets/dialogs/bulk_actions.tsx
index 8ca35fe273..d708c4e99c 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.tsx
+++ b/apps/client/src/widgets/dialogs/bulk_actions.tsx
@@ -13,6 +13,11 @@ import AbstractBulkAction from "../bulk_actions/abstract_bulk_action";
import FNote from "../../entities/fnote";
import froca from "../../services/froca";
import { useTriliumEvent } from "../react/hooks";
+import Chip from "../react/Chip";
+import NoteLink from "../react/NoteLink";
+import LinkButton from "../react/LinkButton";
+
+const COLLAPSED_NOTE_CHIP_COUNT = 5;
export default function BulkActionsDialog() {
const [ selectedOrActiveNoteIds, setSelectedOrActiveNoteIds ] = useState<string[]>();
@@ -91,6 +96,11 @@ export default function BulkActionsDialog() {
}}
onHidden={() => setShown(false)}
>
+ <h4>{t("bulk_actions.selected_notes")}</h4>
+ {selectedOrActiveNoteIds && (
+ <SelectedNotes noteIds={selectedOrActiveNoteIds} onChange={setSelectedOrActiveNoteIds} />
+ )}
+
<h4>{t("bulk_actions.affected_notes")}: <span>{affectedNoteCount}</span></h4>
<FormCheckbox
name="include-descendants" label={t("bulk_actions.include_descendants")}
@@ -106,6 +116,34 @@ export default function BulkActionsDialog() {
)
}
+function SelectedNotes({ noteIds, onChange }: { noteIds: string[]; onChange(noteIds: string[]): void }) {
+ const [ expanded, setExpanded ] = useState(false);
+ const shownNoteIds = expanded ? noteIds : noteIds.slice(0, COLLAPSED_NOTE_CHIP_COUNT);
+ const hiddenCount = noteIds.length - shownNoteIds.length;
+
+ return (
+ <div className="bulk-actions-selected-notes">
+ {shownNoteIds.map((noteId) => (
+ <Chip
+ key={noteId}
+ removeButtonText={t("bulk_actions.remove_from_selection")}
+ disabled={noteIds.length === 1}
+ onRemove={() => onChange(noteIds.filter((id) => id !== noteId))}
+ >
+ <NoteLink notePath={noteId} showNoteIcon />
+ </Chip>
+ ))}
+ {hiddenCount > 0 && (
+ <LinkButton
+ className="bulk-actions-more-notes"
+ text={t("bulk_actions.more_notes", { count: hiddenCount })}
+ onClick={() => setExpanded(true)}
+ />
+ )}
+ </div>
+ );
+}
+
function AvailableActionsList() {
return <table class="bulk-available-action-list">
{bulk_action.ACTION_GROUPS.map((actionGroup) => {
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html
index f71e67df41..2864dfe13a 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Bulk Actions.html
@@ -19,6 +19,14 @@
</li>
<li>Right click in the&nbsp;<a class="reference-link" href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>&nbsp;and
select <em>Advanced</em> → <em>Apply bulk actions</em>.</li>
+ <li>The <em>Selected notes</em> section at the top of the dialog lists the notes
+ the actions apply to. When many notes are selected, only the first few
+ are shown; click <em>+N more</em> to show the rest.
+ <ul>
+ <li>To leave a note out without changing the selection in the note tree, press
+ the X button on its entry. The last remaining note cannot be removed.</li>
+ </ul>
+ </li>
<li>By default, only the selected notes will be affected. To also include
all the descendants of the notes, check <em>Include descendants of the selected notes</em>.
The number of affected notes at the top of the dialog will update to reflect
diff --git a/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md b/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md
index 8b7f1ad327..132a5e5a82 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Bulk Actions.md
@@ -9,6 +9,8 @@ The _Bulk Actions_ dialog makes it easy to apply changes to multiple notes at on
* A single note (and potentially its child notes) simply by clicking on it (with a left click or a right click).
* Multiple notes. See <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree/Multiple%20selection.md">Multiple selection</a> on how to do so.
* Right click in the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a> and select _Advanced_ → _Apply bulk actions_.
+* The _Selected notes_ section at the top of the dialog lists the notes the actions apply to. When many notes are selected, only the first few are shown; click _+N more_ to show the rest.
+ * To leave a note out without changing the selection in the note tree, press the X button on its entry. The last remaining note cannot be removed.
* By default, only the selected notes will be affected. To also include all the descendants of the notes, check _Include descendants of the selected notes_. The number of affected notes at the top of the dialog will update to reflect the change.
* Click on which action to apply from the _Available actions_ section. A detailed description of each is available in the next section.
* For each action selected, the _Chosen actions_ section will update to reveal the entry. Each action will have its own configuration.