MCPcopy Create free account
hub / github.com/bytedance/flow-builder / useSort

Function useSort

src/hooks/useSort.ts:7–106  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import type { INode } from '../index';
6
7const useSort = () => {
8 const { nodes, onChange } = useContext(BuilderContext);
9
10 const currentNode = useContext(NodeContext);
11
12 const backward = (node: INode = currentNode) => {
13 if (!node) return;
14
15 const path = node.path?.slice() || [];
16
17 const currentIndex = Number(path.pop());
18
19 const parentNodes = get(nodes, path);
20
21 if (parentNodes) {
22 if (currentIndex > 0) {
23 exchangeNodes(parentNodes, currentIndex, currentIndex - 1);
24 onChange([...nodes], 'sort-backward', node);
25 }
26 } else {
27 if (currentIndex > 1) {
28 exchangeNodes(nodes, currentIndex, currentIndex - 1);
29 onChange([...nodes], 'sort-backward', node);
30 }
31 }
32 };
33
34 const forward = (node: INode = currentNode) => {
35 if (!node) return;
36
37 const path = node.path?.slice() || [];
38
39 const currentIndex = Number(path.pop());
40
41 const parentNodes = get(nodes, path);
42
43 if (parentNodes) {
44 if (currentIndex < parentNodes.length - 1) {
45 exchangeNodes(parentNodes, currentIndex, currentIndex + 1);
46 onChange([...nodes], 'sort-forward', node);
47 }
48 } else {
49 if (currentIndex < nodes.length - 2) {
50 exchangeNodes(nodes, currentIndex, currentIndex + 1);
51 onChange([...nodes], 'sort-forward', node);
52 }
53 }
54 };
55
56 const start = (node: INode = currentNode) => {
57 if (!node) return;
58
59 const path = node.path?.slice() || [];
60
61 const currentIndex = Number(path.pop());
62
63 const parentNodes = get(nodes, path);
64

Callers 2

NodeDisplayFunction · 0.85
ConditionNodeDisplayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected