mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 23:47:33 +00:00
fix(studio): keep undo working after a text edit, and move it to the timeline
A rich-text patch adds spans, and it left them without their stable ids. The next preview request minted them and wrote the file a second time — after Studio had recorded the edit in its history. The recorded "after" no longer matched the bytes on disk, so the content check refused and undo reported the file as changed outside Studio. Every colour applied to a run of characters and every text layer added poisoned the history the same way. The patch now mints those ids itself, with the same function the later pass would have used, in the bytes it writes and hands back. The clip split already did this for its clone and its comment names this exact failure. Undo and redo also move out of the header and into the timeline toolbar, left of the selection tool. They act on the timeline and the canvas; the header is for the project. They read the shell optionally, so the standalone timeline the player package exports still mounts without a project.
This commit is contained in:
@@ -25,7 +25,9 @@ describe("rich-text patch operation", () => {
|
||||
);
|
||||
|
||||
expect(matched).toBe(true);
|
||||
expect(html).toContain('<span style="color: red">o</span>');
|
||||
// The id is minted here so the bytes Studio records match the bytes on
|
||||
// disk — see stampNewChildIds.
|
||||
expect(html).toMatch(/<span data-hf-id="hf-[^"]+" style="color: red">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("<b>here</b>");
|
||||
expect(html).toMatch(/<b data-hf-id="hf-[^"]+">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<br>b", "rich-text");
|
||||
|
||||
expect(html).toContain("<br>");
|
||||
expect(html).toMatch(/<br data-hf-id="hf-[^"]+">/);
|
||||
});
|
||||
|
||||
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('<span>a <span style="color: red">b</span> c</span>');
|
||||
expect(html).toMatch(
|
||||
/<span data-hf-id="hf-[^"]+">a <span data-hf-id="hf-[^"]+" style="color: red">b<\/span> c<\/span>/,
|
||||
);
|
||||
});
|
||||
|
||||
it("empties the element when every character was deleted", () => {
|
||||
|
||||
@@ -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 = '<div data-hf-id="hf-a" id="t">plain</div>';
|
||||
const { html, matched } = patchElementInHtml(source, { id: "t" }, [
|
||||
{ type: "rich-text", property: "", value: 'a<span style="color: red">b</span>c' },
|
||||
]);
|
||||
|
||||
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 = '<div data-hf-id="hf-a" id="t">plain</div>';
|
||||
const { html } = patchElementInHtml(source, { id: "t" }, [
|
||||
{ type: "rich-text", property: "", value: '<span data-hf-id="hf-keep">b</span>' },
|
||||
]);
|
||||
|
||||
expect(html).toContain('data-hf-id="hf-keep"');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<string>();
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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({
|
||||
<ViewModeToggle />
|
||||
{/* Right: toolbar buttons */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Tooltip
|
||||
label={
|
||||
editHistory.undoLabel
|
||||
? `Undo ${editHistory.undoLabel} (${getHistoryShortcutLabel("undo")})`
|
||||
: `Undo (${getHistoryShortcutLabel("undo")})`
|
||||
}
|
||||
side="bottom"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
trackStudioEvent("toolbar_action", { action: "undo" });
|
||||
void handleUndo();
|
||||
}}
|
||||
disabled={!editHistory.canUndo}
|
||||
className={`h-7 w-7 flex items-center justify-center rounded-md transition-colors active:scale-[0.98] ${
|
||||
editHistory.canUndo
|
||||
? "text-neutral-400 hover:text-neutral-200 hover:bg-neutral-800"
|
||||
: "text-neutral-700 cursor-default"
|
||||
}`}
|
||||
aria-label="Undo"
|
||||
>
|
||||
<RotateCcw size={14} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
<Tooltip
|
||||
label={
|
||||
editHistory.redoLabel
|
||||
? `Redo ${editHistory.redoLabel} (${getHistoryShortcutLabel("redo")})`
|
||||
: `Redo (${getHistoryShortcutLabel("redo")})`
|
||||
}
|
||||
side="bottom"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
trackStudioEvent("toolbar_action", { action: "redo" });
|
||||
void handleRedo();
|
||||
}}
|
||||
disabled={!editHistory.canRedo}
|
||||
className={`h-7 w-7 flex items-center justify-center rounded-md transition-colors active:scale-[0.98] ${
|
||||
editHistory.canRedo
|
||||
? "text-neutral-400 hover:text-neutral-200 hover:bg-neutral-800"
|
||||
: "text-neutral-700 cursor-default"
|
||||
}`}
|
||||
aria-label="Redo"
|
||||
>
|
||||
<RotateCw size={14} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
<Tooltip label={capturing ? "Capturing frame…" : "Capture current frame"} side="bottom">
|
||||
<a
|
||||
href={captureFrameHref}
|
||||
|
||||
@@ -16,6 +16,10 @@ import {
|
||||
import { useTimelineZoom } from "../player/components/useTimelineZoom";
|
||||
import { usePlayerStore, type TimelineElement } from "../player";
|
||||
import { Tooltip } from "./ui";
|
||||
import { RotateCcw, RotateCw } from "../icons/SystemIcons";
|
||||
import { useStudioShellContextOptional } from "../contexts/StudioContext";
|
||||
import { getHistoryShortcutLabel } from "../utils/studioHelpers";
|
||||
import { trackStudioEvent } from "../utils/studioTelemetry";
|
||||
import { Scissors } from "../icons/SystemIcons";
|
||||
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
|
||||
import type { DomEditSelection } from "./editor/domEditingTypes";
|
||||
@@ -160,6 +164,10 @@ export function TimelineToolbar({ domEditSession, onSplitElement }: TimelineTool
|
||||
|
||||
// CapCut-flat icon buttons: no per-button border/box chrome — a transparent
|
||||
// 28px hit area with a subtle rounded hover wash, consistent 16px glyphs.
|
||||
// Optional: the player package mounts this timeline standalone, where there
|
||||
// is no project and so no edit history to step through.
|
||||
const shell = useStudioShellContextOptional();
|
||||
|
||||
const flatBtn = "flex h-7 w-7 items-center justify-center rounded-md transition-colors";
|
||||
const flatIdle = `${flatBtn} text-neutral-400 hover:bg-white/[0.06] hover:text-neutral-200 active:scale-[0.98]`;
|
||||
const flatActive = `${flatBtn} bg-white/[0.08] text-neutral-100 active:scale-[0.98]`;
|
||||
@@ -171,6 +179,55 @@ export function TimelineToolbar({ domEditSession, onSplitElement }: TimelineTool
|
||||
<div className="border-b border-neutral-800/60">
|
||||
<div className="flex items-center justify-between px-2 py-0.5">
|
||||
<div className="flex items-center gap-0.5">
|
||||
{/* 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 ? (
|
||||
<>
|
||||
<Tooltip
|
||||
label={
|
||||
shell.editHistory.undoLabel
|
||||
? `Undo ${shell.editHistory.undoLabel} (${getHistoryShortcutLabel("undo")})`
|
||||
: `Undo (${getHistoryShortcutLabel("undo")})`
|
||||
}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
trackStudioEvent("toolbar_action", { action: "undo" });
|
||||
void shell.handleUndo();
|
||||
}}
|
||||
disabled={!shell.editHistory.canUndo}
|
||||
aria-label="Undo"
|
||||
className={shell.editHistory.canUndo ? flatIdle : flatDisabled}
|
||||
>
|
||||
<RotateCcw size={16} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
<Tooltip
|
||||
label={
|
||||
shell.editHistory.redoLabel
|
||||
? `Redo ${shell.editHistory.redoLabel} (${getHistoryShortcutLabel("redo")})`
|
||||
: `Redo (${getHistoryShortcutLabel("redo")})`
|
||||
}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
trackStudioEvent("toolbar_action", { action: "redo" });
|
||||
void shell.handleRedo();
|
||||
}}
|
||||
disabled={!shell.editHistory.canRedo}
|
||||
aria-label="Redo"
|
||||
className={shell.editHistory.canRedo ? flatIdle : flatDisabled}
|
||||
>
|
||||
<RotateCw size={16} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
{/* Divider: history | tool-mode */}
|
||||
<div aria-hidden="true" className="mx-1 h-4 w-px bg-neutral-800" />
|
||||
</>
|
||||
) : null}
|
||||
<Tooltip label="Selection tool (V)">
|
||||
<button
|
||||
type="button"
|
||||
|
||||
Reference in New Issue
Block a user