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 possible cyclic slot and hide its styles and props panels (#1306)
Bogdan Chadkin68af3b58b607
diff --git a/apps/builder/app/builder/features/inspector/inspector.tsx b/apps/builder/app/builder/features/inspector/inspector.tsxindex faf97048a4..d3ed8dbb8d 100644--- a/apps/builder/app/builder/features/inspector/inspector.tsx+++ b/apps/builder/app/builder/features/inspector/inspector.tsx@@ -1,4 +1,4 @@-import { useRef } from "react";+import { useEffect, useRef, useState } from "react";import { useStore } from "@nanostores/react";import type { Publish } from "~/shared/pubsub";import {@@ -49,6 +49,16 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {const selectedInstance = useStore(selectedInstanceStore);const tabsRef = useRef<HTMLDivElement>(null);+ const [tab, setTab] = useState("style");++ useEffect(() => {+ if (selectedInstance?.component === "Slot") {+ if (tab === "style" || tab === "props") {+ setTab("settings");+ }+ }+ }, [selectedInstance, tab]);+if (navigatorLayout === "docked") {return <NavigatorTreePreview />;}@@ -75,13 +85,19 @@ export const Inspector = ({ publish, navigatorLayout }: InspectorProps) => {skipDelayDuration={0}><FloatingPanelProvider container={tabsRef}>- <Flex as={Tabs} defaultValue="style" grow ref={tabsRef}>+ <Flex as={Tabs} grow ref={tabsRef} value={tab} onValueChange={setTab}><TabsList>- <TabsTrigger value="style">+ <TabsTrigger+ value="style"+ disabled={selectedInstance.component === "Slot"}+ ><DeprecatedText2>Style</DeprecatedText2></TabsTrigger>{/* @note: events would be part of props */}- <TabsTrigger value="props">+ <TabsTrigger+ value="props"+ disabled={selectedInstance.component === "Slot"}+ ><DeprecatedText2>Properties</DeprecatedText2></TabsTrigger><TabsTrigger value="settings">diff --git a/apps/builder/app/shared/tree-utils.test.ts b/apps/builder/app/shared/tree-utils.test.tsindex 781fcde4df..5d167c01a2 100644--- a/apps/builder/app/shared/tree-utils.test.ts+++ b/apps/builder/app/shared/tree-utils.test.ts@@ -468,6 +468,88 @@ test("insert tree of instances copy and provide map from ids map", () => {]);});+test("insert slot or do nothing when slot is cyclic", () => {+ const instances = new Map([+ createInstancePair("root", "Body", [+ { type: "id", value: "slot" },+ { type: "id", value: 'sibling",' },+ ]),+ createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),+ createInstancePair("fragment", "Fragment", [+ { type: "id", value: "child" },+ ]),+ createInstancePair("child", "Box", []),+ createInstancePair("sibling", "Box", []),+ ]);+ const copiedInstances = [+ createInstance("slot", "Slot", [{ type: "id", value: "fragment" }]),+ ];++ // insert slot own descendant+ let copiedInstanceIds = insertInstancesCopyMutable(+ instances,+ copiedInstances,+ {+ parentSelector: ["child", "fragment", "slot", "root"],+ position: "end",+ }+ );+ expect(copiedInstanceIds).toEqual(new Map());+ expect(Array.from(instances.entries())).toEqual([+ createInstancePair("root", "Body", [+ { type: "id", value: "slot" },+ { type: "id", value: 'sibling",' },+ ]),+ createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),+ createInstancePair("fragment", "Fragment", [+ { type: "id", value: "child" },+ ]),+ createInstancePair("child", "Box", []),+ createInstancePair("sibling", "Box", []),+ ]);++ // insert slot into itself+ copiedInstanceIds = insertInstancesCopyMutable(instances, copiedInstances, {+ parentSelector: ["slot", "root"],+ position: "end",+ });+ expect(copiedInstanceIds).toEqual(new Map());+ expect(Array.from(instances.entries())).toEqual([+ createInstancePair("root", "Body", [+ { type: "id", value: "slot" },+ { type: "id", value: 'sibling",' },+ ]),+ createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),+ createInstancePair("fragment", "Fragment", [+ { type: "id", value: "child" },+ ]),+ createInstancePair("child", "Box", []),+ createInstancePair("sibling", "Box", []),+ ]);++ // insert slot into sibling+ copiedInstanceIds = insertInstancesCopyMutable(instances, copiedInstances, {+ parentSelector: ["sibling", "root"],+ position: "end",+ });+ expect(copiedInstanceIds).toEqual(new Map([["slot", expectString]]));+ expect(Array.from(instances.entries())).toEqual([+ createInstancePair("root", "Body", [+ { type: "id", value: "slot" },+ { type: "id", value: 'sibling",' },+ ]),+ createInstancePair("slot", "Slot", [{ type: "id", value: "fragment" }]),+ createInstancePair("fragment", "Fragment", [+ { type: "id", value: "child" },+ ]),+ createInstancePair("child", "Box", []),+ createInstancePair("sibling", "Box", [{ type: "id", value: expectString }]),+ createInstancePair(expectString, "Slot", [+ { type: "id", value: "fragment" },+ ]),+ ]);+});+test("insert style sources copy with new ids and provide map from old ids", () => {const styleSources = new Map([["local1", createStyleSource("local", "local1")],diff --git a/apps/builder/app/shared/tree-utils.ts b/apps/builder/app/shared/tree-utils.tsindex 9eddf2cbf7..2fb049c0ec 100644--- a/apps/builder/app/shared/tree-utils.ts+++ b/apps/builder/app/shared/tree-utils.ts@@ -181,6 +181,22 @@ const getInstanceOrCreateFragmentIfNecessary = (return instance;};+const getSlotFragmentSelector = (+ instances: Instances,+ instanceSelector: InstanceSelector+) => {+ const instance = instances.get(instanceSelector[0]);+ if (+ instance?.component !== "Slot" ||+ instance.children.length === 0 ||+ instance.children[0].type !== "id"+ ) {+ return;+ }+ // first slot child is always fragment+ return [instance.children[0].value, ...instanceSelector];+};+export const reparentInstanceMutable = (instances: Instances,instanceSelector: InstanceSelector,@@ -328,7 +344,7 @@ export const insertInstancesCopyMutable = (copiedInstanceIds.set(instance.id, newInstanceId);}- const preservedChildren = new Set<Instance["id"]>();+ const preservedChildIds = new Set<Instance["id"]>();for (const instance of copiedInstances) {copiedInstancesWithNewIds.push({@@ -337,7 +353,7 @@ export const insertInstancesCopyMutable = (children: instance.children.map((child) => {if (child.type === "id") {if (copiedInstanceIds.has(child.value) === false) {- preservedChildren.add(child.value);+ preservedChildIds.add(child.value);}return {type: "id",@@ -352,8 +368,12 @@ export const insertInstancesCopyMutable = (// slot descendants ids are preserved// so need to prevent pasting slot inside itself// to avoid circular tree- for (const instanceId of dropTarget.parentSelector) {- if (preservedChildren.has(instanceId)) {+ const dropTargetSelector =+ // consider slot fragment when check for cycles to avoid cases like pasting slot directly into slot+ getSlotFragmentSelector(instances, dropTarget.parentSelector) ??+ dropTarget.parentSelector;+ for (const instanceId of dropTargetSelector) {+ if (preservedChildIds.has(instanceId)) {return new Map();}}