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: reparent within another slot instance (#5021)
Bogdan Chadkin47d051722349
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsxindex 8059bc6a0c..c576598ce8 100644--- a/apps/builder/app/shared/instance-utils.test.tsx+++ b/apps/builder/app/shared/instance-utils.test.tsx@@ -25,18 +25,14 @@ import type {WebstudioFragment,WsComponentMeta,} from "@webstudio-is/sdk";-import {- coreMetas,- portalComponent,- collectionComponent,-} from "@webstudio-is/sdk";+import { coreMetas, portalComponent } from "@webstudio-is/sdk";import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";import {findClosestEditableInstanceSelector,deleteInstanceMutable,extractWebstudioFragment,insertWebstudioFragmentCopy,- reparentInstance,+ reparentInstanceMutable,getWebstudioData,insertInstanceChildrenMutable,findClosestInsertable,@@ -501,249 +497,267 @@ describe("insert webstudio fragment at", () => {describe("reparent instance", () => {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", []),- ])+ const data = renderData(+ <$.Body ws:id="body">+ <$.Box ws:id="box">+ <$.Text ws:id="text"></$.Text>+ </$.Box>+ <$.Button ws:id="button"></$.Button>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["text", "box", "body"], {+ reparentInstanceMutable(data, ["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", []),- ])+ const newTextId = data.instances.get("body")?.children[1].value as string;+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Box ws:id="box"></$.Box>+ <$.Text ws:id={newTextId}></$.Text>+ <$.Button ws:id="button"></$.Button>+ </$.Body>+ ).instances);});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", []),- ])+ const data = renderData(+ <$.Body ws:id="body">+ <$.Box ws:id="box">+ <$.Text ws:id="text"></$.Text>+ </$.Box>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["text", "box", "body"], {+ reparentInstanceMutable(data, ["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", []),- ])+ const newTextId = data.instances.get("body")?.children[1].value as string;+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Box ws:id="box"></$.Box>+ <$.Text ws:id={newTextId}></$.Text>+ </$.Body>+ ).instances);});- 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", []),- ])+ test("before itself", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Text ws:id="text"></$.Text>+ <$.Box ws:id="box"></$.Box>+ <$.Button ws:id="button"></$.Button>+ </$.Body>+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstanceMutable(data, ["box", "body"], {+ parentSelector: ["body"],+ position: 1,+ });+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Text ws:id="text"></$.Text>+ <$.Box ws:id="box"></$.Box>+ <$.Button ws:id="button"></$.Button>+ </$.Body>+ ).instances+ );+ });++ test("after itself", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Text ws:id="text"></$.Text>+ <$.Box ws:id="box"></$.Box>+ <$.Button ws:id="button"></$.Button>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["box", "body"], {- parentSelector: ["portal", "body"],+ reparentInstanceMutable(data, ["box", "body"], {+ parentSelector: ["body"],+ position: 2,+ });+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Text ws:id="text"></$.Text>+ <$.Box ws:id="box"></$.Box>+ <$.Button ws:id="button"></$.Button>+ </$.Body>+ ).instances+ );+ });++ test("wrap with fragment when reparent into empty slot", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot"></$.Slot>+ <$.Box ws:id="box"></$.Box>+ </$.Body>+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstanceMutable(data, ["box", "body"], {+ parentSelector: ["slot", "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", []),- ])+ const newFragmentId = data.instances.get("slot")?.children[0]+ .value as string;+ const newBoxId = data.instances.get(newFragmentId)?.children[0]+ .value as string;+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot">+ <$.Fragment ws:id={newFragmentId}>+ <$.Box ws:id={newBoxId}></$.Box>+ </$.Fragment>+ </$.Slot>+ </$.Body>+ ).instances);});- 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", []),- ])+ test("reuse existing fragment when reparent into slot", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot">+ <$.Fragment ws:id="fragment"></$.Fragment>+ </$.Slot>+ <$.Box ws:id="box"></$.Box>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["box", "body"], {- parentSelector: ["portal", "body"],+ reparentInstanceMutable(data, ["box", "body"], {+ parentSelector: ["slot", "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", []),- ])+ const newBoxId = data.instances.get("fragment")?.children[0]+ .value as string;+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot">+ <$.Fragment ws:id="fragment">+ <$.Box ws:id={newBoxId}></$.Box>+ </$.Fragment>+ </$.Slot>+ </$.Body>+ ).instances);});- 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", []),- ])+ test("reparent slot child from one instance of this slot into another", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot1">+ <$.Fragment ws:id="fragment">+ <$.Box ws:id="box"></$.Box>+ </$.Fragment>+ </$.Slot>+ <$.Slot ws:id="slot2">+ {/* same ids */}+ <$.Fragment ws:id="fragment">+ <$.Box ws:id="box"></$.Box>+ </$.Fragment>+ </$.Slot>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["portal", "body"], {- parentSelector: ["fragment", "portal", "body"],+ reparentInstanceMutable(data, ["box", "fragment", "slot1", "body"], {+ parentSelector: ["slot2", "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", []),- ])+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot1">+ <$.Fragment ws:id="fragment">+ <$.Box ws:id="box"></$.Box>+ </$.Fragment>+ </$.Slot>+ <$.Slot ws:id="slot2">+ {/* same ids */}+ <$.Fragment ws:id="fragment">+ <$.Box ws:id="box"></$.Box>+ </$.Fragment>+ </$.Slot>+ </$.Body>+ ).instances);});- test("from collection item", () => {- // body- // list- // box- $instances.set(- toMap([- createInstance("body", "Body", [{ type: "id", value: "list" }]),- createInstance("list", collectionComponent, [- { type: "id", value: "box" },- ]),- createInstance("box", "Box", []),- ])+ test("prevent slot reparenting into own children to avoid infinite loop", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot">+ <$.Fragment ws:id="fragment"></$.Fragment>+ </$.Slot>+ </$.Body>);$registeredComponentMetas.set(createFakeComponentMetas({}));- reparentInstance(["box", "list[0]", "list", "body"], {- parentSelector: ["body"],+ reparentInstanceMutable(data, ["slot", "body"], {+ parentSelector: ["fragment", "slot", "body"],position: "end",});- 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: newBoxId },- ]),- createInstance("list", collectionComponent, []),- createInstance(newBoxId, "Box", []),- ])+ expect(data.instances).toEqual(+ renderData(+ <$.Body ws:id="body">+ <$.Slot ws:id="slot">+ <$.Fragment ws:id="fragment"></$.Fragment>+ </$.Slot>+ </$.Body>+ ).instances);});- test("reparent required child", () => {- $instances.set(+ test("from collection item", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <ws.collection ws:id="collection">+ <$.Box ws:id="box"></$.Box>+ </ws.collection>+ </$.Body>+ );+ $registeredComponentMetas.set(createFakeComponentMetas({}));+ reparentInstanceMutable(+ data,+ ["box", "collection[0]", "collection", "body"],+ { parentSelector: ["body"], position: "end" }+ );+ const newBoxId = data.instances.get("body")?.children[1].value as string;+ expect(data.instances).toEqual(renderData(<$.Body ws:id="body">- <$.Tooltip ws:id="tooltip">- <$.TooltipTrigger ws:id="trigger"></$.TooltipTrigger>- <$.TooltipContent ws:id="content"></$.TooltipContent>- </$.Tooltip>+ <ws.collection ws:id="collection"></ws.collection>+ <$.Box ws:id={newBoxId}></$.Box></$.Body>).instances);+ });++ test("move required child within same instance", () => {+ const data = renderData(+ <$.Body ws:id="body">+ <$.Tooltip ws:id="tooltip">+ <$.TooltipTrigger ws:id="trigger"></$.TooltipTrigger>+ <$.TooltipContent ws:id="content"></$.TooltipContent>+ </$.Tooltip>+ </$.Body>+ );$registeredComponentMetas.set(new Map(Object.entries({ ...defaultMetas, ...radixMetas })));- reparentInstance(["trigger", "tooltip", "body"], {+ reparentInstanceMutable(data, ["trigger", "tooltip", "body"], {parentSelector: ["tooltip", "body"],position: "end",});- expect($instances.get()).toEqual(+ expect(data.instances).toEqual(renderData(<$.Body ws:id="body"><$.Tooltip ws:id="tooltip">diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 45ae284a78..f0609c257e 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -385,51 +385,107 @@ export const getComponentTemplateData = (return generateDataFromEmbedTemplate(template, metas);};-export const reparentInstance = (+export const reparentInstanceMutable = (+ data: Omit<WebstudioData, "pages">,sourceInstanceSelector: InstanceSelector,dropTarget: DroppableTarget) => {const [rootInstanceId] = sourceInstanceSelector;- updateWebstudioData((data) => {- const fragment = extractWebstudioFragment(data, rootInstanceId);- const reparentDropTarget = getReparentDropTargetMutable(- data.instances,- data.props,- $registeredComponentMetas.get(),- sourceInstanceSelector,- dropTarget- );- if (reparentDropTarget === undefined) {- return;- }- deleteInstanceMutable(- data,- getInstancePath(sourceInstanceSelector, data.instances)- );- const { newInstanceIds } = insertWebstudioFragmentCopy({- data,- fragment,- availableVariables: findAvailableVariables({- ...data,- startingInstanceId: reparentDropTarget.parentSelector[0],- }),- });- const newRootInstanceId = newInstanceIds.get(rootInstanceId);- if (newRootInstanceId === undefined) {+ // delect is target is one of own descendants+ // prevent reparenting to avoid infinite loop+ const instanceDescendants = findTreeInstanceIds(+ data.instances,+ rootInstanceId+ );+ for (const instanceId of instanceDescendants) {+ if (dropTarget.parentSelector.includes(instanceId)) {return;}- const [newParentId] = reparentDropTarget.parentSelector;- const newParent = data.instances.get(newParentId);- if (newParent === undefined) {+ }+ // try to use slot fragment as target instead of slot itself+ const parentInstance = data.instances.get(dropTarget.parentSelector[0]);+ if (+ parentInstance?.component === portalComponent &&+ parentInstance.children.length > 0 &&+ parentInstance.children[0].type === "id"+ ) {+ const fragmentId = parentInstance.children[0].value;+ dropTarget = {+ parentSelector: [fragmentId, ...dropTarget.parentSelector],+ position: dropTarget.position,+ };+ }+ // move within same parent+ if (sourceInstanceSelector[1] === dropTarget.parentSelector[0]) {+ const [parentId] = dropTarget.parentSelector;+ const parent = data.instances.get(parentId);+ if (parent === undefined) {return;}- const newChild = { type: "id" as const, value: newRootInstanceId };- if (reparentDropTarget.position === "end") {- newParent.children.push(newChild);+ const prevPosition = parent.children.findIndex(+ (child) => child.type === "id" && child.value === rootInstanceId+ );+ const child = parent.children[prevPosition];+ parent?.children.splice(prevPosition, 1);+ if (dropTarget.position === "end") {+ parent?.children.push(child);} else {- newParent.children.splice(reparentDropTarget.position, 0, newChild);+ // when parent is the same, we need to adjust the position+ // to account for the removal of the instance.+ let nextPosition = dropTarget.position;+ if (prevPosition < nextPosition) {+ nextPosition -= 1;+ }+ parent?.children.splice(nextPosition, 0, child);}- selectInstance([newRootInstanceId, ...reparentDropTarget.parentSelector]);+ return sourceInstanceSelector;+ }+ // move into another parent+ const fragment = extractWebstudioFragment(data, rootInstanceId);+ deleteInstanceMutable(+ data,+ getInstancePath(sourceInstanceSelector, data.instances)+ );+ // prepare drop target after deleting instance to recreate new slot fragment+ dropTarget =+ getReparentDropTargetMutable(+ data.instances,+ data.props,+ $registeredComponentMetas.get(),+ dropTarget+ ) ?? dropTarget;+ const { newInstanceIds } = insertWebstudioFragmentCopy({+ data,+ fragment,+ availableVariables: findAvailableVariables({+ ...data,+ startingInstanceId: dropTarget.parentSelector[0],+ }),+ });+ const [newParentId] = dropTarget.parentSelector;+ const newRootInstanceId =+ newInstanceIds.get(rootInstanceId) ?? rootInstanceId;+ const newParent = data.instances.get(newParentId);+ const newChild = { type: "id" as const, value: newRootInstanceId };+ if (dropTarget.position === "end") {+ newParent?.children.push(newChild);+ } else {+ newParent?.children.splice(dropTarget.position, 0, newChild);+ }+ return [newRootInstanceId, ...dropTarget.parentSelector];+};++export const reparentInstance = (+ sourceInstanceSelector: InstanceSelector,+ dropTarget: DroppableTarget+) => {+ updateWebstudioData((data) => {+ const newSelector = reparentInstanceMutable(+ data,+ sourceInstanceSelector,+ dropTarget+ );+ selectInstance(newSelector);});};diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.tsindex 6103996631..0894436228 100644--- a/apps/builder/app/shared/tree-utils.ts+++ b/apps/builder/app/shared/tree-utils.ts@@ -11,7 +11,7 @@ import type {StyleSourceSelection,WsComponentMeta,} from "@webstudio-is/sdk";-import { findTreeInstanceIds, collectionComponent } from "@webstudio-is/sdk";+import { collectionComponent } from "@webstudio-is/sdk";// slots can have multiple parents so instance should be addressed// with full rendered path to avoid double selections with slots@@ -230,25 +230,8 @@ export const getReparentDropTargetMutable = (instances: Instances,props: Props,metas: Map<string, WsComponentMeta>,- instanceSelector: InstanceSelector,dropTarget: DroppableTarget): undefined | DroppableTarget => {- const [instanceId, parentInstanceId, grandparentInstanceId] =- instanceSelector;- const grandparentInstance =- grandparentInstanceId === undefined- ? undefined- : instances.get(grandparentInstanceId);-- let prevParent =- parentInstanceId === undefined- ? undefined- : instances.get(parentInstanceId);- // skip parent fake "item" instance and use grandparent collection as parent- if (grandparentInstance?.component === collectionComponent) {- prevParent = grandparentInstance;- }-dropTarget = getCollectionDropTarget(instances, dropTarget) ?? dropTarget;dropTarget =getInstanceOrCreateFragmentIfNecessary(instances, dropTarget) ?? dropTarget;@@ -259,44 +242,7 @@ export const getReparentDropTargetMutable = (metas,dropTarget) ?? dropTarget;- const [parentId] = dropTarget.parentSelector;- const nextParent = instances.get(parentId);-- // delect is target is one of own descendants- // prevent reparenting to avoid infinite loop- const instanceDescendants = findTreeInstanceIds(instances, instanceId);- for (const instanceId of instanceDescendants) {- if (dropTarget.parentSelector.includes(instanceId)) {- return;- }- }-- if (prevParent === undefined || nextParent === undefined) {- return;- }-- const prevPosition = prevParent.children.findIndex(- (child) => child.type === "id" && child.value === instanceId- );- if (prevPosition === -1) {- return;- }-- // if parent is the same, we need to adjust the position- // to account for the removal of the instance.- let nextPosition = dropTarget.position;- if (- nextPosition !== "end" &&- prevParent.id === nextParent.id &&- prevPosition < nextPosition- ) {- nextPosition -= 1;- }-- return {- parentSelector: dropTarget.parentSelector,- position: nextPosition,- };+ return dropTarget;};export const cloneStyles = (