Skip to content

Build your personal knowledge base with Trilium Notes

client/dialogs/bulk actions: use a row button for "Add action"

Adorian Doran76c92add47fb
diff --git a/apps/client/src/widgets/bulk_actions/AddBulkActionButton.tsx b/apps/client/src/widgets/bulk_actions/AddBulkActionButton.tsx
deleted file mode 100644
index c145abb495..0000000000
--- a/apps/client/src/widgets/bulk_actions/AddBulkActionButton.tsx
+++ /dev/null
@@ -1,46 +0,0 @@
-import type { ComponentChildren } from "preact";
-import { Fragment } from "preact/jsx-runtime";
-
-import bulk_action, { ACTION_GROUPS } from "../../services/bulk_action";
-import Dropdown from "../react/Dropdown";
-import { FormListHeader, FormListItem } from "../react/FormList";
-
-interface AddBulkActionButtonProps {
- /** The note the chosen action is stored on, as an `#action` label. */
- noteId: string;
- text: ComponentChildren;
- buttonClassName?: string;
-}
-
-/**
- * A menu of every bulk action, grouped as in {@link ACTION_GROUPS}, that adds the picked one to the
- * note's actions.
- */
-export default function AddBulkActionButton({ noteId, text, buttonClassName }: AddBulkActionButtonProps) {
- return (
- <Dropdown
- buttonClassName={buttonClassName}
- dropdownContainerClassName="action-add-menu"
- text={text}
- noSelectButtonStyle
- mobileBottomSheet
- >
- {ACTION_GROUPS.map(({ actions, title }, index) => (
- <Fragment key={index}>
- <FormListHeader text={title} />
-
- <div>
- {actions.map(({ actionName, actionTitle, actionIcon, actionDescription }) => (
- <FormListItem
- key={actionName}
- icon={actionIcon}
- title={actionDescription}
- onClick={() => bulk_action.addAction(noteId, actionName)}
- >{actionTitle}</FormListItem>
- ))}
- </div>
- </Fragment>
- ))}
- </Dropdown>
- );
-}
diff --git a/apps/client/src/widgets/bulk_actions/BulkAction.css b/apps/client/src/widgets/bulk_actions/BulkAction.css
index be86e3faee..2a8392af97 100644
--- a/apps/client/src/widgets/bulk_actions/BulkAction.css
+++ b/apps/client/src/widgets/bulk_actions/BulkAction.css
@@ -23,7 +23,7 @@
}
}
-.action-add-menu .tn-icon {
+.tn-menu .tn-icon {
--bulk-action-marker-cutout: var(--menu-background-color);
}
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.css b/apps/client/src/widgets/dialogs/bulk_actions.css
index 3c742eb3f1..2be01ac54e 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.css
+++ b/apps/client/src/widgets/dialogs/bulk_actions.css
@@ -6,9 +6,4 @@
.tn-card:last-child {
margin-bottom: 0;
}
-
- .bulk-actions-add-action {
- display: flex;
- justify-content: flex-end;
- }
}
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx
index d437a3fdd9..f7d9633ef5 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx
+++ b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx
@@ -2,7 +2,8 @@ import { act } from "preact/test-utils";
import { beforeEach, describe, expect, it, vi } from "vitest";
import Component from "../../components/component";
-import bulk_action from "../../services/bulk_action";
+import contextMenu, { type MenuCommandItem } from "../../menus/context_menu";
+import bulk_action, { ACTION_GROUPS } from "../../services/bulk_action";
import froca from "../../services/froca";
import noteAttributeCache from "../../services/note_attribute_cache";
import { buildNote } from "../../test/easy-froca";
@@ -128,19 +129,30 @@ describe("BulkActionsDialog", () => {
.toBe("bulk_actions.none_yet");
});
- it("adds the action picked from the add menu at the foot of the actions card", async () => {
+ it("adds the action picked from the add row's menu at the foot of the card", async () => {
+ const show = vi.spyOn(contextMenu, "show").mockResolvedValue();
await open([ "n1" ]);
- const toggle = container.querySelector<HTMLButtonElement>(".bulk-actions-chosen-actions .tn-card-section:last-child .dropdown button");
- expect(toggle?.textContent).toContain("bulk_actions.add_action");
- await act(async () => toggle?.click());
-
- const item = [ ...document.querySelectorAll<HTMLElement>(".action-add-menu .dropdown-item") ]
- .find((el) => el.textContent?.includes("move_note.move_note"));
- expect(item).toBeDefined();
- expect(item?.querySelector(".bx-note.bulk-action-marker-move")).not.toBeNull();
- expect(item?.getAttribute("title") ?? item?.getAttribute("data-bs-original-title"))
- .toBe("move_note.description");
- await act(async () => item?.click());
+ const adder = container.querySelector<HTMLButtonElement>(".bulk-actions-chosen-actions"
+ + " > .tn-card-body > .tn-card-adders:last-child > .tn-card-adder");
+ expect(adder?.textContent).toContain("bulk_actions.add_action");
+ await act(async () => adder?.click());
+ await vi.waitFor(() => expect(show).toHaveBeenCalledOnce());
+
+ const { anchor, items } = show.mock.calls[0][0];
+ expect(anchor).toBe(adder);
+ expect(items.flatMap((item) => ("kind" in item && item.kind === "header"
+ ? [ item.title ]
+ : [])))
+ .toStrictEqual(ACTION_GROUPS.map(({ title }) => title));
+ expect(items[0]).toMatchObject({ kind: "header" });
+
+ const item = items.find((entry): entry is MenuCommandItem<unknown> =>
+ !("kind" in entry) && entry.title === "move_note.move_note");
+ expect(item).toMatchObject({
+ uiIcon: "bx bx-note bulk-action-marker-move",
+ tooltip: "move_note.description"
+ });
+ await act(async () => item?.handler?.(item, new MouseEvent("click")));
expect(server.post).toHaveBeenCalledWith("notes/_bulkAction/attributes", {
type: "label",
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.tsx b/apps/client/src/widgets/dialogs/bulk_actions.tsx
index 92f651f164..d5bac53262 100644
--- a/apps/client/src/widgets/dialogs/bulk_actions.tsx
+++ b/apps/client/src/widgets/dialogs/bulk_actions.tsx
@@ -6,13 +6,12 @@ import "./bulk_actions.css";
import { BulkActionAffectedNotes } from "@triliumnext/commons";
import server from "../../services/server";
import Button from "../react/Button";
-import { Card, CardSection } from "../react/Card";
+import { Card, CardSection, ItemCreationRow } from "../react/Card";
import NoItems from "../react/NoItems";
import { OptionsRowWithToggle } from "../type_widgets/options/components/OptionsRow";
-import bulk_action from "../../services/bulk_action";
-import AddBulkActionButton from "../bulk_actions/AddBulkActionButton";
+import bulk_action, { ACTION_GROUPS } from "../../services/bulk_action";
import { TemplatePreviewNoteContext } from "../bulk_actions/TemplateField";
-import Icon from "../react/Icon";
+import type { MenuItem } from "../../menus/context_menu";
import dialog from "../../services/dialog";
import toast from "../../services/toast";
import AbstractBulkAction from "../bulk_actions/abstract_bulk_action";
@@ -20,6 +19,7 @@ import FNote from "../../entities/fnote";
import froca from "../../services/froca";
import { useTriliumEvent } from "../react/hooks";
import NoteChips from "../react/NoteChips";
+import { escapeHtml } from "../../services/utils";
export default function BulkActionsDialog() {
const [ selectedOrActiveNoteIds, setSelectedOrActiveNoteIds ] = useState<string[]>();
@@ -157,13 +157,11 @@ export default function BulkActionsDialog() {
<TemplatePreviewNoteContext.Provider value={selectedOrActiveNoteIds?.[0]}>
<ExistingActionsList existingActions={existingActions} />
</TemplatePreviewNoteContext.Provider>
- <CardSection className="bulk-actions-add-action">
- <AddBulkActionButton
- noteId="_bulkAction"
- buttonClassName="btn-secondary btn-micro"
- text={<><Icon icon="bx bx-plus" />{" "}{t("bulk_actions.add_action")}</>}
- />
- </CardSection>
+ <ItemCreationRow buttons={[ {
+ label: t("bulk_actions.add_action"),
+ icon: "bx bx-plus",
+ menu: buildAddActionMenu()
+ } ]} />
</Card>
</Modal>
);
@@ -184,3 +182,16 @@ function ExistingActionsList({ existingActions }: { existingActions: AbstractBul
))}
</>;
}
+
+/** Every bulk action, under the heading of its group in {@link ACTION_GROUPS}. */
+function buildAddActionMenu(): MenuItem<string>[] {
+ return ACTION_GROUPS.flatMap(({ title, actions }): MenuItem<string>[] => [
+ { kind: "header", title },
+ ...actions.map(({ actionName, actionTitle, actionIcon, actionDescription }) => ({
+ title: escapeHtml(actionTitle),
+ uiIcon: actionIcon,
+ tooltip: actionDescription,
+ handler: () => bulk_action.addAction("_bulkAction", actionName)
+ }))
+ ]);
+}