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.

extract findClosestNonInlineParent

Roman Pominov3a39c9fb9baf
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 581774ec01..9f4575e818 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -3,7 +3,11 @@ import {
useRootInstance,
useTextEditingInstanceId,
} from "~/shared/nano-states";
-import { getInstancePath, createInstance } from "~/shared/tree-utils";
+import {
+ getInstancePath,
+ createInstance,
+ findClosestNonInlineParent,
+} from "~/shared/tree-utils";
import {
findInstanceByElement,
getInstanceElementById,
@@ -169,10 +173,9 @@ export const useDragAndDrop = () => {
// When trying to drag an inline instance, drag its parent instead
if (components[instance.component].isInlineOnly) {
- const path = getInstancePath(rootInstance, instance.id);
- path.reverse();
- const nonInlineParent = path.find(
- (item) => components[item.component].isInlineOnly === false
+ const nonInlineParent = findClosestNonInlineParent(
+ rootInstance,
+ instance.id
);
if (
nonInlineParent !== undefined &&
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 891cdef93d..6ae40c31ec 100644
--- a/apps/designer/app/canvas/shared/use-track-selected-element.ts
+++ b/apps/designer/app/canvas/shared/use-track-selected-element.ts
@@ -10,7 +10,10 @@ import {
useRootInstance,
useTextEditingInstanceId,
} from "~/shared/nano-states";
-import { findInstanceById } from "~/shared/tree-utils";
+import {
+ findClosestNonInlineParent,
+ findInstanceById,
+} from "~/shared/tree-utils";
import {
getInstanceElementById,
getInstanceIdFromElement,
@@ -95,22 +98,21 @@ export const useTrackSelectedElement = () => {
}
const { isInlineOnly, isContentEditable } = components[component];
- if (isContentEditable === false) {
- return;
- }
-
// 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.
+ // Inline instances are not editable directly, only through parent instance.
if (isInlineOnly) {
- const parentId =
- element.parentElement &&
- getInstanceIdFromElement(element.parentElement);
- if (parentId) {
- selectInstance(parentId);
- setEditingInstanceId(parentId);
+ const parent =
+ rootInstance && findClosestNonInlineParent(rootInstance, id);
+ if (parent && components[parent.component].isContentEditable) {
+ selectInstance(parent.id);
+ setEditingInstanceId(parent.id);
}
- } else setEditingInstanceId(id);
- return;
+ return;
+ }
+
+ if (isContentEditable) {
+ setEditingInstanceId(id);
+ }
}
selectInstance(id);
@@ -121,5 +123,5 @@ export const useTrackSelectedElement = () => {
return () => {
window.removeEventListener("click", handleClick);
};
- }, [selectInstance, setEditingInstanceId]);
+ }, [selectInstance, setEditingInstanceId, rootInstance]);
};
diff --git a/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.test.ts b/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.test.ts
new file mode 100644
index 0000000000..273abe8345
--- /dev/null
+++ b/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.test.ts
@@ -0,0 +1,51 @@
+import { createInstance } from "./create-instance";
+import { findClosestNonInlineParent } from "./find-closest-non-inline-parent";
+
+describe("findClosestNonInlineParent", () => {
+ const italic = createInstance({ component: "Italic" });
+
+ const boldWithItalic = createInstance({
+ component: "Bold",
+ children: [italic],
+ });
+
+ test("when there's no non-inline parent, returns undefined", () => {
+ expect(
+ findClosestNonInlineParent(boldWithItalic, italic.id)
+ ).toBeUndefined();
+ expect(findClosestNonInlineParent(italic, italic.id)).toBeUndefined();
+ });
+
+ test("when the given instance isn't in the tree, returns undefined", () => {
+ const root = createInstance({ component: "Body" });
+ expect(findClosestNonInlineParent(root, italic.id)).toBeUndefined();
+ });
+
+ test("retrunrns the first non-inline parent", () => {
+ const root = createInstance({
+ component: "Body",
+ children: [
+ createInstance({
+ component: "Heading",
+ id: "the-heading",
+ children: [italic],
+ }),
+ ],
+ });
+ expect(findClosestNonInlineParent(root, italic.id)?.id).toBe("the-heading");
+ });
+
+ test("retrunrns the first non-inline parent (nested)", () => {
+ const root = createInstance({
+ component: "Body",
+ children: [
+ createInstance({
+ component: "Heading",
+ id: "the-heading",
+ children: [boldWithItalic],
+ }),
+ ],
+ });
+ expect(findClosestNonInlineParent(root, italic.id)?.id).toBe("the-heading");
+ });
+});
diff --git a/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.ts b/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.ts
new file mode 100644
index 0000000000..da5a74e182
--- /dev/null
+++ b/apps/designer/app/shared/tree-utils/find-closest-non-inline-parent.ts
@@ -0,0 +1,11 @@
+import { Instance, components } from "@webstudio-is/react-sdk";
+import { getInstancePath } from "./get-instance-path";
+
+export const findClosestNonInlineParent = (
+ rootInstance: Instance,
+ instanceId: Instance["id"]
+): Instance | undefined => {
+ const path = getInstancePath(rootInstance, instanceId);
+ path.reverse();
+ return path.find((item) => components[item.component].isInlineOnly === false);
+};
diff --git a/apps/designer/app/shared/tree-utils/index.ts b/apps/designer/app/shared/tree-utils/index.ts
index 242d04ddbb..4f4109330c 100644
--- a/apps/designer/app/shared/tree-utils/index.ts
+++ b/apps/designer/app/shared/tree-utils/index.ts
@@ -10,3 +10,4 @@ export * from "./find-closest-sibling-instance";
export * from "./find-parent-instance";
export * from "./clone-instance";
export * from "./reparent-instance";
+export * from "./find-closest-non-inline-parent";