fix(global-sections): block structural edits inside a global-section instance

syncInstances rebuilds an instance's subtree from the master, so a node
dropped, pasted, duplicated or moved into an instance vanished from every page
on the next master edit. Chosen approach: BLOCK (not preserve-on-sync):
- every resolver component's Craft canMoveIn/canMoveOut refuses a canvas inside
  an instance (canvas DnD, Layers DnD, actions.move); synthetic shell-inner
  nodes now carry the component's rules instead of hardcoded allow-all
- insertBlock (BlocksPanel / EmptyContainerAdd / palette), paste (menu +
  shortcut), duplicate (menu + shortcut) and Sitesmith patch/section replace
  redirect to after the instance or refuse; no empty-container '+' inside one
The instance root still moves/duplicates as a whole; content overrides are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-10-07 13:32:34 -07:00
1 parent ca500d36a9
commit 6d22aabdc0
11 files changed
+271 -17

No files matched your search

@@ -20,6 +20,12 @@ import { SectionLibraryModal, openSectionLibrary, closeSectionLibrary } from '..
import { Canvas } from '../editor/Canvas';
import { useApplyAiResponse } from '../utils/apply-ai-response';
import { TemplateModal } from '../panels/topbar/TemplateModal';
import { insertBlock } from '../panels/left/blocks-registry';
import { moveLayer } from '../panels/left/layers-dnd';
import { CORE_COMMANDS } from '../editor/core-commands';
import { setClipboardTree } from '../hooks/clipboard';
import { useNodeActions } from '../hooks/useNodeActions';
import { EmptyContainerAdd } from '../editor/EmptyContainerAdd';
import { HistoryModal } from '../panels/history/HistoryModal';
/**
@@ -514,6 +520,118 @@ describe('#57 global sections', () => {
expect(document.body.textContent).toContain('Click Done on the global section');
});
describe('#57 finding 4: no structural edits inside an instance (blocked, so a master sync never deletes user content)', () => {
const cmd = (id: string) => CORE_COMMANDS.find((c) => c.id === id)!;
const ctx = (ids: string[]) => ({ query: h!.query, actions: h!.actions, selectedIds: ids, setDevice: vi.fn() }) as any;
const setup = () => {
mount();
const sec = addPromo();
let libraryId = '';
h!.act(() => { libraryId = globals.makeGlobal(sec, 'Promo')!; });
// A plain section outside the instance, with a heading we can try to move in.
let outside = '';
h!.act(() => {
const tree = buildNodeTree(h!.query, { type: { resolvedName: 'Section' }, props: {}, nodes: [{ type: { resolvedName: 'Heading' }, props: { text: 'Outsider' } }] });
h!.actions.addNodeTree(tree, 'ROOT');
outside = tree.rootNodeId;
});
const nodes = live();
const [hid] = byType(nodes, 'Heading', sec);
const inner = nodes[hid].parent as string; // the instance's inner canvas
const [outsider] = byType(nodes, 'Heading', outside);
return { sec, libraryId, hid, inner, outsider };
};
const subtreeSize = (sec: string) => Object.keys(live()).filter((id) => { for (let p: string | null = id; p; p = live()[p]?.parent ?? null) if (p === sec) return true; return false; }).length;
test('canvas/layers DnD: dropping or moving a node into an instance is refused; reordering inside it too', () => {
const { sec, inner, hid, outsider } = setup();
const size = subtreeSize(sec);
expect(h!.query.node(inner).isDroppable(outsider, () => {})).toBe(false);
expect(moveLayer(h!.query, h!.actions, outsider, inner, 0).ok).toBe(false);
expect(() => h!.act(() => { h!.actions.move(outsider, inner, 0); })).toThrow();
// reorder inside the instance
expect(moveLayer(h!.query, h!.actions, hid, inner, 3).ok).toBe(false);
// moving an inner node OUT of the instance is refused as well
expect(moveLayer(h!.query, h!.actions, hid, 'ROOT', 0).ok).toBe(false);
expect(subtreeSize(sec)).toBe(size);
// the instance root itself still moves as a whole
expect(moveLayer(h!.query, h!.actions, sec, 'ROOT', 1).ok).toBe(true);
});
test('BlocksPanel / EmptyContainerAdd insert: an explicit parent inside an instance is refused; "after selection" lands after the instance', () => {
const { sec, inner, hid } = setup();
const size = subtreeSize(sec);
let r: string | null = 'x';
h!.act(() => { r = insertBlock(h!.query, h!.actions, 'text', { parentId: inner }); });
expect(r).toBeNull();
expect(subtreeSize(sec)).toBe(size);
h!.act(() => { h!.actions.selectNode(hid); });
h!.act(() => { r = insertBlock(h!.query, h!.actions, 'text', { afterSelection: true }); });
expect(r).toBeTruthy();
expect(subtreeSize(sec)).toBe(size);
const rootKids = live().ROOT.nodes;
expect(rootKids[rootKids.indexOf(sec) + 1]).toBe(r);
});
test('paste and duplicate never add nodes inside an instance', () => {
const { sec, hid, outsider } = setup();
const size = subtreeSize(sec);
setClipboardTree(h!.query.node(outsider).toNodeTree());
h!.act(() => { cmd('edit.paste').run(ctx([hid])); });
expect(subtreeSize(sec)).toBe(size);
const rootKids = live().ROOT.nodes;
expect(live()[rootKids[rootKids.indexOf(sec) + 1]].type.resolvedName).toBe('Heading');
h!.act(() => { cmd('edit.duplicate').run(ctx([hid])); });
expect(subtreeSize(sec)).toBe(size);
setClipboardTree(null);
});
test('Sitesmith section replace aimed inside an instance appends instead of restructuring it', async () => {
const { sec, hid } = setup();
const size = subtreeSize(sec);
const tree = { type: { resolvedName: 'Section' }, props: {}, nodes: [{ type: { resolvedName: 'Heading' }, props: { text: 'AI section' } }] } as any;
let r: any;
await h!.act(async () => { r = await applyAi({ type: 'replace', scope: 'section', pages: [{ name: 'x', tree }] } as any, hid); });
expect(r.ok).toBe(true);
expect(subtreeSize(sec)).toBe(size);
expect(live()[hid]).toBeTruthy();
expect(JSON.stringify(live())).toContain('AI section');
});
test('the empty-container "+" is not offered inside an instance', () => {
mount(<EmptyContainerAdd />);
let secId = '';
h!.act(() => {
const tree = buildNodeTree(h!.query, { type: { resolvedName: 'Section' }, props: {}, nodes: [] });
h!.actions.addNodeTree(tree, 'ROOT');
secId = tree.rootNodeId;
});
const inner = live()[secId].linkedNodes ? Object.values(live()[secId].linkedNodes)[0] as string : '';
expect(inner).toBeTruthy();
h!.act(() => { h!.actions.selectNode(inner); });
expect(document.body.querySelector('.empty-container-add, [aria-label^="Add"]')).not.toBeNull();
h!.act(() => { globals.makeGlobal(secId, 'Empty'); });
h!.act(() => { h!.actions.selectNode(Object.values(live()[secId].linkedNodes)[0] as string); });
expect(document.body.querySelector('.empty-container-add, [aria-label^="Add"]')).toBeNull();
});
test('context-menu duplicate of an inner node is refused; duplicating the instance root is allowed', () => {
const { sec, hid } = setup();
let dup: (() => string | null) | null = null;
let dupRoot: (() => string | null) | null = null;
const Hook: React.FC = () => { dup = useNodeActions(hid).duplicate; dupRoot = useNodeActions(sec).duplicate; return null; };
h!.mountChild(<PageProvider><GlobalSectionsProvider><Probe /><Hook /></GlobalSectionsProvider></PageProvider>);
const size = subtreeSize(sec);
let r: string | null = 'x';
h!.act(() => { r = dup!(); });
expect(r).toBeNull();
expect(subtreeSize(sec)).toBe(size);
h!.act(() => { r = dupRoot!(); });
expect(r).toBeTruthy();
expect(instanceMeta(live()[r!])).toBeTruthy();
});
});
test('remove is refused while instances exist, allowed once none are left', () => {
mount();
const sec = addPromo();