mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-08 21:15:18 +09:00
24 lines
769 B
TypeScript
24 lines
769 B
TypeScript
import produce from "immer";
|
|
import { type Instance } from "@webstudio-is/sdk";
|
|
import { findInstanceById } from "./find-instance";
|
|
import { deleteInstanceMutable } from "./delete-instance";
|
|
import {
|
|
insertInstanceMutable,
|
|
type InstanceInsertionSpec,
|
|
} from "./insert-instance";
|
|
|
|
export type InstanceReparentingSpec = {
|
|
id: Instance["id"];
|
|
parentId: Instance["id"];
|
|
position: InstanceInsertionSpec["position"];
|
|
};
|
|
|
|
export const reparentInstance = produce(
|
|
(draftRootInstance: Instance, spec: InstanceReparentingSpec) => {
|
|
const instance = findInstanceById(draftRootInstance, spec.id);
|
|
if (instance === undefined) return;
|
|
deleteInstanceMutable(draftRootInstance, instance.id);
|
|
insertInstanceMutable(draftRootInstance, instance, spec);
|
|
}
|
|
);
|