mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 15:24:19 +00:00
fix(studio): remove trap options and wrong defaults from the inspector
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import React, { act } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { DomEditSelection } from "./domEditingTypes";
|
||||
import { PropertyPanel3dTransform } from "./propertyPanel3dTransform";
|
||||
|
||||
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
function createSelection(): DomEditSelection {
|
||||
const composition = document.createElement("div");
|
||||
composition.dataset.compositionId = "main";
|
||||
Object.defineProperty(composition, "offsetHeight", { value: 720 });
|
||||
const element = document.createElement("div");
|
||||
composition.append(element);
|
||||
document.body.append(composition);
|
||||
|
||||
return {
|
||||
element,
|
||||
id: "card",
|
||||
selector: "#card",
|
||||
sourceFile: "index.html",
|
||||
compositionPath: "index.html",
|
||||
label: "Card",
|
||||
tagName: "div",
|
||||
isCompositionHost: false,
|
||||
isInsideLockedComposition: false,
|
||||
boundingBox: { x: 0, y: 0, width: 100, height: 100 },
|
||||
textContent: null,
|
||||
dataAttributes: {},
|
||||
inlineStyles: {},
|
||||
computedStyles: {},
|
||||
textFields: [],
|
||||
capabilities: {
|
||||
canSelect: true,
|
||||
canEditStyles: true,
|
||||
canCrop: true,
|
||||
canMove: true,
|
||||
canResize: true,
|
||||
canApplyManualOffset: true,
|
||||
canApplyManualSize: true,
|
||||
canApplyManualRotation: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("PropertyPanel3dTransform depth control", () => {
|
||||
it("previews and commits only z when scrolling for depth", () => {
|
||||
vi.useFakeTimers();
|
||||
const previews: Array<Record<string, number>> = [];
|
||||
const commits: Array<Record<string, number | string>> = [];
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
const root = createRoot(host);
|
||||
|
||||
act(() => {
|
||||
root.render(
|
||||
<PropertyPanel3dTransform
|
||||
gsapRuntimeValues={{
|
||||
rotationX: 0,
|
||||
rotationY: 0,
|
||||
rotationZ: 0,
|
||||
transformPerspective: 0,
|
||||
z: 0,
|
||||
}}
|
||||
gsapAnimId={null}
|
||||
gsapKeyframes={null}
|
||||
currentPct={0}
|
||||
elStart={0}
|
||||
elDuration={1}
|
||||
element={createSelection()}
|
||||
onCommitAnimatedProperties={async (_element, props) => {
|
||||
commits.push(props);
|
||||
}}
|
||||
onLivePreviewProps={(_element, props) => {
|
||||
previews.push(props);
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
|
||||
const cube = host.querySelector<SVGSVGElement>('svg[role="slider"]');
|
||||
if (!cube) throw new Error("Missing 3D transform cube");
|
||||
|
||||
act(() => {
|
||||
cube.dispatchEvent(new WheelEvent("wheel", { deltaY: -400, cancelable: true }));
|
||||
});
|
||||
expect(previews).toEqual([{ z: 100 }]);
|
||||
|
||||
act(() => vi.advanceTimersByTime(160));
|
||||
expect(commits).toEqual([{ z: 100 }]);
|
||||
|
||||
act(() => root.unmount());
|
||||
});
|
||||
});
|
||||
@@ -76,15 +76,8 @@ function Cube3dControl({
|
||||
rotationY: gsapRuntimeValues.rotationY ?? 0,
|
||||
rotationZ: gsapRuntimeValues.rotationZ ?? 0,
|
||||
};
|
||||
// Comp-derived lens (see naturalDepthPerspective) applied the first time depth is
|
||||
// set, so the scene's foreshortening scales with the canvas instead of a magic 800.
|
||||
// Comp-derived lens for the cube widget's depth preview when the element has no perspective.
|
||||
const depthPerspective = naturalDepthPerspective(element.element);
|
||||
// A gentle, fixed "depth pose" tilt (degrees) dropped on a flat element the first
|
||||
// time it gets depth, so translateZ reads as 3D foreshortening instead of a plain
|
||||
// resize — small enough to look like a premium card, not a flip.
|
||||
const DEPTH_POSE_X = 10;
|
||||
const DEPTH_POSE_Y = -15;
|
||||
const isFlat = Math.round(pose.rotationX) === 0 && Math.round(pose.rotationY) === 0;
|
||||
// Commit only the rotation axes the drag actually changed (each rounded to a
|
||||
// whole degree). Reuses the keyframe-aware animated-property commit, so a drag
|
||||
// at the playhead writes/updates a keyframe just like the numeric fields.
|
||||
@@ -133,42 +126,10 @@ function Cube3dControl({
|
||||
onPoseDraft={livePreview}
|
||||
onPoseCommit={commitPose}
|
||||
onDepthDraft={(z) => {
|
||||
// Preview WITH a lens so depth is visible while scrolling — the same
|
||||
// default the commit applies, so the element doesn't snap on release.
|
||||
const preview: Record<string, number> = gsapRuntimeValues.transformPerspective
|
||||
? { z }
|
||||
: { z, transformPerspective: depthPerspective };
|
||||
// Depth-pose preview: a flat element only scales under Z, so mirror the
|
||||
// commit and preview the gentle tilt that makes the depth read as 3D.
|
||||
if (isFlat) {
|
||||
preview.rotationX = DEPTH_POSE_X;
|
||||
preview.rotationY = DEPTH_POSE_Y;
|
||||
}
|
||||
onLivePreviewProps?.(element, preview);
|
||||
onLivePreviewProps?.(element, { z });
|
||||
}}
|
||||
onDepthCommit={(z) => {
|
||||
// Best-UX depth: scroll moves Z, and a 3D transform always has a lens —
|
||||
// like an After Effects camera. translateZ is invisible without a
|
||||
// perspective, so the FIRST time depth is added (Perspective still 0) we
|
||||
// set a sensible comp-derived lens ONCE. Every later scroll touches Z
|
||||
// only, and Perspective stays an independent, editable field. The cube's
|
||||
// scroll is clamped in front of the lens, so Z can't run away past it.
|
||||
const props: Record<string, number> = { z };
|
||||
if (!gsapRuntimeValues.transformPerspective && depthPerspective > 0) {
|
||||
props.transformPerspective = depthPerspective;
|
||||
}
|
||||
// Depth-pose: a flat element (no tilt) only scales under Z — it can't read
|
||||
// as depth. So the first time depth lands on a flat element, also drop a
|
||||
// gentle fixed tilt; the foreshortening makes depth read as 3D IN PLACE
|
||||
// (no screen travel, per-element lens unchanged). Once the element has any
|
||||
// tilt, depth scrolls touch Z only. Reset tilt to 0 to go flat again.
|
||||
if (isFlat) {
|
||||
props.rotationX = DEPTH_POSE_X;
|
||||
props.rotationY = DEPTH_POSE_Y;
|
||||
}
|
||||
// One commit for all props so the writes can't race read-modify-write on
|
||||
// the same script (which dropped a prop and reverted after a seek).
|
||||
void onCommitAnimatedProperties(element, props);
|
||||
void onCommitAnimatedProperties(element, { z });
|
||||
}}
|
||||
onRecenter={recenter}
|
||||
onKeyframe={onKeyframe}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import React, { act } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import type { DomEditSelection } from "./domEditingTypes";
|
||||
import { SelectField } from "./propertyPanelPrimitives";
|
||||
import { StyleSections } from "./propertyPanelStyleSections";
|
||||
|
||||
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
|
||||
const roots: Root[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const root of roots) {
|
||||
act(() => root.unmount());
|
||||
}
|
||||
roots.length = 0;
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
|
||||
function render(node: React.ReactNode): HTMLElement {
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
const root = createRoot(host);
|
||||
roots.push(root);
|
||||
act(() => root.render(node));
|
||||
return host;
|
||||
}
|
||||
|
||||
function findSelect(host: HTMLElement, label: string): HTMLSelectElement {
|
||||
const field = Array.from(host.querySelectorAll("label")).find(
|
||||
(candidate) => candidate.querySelector("span")?.textContent === label,
|
||||
);
|
||||
const select = field?.querySelector<HTMLSelectElement>("select");
|
||||
if (!select) throw new Error(`Missing select field: ${label}`);
|
||||
return select;
|
||||
}
|
||||
|
||||
function expandSection(host: HTMLElement, title: string): void {
|
||||
const section = host.querySelector<HTMLElement>(`[data-panel-section="${title}"]`);
|
||||
const button = section?.querySelector<HTMLButtonElement>("button");
|
||||
if (!button) throw new Error(`Missing panel section: ${title}`);
|
||||
act(() => button.click());
|
||||
}
|
||||
|
||||
function createSelection(): DomEditSelection {
|
||||
return {
|
||||
element: document.createElement("div"),
|
||||
id: "card",
|
||||
selector: "#card",
|
||||
sourceFile: "index.html",
|
||||
compositionPath: "index.html",
|
||||
label: "Card",
|
||||
tagName: "div",
|
||||
isCompositionHost: false,
|
||||
isInsideLockedComposition: false,
|
||||
boundingBox: { x: 0, y: 0, width: 100, height: 100 },
|
||||
textContent: null,
|
||||
dataAttributes: {},
|
||||
inlineStyles: {},
|
||||
computedStyles: {},
|
||||
textFields: [],
|
||||
capabilities: {
|
||||
canSelect: true,
|
||||
canEditStyles: true,
|
||||
canCrop: true,
|
||||
canMove: true,
|
||||
canResize: true,
|
||||
canApplyManualOffset: true,
|
||||
canApplyManualSize: true,
|
||||
canApplyManualRotation: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("SelectField unlisted values", () => {
|
||||
it("disables an unlisted display-only value only when requested", () => {
|
||||
const host = render(
|
||||
<div>
|
||||
<SelectField
|
||||
label="Shadow"
|
||||
value="custom"
|
||||
disableUnlistedValue
|
||||
options={["none", "soft"]}
|
||||
onChange={() => undefined}
|
||||
/>
|
||||
<SelectField
|
||||
label="Style"
|
||||
value="groove"
|
||||
options={["none", "solid"]}
|
||||
onChange={() => undefined}
|
||||
/>
|
||||
</div>,
|
||||
);
|
||||
|
||||
const shadow = findSelect(host, "Shadow");
|
||||
expect(shadow.value).toBe("custom");
|
||||
expect(shadow.options[0]?.value).toBe("custom");
|
||||
expect(shadow.options[0]?.disabled).toBe(true);
|
||||
expect(
|
||||
Array.from(shadow.options)
|
||||
.slice(1)
|
||||
.every((option) => !option.disabled),
|
||||
).toBe(true);
|
||||
|
||||
const style = findSelect(host, "Style");
|
||||
expect(style.value).toBe("groove");
|
||||
expect(style.options[0]?.value).toBe("groove");
|
||||
expect(style.options[0]?.disabled).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("StyleSections curated options", () => {
|
||||
it("keeps authored off-list values visible and commits remaining options", () => {
|
||||
const calls: Array<[property: string, value: string]> = [];
|
||||
const host = render(
|
||||
<StyleSections
|
||||
projectId="project"
|
||||
element={createSelection()}
|
||||
styles={{
|
||||
"border-style": "groove",
|
||||
"border-width": "2px",
|
||||
"box-shadow": "0 2px 4px red",
|
||||
"clip-path": "polygon(0 0, 100% 0, 100% 100%)",
|
||||
overflow: "auto",
|
||||
}}
|
||||
assets={[]}
|
||||
onSetStyle={(property, value) => {
|
||||
calls.push([property, value]);
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
|
||||
expandSection(host, "stroke");
|
||||
expandSection(host, "effects");
|
||||
expandSection(host, "clip");
|
||||
|
||||
const strokeStyle = findSelect(host, "Style");
|
||||
expect(strokeStyle.value).toBe("groove");
|
||||
expect(Array.from(strokeStyle.options, (option) => option.value)).toEqual([
|
||||
"groove",
|
||||
"none",
|
||||
"solid",
|
||||
"dashed",
|
||||
"dotted",
|
||||
"double",
|
||||
]);
|
||||
|
||||
const overflow = findSelect(host, "Overflow");
|
||||
expect(overflow.value).toBe("auto");
|
||||
expect(Array.from(overflow.options, (option) => option.value)).toEqual([
|
||||
"auto",
|
||||
"visible",
|
||||
"hidden",
|
||||
"clip",
|
||||
]);
|
||||
expect(findSelect(host, "Shadow").options[0]?.disabled).toBe(true);
|
||||
expect(findSelect(host, "Mask").options[0]?.disabled).toBe(true);
|
||||
expect(calls).toEqual([]);
|
||||
|
||||
act(() => {
|
||||
strokeStyle.value = "dashed";
|
||||
strokeStyle.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
overflow.value = "hidden";
|
||||
overflow.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
});
|
||||
|
||||
expect(calls).toContainEqual(["border-style", "dashed"]);
|
||||
expect(calls).toContainEqual(["overflow", "hidden"]);
|
||||
});
|
||||
});
|
||||
@@ -305,16 +305,19 @@ export function SelectField({
|
||||
label,
|
||||
value,
|
||||
disabled,
|
||||
disableUnlistedValue,
|
||||
options,
|
||||
onChange,
|
||||
}: {
|
||||
label: string;
|
||||
value: string;
|
||||
disabled?: boolean;
|
||||
disableUnlistedValue?: boolean;
|
||||
options: string[];
|
||||
onChange: (nextValue: string) => void;
|
||||
}) {
|
||||
const renderedOptions = value && !options.includes(value) ? [value, ...options] : options;
|
||||
const hasUnlistedValue = Boolean(value && !options.includes(value));
|
||||
const renderedOptions = hasUnlistedValue ? [value, ...options] : options;
|
||||
return (
|
||||
<label className={`${FIELD} flex items-center gap-3`}>
|
||||
<span className="flex-shrink-0 text-[11px] font-medium text-neutral-500">{label}</span>
|
||||
@@ -325,7 +328,11 @@ export function SelectField({
|
||||
className="min-w-0 w-full appearance-none bg-transparent text-[11px] font-medium text-neutral-100 outline-none disabled:cursor-not-allowed disabled:text-neutral-600"
|
||||
>
|
||||
{renderedOptions.map((option) => (
|
||||
<option key={option} value={option}>
|
||||
<option
|
||||
key={option}
|
||||
value={option}
|
||||
disabled={disableUnlistedValue && hasUnlistedValue && option === value}
|
||||
>
|
||||
{option}
|
||||
</option>
|
||||
))}
|
||||
|
||||
@@ -387,7 +387,7 @@ export function TextSection({
|
||||
|
||||
if (textFields.length === 1) {
|
||||
return (
|
||||
<Section title="Text" defaultCollapsed>
|
||||
<Section title="Text">
|
||||
<TextFieldEditor
|
||||
field={activeField}
|
||||
styles={styles}
|
||||
|
||||
@@ -250,18 +250,7 @@ export function StyleSections({
|
||||
await onSetStyle(property, value);
|
||||
}
|
||||
}}
|
||||
options={[
|
||||
"none",
|
||||
"solid",
|
||||
"dashed",
|
||||
"dotted",
|
||||
"double",
|
||||
"hidden",
|
||||
"groove",
|
||||
"ridge",
|
||||
"inset",
|
||||
"outset",
|
||||
]}
|
||||
options={["none", "solid", "dashed", "dotted", "double"]}
|
||||
/>
|
||||
</div>
|
||||
<ColorField
|
||||
@@ -279,14 +268,14 @@ export function StyleSections({
|
||||
label="Shadow"
|
||||
value={boxShadowPreset}
|
||||
disabled={styleEditingDisabled}
|
||||
disableUnlistedValue
|
||||
onChange={(next) => {
|
||||
if (next === "custom") return;
|
||||
onSetStyle(
|
||||
"box-shadow",
|
||||
buildBoxShadowPresetValue(next as BoxShadowPreset, styles["box-shadow"]),
|
||||
);
|
||||
}}
|
||||
options={["custom", "none", "soft", "lift", "glow"]}
|
||||
options={["none", "soft", "lift", "glow"]}
|
||||
/>
|
||||
<div className={RESPONSIVE_GRID}>
|
||||
<div className="grid min-w-0 gap-1.5">
|
||||
@@ -334,14 +323,14 @@ export function StyleSections({
|
||||
value={styles.overflow || "visible"}
|
||||
disabled={styleEditingDisabled}
|
||||
onChange={(next) => onSetStyle("overflow", next)}
|
||||
options={["visible", "hidden", "clip", "auto", "scroll"]}
|
||||
options={["visible", "hidden", "clip"]}
|
||||
/>
|
||||
<SelectField
|
||||
label="Mask"
|
||||
value={clipPathPreset}
|
||||
disabled={styleEditingDisabled}
|
||||
disableUnlistedValue
|
||||
onChange={(next) => {
|
||||
if (next === "custom") return;
|
||||
onSetStyle(
|
||||
"clip-path",
|
||||
buildClipPathValue(
|
||||
@@ -351,7 +340,7 @@ export function StyleSections({
|
||||
),
|
||||
);
|
||||
}}
|
||||
options={["custom", "none", "inset", "circle"]}
|
||||
options={["none", "inset", "circle"]}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid min-w-0 gap-1.5">
|
||||
|
||||
Reference in New Issue
Block a user