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.

make drag item selected after reparent & bugfixes

Roman Pominov6b9cf0108c86
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index 1c99f92927..4483689620 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -110,6 +110,8 @@ export const useInsertInstance = () => {
};
export const useReparentInstance = () => {
+ const [selectedInstance, setSelectedInstance] = useSelectedInstance();
+
useSubscribe<
"reparentInstance",
{
@@ -125,6 +127,11 @@ export const useReparentInstance = () => {
dropTarget.instanceId,
dropTarget.position
);
+
+ // Make the drag item selected
+ if (selectedInstance?.id !== instanceId) {
+ setSelectedInstance(findInstanceById(rootInstance, instanceId));
+ }
});
});
};
@@ -236,7 +243,14 @@ const publishRect = (rect: DOMRect) => {
export const usePublishSelectedInstanceDataRect = () => {
const [element] = useSelectedElement();
- const [refCallback, rect] = useMeasure();
+ const [refCallback, rect, handleChange] = useMeasure();
+
+ // Reparenting an instance may update the rect.
+ // This is a bit hacky, maybe we should use a mutation observer instead.
+ useSubscribe("reparentInstance", () => {
+ // Using setTimeout to make sure all other "reparentInstance" handlers are finished
+ setTimeout(handleChange);
+ });
useEffect(() => {
// Disconnect observer when there is no element.
diff --git a/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx b/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx
index e5144e8ca6..8c13f9a419 100644
--- a/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx
+++ b/apps/designer/app/designer/shared/tree/sortable-tree.stories.tsx
@@ -50,7 +50,7 @@ export const StressTest = ({ animate }: { animate: boolean }) => {
animate={animate}
root={root}
selectedInstanceId={selectedInstanceId}
- onSelect={(instance) => setSelectedInstanceId(instance.id)}
+ onSelect={(instanceId) => setSelectedInstanceId(instanceId)}
onDragEnd={(payload) =>
setRoot(
produce((draft) =>
diff --git a/apps/designer/app/shared/dom-hooks/use-measure.ts b/apps/designer/app/shared/dom-hooks/use-measure.ts
index fadea3217f..5712b6e7c6 100644
--- a/apps/designer/app/shared/dom-hooks/use-measure.ts
+++ b/apps/designer/app/shared/dom-hooks/use-measure.ts
@@ -11,7 +11,7 @@ export type UseMeasureRef<MeasuredElement extends HTMLElement = HTMLElement> = (
) => void;
export type UseMeasureResult<
MeasuredElement extends HTMLElement = HTMLElement
-> = [UseMeasureRef<MeasuredElement>, DOMRect | undefined];
+> = [UseMeasureRef<MeasuredElement>, DOMRect | undefined, () => void];
export const useMeasure = <
MeasuredElement extends HTMLElement = HTMLElement
@@ -45,5 +45,5 @@ export const useMeasure = <
useEffect(handleChange, [handleChange]);
- return [setElement, rect];
+ return [setElement, rect, handleChange];
};