From edf26892082c818635df65c4f5791a82a9df6d1c Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 29 Nov 2026 06:08:46 -0500 Subject: [PATCH 1/4] perf(core): a sealed drag restyles only the selection outline pieces that moved On each drag step the sealed frame cleared and rewrote the whole inline style of every node of Studio's selection outline copy, about 450 style writes a frame. The look is now written once when the copy mounts, and each step writes only the place values that changed. --- .../src/runtime/editing/dragChrome.test.ts | 29 +++++++++++++++++++ .../core/src/runtime/editing/dragChrome.ts | 29 ++++++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/packages/core/src/runtime/editing/dragChrome.test.ts b/packages/core/src/runtime/editing/dragChrome.test.ts index 8e32a022c8b..d76025b4c35 100644 --- a/packages/core/src/runtime/editing/dragChrome.test.ts +++ b/packages/core/src/runtime/editing/dragChrome.test.ts @@ -81,6 +81,35 @@ 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, and puts back the look a place no longer overrides", () => { + const root = watchRoot(); + drawDrafts([], { + ...chromeAt(140, [box]), + place: [{ ...place(140)[0], "border-radius": "2px" }, {}, {}], + }); + const drawn = root().firstElementChild!.firstElementChild as HTMLElement; + const seen: MutationRecord[] = []; + const observer = new MutationObserver((records) => seen.push(...records)); + observer.observe(root(), { subtree: true, attributes: true }); + drawDrafts([], chromeAt(148)); + seen.push(...observer.takeRecords()); + expect(new Set(seen.map((record) => record.target))).toEqual(new Set([drawn])); + expect([drawn.style.left, drawn.style.borderRadius, drawn.style.border]).toEqual([ + "148px", + "6px", + "1px solid rgb(60, 230, 172)", + ]); + seen.length = 0; + drawDrafts([], chromeAt(148)); + seen.push(...observer.takeRecords()); + observer.disconnect(); + expect(seen).toEqual([]); + drawDrafts([], { ...chromeAt(148), place: [{ left: "148px" }, {}, {}] }); + expect([drawn.style.left, drawn.style.width]).toEqual(["148px", ""]); + }); + // 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", () => { diff --git a/packages/core/src/runtime/editing/dragChrome.ts b/packages/core/src/runtime/editing/dragChrome.ts index 9217f580add..2ebb46aa8fa 100644 --- a/packages/core/src/runtime/editing/dragChrome.ts +++ b/packages/core/src/runtime/editing/dragChrome.ts @@ -105,6 +105,7 @@ let host: HTMLElement | null = null; let plane: HTMLElement | null = null; let placed: Array = []; let looks: Array> = []; +let places: Array> = []; export const isDragChromeHost = (element: Element): boolean => element === host; @@ -114,8 +115,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); } @@ -139,6 +143,20 @@ function writeStyle(element: HTMLElement | SVGElement, props: Record): void { + const [element, look, last] = [placed[i]!, looks[i]!, places[i]!]; + for (const name of Object.keys(last)) { + if (name in next) continue; + if (name in look) element.style.setProperty(name, look[name]!); + else element.style.removeProperty(name); + } + for (const [name, value] of Object.entries(next)) + if (last[name] !== value) element.style.setProperty(name, value); + if ("pointer-events" in last || "pointer-events" in next) + element.style.setProperty("pointer-events", "none", "important"); + 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) { @@ -148,13 +166,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 (plane.style.transform !== transform) plane.style.transform = transform; + chrome.place.forEach((next, i) => writeMovedPlace(i, next)); return true; } @@ -163,6 +177,7 @@ export function clearDragChrome(): void { host = plane = null; placed = []; looks = []; + places = []; } /** Runs `read` with the chrome host out of the document, so nothing it reads carries it. */ From d39e3ea6746f71892b43bcf36c8666e63162583c Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sun, 29 Nov 2026 08:02:31 -0500 Subject: [PATCH 2/4] fix(core): a drag outline piece shows its own look again when a place shorthand goes A place value that overlaps others, such as inset, rebuilds the node as before. Only independent values (left, top, width and the like) are written alone, and the plane's transform is compared with what was last written. --- .../src/runtime/editing/dragChrome.test.ts | 50 ++++++++++--------- .../core/src/runtime/editing/dragChrome.ts | 38 ++++++++++---- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/packages/core/src/runtime/editing/dragChrome.test.ts b/packages/core/src/runtime/editing/dragChrome.test.ts index d76025b4c35..eb566c8e387 100644 --- a/packages/core/src/runtime/editing/dragChrome.test.ts +++ b/packages/core/src/runtime/editing/dragChrome.test.ts @@ -83,31 +83,33 @@ describe("drag chrome", () => { // 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, and puts back the look a place no longer overrides", () => { - const root = watchRoot(); - drawDrafts([], { - ...chromeAt(140, [box]), - place: [{ ...place(140)[0], "border-radius": "2px" }, {}, {}], - }); - const drawn = root().firstElementChild!.firstElementChild as HTMLElement; - const seen: MutationRecord[] = []; - const observer = new MutationObserver((records) => seen.push(...records)); - observer.observe(root(), { subtree: true, attributes: true }); + 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)); - seen.push(...observer.takeRecords()); - expect(new Set(seen.map((record) => record.target))).toEqual(new Set([drawn])); - expect([drawn.style.left, drawn.style.borderRadius, drawn.style.border]).toEqual([ - "148px", - "6px", - "1px solid rgb(60, 230, 172)", - ]); - seen.length = 0; - drawDrafts([], chromeAt(148)); - seen.push(...observer.takeRecords()); - observer.disconnect(); - expect(seen).toEqual([]); - drawDrafts([], { ...chromeAt(148), place: [{ left: "148px" }, {}, {}] }); - expect([drawn.style.left, drawn.style.width]).toEqual(["148px", ""]); + 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, 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 diff --git a/packages/core/src/runtime/editing/dragChrome.ts b/packages/core/src/runtime/editing/dragChrome.ts index 2ebb46aa8fa..35f83d6f1c7 100644 --- a/packages/core/src/runtime/editing/dragChrome.ts +++ b/packages/core/src/runtime/editing/dragChrome.ts @@ -106,6 +106,7 @@ let plane: HTMLElement | null = null; let placed: Array = []; let looks: Array> = []; let places: Array> = []; +let planeAt = ""; export const isDragChromeHost = (element: Element): boolean => element === host; @@ -143,17 +144,31 @@ function writeStyle(element: HTMLElement | SVGElement, props: Record): 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): void { - const [element, look, last] = [placed[i]!, looks[i]!, places[i]!]; - for (const name of Object.keys(last)) { - if (name in next) continue; - if (name in look) element.style.setProperty(name, look[name]!); - else element.style.removeProperty(name); - } - for (const [name, value] of Object.entries(next)) - if (last[name] !== value) element.style.setProperty(name, value); - if ("pointer-events" in last || "pointer-events" in next) - element.style.setProperty("pointer-events", "none", "important"); + 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; } @@ -167,7 +182,7 @@ export function drawDragChrome(chrome: EditingDragChrome): boolean { if (!plane || chrome.place.length !== placed.length) return false; const { origin, scale } = chrome; const transform = `scale(${1 / scale.x}, ${1 / scale.y}) translate(${-origin.x}px, ${-origin.y}px)`; - if (plane.style.transform !== transform) plane.style.transform = transform; + if (planeAt !== transform) plane.style.setProperty("transform", (planeAt = transform)); chrome.place.forEach((next, i) => writeMovedPlace(i, next)); return true; } @@ -178,6 +193,7 @@ export function clearDragChrome(): void { placed = []; looks = []; places = []; + planeAt = ""; } /** Runs `read` with the chrome host out of the document, so nothing it reads carries it. */ From 67cbab01a3036e31785950876ee5e6add2d3fe51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 30 Nov 2026 23:44:55 +0000 Subject: [PATCH 3/4] fix(core): a drag outline writes only what moved, as a fresh draw would leave it Studio's outline carries transform-origin and cursor in its place, so every move rewrote the wrapper and box whole. Those two are now written on their own. A step whose properties could overlap another declaration is drawn fresh each time, so its order is the step's, and a value the browser refuses falls back to the look's. --- .../src/runtime/editing/dragChrome.test.ts | 64 +++++++++++++++++++ .../core/src/runtime/editing/dragChrome.ts | 24 +++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/packages/core/src/runtime/editing/dragChrome.test.ts b/packages/core/src/runtime/editing/dragChrome.test.ts index eb566c8e387..3381f7c7f75 100644 --- a/packages/core/src/runtime/editing/dragChrome.test.ts +++ b/packages/core/src/runtime/editing/dragChrome.test.ts @@ -112,6 +112,70 @@ describe("drag chrome", () => { expect(write.mock.calls.at(-1)).toEqual(["pointer-events", "none", "important"]); }); + /** Studio's own place for an outline: a turned wrapper and the box inside it. */ + const studioPlace = (left: number, originX: number) => [ + { "transform-origin": `${originX}px 45px`, transform: "rotate(30deg)" }, + { left: `${left}px`, top: "20px", width: "40px", height: "30px", cursor: "move" }, + {}, + {}, + ]; + const nodeStyle = (i: number) => + (watchedRoot!().firstElementChild!.querySelectorAll("*")[i] as HTMLElement).style; + const styleMap = (style: CSSStyleDeclaration) => + Object.fromEntries( + [...Array(style.length).keys()].map((k) => [style[k], style.getPropertyValue(style[k]!)]), + ); + let watchedRoot: (() => ShadowRoot) | null = null; + const wrapped: EditingDragChromeNode = { tag: "div", look: {}, children: [box] }; + + // fails without the fix: Studio's wrapper and box carry transform-origin and cursor, so each move rewrote both whole. + it("writes only what moved in the place Studio sends, its transform-origin and cursor included", () => { + drawDrafts([], { ...chromeAt(0, [wrapped]), place: studioPlace(140, 60) }); + const write = vi.spyOn(CSSStyleDeclaration.prototype, "setProperty"); + drawDrafts([], { ...chromeAt(0), place: studioPlace(148, 64) }); + expect(write.mock.calls).toEqual([ + ["transform-origin", "64px 45px"], + ["left", "148px"], + ]); + }); + + // fails without the fix: overlapping logical and physical declarations swapped order with the same values, and the + // fast path kept the old order, so the other one won. + it("keeps the order a step gives overlapping declarations, as a fresh draw does", () => { + watchedRoot = watchRoot(); + const step = (place: Record, nodes?: EditingDragChromeNode[]) => + drawDrafts([], { ...chromeAt(0, nodes), place: [place, {}, {}] }); + step({ left: "3px", "inset-inline-start": "5px" }, [box]); + step({ "inset-inline-start": "5px", left: "3px" }); + const moved = nodeStyle(0).cssText; + step({ "inset-inline-start": "5px", left: "3px" }, [box]); + expect(moved).toBe(nodeStyle(0).cssText); + }); + + it("clears a place property a step swaps for another, as a fresh draw does", () => { + watchedRoot = watchRoot(); + const step = (place: Record, nodes?: EditingDragChromeNode[]) => + drawDrafts([], { ...chromeAt(0, nodes), place: [place, {}, {}] }); + step({ left: "3px", top: "4px" }, [box]); + step({ left: "3px", width: "9px" }); + expect([nodeStyle(0).top, nodeStyle(0).width]).toEqual(["", "9px"]); + }); + + // fails without the fix: a value the browser refused left the previous one in force, so a step that had hidden the + // outline kept it hidden. + it("matches a fresh draw when the browser refuses a value a step sends", () => { + watchedRoot = watchRoot(); + const node = { ...box, look: { ...box.look, opacity: "0.8" } }; + const step = (opacity: string, nodes?: EditingDragChromeNode[]) => + drawDrafts([], { ...chromeAt(140, nodes), place: [{ ...place(140)[0], opacity }, {}, {}] }); + step("0", [node]); + step("not-a-number"); + const moved = styleMap(nodeStyle(0)); + step("not-a-number", [node]); + expect(moved).toEqual(styleMap(nodeStyle(0))); + expect(moved.opacity).toBe("0.8"); + }); + // 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", () => { diff --git a/packages/core/src/runtime/editing/dragChrome.ts b/packages/core/src/runtime/editing/dragChrome.ts index 35f83d6f1c7..009047d8a62 100644 --- a/packages/core/src/runtime/editing/dragChrome.ts +++ b/packages/core/src/runtime/editing/dragChrome.ts @@ -144,14 +144,17 @@ function writeStyle(element: HTMLElement | SVGElement, props: Record): void { @@ -162,14 +165,23 @@ function writeNode(i: number, place: Record): void { element.style.setProperty("pointer-events", "none", "important"); } +/** One property as a fresh draw would leave it: the place's value, else the look's when the browser refuses it. */ +function writeAlone(i: number, name: string, value: string): void { + const { style } = placed[i]!; + style.removeProperty(name); + style.setProperty(name, value); + const look = looks[i]![name]; + if (!style.getPropertyValue(name) && look) style.setProperty(name, look); +} + function writeMovedPlace(i: number, next: Record): 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; + const names = Object.keys(next); + const sameNames = + names.length === Object.keys(last).length && names.every((name) => name in last); + if (!sameNames || !names.every((name) => WRITTEN_ALONE.has(name))) return writeNode(i, next); + for (const name of names) if (last[name] !== next[name]) writeAlone(i, name, next[name]!); } /** Mounts, places or clears the chrome in the drafts' turn; false when this place fits no mounted copy. */ From a4c5d81e0525ef618d981df6700e94ec347c04ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 1 Dec 2026 00:18:24 +0000 Subject: [PATCH 4/4] fix(core): a drag outline step that changes nothing writes nothing A resize handle's place is the same on most steps, yet its sides sent it through the full rewrite every time. A step that changes nothing now writes nothing, and a value the browser reads back empty, an explicit clear or a refused one, redraws the node fresh so it matches a fresh draw, shorthand-given values included. --- .../src/runtime/editing/dragChrome.test.ts | 40 +++++++++++++++++++ .../core/src/runtime/editing/dragChrome.ts | 14 ++++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/core/src/runtime/editing/dragChrome.test.ts b/packages/core/src/runtime/editing/dragChrome.test.ts index 3381f7c7f75..5305fb733ae 100644 --- a/packages/core/src/runtime/editing/dragChrome.test.ts +++ b/packages/core/src/runtime/editing/dragChrome.test.ts @@ -176,6 +176,46 @@ describe("drag chrome", () => { expect(moved.opacity).toBe("0.8"); }); + /** Studio's resize handle: a cursor that turns with the layer, and corner sides the box's size does not move. */ + const handlePlace = (cursor: string) => [ + { + cursor, + "touch-action": "none", + right: "calc(-6px - max(0px, 6px - (100% - 0px) / 4))", + bottom: "calc(-6px - max(0px, 6px - (100% - 0px) / 4))", + }, + {}, + {}, + ]; + + // fails without the fix: a resize handle's sides sent it through the full rewrite on every step, + // even one that changed nothing. + it("writes nothing for a step that changes nothing, and only the cursor when a resize handle turns", () => { + drawDrafts([], { ...chromeAt(0, [box]), place: handlePlace("nwse-resize") }); + const write = vi.spyOn(CSSStyleDeclaration.prototype, "setProperty"); + drawDrafts([], { ...chromeAt(0), place: handlePlace("nwse-resize") }); + expect(write).not.toHaveBeenCalled(); + drawDrafts([], { ...chromeAt(0), place: handlePlace("ns-resize") }); + expect(write.mock.calls).toEqual([["cursor", "ns-resize"]]); + drawDrafts([], { ...chromeAt(0), place: [{ inset: "4px" }, {}, {}] }); + write.mockClear(); + drawDrafts([], { ...chromeAt(0), place: [{ inset: "4px" }, {}, {}] }); + expect(write).not.toHaveBeenCalled(); + }); + + // fails without the fix: an empty value clears a declaration in a fresh draw, but the fast path put the look's back. + it("clears a property a step empties, as a fresh draw does", () => { + watchedRoot = watchRoot(); + const node = { ...box, look: { ...box.look, opacity: "0.8" } }; + const step = (opacity: string, nodes?: EditingDragChromeNode[]) => + drawDrafts([], { ...chromeAt(140, nodes), place: [{ ...place(140)[0], opacity }, {}, {}] }); + step("0.5", [node]); + step(""); + const moved = styleMap(nodeStyle(0)); + step("", [node]); + expect(moved).toEqual(styleMap(nodeStyle(0))); + }); + // 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", () => { diff --git a/packages/core/src/runtime/editing/dragChrome.ts b/packages/core/src/runtime/editing/dragChrome.ts index 009047d8a62..64eceac395e 100644 --- a/packages/core/src/runtime/editing/dragChrome.ts +++ b/packages/core/src/runtime/editing/dragChrome.ts @@ -148,6 +148,8 @@ function writeStyle(element: HTMLElement | SVGElement, props: Record): void { @@ -165,13 +168,12 @@ function writeNode(i: number, place: Record): void { element.style.setProperty("pointer-events", "none", "important"); } -/** One property as a fresh draw would leave it: the place's value, else the look's when the browser refuses it. */ -function writeAlone(i: number, name: string, value: string): void { +/** Writes one property on its own; false when the browser took no value, which only a fresh draw resolves. */ +function writeAlone(i: number, name: string, value: string): boolean { const { style } = placed[i]!; style.removeProperty(name); style.setProperty(name, value); - const look = looks[i]![name]; - if (!style.getPropertyValue(name) && look) style.setProperty(name, look); + return style.getPropertyValue(name) !== ""; } function writeMovedPlace(i: number, next: Record): void { @@ -180,8 +182,10 @@ function writeMovedPlace(i: number, next: Record): void { const names = Object.keys(next); const sameNames = names.length === Object.keys(last).length && names.every((name) => name in last); + const moved = names.filter((name) => last[name] !== next[name]); + if (sameNames && !moved.length && names.join() === Object.keys(last).join()) return; if (!sameNames || !names.every((name) => WRITTEN_ALONE.has(name))) return writeNode(i, next); - for (const name of names) if (last[name] !== next[name]) writeAlone(i, name, next[name]!); + if (!moved.every((name) => writeAlone(i, name, next[name]!))) writeNode(i, next); } /** Mounts, places or clears the chrome in the drafts' turn; false when this place fits no mounted copy. */