fix(layer-editor): use stable _key for drag-and-drop sorting

Add a unique `_key` to each layer config to maintain stable sortable IDs
across reorder operations. Update LayerRow to use the sortableId prop
and the `use:sortable` directive. Refactor layer-crud utilities to
generate and propagate `_key` values on creation and initialization.
This commit is contained in:
hyper
2026-07-03 21:49:18 +08:00
parent 186e3c0db6
commit 0add3f27d5
7 changed files with 86 additions and 30 deletions
@@ -9,6 +9,7 @@ import alignRightIcon from "./icons/align-right.png";
export interface LayerRowProps {
store: DeckStore;
index: number;
sortableId: string;
layer: LayerConfig;
hovered: boolean;
onHoverChange: (idx: number | null) => void;
@@ -64,7 +65,7 @@ function alignSrc(v: string) {
}
export function LayerRow(props: LayerRowProps) {
const sortable = createSortable(props.index);
const sortable = createSortable(props.sortableId);
const [dndState] = useDragDropContext()!;
const [fontOpen, setFontOpen] = createSignal(false);
let fontRef: HTMLDivElement | undefined;
@@ -89,11 +90,12 @@ export function LayerRow(props: LayerRowProps) {
return (
<div
ref={sortable}
use:sortable={sortable}
class={`flex items-center gap-1 py-1.5 px-1 relative group border-b border-gray-100 ${selected() ? "bg-blue-50" : ""}`}
classList={{
"opacity-25": sortable.isActiveDraggable,
"transition-transform": !!dndState.active.draggable,
"transition-transform":
!!dndState.active.draggable && !sortable.isActiveDraggable,
}}
onMouseEnter={() => props.onHoverChange(props.index)}
onMouseLeave={() => props.onHoverChange(null)}