@transitrix/diagrams 1.4.0 → 1.6.0
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.
- package/dist/activities/__tests__/gantt.test.js +39 -0
- package/dist/capability-map/CapabilityCardNode.d.ts +19 -0
- package/dist/capability-map/CapabilityCardNode.js +66 -0
- package/dist/capability-map/CapabilityMapView.d.ts +59 -0
- package/dist/capability-map/CapabilityMapView.js +204 -0
- package/dist/capability-map/CapabilityRootNode.d.ts +12 -0
- package/dist/capability-map/CapabilityRootNode.js +23 -0
- package/dist/capability-map/__tests__/CapabilityMapView.test.d.ts +1 -0
- package/dist/capability-map/__tests__/CapabilityMapView.test.js +217 -0
- package/dist/capability-map/__tests__/address.test.d.ts +1 -0
- package/dist/capability-map/__tests__/address.test.js +49 -0
- package/dist/capability-map/__tests__/dsm-layout.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-layout.test.js +46 -0
- package/dist/capability-map/__tests__/dsm-mutations.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-mutations.test.js +125 -0
- package/dist/capability-map/__tests__/dsm-validate.test.d.ts +1 -0
- package/dist/capability-map/__tests__/dsm-validate.test.js +56 -0
- package/dist/capability-map/address.d.ts +12 -0
- package/dist/capability-map/address.js +48 -0
- package/dist/capability-map/dsm-layout.d.ts +9 -0
- package/dist/capability-map/dsm-layout.js +125 -0
- package/dist/capability-map/dsm-mutations.d.ts +28 -0
- package/dist/capability-map/dsm-mutations.js +196 -0
- package/dist/capability-map/dsm-schema.d.ts +71 -0
- package/dist/capability-map/dsm-schema.js +14 -0
- package/dist/capability-map/dsm-theme.d.ts +20 -0
- package/dist/capability-map/dsm-theme.js +36 -0
- package/dist/capability-map/dsm-validate.d.ts +2 -0
- package/dist/capability-map/dsm-validate.js +93 -0
- package/dist/capability-map/index.d.ts +9 -0
- package/dist/capability-map/index.js +6 -0
- package/dist/compliance/__tests__/trace.test.d.ts +1 -0
- package/dist/compliance/__tests__/trace.test.js +144 -0
- package/dist/compliance/classify.js +7 -1
- package/dist/compliance/index.d.ts +2 -1
- package/dist/compliance/index.js +1 -0
- package/dist/compliance/reverse-index.js +4 -1
- package/dist/compliance/trace.d.ts +26 -0
- package/dist/compliance/trace.js +89 -0
- package/dist/compliance/types.d.ts +94 -1
- package/dist/goals/GoalNode.d.ts +18 -0
- package/dist/goals/GoalNode.js +91 -0
- package/dist/goals/GoalTreeView.d.ts +57 -0
- package/dist/goals/GoalTreeView.js +228 -0
- package/dist/goals/__tests__/GoalTreeView.test.d.ts +1 -0
- package/dist/goals/__tests__/GoalTreeView.test.js +296 -0
- package/dist/goals/index.d.ts +3 -0
- package/dist/goals/index.js +1 -0
- package/dist/goals/theme.d.ts +22 -0
- package/dist/goals/theme.js +32 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/webview/__tests__/render-blocks.test.js +48 -0
- package/dist/webview/notation-style.d.ts +11 -0
- package/dist/webview/notation-style.js +11 -0
- package/dist/webview/render-blocks.js +8 -4
- package/dist/webview/render-goals.js +2 -1
- package/package.json +4 -1
- package/src/activities/__tests__/gantt.test.ts +45 -0
- package/src/capability-map/CapabilityCardNode.tsx +146 -0
- package/src/capability-map/CapabilityMapView.tsx +331 -0
- package/src/capability-map/CapabilityRootNode.tsx +38 -0
- package/src/capability-map/__tests__/CapabilityMapView.test.tsx +232 -0
- package/src/capability-map/__tests__/address.test.ts +59 -0
- package/src/capability-map/__tests__/dsm-layout.test.ts +52 -0
- package/src/capability-map/__tests__/dsm-mutations.test.ts +141 -0
- package/src/capability-map/__tests__/dsm-validate.test.ts +65 -0
- package/src/capability-map/address.ts +48 -0
- package/src/capability-map/dsm-layout.ts +142 -0
- package/src/capability-map/dsm-mutations.ts +208 -0
- package/src/capability-map/dsm-schema.ts +80 -0
- package/src/capability-map/dsm-theme.ts +53 -0
- package/src/capability-map/dsm-validate.ts +103 -0
- package/src/capability-map/index.ts +35 -0
- package/src/compliance/__tests__/trace.test.ts +157 -0
- package/src/compliance/classify.ts +7 -1
- package/src/compliance/index.ts +6 -0
- package/src/compliance/reverse-index.ts +3 -1
- package/src/compliance/trace.ts +118 -0
- package/src/compliance/types.ts +101 -1
- package/src/goals/GoalNode.tsx +212 -0
- package/src/goals/GoalTreeView.tsx +350 -0
- package/src/goals/__tests__/GoalTreeView.test.tsx +313 -0
- package/src/goals/index.ts +4 -0
- package/src/goals/theme.ts +54 -0
- package/src/index.ts +29 -1
- package/src/webview/__tests__/render-blocks.test.ts +54 -0
- package/src/webview/notation-style.ts +12 -0
- package/src/webview/render-blocks.ts +8 -4
- package/src/webview/render-goals.ts +2 -1
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState, useEffect } from 'react';
|
|
2
|
+
import ReactFlow, {
|
|
3
|
+
Controls,
|
|
4
|
+
MiniMap,
|
|
5
|
+
Background,
|
|
6
|
+
ConnectionLineType,
|
|
7
|
+
useNodesState,
|
|
8
|
+
useEdgesState,
|
|
9
|
+
type Node,
|
|
10
|
+
type Edge,
|
|
11
|
+
type NodeMouseHandler,
|
|
12
|
+
type NodeDragHandler,
|
|
13
|
+
} from 'reactflow';
|
|
14
|
+
import 'reactflow/dist/style.css';
|
|
15
|
+
import type { Goal, GoalTree, GoalTreeLayout, LayoutOptions } from './types.js';
|
|
16
|
+
import { layoutGoalTree } from './layout.js';
|
|
17
|
+
import { reparent, addChild, deleteWithDescendants, moveToBacklog, restoreFromBacklog } from './mutations.js';
|
|
18
|
+
import type { ThemeTokens } from './theme.js';
|
|
19
|
+
import { resolveTheme } from './theme.js';
|
|
20
|
+
import GoalNode, { type GoalNodeData } from './GoalNode.js';
|
|
21
|
+
|
|
22
|
+
export type { ThemeTokens } from './theme.js';
|
|
23
|
+
|
|
24
|
+
export interface GoalTreeViewProps {
|
|
25
|
+
tree: GoalTree;
|
|
26
|
+
layout?: GoalTreeLayout;
|
|
27
|
+
layoutOptions?: LayoutOptions;
|
|
28
|
+
theme?: ThemeTokens;
|
|
29
|
+
readOnly?: boolean;
|
|
30
|
+
showBacklog?: boolean;
|
|
31
|
+
showMiniMap?: boolean;
|
|
32
|
+
onChange?: (event: GoalTreeChange) => void;
|
|
33
|
+
onEditRequest?: (goal: Goal) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type GoalTreeChange =
|
|
37
|
+
| { kind: 'reparent'; sourceId: number; targetId: number; result: GoalTree }
|
|
38
|
+
| { kind: 'addChild'; parentId: number; newGoal: Goal; result: GoalTree }
|
|
39
|
+
| { kind: 'delete'; id: number; result: GoalTree }
|
|
40
|
+
| { kind: 'moveToBacklog'; id: number; result: GoalTree }
|
|
41
|
+
| { kind: 'restoreFromBacklog'; id: number; newParentId: number; result: GoalTree };
|
|
42
|
+
|
|
43
|
+
const BACKLOG_DRAG_MIME = 'application/x-transitrix-goal-id';
|
|
44
|
+
const nodeTypes = { goalNode: GoalNode };
|
|
45
|
+
|
|
46
|
+
/** Where a card was dropped, as classified from the pointer position at drag-stop. */
|
|
47
|
+
export type DropTarget = { kind: 'node'; id: number } | { kind: 'backlog' } | null;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Pure decision: given where a dragged card was dropped, compute the mutation
|
|
51
|
+
* and the GoalTreeChange to report (or null for a no-op drop). Kept separate
|
|
52
|
+
* from the DOM/pointer-event glue in onNodeDragStop so the mutation-selection
|
|
53
|
+
* logic — which card wins reparent vs moveToBacklog vs a refused mutation —
|
|
54
|
+
* is unit-testable without needing a real drag gesture through reactflow's
|
|
55
|
+
* internal, DOM-measurement-gated dragging.
|
|
56
|
+
*/
|
|
57
|
+
export function computeDragOutcome(tree: GoalTree, sourceId: number, dropTarget: DropTarget): GoalTreeChange | null {
|
|
58
|
+
if (!dropTarget) return null;
|
|
59
|
+
if (dropTarget.kind === 'backlog') {
|
|
60
|
+
const mutation = moveToBacklog(tree, sourceId);
|
|
61
|
+
if (!mutation.ok || !mutation.result) return null;
|
|
62
|
+
return { kind: 'moveToBacklog', id: sourceId, result: mutation.result };
|
|
63
|
+
}
|
|
64
|
+
if (dropTarget.id === sourceId) return null;
|
|
65
|
+
const mutation = reparent(tree, sourceId, dropTarget.id);
|
|
66
|
+
if (!mutation.ok || !mutation.result) return null;
|
|
67
|
+
return { kind: 'reparent', sourceId, targetId: dropTarget.id, result: mutation.result };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Goals reachable from a root (level 0 or parent_id 0) via valid parent_id chains. */
|
|
71
|
+
function partitionCanvasAndBacklog(goals: Goal[]): { canvas: Goal[]; backlog: Goal[] } {
|
|
72
|
+
const children = new Map<number, Goal[]>();
|
|
73
|
+
for (const g of goals) {
|
|
74
|
+
if (!children.has(g.parent_id)) children.set(g.parent_id, []);
|
|
75
|
+
children.get(g.parent_id)!.push(g);
|
|
76
|
+
}
|
|
77
|
+
const roots = goals.filter((g) => g.level === 0 || g.parent_id === 0);
|
|
78
|
+
const reachable = new Set<number>();
|
|
79
|
+
const stack = [...roots.map((r) => r.id)];
|
|
80
|
+
while (stack.length > 0) {
|
|
81
|
+
const id = stack.pop()!;
|
|
82
|
+
if (reachable.has(id)) continue;
|
|
83
|
+
reachable.add(id);
|
|
84
|
+
for (const child of children.get(id) ?? []) stack.push(child.id);
|
|
85
|
+
}
|
|
86
|
+
const canvas: Goal[] = [];
|
|
87
|
+
const backlog: Goal[] = [];
|
|
88
|
+
for (const g of goals) {
|
|
89
|
+
if (reachable.has(g.id)) canvas.push(g);
|
|
90
|
+
else backlog.push(g); // unreachable from any root — broken/orphaned parent chain
|
|
91
|
+
}
|
|
92
|
+
return { canvas, backlog };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function GoalTreeViewInner({
|
|
96
|
+
tree,
|
|
97
|
+
layout: layoutProp,
|
|
98
|
+
layoutOptions,
|
|
99
|
+
theme: themeProp,
|
|
100
|
+
readOnly = false,
|
|
101
|
+
showBacklog = false,
|
|
102
|
+
showMiniMap = false,
|
|
103
|
+
onChange,
|
|
104
|
+
onEditRequest,
|
|
105
|
+
}: GoalTreeViewProps): React.ReactElement {
|
|
106
|
+
const theme = useMemo(() => resolveTheme(themeProp), [themeProp]);
|
|
107
|
+
const [collapsedIds, setCollapsedIds] = useState<Set<number>>(new Set());
|
|
108
|
+
const [hoveredId, setHoveredId] = useState<number | null>(null);
|
|
109
|
+
|
|
110
|
+
const { canvas: canvasGoals, backlog: backlogGoals } = useMemo(
|
|
111
|
+
() => partitionCanvasAndBacklog(tree.goals),
|
|
112
|
+
[tree.goals],
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const layout = useMemo<GoalTreeLayout>(() => {
|
|
116
|
+
if (layoutProp) return layoutProp;
|
|
117
|
+
const hideCollapsed = [...(layoutOptions?.hideCollapsed ?? []), ...collapsedIds];
|
|
118
|
+
return layoutGoalTree(
|
|
119
|
+
{ goal_types: tree.goal_types, goals: canvasGoals },
|
|
120
|
+
{ ...layoutOptions, hideCollapsed },
|
|
121
|
+
);
|
|
122
|
+
}, [layoutProp, layoutOptions, collapsedIds, tree.goal_types, canvasGoals]);
|
|
123
|
+
|
|
124
|
+
const onAddChild = useCallback(
|
|
125
|
+
(parentId: number, parentLevel: number) => {
|
|
126
|
+
if (readOnly) return;
|
|
127
|
+
const before = new Set(tree.goals.map((g) => g.id));
|
|
128
|
+
const mutation = addChild(tree, parentId, { name: 'New goal', type: '', level: parentLevel + 1, parent_id: parentId });
|
|
129
|
+
if (!mutation.ok || !mutation.result) return;
|
|
130
|
+
const newGoal = mutation.result.goals.find((g) => !before.has(g.id));
|
|
131
|
+
if (!newGoal) return;
|
|
132
|
+
onChange?.({ kind: 'addChild', parentId, newGoal, result: mutation.result });
|
|
133
|
+
},
|
|
134
|
+
[tree, readOnly, onChange],
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const onDeleteNode = useCallback(
|
|
138
|
+
(id: number) => {
|
|
139
|
+
if (readOnly) return;
|
|
140
|
+
const mutation = deleteWithDescendants(tree, id);
|
|
141
|
+
if (!mutation.ok || !mutation.result) return;
|
|
142
|
+
onChange?.({ kind: 'delete', id, result: mutation.result });
|
|
143
|
+
},
|
|
144
|
+
[tree, readOnly, onChange],
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
const onRestoreFromBacklog = useCallback(
|
|
148
|
+
(id: number, newParentId: number) => {
|
|
149
|
+
if (readOnly) return;
|
|
150
|
+
const mutation = restoreFromBacklog(tree, id, newParentId);
|
|
151
|
+
if (!mutation.ok || !mutation.result) return;
|
|
152
|
+
onChange?.({ kind: 'restoreFromBacklog', id, newParentId, result: mutation.result });
|
|
153
|
+
},
|
|
154
|
+
[tree, readOnly, onChange],
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const onToggleCollapse = useCallback((id: number) => {
|
|
158
|
+
setCollapsedIds((prev) => {
|
|
159
|
+
const next = new Set(prev);
|
|
160
|
+
if (next.has(id)) next.delete(id);
|
|
161
|
+
else next.add(id);
|
|
162
|
+
return next;
|
|
163
|
+
});
|
|
164
|
+
}, []);
|
|
165
|
+
|
|
166
|
+
const buildNodes = useCallback((): Node<GoalNodeData>[] => {
|
|
167
|
+
return layout.nodes.map((n) => ({
|
|
168
|
+
id: String(n.id),
|
|
169
|
+
type: 'goalNode',
|
|
170
|
+
position: { x: n.x, y: n.y },
|
|
171
|
+
draggable: !readOnly,
|
|
172
|
+
data: {
|
|
173
|
+
goal: n.data,
|
|
174
|
+
theme,
|
|
175
|
+
readOnly,
|
|
176
|
+
isDropTarget: hoveredId === n.id,
|
|
177
|
+
hasHiddenChildren: n.hasHiddenChildren,
|
|
178
|
+
isCollapsed: collapsedIds.has(n.id),
|
|
179
|
+
onAddChild,
|
|
180
|
+
onDelete: onDeleteNode,
|
|
181
|
+
onToggleCollapse,
|
|
182
|
+
},
|
|
183
|
+
}));
|
|
184
|
+
}, [layout.nodes, theme, readOnly, hoveredId, collapsedIds, onAddChild, onDeleteNode, onToggleCollapse]);
|
|
185
|
+
|
|
186
|
+
const buildEdges = useCallback((): Edge[] => {
|
|
187
|
+
return layout.edges.map((e) => ({
|
|
188
|
+
id: `e${e.source}-${e.target}`,
|
|
189
|
+
source: String(e.source),
|
|
190
|
+
target: String(e.target),
|
|
191
|
+
type: 'default',
|
|
192
|
+
style: { stroke: theme.edgeColor, strokeWidth: theme.edgeWidth },
|
|
193
|
+
}));
|
|
194
|
+
}, [layout.edges, theme]);
|
|
195
|
+
|
|
196
|
+
const [nodes, setNodes, onNodesChange] = useNodesState<GoalNodeData>(buildNodes());
|
|
197
|
+
const [edges, setEdges, onEdgesChange] = useEdgesState(buildEdges());
|
|
198
|
+
|
|
199
|
+
// Re-sync whenever the underlying layout/theme/interaction state changes —
|
|
200
|
+
// tree/layout are host-controlled props, not owned by this component.
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
setNodes(buildNodes());
|
|
203
|
+
setEdges(buildEdges());
|
|
204
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
205
|
+
}, [layout, theme, readOnly, hoveredId, collapsedIds]);
|
|
206
|
+
|
|
207
|
+
const checkIntersection = useCallback(
|
|
208
|
+
(a: Node<GoalNodeData>, b: Node<GoalNodeData>): boolean => {
|
|
209
|
+
const midX = a.position.x + 125;
|
|
210
|
+
const midY = a.position.y + 40;
|
|
211
|
+
return midX > b.position.x && midX < b.position.x + 250 && midY > b.position.y && midY < b.position.y + 80;
|
|
212
|
+
},
|
|
213
|
+
[],
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const onNodeDrag: NodeDragHandler = useCallback(
|
|
217
|
+
(_event, node) => {
|
|
218
|
+
if (readOnly) return;
|
|
219
|
+
const hit = nodes.find((n) => n.id !== node.id && checkIntersection(node as Node<GoalNodeData>, n));
|
|
220
|
+
setHoveredId(hit ? Number(hit.id) : null);
|
|
221
|
+
},
|
|
222
|
+
[readOnly, nodes, checkIntersection],
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const onNodeDragStop: NodeDragHandler = useCallback(
|
|
226
|
+
(event, node) => {
|
|
227
|
+
if (readOnly) {
|
|
228
|
+
setHoveredId(null);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const sourceId = Number(node.id);
|
|
232
|
+
const mouseEvent = event as unknown as MouseEvent;
|
|
233
|
+
const dropEl = typeof document !== 'undefined' && 'clientX' in mouseEvent
|
|
234
|
+
? document.elementFromPoint(mouseEvent.clientX, mouseEvent.clientY)
|
|
235
|
+
: null;
|
|
236
|
+
const dropTarget: DropTarget = dropEl?.closest('[data-goaltree-backlog-zone]')
|
|
237
|
+
? { kind: 'backlog' }
|
|
238
|
+
: hoveredId != null ? { kind: 'node', id: hoveredId } : null;
|
|
239
|
+
const change = computeDragOutcome(tree, sourceId, dropTarget);
|
|
240
|
+
if (change) onChange?.(change);
|
|
241
|
+
setHoveredId(null);
|
|
242
|
+
},
|
|
243
|
+
[readOnly, hoveredId, tree, onChange],
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const onNodeDoubleClick: NodeMouseHandler = useCallback(
|
|
247
|
+
(_event, node) => {
|
|
248
|
+
const goal = tree.goals.find((g) => g.id === Number(node.id));
|
|
249
|
+
if (goal) onEditRequest?.(goal);
|
|
250
|
+
},
|
|
251
|
+
[tree.goals, onEditRequest],
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const onBacklogItemDragStart = useCallback((event: React.DragEvent, goalId: number) => {
|
|
255
|
+
event.dataTransfer.setData(BACKLOG_DRAG_MIME, String(goalId));
|
|
256
|
+
event.dataTransfer.effectAllowed = 'move';
|
|
257
|
+
}, []);
|
|
258
|
+
|
|
259
|
+
const onCanvasDragOver = useCallback(
|
|
260
|
+
(event: React.DragEvent) => {
|
|
261
|
+
if (readOnly) return;
|
|
262
|
+
event.preventDefault();
|
|
263
|
+
},
|
|
264
|
+
[readOnly],
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const onCanvasDrop = useCallback(
|
|
268
|
+
(event: React.DragEvent) => {
|
|
269
|
+
if (readOnly) return;
|
|
270
|
+
event.preventDefault();
|
|
271
|
+
const idStr = event.dataTransfer.getData(BACKLOG_DRAG_MIME);
|
|
272
|
+
if (!idStr) return;
|
|
273
|
+
const goalId = Number(idStr);
|
|
274
|
+
const targetEl = document.elementFromPoint(event.clientX, event.clientY);
|
|
275
|
+
const nodeEl = targetEl?.closest('.react-flow__node');
|
|
276
|
+
const targetIdAttr = nodeEl?.getAttribute('data-id');
|
|
277
|
+
if (targetIdAttr == null) return;
|
|
278
|
+
onRestoreFromBacklog(goalId, Number(targetIdAttr));
|
|
279
|
+
},
|
|
280
|
+
[readOnly, onRestoreFromBacklog],
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
const showBacklogPanel = showBacklog && backlogGoals.length > 0;
|
|
284
|
+
|
|
285
|
+
return (
|
|
286
|
+
<div style={{ display: 'flex', width: '100%', height: '100%' }}>
|
|
287
|
+
{showBacklogPanel && (
|
|
288
|
+
<div
|
|
289
|
+
data-goaltree-backlog-zone
|
|
290
|
+
style={{
|
|
291
|
+
width: 220,
|
|
292
|
+
flexShrink: 0,
|
|
293
|
+
borderRight: '1px solid #e2e8f0',
|
|
294
|
+
padding: 10,
|
|
295
|
+
overflowY: 'auto',
|
|
296
|
+
background: '#f8fafc',
|
|
297
|
+
}}
|
|
298
|
+
>
|
|
299
|
+
<div style={{ fontWeight: 700, fontSize: 12, color: '#334155', marginBottom: 8 }}>Backlog</div>
|
|
300
|
+
{backlogGoals.map((goal) => (
|
|
301
|
+
<div
|
|
302
|
+
key={goal.id}
|
|
303
|
+
draggable={!readOnly}
|
|
304
|
+
onDragStart={(e) => onBacklogItemDragStart(e, goal.id)}
|
|
305
|
+
onDoubleClick={() => onEditRequest?.(goal)}
|
|
306
|
+
title="Drag onto a canvas card to set its parent"
|
|
307
|
+
style={{
|
|
308
|
+
padding: 8,
|
|
309
|
+
marginBottom: 6,
|
|
310
|
+
background: '#ffffff',
|
|
311
|
+
border: '1px solid #cbd5e1',
|
|
312
|
+
borderRadius: 6,
|
|
313
|
+
fontSize: 11,
|
|
314
|
+
cursor: readOnly ? 'default' : 'grab',
|
|
315
|
+
}}
|
|
316
|
+
>
|
|
317
|
+
<div style={{ fontWeight: 600 }}>{goal.name}</div>
|
|
318
|
+
<div style={{ color: '#64748b' }}>Level: {goal.level}</div>
|
|
319
|
+
</div>
|
|
320
|
+
))}
|
|
321
|
+
</div>
|
|
322
|
+
)}
|
|
323
|
+
|
|
324
|
+
<div style={{ flex: 1, position: 'relative' }} onDragOver={onCanvasDragOver} onDrop={onCanvasDrop}>
|
|
325
|
+
<ReactFlow
|
|
326
|
+
nodes={nodes}
|
|
327
|
+
edges={edges}
|
|
328
|
+
onNodesChange={readOnly ? undefined : onNodesChange}
|
|
329
|
+
onEdgesChange={readOnly ? undefined : onEdgesChange}
|
|
330
|
+
nodeTypes={nodeTypes}
|
|
331
|
+
nodesDraggable={!readOnly}
|
|
332
|
+
nodesConnectable={false}
|
|
333
|
+
onNodeDrag={onNodeDrag}
|
|
334
|
+
onNodeDragStop={onNodeDragStop}
|
|
335
|
+
onNodeDoubleClick={onNodeDoubleClick}
|
|
336
|
+
connectionLineType={ConnectionLineType.Bezier}
|
|
337
|
+
fitView
|
|
338
|
+
>
|
|
339
|
+
<Background />
|
|
340
|
+
<Controls />
|
|
341
|
+
{showMiniMap && <MiniMap pannable zoomable />}
|
|
342
|
+
</ReactFlow>
|
|
343
|
+
</div>
|
|
344
|
+
</div>
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function GoalTreeView(props: GoalTreeViewProps): React.ReactElement {
|
|
349
|
+
return <GoalTreeViewInner {...props} />;
|
|
350
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { render, screen, cleanup, waitFor } from '@testing-library/react';
|
|
5
|
+
import { GoalTreeView, computeDragOutcome } from '../GoalTreeView.js';
|
|
6
|
+
import type { GoalTree } from '../types.js';
|
|
7
|
+
import type { GoalTreeChange } from '../GoalTreeView.js';
|
|
8
|
+
|
|
9
|
+
// jsdom has no ResizeObserver / layout engine. reactflow keeps a node's DOM
|
|
10
|
+
// wrapper `visibility: hidden` and excludes it from drag/intersection logic
|
|
11
|
+
// until it has been "measured" via ResizeObserver — so the mock must
|
|
12
|
+
// actually invoke its callback (with the mocked getBoundingClientRect below)
|
|
13
|
+
// for nodes to become interactive, not just satisfy the constructor call.
|
|
14
|
+
class MockResizeObserver {
|
|
15
|
+
#callback: ResizeObserverCallback;
|
|
16
|
+
constructor(callback: ResizeObserverCallback) {
|
|
17
|
+
this.#callback = callback;
|
|
18
|
+
}
|
|
19
|
+
observe(target: Element) {
|
|
20
|
+
queueMicrotask(() => {
|
|
21
|
+
const rect = target.getBoundingClientRect();
|
|
22
|
+
this.#callback(
|
|
23
|
+
[{ target, contentRect: rect } as ResizeObserverEntry],
|
|
24
|
+
this as unknown as ResizeObserver,
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
unobserve() {}
|
|
29
|
+
disconnect() {}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// jsdom also has no DOMMatrixReadOnly, which reactflow's dimension-tracking
|
|
33
|
+
// uses to read the translateX/Y out of a node's computed transform. Only
|
|
34
|
+
// translate() (no scale/rotate) ever appears in this component's own CSS, so
|
|
35
|
+
// a minimal shim covering that one case is enough.
|
|
36
|
+
class MockDOMMatrixReadOnly {
|
|
37
|
+
m41 = 0;
|
|
38
|
+
m42 = 0;
|
|
39
|
+
constructor(transform?: string) {
|
|
40
|
+
if (!transform || transform === 'none') return;
|
|
41
|
+
const t = /translate\((-?[\d.]+)px,\s*(-?[\d.]+)px\)/.exec(transform);
|
|
42
|
+
if (t) {
|
|
43
|
+
this.m41 = Number(t[1]);
|
|
44
|
+
this.m42 = Number(t[2]);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const m = /matrix\(([^)]+)\)/.exec(transform);
|
|
48
|
+
if (m) {
|
|
49
|
+
const parts = m[1].split(',').map((p) => Number(p.trim()));
|
|
50
|
+
this.m41 = parts[4] ?? 0;
|
|
51
|
+
this.m42 = parts[5] ?? 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function makeTree(): GoalTree {
|
|
57
|
+
return {
|
|
58
|
+
goal_types: [
|
|
59
|
+
{ name: 'Strategy', level: 0 },
|
|
60
|
+
{ name: 'Business Goal', level: 1 },
|
|
61
|
+
],
|
|
62
|
+
goals: [
|
|
63
|
+
{ id: 1, name: 'Triple revenue', type: 'Strategy', level: 0, parent_id: 0 },
|
|
64
|
+
{ id: 2, name: 'Launch in EU', type: 'Business Goal', level: 1, parent_id: 1 },
|
|
65
|
+
{
|
|
66
|
+
id: 3,
|
|
67
|
+
name: 'Cut churn',
|
|
68
|
+
type: 'Business Goal',
|
|
69
|
+
level: 1,
|
|
70
|
+
parent_id: 1,
|
|
71
|
+
factors: [
|
|
72
|
+
{ id: 10, name: 'Support backlog', impact_type: 'risk' },
|
|
73
|
+
{ id: 11, name: 'New pricing', impact_type: 'opportunity' },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
beforeEach(() => {
|
|
81
|
+
(global as unknown as { ResizeObserver: unknown }).ResizeObserver = MockResizeObserver;
|
|
82
|
+
(window as unknown as { DOMMatrixReadOnly: unknown }).DOMMatrixReadOnly = MockDOMMatrixReadOnly;
|
|
83
|
+
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockImplementation(function (this: Element) {
|
|
84
|
+
// Individual goal cards report their real 250x80 footprint (matters for
|
|
85
|
+
// reactflow's own drag/measurement bookkeeping); the flow container and
|
|
86
|
+
// everything else gets a generously large rect so viewport fitting has
|
|
87
|
+
// room to work with.
|
|
88
|
+
const isNode = this.classList?.contains('react-flow__node');
|
|
89
|
+
const width = isNode ? 250 : 1000;
|
|
90
|
+
const height = isNode ? 80 : 800;
|
|
91
|
+
return {
|
|
92
|
+
width, height, top: 0, left: 0, right: width, bottom: height, x: 0, y: 0,
|
|
93
|
+
toJSON() { return this; },
|
|
94
|
+
} as DOMRect;
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
afterEach(() => {
|
|
99
|
+
cleanup();
|
|
100
|
+
vi.restoreAllMocks();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('GoalTreeView', () => {
|
|
104
|
+
it('renders without crashing and shows every canvas goal', async () => {
|
|
105
|
+
render(<GoalTreeView tree={makeTree()} />);
|
|
106
|
+
await waitFor(() => {
|
|
107
|
+
expect(screen.getByText('Triple revenue')).toBeTruthy();
|
|
108
|
+
expect(screen.getByText('Launch in EU')).toBeTruthy();
|
|
109
|
+
expect(screen.getByText('Cut churn')).toBeTruthy();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('renders factor indicators from goal.factors', async () => {
|
|
114
|
+
render(<GoalTreeView tree={makeTree()} />);
|
|
115
|
+
await waitFor(() => {
|
|
116
|
+
expect(screen.getByText('Triple revenue')).toBeTruthy();
|
|
117
|
+
});
|
|
118
|
+
// One up-caret (opportunity) and one down-caret (risk) on "Cut churn".
|
|
119
|
+
expect(screen.getByLabelText('1 positive factor')).toBeTruthy();
|
|
120
|
+
expect(screen.getByLabelText('1 negative factor')).toBeTruthy();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('puts a goal with a broken parent_id in the backlog, not on canvas', async () => {
|
|
124
|
+
const tree: GoalTree = {
|
|
125
|
+
goal_types: [
|
|
126
|
+
{ name: 'Strategy', level: 0 },
|
|
127
|
+
{ name: 'Business Goal', level: 1 },
|
|
128
|
+
],
|
|
129
|
+
goals: [
|
|
130
|
+
{ id: 1, name: 'Root goal', type: 'Strategy', level: 0, parent_id: 0 },
|
|
131
|
+
// level 1 + a parent_id that resolves to nothing — not a root by
|
|
132
|
+
// either "Roots: level === 0 || parent_id === 0" clause, and
|
|
133
|
+
// unreachable from goal 1 — must land in the backlog.
|
|
134
|
+
{ id: 2, name: 'Orphaned goal', type: 'Business Goal', level: 1, parent_id: 999 },
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
render(<GoalTreeView tree={tree} showBacklog />);
|
|
138
|
+
await waitFor(() => {
|
|
139
|
+
expect(screen.getByText('Root goal')).toBeTruthy();
|
|
140
|
+
});
|
|
141
|
+
expect(screen.getByText('Orphaned goal')).toBeTruthy();
|
|
142
|
+
expect(screen.queryByText('Backlog')).toBeTruthy();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('does not render add/delete affordances when readOnly', async () => {
|
|
146
|
+
render(<GoalTreeView tree={makeTree()} readOnly />);
|
|
147
|
+
await waitFor(() => {
|
|
148
|
+
expect(screen.getByText('Triple revenue')).toBeTruthy();
|
|
149
|
+
});
|
|
150
|
+
expect(screen.queryByLabelText('Add child goal')).toBeNull();
|
|
151
|
+
expect(screen.queryByLabelText('Delete goal')).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('fires onChange with kind "addChild" when the add-child button is clicked', async () => {
|
|
155
|
+
const onChange = vi.fn<(e: GoalTreeChange) => void>();
|
|
156
|
+
render(<GoalTreeView tree={makeTree()} onChange={onChange} />);
|
|
157
|
+
await waitFor(() => {
|
|
158
|
+
expect(screen.getByText('Triple revenue')).toBeTruthy();
|
|
159
|
+
});
|
|
160
|
+
// Target the root (id 1, level 0) specifically — makeTree()'s goal_types
|
|
161
|
+
// only go up to level 1, so clicking "add child" on an existing level-1
|
|
162
|
+
// goal would be correctly refused (would exceed max level) and never
|
|
163
|
+
// fire onChange at all.
|
|
164
|
+
const rootNode = document.querySelector('.react-flow__node[data-id="1"]');
|
|
165
|
+
const addButton = rootNode!.querySelector('[aria-label="Add child goal"]')!;
|
|
166
|
+
addButton.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
167
|
+
|
|
168
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
169
|
+
const event = onChange.mock.calls[0][0];
|
|
170
|
+
expect(event.kind).toBe('addChild');
|
|
171
|
+
if (event.kind === 'addChild') {
|
|
172
|
+
expect(event.result.goals.length).toBe(4);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('fires onChange with kind "delete" when the delete button is clicked', async () => {
|
|
177
|
+
const onChange = vi.fn<(e: GoalTreeChange) => void>();
|
|
178
|
+
render(<GoalTreeView tree={makeTree()} onChange={onChange} />);
|
|
179
|
+
await waitFor(() => {
|
|
180
|
+
expect(screen.getByText('Cut churn')).toBeTruthy();
|
|
181
|
+
});
|
|
182
|
+
const deleteButtons = screen.getAllByLabelText('Delete goal');
|
|
183
|
+
deleteButtons[deleteButtons.length - 1].dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
184
|
+
|
|
185
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
186
|
+
const event = onChange.mock.calls[0][0];
|
|
187
|
+
expect(event.kind).toBe('delete');
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('fires onChange with kind "restoreFromBacklog" when a backlog card is dropped onto a canvas card', async () => {
|
|
191
|
+
const tree: GoalTree = {
|
|
192
|
+
goal_types: [
|
|
193
|
+
{ name: 'Strategy', level: 0 },
|
|
194
|
+
{ name: 'Business Goal', level: 1 },
|
|
195
|
+
],
|
|
196
|
+
goals: [
|
|
197
|
+
{ id: 1, name: 'Root goal', type: 'Strategy', level: 0, parent_id: 0 },
|
|
198
|
+
{ id: 2, name: 'Orphaned goal', type: 'Business Goal', level: 1, parent_id: 999 },
|
|
199
|
+
],
|
|
200
|
+
};
|
|
201
|
+
const onChange = vi.fn<(e: GoalTreeChange) => void>();
|
|
202
|
+
render(<GoalTreeView tree={tree} onChange={onChange} showBacklog />);
|
|
203
|
+
await waitFor(() => {
|
|
204
|
+
expect(screen.getByText('Orphaned goal')).toBeTruthy();
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// jsdom has no native DataTransfer; stub the minimal setData/getData
|
|
208
|
+
// surface this component's native (non-reactflow) HTML5 drag path uses
|
|
209
|
+
// and attach it directly to the dispatched events.
|
|
210
|
+
const store = new Map<string, string>();
|
|
211
|
+
const dataTransfer = {
|
|
212
|
+
setData: (k: string, v: string) => store.set(k, v),
|
|
213
|
+
getData: (k: string) => store.get(k) ?? '',
|
|
214
|
+
effectAllowed: 'move',
|
|
215
|
+
};
|
|
216
|
+
function fireDnd(el: Element, type: string, coords: { clientX: number; clientY: number }) {
|
|
217
|
+
const event = new Event(type, { bubbles: true, cancelable: true });
|
|
218
|
+
Object.defineProperty(event, 'dataTransfer', { value: dataTransfer });
|
|
219
|
+
Object.assign(event, coords);
|
|
220
|
+
el.dispatchEvent(event);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const backlogItem = screen.getByText('Orphaned goal').closest('div[draggable]')!;
|
|
224
|
+
const rootNode = document.querySelector('.react-flow__node[data-id="1"]')!;
|
|
225
|
+
// jsdom doesn't implement elementFromPoint at all (not just "returns null").
|
|
226
|
+
document.elementFromPoint = vi.fn().mockReturnValue(rootNode);
|
|
227
|
+
|
|
228
|
+
fireDnd(backlogItem, 'dragstart', { clientX: 0, clientY: 0 });
|
|
229
|
+
fireDnd(rootNode, 'drop', { clientX: 10, clientY: 10 });
|
|
230
|
+
|
|
231
|
+
await waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
232
|
+
const event = onChange.mock.calls[0][0];
|
|
233
|
+
expect(event.kind).toBe('restoreFromBacklog');
|
|
234
|
+
if (event.kind === 'restoreFromBacklog') {
|
|
235
|
+
expect(event.id).toBe(2);
|
|
236
|
+
expect(event.newParentId).toBe(1);
|
|
237
|
+
expect(event.result.goals.find((g) => g.id === 2)?.parent_id).toBe(1);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// Dragging a card is choreographed by reactflow's own pointer/DOM-measurement
|
|
242
|
+
// machinery (ResizeObserver-gated node "readiness", d3-drag pointer capture,
|
|
243
|
+
// viewport transform math) — faithfully simulating that in jsdom means
|
|
244
|
+
// re-implementing reactflow's internals rather than testing this component.
|
|
245
|
+
// computeDragOutcome() is the pure decision GoalTreeView's onNodeDragStop
|
|
246
|
+
// calls once reactflow reports a drag: given where a card was dropped, which
|
|
247
|
+
// mutation fires and what onChange payload it produces. Testing it directly
|
|
248
|
+
// covers the actual reparent/moveToBacklog logic without that DOM coupling.
|
|
249
|
+
describe('computeDragOutcome (reparent / moveToBacklog decision logic)', () => {
|
|
250
|
+
const tree: GoalTree = {
|
|
251
|
+
goal_types: [
|
|
252
|
+
{ name: 'Strategy', level: 0 },
|
|
253
|
+
{ name: 'Business Goal', level: 1 },
|
|
254
|
+
],
|
|
255
|
+
goals: [
|
|
256
|
+
{ id: 1, name: 'Goal A', type: 'Strategy', level: 0, parent_id: 0 },
|
|
257
|
+
{ id: 2, name: 'Goal C', type: 'Strategy', level: 0, parent_id: 0 },
|
|
258
|
+
{ id: 3, name: 'Goal B', type: 'Business Goal', level: 1, parent_id: 1 },
|
|
259
|
+
],
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
it('fires a "reparent" change when dropped onto another card', () => {
|
|
263
|
+
const change = computeDragOutcome(tree, 3, { kind: 'node', id: 2 });
|
|
264
|
+
expect(change?.kind).toBe('reparent');
|
|
265
|
+
if (change?.kind === 'reparent') {
|
|
266
|
+
expect(change.sourceId).toBe(3);
|
|
267
|
+
expect(change.targetId).toBe(2);
|
|
268
|
+
expect(change.result.goals.find((g) => g.id === 3)?.parent_id).toBe(2);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('fires a "moveToBacklog" change when dropped on the backlog zone', () => {
|
|
273
|
+
const change = computeDragOutcome(tree, 3, { kind: 'backlog' });
|
|
274
|
+
expect(change?.kind).toBe('moveToBacklog');
|
|
275
|
+
if (change?.kind === 'moveToBacklog') {
|
|
276
|
+
expect(change.id).toBe(3);
|
|
277
|
+
expect(change.result.goals.find((g) => g.id === 3)?.parent_id).toBe(0);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('is a no-op when dropped nowhere in particular', () => {
|
|
282
|
+
expect(computeDragOutcome(tree, 3, null)).toBeNull();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('is a no-op when dropped on itself', () => {
|
|
286
|
+
expect(computeDragOutcome(tree, 3, { kind: 'node', id: 3 })).toBeNull();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('is a no-op when the reparent would exceed the schema max level', () => {
|
|
290
|
+
// Goal 3 is already level 1 (max); reparenting it under itself-sibling
|
|
291
|
+
// level-1 goal would push it to level 2.
|
|
292
|
+
const deeper: GoalTree = {
|
|
293
|
+
...tree,
|
|
294
|
+
goals: [...tree.goals, { id: 4, name: 'Goal D', type: 'Business Goal', level: 1, parent_id: 1 }],
|
|
295
|
+
};
|
|
296
|
+
expect(computeDragOutcome(deeper, 3, { kind: 'node', id: 4 })).toBeNull();
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('fires onEditRequest on double-click', async () => {
|
|
301
|
+
const onEditRequest = vi.fn();
|
|
302
|
+
render(<GoalTreeView tree={makeTree()} onEditRequest={onEditRequest} />);
|
|
303
|
+
await waitFor(() => {
|
|
304
|
+
expect(screen.getByText('Triple revenue')).toBeTruthy();
|
|
305
|
+
});
|
|
306
|
+
const nodeEl = document.querySelector('.react-flow__node[data-id="1"]');
|
|
307
|
+
expect(nodeEl).toBeTruthy();
|
|
308
|
+
nodeEl!.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }));
|
|
309
|
+
|
|
310
|
+
await waitFor(() => expect(onEditRequest).toHaveBeenCalledTimes(1));
|
|
311
|
+
expect(onEditRequest.mock.calls[0][0]).toMatchObject({ id: 1, name: 'Triple revenue' });
|
|
312
|
+
});
|
|
313
|
+
});
|
package/src/goals/index.ts
CHANGED
|
@@ -18,3 +18,7 @@ export { parseCanonicalGoals } from './parse-canonical.js';
|
|
|
18
18
|
export type { CanonicalGoalsResult } from './parse-canonical.js';
|
|
19
19
|
export { layoutGoalTree, selectScopedGoals } from './layout.js';
|
|
20
20
|
export { reparent, addChild, deleteWithDescendants, moveToBacklog, restoreFromBacklog } from './mutations.js';
|
|
21
|
+
|
|
22
|
+
export { GoalTreeView } from './GoalTreeView.js';
|
|
23
|
+
export type { GoalTreeViewProps, GoalTreeChange } from './GoalTreeView.js';
|
|
24
|
+
export type { ThemeTokens } from './theme.js';
|