fix(pages): bring an added, copied or moved section into view on the canvas

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-03 16:12:59 +08:00
1 parent 68159086a9
commit 5c216551fa
2 files changed
+18 -1

No files matched your search

@@ -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));
@@ -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);
}