From 5c216551fad329d6e8ea2285480e461639947b96 Mon Sep 17 00:00:00 2001 From: ngqile Date: Thu, 3 Dec 2026 16:12:59 +0800 Subject: [PATCH] fix(pages): bring an added, copied or moved section into view on the canvas Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- .../app/components/pages/usePageDraft.test.tsx | 12 ++++++++++++ .../app/components/pages/usePageDraft.ts | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/solvway-admin-portal/app/components/pages/usePageDraft.test.tsx b/solvway-admin-portal/app/components/pages/usePageDraft.test.tsx index 8a579a0..1e5105e 100644 --- a/solvway-admin-portal/app/components/pages/usePageDraft.test.tsx +++ b/solvway-admin-portal/app/components/pages/usePageDraft.test.tsx @@ -70,6 +70,18 @@ describe("usePageDraft for the on-page editor", () => { expect(result.current.selected).toBe(1); }); + it("brings an added, duplicated or moved section into view", async () => { + const { result } = await loaded(); + act(() => result.current.addBlock("CTA")); + expect(result.current.revealed).toMatchObject({ index: 3 }); + act(() => result.current.duplicateBlock(0)); + expect(result.current.revealed).toMatchObject({ index: 1 }); + act(() => result.current.moveBlock(0, 1)); + expect(result.current.revealed).toMatchObject({ index: 1 }); + act(() => result.current.moveBlockTo(4, 0)); + expect(result.current.revealed).toMatchObject({ index: 0 }); + }); + it("drags a section to a new place, and the selection follows it", async () => { const { result } = await loaded(); act(() => result.current.select(0)); diff --git a/solvway-admin-portal/app/components/pages/usePageDraft.ts b/solvway-admin-portal/app/components/pages/usePageDraft.ts index 737ce28..585ac83 100644 --- a/solvway-admin-portal/app/components/pages/usePageDraft.ts +++ b/solvway-admin-portal/app/components/pages/usePageDraft.ts @@ -214,11 +214,14 @@ export function usePageDraft({ pageId, fromId, initialBlocks }: { pageId?: numbe step(`${key}:html`); setBlock(key, (b) => ({ ...b, html: value })); }; + /** Brings a section into view on the canvas: one just added, copied, moved, or refused by the server. */ + const reveal = (index: number) => setRevealed((r) => ({ index, n: (r?.n ?? 0) + 1 })); const moveBlockTo = (from: number, to: number) => { if (from === to) return; step(null); setRows((rs) => moveItem(rs, from, to)); setSelected((s) => (s === from ? to : s)); + reveal(to); }; const moveBlock = (index: number, delta: -1 | 1) => moveBlockTo(index, index + delta); const removeBlock = (key: number) => { @@ -232,6 +235,7 @@ export function usePageDraft({ pageId, fromId, initialBlocks }: { pageId?: numbe step(null); setRows((rs) => [...rs.slice(0, at), { key: ++nextKey.current, block: emptyBlock(type) }, ...rs.slice(at)]); setSelected(at); + reveal(at); }; const addBlock = (type: BlockType) => insertBlock(rows.length, type); /** A copy of the block below it, selected; a chosen image not saved yet goes with it. */ @@ -243,6 +247,7 @@ export function usePageDraft({ pageId, fromId, initialBlocks }: { pageId?: numbe setRows((rs) => [...rs.slice(0, index + 1), { key, block: structuredClone(row.block) }, ...rs.slice(index + 1)]); if (pending[row.key]) setPending((p) => ({ ...p, [key]: p[row.key] })); setSelected(index + 1); + reveal(index + 1); }; /** A Custom HTML block whose canvas frame does not show its current HTML: changed or moved since the last save. */ const stale = (index: number) => { @@ -270,7 +275,7 @@ export function usePageDraft({ pageId, fromId, initialBlocks }: { pageId?: numbe if (block) { const index = Number(block[1]); setSelected(index); - setRevealed((r) => ({ index, n: (r?.n ?? 0) + 1 })); + reveal(index); } else if (field) setSelected(null); }