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.

getInstanceIdFromElement and findInstanceByElement

Roman Pominovbe70fc915fd8
diff --git a/apps/designer/app/canvas/features/wrapper-component/use-ensure-focus.ts b/apps/designer/app/canvas/features/wrapper-component/use-ensure-focus.ts
index cc17b72395..f1ee74ddca 100644
--- a/apps/designer/app/canvas/features/wrapper-component/use-ensure-focus.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/use-ensure-focus.ts
@@ -1,5 +1,6 @@
import { useCallback } from "react";
import { useSelectedInstance } from "~/canvas/shared/nano-states";
+import { getInstanceIdFromElement } from "~/shared/dom-utils";
/**
* When instance is created or selected via setInstance, we need to focus it.
@@ -10,7 +11,7 @@ export const useEnsureFocus = () => {
(element: HTMLElement | null) => {
if (
element !== null &&
- element.id === selectedInstance?.id &&
+ getInstanceIdFromElement(element) === selectedInstance?.id &&
document.activeElement !== element
) {
element.focus();
diff --git a/apps/designer/app/canvas/shared/instance.ts b/apps/designer/app/canvas/shared/instance.ts
index f341233b62..bafa5c8da6 100644
--- a/apps/designer/app/canvas/shared/instance.ts
+++ b/apps/designer/app/canvas/shared/instance.ts
@@ -36,6 +36,7 @@ import {
useTextEditingInstanceId,
} from "~/shared/nano-states";
import { useMeasure } from "~/shared/dom-hooks";
+import { findInstanceByElement } from "~/shared/dom-utils";
export const usePopulateRootInstance = (tree: Tree) => {
const [, setRootInstance] = useRootInstance();
@@ -238,8 +239,8 @@ export const useSetHoveredInstance = () => {
useEffect(() => {
let instance;
- if (rootInstance !== undefined && hoveredElement?.id) {
- instance = findInstanceById(rootInstance, hoveredElement.id);
+ if (rootInstance !== undefined && hoveredElement) {
+ instance = findInstanceByElement(rootInstance, hoveredElement);
}
setHoveredInstance(instance);
}, [rootInstance, hoveredElement, setHoveredInstance]);
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index b1218606fd..d8a4f36f15 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -3,12 +3,11 @@ import {
useRootInstance,
useTextEditingInstanceId,
} from "~/shared/nano-states";
+import { getInstancePath, createInstance } from "~/shared/tree-utils";
import {
- findInstanceById,
- getInstancePath,
- createInstance,
-} from "~/shared/tree-utils";
-import { getInstanceElementById } from "~/shared/dom-utils";
+ findInstanceByElement,
+ getInstanceElementById,
+} from "~/shared/dom-utils";
import {
type DropTarget,
type Point,
@@ -67,12 +66,11 @@ export const useDragAndDrop = () => {
const dropHandlers = useDrop<Instance>({
isDropTarget(element) {
- return (
- (rootInstance !== undefined &&
- element.id !== "" &&
- findInstanceById(rootInstance, element.id)) ||
- false
- );
+ const instance =
+ rootInstance !== undefined &&
+ findInstanceByElement(rootInstance, element);
+
+ return instance || false;
},
// This must be fast, it can be called multiple times per pointer move
@@ -139,11 +137,11 @@ export const useDragAndDrop = () => {
const dragHandlers = useDrag<Instance>({
isDragItem(element) {
- if (rootInstance === undefined || element.id === "") {
+ if (rootInstance === undefined) {
return false;
}
- const instance = findInstanceById(rootInstance, element.id);
+ const instance = findInstanceByElement(rootInstance, element);
if (instance === undefined) {
return false;
diff --git a/apps/designer/app/canvas/shared/use-track-selected-element.ts b/apps/designer/app/canvas/shared/use-track-selected-element.ts
index 1de08eb109..592830f82a 100644
--- a/apps/designer/app/canvas/shared/use-track-selected-element.ts
+++ b/apps/designer/app/canvas/shared/use-track-selected-element.ts
@@ -11,7 +11,10 @@ import {
useTextEditingInstanceId,
} from "~/shared/nano-states";
import { findInstanceById } from "~/shared/tree-utils";
-import { getInstanceElementById } from "~/shared/dom-utils";
+import {
+ getInstanceElementById,
+ getInstanceIdFromElement,
+} from "~/shared/dom-utils";
const eventOptions = {
passive: true,
@@ -43,7 +46,7 @@ export const useTrackSelectedElement = () => {
if (
selectedInstance !== undefined &&
(selectedElement === undefined ||
- selectedInstance?.id !== selectedElement.id)
+ selectedInstance?.id !== getInstanceIdFromElement(selectedElement))
) {
const element = getInstanceElementById(selectedInstance.id);
if (element === null) return;
@@ -100,7 +103,9 @@ export const useTrackSelectedElement = () => {
// When user double clicks on an inline instance, we need to select the parent instance and put it indo text editing mode.
// Inline instances are not directly, only through parent instance.
if (isInlineOnly) {
- const parentId = element.parentElement?.id;
+ const parentId =
+ element.parentElement &&
+ getInstanceIdFromElement(element.parentElement);
if (parentId) {
selectInstance(parentId);
setEditingInstanceId(parentId);
diff --git a/apps/designer/app/shared/dom-utils/get-instance-element-by-id.ts b/apps/designer/app/shared/dom-utils/get-instance-element-by-id.ts
deleted file mode 100644
index 66fffcf423..0000000000
--- a/apps/designer/app/shared/dom-utils/get-instance-element-by-id.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { type Instance } from "@webstudio-is/react-sdk";
-
-export const getInstanceElementById = (id: Instance["id"]) => {
- return document.getElementById(id);
-};
diff --git a/apps/designer/app/shared/dom-utils/index.ts b/apps/designer/app/shared/dom-utils/index.ts
index 314ea5a25a..b1120b975a 100644
--- a/apps/designer/app/shared/dom-utils/index.ts
+++ b/apps/designer/app/shared/dom-utils/index.ts
@@ -1 +1 @@
-export * from "./get-instance-element-by-id";
+export * from "./instance";
diff --git a/apps/designer/app/shared/dom-utils/instance.ts b/apps/designer/app/shared/dom-utils/instance.ts
new file mode 100644
index 0000000000..070cbceff2
--- /dev/null
+++ b/apps/designer/app/shared/dom-utils/instance.ts
@@ -0,0 +1,25 @@
+import { type Instance } from "@webstudio-is/react-sdk";
+import { findInstanceById } from "~/shared/tree-utils";
+
+export const getInstanceElementById = (id: Instance["id"]) => {
+ return document.getElementById(id);
+};
+
+export const getInstanceIdFromElement = (
+ element: Element
+): Instance["id"] | undefined => {
+ return element.id === "" ? undefined : element.id;
+};
+
+export const findInstanceByElement = (
+ rootInstance: Instance,
+ element: Element
+): Instance | undefined => {
+ const id = getInstanceIdFromElement(element);
+
+ if (id === undefined) {
+ return undefined;
+ }
+
+ return findInstanceById(rootInstance, id);
+};