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
Delete instance styles and props (#887)
Bogdan Chadkin9221c2323483
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.tsindex 6fda42e3dd..6db86815ab 100644--- a/apps/designer/app/canvas/shared/instance.ts+++ b/apps/designer/app/canvas/shared/instance.ts@@ -9,10 +9,10 @@ import {rootInstanceContainer,selectedInstanceIdStore,stylesContainer,- useRootInstance,useTextEditingInstanceId,} from "~/shared/nano-states";import { publish } from "~/shared/pubsub";+import { deleteInstanceMutable } from "~/shared/tree-utils";declare module "~/shared/pubsub" {export interface PubsubMap {@@ -116,38 +116,29 @@ export const useReparentInstance = () => {};export const useDeleteInstance = () => {- const [rootInstance] = useRootInstance();- useSubscribe("deleteInstance", ({ id }) => {- const selectedInstanceId = selectedInstanceIdStore.get();- if (rootInstance !== undefined && selectedInstanceId !== undefined) {- // @todo tell user they can't delete root- if (id === rootInstance.id) {- return;- }-- const parentInstance = utils.tree.findParentInstance(rootInstance, id);- if (parentInstance !== undefined) {- const siblingInstance = utils.tree.findParentInstance(- parentInstance,- id- );- selectedInstanceIdStore.set(siblingInstance?.id ?? parentInstance.id);- }+ useSubscribe("deleteInstance", ({ id: deletedInstanceId }) => {+ const rootInstance = rootInstanceContainer.get();+ // @todo tell user they can't delete root+ if (deletedInstanceId === rootInstance?.id) {+ return;}- // @todo deleting instance should involve also deleting it's props- // If we don't delete them - they just live both on client and db- // Pros:- // - if we undo the deletion we don't need to undo the props deletion- // - in a multiplayer environment, some other user could have changed a prop while we have deleted the instance- // and then if we restore the instance, we would be restoring it with our props, potentially overwriting other users changes- // The way it is now it will actually still enable parallel deletion props editing and restoration.- // Contra: we are piling them up.- // Potentially we could also solve this by periodically removing unused props after while when instance was deleted- store.createTransaction([rootInstanceContainer], (rootInstance) => {- if (rootInstance !== undefined) {- utils.tree.deleteInstanceMutable(rootInstance, id);+ store.createTransaction(+ [rootInstanceContainer, propsStore, stylesContainer],+ (rootInstance, props, styles) => {+ if (rootInstance === undefined) {+ return;+ }+ const parentInstance = deleteInstanceMutable({+ rootInstance,+ props,+ styles,+ deletedInstanceId,+ });+ if (parentInstance) {+ selectedInstanceIdStore.set(parentInstance.id);+ }}- });+ );});};diff --git a/apps/designer/app/shared/tree-utils.test.ts b/apps/designer/app/shared/tree-utils.test.tsnew file mode 100644index 0000000000..b3a0b8b7f8--- /dev/null+++ b/apps/designer/app/shared/tree-utils.test.ts@@ -0,0 +1,97 @@+import { test, expect } from "@jest/globals";+import type { Instance, Props, Styles } from "@webstudio-is/project-build";+import { deleteInstanceMutable } from "./tree-utils";++const createInstance = (id: string, children: Instance[]): Instance => {+ return {+ type: "instance",+ id,+ component: "Box",+ children: children,+ };+};++const createProp = (id: string, instanceId: string): Props[number] => {+ return {+ type: "string",+ id,+ instanceId,+ name: "prop",+ value: "value",+ };+};++const createStyleDecl = (instanceId: string): Styles[number] => {+ return {+ breakpointId: "breakpoint",+ instanceId,+ property: "width",+ value: {+ type: "keyword",+ value: "keyword",+ },+ };+};++test("delete instance with own and descendants styles and props", () => {+ const rootInstance: Instance = createInstance("root", [+ createInstance("box1", []),+ createInstance("box2", [+ createInstance("box3", [+ createInstance("child1", []),+ createInstance("child2", [createInstance("descendant", [])]),+ createInstance("child3", []),+ ]),+ ]),+ createInstance("box4", []),+ ]);+ const props: Props = [+ createProp("1", "root"),+ createProp("2", "box1"),+ createProp("3", "box2"),+ createProp("4", "box3"),+ createProp("7", "child3"),+ createProp("6", "child2"),+ createProp("5", "child1"),+ createProp("8", "descendant"),+ createProp("9", "box4"),+ ];+ const styles: Styles = [+ createStyleDecl("root"),+ createStyleDecl("box1"),+ createStyleDecl("box2"),+ createStyleDecl("box3"),+ createStyleDecl("child3"),+ createStyleDecl("child2"),+ createStyleDecl("child1"),+ createStyleDecl("descendant"),+ createStyleDecl("box4"),+ ];++ deleteInstanceMutable({+ rootInstance,+ props,+ styles,+ deletedInstanceId: "box3",+ });++ expect(rootInstance).toEqual(+ createInstance("root", [+ createInstance("box1", []),+ createInstance("box2", []),+ createInstance("box4", []),+ ])+ );+ expect(props).toEqual([+ createProp("1", "root"),+ createProp("2", "box1"),+ createProp("3", "box2"),+ createProp("9", "box4"),+ ]);+ expect(styles).toEqual([+ createStyleDecl("root"),+ createStyleDecl("box1"),+ createStyleDecl("box2"),+ createStyleDecl("box4"),+ ]);+});diff --git a/apps/designer/app/shared/tree-utils.ts b/apps/designer/app/shared/tree-utils.tsnew file mode 100644index 0000000000..4d081f8040--- /dev/null+++ b/apps/designer/app/shared/tree-utils.ts@@ -0,0 +1,61 @@+import type { Instance, Props, Styles } from "@webstudio-is/project-build";+import { removeByMutable } from "./array-utils";++const traverseInstances = (+ instance: Instance,+ cb: (child: Instance, parent: Instance) => void+) => {+ for (const child of instance.children) {+ if (child.type === "text") {+ continue;+ }+ if (child.type === "instance") {+ cb(child, instance);+ traverseInstances(child, cb);+ }+ }+};++export const deleteInstanceMutable = ({+ rootInstance,+ props,+ styles,+ deletedInstanceId,+}: {+ rootInstance: Instance;+ props: Props;+ styles: Styles;+ deletedInstanceId: string;+}) => {+ const parentInstances = new Map<Instance["id"], Instance>();+ const deletedInstances = new Set<Instance["id"]>();++ traverseInstances(rootInstance, (child, instance) => {+ parentInstances.set(child.id, instance);+ // mark as deleted the instance+ if (child.id === deletedInstanceId) {+ deletedInstances.add(child.id);+ }+ // and all descendants of deleted instance+ if (deletedInstances.has(instance.id)) {+ deletedInstances.add(child.id);+ }+ });++ const parentInstance = parentInstances.get(deletedInstanceId);+ if (parentInstance === undefined) {+ return;+ }++ removeByMutable(+ parentInstance.children,+ (child) => child.type === "instance" && child.id === deletedInstanceId+ );+ // delete props and styles of deleted instance and its descendants+ removeByMutable(props, (prop) => deletedInstances.has(prop.instanceId));+ removeByMutable(styles, (styleDecl) =>+ deletedInstances.has(styleDecl.instanceId)+ );++ return parentInstance;+};