diff --git a/packages/studio-server/src/helpers/sourceMutation.richText.test.ts b/packages/studio-server/src/helpers/sourceMutation.richText.test.ts index 0e9687196d..0fb0a04e9e 100644 --- a/packages/studio-server/src/helpers/sourceMutation.richText.test.ts +++ b/packages/studio-server/src/helpers/sourceMutation.richText.test.ts @@ -25,7 +25,9 @@ describe("rich-text patch operation", () => { ); expect(matched).toBe(true); - expect(html).toContain('o'); + // The id is minted here so the bytes Studio records match the bytes on + // disk — see stampNewChildIds. + expect(html).toMatch(/o<\/span>/); }); it("keeps the words and drops the script when the payload is hostile", () => { @@ -34,7 +36,7 @@ describe("rich-text patch operation", () => { expect(html).not.toContain("script"); expect(html).not.toContain("alert"); expect(html).toContain("still"); - expect(html).toContain("here"); + expect(html).toMatch(/here<\/b>/); }); it("strips an event handler smuggled onto an allowed tag", () => { @@ -115,7 +117,7 @@ describe("rich-text round trips what a real composition contains", () => { it("keeps a line break", () => { const { html } = patchTitle("safe", "a
b", "rich-text"); - expect(html).toContain("
"); + expect(html).toMatch(/
/); }); it("keeps the wrapper span a flex element needs", () => { @@ -125,7 +127,9 @@ describe("rich-text round trips what a real composition contains", () => { "rich-text", ); - expect(html).toContain('a b c'); + expect(html).toMatch( + /a b<\/span> c<\/span>/, + ); }); it("empties the element when every character was deleted", () => { diff --git a/packages/studio-server/src/helpers/sourceMutation.test.ts b/packages/studio-server/src/helpers/sourceMutation.test.ts index 476fceac26..3520979fa0 100644 --- a/packages/studio-server/src/helpers/sourceMutation.test.ts +++ b/packages/studio-server/src/helpers/sourceMutation.test.ts @@ -542,3 +542,34 @@ describe("T7 — data-hf-id targeting (spec for R1)", () => { expect(html).toContain('data-hf-id="hf-a1b2"'); }); }); + +/** + * A rich-text operation adds elements, so it has to give them their stable ids + * here, in the bytes it writes and returns. + * + * Otherwise the next preview request mints them and writes the file a second + * time, after Studio has recorded the edit. The recorded "after" stops matching + * disk, the content check refuses, and undo reports the file as changed outside + * Studio — for every colour applied to a run of characters. + */ +describe("patchElementInHtml stamps the ids a rich-text patch introduces", () => { + it("gives each new span its id in the same write", () => { + const source = '
plain
'; + const { html, matched } = patchElementInHtml(source, { id: "t" }, [ + { type: "rich-text", property: "", value: 'abc' }, + ]); + + expect(matched).toBe(true); + expect(html).toContain("color: red"); + expect((html.match(/data-hf-id=/g) ?? []).length).toBe(2); + }); + + it("leaves an id a rich-text patch carried in alone", () => { + const source = '
plain
'; + const { html } = patchElementInHtml(source, { id: "t" }, [ + { type: "rich-text", property: "", value: 'b' }, + ]); + + expect(html).toContain('data-hf-id="hf-keep"'); + }); +}); diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index b61550975f..ac7c8562b6 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -3,7 +3,7 @@ import postcss from "postcss"; import selectorParser from "postcss-selector-parser"; import { isAllowedHtmlAttribute, isSafeAttributeValue } from "@hyperframes/core/html-attr-safety"; import { sanitizeRichTextChildren } from "@hyperframes/core/rich-text-sanitize"; -import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; +import { EXCLUDED_TAGS, ensureHfIds, mintHfId } from "@hyperframes/parsers/hf-ids"; import { readClipTiming, writeClipTiming } from "@hyperframes/core/composition-contract"; import { parseStyleDecls, patchStyleAttrString } from "./sourceStyleMutation.js"; @@ -159,6 +159,36 @@ function resolveOperationTarget(parent: HTMLElement, op: PatchOperation): HTMLEl } } +/** + * Give the elements a rich-text patch just introduced their stable ids, here, + * in the bytes about to be written and handed back. + * + * Otherwise the next preview request mints them and writes the file a second + * time, after Studio has already recorded the edit in its history. The recorded + * "after" stops matching disk, the content check refuses, and undo reports the + * file as changed outside Studio — for every colour applied to a run of + * characters and every text layer added. The clip split stamps its own clone + * for exactly this reason. + * + * Minted one element at a time with the same function `ensureHfIds` uses, so + * these ids are the ones the next pass would have assigned. Not `ensureHfIds` + * itself: it takes a whole document, and handing it this element's markup would + * put the markup back as one. + */ +function stampNewChildIds(parent: Element): void { + const assigned = new Set(); + const root = parent.ownerDocument?.body ?? parent; + for (const el of root.querySelectorAll("[data-hf-id]")) { + const id = el.getAttribute("data-hf-id"); + if (id) assigned.add(id); + } + for (const el of parent.querySelectorAll("*")) { + if (el.getAttribute("data-hf-id")) continue; + if (EXCLUDED_TAGS.has(el.tagName.toLowerCase())) continue; + el.setAttribute("data-hf-id", mintHfId(el, assigned)); + } +} + // fallow-ignore-next-line complexity export function patchElementInHtml( source: string, @@ -224,6 +254,7 @@ export function patchElementInHtml( if (op.value != null) { opTarget.innerHTML = op.value; sanitizeRichTextChildren(opTarget); + stampNewChildIds(opTarget); } break; } diff --git a/packages/studio/src/components/StudioHeader.tsx b/packages/studio/src/components/StudioHeader.tsx index 6154a4d33e..9efe2e2a22 100644 --- a/packages/studio/src/components/StudioHeader.tsx +++ b/packages/studio/src/components/StudioHeader.tsx @@ -1,6 +1,5 @@ import { useRef, type MouseEvent } from "react"; -import { RotateCcw, RotateCw, Camera } from "../icons/SystemIcons"; -import { getHistoryShortcutLabel } from "../utils/studioHelpers"; +import { Camera } from "../icons/SystemIcons"; import { useStudioShellContext } from "../contexts/StudioContext"; import { usePanelLayoutContext } from "../contexts/PanelLayoutContext"; import { useViewMode, type StudioViewMode } from "../contexts/ViewModeContext"; @@ -207,7 +206,7 @@ export function StudioHeader({ inspectorPanelActive, onExport, }: StudioHeaderProps) { - const { projectId, editHistory, handleUndo, handleRedo, renderQueue } = useStudioShellContext(); + const { projectId, renderQueue } = useStudioShellContext(); const { rightCollapsed, setRightCollapsed, setRightPanelTab } = usePanelLayoutContext(); const isRendering = renderQueue.isRendering; @@ -225,56 +224,6 @@ export function StudioHeader({ {/* Right: toolbar buttons */}
- - - - - -
+ {/* Undo/redo sit with the editing actions rather than in the header: + they act on the timeline and the canvas, which are both down here, + and the header is for the project rather than the edit. */} + {shell ? ( + <> + + + + + + + {/* Divider: history | tool-mode */} +