Skip to content

Commit 544c956

Browse files
extract get target container func from hook
1 parent 3bb01e3 commit 544c956

2 files changed

Lines changed: 38 additions & 34 deletions

File tree

editor/hooks/use-target-node.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import React, { useEffect, useState } from "react";
1+
import { useEffect, useState } from "react";
22
import type { ReflectSceneNode } from "@design-sdk/core";
3+
import type { DesignInput } from "@designto/config/input";
34
import { useEditorState } from "core/states";
4-
import { DesignInput } from "@designto/config/input";
5-
6-
import { utils as _design_utils } from "@design-sdk/core";
7-
const designq = _design_utils.query;
5+
import { getTargetContainer } from "utils/get-target-node";
86

97
export function useTargetContainer() {
108
const [t, setT] = useState<{ target: ReflectSceneNode; root: DesignInput }>({
@@ -13,36 +11,8 @@ export function useTargetContainer() {
1311
});
1412
const [state] = useEditorState();
1513

16-
//
1714
useEffect(() => {
18-
const thisPageNodes = state.selectedPage
19-
? state.design.pages.find((p) => p.id == state.selectedPage).children
20-
: null;
21-
22-
const targetId =
23-
state?.selectedNodes?.length === 1 ? state.selectedNodes[0] : null;
24-
25-
const container_of_target =
26-
designq.find_node_by_id_under_inpage_nodes(targetId, thisPageNodes) ||
27-
null;
28-
29-
const root = thisPageNodes
30-
? container_of_target &&
31-
(container_of_target.origin === "COMPONENT"
32-
? DesignInput.forMasterComponent({
33-
master: container_of_target,
34-
all: state.design.pages,
35-
components: state.design.components,
36-
})
37-
: DesignInput.fromDesignWithComponents({
38-
design: container_of_target,
39-
components: state.design.components,
40-
}))
41-
: state.design?.input;
42-
43-
const target =
44-
designq.find_node_by_id_under_entry(targetId, root?.entry) ?? root?.entry;
45-
15+
const { root, target } = getTargetContainer(state);
4616
setT({ target, root });
4717
}, [state?.selectedNodes, state?.selectedPage, state?.design?.pages]);
4818

editor/utils/get-target-node.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { EditorState } from "core/states";
2+
import { DesignInput } from "@designto/config/input";
3+
import { utils as _design_utils } from "@design-sdk/core";
4+
const designq = _design_utils.query;
5+
6+
export function getTargetContainer(state: EditorState) {
7+
const thisPageNodes = state.selectedPage
8+
? state.design.pages.find((p) => p.id == state.selectedPage).children
9+
: null;
10+
11+
const targetId =
12+
state?.selectedNodes?.length === 1 ? state.selectedNodes[0] : null;
13+
14+
const container_of_target =
15+
designq.find_node_by_id_under_inpage_nodes(targetId, thisPageNodes) || null;
16+
17+
const root = thisPageNodes
18+
? container_of_target &&
19+
(container_of_target.origin === "COMPONENT"
20+
? DesignInput.forMasterComponent({
21+
master: container_of_target,
22+
all: state.design.pages,
23+
components: state.design.components,
24+
})
25+
: DesignInput.fromDesignWithComponents({
26+
design: container_of_target,
27+
components: state.design.components,
28+
}))
29+
: state.design?.input;
30+
31+
const target =
32+
designq.find_node_by_id_under_entry(targetId, root?.entry) ?? root?.entry;
33+
return { root, target };
34+
}

0 commit comments

Comments
 (0)