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/collections/board: add a command to duplicate items
Adorian Doran8634daae22e7
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 00b30edb57..3db711da52 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3348,6 +3348,7 @@"archive-note": "Archive note","unarchive-note": "Unarchive note","move-to": "Move to",+ "duplicate-item": "Duplicate","insert-above": "Insert above","insert-below": "Insert below","delete-column": "Delete column...",diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex e050a4dbf8..2943948a7e 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -11,6 +11,7 @@ import froca from "../../../services/froca";import note_create from "../../../services/note_create";import noteAttributeCache from "../../../services/note_attribute_cache";import server from "../../../services/server";+import ws from "../../../services/ws";import { buildNote } from "../../../test/easy-froca";import { BoardViewData } from ".";import BoardApi, { getPendingWrites, PendingColumnWrites, releasePendingWrites } from "./api";@@ -70,7 +71,7 @@ function createApi(pending,getStatusDefinition(board, statusAttribute));- return { api, saved, editing, pendingRenames: pending.renames };+ return { api, board, saved, editing, pendingRenames: pending.renames };}describe("BoardApi column mutations", () => {@@ -914,3 +915,21 @@ describe("removing a column with the question put first", () => {expect(error).toHaveBeenCalledWith("board_view.save-error");});});++describe("duplicating a card", () => {+ it("copies it into the board and puts the copy straight after the original", async () => {+ const { api, board } = createApi({ columns: [ { value: "To Do" } ] }, [ "To Do" ]);+ const post = vi.spyOn(server, "post")+ .mockResolvedValue({ branch: { branchId: "copyBranch" } } as never);+ // The global ws stub carries no such method, so it is put there the way bulk_action does.+ const wait = vi.fn(async () => {});+ ws.waitForMaxKnownEntityChangeId = wait as typeof ws.waitForMaxKnownEntityChangeId;++ await api.duplicateItem("card1", "card1Branch");++ expect(post).toHaveBeenCalledWith(`notes/card1/duplicate/${board.noteId}`);+ // Waited for, since the branch the server names has to be in froca before it can be moved.+ expect(wait).toHaveBeenCalled();+ expect(branches.moveAfterBranch).toHaveBeenCalledWith([ "copyBranch" ], "card1Branch");+ });+});diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 7b838fdfe0..cc6bdbaea6 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -10,6 +10,7 @@ import froca from "../../../services/froca";import { t } from "../../../services/i18n";import note_create from "../../../services/note_create";import server from "../../../services/server";+import ws from "../../../services/ws";import toast from "../../../services/toast";import { BoardColumnData, BoardViewData } from ".";import { type BoardStatusDefinition, canStoreColumnsInDefinition, DEFAULT_GROUP_BY } from "./columns";@@ -553,6 +554,22 @@ export default class BoardApi {return server.put(`notes/${noteId}/title`, { title: newTitle.trim() });}+ /**+ * Copies a card into the board, the way the tree duplicates a note, and puts the copy straight+ * after the one it was made from.+ *+ * The copy carries the original's attributes, the grouping label among them, so it lands in the+ * same column without being told to. The wait is what makes the move possible: the branch is+ * named by the server and has to be in froca before it can be placed.+ */+ async duplicateItem(noteId: string, branchId: string) {+ const { branch } = await server.post<{ branch: { branchId: string } }>(+ `notes/${noteId}/duplicate/${this.parentNote.noteId}`);++ await ws.waitForMaxKnownEntityChangeId();+ await branches.moveAfterBranch([ branch.branchId ], branchId);+ }+removeFromBoard(noteId: string) {const note = froca.getNoteFromCache(noteId);if (!note) return;diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex de5de1dc7d..d5a7862ac2 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -278,6 +278,22 @@ describe("Board item context menu", () => {expect(deleteNotes).toHaveBeenCalledWith([ "branchId" ], false, false);});+ it("copies a card into the board, after the one it was made from", async () => {+ const api = {+ columns: [ "To Do" ],+ isColumnArchived: () => false,+ duplicateItem: vi.fn(async () => {})+ } as unknown as BoardApi;++ const entry = openItemMenu(api).find(item =>+ item && "uiIcon" in item && item.uiIcon === "bx bx-outline");+ if (!entry || !("handler" in entry)) throw new Error("expected a duplicate entry");++ await entry.handler?.(entry, {} as never);++ expect(api.duplicateItem).toHaveBeenCalledWith(expect.any(String), "branchId");+ });+/** Opens the menu a card offers, and hands back what it was given to show. */function openItemMenu(api: BoardApi, column = "To Do") {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 21e7aff9e1..45fe72853c 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -177,6 +177,11 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNohandler: () => api.changeColumn(note.noteId, columnToMoveTo)})),},+ {+ title: t("board_view.duplicate-item"),+ uiIcon: "bx bx-outline",+ handler: () => api.duplicateItem(note.noteId, branchId)+ },{ kind: "separator" },getArchiveMenuItem(note),{