fix(site-builder): Undo can no longer cross the load or a page switch
Craft records actions.deserialize as an undoable step and nothing cleared history, so Undo right after load returned the canvas to the blank pre-load Frame (autosave would then save it), and Undo after a page switch put the previous page's content into the current slot. - clear history after the load-time deserialize and after every page/ header/footer switch (in-place AI replacement of the same slot keeps it) - continuousSetProp opens a burst only when its first call actually changed something, so it never merges into an entry it did not create - the empty-save banner's Undo is disabled when there is nothing to undo Real-editor tests: load, page/header switch, no-op-first burst, banner. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
eb134a4743
commit
572e736f1f
14 files changed
+310
-17
No files matched your search
@@ -0,0 +1,175 @@
|
||||
import { describe, test, expect, afterEach, beforeEach, vi } from 'vitest';
|
||||
import React from 'react';
|
||||
import { renderEditorHarness, EditorHarness } from '../editorHarness';
|
||||
import { EditorConfigProvider } from '../../state/EditorConfigContext';
|
||||
import { SiteDesignProvider } from '../../state/SiteDesignContext';
|
||||
import { PageProvider, usePages } from '../../state/PageContext';
|
||||
import { useWhpApi } from '../../hooks/useWhpApi';
|
||||
import { useNodeProp, closeContinuousBurst } from '../../panels/right/styles/shared';
|
||||
import { WhpConfig } from '../../types';
|
||||
|
||||
/**
|
||||
* The undo history must start at the state the customer sees, never before it.
|
||||
* Against a REAL Craft.js editor: the load-time deserialize and every page /
|
||||
* header / footer switch reset history, and a continuous-edit burst never
|
||||
* merges into an entry it did not create (that is how Undo could fold back
|
||||
* into the load and blank the page).
|
||||
*/
|
||||
const node = (id: string, text: string) => ({
|
||||
type: { resolvedName: 'Heading' }, isCanvas: false,
|
||||
props: { text, level: 'h2', style: { color: '#000000' } },
|
||||
displayName: 'Heading', custom: {}, hidden: false, parent: 'ROOT', nodes: [], linkedNodes: {},
|
||||
});
|
||||
const pageState = (id: string, text: string) => JSON.stringify({
|
||||
ROOT: {
|
||||
type: { resolvedName: 'Container' }, isCanvas: true, props: { style: {}, tag: 'div' },
|
||||
displayName: 'Container', custom: {}, hidden: false, nodes: [id], linkedNodes: {},
|
||||
},
|
||||
[id]: node(id, text),
|
||||
});
|
||||
|
||||
const whpConfig: WhpConfig = {
|
||||
user: 'u', apiUrl: '/api', csrfToken: 't', siteId: 1,
|
||||
siteDomain: 'example.com', siteName: 'Example', backUrl: '/', isRoot: false,
|
||||
};
|
||||
|
||||
const project = {
|
||||
success: true,
|
||||
rev: 'r1',
|
||||
project: {
|
||||
pages_craft_state: [
|
||||
{ id: 'home', name: 'Home', slug: 'index', craftState: pageState('h-1', 'Home text') },
|
||||
{ id: 'about', name: 'About', slug: 'about', craftState: pageState('a-1', 'About text') },
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
let harness: EditorHarness | null = null;
|
||||
let api: { load: () => Promise<unknown>; switchPage: (id: string) => void; activePageId: string };
|
||||
|
||||
const Probe: React.FC = () => {
|
||||
const { load } = useWhpApi();
|
||||
const { switchPage, activePageId } = usePages();
|
||||
api = { load, switchPage, activePageId };
|
||||
return null;
|
||||
};
|
||||
|
||||
function mountWhp(extra?: React.ReactNode) {
|
||||
harness = renderEditorHarness();
|
||||
harness.mountChild(
|
||||
<EditorConfigProvider config={whpConfig}>
|
||||
<SiteDesignProvider>
|
||||
<PageProvider>
|
||||
<Probe />
|
||||
{extra}
|
||||
</PageProvider>
|
||||
</SiteDesignProvider>
|
||||
</EditorConfigProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
const rootNodes = () => harness!.query.node('ROOT').get().data.nodes as string[];
|
||||
const textOf = (id: string) => harness!.query.node(id).get().data.props.text;
|
||||
const undo = () => harness!.act(() => { harness!.actions.history.undo(); });
|
||||
const canUndo = () => harness!.query.history.canUndo() as boolean;
|
||||
|
||||
async function load() {
|
||||
await harness!.act(async () => { await api.load(); });
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: true, status: 200, json: async () => project })));
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
});
|
||||
afterEach(() => {
|
||||
closeContinuousBurst();
|
||||
harness?.unmount();
|
||||
harness = null;
|
||||
vi.useRealTimers();
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('history baseline after load', () => {
|
||||
test('a loaded populated project has nothing to undo, and Undo does not blank it', async () => {
|
||||
mountWhp();
|
||||
await load();
|
||||
expect(rootNodes()).toEqual(['h-1']);
|
||||
expect(canUndo()).toBe(false);
|
||||
undo();
|
||||
expect(rootNodes()).toEqual(['h-1']);
|
||||
expect(textOf('h-1')).toBe('Home text');
|
||||
});
|
||||
});
|
||||
|
||||
describe('history baseline after a page switch', () => {
|
||||
test('Undo right after switching pages does not bring the previous page back', async () => {
|
||||
vi.useFakeTimers();
|
||||
mountWhp();
|
||||
await load();
|
||||
// An undoable edit on Home, then switch to About.
|
||||
harness!.act(() => { harness!.actions.setProp('h-1', (p: any) => { p.text = 'Edited home'; }); });
|
||||
expect(canUndo()).toBe(true);
|
||||
harness!.act(() => { api.switchPage('about'); });
|
||||
harness!.act(() => { vi.advanceTimersByTime(0); });
|
||||
expect(rootNodes()).toEqual(['a-1']);
|
||||
expect(canUndo()).toBe(false);
|
||||
undo();
|
||||
undo();
|
||||
expect(rootNodes()).toEqual(['a-1']);
|
||||
expect(textOf('a-1')).toBe('About text');
|
||||
});
|
||||
|
||||
test('switching to the header and back also starts a fresh history', async () => {
|
||||
vi.useFakeTimers();
|
||||
mountWhp();
|
||||
await load();
|
||||
harness!.act(() => { harness!.actions.setProp('h-1', (p: any) => { p.text = 'Edited home'; }); });
|
||||
harness!.act(() => { api.switchPage('__header__'); });
|
||||
harness!.act(() => { vi.advanceTimersByTime(0); });
|
||||
expect(canUndo()).toBe(false);
|
||||
harness!.act(() => { api.switchPage('home'); });
|
||||
harness!.act(() => { vi.advanceTimersByTime(0); });
|
||||
expect(canUndo()).toBe(false);
|
||||
expect(textOf('h-1')).toBe('Edited home');
|
||||
});
|
||||
});
|
||||
|
||||
/** A continuous control (colour / text) wired through the real useNodeProp. */
|
||||
const BurstProbe: React.FC = () => {
|
||||
const { setPropContinuous, setPropStyle } = useNodeProp('h-1');
|
||||
(window as any).__burst = { setPropContinuous, setPropStyle };
|
||||
return null;
|
||||
};
|
||||
const burst = () => (window as any).__burst as {
|
||||
setPropContinuous: (k: string, v: string) => void;
|
||||
setPropStyle: (k: string, v: string) => void;
|
||||
};
|
||||
|
||||
describe('a burst that opens with a no-op change', () => {
|
||||
test('no-op first change, then a real change: Undo restores only the real change and never blanks the canvas', () => {
|
||||
// Deliberately keep the pre-edit deserialize entry in history (as any
|
||||
// earlier entry would be): the burst must not merge into it.
|
||||
harness = renderEditorHarness({ initialState: pageState('h-1', 'Home text') });
|
||||
harness.mountChild(<BurstProbe />);
|
||||
expect(canUndo()).toBe(true); // the foreign (load-like) entry
|
||||
harness.act(() => { burst().setPropContinuous('text', 'Home text'); }); // no-op plain prop
|
||||
harness.act(() => { burst().setPropContinuous('text', 'Home text!'); }); // real
|
||||
expect(textOf('h-1')).toBe('Home text!');
|
||||
undo();
|
||||
expect(rootNodes()).toEqual(['h-1']);
|
||||
expect(textOf('h-1')).toBe('Home text');
|
||||
});
|
||||
|
||||
test('no-op first change after a discrete edit: Undo reverts the text but keeps the discrete edit', () => {
|
||||
harness = renderEditorHarness({ initialState: pageState('h-1', 'Home text') });
|
||||
harness.mountChild(<BurstProbe />);
|
||||
harness.act(() => { harness!.actions.history.clear(); });
|
||||
harness.act(() => { burst().setPropStyle('fontWeight', '700'); });
|
||||
harness.act(() => { burst().setPropContinuous('text', 'Home text'); }); // no-op
|
||||
harness.act(() => { burst().setPropContinuous('text', 'Home text!'); });
|
||||
undo();
|
||||
expect(textOf('h-1')).toBe('Home text');
|
||||
expect(harness.query.node('h-1').get().data.props.style.fontWeight).toBe('700');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import { describe, test, expect, afterEach, beforeEach, vi } from 'vitest';
|
||||
import React from 'react';
|
||||
import { renderEditorHarness, EditorHarness } from '../editorHarness';
|
||||
|
||||
vi.mock('../../panels/topbar/TemplateModal', () => ({ TemplateModal: () => null }));
|
||||
vi.mock('../../panels/topbar/HeadCodeModal', () => ({ HeadCodeModal: () => null }));
|
||||
vi.mock('../../panels/sitesmith/SitesmithButton', () => ({ SitesmithButton: () => null }));
|
||||
|
||||
import { EditorConfigProvider } from '../../state/EditorConfigContext';
|
||||
import { SaveGuardProvider } from '../../state/SaveGuardContext';
|
||||
import { SiteDesignProvider } from '../../state/SiteDesignContext';
|
||||
import { PageProvider } from '../../state/PageContext';
|
||||
import { LoadGate } from '../../editor/LoadGate';
|
||||
import { TopBar } from '../../panels/topbar/TopBar';
|
||||
import { WhpConfig } from '../../types';
|
||||
|
||||
/**
|
||||
* The empty-save banner's Undo, driven through the real LoadGate + TopBar on
|
||||
* a REAL Craft.js editor: right after load there is nothing to undo, so the
|
||||
* button must be disabled rather than silently doing nothing (or, before the
|
||||
* history reset, blanking the page).
|
||||
*/
|
||||
const pageState = JSON.stringify({
|
||||
ROOT: {
|
||||
type: { resolvedName: 'Container' }, isCanvas: true, props: { style: {}, tag: 'div' },
|
||||
displayName: 'Container', custom: {}, hidden: false, nodes: ['h-1'], linkedNodes: {},
|
||||
},
|
||||
'h-1': {
|
||||
type: { resolvedName: 'Heading' }, isCanvas: false,
|
||||
props: { text: 'Home text', level: 'h2', style: {} },
|
||||
displayName: 'Heading', custom: {}, hidden: false, parent: 'ROOT', nodes: [], linkedNodes: {},
|
||||
},
|
||||
});
|
||||
|
||||
const whpConfig: WhpConfig = {
|
||||
user: 'u', apiUrl: '/api', csrfToken: 't', siteId: 1,
|
||||
siteDomain: 'example.com', siteName: 'Example', backUrl: '/', isRoot: false,
|
||||
};
|
||||
const jsonResp = (body: unknown, status = 200) => ({ ok: status >= 200 && status < 300, status, json: async () => body });
|
||||
|
||||
let harness: EditorHarness | null = null;
|
||||
const q = (sel: string) => harness!.container.querySelector(sel) as HTMLButtonElement | null;
|
||||
const flush = () => harness!.act(async () => { for (let i = 0; i < 10; i++) await Promise.resolve(); });
|
||||
|
||||
beforeEach(() => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
vi.stubGlobal('fetch', vi.fn(async (url: string) => String(url).includes('action=load')
|
||||
? jsonResp({ success: true, rev: 'r1', project: { pages_craft_state: [{ id: 'home', name: 'Home', slug: 'index', craftState: pageState }] } })
|
||||
: jsonResp({ success: false, error: 'empty_overwrite_refused', message: 'e' }, 409)));
|
||||
});
|
||||
afterEach(() => {
|
||||
harness?.unmount();
|
||||
harness = null;
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('empty-save banner Undo', () => {
|
||||
test('is disabled when there is nothing to undo, and enabled once there is', async () => {
|
||||
harness = renderEditorHarness();
|
||||
harness.mountChild(
|
||||
<EditorConfigProvider config={whpConfig}>
|
||||
<SaveGuardProvider>
|
||||
<SiteDesignProvider>
|
||||
<PageProvider>
|
||||
<LoadGate />
|
||||
<TopBar device="desktop" onDeviceChange={() => {}} showGuides onToggleGuides={() => {}} />
|
||||
</PageProvider>
|
||||
</SiteDesignProvider>
|
||||
</SaveGuardProvider>
|
||||
</EditorConfigProvider>,
|
||||
);
|
||||
await flush();
|
||||
await harness.act(async () => { q('button[aria-label="Save Draft"]')!.click(); });
|
||||
await flush();
|
||||
expect(q('[data-testid="empty-save-banner"]')).not.toBeNull();
|
||||
expect(q('[data-testid="empty-save-undo"]')!.disabled).toBe(true);
|
||||
harness.act(() => { q('[data-testid="empty-save-undo"]')!.click(); });
|
||||
expect(harness.query.node('ROOT').get().data.nodes).toEqual(['h-1']);
|
||||
|
||||
harness.act(() => { harness!.actions.setProp('h-1', (p: any) => { p.text = 'x'; }); });
|
||||
expect(q('[data-testid="empty-save-undo"]')!.disabled).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user