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
fix(presentation): put back a slide move that is refused
Elian Doran6cabd6793978
diff --git a/apps/client/src/services/branches.spec.ts b/apps/client/src/services/branches.spec.tsindex 8cab8a174d..bbde96c716 100644--- a/apps/client/src/services/branches.spec.ts+++ b/apps/client/src/services/branches.spec.ts@@ -87,7 +87,7 @@ describe("moveBeforeBranch", () => {// virt- branches and root/hoisted branches are filtered out before the loop.makeBranch("rootBranch", "root");- await branches.moveBeforeBranch(["virt-x", "rootBranch", "a1"], "before1");+ await expect(branches.moveBeforeBranch(["virt-x", "rootBranch", "a1"], "before1")).resolves.toBe(true);expect(server.put).toHaveBeenCalledTimes(1);expect(server.put).toHaveBeenCalledWith(`branches/a1/move-before/before1`);expect(beforeBranch.noteId).toBe(targetNote.noteId);@@ -95,23 +95,23 @@ describe("moveBeforeBranch", () => {// server failure -> error toast and early returnserver.put = vi.fn(async () => ({ success: false, message: "nope" })) as typeof server.put;- await branches.moveBeforeBranch(["a1"], "before1");+ await expect(branches.moveBeforeBranch(["a1"], "before1")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledWith("nope");});it("returns early when before-branch is missing", async () => {- await branches.moveBeforeBranch(["a1"], "does-not-exist");+ await expect(branches.moveBeforeBranch(["a1"], "does-not-exist")).resolves.toBe(false);expect(server.put).not.toHaveBeenCalled();});it("rejects moving before root or launch bar config", async () => {makeBranch("rootDest", "root");- await branches.moveBeforeBranch(["a1"], "rootDest");+ await expect(branches.moveBeforeBranch(["a1"], "rootDest")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledTimes(1);const lbSpy = vi.spyOn(utils, "isLaunchBarConfig").mockReturnValue(true);makeBranch("lbDest", "_lbVisibleLaunchers");- await branches.moveBeforeBranch(["a1"], "lbDest");+ await expect(branches.moveBeforeBranch(["a1"], "lbDest")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledTimes(2);lbSpy.mockRestore();});@@ -126,26 +126,26 @@ describe("moveAfterBranch", () => {makeBranch("m1", n1.noteId);makeBranch("m2", n2.noteId);- await branches.moveAfterBranch(["m1", "m2"], "afterDest");+ await expect(branches.moveAfterBranch(["m1", "m2"], "afterDest")).resolves.toBe(true);expect(server.put).toHaveBeenCalledTimes(2);// reversed order: m2 first, then m1expect((server.put as any).mock.calls[0][0]).toBe("branches/m2/move-after/afterDest");expect((server.put as any).mock.calls[1][0]).toBe("branches/m1/move-after/afterDest");server.put = vi.fn(async () => ({ success: false, message: "fail" })) as typeof server.put;- await branches.moveAfterBranch(["m1"], "afterDest");+ await expect(branches.moveAfterBranch(["m1"], "afterDest")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledWith("fail");});it("returns early when after-note is missing", async () => {- await branches.moveAfterBranch(["m1"], "missing");+ await expect(branches.moveAfterBranch(["m1"], "missing")).resolves.toBe(false);expect(server.put).not.toHaveBeenCalled();});it("rejects moving after a forbidden destination note", async () => {// dest note id "root" is in the forbidden listmakeBranch("forbiddenDest", "root");- await branches.moveAfterBranch(["m1"], "forbiddenDest");+ await expect(branches.moveAfterBranch(["m1"], "forbiddenDest")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledTimes(1);expect(server.put).not.toHaveBeenCalled();});@@ -153,13 +153,13 @@ describe("moveAfterBranch", () => {describe("moveToParentNote", () => {it("returns early when new parent branch is missing", async () => {- await branches.moveToParentNote(["m1"], "no-parent");+ await expect(branches.moveToParentNote(["m1"], "no-parent")).resolves.toBe(false);expect(server.put).not.toHaveBeenCalled();});it("rejects moving into _lbRoot", async () => {makeBranch("lbRootBranch", "_lbRoot");- await branches.moveToParentNote(["m1"], "lbRootBranch");+ await expect(branches.moveToParentNote(["m1"], "lbRootBranch")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledTimes(1);expect(server.put).not.toHaveBeenCalled();});@@ -184,7 +184,8 @@ describe("moveToParentNote", () => {const hoisted = buildNote({ id: "hoistedId", title: "Hoisted" });makeBranch("hoistedBranch", hoisted.noteId, "root");- await branches.moveToParentNote(["missingBranch", "hoistedBranch", "regularBranch", "searchChildBranch", "regularBranch2"], "destParent", "comp-1");+ await expect(branches.moveToParentNote(["missingBranch", "hoistedBranch", "regularBranch", "searchChildBranch", "regularBranch2"], "destParent", "comp-1"))+ .resolves.toBe(true);expect(server.put).toHaveBeenCalledTimes(1);expect(server.put).toHaveBeenCalledWith("branches/move-to/destParent",@@ -195,7 +196,7 @@ describe("moveToParentNote", () => {const parentNote = buildNote({ title: "Parent3" });makeBranch("destParent3", parentNote.noteId);- await branches.moveToParentNote(["missingBranch"], "destParent3");+ await expect(branches.moveToParentNote(["missingBranch"], "destParent3")).resolves.toBe(false);expect(server.put).not.toHaveBeenCalled();});@@ -206,7 +207,7 @@ describe("moveToParentNote", () => {makeBranch("regularBranch2", regular.noteId, "root");server.put = vi.fn(async () => ({ success: false, message: "boom" })) as typeof server.put;- await branches.moveToParentNote(["regularBranch2"], "destParent2");+ await expect(branches.moveToParentNote(["regularBranch2"], "destParent2")).resolves.toBe(false);expect(toastService.showError).toHaveBeenCalledWith("boom");});});diff --git a/apps/client/src/services/branches.ts b/apps/client/src/services/branches.tsindex 00f5d94d73..70af69ae86 100644--- a/apps/client/src/services/branches.ts+++ b/apps/client/src/services/branches.ts@@ -17,18 +17,19 @@ interface Response {message: string;}+/** Moves the branches before `beforeBranchId`. Returns whether every move went through. */async function moveBeforeBranch(branchIdsToMove: string[], beforeBranchId: string) {branchIdsToMove = filterRootNote(branchIdsToMove);branchIdsToMove = filterSearchBranches(branchIdsToMove);const beforeBranch = froca.getBranch(beforeBranchId);if (!beforeBranch) {- return;+ return false;}if (beforeBranch.noteId === "root" || utils.isLaunchBarConfig(beforeBranch.noteId)) {toastService.showError(t("branches.cannot-move-notes-here"));- return;+ return false;}for (const branchIdToMove of branchIdsToMove) {@@ -36,25 +37,27 @@ async function moveBeforeBranch(branchIdsToMove: string[], beforeBranchId: strinif (!resp.success) {toastService.showError(resp.message);- return;+ return false;}}+ return true;}+/** Moves the branches after `afterBranchId`. Returns whether every move went through. */async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string) {branchIdsToMove = filterRootNote(branchIdsToMove);branchIdsToMove = filterSearchBranches(branchIdsToMove);const afterNote = await froca.getBranch(afterBranchId)?.getNote();if (!afterNote) {- return;+ return false;}const forbiddenNoteIds = ["root", hoistedNoteService.getHoistedNoteId(), "_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers"];if (forbiddenNoteIds.includes(afterNote.noteId)) {toastService.showError(t("branches.cannot-move-notes-here"));- return;+ return false;}branchIdsToMove.reverse(); // need to reverse to keep the note order@@ -64,20 +67,22 @@ async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string)if (!resp.success) {toastService.showError(resp.message);- return;+ return false;}}+ return true;}+/** Moves the movable branches under the note of `newParentBranchId`. Returns whether the move went through. */async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: string, componentId?: string) {const newParentBranch = froca.getBranch(newParentBranchId);if (!newParentBranch) {- return;+ return false;}if (newParentBranch.noteId === "_lbRoot") {toastService.showError(t("branches.cannot-move-notes-here"));- return;+ return false;}const movableBranchIds: string[] = [];@@ -92,7 +97,7 @@ async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: st}if (movableBranchIds.length === 0) {- return;+ return false;}const resp = await server.put<Response>(`branches/move-to/${newParentBranchId}`, { branchIds: movableBranchIds }, componentId);@@ -100,6 +105,7 @@ async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: stif (!resp.success) {toastService.showError(resp.message);}+ return resp.success;}/**diff --git a/apps/client/src/services/clipboard.spec.ts b/apps/client/src/services/clipboard.spec.tsindex ae758f7a8e..9fd40cdb00 100644--- a/apps/client/src/services/clipboard.spec.ts+++ b/apps/client/src/services/clipboard.spec.ts@@ -19,8 +19,8 @@ import * as ws from "./ws.js";// Provide a no-op so looking up an unknown branch id doesn't blow up.(globalThis as any).logError = (globalThis as any).logError ?? (() => {});-branchService.moveAfterBranch = vi.fn(async () => {}) as typeof branchService.moveAfterBranch;-branchService.moveToParentNote = vi.fn(async () => {}) as typeof branchService.moveToParentNote;+branchService.moveAfterBranch = vi.fn(async () => true) as typeof branchService.moveAfterBranch;+branchService.moveToParentNote = vi.fn(async () => true) as typeof branchService.moveToParentNote;branchService.cloneNoteAfter = vi.fn(async () => {}) as typeof branchService.cloneNoteAfter;branchService.cloneNoteToBranch = vi.fn(async () => {}) as typeof branchService.cloneNoteToBranch;toastService.showMessage = vi.fn() as typeof toastService.showMessage;diff --git a/apps/client/src/services/note_paste.spec.ts b/apps/client/src/services/note_paste.spec.tsindex 54fbdc8fdc..3843ff54dc 100644--- a/apps/client/src/services/note_paste.spec.ts+++ b/apps/client/src/services/note_paste.spec.ts@@ -10,7 +10,7 @@ import toastService from "./toast.js";vi.mock("./clipboard_ext.js", () => ({ copyHtml: vi.fn(async () => true) }));-branchService.moveToParentNote = vi.fn(async () => {}) as typeof branchService.moveToParentNote;+branchService.moveToParentNote = vi.fn(async () => true) as typeof branchService.moveToParentNote;toastService.showMessage = vi.fn() as typeof toastService.showMessage;describe("pasteNotes", () => {diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 2961ef8267..5d15ad9f72 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -1032,7 +1032,7 @@ describe("BoardApi card operations", () => {it("sends a card to the head of its column, and leaves one already there alone", async () => {const { api, items } = createBoardWithCards();- const moveBefore = vi.spyOn(branches, "moveBeforeBranch").mockResolvedValue(undefined);+ const moveBefore = vi.spyOn(branches, "moveBeforeBranch").mockResolvedValue(true);const last = items[items.length - 1];await api.moveToColumnStart(last.note.noteId, last.branch.branchId, "Done");diff --git a/apps/client/src/widgets/collections/presentation/model.spec.ts b/apps/client/src/widgets/collections/presentation/model.spec.tsindex 30d58f1b09..2444549761 100644--- a/apps/client/src/widgets/collections/presentation/model.spec.ts+++ b/apps/client/src/widgets/collections/presentation/model.spec.ts@@ -361,7 +361,7 @@ describe("duplicating and deleting slides", () => {vi.spyOn(froca, "getBranchId").mockImplementation(async (parentNoteId, childNoteId) => `${parentNoteId}_${childNoteId}`);const post = vi.spyOn(server, "post").mockResolvedValue({ note: { noteId: "copy" }, branch: { branchId: "a_copy" } });vi.spyOn(ws, "waitForMaxKnownEntityChangeId").mockResolvedValue(undefined);- const moveAfter = vi.spyOn(branches, "moveAfterBranch").mockResolvedValue(undefined);+ const moveAfter = vi.spyOn(branches, "moveAfterBranch").mockResolvedValue(true);await expect(duplicateSlide(model, "deck", "a1")).resolves.toBe("copy");expect(post).toHaveBeenCalledWith("notes/a1/duplicate/a");@@ -477,9 +477,9 @@ describe("moving slides", () => {it("moves the slide's branch next to the target's, or under the target", async () => {const getBranchId = vi.spyOn(froca, "getBranchId").mockImplementation(async (parentNoteId, childNoteId) => `${parentNoteId}_${childNoteId}`);- const moveBefore = vi.spyOn(branches, "moveBeforeBranch").mockResolvedValue(undefined);- const moveAfter = vi.spyOn(branches, "moveAfterBranch").mockResolvedValue(undefined);- const moveTo = vi.spyOn(branches, "moveToParentNote").mockResolvedValue(undefined);+ const moveBefore = vi.spyOn(branches, "moveBeforeBranch").mockResolvedValue(true);+ const moveAfter = vi.spyOn(branches, "moveAfterBranch").mockResolvedValue(true);+ const moveTo = vi.spyOn(branches, "moveToParentNote").mockResolvedValue(true);await expect(moveSlideNote(model, "deck", { noteId: "c", targetNoteId: "a", position: "before" })).resolves.toBe(true);@@ -492,6 +492,11 @@ describe("moving slides", () => {await moveSlideNote(model, "deck", { noteId: "a1", targetNoteId: "b", position: "into" });expect(moveTo).toHaveBeenCalledWith([ "a_a1" ], "deck_b");+ // A move that is refused, as into a parent that already holds a clone of the note.+ moveBefore.mockResolvedValueOnce(false);+ await expect(moveSlideNote(model, "deck", { noteId: "c", targetNoteId: "a", position: "before" }))+ .resolves.toBe(false);+// A branch that froca cannot find moves nothing.getBranchId.mockResolvedValue(null);moveBefore.mockClear();diff --git a/apps/client/src/widgets/collections/presentation/model.ts b/apps/client/src/widgets/collections/presentation/model.tsindex c7e94a945c..0eab9f56e7 100644--- a/apps/client/src/widgets/collections/presentation/model.ts+++ b/apps/client/src/widgets/collections/presentation/model.ts@@ -391,8 +391,8 @@ export function isSlideRebuilt(before: PresentationModel, after: PresentationMod/*** Moves the branch of the slide's note next to the target's branch, under the target's parent, or under- * the target for `into`. Returns whether it found both branches; a move the server refuses shows a- * toast but still returns `true`.+ * the target for `into`. Returns whether the move went through: `false` when a branch is missing or the+ * move is refused, which shows a toast.*/export async function moveSlideNote(presentation: PresentationModel, collectionNoteId: string, move: SlideMove) {const parentNoteId = getSlideParentNoteId(presentation, collectionNoteId, move.noteId);@@ -404,14 +404,9 @@ export async function moveSlideNote(presentation: PresentationModel, collectionN]);if (!branchId || !targetBranchId) return false;- if (move.position === "into") {- await branches.moveToParentNote([ branchId ], targetBranchId);- } else if (move.position === "before") {- await branches.moveBeforeBranch([ branchId ], targetBranchId);- } else {- await branches.moveAfterBranch([ branchId ], targetBranchId);- }- return true;+ if (move.position === "into") return await branches.moveToParentNote([ branchId ], targetBranchId);+ if (move.position === "before") return await branches.moveBeforeBranch([ branchId ], targetBranchId);+ return await branches.moveAfterBranch([ branchId ], targetBranchId);}/** Returns the slides in sidebar order: each top-level slide, then its vertical slides. */