mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(studio): Add keyframe on a sealed preview keeps a moved layer in place (#8117)
* test(studio): Add keyframe on a sealed preview seeds from a moved layer's translate * fix(studio): Add keyframe on a sealed preview keeps a moved layer in place * fix(studio): a sealed keyframe seed reads the translate the frame renders, a stylesheet's too
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { readTranslatePx, readTranslatePxLeavingPercent } from "./plainTranslate";
|
||||
import {
|
||||
readTranslatePx,
|
||||
readTranslatePxLeavingPercent,
|
||||
rememberFrameTranslate,
|
||||
} from "./plainTranslate";
|
||||
|
||||
function box(style: string): HTMLElement {
|
||||
const element = document.createElement("div");
|
||||
@@ -94,4 +98,21 @@ describe("readTranslatePxLeavingPercent", () => {
|
||||
vi.restoreAllMocks();
|
||||
el.remove();
|
||||
});
|
||||
|
||||
// fails without the fix: a sealed preview's stand-in has no window, so Add keyframe seeded 0,0 and the layer jumped.
|
||||
it("seeds from the inline translate of a layer with no window, like a sealed preview's stand-in", () => {
|
||||
const standIn = document.implementation.createHTMLDocument("").createElement("div");
|
||||
standIn.style.setProperty("translate", "319.893px -75.508px");
|
||||
|
||||
expect(readTranslatePxLeavingPercent(standIn)).toEqual({ x: 319.893, y: -75.508 });
|
||||
});
|
||||
|
||||
it("seeds a stand-in from the translate its frame renders, a stylesheet's included, until a move rewrites it", () => {
|
||||
const standIn = document.implementation.createHTMLDocument("").createElement("div");
|
||||
rememberFrameTranslate(standIn, { x: 40, y: 30 }, "40px 30px");
|
||||
|
||||
expect(readTranslatePxLeavingPercent(standIn)).toEqual({ x: 40, y: 30 });
|
||||
standIn.style.setProperty("translate", "70px 10px");
|
||||
expect(readTranslatePxLeavingPercent(standIn)).toEqual({ x: 70, y: 10 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -102,10 +102,10 @@ export function translatePx(value: string, read?: ComputedRead): Point {
|
||||
};
|
||||
}
|
||||
|
||||
const frameTranslates = new WeakMap<Element, { inline: string; px: Point }>();
|
||||
const frameTranslates = new WeakMap<Element, { inline: string; px: Point; computed: string }>();
|
||||
|
||||
export function rememberFrameTranslate(el: HTMLElement, px: Point): void {
|
||||
frameTranslates.set(el, { inline: el.style.getPropertyValue("translate"), px });
|
||||
export function rememberFrameTranslate(el: HTMLElement, px: Point, computed: string): void {
|
||||
frameTranslates.set(el, { inline: el.style.getPropertyValue("translate"), px, computed });
|
||||
}
|
||||
|
||||
/** The element's CSS `translate` in px, as it renders now. */
|
||||
@@ -120,7 +120,11 @@ export function readTranslatePx(el: HTMLElement): Point {
|
||||
}
|
||||
|
||||
export function readTranslatePxLeavingPercent(el: HTMLElement): Point {
|
||||
const value = el.ownerDocument.defaultView?.getComputedStyle(el).translate || "none";
|
||||
const cs = el.ownerDocument.defaultView?.getComputedStyle(el);
|
||||
const inline = el.style.getPropertyValue("translate");
|
||||
const frame = cs ? undefined : frameTranslates.get(el);
|
||||
const computed = frame?.inline === inline ? frame.computed : cs?.translate;
|
||||
const value = computed || inline || "none";
|
||||
const [x = "0px", y = "0px"] = value === "none" ? [] : splitTopLevelWhitespace(value);
|
||||
const px = (part: string) => roundTo3(evaluateLength(part, 0)) || 0;
|
||||
return { x: px(x), y: px(y) };
|
||||
|
||||
@@ -357,6 +357,7 @@ export async function resolvePreviewSelection(
|
||||
rememberFrameTranslate(
|
||||
element,
|
||||
translatePx(styles.translate ?? "none", (p) => styles[p] ?? ""),
|
||||
styles.translate ?? "none",
|
||||
);
|
||||
const computedStyles = Object.fromEntries(
|
||||
CURATED_STYLE_PROPERTIES.flatMap((p) => (p in styles ? [[p, styles[p]!]] : [])),
|
||||
|
||||
Reference in New Issue
Block a user