feat(studio): draggable layer reorder with z-index persistence (#1216)

* feat(studio): add drag-to-reorder in layers panel with z-index persistence

Layers panel now sorts siblings by computed z-index (descending) to
reflect visual stacking order. Users can drag layer rows to reorder
them within a sibling group — on drop, sequential z-index values are
assigned and persisted via the existing inline-style patch pipeline
with a single preview reload.

- sortLayersByZIndex: recursive sibling-group sort by computed z-index
- useLayerDrag: pointer-capture drag gesture with 4px threshold,
  insertion indicator line, and depth-constrained sibling reorder
- handleDomZIndexReorderCommit: batch z-index commit with coalesced
  undo entry and single skipRefresh=false on the final patch

* fix(studio): harden layer drag-to-reorder edge cases

- Guard drag initiation against locked compositions by checking
  data-timeline-locked ancestors in isLayerDraggable
- Show not-allowed cursor and reduced opacity on non-draggable layer rows
- Fire toast when attempting to drag a layer with no same-depth siblings
- Preserve z-index spacing on reorder by redistributing existing values
  instead of flattening to sequential integers
- Auto-set position:relative on unpositioned elements when z-index is
  applied so the stacking order actually takes visual effect
- Add tests for isLayerDraggable (anonymous, id, selector, locked, free)

* fix(studio): handle z-index ties in layer reorder + trim file sizes

- Fall back to sequential z-index when any duplicates exist in the
  sibling set, not just when all values are identical — fixes silent
  no-op reorder when tied values preserve DOM-order stacking
- Trim useDomEditSession.ts from 602 to 600 lines (CI file-size gate)

* test(studio): add duplicate z-index tiebreak test for layer sorting

Cover the [2, 1, 2] case where tied z-index values fall back to
reverse DOM order — locks the hasDupes fix against regressions.

* style(studio): fix oxfmt formatting in LayersPanel
This commit is contained in:
Miguel Ángel
2026-06-05 20:08:22 -04:00
committed by GitHub
parent 1f37920fe1
commit 7a0cb085bb
6 changed files with 553 additions and 17 deletions
@@ -529,6 +529,54 @@ export function useDomEditCommits({
],
);
const handleDomZIndexReorderCommit = useCallback(
(
entries: Array<{
element: HTMLElement;
zIndex: number;
id?: string;
selector?: string;
selectorIndex?: number;
sourceFile: string;
}>,
) => {
if (entries.length === 0) return;
const coalesceKey = `z-reorder:${entries.map((e) => e.id ?? e.selector ?? "el").join(":")}`;
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
entry.element.style.zIndex = String(entry.zIndex);
const patches: Array<{ type: "inline-style"; property: string; value: string }> = [
{ type: "inline-style", property: "z-index", value: String(entry.zIndex) },
];
try {
const win = entry.element.ownerDocument?.defaultView;
if (win && win.getComputedStyle(entry.element).position === "static") {
entry.element.style.position = "relative";
patches.push({ type: "inline-style", property: "position", value: "relative" });
}
} catch {
/* cross-origin or detached — skip */
}
commitPositionPatchToHtml(
{
element: entry.element,
id: entry.id ?? null,
selector: entry.selector,
selectorIndex: entry.selectorIndex,
sourceFile: entry.sourceFile,
} as unknown as DomEditSelection,
patches,
{
label: "Reorder layers",
coalesceKey,
skipRefresh: i < entries.length - 1,
},
);
}
},
[commitPositionPatchToHtml],
);
return {
resolveImportedFontAsset,
handleDomStyleCommit,
@@ -547,5 +595,6 @@ export function useDomEditCommits({
handleDomMotionCommit,
handleDomMotionClear,
handleDomEditElementDelete,
handleDomZIndexReorderCommit,
};
}