Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: insert copy on reparent (#2787)
Bogdan Chadkineb5f9809ba55
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.tsindex 4ae2e320c8..6a64e53194 100644--- a/apps/builder/app/shared/instance-utils.test.ts+++ b/apps/builder/app/shared/instance-utils.test.ts@@ -53,6 +53,8 @@ import { createDefaultPages } from "@webstudio-is/project-build";enableMapSet();registerContainers();+$pages.set(createDefaultPages({ rootInstanceId: "" }));+const defaultMetasMap = new Map(Object.entries(defaultMetas));const createFakeComponentMetas = (@@ -515,32 +517,227 @@ test("insert template data with only new style sources", () => {});describe("reparent instance", () => {- test("from collectioo item", () => {+ test("between instances", () => {+ // body+ // box+ // text+ // button+ $instances.set(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "box" },+ { type: "id", value: "button" },+ ]),+ createInstance("box", "Box", [{ type: "id", value: "text" }]),+ createInstance("button", "Button", []),+ createInstance("text", "Text", []),+ ])+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstance(["text", "box", "body"], {+ parentSelector: ["body"],+ position: 1,+ });+ const instances = $instances.get();+ const newTextId = instances.get("body")?.children[1].value as string;+ // body+ // box+ // text+ // button+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "box" },+ { type: "id", value: newTextId },+ { type: "id", value: "button" },+ ]),+ createInstance("box", "Box", []),+ createInstance("button", "Button", []),+ createInstance(newTextId, "Text", []),+ ])+ );+ });++ test("to the end", () => {+ // body+ // box+ // text+ $instances.set(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "box" }]),+ createInstance("box", "Box", [{ type: "id", value: "text" }]),+ createInstance("text", "Text", []),+ ])+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstance(["text", "box", "body"], {+ parentSelector: ["body"],+ position: "end",+ });+ const instances = $instances.get();+ const newTextId = instances.get("body")?.children[1].value as string;+ // body+ // box+ // text+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "box" },+ { type: "id", value: newTextId },+ ]),+ createInstance("box", "Box", []),+ createInstance(newTextId, "Text", []),+ ])+ );+ });++ test("wrap with fragment when reparent into empty portal", () => {+ // body+ // portal+ // box+ $instances.set(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "portal" },+ { type: "id", value: "box" },+ ]),+ createInstance("portal", portalComponent, []),+ createInstance("box", "Box", []),+ ])+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstance(["box", "body"], {+ parentSelector: ["portal", "body"],+ position: "end",+ });+ const instances = $instances.get();+ const newFragmentId = instances.get("portal")?.children[0].value as string;+ const newBoxId = instances.get(newFragmentId)?.children[0].value as string;+ // body+ // portal+ // fragment+ // box+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "portal" }]),+ createInstance("portal", portalComponent, [+ { type: "id", value: newFragmentId },+ ]),+ createInstance(newFragmentId, "Fragment", [+ { type: "id", value: newBoxId },+ ]),+ createInstance(newBoxId, "Box", []),+ ])+ );+ });++ test("reuse existing fragment when reparent into portal", () => {+ // body+ // portal+ // fragment+ // box+ $instances.set(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "portal" },+ { type: "id", value: "box" },+ ]),+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", []),+ createInstance("box", "Box", []),+ ])+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstance(["box", "body"], {+ parentSelector: ["portal", "body"],+ position: "end",+ });+ const instances = $instances.get();+ const newBoxId = instances.get("fragment")?.children[0].value as string;+ // body+ // portal+ // fragment+ // box+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "portal" }]),+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", [+ { type: "id", value: newBoxId },+ ]),+ createInstance(newBoxId, "Box", []),+ ])+ );+ });++ test("prevent portal reparenting into own children to avoid infinite loop", () => {+ // body+ // portal+ // fragment+ $instances.set(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "portal" }]),+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", []),+ ])+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstance(["portal", "body"], {+ parentSelector: ["fragment", "portal", "body"],+ position: "end",+ });+ const instances = $instances.get();+ // body+ // portal+ // fragment+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "portal" }]),+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", []),+ ])+ );+ });++ test("from collection item", () => {// body// list// box$instances.set(- new Map([- createInstancePair("body", "Body", [{ type: "id", value: "list" }]),- createInstancePair("list", collectionComponent, [+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "list" }]),+ createInstance("list", collectionComponent, [{ type: "id", value: "box" },]),- createInstancePair("box", "Box", []),+ createInstance("box", "Box", []),]));$registeredComponentMetas.set(createFakeComponentMetas({}));reparentInstance(["box", "list[0]", "list", "body"], {parentSelector: ["body"],- position: 1,+ position: "end",});- expect($instances.get()).toEqual(- new Map([- createInstancePair("body", "Body", [+ const instances = $instances.get();+ const newBoxId = instances.get("body")?.children[1].value as string;+ // body+ // list+ // box+ expect(instances).toEqual(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "list" },- { type: "id", value: "box" },+ { type: "id", value: newBoxId },]),- createInstancePair("list", collectionComponent, []),- createInstancePair("box", "Box", []),+ createInstance("list", collectionComponent, []),+ createInstance(newBoxId, "Box", []),]));});@@ -1145,7 +1342,6 @@ describe("insert instances slice copy", () => {$project.set({ id: "current_project" } as Project);beforeEach(() => {- $pages.set(createDefaultPages({ rootInstanceId: "" }));$assets.set(new Map());$breakpoints.set(new Map());$styleSourceSelections.set(new Map());diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 401dbf8c18..6f439d9163 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -50,9 +50,9 @@ import {type InstanceSelector,findLocalStyleSourcesWithinInstances,insertInstancesMutable,- reparentInstanceMutable,getAncestorInstanceSelector,insertPropsCopyMutable,+ getReparentDropTargetMutable,} from "./tree-utils";import { removeByMutable } from "./array-utils";import { isBaseBreakpoint } from "./breakpoints";@@ -441,23 +441,52 @@ export const getComponentTemplateData = (component: string) => {};export const reparentInstance = (- targetInstanceSelector: InstanceSelector,+ sourceInstanceSelector: InstanceSelector,dropTarget: DroppableTarget) => {- serverSyncStore.createTransaction(- [$instances, $props],- (instances, props) => {- reparentInstanceMutable(- instances,- props,- $registeredComponentMetas.get(),- targetInstanceSelector,- dropTarget- );+ const [rootInstanceId] = sourceInstanceSelector;+ const fragment = getInstancesSlice(rootInstanceId);+ updateWebstudioData((data) => {+ const reparentDropTarget = getReparentDropTargetMutable(+ data.instances,+ data.props,+ $registeredComponentMetas.get(),+ sourceInstanceSelector,+ dropTarget+ );+ if (reparentDropTarget === undefined) {+ return;}- );- $selectedInstanceSelector.set(targetInstanceSelector);- $selectedStyleSourceSelector.set(undefined);+ deleteInstanceMutable(data, sourceInstanceSelector);+ const rootInstanceId = insertInstancesSliceCopy({+ data,+ slice: fragment,+ availableDataSources: findAvailableDataSources(+ data.dataSources,+ data.instances,+ reparentDropTarget.parentSelector+ ),+ });+ if (rootInstanceId === undefined) {+ return;+ }+ const [newParentId] = reparentDropTarget.parentSelector;+ const newParent = data.instances.get(newParentId);+ if (newParent === undefined) {+ return;+ }+ const newChild = { type: "id" as const, value: rootInstanceId };+ if (reparentDropTarget.position === "end") {+ newParent.children.push(newChild);+ } else {+ newParent.children.splice(reparentDropTarget.position, 0, newChild);+ }+ $selectedInstanceSelector.set([+ rootInstanceId,+ ...reparentDropTarget.parentSelector,+ ]);+ $selectedStyleSourceSelector.set(undefined);+ });};export const deleteInstanceMutable = (diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.tsindex 9adbf85a10..d7940fe0a3 100644--- a/apps/builder/app/shared/tree-utils.test.ts+++ b/apps/builder/app/shared/tree-utils.test.ts@@ -1,7 +1,6 @@import { test, expect, describe } from "@jest/globals";import type {Instance,- Instances,Prop,StyleDecl,Styles,@@ -17,7 +16,6 @@ import {getAncestorInstanceSelector,insertInstancesMutable,insertPropsCopyMutable,- reparentInstanceMutable,} from "./tree-utils";const baseMetasMap = new Map(Object.entries(baseMetas));@@ -367,248 +365,6 @@ describe("insert instances into container with text or rich text children", () =});});-test("reparent instance into target", () => {- const instances: Instances = new Map([- createInstancePair("root", "Body", [- { type: "id", value: "target" },- { type: "id", value: "box1" },- { type: "id", value: "box2" },- ]),- createInstancePair("target", "Box", []),- createInstancePair("box1", "Box", [- { type: "id", value: "box11" },- { type: "id", value: "box12" },- { type: "id", value: "box13" },- ]),- createInstancePair("box2", "Box", []),- createInstancePair("box11", "Box", []),- createInstancePair("box12", "Box", []),- createInstancePair("box13", "Box", []),- ]);-- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["target", "root"],- {- parentSelector: ["box1", "root"],- position: 1,- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "box1" },- { type: "id", value: "box2" },- ]),- createInstancePair("target", "Box", []),- createInstancePair("box1", "Box", [- { type: "id", value: "box11" },- { type: "id", value: "target" },- { type: "id", value: "box12" },- { type: "id", value: "box13" },- ]),- createInstancePair("box2", "Box", []),- createInstancePair("box11", "Box", []),- createInstancePair("box12", "Box", []),- createInstancePair("box13", "Box", []),- ])- );-- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["target", "box1", "root"],- {- parentSelector: ["box1", "root"],- position: 3,- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "box1" },- { type: "id", value: "box2" },- ]),- createInstancePair("target", "Box", []),- createInstancePair("box1", "Box", [- { type: "id", value: "box11" },- { type: "id", value: "box12" },- { type: "id", value: "target" },- { type: "id", value: "box13" },- ]),- createInstancePair("box2", "Box", []),- createInstancePair("box11", "Box", []),- createInstancePair("box12", "Box", []),- createInstancePair("box13", "Box", []),- ])- );-- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["target", "box1", "root"],- {- parentSelector: ["root"],- position: "end",- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "box1" },- { type: "id", value: "box2" },- { type: "id", value: "target" },- ]),- createInstancePair("target", "Box", []),- createInstancePair("box1", "Box", [- { type: "id", value: "box11" },- { type: "id", value: "box12" },- { type: "id", value: "box13" },- ]),- createInstancePair("box2", "Box", []),- createInstancePair("box11", "Box", []),- createInstancePair("box12", "Box", []),- createInstancePair("box13", "Box", []),- ])- );-});--test("reparent instance into slot", () => {- const instances: Instances = new Map([- createInstancePair("root", "Body", [- { type: "id", value: "slot1" },- { type: "id", value: "box2" },- { type: "id", value: "box3" },- ]),- createInstancePair("slot1", "Slot", [{ type: "id", value: "fragment11" }]),- createInstancePair("fragment11", "Fragment", []),- createInstancePair("box2", "Box", [- { type: "id", value: "box21" },- { type: "id", value: "box22" },- { type: "id", value: "box23" },- ]),- createInstancePair("box21", "Box", []),- createInstancePair("box22", "Box", []),- createInstancePair("box23", "Box", []),- createInstancePair("slot3", "Slot", []),- ]);-- // reuse existing fragment when drop into slot- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["box2", "root"],- {- parentSelector: ["slot1", "root"],- position: "end",- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "slot1" },- { type: "id", value: "box3" },- ]),- createInstancePair("slot1", "Slot", [- { type: "id", value: "fragment11" },- ]),- createInstancePair("fragment11", "Fragment", [- { type: "id", value: "box2" },- ]),- createInstancePair("box2", "Box", [- { type: "id", value: "box21" },- { type: "id", value: "box22" },- { type: "id", value: "box23" },- ]),- createInstancePair("box21", "Box", []),- createInstancePair("box22", "Box", []),- createInstancePair("box23", "Box", []),- createInstancePair("slot3", "Slot", []),- ])- );-- // create new fragment when drop into empty slot- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["box2", "fragment11", "slot1", "root"],- {- parentSelector: ["slot3", "root"],- position: "end",- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "slot1" },- { type: "id", value: "box3" },- ]),- createInstancePair("slot1", "Slot", [- { type: "id", value: "fragment11" },- ]),- createInstancePair("fragment11", "Fragment", []),- createInstancePair("box2", "Box", [- { type: "id", value: "box21" },- { type: "id", value: "box22" },- { type: "id", value: "box23" },- ]),- createInstancePair("box21", "Box", []),- createInstancePair("box22", "Box", []),- createInstancePair("box23", "Box", []),- createInstancePair("slot3", "Slot", [- { type: "id", value: expectString },- ]),- createInstancePair(expectString, "Fragment", [- { type: "id", value: "box2" },- ]),- ])- );-- // prevent reparenting into own children to avoid infinite loop- reparentInstanceMutable(- instances,- new Map(),- baseMetasMap,- ["slot1", "root"],- {- parentSelector: ["fragment11", "slot1", "root"],- position: "end",- }- );- expect(instances).toEqual(- new Map([- createInstancePair("root", "Body", [- { type: "id", value: "slot1" },- { type: "id", value: "box3" },- ]),- createInstancePair("slot1", "Slot", [- { type: "id", value: "fragment11" },- ]),- createInstancePair("fragment11", "Fragment", []),- createInstancePair("box2", "Box", [- { type: "id", value: "box21" },- { type: "id", value: "box22" },- { type: "id", value: "box23" },- ]),- createInstancePair("box21", "Box", []),- createInstancePair("box22", "Box", []),- createInstancePair("box23", "Box", []),- createInstancePair("slot3", "Slot", [- { type: "id", value: expectString },- ]),- createInstancePair(expectString, "Fragment", [- { type: "id", value: "box2" },- ]),- ])- );-});-test("insert props copy with new ids and apply new instance ids", () => {const props = new Map([["prop1", createProp("prop1", "instance1")],diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.tsindex 57cc0c58b3..eaf0ca9958 100644--- a/apps/builder/app/shared/tree-utils.ts+++ b/apps/builder/app/shared/tree-utils.ts@@ -196,13 +196,13 @@ export const wrapEditableChildrenAroundDropTargetMutable = (};};-export const reparentInstanceMutable = (+export const getReparentDropTargetMutable = (instances: Instances,props: Props,metas: Map<string, WsComponentMeta>,instanceSelector: InstanceSelector,dropTarget: DroppableTarget-) => {+): undefined | DroppableTarget => {const [instanceId, parentInstanceId, grandparentInstanceId] =instanceSelector;const grandparentInstance =@@ -230,7 +230,6 @@ export const reparentInstanceMutable = () ?? dropTarget;const [parentId] = dropTarget.parentSelector;const nextParent = instances.get(parentId);- const instance = instances.get(instanceId);// delect is target is one of own descendants// prevent reparenting to avoid infinite loop@@ -241,11 +240,7 @@ export const reparentInstanceMutable = (}}- if (- prevParent === undefined ||- nextParent === undefined ||- instance === undefined- ) {+ if (prevParent === undefined || nextParent === undefined) {return;}@@ -267,12 +262,10 @@ export const reparentInstanceMutable = (nextPosition -= 1;}- const [child] = prevParent.children.splice(prevPosition, 1);- if (nextPosition === "end") {- nextParent.children.push(child);- } else {- nextParent.children.splice(nextPosition, 0, child);- }+ return {+ parentSelector: dropTarget.parentSelector,+ position: nextPosition,+ };};export const cloneStyles = (