mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
* test(studio): text fields and text edits in a sealed preview * fix(studio): text fields and text edits work in a sealed preview * refactor(core): shorter notes on edit-element markup * refactor(studio): name the sealed text edit's steps so the code audit passes * refactor(studio): share the text commits' steps and split the put-back check * refactor(studio): say the sealed text edit's steps in names, not comments * feat(core): a sealed preview's frame runs an in-place text editing session for Studio * fix(core): a text edit session restores a removed element and ends only for its own port * feat(core): a sealed text edit ends keeping the text when its element loses focus * test(studio): in-place text editing in a sealed preview runs in the frame and saves * feat(studio): edit text in place on a sealed preview's canvas * refactor(core): drop two comments the text edit helpers' names already carry * fix(studio): a sealed text edit saves even when the selection moved before the frame replied * fix(studio): a sealed text edit's selection box grows with the text as it is typed * test(studio): a sealed text edit refuses a frame reply carrying a script or handler * fix(studio): a sealed text edit saves only text markup the frame returns Studio re-runs the frame's own test, isTextMarkup, on the html and previous a sealed frame returns, through textEditResult and the ended-event parser in core. A reply or ended edit carrying anything else ends the edit with the frame's original back and saves nothing. * refactor(core): name the frame-reply markup check instead of explaining it * test(studio): sealed text rollback, blur before unmount, and Enter on a refused layer * fix(studio): sealed text edits keep newer drafts, save once on unmount, free Enter on refused layers * test(studio): a blur-ended sealed save holds the drain, and over-limit text leaves Enter alone * fix(studio): a blur-ended sealed save holds the drain; one size check for editable text * test(studio): sealed inline tests dispose each frame's client, so no edit outlives its test * refactor(studio): split the sealed end reply and edit-element parsing; share hook parameters * test(studio): one table for an inline edit's save lifecycle, live and sealed * fix(studio): an open inline text edit settles on a drain and ends when its element goes * test(studio): an element removed mid-edit comes back showing its original, live and sealed * fix(studio): a live edit's element removed mid-edit comes back clean, as a sealed one does * test(studio): inline edit rows for a reopened draft, a re-added node and removal before focus * fix(studio): an inline edit settles only its own session and ends when its element leaves * test(studio): a reopened sealed edit types only after the frame handled its start * test(studio): an untouched inline edit whose element goes saves nothing and says nothing * fix(studio): an untouched inline edit settles with no save and no message * test(studio): inline edit tests type before they commit; an untouched commit saves nothing * test(studio): inline text saves keep their order per heading and a refused redraw shows after cancel * fix(studio): inline text saves land in begin order per heading; an open edit shows a refused redraw * test(studio): the sealed test frame reloads with a new runtime over the same heading * refactor(studio): keep the rich text save order in one ref * test(studio): a held lookup belongs to the save that reached it * test(studio): inline text rows for late end replies, Design panel saves and refused redraws * fix(studio): text saves land in request order per target; the canvas shows the newest landed save * test(studio): Design panel rows edit a plain paragraph the panel can write * test(studio): round-8 rows for late blurs, panel saves, skipped saves, reloads and retired edits * fix(studio): the save pipeline owns the newest saved text per target, for every writer * test(studio): a panel save made over an open inline draft saves the draft first * fix(studio): a failed first text save draws the file again through the save drain * test(studio): reload rows keep one hook across the reload, as the app does * refactor(studio): move a text save's write steps out of the text commit hook * fix(studio): the rich-text save imports the resync it uses from the moved steps * test(studio): round-9 rows for an added field, an end in flight and a write after a cancel * fix(studio): panel text saves plan against what landed and wait for a draft already ending * test(studio): round-9 rows pin the added field whole and the superseded panel save * test(studio): the added-field row keeps the space between the fields that were there * test(studio): generated inline text sequences, sealed and unsealed, on the shared preview harness * test(studio): a full reload loads the landed file, and an open edit ends at the sequence's end * test(studio): each generated text seed reports every mismatch, sealed and unsealed * test(studio): the text mount re-renders on each selection, and keys reach only an element in an edit * test(studio): write fates follow write order per seed, and the model keeps frame reloads * test(studio): the text table names writes Studio skipped, and compares styles in one order * test(studio): saves queued on an element land before a new inline edit on it opens * fix(studio): a new inline edit opens after the element's queued text saves settle * test(studio): the queued-save rows act when the caret shows, as a person does * test(studio): rows that reopened an edit before its save settled now open it after * test(studio): the text table's frame reload waits for a live edit, as Studio's reload does * test(studio): deadline seeds check a lost save is reported; reload-during-edit seeds wait on row 41 * test(studio): a sealed text edit holds a preview reload until it ends * test(studio): reload-during-edit text seeds hold parity again * fix(studio): a sealed text edit holds a preview reload until it ends, as an unsealed one does * test(studio): the text table holds a reload by the preview's gesture document, as Studio does * fix(studio): a sealed text edit's held reload waits for the end's reply, which carries the draft * test(studio): a sealed edit's held reload waits for the end reply, and the port timeout releases it * test(studio): a reload stays held until a frame-ended sealed edit's save has read the frame * fix(studio): a frame-ended sealed edit holds the reload until its save has read the frame * test(studio): the text table's reload also waits for pending saves, as Studio's shadow reload does * test(studio): typing in a sealed edit whose start reply missed its deadline is saved * fix(studio): a sealed edit whose start reply never came saves what was typed or says it could not * test(studio): two panel saves on different fields of one element both land * fix(studio): a panel text save composes on what landed, so a save on another field never drops it * fix(studio): an older panel text save is superseded only by a newer one on the same field * test(studio): a sealed edit stays open when its start reply misses the deadline * fix(studio): a sealed edit whose start reply misses the deadline stays open and ends like any edit * test(studio): a text table gesture waits for a reload in flight, as a person sees the frame first * test(studio): the text table reads the caret after loads and edits only fields the panel shows * test(studio): the text table tracks each text save as Studio's edit context does * test(studio): a panel remove that ended an edit lands though a newer save claimed first * test(studio): the remove rows answer the newest edit's end first, as the frame may * test(studio): the remove rows open a second edit before the newer save, as seed 31 does * test(studio): the remove rows release both held ends, newest first * fix(studio): a field text save that waited on an edit lands though a newer save claimed first * test(studio): a failed field save takes back its draw, and the next write waits for that * test(studio): the failed add row saves from the panel as the canvas shows it * fix(studio): a failed field text save takes back its own draw before the next save plans * test(studio): a newer unsealed field save never writes a failed save's draw * fix(studio): a field save re-reads its element only after an earlier save on it * fix(studio): the newest failed text save still draws the file again; older ones take back their draw * test(studio): the text table answers a request a released write sends while it settles * test(studio): the text table releases a write that starts waiting while it settles * refactor(studio): one serialization rule for text saves, and the queued-save wait clears its timer * test(studio): the box-wake row's frame geometry carries its layout, as frames now send * test(studio): a sealed text edit keeps the keys until it ends, in the sealed keyboard walk * test(studio): a sealed text edit holds the frame's keyboard until it ends, then gives it back * fix(studio): a sealed text edit keeps the keyboard in the frame until it ends * test(studio): the keyboard rows' frame stays out of the DOM, so only the test loads it * test(studio): a sealed text edit the frame never ends closes once Studio takes focus back * fix(studio): Studio taking focus back ends a sealed text edit and its keyboard hold * test(studio): the sealed keyboard walk ends an edit the film never ends by focusing Studio * test(studio): the sealed keyboard walk follows the preview a save reloads * test(studio): the sealed keyboard walk retries a step whose preview a save replaced * test(studio): the sealed keyboard walk picks the heading again in the reloaded preview * refactor(studio): split the text save helpers and harness so each stays within the audit limits * test(studio): the sealed keyboard walk finds the live preview each step and types once it has focus * test(studio): the sealed keyboard walk picks the heading again when a reload eats its Enter * test(studio): the sealed keyboard walk retries a canvas pick a reload swallowed * refactor(studio): group the text hooks' refs and session-end effects so each stays within the audit * test(studio): a double press opens a sealed heading's text edit * test(studio): split the seeded text sequences over two files so each fits the flake gate * test(studio): an inline edit's text survives a panel edit of another field after it * build(producer): its size budget covers the frame runtime's sealed text editing * fix(studio): a sealed double press opens the layer it picked, at the press, and a lost edit says so A sealed first press is often on a layer the frame has not picked yet, so sealed presses pair by time and place and the second must land on the selection's box. The caret goes where the press landed, and a frame that never answers an end is reported through the edit session's toast, which Desktop provides. * fix(studio): a sealed text edit hands the keyboard back to the control that opened it * test(studio): a panel remove made before the panel shows an add keeps the added field * test(studio): a sealed inline text edit typed in steps saves as one undo step * refactor(studio): a sealed press learns it hit the selection box from its own event The selection box stays as on main. The press handler splits into pairing and opening, sealed presses pair by place in isDoublePress, and the edit session's existing complexity gets the next-line ignore its peers use.
162 lines
6.6 KiB
TypeScript
162 lines
6.6 KiB
TypeScript
// Studio's inline text editing and text saves, mounted in the test page as the canvas wires them, over a file whose
|
|
// writes the test decides: each lands, fails or waits, in the order they reach it.
|
|
import { createElement, useState } from "react";
|
|
import { createRoot } from "react-dom/client";
|
|
import { flushSync } from "react-dom";
|
|
import { patchElementInHtml } from "@hyperframes/studio-server/source-mutation";
|
|
import {
|
|
findPreviewElementForSelection,
|
|
gestureDocumentOf,
|
|
resolveFoundSelection,
|
|
resolvePreviewSelection,
|
|
} from "../src/components/editor/previewSelection";
|
|
import type { DomEditSelection } from "../src/components/editor/domEditing";
|
|
import type { PersistDomEditOperations } from "../src/hooks/domEditCommitTypes";
|
|
import { useDomEditTextCommits } from "../src/hooks/useDomEditTextCommits";
|
|
import { useInlineTextEdit } from "../src/hooks/useInlineTextEdit";
|
|
import {
|
|
afterStudioPendingEdits,
|
|
hasStudioPendingEdits,
|
|
isStudioEditSaving,
|
|
trackedStudioEdit,
|
|
} from "../src/utils/studioPendingEdits";
|
|
import {
|
|
afterStudioManualEditGestures,
|
|
isStudioManualEditGestureLiveIn,
|
|
} from "../src/components/editor/manualEditGesture";
|
|
|
|
const PATH = "index.html";
|
|
const OPTIONS = { activeCompositionPath: PATH, isMasterView: false, skipSourceProbe: true };
|
|
|
|
export type WriteFate = "land" | "fail" | "wait";
|
|
|
|
export interface TextEditing {
|
|
select(): Promise<DomEditSelection>;
|
|
controls: () => ReturnType<typeof useInlineTextEdit>;
|
|
commits: () => ReturnType<typeof useDomEditTextCommits>;
|
|
/** The element's markup in the file, as the patcher left it. */
|
|
saved(): string;
|
|
/** Every write that reached the file, in order, with its value. */
|
|
writes: string[];
|
|
/** Writes Studio dropped as superseded before they reached the file. */
|
|
skipped: string[];
|
|
/** What Studio told the person failed: error toasts and lost edits. */
|
|
reported: string[];
|
|
/** The fate the next writes take, in the order they reach the file; `land` once it runs out. */
|
|
fates: WriteFate[];
|
|
/** Lets every write still waiting go on, landing. */
|
|
releaseWaiting(): void;
|
|
waiting(): number;
|
|
/** Runs `load` as Studio's preview reload does: after any edit gesture live in the preview. */
|
|
reloadWhenFree(load: () => void): void;
|
|
pending(): boolean;
|
|
}
|
|
|
|
export function mountTextEditing(
|
|
iframe: HTMLIFrameElement,
|
|
sealed: boolean,
|
|
fileHtml: string,
|
|
id: string,
|
|
/** Called with the element's markup each time a write lands, so a full reload loads the new file. */
|
|
onLanded: (markup: string) => void,
|
|
): TextEditing {
|
|
let file = fileHtml;
|
|
const writes: string[] = [];
|
|
const skipped: string[] = [];
|
|
const reported: string[] = [];
|
|
const fates: WriteFate[] = [];
|
|
const waiting: Array<() => void> = [];
|
|
const valueOf = (operations: Parameters<PersistDomEditOperations>[1]) =>
|
|
String(operations[0]?.value);
|
|
const takeFate = async () => {
|
|
const fate = fates.shift() ?? "land";
|
|
if (fate === "wait") await new Promise<void>((resolve) => waiting.push(resolve));
|
|
if (fate === "fail") throw new Error("write refused");
|
|
};
|
|
const persist: PersistDomEditOperations = async (selection, operations, options) => {
|
|
if (options?.shouldSave?.() === false) return void skipped.push(valueOf(operations));
|
|
await takeFate();
|
|
file = patchElementInHtml(file, { id: selection.id }, operations).html;
|
|
writes.push(valueOf(operations));
|
|
onLanded(saved());
|
|
};
|
|
const select = async () => {
|
|
if (!sealed) {
|
|
const element = iframe.contentDocument!.getElementById(id)!;
|
|
return (await resolveFoundSelection(iframe, element, OPTIONS))!;
|
|
}
|
|
const found = await findPreviewElementForSelection(iframe, { id, sourceFile: PATH }, PATH);
|
|
return (await resolvePreviewSelection(iframe, found!, OPTIONS))!;
|
|
};
|
|
let setSelection!: (selection: DomEditSelection | null) => void;
|
|
let controls!: ReturnType<typeof useInlineTextEdit>;
|
|
let commits!: ReturnType<typeof useDomEditTextCommits>;
|
|
const root = createRoot(document.createElement("div"));
|
|
flushSync(() =>
|
|
root.render(
|
|
createElement(() => {
|
|
const [selection, set] = useState<DomEditSelection | null>(null);
|
|
setSelection = set;
|
|
const raw = useDomEditTextCommits({
|
|
activeCompPath: PATH,
|
|
previewIframeRef: { current: iframe },
|
|
showToast: (message, kind) => void (kind === "error" && reported.push(String(message))),
|
|
reloadPreview: () => (window as unknown as { __reload: () => void }).__reload(),
|
|
domEditSelection: selection,
|
|
applyDomSelection: () => {},
|
|
refreshDomEditSelectionFromPreview: () => {},
|
|
buildDomSelectionFromTarget: (target, options) =>
|
|
resolveFoundSelection(iframe, target, { ...OPTIONS, ...options }),
|
|
persistDomEditOperations: persist,
|
|
resolveImportedFontAsset: () => null,
|
|
readOnlyPreview: false,
|
|
});
|
|
// As DomEditContext's trackEditCommits: each save is a pending edit from the call, after older saves.
|
|
const tracked = <F extends (...args: never[]) => unknown>(save: F) =>
|
|
trackedStudioEdit(save, { afterOlderSaves: true }) as F;
|
|
commits = {
|
|
...raw,
|
|
handleDomTextCommitForSelection: tracked(raw.handleDomTextCommitForSelection),
|
|
handleDomRichTextCommit: tracked(raw.handleDomRichTextCommit),
|
|
handleDomAddTextField: tracked(raw.handleDomAddTextField),
|
|
handleDomRemoveTextField: tracked(raw.handleDomRemoveTextField),
|
|
};
|
|
controls = useInlineTextEdit({
|
|
onCommit: commits.handleDomRichTextCommit,
|
|
onLost: (reason) => void reported.push(reason),
|
|
});
|
|
return null;
|
|
}),
|
|
),
|
|
);
|
|
function saved() {
|
|
return new DOMParser().parseFromString(file, "text/html").getElementById(id)!.innerHTML;
|
|
}
|
|
// As Studio's shadow reload (useShadowPreviewReload.ts): after a live gesture, then after pending saves.
|
|
function reloadWhenFree(load: () => void): void {
|
|
const doc = gestureDocumentOf(iframe);
|
|
const retry = () => reloadWhenFree(load);
|
|
if (doc && isStudioManualEditGestureLiveIn(doc)) afterStudioManualEditGestures(doc, retry);
|
|
else if (isStudioEditSaving()) afterStudioPendingEdits(retry);
|
|
else load();
|
|
}
|
|
return {
|
|
select: async () => {
|
|
const selection = await select();
|
|
flushSync(() => setSelection(selection));
|
|
return selection;
|
|
},
|
|
controls: () => controls,
|
|
commits: () => commits,
|
|
saved,
|
|
writes,
|
|
skipped,
|
|
reported,
|
|
fates,
|
|
releaseWaiting: () => waiting.splice(0).forEach((go) => go()),
|
|
waiting: () => waiting.length,
|
|
reloadWhenFree,
|
|
pending: () => hasStudioPendingEdits() || waiting.length > 0,
|
|
};
|
|
}
|