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.

add getInstanceElementById

Roman Pominov4fb1e07c5f40
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 94132ee664..a1101b15be 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -4,6 +4,7 @@ import {
useTextEditingInstanceId,
} from "~/shared/nano-states";
import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
+import { getInstanceElementById } from "~/shared/dom-utils";
import {
type DropTarget,
type Point,
@@ -49,7 +50,7 @@ export const useDragAndDrop = () => {
const autoScrollHandlers = useAutoScroll({ fullscreen: true });
const getDefaultDropTarget = () => {
- const element = rootInstance && document.getElementById(rootInstance.id);
+ const element = rootInstance && getInstanceElementById(rootInstance.id);
// Should never happen
if (!element || !rootInstance) {
@@ -108,7 +109,7 @@ export const useDragAndDrop = () => {
return dropTarget;
}
- const element = data && document.getElementById(data.id);
+ const element = data && getInstanceElementById(data.id);
if (element === null) {
return getDefaultDropTarget();
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 3428a7d4cf..1de08eb109 100644
--- a/apps/designer/app/canvas/shared/use-track-selected-element.ts
+++ b/apps/designer/app/canvas/shared/use-track-selected-element.ts
@@ -11,6 +11,7 @@ import {
useTextEditingInstanceId,
} from "~/shared/nano-states";
import { findInstanceById } from "~/shared/tree-utils";
+import { getInstanceElementById } from "~/shared/dom-utils";
const eventOptions = {
passive: true,
@@ -44,7 +45,7 @@ export const useTrackSelectedElement = () => {
(selectedElement === undefined ||
selectedInstance?.id !== selectedElement.id)
) {
- const element = document.getElementById(selectedInstance.id);
+ const element = getInstanceElementById(selectedInstance.id);
if (element === null) return;
element.focus();
setSelectedElement(element);
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
new file mode 100644
index 0000000000..66fffcf423
--- /dev/null
+++ b/apps/designer/app/shared/dom-utils/get-instance-element-by-id.ts
@@ -0,0 +1,5 @@
+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
new file mode 100644
index 0000000000..314ea5a25a
--- /dev/null
+++ b/apps/designer/app/shared/dom-utils/index.ts
@@ -0,0 +1 @@
+export * from "./get-instance-element-by-id";