diff --git a/docs/modernization/260.md b/docs/modernization/260.md index 2d1dce31..da809af4 100644 --- a/docs/modernization/260.md +++ b/docs/modernization/260.md @@ -36,7 +36,7 @@ A separate pre-existing coordinator history issue remains for the final batch: o Checked entries are migrated in this tree. Keep #260 open until the complete group has merged and its exact upstream CI/Vercel result and public deployment have been verified. No legacy component is deliberately retained without a follow-up. -- [ ] `src/lib/components/editor/Editor.svelte` +- [x] `src/lib/components/editor/Editor.svelte` - [x] `src/lib/components/editor/EditorAbove.svelte` - [x] `src/lib/components/editor/EditorCharge.svelte` - [x] `src/lib/components/editor/EditorControls.svelte` @@ -66,8 +66,12 @@ Checked entries are migrated in this tree. Keep #260 open until the complete gro - [x] `src/lib/components/editor/Positions.svelte` - [x] `src/lib/components/editor/Switch.svelte` -## Final batch +## Final batch: editor coordinator -Editor.svelte remains. Preserve initialization order, menu/COA synchronization and normalization, and fix the consecutive-edit history boundary described above. Verify undo/redo after uninterrupted sequences as well as individual edit/undo/redo cycles, JSON/shared-link reload, viewer/editor transitions and FMG synchronization. +`Editor.svelte` uses runes with explicit reroll, external-history restoration and menu-commit boundaries. History published by the editor does not re-enter restoration. Imported/legacy defaults are normalized synchronously in the existing entry; the next fresh edit always adds a new step. Rerolls are keyed by the gallery history id so unrelated shared-store changes (rotation, path selection and overlays) cannot reset history. Menu normalization uses a detached copy, and artwork passed to the SVG renderer remains plain data. -The first three batches migrate **28 of 29 components, with Editor.svelte remaining**. Verify each merged batch before marking it complete on #260. Use fixtures for claims and Discord; parent #237 remains open for its other follow-ups. +Three new cases in `test:editor-geometry` fail on the unchanged `fddc0c8` baseline and pass here: uninterrupted shadow edits with undo/redo and replacement branches, consecutive edits across layers, and legacy-entry normalization followed by fresh edits. All eleven previous geometry cases pass on both trees. Nine representative exports have identical metadata and PNG bytes; SVGs match after normalizing only random shield/division/inscription instance IDs. + +Validation: 211 service/browser tests, strict application/service-worker checks (0 errors, the existing 92 accessibility warnings), credential-free Vercel build, three deployment-output tests and one production update/offline test pass. Targeted coordinator ESLint and formatting checks pass. Existing suites cover JSON/shared links, viewer/gallery transitions, reroll/rollback, exports and FMG synchronization. + +All **29 of 29 editor components** are migrated in this tree. Keep #260 open until this batch is merged and the exact upstream CI/Vercel result and public deployment are verified. Parent #237 still tracks #261, #262 and #264. diff --git a/src/lib/components/editor/Editor.svelte b/src/lib/components/editor/Editor.svelte index d55c5b92..d5202a5d 100644 --- a/src/lib/components/editor/Editor.svelte +++ b/src/lib/components/editor/Editor.svelte @@ -8,11 +8,21 @@ EditorInscription as InscriptionMenu } from "#lib/types/editor.js"; import {settingsStorage} from "#lib/data/storage.ts"; - import {tick} from "svelte"; + import {tick, untrack} from "svelte"; import {t, dictionary, locale} from "svelte-i18n"; import {fade, fly, slide} from "svelte/transition"; import {DEFAULT_ZOOM} from "#lib/config/defaults.ts"; - import {changes, grid, history, isTextReady, message, shield, showGrid, state, tinctures, uploaded} from "#lib/data/stores.ts"; + import { + changes, + grid, + history, + isTextReady, + message, + showGrid, + state as editorState, + tinctures, + uploaded + } from "#lib/data/stores.ts"; import {charges, chargeSets, divisions, ordinaries, shields, shieldSets} from "#lib/data/dataModel.js"; import {createConfig, generate, getTincture} from "#lib/scripts/generator.js"; import {highlight, lowlight} from "#lib/scripts/highlight.js"; @@ -39,11 +49,9 @@ import EditorStriped from "./EditorStriped.svelte"; import EditorStroke from "./EditorStroke.svelte"; import EditorTincture from "./EditorTincture.svelte"; - export let historyId: number; - export let seed: string | number | undefined; + let {historyId, seed}: {historyId: number; seed: string | number | undefined} = $props(); - let restoring = false; - let section = { + let section = $state({ shield: false, general: false, field: false, @@ -51,43 +59,78 @@ ordinary: [] as boolean[], charge: [] as boolean[], inscription: [] as boolean[] - }; + }); const isLandscape = innerWidth > innerHeight; - $state.transform = null; - $state.positions = null; + $editorState.transform = null; + $editorState.positions = null; + + let coa = $state.raw(untrack(() => $history[historyId] || generate(seed || undefined, $tinctures))); + let menu = $state(untrack(defineMenuState)); + // Our own history publication must not be interpreted as undo/redo. Otherwise + // a restoration flag can leak into the next user edit and replace its entry. + let published = ""; + let activeHistoryId: number | undefined; + + $effect.pre(() => { + const id = historyId; + untrack(() => { + if (id === activeHistoryId) return; + activeHistoryId = id; + reroll(id); + }); + }); + $effect.pre(() => { + const [value] = $changes; + untrack(() => { + if (value && value !== published) restore(value); + }); + }); + $effect.pre(() => { + // Track every bound menu property, but keep normalization and history + // writes outside this effect's dependencies. Uploaded charge data can also + // change which tincture fields are supported. + JSON.stringify(menu); + void $uploaded; + untrack(() => commitMenu(false)); + }); - let coa = $history[historyId] || generate(seed || undefined, $tinctures); // on load - let menu: EditorMenu; - $: restore($changes); // on undo/redo - $: reroll(historyId); // on reroll - $: update(menu, $uploaded); // on menu update and charge upload - $: edit(coa); // on edit // Persist changes, not recovered defaults during initial rendering. let savedGrid = $grid; let savedShowGrid = $showGrid; - $: if ($grid !== savedGrid) { - settingsStorage.setItem("grid", $grid); - savedGrid = $grid; - } - $: if ($showGrid !== savedShowGrid) { - settingsStorage.setItem("showGrid", $showGrid); - savedShowGrid = $showGrid; - } + $effect(() => { + if ($grid !== savedGrid) { + settingsStorage.setItem("grid", $grid); + savedGrid = $grid; + } + }); + $effect(() => { + if ($showGrid !== savedShowGrid) { + settingsStorage.setItem("showGrid", $showGrid); + savedShowGrid = $showGrid; + } + }); function reroll(historyId: number) { - restoring = false; coa = $history[historyId] || generate(seed || undefined, $tinctures); if (!$history[historyId]) $history.push(coa); changes.reset(); menu = defineMenuState(); + commitMenu(false); } - function edit(coa: Coa) { - const value = JSON.stringify(coa); + function commitMenu(restoring: boolean) { + const originalMenu = JSON.stringify(menu); + const normalizedMenu: EditorMenu = JSON.parse(originalMenu); + const next = update(normalizedMenu); + if (JSON.stringify(normalizedMenu) !== originalMenu) menu = normalizedMenu; + const value = JSON.stringify(next); + // A plain, detached COA keeps the legacy SVG renderer and drag handlers + // independent of the menu proxy, and only remounts artwork on real changes. + if (JSON.stringify(coa) !== value) coa = JSON.parse(value); + published = value; if (restoring) changes.replace(value); else changes.add(value); - restoring = false; } const toggleSection = (name: keyof typeof section, index?: number) => async () => { @@ -105,9 +148,10 @@ }; // get coa from menu on menu change - function update(_menu: EditorMenu, _uploaded: unknown) { + function update(menu: EditorMenu): Coa { + const next = {...coa}; // remove seed reference as it would be confusing - delete coa.seed; + delete next.seed; function getTinctureFromField(field: Field) { if (field.type === "tincture") return field.t1; @@ -118,39 +162,39 @@ } // shield attribute changed - coa.shield = menu.shield; + next.shield = menu.shield; // name attribute changed if (menu.name) { - coa.name = menu.name; + next.name = menu.name; } else { - delete coa.name; + delete next.name; } // zoom attribute changed if (menu.zoom !== DEFAULT_ZOOM) { - coa.zoom = menu.zoom; + next.zoom = menu.zoom; } else { - delete coa.zoom; + delete next.zoom; } // field attributes changed - coa.t1 = getTinctureFromField(menu.field); + next.t1 = getTinctureFromField(menu.field); // division attributes changed if (menu.division.division && menu.division.division !== "no") { - coa.division = {division: menu.division.division, t: getTinctureFromField(menu.division)}; - if (divisions.data[menu.division.division].templateLined) coa.division.line = menu.division.line; - coa.division.t = getTinctureFromField(menu.division); + next.division = {division: menu.division.division, t: getTinctureFromField(menu.division)}; + if (divisions.data[menu.division.division].templateLined) next.division.line = menu.division.line; + next.division.t = getTinctureFromField(menu.division); } else { - delete coa.division; + delete next.division; menu.charges.forEach(c => (c.divided = "")); menu.ordinaries.forEach(o => (o.divided = "")); } // ordinary attributes changed if (menu.ordinaries.length) { - coa.ordinaries = menu.ordinaries.map(o => { + next.ordinaries = menu.ordinaries.map(o => { if (o.striped) { o.t2 ||= selectSecondTincture(o.field.t1); } else { @@ -158,7 +202,7 @@ } const item: Ordinary = {ordinary: o.ordinary, t: getTinctureFromField(o.field)}; if (ordinaries.lined[o.ordinary]) item.line = o.line; - if (coa.division && o.divided) item.divided = o.divided; + if (next.division && o.divided) item.divided = o.divided; if (o.showStroke) item.stroke = o.stroke; if (o.showStroke && o.strokeWidth !== 1) item.strokeWidth = o.strokeWidth; if (o.size && o.size !== 1) item.size = o.size; @@ -177,11 +221,11 @@ } return item; }); - } else delete coa.ordinaries; + } else delete next.ordinaries; // charges attributes changed if (menu.charges.length) { - coa.charges = menu.charges.map(c => { + next.charges = menu.charges.map(c => { updateFields(c); const item: Charge = {charge: c.charge, t: getTinctureFromField(c.field1), p: c.p, size: c.size}; if ((charges.data[c.charge]?.colors || 1) > 1 && c.field2) { @@ -207,11 +251,11 @@ if (c.angle) item.angle = c.angle; return item; }); - } else delete coa.charges; + } else delete next.charges; // inscriptions attributes changed if (menu.inscriptions.length) { - coa.inscriptions = menu.inscriptions.map(i => { + next.inscriptions = menu.inscriptions.map(i => { const item: Inscription = {text: i.text, font: i.font, size: i.size, path: i.path}; if (i.bold) item.bold = 1; if (i.italic) item.italic = 1; @@ -220,14 +264,16 @@ if (i.shadow) item.shadow = i.shadow; return item; }); - } else delete coa.inscriptions; + } else delete next.inscriptions; + return next; } - function restore(_changes: [string, number]) { - if (!changes.length()) return; - restoring = true; - coa = JSON.parse($changes[0]); + function restore(value: string) { + coa = JSON.parse(value); menu = defineMenuState(); + // Normalize imported/legacy defaults synchronously in the restored entry. + // No restoration state survives into the next menu edit. + commitMenu(true); } function getField(fieldStr: string): Field { @@ -303,7 +349,7 @@ const striped = o.compony ? "compony" : o.gyronny ? "gyronny" : ""; const componySize = o.compony || 0; const gyronnyNumber = o.gyronny || 0; - if (angle) $state.transform = `rotate(${angle})`; + if (angle) $editorState.transform = `rotate(${angle})`; const ordinaryObj: OrdinaryMenu = { ordinary, field, @@ -347,7 +393,7 @@ const x = c.x || 0; const y = c.y || 0; const angle = c.angle || 0; - if (angle) $state.transform = `rotate(${angle})`; + if (angle) $editorState.transform = `rotate(${angle})`; const chargeObj: ChargeMenu = { charge, type, @@ -498,20 +544,22 @@ menu.inscriptions = [...menu.inscriptions, i]; } - if (!("ontouchstart" in window) && (coa.ordinaries || coa.charges)) { - if (!$message) message.info("info.tipEditControls"); - } + untrack(() => { + if (!("ontouchstart" in window) && (coa.ordinaries || coa.charges)) { + if (!$message) message.info("info.tipEditControls"); + } + }); function isRaster(charge: string) { const el = document.getElementById(charge); return el ? el.tagName === "image" : false; } - $: translateSafely = (group: string, key: string) => { + function translateSafely(group: string, key: string) { const messages = $dictionary[$locale || ""]?.[group]; const isInDictionary = messages && typeof messages === "object" && !Array.isArray(messages) && key in messages; return isInDictionary ? $t(`${group}.${key}`) : key; - }; + }
@@ -520,16 +568,16 @@ {/key} - {#if $state.selectedPath !== -1} + {#if $editorState.selectedPath !== -1}
- +
{/if}
diff --git a/tests/editor-geometry.test.mjs b/tests/editor-geometry.test.mjs index 14a7beeb..94022bfd 100644 --- a/tests/editor-geometry.test.mjs +++ b/tests/editor-geometry.test.mjs @@ -448,3 +448,93 @@ test("inscription and endpoint dragging retain selected path, snapped points and await page.waitForFunction(() => !document.querySelector("#coaEdit .points")); assert.equal((await state(page)).selectedPath, -1); }); + +// Each action settles without an intervening undo/redo. Those operations used +// to clear a stale restoration flag and hide lost consecutive history entries. +async function consecutiveEdit(page, action, verify) { + const before = await snapshot(page); + await action(); + const after = await snapshot(page); + verify(after.coa); + assert.equal(after.index, before.index + 1, "each fresh edit adds an undo step"); + assert.equal(after.length, before.index + 2); + return after; +} + +test("consecutive shadow edits each retain their own undo step and allow a new branch", async t => { + const page = await session(t); + await expand(page, "#inscription_0"); + const p = panel("inscription_0"); + const initial = await snapshot(page); + const enabled = await consecutiveEdit( + page, + () => page.$eval(`${p} ${tip("tooltip.inscriptions.shadow")} .switch-label`, n => n.click()), + coa => assert.equal(coa.inscriptions[0].shadow.x, 1) + ); + const shifted = await consecutiveEdit( + page, + () => input(page, `${p} > .subsection:nth-child(2) ${tip("tooltip.shift")} input:nth-of-type(1)`, "3"), + coa => assert.equal(coa.inscriptions[0].shadow.x, 3) + ); + const blurred = await consecutiveEdit( + page, + () => input(page, `${p} ${tip("tooltip.inscriptions.shadowBlur")} input`, "2"), + coa => assert.equal(coa.inscriptions[0].shadow.blur, 2) + ); + for (const expected of [shifted, enabled, initial]) assert.deepEqual((await history(page, "Z")).coa, expected.coa); + for (const expected of [enabled, shifted, blurred]) assert.deepEqual((await history(page, "X")).coa, expected.coa); + await history(page, "Z"); + const branched = await consecutiveEdit( + page, + () => input(page, `${p} ${tip("tooltip.inscriptions.shadowColor")} input`, "#123456"), + coa => assert.equal(coa.inscriptions[0].shadow.color, "#123456") + ); + assert.equal(branched.coa.inscriptions[0].shadow.blur, 0); + assert.deepEqual((await history(page, "X")).coa, branched.coa); +}); + +test("consecutive edits across layers preserve every history entry", async t => { + const page = await session(t); + await expand(page, "#ordinary_0"); + await expand(page, "#charge_0"); + await expand(page, "#inscription_0"); + const entries = [await snapshot(page)]; + for (const [selector, value, verify] of [ + [`${panel("ordinary_0")} ${tip("tooltip.size")} input`, "125", coa => assert.equal(coa.ordinaries[0].size, 1.25)], + [`${panel("charge_0")} ${tip("tooltip.stretch")} input`, "30", coa => assert.equal(coa.charges[0].stretch, 0.3)], + [ + `${panel("inscription_0")} ${tip("tooltip.inscriptions.text")} input`, + "Consecutive & crest", + coa => assert.equal(coa.inscriptions[0].text, "Consecutive & crest") + ] + ]) + entries.push(await consecutiveEdit(page, () => input(page, selector, value), verify)); + for (const expected of entries.slice(0, -1).reverse()) assert.deepEqual((await history(page, "Z")).coa, expected.coa); + for (const expected of entries.slice(1)) assert.deepEqual((await history(page, "X")).coa, expected.coa); +}); + +test("restored legacy defaults stay in one step before consecutive fresh edits", async t => { + const page = await session(t); + const initial = await snapshot(page); + await page.evaluate(async () => { + const {changes} = await import("/src/lib/data/stores.ts"); + changes.add(JSON.stringify({t1: "or", inscriptions: [{text: "Legacy", font: "Arial", size: 12}]})); + }); + const restored = await snapshot(page); + assert.equal(restored.index, initial.index + 1); + assert.equal(restored.coa.inscriptions[0].path, "M-50 0 L50 0"); + await expand(page, "#inscription_0"); + const selector = `${panel("inscription_0")} ${tip("tooltip.inscriptions.text")} input`; + const first = await consecutiveEdit( + page, + () => input(page, selector, "First"), + coa => assert.equal(coa.inscriptions[0].text, "First") + ); + const second = await consecutiveEdit( + page, + () => input(page, selector, "Second"), + coa => assert.equal(coa.inscriptions[0].text, "Second") + ); + for (const expected of [first, restored, initial]) assert.deepEqual((await history(page, "Z")).coa, expected.coa); + for (const expected of [restored, first, second]) assert.deepEqual((await history(page, "X")).coa, expected.coa); +});