Skip to content

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.

update selected instance when running undo

Oleg Isonen7d6383c9a1f8
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsx
index 4d78b0352e..c576a43689 100644
--- a/app/canvas/canvas.tsx
+++ b/app/canvas/canvas.tsx
@@ -25,6 +25,7 @@ import {
useReparentInstance,
usePublishSelectedInstance,
usePublishRootInstance,
+ useUpdateSelectedInstance,
} from "./shared/instance";
import { useUpdateStyle } from "./shared/style";
import { useActiveElementTracking } from "./shared/active-element";
@@ -100,6 +101,7 @@ const DesignMode = ({ treeId, project }: DesignModeProps) => {
usePublishRootInstance();
useActiveElementTracking();
useSync({ project });
+ useUpdateSelectedInstance();
const elements = useElementsTree();
return (
// Using touch backend becuase html5 drag&drop doesn't fire drag events in our case
diff --git a/app/canvas/shared/instance.ts b/app/canvas/shared/instance.ts
index 1f540019fb..377236f9cf 100644
--- a/app/canvas/shared/instance.ts
+++ b/app/canvas/shared/instance.ts
@@ -14,6 +14,7 @@ import {
findParentInstance,
findClosestSiblingInstance,
insertInstanceMutable,
+ findInstanceById,
} from "~/shared/tree-utils";
import store from "immerhin";
import { DropData, type SelectedInstanceData } from "~/shared/component";
@@ -156,6 +157,26 @@ export const usePublishSelectedInstance = ({
}, [instance, allUserProps, treeId, browserStyle]);
};
+/**
+ * We need to set the selected instance after a any root instance update,
+ * because anything that we change on the selected instance is actually done on the root, so
+ * when we run "undo", root is going to be undone but not the selected instance, unless we update it here.
+ */
+export const useUpdateSelectedInstance = () => {
+ const [rootInstance] = useRootInstance();
+ const [selectedInstance, setSelectedInstance] = useSelectedInstance();
+
+ useEffect(() => {
+ if (rootInstance === undefined || selectedInstance === undefined) return;
+ const nextSelectedInstance = findInstanceById(
+ rootInstance,
+ selectedInstance.id
+ );
+ if (nextSelectedInstance === undefined) return;
+ setSelectedInstance(nextSelectedInstance);
+ }, [rootInstance, selectedInstance, setSelectedInstance]);
+};
+
export const usePublishRootInstance = () => {
const [rootInstance] = useRootInstance();
useEffect(() => {
diff --git a/app/canvas/shared/style.ts b/app/canvas/shared/style.ts
index f0a56a02b4..3b89b60a0b 100644
--- a/app/canvas/shared/style.ts
+++ b/app/canvas/shared/style.ts
@@ -1,11 +1,11 @@
import store from "immerhin";
import { type StyleUpdates } from "~/shared/component";
-import { findInstanceById, setInstanceStyleMutable } from "~/shared/tree-utils";
+import { setInstanceStyleMutable } from "~/shared/tree-utils";
import { rootInstanceContainer, useSelectedInstance } from "./nano-values";
import { useSubscribe } from "./pubsub";
export const useUpdateStyle = () => {
- const [selectedInstance, setSelectedInstance] = useSelectedInstance();
+ const [selectedInstance] = useSelectedInstance();
useSubscribe<"updateStyle", StyleUpdates>(
"updateStyle",
({ id, updates, breakpoint }) => {
@@ -19,13 +19,6 @@ export const useUpdateStyle = () => {
}
setInstanceStyleMutable(rootInstance, id, updates, breakpoint);
});
-
- if (rootInstanceContainer.value === undefined) return;
- const instance = findInstanceById(rootInstanceContainer.value, id);
- if (instance === undefined) return;
- // We need to set new version of the selected instance after a style update,
- // so that anything that depends on instance.cssRules gets updated too.
- setSelectedInstance(instance);
}
);
};
diff --git a/app/designer/features/style-panel/use-style-data.ts b/app/designer/features/style-panel/use-style-data.ts
index 89503c08dc..a6ff7626f6 100644
--- a/app/designer/features/style-panel/use-style-data.ts
+++ b/app/designer/features/style-panel/use-style-data.ts
@@ -47,7 +47,8 @@ export const useStyleData = ({
const [currentStyle, setCurrentStyle] = useState(getCurrentStyle());
useEffect(() => {
- setCurrentStyle(getCurrentStyle());
+ const currentStyle = getCurrentStyle();
+ setCurrentStyle(currentStyle);
}, [getCurrentStyle]);
const inheritedStyle = useMemo(() => {