@canmingir/link 1.2.10 → 1.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,268 @@
1
+ export function assertLinkedGraph(data) {
2
+ if (!data || typeof data !== "object") {
3
+ throw new Error(
4
+ "FlowChart expected a linked graph object: { nodes, roots? }."
5
+ );
6
+ }
7
+ const { nodes, roots } = data;
8
+ if (!nodes || typeof nodes !== "object") {
9
+ throw new Error("FlowChart expected data.nodes to be an object.");
10
+ }
11
+
12
+ let useRoots = Array.isArray(roots) ? [...roots] : null;
13
+ if (!useRoots || useRoots.length === 0) {
14
+ useRoots = Object.keys(nodes).filter((id) => !nodes[id]?.previous);
15
+ }
16
+ if (useRoots.length === 0) {
17
+ const first = Object.keys(nodes)[0];
18
+ if (first) useRoots = [first];
19
+ }
20
+
21
+ for (const r of useRoots) {
22
+ if (!nodes[r]) throw new Error(`Root id "${r}" not found in data.nodes.`);
23
+ }
24
+
25
+ return { nodesById: nodes, roots: useRoots };
26
+ }
27
+
28
+ export function buildTreeFromLinked(rootId, nodesById) {
29
+ if (!rootId || !nodesById?.[rootId]) return null;
30
+ const seen = new Set();
31
+
32
+ const cloneNode = (node) => {
33
+ if (!node) return null;
34
+ const { next, previous, children, ...rest } = node;
35
+ return { ...rest, id: node.id, previous, next, children: [] };
36
+ };
37
+
38
+ const dfs = (id) => {
39
+ if (!id || seen.has(id) || !nodesById[id]) return null;
40
+ seen.add(id);
41
+
42
+ const node = nodesById[id];
43
+ const out = cloneNode(node);
44
+
45
+ const nextArr = Array.isArray(node.next)
46
+ ? node.next
47
+ : node.next != null
48
+ ? [node.next]
49
+ : [];
50
+
51
+ for (const nxt of nextArr) {
52
+ const nextId = typeof nxt === "string" ? nxt : nxt?.id;
53
+ if (!nextId || !nodesById[nextId]) continue;
54
+
55
+ const target = nodesById[nextId];
56
+ if (target.previous == null || target.previous === id) {
57
+ const built = dfs(nextId);
58
+ if (built) out.children.push(built);
59
+ }
60
+ }
61
+ return out;
62
+ };
63
+
64
+ return dfs(rootId);
65
+ }
66
+
67
+ export const getContentParts = (n) => {
68
+ const entries = Object.entries(n).filter(
69
+ ([key]) =>
70
+ key !== "children" && key !== "id" && key !== "previous" && key !== "next"
71
+ );
72
+
73
+ if (entries.length === 0) {
74
+ return {
75
+ title: "(empty)",
76
+ subtitle: null,
77
+ metaEntries: [],
78
+ };
79
+ }
80
+
81
+ const preferredTitleKeys = ["label", "title", "name"];
82
+ const titleEntry =
83
+ entries.find(([key]) => preferredTitleKeys.includes(key)) || entries[0];
84
+ const [titleKey, rawTitle] = titleEntry;
85
+ const title = String(rawTitle);
86
+ let remaining = entries.filter(([key]) => key !== titleKey);
87
+
88
+ const preferredSubtitleKeys = ["description", "role", "type", "status"];
89
+ const subtitleEntry =
90
+ remaining.find(([key]) => preferredSubtitleKeys.includes(key)) || null;
91
+
92
+ let subtitle = null;
93
+ if (subtitleEntry) {
94
+ const [subtitleKey, raw] = subtitleEntry;
95
+ subtitle = String(raw);
96
+ remaining = remaining.filter(([key]) => key !== subtitleKey);
97
+ }
98
+
99
+ const metaEntries = remaining
100
+ .filter(([, value]) => {
101
+ const t = typeof value;
102
+ return (
103
+ (t === "string" || t === "number" || t === "boolean") &&
104
+ value !== "" &&
105
+ value !== null
106
+ );
107
+ })
108
+ .map(([k, v]) => [k, v]);
109
+
110
+ return { title, subtitle, metaEntries };
111
+ };
112
+
113
+ export const toNextArray = (next) => {
114
+ if (!next) return [];
115
+ return Array.isArray(next) ? next : [next];
116
+ };
117
+
118
+ export const setNextProperty = (node, nextIds) => {
119
+ if (!nextIds || nextIds.length === 0) {
120
+ delete node.next;
121
+ } else if (nextIds.length === 1) {
122
+ node.next = nextIds[0];
123
+ } else {
124
+ node.next = nextIds;
125
+ }
126
+ };
127
+
128
+ export const addToNext = (node, childIds) => {
129
+ const current = toNextArray(node.next);
130
+ const newIds = childIds.filter((id) => !current.includes(id));
131
+ setNextProperty(node, [...current, ...newIds]);
132
+ };
133
+
134
+ export const removeFromNext = (node, childIds) => {
135
+ const removeSet = new Set(childIds);
136
+ const filtered = toNextArray(node.next).filter((id) => !removeSet.has(id));
137
+ setNextProperty(node, filtered);
138
+ };
139
+
140
+ export const cleanupReferences = (nodes, removedIds) => {
141
+ const removeSet = new Set(removedIds);
142
+ Object.values(nodes).forEach((node) => {
143
+ if (removeSet.has(node.next)) {
144
+ delete node.next;
145
+ } else if (Array.isArray(node.next)) {
146
+ const filtered = node.next.filter((n) => !removeSet.has(n));
147
+ setNextProperty(node, filtered);
148
+ }
149
+ if (removeSet.has(node.previous)) {
150
+ delete node.previous;
151
+ }
152
+ });
153
+ };
154
+
155
+ export const hexToRgba = (hex, alpha) => {
156
+ const r = parseInt(hex.slice(1, 3), 16);
157
+ const g = parseInt(hex.slice(3, 5), 16);
158
+ const b = parseInt(hex.slice(5, 7), 16);
159
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
160
+ };
161
+
162
+ export const buildDetachedTree = (rootId, nodesById) => {
163
+ if (!rootId || !nodesById?.[rootId]) return null;
164
+ const seen = new Set();
165
+
166
+ const buildNode = (id) => {
167
+ if (!id || seen.has(id) || !nodesById[id]) return null;
168
+ seen.add(id);
169
+
170
+ const node = nodesById[id];
171
+ const { next, previous, ...rest } = node;
172
+ const result = { ...rest, id, children: [] };
173
+
174
+ const nextIds = Array.isArray(next) ? next : next != null ? [next] : [];
175
+
176
+ nextIds.forEach((nxt) => {
177
+ const nextId = typeof nxt === "string" ? nxt : nxt?.id;
178
+ if (!nextId || !nodesById[nextId]) return;
179
+
180
+ const child = buildNode(nextId);
181
+ if (child) result.children.push(child);
182
+ });
183
+
184
+ return result;
185
+ };
186
+
187
+ return buildNode(rootId);
188
+ };
189
+
190
+ export const getSelectedInStructure = (structure, selectedIds) =>
191
+ selectedIds.filter((id) => structure.nodes?.[id]);
192
+
193
+ export const getRootsToConnect = (structure, selectedIds) => {
194
+ const selectedSet = new Set(selectedIds);
195
+ const roots = [];
196
+
197
+ selectedIds.forEach((id) => {
198
+ let current = structure.nodes[id];
199
+ if (!current) return;
200
+
201
+ let rootId = id;
202
+
203
+ while (current.previous && structure.nodes[current.previous]) {
204
+ if (selectedSet.has(current.previous)) {
205
+ rootId = current.previous;
206
+ current = structure.nodes[current.previous];
207
+ } else break;
208
+ }
209
+
210
+ if (!current.previous && !roots.includes(rootId)) {
211
+ roots.push(rootId);
212
+ }
213
+ });
214
+
215
+ if (roots.length > 0) return roots;
216
+
217
+ return selectedIds.filter((id) => {
218
+ const node = structure.nodes[id];
219
+ return node && (!node.previous || !structure.nodes[node.previous]);
220
+ });
221
+ };
222
+
223
+ export const collectSubtree = (structure, roots, selectedIds) => {
224
+ const selectedSet = new Set(selectedIds);
225
+ const collected = new Set();
226
+
227
+ const dfs = (id) => {
228
+ if (collected.has(id) || !structure.nodes[id]) return;
229
+ collected.add(id);
230
+
231
+ toNextArray(structure.nodes[id].next).forEach((childId) => {
232
+ if (structure.nodes[childId] && selectedSet.has(childId)) {
233
+ dfs(childId);
234
+ }
235
+ });
236
+ };
237
+
238
+ roots.forEach(dfs);
239
+ return collected;
240
+ };
241
+
242
+ export const splitFloatingStructure = (structure, removedIds) => {
243
+ const remainingNodes = {};
244
+
245
+ Object.entries(structure.nodes).forEach(([id, node]) => {
246
+ if (!removedIds.has(id)) {
247
+ remainingNodes[id] = node;
248
+ }
249
+ });
250
+
251
+ cleanupReferences(remainingNodes, [...removedIds]);
252
+
253
+ const remainingRoots =
254
+ structure.roots?.filter((r) => !removedIds.has(r)) ?? [];
255
+
256
+ const roots =
257
+ remainingRoots.length > 0
258
+ ? remainingRoots
259
+ : Object.keys(remainingNodes).filter(
260
+ (id) => !remainingNodes[id].previous
261
+ );
262
+
263
+ return {
264
+ ...structure,
265
+ nodes: remainingNodes,
266
+ roots: [...new Set(roots)],
267
+ };
268
+ };
package/src/lib/index.js CHANGED
@@ -22,7 +22,7 @@ export { default as TableSelectedAction } from "./TableSelectedAction/TableSelec
22
22
  export { default as useTable } from "./useTable/useTable";
23
23
  export { default as useChart } from "./useChart/useChart";
24
24
 
25
- export { default as Flow } from "./Flow/Flow";
25
+ export { default as Flow } from "./Flow/core/Flow";
26
26
 
27
27
  export {
28
28
  HeaderCard,
@@ -1,128 +0,0 @@
1
- import { Box } from "@mui/material";
2
- import { useSelection } from "./SelectionContext";
3
-
4
- import React, { useEffect, useRef, useState } from "react";
5
-
6
- const DraggableNode = ({
7
- children,
8
- registerRef,
9
- onDrag,
10
- nodeId,
11
- selectionColor = "#64748b",
12
- }) => {
13
- const [offset, setOffset] = useState({ x: 0, y: 0 });
14
- const localRef = useRef(null);
15
- const lastDeltaRef = useRef({ x: 0, y: 0 });
16
-
17
- const {
18
- isSelected,
19
- selectNode,
20
- toggleSelection,
21
- clearSelection,
22
- registerNodeHandlers,
23
- moveSelectedNodes,
24
- selectedIds,
25
- } = useSelection();
26
-
27
- const selected = isSelected(nodeId);
28
- const onDragRef = useRef(onDrag);
29
-
30
- useEffect(() => {
31
- onDragRef.current = onDrag;
32
- }, [onDrag]);
33
-
34
- useEffect(() => {
35
- if (nodeId) {
36
- return registerNodeHandlers(nodeId, {
37
- setOffset,
38
- onDrag: () => onDragRef.current?.(),
39
- });
40
- }
41
- }, [nodeId, registerNodeHandlers]);
42
-
43
- const setRef = (el) => {
44
- localRef.current = el;
45
- if (registerRef) registerRef(el);
46
- };
47
-
48
- const handleMouseDown = (e) => {
49
- if (e.button !== 0) return;
50
- e.stopPropagation();
51
-
52
- if (e.shiftKey || e.ctrlKey || e.metaKey) {
53
- toggleSelection(nodeId);
54
- return;
55
- }
56
- if (!selected) {
57
- clearSelection();
58
- selectNode(nodeId);
59
- }
60
-
61
- const startX = e.clientX;
62
- const startY = e.clientY;
63
- const startOffset = { ...offset };
64
- lastDeltaRef.current = { x: 0, y: 0 };
65
-
66
- const onMove = (ev) => {
67
- const dx = ev.clientX - startX;
68
- const dy = ev.clientY - startY;
69
-
70
- const deltaDx = dx - lastDeltaRef.current.x;
71
- const deltaDy = dy - lastDeltaRef.current.y;
72
- lastDeltaRef.current = { x: dx, y: dy };
73
-
74
- setOffset({
75
- x: startOffset.x + dx,
76
- y: startOffset.y + dy,
77
- });
78
-
79
- if (selectedIds.size > 1) {
80
- moveSelectedNodes(deltaDx, deltaDy, nodeId);
81
- }
82
-
83
- if (onDrag) onDrag();
84
- };
85
-
86
- const onUp = () => {
87
- window.removeEventListener("mousemove", onMove);
88
- window.removeEventListener("mouseup", onUp);
89
- };
90
-
91
- window.addEventListener("mousemove", onMove);
92
- window.addEventListener("mouseup", onUp);
93
- };
94
-
95
- return (
96
- <Box
97
- ref={setRef}
98
- data-node-id={nodeId}
99
- onMouseDown={handleMouseDown}
100
- sx={{
101
- display: "inline-flex",
102
- flexDirection: "column",
103
- alignItems: "center",
104
- position: "relative",
105
- transform: `translate(${offset.x}px, ${offset.y}px)`,
106
- cursor: "grab",
107
- "&:active": {
108
- cursor: "grabbing",
109
- },
110
- ...(selected && {
111
- "&::after": {
112
- content: '""',
113
- position: "absolute",
114
- inset: -6,
115
- border: `2px solid ${selectionColor}`,
116
- borderRadius: "12px",
117
- pointerEvents: "none",
118
- boxShadow: `0 0 8px ${selectionColor}66`,
119
- },
120
- }),
121
- }}
122
- >
123
- {children}
124
- </Box>
125
- );
126
- };
127
-
128
- export default DraggableNode;
@@ -1,40 +0,0 @@
1
- import FlowNode from "./FlowNode";
2
-
3
- import React, { useMemo } from "react";
4
- import { assertLinkedGraph, buildTreeFromLinked } from "./flowUtils";
5
-
6
- export const Flow = ({ data, variant = "simple", style, plugin }) => {
7
- const { nodesById, roots } = useMemo(() => assertLinkedGraph(data), [data]);
8
-
9
- const treeData = useMemo(() => {
10
- if (!roots?.length)
11
- return { id: "__empty__", label: "(empty)", children: [] };
12
-
13
- if (roots.length === 1) {
14
- return (
15
- buildTreeFromLinked(roots[0], nodesById) || {
16
- id: roots[0],
17
- children: [],
18
- }
19
- );
20
- }
21
-
22
- const children = roots
23
- .map((r) => buildTreeFromLinked(r, nodesById))
24
- .filter(Boolean);
25
-
26
- return { id: "__root__", label: "Root", children };
27
- }, [nodesById, roots]);
28
-
29
- return (
30
- <FlowNode
31
- node={treeData}
32
- variant={variant}
33
- style={style}
34
- plugin={plugin}
35
- isRoot={true}
36
- />
37
- );
38
- };
39
-
40
- export default Flow;