Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/core/src/runtime/editing/dragChrome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ describe("drag chrome", () => {
expect(drawn.style.getPropertyValue("pointer-events")).toBe("none");
});

// fails without the fix: each step cleared and rewrote every node's whole style, so a sealed drag restyled
// and repainted all of the chrome each frame and dropped frames the unsealed preview did not.
it("writes only the place that moved, counting every style write", () => {
drawDrafts([], chromeAt(140, [box]));
const write = vi.spyOn(CSSStyleDeclaration.prototype, "setProperty");
const third = { ...chromeAt(148), scale: { x: 3, y: 3 } };
drawDrafts([], chromeAt(148));
expect(write.mock.calls).toEqual([["left", "148px"]]);
drawDrafts([], third);
expect(write.mock.calls.slice(1).map(([name]) => name)).toEqual(["transform"]);
drawDrafts([], third);
expect(write).toHaveBeenCalledTimes(2);
});

// fails without the fix: dropping a place's `inset` cleared the look's own left and top, and a changed one
// covered a left the step kept.
it("rewrites a node's look and place when a place shorthand changes or goes", () => {
const node = { ...box, look: { ...box.look, left: "7px", top: "9px" } };
const step = (place: Record<string, string>, nodes?: EditingDragChromeNode[]) =>
drawDrafts([], { ...chromeAt(0, nodes), place: [place, {}, {}] });
step({ inset: "5px", left: "3px" }, [node]);
const write = vi.spyOn(CSSStyleDeclaration.prototype, "setProperty");
step({ inset: "6px", left: "3px" });
expect(write.mock.calls).toContainEqual(["left", "3px"]);
write.mockClear();
step({});
expect(write.mock.calls).toContainEqual(["left", "7px"]);
expect(write.mock.calls).toContainEqual(["top", "9px"]);
expect(write.mock.calls.at(-1)).toEqual(["pointer-events", "none", "important"]);
});

// fails without the fix: a place list the frame could not apply was dropped silently, so Studio kept its
// outline clipped while the frame showed none (a frame reloaded mid-drag).
it("tells Studio when it could not place the chrome, so Studio can send the copy again", () => {
Expand Down
45 changes: 38 additions & 7 deletions packages/core/src/runtime/editing/dragChrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ let host: HTMLElement | null = null;
let plane: HTMLElement | null = null;
let placed: Array<HTMLElement | SVGElement> = [];
let looks: Array<Record<string, string>> = [];
let places: Array<Record<string, string>> = [];
let planeAt = "";

export const isDragChromeHost = (element: Element): boolean => element === host;

Expand All @@ -114,8 +116,11 @@ function build(node: EditingDragChromeNode, into: Node): void {
? document.createElementNS(SVG_NS, node.tag)
: document.createElement(node.tag);
for (const [name, value] of Object.entries(node.attrs ?? {})) element.setAttribute(name, value);
writeStyle(element, node.look);
element.style.setProperty("pointer-events", "none", "important");
placed.push(element);
looks.push(node.look);
places.push({});
into.appendChild(element);
for (const child of node.children ?? []) build(child, element);
}
Expand All @@ -139,6 +144,34 @@ function writeStyle(element: HTMLElement | SVGElement, props: Record<string, str
for (const [name, value] of Object.entries(props)) element.style.setProperty(name, value);
}

const INDEPENDENT = new Set([
"left",
"top",
"width",
"height",
"transform",
"opacity",
"clip-path",
]);

function writeNode(i: number, place: Record<string, string>): void {
const element = placed[i]!;
element.removeAttribute("style");
writeStyle(element, looks[i]!);
writeStyle(element, place);
element.style.setProperty("pointer-events", "none", "important");
}

function writeMovedPlace(i: number, next: Record<string, string>): void {
const last = places[i]!;
const moved = Object.keys(next).filter((name) => last[name] !== next[name]);
const dropped = Object.keys(last).some((name) => !(name in next));
const independent = Object.keys({ ...last, ...next }).every((name) => INDEPENDENT.has(name));
if (dropped || (moved.length && !independent)) writeNode(i, next);
else for (const name of moved) placed[i]!.style.setProperty(name, next[name]!);
places[i] = next;
}

/** Mounts, places or clears the chrome in the drafts' turn; false when this place fits no mounted copy. */
export function drawDragChrome(chrome: EditingDragChrome): boolean {
if (chrome === null) {
Expand All @@ -148,13 +181,9 @@ export function drawDragChrome(chrome: EditingDragChrome): boolean {
if (chrome.nodes) mount(chrome.nodes);
if (!plane || chrome.place.length !== placed.length) return false;
const { origin, scale } = chrome;
plane.style.transform = `scale(${1 / scale.x}, ${1 / scale.y}) translate(${-origin.x}px, ${-origin.y}px)`;
placed.forEach((element, i) => {
element.removeAttribute("style");
writeStyle(element, looks[i]!);
writeStyle(element, chrome.place[i]!);
element.style.setProperty("pointer-events", "none", "important");
});
const transform = `scale(${1 / scale.x}, ${1 / scale.y}) translate(${-origin.x}px, ${-origin.y}px)`;
if (planeAt !== transform) plane.style.setProperty("transform", (planeAt = transform));
chrome.place.forEach((next, i) => writeMovedPlace(i, next));
return true;
}

Expand All @@ -163,6 +192,8 @@ export function clearDragChrome(): void {
host = plane = null;
placed = [];
looks = [];
places = [];
planeAt = "";
}

/** Runs `read` with the chrome host out of the document, so nothing it reads carries it. */
Expand Down
Loading