@dnd-block-tree/react 2.0.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/README.md +61 -0
- package/dist/index.d.mts +337 -0
- package/dist/index.d.ts +337 -0
- package/dist/index.js +2500 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2360 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +65 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2360 @@
|
|
|
1
|
+
import { extractUUID, createStickyCollision, debounce, computeNormalizedIndex, extractBlockId, reparentBlockIndex, getDropZoneType, reparentMultipleBlocks, buildOrderedBlocks, historyReducer, getBlockDepth, validateBlockTree, blockReducer, generateKeyBetween, generateId, getDescendantIds, expandReducer } from '@dnd-block-tree/core';
|
|
2
|
+
export { EventEmitter, blockReducer, buildOrderedBlocks, cloneMap, cloneParentMap, closestCenterCollision, compareFractionalKeys, computeNormalizedIndex, createBlockTree, createStickyCollision, debounce, deleteBlockAndDescendants, expandReducer, extractBlockId, extractUUID, flatToNested, generateId, generateInitialKeys, generateKeyBetween, generateNKeysBetween, getBlockDepth, getDescendantIds, getDropZoneType, getSubtreeDepth, historyReducer, initFractionalOrder, nestedToFlat, reparentBlockIndex, reparentMultipleBlocks, validateBlockTree, weightedVerticalCollision } from '@dnd-block-tree/core';
|
|
3
|
+
import { useDroppable, useDraggable, useSensors, useSensor, PointerSensor, TouchSensor, KeyboardSensor, DragOverlay as DragOverlay$1, DndContext } from '@dnd-kit/core';
|
|
4
|
+
import { memo, useCallback, useEffect, Fragment, useMemo, useRef, useReducer, useState, createContext, useLayoutEffect, useContext } from 'react';
|
|
5
|
+
import { jsx, jsxs, Fragment as Fragment$1 } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
// src/index.ts
|
|
8
|
+
|
|
9
|
+
// src/bridge.ts
|
|
10
|
+
function adaptCollisionDetection(coreDetector) {
|
|
11
|
+
return ({ droppableContainers, collisionRect }) => {
|
|
12
|
+
if (!collisionRect) return [];
|
|
13
|
+
const candidates = [];
|
|
14
|
+
for (const container of droppableContainers) {
|
|
15
|
+
const rect = container.rect.current;
|
|
16
|
+
if (!rect) continue;
|
|
17
|
+
candidates.push({
|
|
18
|
+
id: String(container.id),
|
|
19
|
+
rect: {
|
|
20
|
+
top: rect.top,
|
|
21
|
+
left: rect.left,
|
|
22
|
+
width: rect.width,
|
|
23
|
+
height: rect.height,
|
|
24
|
+
right: rect.right,
|
|
25
|
+
bottom: rect.bottom
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const pointerRect = {
|
|
30
|
+
top: collisionRect.top,
|
|
31
|
+
left: collisionRect.left,
|
|
32
|
+
width: collisionRect.width,
|
|
33
|
+
height: collisionRect.height,
|
|
34
|
+
right: collisionRect.left + collisionRect.width,
|
|
35
|
+
bottom: collisionRect.top + collisionRect.height
|
|
36
|
+
};
|
|
37
|
+
const results = coreDetector(candidates, pointerRect);
|
|
38
|
+
return results.map((result) => {
|
|
39
|
+
const container = droppableContainers.find((c) => String(c.id) === result.id);
|
|
40
|
+
if (!container) return null;
|
|
41
|
+
return {
|
|
42
|
+
id: result.id,
|
|
43
|
+
data: {
|
|
44
|
+
droppableContainer: container,
|
|
45
|
+
value: result.value,
|
|
46
|
+
left: result.left
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}).filter((c) => c !== null);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/utils/haptic.ts
|
|
54
|
+
function triggerHaptic(durationMs = 10) {
|
|
55
|
+
if (typeof navigator !== "undefined" && typeof navigator.vibrate === "function") {
|
|
56
|
+
navigator.vibrate(durationMs);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
var DEFAULT_ACTIVATION_DISTANCE = 8;
|
|
60
|
+
function useConfiguredSensors(config = {}) {
|
|
61
|
+
const {
|
|
62
|
+
activationDistance = DEFAULT_ACTIVATION_DISTANCE,
|
|
63
|
+
activationDelay,
|
|
64
|
+
tolerance
|
|
65
|
+
} = config;
|
|
66
|
+
let pointerConstraint;
|
|
67
|
+
let touchConstraint;
|
|
68
|
+
if (activationDelay !== void 0) {
|
|
69
|
+
pointerConstraint = {
|
|
70
|
+
delay: activationDelay,
|
|
71
|
+
tolerance: tolerance ?? 5
|
|
72
|
+
};
|
|
73
|
+
touchConstraint = pointerConstraint;
|
|
74
|
+
} else {
|
|
75
|
+
pointerConstraint = {
|
|
76
|
+
distance: activationDistance
|
|
77
|
+
};
|
|
78
|
+
touchConstraint = {
|
|
79
|
+
delay: config.longPressDelay ?? 200,
|
|
80
|
+
tolerance: 5
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return useSensors(
|
|
84
|
+
useSensor(PointerSensor, {
|
|
85
|
+
activationConstraint: pointerConstraint
|
|
86
|
+
}),
|
|
87
|
+
useSensor(TouchSensor, {
|
|
88
|
+
activationConstraint: touchConstraint
|
|
89
|
+
}),
|
|
90
|
+
useSensor(KeyboardSensor)
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
function getSensorConfig(config = {}) {
|
|
94
|
+
const {
|
|
95
|
+
activationDistance = DEFAULT_ACTIVATION_DISTANCE,
|
|
96
|
+
activationDelay,
|
|
97
|
+
tolerance
|
|
98
|
+
} = config;
|
|
99
|
+
let activationConstraint;
|
|
100
|
+
if (activationDelay !== void 0) {
|
|
101
|
+
activationConstraint = {
|
|
102
|
+
delay: activationDelay,
|
|
103
|
+
tolerance: tolerance ?? 5
|
|
104
|
+
};
|
|
105
|
+
} else {
|
|
106
|
+
activationConstraint = {
|
|
107
|
+
distance: activationDistance
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
pointer: { activationConstraint },
|
|
112
|
+
touch: { activationConstraint }
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function DropZoneComponent({
|
|
116
|
+
id,
|
|
117
|
+
parentId,
|
|
118
|
+
onHover,
|
|
119
|
+
activeId,
|
|
120
|
+
className = "h-1 rounded transition-colors",
|
|
121
|
+
activeClassName = "bg-blue-500",
|
|
122
|
+
height = 4
|
|
123
|
+
}) {
|
|
124
|
+
const { setNodeRef, isOver, active } = useDroppable({ id });
|
|
125
|
+
const handleInternalHover = useCallback(() => {
|
|
126
|
+
onHover(id, parentId);
|
|
127
|
+
}, [onHover, id, parentId]);
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
if (isOver) handleInternalHover();
|
|
130
|
+
}, [isOver, handleInternalHover]);
|
|
131
|
+
const zoneBlockId = extractUUID(id);
|
|
132
|
+
const isIntoZone = id.startsWith("into-");
|
|
133
|
+
if (isIntoZone && active?.id && zoneBlockId === String(active.id)) return null;
|
|
134
|
+
if (isIntoZone && activeId && zoneBlockId === activeId) return null;
|
|
135
|
+
return /* @__PURE__ */ jsx(
|
|
136
|
+
"div",
|
|
137
|
+
{
|
|
138
|
+
ref: setNodeRef,
|
|
139
|
+
"data-zone-id": id,
|
|
140
|
+
"data-parent-id": parentId ?? "",
|
|
141
|
+
style: { height: isOver ? height * 2 : height },
|
|
142
|
+
className: `${className} ${isOver ? activeClassName : "bg-transparent"}`
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
var DropZone = memo(DropZoneComponent);
|
|
147
|
+
function GhostPreview({ children }) {
|
|
148
|
+
return /* @__PURE__ */ jsx("div", { "data-dnd-ghost": true, className: "opacity-50", style: { pointerEvents: "none" }, children });
|
|
149
|
+
}
|
|
150
|
+
function DraggableBlock({
|
|
151
|
+
block,
|
|
152
|
+
children,
|
|
153
|
+
disabled,
|
|
154
|
+
focusedId,
|
|
155
|
+
isSelected,
|
|
156
|
+
onBlockClick,
|
|
157
|
+
isContainer,
|
|
158
|
+
isExpanded,
|
|
159
|
+
depth,
|
|
160
|
+
posInSet,
|
|
161
|
+
setSize
|
|
162
|
+
}) {
|
|
163
|
+
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({
|
|
164
|
+
id: block.id,
|
|
165
|
+
disabled
|
|
166
|
+
});
|
|
167
|
+
const isFocused = focusedId === block.id;
|
|
168
|
+
return /* @__PURE__ */ jsx(
|
|
169
|
+
"div",
|
|
170
|
+
{
|
|
171
|
+
ref: setNodeRef,
|
|
172
|
+
...attributes,
|
|
173
|
+
...listeners,
|
|
174
|
+
"data-block-id": block.id,
|
|
175
|
+
tabIndex: isFocused ? 0 : -1,
|
|
176
|
+
onClick: onBlockClick ? (e) => onBlockClick(block.id, e) : void 0,
|
|
177
|
+
"data-selected": isSelected || void 0,
|
|
178
|
+
style: { touchAction: "none", minWidth: 0, outline: "none" },
|
|
179
|
+
role: "treeitem",
|
|
180
|
+
"aria-level": depth + 1,
|
|
181
|
+
"aria-posinset": posInSet,
|
|
182
|
+
"aria-setsize": setSize,
|
|
183
|
+
"aria-expanded": isContainer ? isExpanded : void 0,
|
|
184
|
+
"aria-selected": isSelected ?? void 0,
|
|
185
|
+
children: children({ isDragging })
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
function TreeRendererInner({
|
|
190
|
+
blocks,
|
|
191
|
+
blocksByParent,
|
|
192
|
+
parentId,
|
|
193
|
+
activeId,
|
|
194
|
+
expandedMap,
|
|
195
|
+
renderers,
|
|
196
|
+
containerTypes,
|
|
197
|
+
onHover,
|
|
198
|
+
onToggleExpand,
|
|
199
|
+
depth = 0,
|
|
200
|
+
dropZoneClassName,
|
|
201
|
+
dropZoneActiveClassName,
|
|
202
|
+
indentClassName = "ml-6 border-l border-gray-200 pl-4",
|
|
203
|
+
rootClassName = "flex flex-col gap-1",
|
|
204
|
+
canDrag,
|
|
205
|
+
previewPosition,
|
|
206
|
+
draggedBlock,
|
|
207
|
+
focusedId,
|
|
208
|
+
selectedIds,
|
|
209
|
+
onBlockClick,
|
|
210
|
+
animation,
|
|
211
|
+
virtualVisibleIds
|
|
212
|
+
}) {
|
|
213
|
+
const items = blocksByParent.get(parentId) ?? [];
|
|
214
|
+
let filteredBlocks = items.filter((block) => block.id !== activeId);
|
|
215
|
+
if (virtualVisibleIds && depth === 0) {
|
|
216
|
+
filteredBlocks = filteredBlocks.filter((block) => virtualVisibleIds.has(block.id));
|
|
217
|
+
}
|
|
218
|
+
const showGhostHere = previewPosition?.parentId === parentId && draggedBlock;
|
|
219
|
+
const containerClass = depth === 0 ? rootClassName : indentClassName;
|
|
220
|
+
return /* @__PURE__ */ jsxs("div", { className: containerClass, style: { minWidth: 0 }, children: [
|
|
221
|
+
/* @__PURE__ */ jsx(
|
|
222
|
+
DropZone,
|
|
223
|
+
{
|
|
224
|
+
id: parentId ? `into-${parentId}` : "root-start",
|
|
225
|
+
parentId,
|
|
226
|
+
onHover,
|
|
227
|
+
activeId,
|
|
228
|
+
className: dropZoneClassName,
|
|
229
|
+
activeClassName: dropZoneActiveClassName
|
|
230
|
+
}
|
|
231
|
+
),
|
|
232
|
+
filteredBlocks.map((block, index) => {
|
|
233
|
+
const isContainer = containerTypes.includes(block.type);
|
|
234
|
+
const isExpanded = expandedMap[block.id] !== false;
|
|
235
|
+
const Renderer = renderers[block.type];
|
|
236
|
+
const isDragDisabled = canDrag ? !canDrag(block) : false;
|
|
237
|
+
const ghostBeforeThis = showGhostHere && previewPosition.index === index;
|
|
238
|
+
const originalIndex = items.findIndex((b) => b.id === block.id);
|
|
239
|
+
const isLastInOriginal = originalIndex === items.length - 1;
|
|
240
|
+
if (!Renderer) {
|
|
241
|
+
console.warn(`No renderer found for block type: ${block.type}`);
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const GhostRenderer = draggedBlock ? renderers[draggedBlock.type] : null;
|
|
245
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
246
|
+
ghostBeforeThis && GhostRenderer && /* @__PURE__ */ jsx(GhostPreview, { children: GhostRenderer({
|
|
247
|
+
block: draggedBlock,
|
|
248
|
+
isDragging: true,
|
|
249
|
+
depth
|
|
250
|
+
}) }),
|
|
251
|
+
/* @__PURE__ */ jsx(
|
|
252
|
+
DraggableBlock,
|
|
253
|
+
{
|
|
254
|
+
block,
|
|
255
|
+
disabled: isDragDisabled,
|
|
256
|
+
focusedId,
|
|
257
|
+
isSelected: selectedIds?.has(block.id),
|
|
258
|
+
onBlockClick,
|
|
259
|
+
isContainer,
|
|
260
|
+
isExpanded,
|
|
261
|
+
depth,
|
|
262
|
+
posInSet: originalIndex + 1,
|
|
263
|
+
setSize: items.length,
|
|
264
|
+
children: ({ isDragging }) => {
|
|
265
|
+
if (isContainer) {
|
|
266
|
+
const animated = animation?.expandDuration && animation.expandDuration > 0;
|
|
267
|
+
const easing = animation?.easing ?? "ease";
|
|
268
|
+
const duration = animation?.expandDuration ?? 0;
|
|
269
|
+
let childContent;
|
|
270
|
+
if (animated) {
|
|
271
|
+
childContent = /* @__PURE__ */ jsx(
|
|
272
|
+
"div",
|
|
273
|
+
{
|
|
274
|
+
style: {
|
|
275
|
+
display: "grid",
|
|
276
|
+
gridTemplateRows: isExpanded ? "1fr" : "0fr",
|
|
277
|
+
transition: `grid-template-rows ${duration}ms ${easing}`
|
|
278
|
+
},
|
|
279
|
+
children: /* @__PURE__ */ jsx(
|
|
280
|
+
"div",
|
|
281
|
+
{
|
|
282
|
+
style: {
|
|
283
|
+
overflow: "hidden",
|
|
284
|
+
minHeight: 0,
|
|
285
|
+
opacity: isExpanded ? 1 : 0,
|
|
286
|
+
transition: `opacity ${duration}ms ${easing}`
|
|
287
|
+
},
|
|
288
|
+
children: /* @__PURE__ */ jsx(
|
|
289
|
+
TreeRenderer,
|
|
290
|
+
{
|
|
291
|
+
blocks,
|
|
292
|
+
blocksByParent,
|
|
293
|
+
parentId: block.id,
|
|
294
|
+
activeId,
|
|
295
|
+
expandedMap,
|
|
296
|
+
renderers,
|
|
297
|
+
containerTypes,
|
|
298
|
+
onHover,
|
|
299
|
+
onToggleExpand,
|
|
300
|
+
depth: depth + 1,
|
|
301
|
+
dropZoneClassName,
|
|
302
|
+
dropZoneActiveClassName,
|
|
303
|
+
indentClassName,
|
|
304
|
+
rootClassName,
|
|
305
|
+
canDrag,
|
|
306
|
+
previewPosition,
|
|
307
|
+
draggedBlock,
|
|
308
|
+
focusedId,
|
|
309
|
+
selectedIds,
|
|
310
|
+
onBlockClick,
|
|
311
|
+
animation,
|
|
312
|
+
virtualVisibleIds
|
|
313
|
+
}
|
|
314
|
+
)
|
|
315
|
+
}
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
} else {
|
|
320
|
+
childContent = isExpanded ? /* @__PURE__ */ jsx(
|
|
321
|
+
TreeRenderer,
|
|
322
|
+
{
|
|
323
|
+
blocks,
|
|
324
|
+
blocksByParent,
|
|
325
|
+
parentId: block.id,
|
|
326
|
+
activeId,
|
|
327
|
+
expandedMap,
|
|
328
|
+
renderers,
|
|
329
|
+
containerTypes,
|
|
330
|
+
onHover,
|
|
331
|
+
onToggleExpand,
|
|
332
|
+
depth: depth + 1,
|
|
333
|
+
dropZoneClassName,
|
|
334
|
+
dropZoneActiveClassName,
|
|
335
|
+
indentClassName,
|
|
336
|
+
rootClassName,
|
|
337
|
+
canDrag,
|
|
338
|
+
previewPosition,
|
|
339
|
+
draggedBlock,
|
|
340
|
+
focusedId,
|
|
341
|
+
selectedIds,
|
|
342
|
+
onBlockClick,
|
|
343
|
+
animation,
|
|
344
|
+
virtualVisibleIds
|
|
345
|
+
}
|
|
346
|
+
) : null;
|
|
347
|
+
}
|
|
348
|
+
return Renderer({
|
|
349
|
+
block,
|
|
350
|
+
children: childContent,
|
|
351
|
+
isDragging,
|
|
352
|
+
depth,
|
|
353
|
+
isExpanded,
|
|
354
|
+
onToggleExpand: () => onToggleExpand(block.id)
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
return Renderer({
|
|
358
|
+
block,
|
|
359
|
+
isDragging,
|
|
360
|
+
depth
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
),
|
|
365
|
+
!isLastInOriginal && /* @__PURE__ */ jsx(
|
|
366
|
+
DropZone,
|
|
367
|
+
{
|
|
368
|
+
id: `after-${block.id}`,
|
|
369
|
+
parentId: block.parentId,
|
|
370
|
+
onHover,
|
|
371
|
+
activeId,
|
|
372
|
+
className: dropZoneClassName,
|
|
373
|
+
activeClassName: dropZoneActiveClassName
|
|
374
|
+
}
|
|
375
|
+
)
|
|
376
|
+
] }, block.id);
|
|
377
|
+
}),
|
|
378
|
+
showGhostHere && previewPosition.index >= filteredBlocks.length && draggedBlock && (() => {
|
|
379
|
+
const GhostRenderer = renderers[draggedBlock.type];
|
|
380
|
+
return GhostRenderer ? /* @__PURE__ */ jsx(GhostPreview, { children: GhostRenderer({
|
|
381
|
+
block: draggedBlock,
|
|
382
|
+
isDragging: true,
|
|
383
|
+
depth
|
|
384
|
+
}) }) : null;
|
|
385
|
+
})(),
|
|
386
|
+
/* @__PURE__ */ jsx(
|
|
387
|
+
DropZone,
|
|
388
|
+
{
|
|
389
|
+
id: parentId ? `end-${parentId}` : "root-end",
|
|
390
|
+
parentId,
|
|
391
|
+
onHover,
|
|
392
|
+
activeId,
|
|
393
|
+
className: dropZoneClassName,
|
|
394
|
+
activeClassName: dropZoneActiveClassName
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
] });
|
|
398
|
+
}
|
|
399
|
+
var TreeRenderer = memo(TreeRendererInner);
|
|
400
|
+
function DragOverlay({
|
|
401
|
+
activeBlock,
|
|
402
|
+
children,
|
|
403
|
+
selectedCount = 0
|
|
404
|
+
}) {
|
|
405
|
+
const showBadge = selectedCount > 1;
|
|
406
|
+
return /* @__PURE__ */ jsx(DragOverlay$1, { children: activeBlock && /* @__PURE__ */ jsxs("div", { style: { position: "relative" }, children: [
|
|
407
|
+
showBadge && /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
408
|
+
/* @__PURE__ */ jsx(
|
|
409
|
+
"div",
|
|
410
|
+
{
|
|
411
|
+
style: {
|
|
412
|
+
position: "absolute",
|
|
413
|
+
top: 4,
|
|
414
|
+
left: 4,
|
|
415
|
+
right: -4,
|
|
416
|
+
bottom: -4,
|
|
417
|
+
borderRadius: 8,
|
|
418
|
+
border: "1px solid #d1d5db",
|
|
419
|
+
background: "#f3f4f6",
|
|
420
|
+
opacity: 0.6,
|
|
421
|
+
zIndex: -1
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
),
|
|
425
|
+
/* @__PURE__ */ jsx(
|
|
426
|
+
"div",
|
|
427
|
+
{
|
|
428
|
+
style: {
|
|
429
|
+
position: "absolute",
|
|
430
|
+
top: -8,
|
|
431
|
+
right: -8,
|
|
432
|
+
background: "#3b82f6",
|
|
433
|
+
color: "white",
|
|
434
|
+
borderRadius: "50%",
|
|
435
|
+
width: 22,
|
|
436
|
+
height: 22,
|
|
437
|
+
display: "flex",
|
|
438
|
+
alignItems: "center",
|
|
439
|
+
justifyContent: "center",
|
|
440
|
+
fontSize: 11,
|
|
441
|
+
fontWeight: 700,
|
|
442
|
+
zIndex: 10,
|
|
443
|
+
boxShadow: "0 1px 3px rgba(0,0,0,0.2)"
|
|
444
|
+
},
|
|
445
|
+
children: selectedCount
|
|
446
|
+
}
|
|
447
|
+
)
|
|
448
|
+
] }),
|
|
449
|
+
children ? children(activeBlock) : /* @__PURE__ */ jsxs("div", { className: "bg-white border border-gray-300 shadow-md rounded-md p-3 text-sm w-64 pointer-events-none", children: [
|
|
450
|
+
/* @__PURE__ */ jsx("div", { className: "text-gray-500 uppercase text-xs tracking-wide mb-1", children: activeBlock.type }),
|
|
451
|
+
/* @__PURE__ */ jsxs("div", { className: "font-semibold text-gray-800", children: [
|
|
452
|
+
"Block ",
|
|
453
|
+
activeBlock.id.slice(0, 8)
|
|
454
|
+
] })
|
|
455
|
+
] })
|
|
456
|
+
] }) });
|
|
457
|
+
}
|
|
458
|
+
function getBlockPosition(blocks, blockId) {
|
|
459
|
+
const block = blocks.find((b) => b.id === blockId);
|
|
460
|
+
if (!block) return { parentId: null, index: 0 };
|
|
461
|
+
const siblings = blocks.filter((b) => b.parentId === block.parentId);
|
|
462
|
+
const index = siblings.findIndex((b) => b.id === blockId);
|
|
463
|
+
return { parentId: block.parentId, index };
|
|
464
|
+
}
|
|
465
|
+
function computeInitialExpanded(blocks, containerTypes, initialExpanded) {
|
|
466
|
+
if (initialExpanded === "none") {
|
|
467
|
+
const expandedMap2 = {};
|
|
468
|
+
const containers2 = blocks.filter((b) => containerTypes.includes(b.type));
|
|
469
|
+
for (const container of containers2) {
|
|
470
|
+
expandedMap2[container.id] = false;
|
|
471
|
+
}
|
|
472
|
+
return expandedMap2;
|
|
473
|
+
}
|
|
474
|
+
const expandedMap = {};
|
|
475
|
+
const containers = blocks.filter((b) => containerTypes.includes(b.type));
|
|
476
|
+
if (initialExpanded === "all" || initialExpanded === void 0) {
|
|
477
|
+
for (const container of containers) {
|
|
478
|
+
expandedMap[container.id] = true;
|
|
479
|
+
}
|
|
480
|
+
} else if (Array.isArray(initialExpanded)) {
|
|
481
|
+
for (const id of initialExpanded) {
|
|
482
|
+
expandedMap[id] = true;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return expandedMap;
|
|
486
|
+
}
|
|
487
|
+
function getVisibleBlockIds(blocksByParent, containerTypes, expandedMap, parentId = null) {
|
|
488
|
+
const result = [];
|
|
489
|
+
const children = blocksByParent.get(parentId) ?? [];
|
|
490
|
+
for (const block of children) {
|
|
491
|
+
result.push(block.id);
|
|
492
|
+
if (containerTypes.includes(block.type) && expandedMap[block.id] !== false) {
|
|
493
|
+
result.push(...getVisibleBlockIds(blocksByParent, containerTypes, expandedMap, block.id));
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
function BlockTree({
|
|
499
|
+
blocks,
|
|
500
|
+
renderers,
|
|
501
|
+
containerTypes = [],
|
|
502
|
+
onChange,
|
|
503
|
+
dragOverlay,
|
|
504
|
+
activationDistance = 8,
|
|
505
|
+
previewDebounce = 150,
|
|
506
|
+
className = "flex flex-col gap-1",
|
|
507
|
+
dropZoneClassName,
|
|
508
|
+
dropZoneActiveClassName,
|
|
509
|
+
indentClassName,
|
|
510
|
+
showDropPreview = true,
|
|
511
|
+
// Callbacks
|
|
512
|
+
onDragStart,
|
|
513
|
+
onDragMove,
|
|
514
|
+
onDragEnd,
|
|
515
|
+
onDragCancel,
|
|
516
|
+
onBeforeMove,
|
|
517
|
+
onBlockMove,
|
|
518
|
+
onExpandChange,
|
|
519
|
+
onHoverChange,
|
|
520
|
+
// Customization
|
|
521
|
+
canDrag,
|
|
522
|
+
canDrop,
|
|
523
|
+
collisionDetection,
|
|
524
|
+
sensors: sensorConfig,
|
|
525
|
+
animation,
|
|
526
|
+
initialExpanded,
|
|
527
|
+
orderingStrategy = "integer",
|
|
528
|
+
maxDepth,
|
|
529
|
+
keyboardNavigation = false,
|
|
530
|
+
multiSelect = false,
|
|
531
|
+
selectedIds: externalSelectedIds,
|
|
532
|
+
onSelectionChange,
|
|
533
|
+
virtualize
|
|
534
|
+
}) {
|
|
535
|
+
const sensors = useConfiguredSensors({
|
|
536
|
+
activationDistance: sensorConfig?.activationDistance ?? activationDistance,
|
|
537
|
+
activationDelay: sensorConfig?.activationDelay,
|
|
538
|
+
tolerance: sensorConfig?.tolerance,
|
|
539
|
+
longPressDelay: sensorConfig?.longPressDelay
|
|
540
|
+
});
|
|
541
|
+
const initialExpandedMap = useMemo(
|
|
542
|
+
() => computeInitialExpanded(blocks, containerTypes, initialExpanded),
|
|
543
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
544
|
+
[]
|
|
545
|
+
);
|
|
546
|
+
const stateRef = useRef({
|
|
547
|
+
activeId: null,
|
|
548
|
+
hoverZone: null,
|
|
549
|
+
expandedMap: initialExpandedMap,
|
|
550
|
+
virtualState: null,
|
|
551
|
+
isDragging: false
|
|
552
|
+
});
|
|
553
|
+
const initialBlocksRef = useRef([]);
|
|
554
|
+
const cachedReorderRef = useRef(null);
|
|
555
|
+
const fromPositionRef = useRef(null);
|
|
556
|
+
const draggedIdsRef = useRef([]);
|
|
557
|
+
const snapshotRectsRef = useRef(null);
|
|
558
|
+
const needsResnapshot = useRef(false);
|
|
559
|
+
const stickyCollisionRef = useRef(
|
|
560
|
+
adaptCollisionDetection(createStickyCollision(20, snapshotRectsRef))
|
|
561
|
+
);
|
|
562
|
+
const coreStickyRef = useRef(createStickyCollision(20, snapshotRectsRef));
|
|
563
|
+
const snapshotZoneRects = useCallback(() => {
|
|
564
|
+
const root = rootRef.current;
|
|
565
|
+
if (!root) return;
|
|
566
|
+
const zones = root.querySelectorAll("[data-zone-id]");
|
|
567
|
+
const map = /* @__PURE__ */ new Map();
|
|
568
|
+
zones.forEach((el) => {
|
|
569
|
+
const id = el.getAttribute("data-zone-id");
|
|
570
|
+
if (id) map.set(id, el.getBoundingClientRect());
|
|
571
|
+
});
|
|
572
|
+
snapshotRectsRef.current = map;
|
|
573
|
+
}, []);
|
|
574
|
+
const [, forceRender] = useReducer((x) => x + 1, 0);
|
|
575
|
+
const debouncedSetVirtual = useRef(
|
|
576
|
+
debounce((newBlocks) => {
|
|
577
|
+
if (newBlocks) {
|
|
578
|
+
stateRef.current.virtualState = computeNormalizedIndex(newBlocks);
|
|
579
|
+
} else {
|
|
580
|
+
stateRef.current.virtualState = null;
|
|
581
|
+
}
|
|
582
|
+
needsResnapshot.current = true;
|
|
583
|
+
forceRender();
|
|
584
|
+
}, previewDebounce)
|
|
585
|
+
).current;
|
|
586
|
+
const debouncedDragMove = useRef(
|
|
587
|
+
debounce((event) => {
|
|
588
|
+
onDragMove?.(event);
|
|
589
|
+
}, 50)
|
|
590
|
+
).current;
|
|
591
|
+
const originalIndex = useMemo(
|
|
592
|
+
() => computeNormalizedIndex(blocks, orderingStrategy),
|
|
593
|
+
[blocks, orderingStrategy]
|
|
594
|
+
);
|
|
595
|
+
const blocksByParent = useMemo(() => {
|
|
596
|
+
const map = /* @__PURE__ */ new Map();
|
|
597
|
+
for (const [parentId, ids] of originalIndex.byParent.entries()) {
|
|
598
|
+
map.set(parentId, ids.map((id) => originalIndex.byId.get(id)).filter(Boolean));
|
|
599
|
+
}
|
|
600
|
+
return map;
|
|
601
|
+
}, [originalIndex]);
|
|
602
|
+
const focusedIdRef = useRef(null);
|
|
603
|
+
const rootRef = useRef(null);
|
|
604
|
+
const lastClickedIdRef = useRef(null);
|
|
605
|
+
const [internalSelectedIds, setInternalSelectedIds] = useReducer(
|
|
606
|
+
(_, next) => next,
|
|
607
|
+
/* @__PURE__ */ new Set()
|
|
608
|
+
);
|
|
609
|
+
const selectedIds = externalSelectedIds ?? internalSelectedIds;
|
|
610
|
+
const setSelectedIds = useCallback((ids) => {
|
|
611
|
+
if (onSelectionChange) {
|
|
612
|
+
onSelectionChange(ids);
|
|
613
|
+
} else {
|
|
614
|
+
setInternalSelectedIds(ids);
|
|
615
|
+
}
|
|
616
|
+
}, [onSelectionChange]);
|
|
617
|
+
const visibleBlockIds = useMemo(
|
|
618
|
+
() => getVisibleBlockIds(blocksByParent, containerTypes, stateRef.current.expandedMap),
|
|
619
|
+
[blocksByParent, containerTypes, stateRef.current.expandedMap]
|
|
620
|
+
);
|
|
621
|
+
const focusBlock = useCallback((id) => {
|
|
622
|
+
focusedIdRef.current = id;
|
|
623
|
+
if (id && rootRef.current) {
|
|
624
|
+
const el = rootRef.current.querySelector(`[data-block-id="${id}"]`);
|
|
625
|
+
el?.focus();
|
|
626
|
+
}
|
|
627
|
+
forceRender();
|
|
628
|
+
}, []);
|
|
629
|
+
const toggleExpandRef = useRef(() => {
|
|
630
|
+
});
|
|
631
|
+
const handleKeyDown = useCallback((event) => {
|
|
632
|
+
if (!keyboardNavigation) return;
|
|
633
|
+
const currentId = focusedIdRef.current;
|
|
634
|
+
const currentIndex = currentId ? visibleBlockIds.indexOf(currentId) : -1;
|
|
635
|
+
switch (event.key) {
|
|
636
|
+
case "ArrowDown": {
|
|
637
|
+
event.preventDefault();
|
|
638
|
+
const nextIndex = currentIndex < visibleBlockIds.length - 1 ? currentIndex + 1 : currentIndex;
|
|
639
|
+
focusBlock(visibleBlockIds[nextIndex] ?? null);
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
case "ArrowUp": {
|
|
643
|
+
event.preventDefault();
|
|
644
|
+
const prevIndex = currentIndex > 0 ? currentIndex - 1 : 0;
|
|
645
|
+
focusBlock(visibleBlockIds[prevIndex] ?? null);
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
case "ArrowRight": {
|
|
649
|
+
event.preventDefault();
|
|
650
|
+
if (currentId) {
|
|
651
|
+
const block = originalIndex.byId.get(currentId);
|
|
652
|
+
if (block && containerTypes.includes(block.type)) {
|
|
653
|
+
if (stateRef.current.expandedMap[currentId] === false) {
|
|
654
|
+
toggleExpandRef.current(currentId);
|
|
655
|
+
} else {
|
|
656
|
+
const children = blocksByParent.get(currentId) ?? [];
|
|
657
|
+
if (children.length > 0) focusBlock(children[0].id);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
case "ArrowLeft": {
|
|
664
|
+
event.preventDefault();
|
|
665
|
+
if (currentId) {
|
|
666
|
+
const block = originalIndex.byId.get(currentId);
|
|
667
|
+
if (block && containerTypes.includes(block.type) && stateRef.current.expandedMap[currentId] !== false) {
|
|
668
|
+
toggleExpandRef.current(currentId);
|
|
669
|
+
} else if (block?.parentId) {
|
|
670
|
+
focusBlock(block.parentId);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
case "Enter":
|
|
676
|
+
case " ": {
|
|
677
|
+
event.preventDefault();
|
|
678
|
+
if (currentId) {
|
|
679
|
+
const block = originalIndex.byId.get(currentId);
|
|
680
|
+
if (block && containerTypes.includes(block.type)) {
|
|
681
|
+
toggleExpandRef.current(currentId);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
case "Home": {
|
|
687
|
+
event.preventDefault();
|
|
688
|
+
if (visibleBlockIds.length > 0) focusBlock(visibleBlockIds[0]);
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
case "End": {
|
|
692
|
+
event.preventDefault();
|
|
693
|
+
if (visibleBlockIds.length > 0) focusBlock(visibleBlockIds[visibleBlockIds.length - 1]);
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}, [keyboardNavigation, visibleBlockIds, focusBlock, originalIndex, containerTypes, blocksByParent]);
|
|
698
|
+
const handleBlockClick = useCallback((blockId, event) => {
|
|
699
|
+
if (!multiSelect) return;
|
|
700
|
+
if (event.metaKey || event.ctrlKey) {
|
|
701
|
+
const next = new Set(selectedIds);
|
|
702
|
+
if (next.has(blockId)) {
|
|
703
|
+
next.delete(blockId);
|
|
704
|
+
} else {
|
|
705
|
+
next.add(blockId);
|
|
706
|
+
}
|
|
707
|
+
setSelectedIds(next);
|
|
708
|
+
} else if (event.shiftKey && lastClickedIdRef.current) {
|
|
709
|
+
const startIdx = visibleBlockIds.indexOf(lastClickedIdRef.current);
|
|
710
|
+
const endIdx = visibleBlockIds.indexOf(blockId);
|
|
711
|
+
if (startIdx !== -1 && endIdx !== -1) {
|
|
712
|
+
const [from, to] = startIdx < endIdx ? [startIdx, endIdx] : [endIdx, startIdx];
|
|
713
|
+
const next = new Set(selectedIds);
|
|
714
|
+
for (let i = from; i <= to; i++) {
|
|
715
|
+
next.add(visibleBlockIds[i]);
|
|
716
|
+
}
|
|
717
|
+
setSelectedIds(next);
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
setSelectedIds(/* @__PURE__ */ new Set([blockId]));
|
|
721
|
+
}
|
|
722
|
+
lastClickedIdRef.current = blockId;
|
|
723
|
+
}, [multiSelect, selectedIds, setSelectedIds, visibleBlockIds]);
|
|
724
|
+
useEffect(() => {
|
|
725
|
+
if (!needsResnapshot.current || !stateRef.current.isDragging) return;
|
|
726
|
+
needsResnapshot.current = false;
|
|
727
|
+
requestAnimationFrame(() => {
|
|
728
|
+
snapshotZoneRects();
|
|
729
|
+
});
|
|
730
|
+
});
|
|
731
|
+
useEffect(() => {
|
|
732
|
+
if (!keyboardNavigation || !focusedIdRef.current || !rootRef.current) return;
|
|
733
|
+
const el = rootRef.current.querySelector(`[data-block-id="${focusedIdRef.current}"]`);
|
|
734
|
+
el?.focus();
|
|
735
|
+
});
|
|
736
|
+
const previewPosition = useMemo(() => {
|
|
737
|
+
if (!showDropPreview || !stateRef.current.virtualState || !stateRef.current.activeId) {
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
const virtualIndex = stateRef.current.virtualState;
|
|
741
|
+
const activeId = stateRef.current.activeId;
|
|
742
|
+
const block = virtualIndex.byId.get(activeId);
|
|
743
|
+
if (!block) return null;
|
|
744
|
+
const parentId = block.parentId ?? null;
|
|
745
|
+
const siblings = virtualIndex.byParent.get(parentId) ?? [];
|
|
746
|
+
const index = siblings.indexOf(activeId);
|
|
747
|
+
return { parentId, index };
|
|
748
|
+
}, [showDropPreview, stateRef.current.virtualState, stateRef.current.activeId]);
|
|
749
|
+
const activeBlock = stateRef.current.activeId ? originalIndex.byId.get(stateRef.current.activeId) ?? null : null;
|
|
750
|
+
const draggedBlock = activeBlock;
|
|
751
|
+
const handleDragStart = useCallback((event) => {
|
|
752
|
+
const id = String(event.active.id);
|
|
753
|
+
const block = blocks.find((b) => b.id === id);
|
|
754
|
+
if (!block) return;
|
|
755
|
+
if (canDrag && !canDrag(block)) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
const dragEvent = {
|
|
759
|
+
block,
|
|
760
|
+
blockId: id
|
|
761
|
+
};
|
|
762
|
+
const result = onDragStart?.(dragEvent);
|
|
763
|
+
if (result === false) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
fromPositionRef.current = getBlockPosition(blocks, id);
|
|
767
|
+
coreStickyRef.current.reset();
|
|
768
|
+
if (multiSelect && selectedIds.has(id)) {
|
|
769
|
+
draggedIdsRef.current = visibleBlockIds.filter((vid) => selectedIds.has(vid));
|
|
770
|
+
} else {
|
|
771
|
+
draggedIdsRef.current = [id];
|
|
772
|
+
if (multiSelect) {
|
|
773
|
+
setSelectedIds(/* @__PURE__ */ new Set([id]));
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (sensorConfig?.hapticFeedback) {
|
|
777
|
+
triggerHaptic();
|
|
778
|
+
}
|
|
779
|
+
stateRef.current.activeId = id;
|
|
780
|
+
stateRef.current.isDragging = true;
|
|
781
|
+
initialBlocksRef.current = [...blocks];
|
|
782
|
+
cachedReorderRef.current = null;
|
|
783
|
+
needsResnapshot.current = true;
|
|
784
|
+
forceRender();
|
|
785
|
+
}, [blocks, canDrag, onDragStart, multiSelect, selectedIds, setSelectedIds, visibleBlockIds, sensorConfig?.hapticFeedback]);
|
|
786
|
+
const handleDragMove = useCallback((event) => {
|
|
787
|
+
if (!onDragMove) return;
|
|
788
|
+
const id = stateRef.current.activeId;
|
|
789
|
+
if (!id) return;
|
|
790
|
+
const block = blocks.find((b) => b.id === id);
|
|
791
|
+
if (!block) return;
|
|
792
|
+
const moveEvent = {
|
|
793
|
+
block,
|
|
794
|
+
blockId: id,
|
|
795
|
+
overZone: stateRef.current.hoverZone,
|
|
796
|
+
coordinates: {
|
|
797
|
+
x: event.delta.x,
|
|
798
|
+
y: event.delta.y
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
debouncedDragMove(moveEvent);
|
|
802
|
+
}, [blocks, onDragMove, debouncedDragMove]);
|
|
803
|
+
const handleDragOver = useCallback((event) => {
|
|
804
|
+
if (!event.over) return;
|
|
805
|
+
const targetZone = String(event.over.id);
|
|
806
|
+
const activeId = stateRef.current.activeId;
|
|
807
|
+
if (!activeId) return;
|
|
808
|
+
const activeBlock2 = blocks.find((b) => b.id === activeId);
|
|
809
|
+
const targetBlockId = extractBlockId(targetZone);
|
|
810
|
+
const targetBlock = blocks.find((b) => b.id === targetBlockId) ?? null;
|
|
811
|
+
if (canDrop && activeBlock2 && !canDrop(activeBlock2, targetZone, targetBlock)) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (maxDepth != null && activeBlock2) {
|
|
815
|
+
const baseIndex2 = computeNormalizedIndex(initialBlocksRef.current, orderingStrategy);
|
|
816
|
+
const testResult = reparentBlockIndex(baseIndex2, activeId, targetZone, containerTypes, orderingStrategy, maxDepth);
|
|
817
|
+
if (testResult === baseIndex2) return;
|
|
818
|
+
}
|
|
819
|
+
if (stateRef.current.hoverZone !== targetZone) {
|
|
820
|
+
const zoneType = getDropZoneType(targetZone);
|
|
821
|
+
const hoverEvent = {
|
|
822
|
+
zoneId: targetZone,
|
|
823
|
+
zoneType,
|
|
824
|
+
targetBlock
|
|
825
|
+
};
|
|
826
|
+
onHoverChange?.(hoverEvent);
|
|
827
|
+
}
|
|
828
|
+
stateRef.current.hoverZone = targetZone;
|
|
829
|
+
const baseIndex = computeNormalizedIndex(initialBlocksRef.current, orderingStrategy);
|
|
830
|
+
const ids = draggedIdsRef.current;
|
|
831
|
+
const updatedIndex = ids.length > 1 ? reparentMultipleBlocks(baseIndex, ids, targetZone, containerTypes, orderingStrategy, maxDepth) : reparentBlockIndex(baseIndex, activeId, targetZone, containerTypes, orderingStrategy, maxDepth);
|
|
832
|
+
const orderedBlocks = buildOrderedBlocks(updatedIndex, containerTypes, orderingStrategy);
|
|
833
|
+
cachedReorderRef.current = { targetId: targetZone, reorderedBlocks: orderedBlocks };
|
|
834
|
+
if (showDropPreview) {
|
|
835
|
+
debouncedSetVirtual(orderedBlocks);
|
|
836
|
+
}
|
|
837
|
+
}, [blocks, containerTypes, debouncedSetVirtual, canDrop, onHoverChange, showDropPreview, maxDepth, orderingStrategy]);
|
|
838
|
+
const handleDragEnd = useCallback((_event) => {
|
|
839
|
+
debouncedSetVirtual.cancel();
|
|
840
|
+
debouncedDragMove.cancel();
|
|
841
|
+
let cached = cachedReorderRef.current;
|
|
842
|
+
const activeId = stateRef.current.activeId;
|
|
843
|
+
const activeBlockData = activeId ? blocks.find((b) => b.id === activeId) : null;
|
|
844
|
+
if (cached && activeBlockData && fromPositionRef.current && onBeforeMove) {
|
|
845
|
+
const operation = {
|
|
846
|
+
block: activeBlockData,
|
|
847
|
+
from: fromPositionRef.current,
|
|
848
|
+
targetZone: cached.targetId
|
|
849
|
+
};
|
|
850
|
+
const result = onBeforeMove(operation);
|
|
851
|
+
if (result === false) {
|
|
852
|
+
stateRef.current.activeId = null;
|
|
853
|
+
stateRef.current.hoverZone = null;
|
|
854
|
+
stateRef.current.virtualState = null;
|
|
855
|
+
stateRef.current.isDragging = false;
|
|
856
|
+
cachedReorderRef.current = null;
|
|
857
|
+
initialBlocksRef.current = [];
|
|
858
|
+
fromPositionRef.current = null;
|
|
859
|
+
snapshotRectsRef.current = null;
|
|
860
|
+
forceRender();
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
if (result && result.targetZone !== cached.targetId) {
|
|
864
|
+
const baseIndex = computeNormalizedIndex(initialBlocksRef.current, orderingStrategy);
|
|
865
|
+
const ids = draggedIdsRef.current;
|
|
866
|
+
const updatedIndex = ids.length > 1 ? reparentMultipleBlocks(baseIndex, ids, result.targetZone, containerTypes, orderingStrategy, maxDepth) : reparentBlockIndex(baseIndex, activeId, result.targetZone, containerTypes, orderingStrategy, maxDepth);
|
|
867
|
+
const reorderedBlocks = buildOrderedBlocks(updatedIndex, containerTypes, orderingStrategy);
|
|
868
|
+
cached = { targetId: result.targetZone, reorderedBlocks };
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
if (activeBlockData) {
|
|
872
|
+
const endEvent = {
|
|
873
|
+
block: activeBlockData,
|
|
874
|
+
blockId: activeId,
|
|
875
|
+
targetZone: cached?.targetId ?? null,
|
|
876
|
+
cancelled: false
|
|
877
|
+
};
|
|
878
|
+
onDragEnd?.(endEvent);
|
|
879
|
+
}
|
|
880
|
+
if (cached && activeBlockData && fromPositionRef.current) {
|
|
881
|
+
const toPosition = getBlockPosition(cached.reorderedBlocks, activeBlockData.id);
|
|
882
|
+
const moveEvent = {
|
|
883
|
+
block: activeBlockData,
|
|
884
|
+
from: fromPositionRef.current,
|
|
885
|
+
to: toPosition,
|
|
886
|
+
blocks: cached.reorderedBlocks,
|
|
887
|
+
movedIds: [...draggedIdsRef.current]
|
|
888
|
+
};
|
|
889
|
+
onBlockMove?.(moveEvent);
|
|
890
|
+
}
|
|
891
|
+
stateRef.current.activeId = null;
|
|
892
|
+
stateRef.current.hoverZone = null;
|
|
893
|
+
stateRef.current.virtualState = null;
|
|
894
|
+
stateRef.current.isDragging = false;
|
|
895
|
+
cachedReorderRef.current = null;
|
|
896
|
+
initialBlocksRef.current = [];
|
|
897
|
+
fromPositionRef.current = null;
|
|
898
|
+
draggedIdsRef.current = [];
|
|
899
|
+
snapshotRectsRef.current = null;
|
|
900
|
+
if (cached && onChange) {
|
|
901
|
+
onChange(cached.reorderedBlocks);
|
|
902
|
+
}
|
|
903
|
+
forceRender();
|
|
904
|
+
}, [blocks, containerTypes, orderingStrategy, debouncedSetVirtual, debouncedDragMove, onChange, onDragEnd, onBlockMove, onBeforeMove]);
|
|
905
|
+
const handleDragCancel = useCallback((_event) => {
|
|
906
|
+
debouncedSetVirtual.cancel();
|
|
907
|
+
debouncedDragMove.cancel();
|
|
908
|
+
const activeId = stateRef.current.activeId;
|
|
909
|
+
const activeBlockData = activeId ? blocks.find((b) => b.id === activeId) : null;
|
|
910
|
+
if (activeBlockData) {
|
|
911
|
+
const cancelEvent = {
|
|
912
|
+
block: activeBlockData,
|
|
913
|
+
blockId: activeId,
|
|
914
|
+
targetZone: null,
|
|
915
|
+
cancelled: true
|
|
916
|
+
};
|
|
917
|
+
onDragCancel?.(cancelEvent);
|
|
918
|
+
onDragEnd?.(cancelEvent);
|
|
919
|
+
}
|
|
920
|
+
stateRef.current.activeId = null;
|
|
921
|
+
stateRef.current.hoverZone = null;
|
|
922
|
+
stateRef.current.virtualState = null;
|
|
923
|
+
stateRef.current.isDragging = false;
|
|
924
|
+
cachedReorderRef.current = null;
|
|
925
|
+
initialBlocksRef.current = [];
|
|
926
|
+
fromPositionRef.current = null;
|
|
927
|
+
draggedIdsRef.current = [];
|
|
928
|
+
snapshotRectsRef.current = null;
|
|
929
|
+
forceRender();
|
|
930
|
+
}, [blocks, debouncedSetVirtual, debouncedDragMove, onDragCancel, onDragEnd]);
|
|
931
|
+
const handleHover = useCallback((zoneId, _parentId) => {
|
|
932
|
+
const activeId = stateRef.current.activeId;
|
|
933
|
+
if (!activeId) return;
|
|
934
|
+
const activeBlockData = blocks.find((b) => b.id === activeId);
|
|
935
|
+
const targetBlockId = extractBlockId(zoneId);
|
|
936
|
+
const targetBlock = blocks.find((b) => b.id === targetBlockId) ?? null;
|
|
937
|
+
if (canDrop && activeBlockData && !canDrop(activeBlockData, zoneId, targetBlock)) {
|
|
938
|
+
return;
|
|
939
|
+
}
|
|
940
|
+
if (maxDepth != null && activeBlockData) {
|
|
941
|
+
const baseIdx = computeNormalizedIndex(initialBlocksRef.current, orderingStrategy);
|
|
942
|
+
const testResult = reparentBlockIndex(baseIdx, activeId, zoneId, containerTypes, orderingStrategy, maxDepth);
|
|
943
|
+
if (testResult === baseIdx) return;
|
|
944
|
+
}
|
|
945
|
+
if (stateRef.current.hoverZone !== zoneId) {
|
|
946
|
+
const zoneType = getDropZoneType(zoneId);
|
|
947
|
+
const hoverEvent = {
|
|
948
|
+
zoneId,
|
|
949
|
+
zoneType,
|
|
950
|
+
targetBlock
|
|
951
|
+
};
|
|
952
|
+
onHoverChange?.(hoverEvent);
|
|
953
|
+
}
|
|
954
|
+
stateRef.current.hoverZone = zoneId;
|
|
955
|
+
const baseIndex = computeNormalizedIndex(initialBlocksRef.current, orderingStrategy);
|
|
956
|
+
const ids = draggedIdsRef.current;
|
|
957
|
+
const updatedIndex = ids.length > 1 ? reparentMultipleBlocks(baseIndex, ids, zoneId, containerTypes, orderingStrategy, maxDepth) : reparentBlockIndex(baseIndex, activeId, zoneId, containerTypes, orderingStrategy, maxDepth);
|
|
958
|
+
const orderedBlocks = buildOrderedBlocks(updatedIndex, containerTypes, orderingStrategy);
|
|
959
|
+
cachedReorderRef.current = { targetId: zoneId, reorderedBlocks: orderedBlocks };
|
|
960
|
+
if (showDropPreview) {
|
|
961
|
+
debouncedSetVirtual(orderedBlocks);
|
|
962
|
+
}
|
|
963
|
+
}, [blocks, containerTypes, orderingStrategy, debouncedSetVirtual, canDrop, onHoverChange, showDropPreview, maxDepth]);
|
|
964
|
+
const handleToggleExpand = useCallback((id) => {
|
|
965
|
+
const newExpanded = stateRef.current.expandedMap[id] === false;
|
|
966
|
+
stateRef.current.expandedMap = {
|
|
967
|
+
...stateRef.current.expandedMap,
|
|
968
|
+
[id]: newExpanded
|
|
969
|
+
};
|
|
970
|
+
const block = blocks.find((b) => b.id === id);
|
|
971
|
+
if (block && onExpandChange) {
|
|
972
|
+
const expandEvent = {
|
|
973
|
+
block,
|
|
974
|
+
blockId: id,
|
|
975
|
+
expanded: newExpanded
|
|
976
|
+
};
|
|
977
|
+
onExpandChange(expandEvent);
|
|
978
|
+
}
|
|
979
|
+
forceRender();
|
|
980
|
+
}, [blocks, onExpandChange]);
|
|
981
|
+
toggleExpandRef.current = handleToggleExpand;
|
|
982
|
+
const virtualContainerRef = useRef(null);
|
|
983
|
+
const [virtualScroll, setVirtualScroll] = useState({ scrollTop: 0, clientHeight: 0 });
|
|
984
|
+
useEffect(() => {
|
|
985
|
+
if (!virtualize) return;
|
|
986
|
+
const el = virtualContainerRef.current;
|
|
987
|
+
if (!el) return;
|
|
988
|
+
setVirtualScroll({ scrollTop: el.scrollTop, clientHeight: el.clientHeight });
|
|
989
|
+
const onScroll = () => {
|
|
990
|
+
setVirtualScroll({ scrollTop: el.scrollTop, clientHeight: el.clientHeight });
|
|
991
|
+
};
|
|
992
|
+
el.addEventListener("scroll", onScroll, { passive: true });
|
|
993
|
+
return () => el.removeEventListener("scroll", onScroll);
|
|
994
|
+
}, [virtualize]);
|
|
995
|
+
const virtualResult = useMemo(() => {
|
|
996
|
+
if (!virtualize) return null;
|
|
997
|
+
const { itemHeight, overscan = 5 } = virtualize;
|
|
998
|
+
const { scrollTop, clientHeight } = virtualScroll;
|
|
999
|
+
const totalHeight = visibleBlockIds.length * itemHeight;
|
|
1000
|
+
const startRaw = Math.floor(scrollTop / itemHeight);
|
|
1001
|
+
const visibleCount = Math.ceil(clientHeight / itemHeight);
|
|
1002
|
+
const start = Math.max(0, startRaw - overscan);
|
|
1003
|
+
const end = Math.min(visibleBlockIds.length - 1, startRaw + visibleCount + overscan);
|
|
1004
|
+
const offsetY = start * itemHeight;
|
|
1005
|
+
const visibleSet = /* @__PURE__ */ new Set();
|
|
1006
|
+
for (let i = start; i <= end; i++) {
|
|
1007
|
+
visibleSet.add(visibleBlockIds[i]);
|
|
1008
|
+
}
|
|
1009
|
+
return { totalHeight, offsetY, visibleSet };
|
|
1010
|
+
}, [virtualize, virtualScroll, visibleBlockIds]);
|
|
1011
|
+
const effectiveCollision = collisionDetection ?? stickyCollisionRef.current;
|
|
1012
|
+
const treeContent = /* @__PURE__ */ jsx(
|
|
1013
|
+
TreeRenderer,
|
|
1014
|
+
{
|
|
1015
|
+
blocks,
|
|
1016
|
+
blocksByParent,
|
|
1017
|
+
parentId: null,
|
|
1018
|
+
activeId: stateRef.current.activeId,
|
|
1019
|
+
expandedMap: stateRef.current.expandedMap,
|
|
1020
|
+
renderers,
|
|
1021
|
+
containerTypes,
|
|
1022
|
+
onHover: handleHover,
|
|
1023
|
+
onToggleExpand: handleToggleExpand,
|
|
1024
|
+
dropZoneClassName,
|
|
1025
|
+
dropZoneActiveClassName,
|
|
1026
|
+
indentClassName,
|
|
1027
|
+
rootClassName: className,
|
|
1028
|
+
canDrag,
|
|
1029
|
+
previewPosition,
|
|
1030
|
+
draggedBlock,
|
|
1031
|
+
focusedId: keyboardNavigation ? focusedIdRef.current : void 0,
|
|
1032
|
+
selectedIds: multiSelect ? selectedIds : void 0,
|
|
1033
|
+
onBlockClick: multiSelect ? handleBlockClick : void 0,
|
|
1034
|
+
animation,
|
|
1035
|
+
virtualVisibleIds: virtualResult?.visibleSet ?? null
|
|
1036
|
+
}
|
|
1037
|
+
);
|
|
1038
|
+
return /* @__PURE__ */ jsxs(
|
|
1039
|
+
DndContext,
|
|
1040
|
+
{
|
|
1041
|
+
sensors,
|
|
1042
|
+
collisionDetection: effectiveCollision,
|
|
1043
|
+
onDragStart: handleDragStart,
|
|
1044
|
+
onDragMove: handleDragMove,
|
|
1045
|
+
onDragOver: handleDragOver,
|
|
1046
|
+
onDragEnd: handleDragEnd,
|
|
1047
|
+
onDragCancel: handleDragCancel,
|
|
1048
|
+
children: [
|
|
1049
|
+
virtualize ? /* @__PURE__ */ jsx(
|
|
1050
|
+
"div",
|
|
1051
|
+
{
|
|
1052
|
+
ref: (el) => {
|
|
1053
|
+
virtualContainerRef.current = el;
|
|
1054
|
+
rootRef.current = el;
|
|
1055
|
+
},
|
|
1056
|
+
className,
|
|
1057
|
+
style: { minWidth: 0, overflow: "auto", position: "relative" },
|
|
1058
|
+
onKeyDown: keyboardNavigation ? handleKeyDown : void 0,
|
|
1059
|
+
role: keyboardNavigation ? "tree" : void 0,
|
|
1060
|
+
children: /* @__PURE__ */ jsx("div", { style: { height: virtualResult.totalHeight, position: "relative" }, children: /* @__PURE__ */ jsx("div", { style: { position: "absolute", top: virtualResult.offsetY, left: 0, right: 0 }, children: treeContent }) })
|
|
1061
|
+
}
|
|
1062
|
+
) : /* @__PURE__ */ jsx(
|
|
1063
|
+
"div",
|
|
1064
|
+
{
|
|
1065
|
+
ref: rootRef,
|
|
1066
|
+
className,
|
|
1067
|
+
style: { minWidth: 0 },
|
|
1068
|
+
onKeyDown: keyboardNavigation ? handleKeyDown : void 0,
|
|
1069
|
+
role: keyboardNavigation ? "tree" : void 0,
|
|
1070
|
+
children: treeContent
|
|
1071
|
+
}
|
|
1072
|
+
),
|
|
1073
|
+
/* @__PURE__ */ jsx(DragOverlay, { activeBlock, selectedCount: multiSelect ? selectedIds.size : 0, children: dragOverlay })
|
|
1074
|
+
]
|
|
1075
|
+
}
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
function BlockTreeSSR({ fallback = null, ...props }) {
|
|
1079
|
+
const [mounted, setMounted] = useState(false);
|
|
1080
|
+
useEffect(() => {
|
|
1081
|
+
setMounted(true);
|
|
1082
|
+
}, []);
|
|
1083
|
+
if (!mounted) {
|
|
1084
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: fallback });
|
|
1085
|
+
}
|
|
1086
|
+
return /* @__PURE__ */ jsx(BlockTree, { ...props });
|
|
1087
|
+
}
|
|
1088
|
+
function createBlockState() {
|
|
1089
|
+
const BlockContext = createContext(null);
|
|
1090
|
+
function useBlockState() {
|
|
1091
|
+
const ctx = useContext(BlockContext);
|
|
1092
|
+
if (!ctx) throw new Error("useBlockState must be used inside BlockStateProvider");
|
|
1093
|
+
return ctx;
|
|
1094
|
+
}
|
|
1095
|
+
function BlockStateProvider({
|
|
1096
|
+
children,
|
|
1097
|
+
initialBlocks = [],
|
|
1098
|
+
containerTypes = [],
|
|
1099
|
+
onChange,
|
|
1100
|
+
orderingStrategy = "integer",
|
|
1101
|
+
maxDepth,
|
|
1102
|
+
onBlockAdd,
|
|
1103
|
+
onBlockDelete
|
|
1104
|
+
}) {
|
|
1105
|
+
const reducerWithOptions = useCallback(
|
|
1106
|
+
(state2, action) => blockReducer(state2, action, containerTypes, orderingStrategy, maxDepth),
|
|
1107
|
+
[containerTypes, orderingStrategy, maxDepth]
|
|
1108
|
+
);
|
|
1109
|
+
const [state, dispatch] = useReducer(
|
|
1110
|
+
reducerWithOptions,
|
|
1111
|
+
computeNormalizedIndex(initialBlocks, orderingStrategy)
|
|
1112
|
+
);
|
|
1113
|
+
const blocks = useMemo(() => {
|
|
1114
|
+
const result = [];
|
|
1115
|
+
const walk = (parentId) => {
|
|
1116
|
+
const children2 = state.byParent.get(parentId) ?? [];
|
|
1117
|
+
for (let i = 0; i < children2.length; i++) {
|
|
1118
|
+
const id = children2[i];
|
|
1119
|
+
const b = state.byId.get(id);
|
|
1120
|
+
if (b) {
|
|
1121
|
+
result.push(orderingStrategy === "fractional" ? b : { ...b, order: i });
|
|
1122
|
+
if (containerTypes.includes(b.type)) walk(b.id);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
walk(null);
|
|
1127
|
+
return result;
|
|
1128
|
+
}, [state, containerTypes, orderingStrategy]);
|
|
1129
|
+
useMemo(() => {
|
|
1130
|
+
onChange?.(blocks);
|
|
1131
|
+
}, [blocks, onChange]);
|
|
1132
|
+
const blockMap = useMemo(() => state.byId, [state]);
|
|
1133
|
+
const childrenMap = useMemo(() => {
|
|
1134
|
+
const map = /* @__PURE__ */ new Map();
|
|
1135
|
+
for (const [parentId, ids] of state.byParent.entries()) {
|
|
1136
|
+
map.set(
|
|
1137
|
+
parentId,
|
|
1138
|
+
ids.map((id) => state.byId.get(id)).filter(Boolean)
|
|
1139
|
+
);
|
|
1140
|
+
}
|
|
1141
|
+
return map;
|
|
1142
|
+
}, [state]);
|
|
1143
|
+
const indexMap = useMemo(() => {
|
|
1144
|
+
const map = /* @__PURE__ */ new Map();
|
|
1145
|
+
for (const ids of state.byParent.values()) {
|
|
1146
|
+
ids.forEach((id, index) => {
|
|
1147
|
+
map.set(id, index);
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
return map;
|
|
1151
|
+
}, [state]);
|
|
1152
|
+
const createItem = useCallback(
|
|
1153
|
+
(type, parentId = null) => {
|
|
1154
|
+
const siblings = state.byParent.get(parentId) ?? [];
|
|
1155
|
+
let order = siblings.length;
|
|
1156
|
+
if (orderingStrategy === "fractional") {
|
|
1157
|
+
const lastId = siblings[siblings.length - 1];
|
|
1158
|
+
const lastOrder = lastId ? String(state.byId.get(lastId).order) : null;
|
|
1159
|
+
order = generateKeyBetween(lastOrder, null);
|
|
1160
|
+
}
|
|
1161
|
+
const newItem = { id: generateId(), type, parentId, order };
|
|
1162
|
+
dispatch({ type: "ADD_ITEM", payload: newItem });
|
|
1163
|
+
onBlockAdd?.({ block: newItem, parentId, index: siblings.length });
|
|
1164
|
+
return newItem;
|
|
1165
|
+
},
|
|
1166
|
+
[state, orderingStrategy, onBlockAdd]
|
|
1167
|
+
);
|
|
1168
|
+
const insertItem = useCallback(
|
|
1169
|
+
(type, referenceId, position) => {
|
|
1170
|
+
const referenceBlock = state.byId.get(referenceId);
|
|
1171
|
+
if (!referenceBlock) throw new Error(`Reference block ${referenceId} not found`);
|
|
1172
|
+
const parentId = referenceBlock.parentId ?? null;
|
|
1173
|
+
const siblings = state.byParent.get(parentId) ?? [];
|
|
1174
|
+
const index = siblings.indexOf(referenceId);
|
|
1175
|
+
const insertIndex = position === "before" ? index : index + 1;
|
|
1176
|
+
let order = insertIndex;
|
|
1177
|
+
if (orderingStrategy === "fractional") {
|
|
1178
|
+
const prevId = insertIndex > 0 ? siblings[insertIndex - 1] : null;
|
|
1179
|
+
const nextId = insertIndex < siblings.length ? siblings[insertIndex] : null;
|
|
1180
|
+
const prevOrder = prevId ? String(state.byId.get(prevId).order) : null;
|
|
1181
|
+
const nextOrder = nextId ? String(state.byId.get(nextId).order) : null;
|
|
1182
|
+
order = generateKeyBetween(prevOrder, nextOrder);
|
|
1183
|
+
}
|
|
1184
|
+
const newItem = { id: generateId(), type, parentId, order };
|
|
1185
|
+
dispatch({
|
|
1186
|
+
type: "INSERT_ITEM",
|
|
1187
|
+
payload: { item: newItem, parentId, index: insertIndex }
|
|
1188
|
+
});
|
|
1189
|
+
onBlockAdd?.({ block: newItem, parentId, index: insertIndex });
|
|
1190
|
+
return newItem;
|
|
1191
|
+
},
|
|
1192
|
+
[state, orderingStrategy, onBlockAdd]
|
|
1193
|
+
);
|
|
1194
|
+
const deleteItem = useCallback((id) => {
|
|
1195
|
+
const block = state.byId.get(id);
|
|
1196
|
+
if (block && onBlockDelete) {
|
|
1197
|
+
const deletedIds = [...getDescendantIds(state, id)];
|
|
1198
|
+
onBlockDelete({ block, deletedIds, parentId: block.parentId });
|
|
1199
|
+
}
|
|
1200
|
+
dispatch({ type: "DELETE_ITEM", payload: { id } });
|
|
1201
|
+
}, [state, onBlockDelete]);
|
|
1202
|
+
const moveItem = useCallback((activeId, targetZone) => {
|
|
1203
|
+
dispatch({ type: "MOVE_ITEM", payload: { activeId, targetZone } });
|
|
1204
|
+
}, []);
|
|
1205
|
+
const setAll = useCallback((all) => {
|
|
1206
|
+
dispatch({ type: "SET_ALL", payload: all });
|
|
1207
|
+
}, []);
|
|
1208
|
+
const value = useMemo(
|
|
1209
|
+
() => ({
|
|
1210
|
+
blocks,
|
|
1211
|
+
blockMap,
|
|
1212
|
+
childrenMap,
|
|
1213
|
+
indexMap,
|
|
1214
|
+
normalizedIndex: state,
|
|
1215
|
+
createItem,
|
|
1216
|
+
insertItem,
|
|
1217
|
+
deleteItem,
|
|
1218
|
+
moveItem,
|
|
1219
|
+
setAll
|
|
1220
|
+
}),
|
|
1221
|
+
[
|
|
1222
|
+
blocks,
|
|
1223
|
+
blockMap,
|
|
1224
|
+
childrenMap,
|
|
1225
|
+
indexMap,
|
|
1226
|
+
state,
|
|
1227
|
+
createItem,
|
|
1228
|
+
insertItem,
|
|
1229
|
+
deleteItem,
|
|
1230
|
+
moveItem,
|
|
1231
|
+
setAll
|
|
1232
|
+
]
|
|
1233
|
+
);
|
|
1234
|
+
return /* @__PURE__ */ jsx(BlockContext.Provider, { value, children });
|
|
1235
|
+
}
|
|
1236
|
+
return {
|
|
1237
|
+
BlockStateProvider,
|
|
1238
|
+
useBlockState
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
function createTreeState(options = {}) {
|
|
1242
|
+
const { previewDebounce = 150, containerTypes = [] } = options;
|
|
1243
|
+
const TreeContext = createContext(null);
|
|
1244
|
+
function useTreeState() {
|
|
1245
|
+
const ctx = useContext(TreeContext);
|
|
1246
|
+
if (!ctx) throw new Error("useTreeState must be used inside TreeStateProvider");
|
|
1247
|
+
return ctx;
|
|
1248
|
+
}
|
|
1249
|
+
function TreeStateProvider({ children, blocks, blockMap }) {
|
|
1250
|
+
const [activeId, setActiveId] = useState(null);
|
|
1251
|
+
const [hoverZone, setHoverZone] = useState(null);
|
|
1252
|
+
const [virtualState, setVirtualState] = useState(null);
|
|
1253
|
+
const [expandedMap, dispatchExpand] = useReducer(expandReducer, {});
|
|
1254
|
+
const initialBlocksRef = useRef([]);
|
|
1255
|
+
const cachedReorderRef = useRef(null);
|
|
1256
|
+
const activeBlock = useMemo(() => {
|
|
1257
|
+
if (!activeId) return null;
|
|
1258
|
+
return blockMap.get(activeId) ?? null;
|
|
1259
|
+
}, [activeId, blockMap]);
|
|
1260
|
+
const debouncedSetVirtualBlocks = useMemo(
|
|
1261
|
+
() => debounce((newBlocks) => {
|
|
1262
|
+
if (!newBlocks) {
|
|
1263
|
+
setVirtualState(null);
|
|
1264
|
+
} else {
|
|
1265
|
+
setVirtualState(computeNormalizedIndex(newBlocks));
|
|
1266
|
+
}
|
|
1267
|
+
}, previewDebounce),
|
|
1268
|
+
[previewDebounce]
|
|
1269
|
+
);
|
|
1270
|
+
const effectiveState = useMemo(() => {
|
|
1271
|
+
return virtualState ?? computeNormalizedIndex(blocks);
|
|
1272
|
+
}, [virtualState, blocks]);
|
|
1273
|
+
const effectiveBlocks = useMemo(() => {
|
|
1274
|
+
return buildOrderedBlocks(effectiveState, containerTypes);
|
|
1275
|
+
}, [effectiveState, containerTypes]);
|
|
1276
|
+
const blocksByParent = useMemo(() => {
|
|
1277
|
+
const map = /* @__PURE__ */ new Map();
|
|
1278
|
+
for (const [parentId, ids] of effectiveState.byParent.entries()) {
|
|
1279
|
+
map.set(
|
|
1280
|
+
parentId,
|
|
1281
|
+
ids.map((id) => effectiveState.byId.get(id)).filter(Boolean)
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
return map;
|
|
1285
|
+
}, [effectiveState]);
|
|
1286
|
+
const handleDragStart = useCallback(
|
|
1287
|
+
(id) => {
|
|
1288
|
+
setActiveId(id);
|
|
1289
|
+
if (id) {
|
|
1290
|
+
initialBlocksRef.current = [...blocks];
|
|
1291
|
+
cachedReorderRef.current = null;
|
|
1292
|
+
}
|
|
1293
|
+
},
|
|
1294
|
+
[blocks]
|
|
1295
|
+
);
|
|
1296
|
+
const handleDragOver = useCallback(
|
|
1297
|
+
(targetZone) => {
|
|
1298
|
+
if (!activeId) return;
|
|
1299
|
+
setHoverZone(targetZone);
|
|
1300
|
+
const baseIndex = computeNormalizedIndex(initialBlocksRef.current);
|
|
1301
|
+
const updatedIndex = reparentBlockIndex(baseIndex, activeId, targetZone, containerTypes);
|
|
1302
|
+
const orderedBlocks = buildOrderedBlocks(updatedIndex, containerTypes);
|
|
1303
|
+
cachedReorderRef.current = { targetId: targetZone, reorderedBlocks: orderedBlocks };
|
|
1304
|
+
debouncedSetVirtualBlocks(orderedBlocks);
|
|
1305
|
+
},
|
|
1306
|
+
[activeId, debouncedSetVirtualBlocks, containerTypes]
|
|
1307
|
+
);
|
|
1308
|
+
useCallback(() => {
|
|
1309
|
+
debouncedSetVirtualBlocks.cancel();
|
|
1310
|
+
setVirtualState(null);
|
|
1311
|
+
setActiveId(null);
|
|
1312
|
+
setHoverZone(null);
|
|
1313
|
+
const result = cachedReorderRef.current;
|
|
1314
|
+
cachedReorderRef.current = null;
|
|
1315
|
+
initialBlocksRef.current = [];
|
|
1316
|
+
return result;
|
|
1317
|
+
}, [debouncedSetVirtualBlocks]);
|
|
1318
|
+
const handleHover = useCallback(
|
|
1319
|
+
(zoneId, _parentId) => {
|
|
1320
|
+
if (!activeId) return;
|
|
1321
|
+
handleDragOver(zoneId);
|
|
1322
|
+
},
|
|
1323
|
+
[activeId, handleDragOver]
|
|
1324
|
+
);
|
|
1325
|
+
const toggleExpand = useCallback((id) => {
|
|
1326
|
+
dispatchExpand({ type: "TOGGLE", id });
|
|
1327
|
+
}, []);
|
|
1328
|
+
const setExpandAll = useCallback(
|
|
1329
|
+
(expanded) => {
|
|
1330
|
+
const containerIds = blocks.filter((b) => containerTypes.includes(b.type)).map((b) => b.id);
|
|
1331
|
+
dispatchExpand({ type: "SET_ALL", expanded, ids: containerIds });
|
|
1332
|
+
},
|
|
1333
|
+
[blocks, containerTypes]
|
|
1334
|
+
);
|
|
1335
|
+
useEffect(() => {
|
|
1336
|
+
return () => {
|
|
1337
|
+
debouncedSetVirtualBlocks.cancel();
|
|
1338
|
+
};
|
|
1339
|
+
}, [debouncedSetVirtualBlocks]);
|
|
1340
|
+
const value = useMemo(
|
|
1341
|
+
() => ({
|
|
1342
|
+
activeId,
|
|
1343
|
+
activeBlock,
|
|
1344
|
+
hoverZone,
|
|
1345
|
+
expandedMap,
|
|
1346
|
+
effectiveBlocks,
|
|
1347
|
+
blocksByParent,
|
|
1348
|
+
setActiveId: handleDragStart,
|
|
1349
|
+
setHoverZone,
|
|
1350
|
+
toggleExpand,
|
|
1351
|
+
setExpandAll,
|
|
1352
|
+
handleHover
|
|
1353
|
+
}),
|
|
1354
|
+
[
|
|
1355
|
+
activeId,
|
|
1356
|
+
activeBlock,
|
|
1357
|
+
hoverZone,
|
|
1358
|
+
expandedMap,
|
|
1359
|
+
effectiveBlocks,
|
|
1360
|
+
blocksByParent,
|
|
1361
|
+
handleDragStart,
|
|
1362
|
+
toggleExpand,
|
|
1363
|
+
setExpandAll,
|
|
1364
|
+
handleHover
|
|
1365
|
+
]
|
|
1366
|
+
);
|
|
1367
|
+
return /* @__PURE__ */ jsx(TreeContext.Provider, { value, children });
|
|
1368
|
+
}
|
|
1369
|
+
return {
|
|
1370
|
+
TreeStateProvider,
|
|
1371
|
+
useTreeState
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
function useBlockHistory(initialBlocks, options = {}) {
|
|
1375
|
+
const { maxSteps = 50 } = options;
|
|
1376
|
+
const [state, dispatch] = useReducer(historyReducer, {
|
|
1377
|
+
past: [],
|
|
1378
|
+
present: initialBlocks,
|
|
1379
|
+
future: []
|
|
1380
|
+
});
|
|
1381
|
+
const set = useCallback(
|
|
1382
|
+
(blocks) => dispatch({ type: "SET", payload: blocks, maxSteps }),
|
|
1383
|
+
[maxSteps]
|
|
1384
|
+
);
|
|
1385
|
+
const undo = useCallback(() => dispatch({ type: "UNDO" }), []);
|
|
1386
|
+
const redo = useCallback(() => dispatch({ type: "REDO" }), []);
|
|
1387
|
+
return {
|
|
1388
|
+
blocks: state.present,
|
|
1389
|
+
set,
|
|
1390
|
+
undo,
|
|
1391
|
+
redo,
|
|
1392
|
+
canUndo: state.past.length > 0,
|
|
1393
|
+
canRedo: state.future.length > 0
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
function useLayoutAnimation(containerRef, options = {}) {
|
|
1397
|
+
const {
|
|
1398
|
+
duration = 200,
|
|
1399
|
+
easing = "ease",
|
|
1400
|
+
selector = "[data-block-id]"
|
|
1401
|
+
} = options;
|
|
1402
|
+
const prevPositions = useRef(/* @__PURE__ */ new Map());
|
|
1403
|
+
useLayoutEffect(() => {
|
|
1404
|
+
const container = containerRef.current;
|
|
1405
|
+
if (!container) return;
|
|
1406
|
+
const children = container.querySelectorAll(selector);
|
|
1407
|
+
const currentPositions = /* @__PURE__ */ new Map();
|
|
1408
|
+
children.forEach((el) => {
|
|
1409
|
+
const id = el.dataset.blockId;
|
|
1410
|
+
if (id) {
|
|
1411
|
+
currentPositions.set(id, el.getBoundingClientRect());
|
|
1412
|
+
}
|
|
1413
|
+
});
|
|
1414
|
+
children.forEach((el) => {
|
|
1415
|
+
const htmlEl = el;
|
|
1416
|
+
const id = htmlEl.dataset.blockId;
|
|
1417
|
+
if (!id) return;
|
|
1418
|
+
const prev = prevPositions.current.get(id);
|
|
1419
|
+
const curr = currentPositions.get(id);
|
|
1420
|
+
if (!prev || !curr) return;
|
|
1421
|
+
const deltaY = prev.top - curr.top;
|
|
1422
|
+
const deltaX = prev.left - curr.left;
|
|
1423
|
+
if (deltaY === 0 && deltaX === 0) return;
|
|
1424
|
+
htmlEl.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
|
|
1425
|
+
htmlEl.style.transition = "none";
|
|
1426
|
+
requestAnimationFrame(() => {
|
|
1427
|
+
htmlEl.style.transition = `transform ${duration}ms ${easing}`;
|
|
1428
|
+
htmlEl.style.transform = "";
|
|
1429
|
+
const onEnd = () => {
|
|
1430
|
+
htmlEl.style.transition = "";
|
|
1431
|
+
htmlEl.removeEventListener("transitionend", onEnd);
|
|
1432
|
+
};
|
|
1433
|
+
htmlEl.addEventListener("transitionend", onEnd);
|
|
1434
|
+
});
|
|
1435
|
+
});
|
|
1436
|
+
prevPositions.current = currentPositions;
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
function useVirtualTree({
|
|
1440
|
+
containerRef,
|
|
1441
|
+
itemCount,
|
|
1442
|
+
itemHeight,
|
|
1443
|
+
overscan = 5
|
|
1444
|
+
}) {
|
|
1445
|
+
const [scrollTop, setScrollTop] = useState(0);
|
|
1446
|
+
const [containerHeight, setContainerHeight] = useState(0);
|
|
1447
|
+
const rafId = useRef(0);
|
|
1448
|
+
const handleScroll = useCallback(() => {
|
|
1449
|
+
cancelAnimationFrame(rafId.current);
|
|
1450
|
+
rafId.current = requestAnimationFrame(() => {
|
|
1451
|
+
const el = containerRef.current;
|
|
1452
|
+
if (el) {
|
|
1453
|
+
setScrollTop(el.scrollTop);
|
|
1454
|
+
setContainerHeight(el.clientHeight);
|
|
1455
|
+
}
|
|
1456
|
+
});
|
|
1457
|
+
}, [containerRef]);
|
|
1458
|
+
useEffect(() => {
|
|
1459
|
+
const el = containerRef.current;
|
|
1460
|
+
if (!el) return;
|
|
1461
|
+
setScrollTop(el.scrollTop);
|
|
1462
|
+
setContainerHeight(el.clientHeight);
|
|
1463
|
+
el.addEventListener("scroll", handleScroll, { passive: true });
|
|
1464
|
+
return () => {
|
|
1465
|
+
el.removeEventListener("scroll", handleScroll);
|
|
1466
|
+
cancelAnimationFrame(rafId.current);
|
|
1467
|
+
};
|
|
1468
|
+
}, [containerRef, handleScroll]);
|
|
1469
|
+
const totalHeight = itemCount * itemHeight;
|
|
1470
|
+
const startRaw = Math.floor(scrollTop / itemHeight);
|
|
1471
|
+
const visibleCount = Math.ceil(containerHeight / itemHeight);
|
|
1472
|
+
const start = Math.max(0, startRaw - overscan);
|
|
1473
|
+
const end = Math.min(itemCount - 1, startRaw + visibleCount + overscan);
|
|
1474
|
+
const offsetY = start * itemHeight;
|
|
1475
|
+
return {
|
|
1476
|
+
visibleRange: { start, end },
|
|
1477
|
+
totalHeight,
|
|
1478
|
+
offsetY
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
1481
|
+
var MAX_EVENTS = 100;
|
|
1482
|
+
function useDevToolsCallbacks() {
|
|
1483
|
+
const [events, setEvents] = useState([]);
|
|
1484
|
+
const nextIdRef = useRef(1);
|
|
1485
|
+
const addEvent = useCallback((type, summary) => {
|
|
1486
|
+
const entry = {
|
|
1487
|
+
id: nextIdRef.current++,
|
|
1488
|
+
timestamp: Date.now(),
|
|
1489
|
+
type,
|
|
1490
|
+
summary
|
|
1491
|
+
};
|
|
1492
|
+
setEvents((prev) => [entry, ...prev].slice(0, MAX_EVENTS));
|
|
1493
|
+
}, []);
|
|
1494
|
+
const clearEvents = useCallback(() => {
|
|
1495
|
+
setEvents([]);
|
|
1496
|
+
}, []);
|
|
1497
|
+
const callbacks = useMemo(() => ({
|
|
1498
|
+
onDragStart: (event) => {
|
|
1499
|
+
addEvent("dragStart", `Started dragging "${event.blockId}"`);
|
|
1500
|
+
},
|
|
1501
|
+
onDragEnd: (event) => {
|
|
1502
|
+
if (event.cancelled) {
|
|
1503
|
+
addEvent("dragEnd", `Cancelled drag of "${event.blockId}"`);
|
|
1504
|
+
} else {
|
|
1505
|
+
addEvent("dragEnd", `Dropped "${event.blockId}" at ${event.targetZone ?? "none"}`);
|
|
1506
|
+
}
|
|
1507
|
+
},
|
|
1508
|
+
onBlockMove: (event) => {
|
|
1509
|
+
const fromStr = `parent=${event.from.parentId ?? "root"}[${event.from.index}]`;
|
|
1510
|
+
const toStr = `parent=${event.to.parentId ?? "root"}[${event.to.index}]`;
|
|
1511
|
+
const ids = event.movedIds.length > 1 ? ` (${event.movedIds.length} blocks)` : "";
|
|
1512
|
+
addEvent("blockMove", `Moved "${event.block.id}" from ${fromStr} to ${toStr}${ids}`);
|
|
1513
|
+
},
|
|
1514
|
+
onExpandChange: (event) => {
|
|
1515
|
+
addEvent("expandChange", `${event.expanded ? "Expanded" : "Collapsed"} "${event.blockId}"`);
|
|
1516
|
+
},
|
|
1517
|
+
onHoverChange: (event) => {
|
|
1518
|
+
if (event.zoneId) {
|
|
1519
|
+
addEvent("hoverChange", `Hovering over zone "${event.zoneId}"`);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
}), [addEvent]);
|
|
1523
|
+
return { callbacks, events, clearEvents };
|
|
1524
|
+
}
|
|
1525
|
+
function DevToolsLogo({ size = 20 }) {
|
|
1526
|
+
return /* @__PURE__ */ jsxs(
|
|
1527
|
+
"svg",
|
|
1528
|
+
{
|
|
1529
|
+
width: size,
|
|
1530
|
+
height: size,
|
|
1531
|
+
viewBox: "0 0 24 24",
|
|
1532
|
+
fill: "none",
|
|
1533
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1534
|
+
children: [
|
|
1535
|
+
/* @__PURE__ */ jsx("rect", { x: "2", y: "2", width: "8", height: "5", rx: "1", fill: "#3b82f6", opacity: "0.9" }),
|
|
1536
|
+
/* @__PURE__ */ jsx("rect", { x: "8", y: "10", width: "8", height: "5", rx: "1", fill: "#10b981", opacity: "0.9" }),
|
|
1537
|
+
/* @__PURE__ */ jsx("rect", { x: "14", y: "18", width: "8", height: "5", rx: "1", fill: "#f59e0b", opacity: "0.9" }),
|
|
1538
|
+
/* @__PURE__ */ jsx("path", { d: "M6 7 L6 10 L8 10", stroke: "#888", strokeWidth: "1.5", fill: "none", opacity: "0.6" }),
|
|
1539
|
+
/* @__PURE__ */ jsx("path", { d: "M12 15 L12 18 L14 18", stroke: "#888", strokeWidth: "1.5", fill: "none", opacity: "0.6" })
|
|
1540
|
+
]
|
|
1541
|
+
}
|
|
1542
|
+
);
|
|
1543
|
+
}
|
|
1544
|
+
function computeDiffMap(prev, next) {
|
|
1545
|
+
const prevMap = new Map(prev.map((b) => [b.id, b]));
|
|
1546
|
+
const changeMap = /* @__PURE__ */ new Map();
|
|
1547
|
+
for (const block of next) {
|
|
1548
|
+
const prevBlock = prevMap.get(block.id);
|
|
1549
|
+
if (!prevBlock) {
|
|
1550
|
+
changeMap.set(block.id, "added");
|
|
1551
|
+
} else if (prevBlock.parentId !== block.parentId || prevBlock.order !== block.order) {
|
|
1552
|
+
changeMap.set(block.id, "moved");
|
|
1553
|
+
} else {
|
|
1554
|
+
changeMap.set(block.id, "unchanged");
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return changeMap;
|
|
1558
|
+
}
|
|
1559
|
+
function buildDiffTree(blocks, changeMap) {
|
|
1560
|
+
const result = [];
|
|
1561
|
+
const byParent = /* @__PURE__ */ new Map();
|
|
1562
|
+
for (const block of blocks) {
|
|
1563
|
+
const key = block.parentId ?? null;
|
|
1564
|
+
const list = byParent.get(key) ?? [];
|
|
1565
|
+
list.push(block);
|
|
1566
|
+
byParent.set(key, list);
|
|
1567
|
+
}
|
|
1568
|
+
function walk(parentId, depth) {
|
|
1569
|
+
const children = byParent.get(parentId) ?? [];
|
|
1570
|
+
children.sort((a, b) => {
|
|
1571
|
+
const ao = a.order, bo = b.order;
|
|
1572
|
+
if (typeof ao === "number" && typeof bo === "number") return ao - bo;
|
|
1573
|
+
return String(ao) < String(bo) ? -1 : String(ao) > String(bo) ? 1 : 0;
|
|
1574
|
+
});
|
|
1575
|
+
for (const block of children) {
|
|
1576
|
+
result.push({ block, changeType: changeMap.get(block.id) ?? "unchanged", depth });
|
|
1577
|
+
walk(block.id, depth + 1);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
walk(null, 0);
|
|
1581
|
+
return result;
|
|
1582
|
+
}
|
|
1583
|
+
var TYPE_COLORS = {
|
|
1584
|
+
dragStart: "#3b82f6",
|
|
1585
|
+
dragEnd: "#10b981",
|
|
1586
|
+
blockMove: "#f59e0b",
|
|
1587
|
+
expandChange: "#8b5cf6",
|
|
1588
|
+
hoverChange: "#6b7280"
|
|
1589
|
+
};
|
|
1590
|
+
var TYPE_LABELS = {
|
|
1591
|
+
dragStart: "DRAG",
|
|
1592
|
+
dragEnd: "DROP",
|
|
1593
|
+
blockMove: "MOVE",
|
|
1594
|
+
expandChange: "EXPAND",
|
|
1595
|
+
hoverChange: "HOVER"
|
|
1596
|
+
};
|
|
1597
|
+
var TYPE_TOOLTIPS = {
|
|
1598
|
+
dragStart: "onDragStart -- a block was picked up",
|
|
1599
|
+
dragEnd: "onDragEnd -- a block was dropped or drag was cancelled",
|
|
1600
|
+
blockMove: "onBlockMove -- a block was reparented to a new position",
|
|
1601
|
+
expandChange: "onExpandChange -- a container was expanded or collapsed",
|
|
1602
|
+
hoverChange: "onHoverChange -- the pointer entered a drop zone"
|
|
1603
|
+
};
|
|
1604
|
+
var DEFAULT_WIDTH = 340;
|
|
1605
|
+
var DEFAULT_HEIGHT = 420;
|
|
1606
|
+
var DIFF_EXTRA_WIDTH = 300;
|
|
1607
|
+
var MIN_WIDTH = 280;
|
|
1608
|
+
var MIN_HEIGHT = 200;
|
|
1609
|
+
var BTN_SIZE = 40;
|
|
1610
|
+
var BTN_MARGIN = 16;
|
|
1611
|
+
var STORAGE_KEY = "dnd-devtools-position";
|
|
1612
|
+
var BTN_DRAG_THRESHOLD = 5;
|
|
1613
|
+
function cornerToXY(corner) {
|
|
1614
|
+
const vw = typeof window !== "undefined" ? window.innerWidth : 1024;
|
|
1615
|
+
const vh = typeof window !== "undefined" ? window.innerHeight : 768;
|
|
1616
|
+
switch (corner) {
|
|
1617
|
+
case "top-left":
|
|
1618
|
+
return { x: BTN_MARGIN, y: BTN_MARGIN };
|
|
1619
|
+
case "top-right":
|
|
1620
|
+
return { x: vw - BTN_MARGIN - BTN_SIZE, y: BTN_MARGIN };
|
|
1621
|
+
case "bottom-right":
|
|
1622
|
+
return { x: vw - BTN_MARGIN - BTN_SIZE, y: vh - BTN_MARGIN - BTN_SIZE };
|
|
1623
|
+
case "bottom-left":
|
|
1624
|
+
default:
|
|
1625
|
+
return { x: BTN_MARGIN, y: vh - BTN_MARGIN - BTN_SIZE };
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
function xyToCorner(x, y) {
|
|
1629
|
+
const vw = typeof window !== "undefined" ? window.innerWidth : 1024;
|
|
1630
|
+
const vh = typeof window !== "undefined" ? window.innerHeight : 768;
|
|
1631
|
+
const isLeft = x + BTN_SIZE / 2 < vw / 2;
|
|
1632
|
+
const isTop = y + BTN_SIZE / 2 < vh / 2;
|
|
1633
|
+
if (isTop && isLeft) return "top-left";
|
|
1634
|
+
if (isTop) return "top-right";
|
|
1635
|
+
if (isLeft) return "bottom-left";
|
|
1636
|
+
return "bottom-right";
|
|
1637
|
+
}
|
|
1638
|
+
function loadStoredPosition() {
|
|
1639
|
+
if (typeof window === "undefined") return null;
|
|
1640
|
+
try {
|
|
1641
|
+
const v = localStorage.getItem(STORAGE_KEY);
|
|
1642
|
+
if (v === "bottom-left" || v === "bottom-right" || v === "top-left" || v === "top-right") return v;
|
|
1643
|
+
} catch {
|
|
1644
|
+
}
|
|
1645
|
+
return null;
|
|
1646
|
+
}
|
|
1647
|
+
function savePosition(corner) {
|
|
1648
|
+
try {
|
|
1649
|
+
localStorage.setItem(STORAGE_KEY, corner);
|
|
1650
|
+
} catch {
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
function computeCardOrigin(corner, width, height) {
|
|
1654
|
+
const vw = typeof window !== "undefined" ? window.innerWidth : 1024;
|
|
1655
|
+
const vh = typeof window !== "undefined" ? window.innerHeight : 768;
|
|
1656
|
+
let x;
|
|
1657
|
+
let y;
|
|
1658
|
+
switch (corner) {
|
|
1659
|
+
case "bottom-right":
|
|
1660
|
+
x = vw - BTN_MARGIN - width;
|
|
1661
|
+
y = vh - BTN_MARGIN - height - BTN_SIZE - 8;
|
|
1662
|
+
break;
|
|
1663
|
+
case "top-left":
|
|
1664
|
+
x = BTN_MARGIN;
|
|
1665
|
+
y = BTN_MARGIN + BTN_SIZE + 8;
|
|
1666
|
+
break;
|
|
1667
|
+
case "top-right":
|
|
1668
|
+
x = vw - BTN_MARGIN - width;
|
|
1669
|
+
y = BTN_MARGIN + BTN_SIZE + 8;
|
|
1670
|
+
break;
|
|
1671
|
+
case "bottom-left":
|
|
1672
|
+
default:
|
|
1673
|
+
x = BTN_MARGIN;
|
|
1674
|
+
y = vh - BTN_MARGIN - height - BTN_SIZE - 8;
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
return {
|
|
1678
|
+
x: Math.max(0, Math.min(x, vw - width)),
|
|
1679
|
+
y: Math.max(0, Math.min(y, vh - height))
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
function BlockTreeDevTools({
|
|
1683
|
+
blocks,
|
|
1684
|
+
containerTypes = [],
|
|
1685
|
+
events,
|
|
1686
|
+
onClearEvents,
|
|
1687
|
+
getLabel = (b) => b.type,
|
|
1688
|
+
initialOpen = false,
|
|
1689
|
+
position = "bottom-left",
|
|
1690
|
+
buttonStyle,
|
|
1691
|
+
panelStyle,
|
|
1692
|
+
forceMount = false
|
|
1693
|
+
}) {
|
|
1694
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production" && !forceMount) {
|
|
1695
|
+
return null;
|
|
1696
|
+
}
|
|
1697
|
+
const [isOpen, setIsOpen] = useState(initialOpen);
|
|
1698
|
+
const [showDiff, setShowDiff] = useState(false);
|
|
1699
|
+
const [cardPos, setCardPos] = useState(null);
|
|
1700
|
+
const [cardSize, setCardSize] = useState({ w: DEFAULT_WIDTH, h: DEFAULT_HEIGHT });
|
|
1701
|
+
const [showTooltip, setShowTooltip] = useState(false);
|
|
1702
|
+
const [activeCorner, setActiveCorner] = useState(() => loadStoredPosition() ?? position);
|
|
1703
|
+
const [btnPos, setBtnPos] = useState(null);
|
|
1704
|
+
const [btnDragging, setBtnDragging] = useState(false);
|
|
1705
|
+
const [btnTransition, setBtnTransition] = useState(false);
|
|
1706
|
+
const btnDragRef = useRef({
|
|
1707
|
+
active: false,
|
|
1708
|
+
startX: 0,
|
|
1709
|
+
startY: 0,
|
|
1710
|
+
origX: 0,
|
|
1711
|
+
origY: 0,
|
|
1712
|
+
moved: false
|
|
1713
|
+
});
|
|
1714
|
+
useEffect(() => {
|
|
1715
|
+
if (!btnDragging) {
|
|
1716
|
+
setBtnPos(cornerToXY(activeCorner));
|
|
1717
|
+
}
|
|
1718
|
+
}, [activeCorner, btnDragging]);
|
|
1719
|
+
useEffect(() => {
|
|
1720
|
+
const onResize = () => {
|
|
1721
|
+
if (!btnDragRef.current.active) {
|
|
1722
|
+
setBtnPos(cornerToXY(activeCorner));
|
|
1723
|
+
}
|
|
1724
|
+
};
|
|
1725
|
+
window.addEventListener("resize", onResize);
|
|
1726
|
+
return () => window.removeEventListener("resize", onResize);
|
|
1727
|
+
}, [activeCorner]);
|
|
1728
|
+
const handleBtnPointerDown = useCallback((e) => {
|
|
1729
|
+
if (!btnPos) return;
|
|
1730
|
+
btnDragRef.current = {
|
|
1731
|
+
active: true,
|
|
1732
|
+
startX: e.clientX,
|
|
1733
|
+
startY: e.clientY,
|
|
1734
|
+
origX: btnPos.x,
|
|
1735
|
+
origY: btnPos.y,
|
|
1736
|
+
moved: false
|
|
1737
|
+
};
|
|
1738
|
+
e.target.setPointerCapture(e.pointerId);
|
|
1739
|
+
}, [btnPos]);
|
|
1740
|
+
const handleBtnPointerMove = useCallback((e) => {
|
|
1741
|
+
const r = btnDragRef.current;
|
|
1742
|
+
if (!r.active) return;
|
|
1743
|
+
const dx = e.clientX - r.startX;
|
|
1744
|
+
const dy = e.clientY - r.startY;
|
|
1745
|
+
if (!r.moved && Math.abs(dx) < BTN_DRAG_THRESHOLD && Math.abs(dy) < BTN_DRAG_THRESHOLD) return;
|
|
1746
|
+
r.moved = true;
|
|
1747
|
+
setBtnDragging(true);
|
|
1748
|
+
const newX = r.origX + dx;
|
|
1749
|
+
const newY = r.origY + dy;
|
|
1750
|
+
const maxX = window.innerWidth - BTN_SIZE;
|
|
1751
|
+
const maxY = window.innerHeight - BTN_SIZE;
|
|
1752
|
+
setBtnPos({
|
|
1753
|
+
x: Math.max(0, Math.min(newX, maxX)),
|
|
1754
|
+
y: Math.max(0, Math.min(newY, maxY))
|
|
1755
|
+
});
|
|
1756
|
+
}, []);
|
|
1757
|
+
const handleBtnPointerUp = useCallback(() => {
|
|
1758
|
+
if (!btnPos) return;
|
|
1759
|
+
const wasMoved = btnDragRef.current.moved;
|
|
1760
|
+
btnDragRef.current.active = false;
|
|
1761
|
+
if (wasMoved) {
|
|
1762
|
+
const newCorner = xyToCorner(btnPos.x, btnPos.y);
|
|
1763
|
+
setActiveCorner(newCorner);
|
|
1764
|
+
savePosition(newCorner);
|
|
1765
|
+
setBtnTransition(true);
|
|
1766
|
+
setBtnPos(cornerToXY(newCorner));
|
|
1767
|
+
setTimeout(() => {
|
|
1768
|
+
setBtnTransition(false);
|
|
1769
|
+
setBtnDragging(false);
|
|
1770
|
+
}, 300);
|
|
1771
|
+
} else {
|
|
1772
|
+
setBtnDragging(false);
|
|
1773
|
+
setIsOpen((prev) => !prev);
|
|
1774
|
+
}
|
|
1775
|
+
}, [btnPos]);
|
|
1776
|
+
const dragRef = useRef({
|
|
1777
|
+
dragging: false,
|
|
1778
|
+
startX: 0,
|
|
1779
|
+
startY: 0,
|
|
1780
|
+
origX: 0,
|
|
1781
|
+
origY: 0
|
|
1782
|
+
});
|
|
1783
|
+
const resizeRef = useRef({ active: false, edge: "", startX: 0, startY: 0, origX: 0, origY: 0, origW: 0, origH: 0 });
|
|
1784
|
+
const cardRef = useRef(null);
|
|
1785
|
+
const prevBlocksRef = useRef(blocks);
|
|
1786
|
+
const diffChangeMap = useMemo(() => computeDiffMap(prevBlocksRef.current, blocks), [blocks]);
|
|
1787
|
+
useEffect(() => {
|
|
1788
|
+
prevBlocksRef.current = blocks;
|
|
1789
|
+
}, [blocks]);
|
|
1790
|
+
const diffTree = useMemo(() => buildDiffTree(blocks, diffChangeMap), [blocks, diffChangeMap]);
|
|
1791
|
+
const diffStats = useMemo(() => {
|
|
1792
|
+
let added = 0, moved = 0;
|
|
1793
|
+
for (const { changeType } of diffTree) {
|
|
1794
|
+
if (changeType === "added") added++;
|
|
1795
|
+
if (changeType === "moved") moved++;
|
|
1796
|
+
}
|
|
1797
|
+
return { added, moved };
|
|
1798
|
+
}, [diffTree]);
|
|
1799
|
+
useEffect(() => {
|
|
1800
|
+
setCardSize((prev) => {
|
|
1801
|
+
const targetW = showDiff ? DEFAULT_WIDTH + DIFF_EXTRA_WIDTH : DEFAULT_WIDTH;
|
|
1802
|
+
const wasDefault = Math.abs(prev.w - DEFAULT_WIDTH) < 20;
|
|
1803
|
+
const wasExpanded = Math.abs(prev.w - (DEFAULT_WIDTH + DIFF_EXTRA_WIDTH)) < 20;
|
|
1804
|
+
let newW = prev.w;
|
|
1805
|
+
if (showDiff && (wasDefault || prev.w < targetW)) {
|
|
1806
|
+
newW = targetW;
|
|
1807
|
+
} else if (!showDiff && wasExpanded) {
|
|
1808
|
+
newW = DEFAULT_WIDTH;
|
|
1809
|
+
}
|
|
1810
|
+
if (newW !== prev.w) {
|
|
1811
|
+
setCardPos((cp) => {
|
|
1812
|
+
if (!cp) return cp;
|
|
1813
|
+
const vw = typeof window !== "undefined" ? window.innerWidth : 1024;
|
|
1814
|
+
const maxX = Math.max(0, vw - newW);
|
|
1815
|
+
if (cp.x > maxX) return { ...cp, x: maxX };
|
|
1816
|
+
return cp;
|
|
1817
|
+
});
|
|
1818
|
+
return { ...prev, w: newW };
|
|
1819
|
+
}
|
|
1820
|
+
return prev;
|
|
1821
|
+
});
|
|
1822
|
+
}, [showDiff]);
|
|
1823
|
+
const getDefaultCardPos = useCallback(() => {
|
|
1824
|
+
return computeCardOrigin(activeCorner, cardSize.w, cardSize.h);
|
|
1825
|
+
}, [activeCorner, cardSize.w, cardSize.h]);
|
|
1826
|
+
useEffect(() => {
|
|
1827
|
+
if (isOpen && !cardPos) {
|
|
1828
|
+
setCardPos(getDefaultCardPos());
|
|
1829
|
+
}
|
|
1830
|
+
}, [isOpen, cardPos, getDefaultCardPos]);
|
|
1831
|
+
const handleDragPointerDown = useCallback((e) => {
|
|
1832
|
+
e.preventDefault();
|
|
1833
|
+
dragRef.current = {
|
|
1834
|
+
dragging: true,
|
|
1835
|
+
startX: e.clientX,
|
|
1836
|
+
startY: e.clientY,
|
|
1837
|
+
origX: cardPos?.x ?? 0,
|
|
1838
|
+
origY: cardPos?.y ?? 0
|
|
1839
|
+
};
|
|
1840
|
+
e.target.setPointerCapture(e.pointerId);
|
|
1841
|
+
}, [cardPos]);
|
|
1842
|
+
const handleDragPointerMove = useCallback((e) => {
|
|
1843
|
+
if (!dragRef.current.dragging) return;
|
|
1844
|
+
const dx = e.clientX - dragRef.current.startX;
|
|
1845
|
+
const dy = e.clientY - dragRef.current.startY;
|
|
1846
|
+
const newX = dragRef.current.origX + dx;
|
|
1847
|
+
const newY = dragRef.current.origY + dy;
|
|
1848
|
+
const maxX = window.innerWidth - cardSize.w;
|
|
1849
|
+
const maxY = window.innerHeight - 40;
|
|
1850
|
+
setCardPos({
|
|
1851
|
+
x: Math.max(0, Math.min(newX, maxX)),
|
|
1852
|
+
y: Math.max(0, Math.min(newY, maxY))
|
|
1853
|
+
});
|
|
1854
|
+
}, [cardSize.w]);
|
|
1855
|
+
const handleDragPointerUp = useCallback(() => {
|
|
1856
|
+
dragRef.current.dragging = false;
|
|
1857
|
+
}, []);
|
|
1858
|
+
const handleResizePointerDown = useCallback((edge) => (e) => {
|
|
1859
|
+
e.preventDefault();
|
|
1860
|
+
e.stopPropagation();
|
|
1861
|
+
resizeRef.current = {
|
|
1862
|
+
active: true,
|
|
1863
|
+
edge,
|
|
1864
|
+
startX: e.clientX,
|
|
1865
|
+
startY: e.clientY,
|
|
1866
|
+
origX: cardPos?.x ?? 0,
|
|
1867
|
+
origY: cardPos?.y ?? 0,
|
|
1868
|
+
origW: cardSize.w,
|
|
1869
|
+
origH: cardSize.h
|
|
1870
|
+
};
|
|
1871
|
+
e.target.setPointerCapture(e.pointerId);
|
|
1872
|
+
}, [cardPos, cardSize]);
|
|
1873
|
+
const handleResizePointerMove = useCallback((e) => {
|
|
1874
|
+
const r = resizeRef.current;
|
|
1875
|
+
if (!r.active) return;
|
|
1876
|
+
const dx = e.clientX - r.startX;
|
|
1877
|
+
const dy = e.clientY - r.startY;
|
|
1878
|
+
let newW = r.origW;
|
|
1879
|
+
let newH = r.origH;
|
|
1880
|
+
let newX = r.origX;
|
|
1881
|
+
let newY = r.origY;
|
|
1882
|
+
if (r.edge.includes("e")) newW = Math.max(MIN_WIDTH, r.origW + dx);
|
|
1883
|
+
if (r.edge.includes("w")) {
|
|
1884
|
+
newW = Math.max(MIN_WIDTH, r.origW - dx);
|
|
1885
|
+
newX = r.origX + (r.origW - newW);
|
|
1886
|
+
}
|
|
1887
|
+
if (r.edge.includes("s")) newH = Math.max(MIN_HEIGHT, r.origH + dy);
|
|
1888
|
+
if (r.edge.includes("n")) {
|
|
1889
|
+
newH = Math.max(MIN_HEIGHT, r.origH - dy);
|
|
1890
|
+
newY = r.origY + (r.origH - newH);
|
|
1891
|
+
}
|
|
1892
|
+
setCardSize({ w: newW, h: newH });
|
|
1893
|
+
setCardPos({ x: newX, y: newY });
|
|
1894
|
+
}, []);
|
|
1895
|
+
const handleResizePointerUp = useCallback(() => {
|
|
1896
|
+
resizeRef.current.active = false;
|
|
1897
|
+
}, []);
|
|
1898
|
+
const toggle = useCallback(() => {
|
|
1899
|
+
setIsOpen((prev) => !prev);
|
|
1900
|
+
}, []);
|
|
1901
|
+
const toggleDiff = useCallback(() => {
|
|
1902
|
+
setShowDiff((prev) => !prev);
|
|
1903
|
+
}, []);
|
|
1904
|
+
const renderCountRef = useRef(0);
|
|
1905
|
+
const lastRenderTimeRef = useRef(performance.now());
|
|
1906
|
+
const prevBlockCountRef = useRef(blocks.length);
|
|
1907
|
+
renderCountRef.current++;
|
|
1908
|
+
const renderTime = performance.now() - lastRenderTimeRef.current;
|
|
1909
|
+
lastRenderTimeRef.current = performance.now();
|
|
1910
|
+
const blockCountDelta = blocks.length - prevBlockCountRef.current;
|
|
1911
|
+
useEffect(() => {
|
|
1912
|
+
prevBlockCountRef.current = blocks.length;
|
|
1913
|
+
}, [blocks.length]);
|
|
1914
|
+
const treeStats = useMemo(() => {
|
|
1915
|
+
const index = computeNormalizedIndex(blocks);
|
|
1916
|
+
const containers = blocks.filter((b) => containerTypes.includes(b.type));
|
|
1917
|
+
let maxDepthVal = 0;
|
|
1918
|
+
for (const block of blocks) {
|
|
1919
|
+
const d = getBlockDepth(index, block.id);
|
|
1920
|
+
if (d > maxDepthVal) maxDepthVal = d;
|
|
1921
|
+
}
|
|
1922
|
+
const validation = validateBlockTree(index);
|
|
1923
|
+
return {
|
|
1924
|
+
blockCount: blocks.length,
|
|
1925
|
+
containerCount: containers.length,
|
|
1926
|
+
maxDepth: maxDepthVal,
|
|
1927
|
+
validation
|
|
1928
|
+
};
|
|
1929
|
+
}, [blocks, containerTypes]);
|
|
1930
|
+
const formatTime = (ts) => {
|
|
1931
|
+
const d = new Date(ts);
|
|
1932
|
+
return `${d.getHours().toString().padStart(2, "0")}:${d.getMinutes().toString().padStart(2, "0")}:${d.getSeconds().toString().padStart(2, "0")}`;
|
|
1933
|
+
};
|
|
1934
|
+
const isBottom = activeCorner.startsWith("bottom");
|
|
1935
|
+
const isLeft = activeCorner.endsWith("left");
|
|
1936
|
+
if (!btnPos) return null;
|
|
1937
|
+
const triggerBtnStyle = {
|
|
1938
|
+
position: "fixed",
|
|
1939
|
+
left: btnPos.x,
|
|
1940
|
+
top: btnPos.y,
|
|
1941
|
+
zIndex: 99998,
|
|
1942
|
+
width: BTN_SIZE,
|
|
1943
|
+
height: BTN_SIZE,
|
|
1944
|
+
borderRadius: "50%",
|
|
1945
|
+
border: "none",
|
|
1946
|
+
background: isOpen ? "rgba(59, 130, 246, 0.9)" : "rgba(30, 30, 30, 0.85)",
|
|
1947
|
+
color: "#fff",
|
|
1948
|
+
cursor: btnDragging ? "grabbing" : "pointer",
|
|
1949
|
+
display: "flex",
|
|
1950
|
+
alignItems: "center",
|
|
1951
|
+
justifyContent: "center",
|
|
1952
|
+
boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
|
|
1953
|
+
transition: btnTransition ? "left 0.3s cubic-bezier(0.4, 0, 0.2, 1), top 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.15s" : "background 0.15s",
|
|
1954
|
+
touchAction: "none",
|
|
1955
|
+
userSelect: "none",
|
|
1956
|
+
...buttonStyle
|
|
1957
|
+
};
|
|
1958
|
+
const tooltipStyle = {
|
|
1959
|
+
position: "absolute",
|
|
1960
|
+
whiteSpace: "nowrap",
|
|
1961
|
+
fontSize: 11,
|
|
1962
|
+
fontWeight: 500,
|
|
1963
|
+
padding: "4px 10px",
|
|
1964
|
+
borderRadius: 6,
|
|
1965
|
+
background: "rgba(15, 15, 20, 0.95)",
|
|
1966
|
+
color: "#ccc",
|
|
1967
|
+
boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
|
|
1968
|
+
pointerEvents: "none",
|
|
1969
|
+
...isBottom ? { bottom: "100%", marginBottom: 8 } : { top: "100%", marginTop: 8 },
|
|
1970
|
+
...isLeft ? { left: 0 } : { right: 0 }
|
|
1971
|
+
};
|
|
1972
|
+
const cardStyle = {
|
|
1973
|
+
position: "fixed",
|
|
1974
|
+
left: cardPos?.x ?? 0,
|
|
1975
|
+
top: cardPos?.y ?? 0,
|
|
1976
|
+
zIndex: 99999,
|
|
1977
|
+
width: cardSize.w,
|
|
1978
|
+
height: cardSize.h,
|
|
1979
|
+
background: "rgba(20, 20, 24, 0.95)",
|
|
1980
|
+
backdropFilter: "blur(12px)",
|
|
1981
|
+
border: "1px solid rgba(255,255,255,0.1)",
|
|
1982
|
+
borderRadius: 10,
|
|
1983
|
+
color: "#e0e0e0",
|
|
1984
|
+
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
|
|
1985
|
+
fontSize: 12,
|
|
1986
|
+
display: "flex",
|
|
1987
|
+
flexDirection: "column",
|
|
1988
|
+
boxShadow: "0 8px 32px rgba(0,0,0,0.5)",
|
|
1989
|
+
overflow: "hidden",
|
|
1990
|
+
...panelStyle
|
|
1991
|
+
};
|
|
1992
|
+
const titleBarStyle = {
|
|
1993
|
+
display: "flex",
|
|
1994
|
+
alignItems: "center",
|
|
1995
|
+
justifyContent: "space-between",
|
|
1996
|
+
padding: "8px 12px",
|
|
1997
|
+
gap: 8,
|
|
1998
|
+
background: "rgba(255,255,255,0.04)",
|
|
1999
|
+
borderBottom: "1px solid rgba(255,255,255,0.08)",
|
|
2000
|
+
cursor: "grab",
|
|
2001
|
+
userSelect: "none",
|
|
2002
|
+
flexShrink: 0,
|
|
2003
|
+
touchAction: "none"
|
|
2004
|
+
};
|
|
2005
|
+
const titleTextStyle = {
|
|
2006
|
+
fontSize: 11,
|
|
2007
|
+
fontWeight: 600,
|
|
2008
|
+
letterSpacing: "0.03em",
|
|
2009
|
+
opacity: 0.8,
|
|
2010
|
+
flexShrink: 0
|
|
2011
|
+
};
|
|
2012
|
+
const titleBtnStyle = (active) => ({
|
|
2013
|
+
height: 22,
|
|
2014
|
+
padding: "0 8px",
|
|
2015
|
+
borderRadius: 4,
|
|
2016
|
+
border: "1px solid " + (active ? "rgba(59,130,246,0.5)" : "rgba(128,128,128,0.25)"),
|
|
2017
|
+
background: active ? "rgba(59,130,246,0.15)" : "transparent",
|
|
2018
|
+
color: active ? "#93b8f7" : "#999",
|
|
2019
|
+
cursor: "pointer",
|
|
2020
|
+
fontSize: 10,
|
|
2021
|
+
fontWeight: 600,
|
|
2022
|
+
letterSpacing: "0.02em",
|
|
2023
|
+
flexShrink: 0
|
|
2024
|
+
});
|
|
2025
|
+
const closeBtnStyle = {
|
|
2026
|
+
width: 20,
|
|
2027
|
+
height: 20,
|
|
2028
|
+
borderRadius: 4,
|
|
2029
|
+
border: "none",
|
|
2030
|
+
background: "transparent",
|
|
2031
|
+
color: "#999",
|
|
2032
|
+
cursor: "pointer",
|
|
2033
|
+
display: "flex",
|
|
2034
|
+
alignItems: "center",
|
|
2035
|
+
justifyContent: "center",
|
|
2036
|
+
fontSize: 14,
|
|
2037
|
+
lineHeight: "1",
|
|
2038
|
+
padding: 0,
|
|
2039
|
+
flexShrink: 0
|
|
2040
|
+
};
|
|
2041
|
+
const bodyStyle = {
|
|
2042
|
+
flex: 1,
|
|
2043
|
+
display: "flex",
|
|
2044
|
+
flexDirection: "row",
|
|
2045
|
+
overflow: "hidden",
|
|
2046
|
+
minHeight: 0
|
|
2047
|
+
};
|
|
2048
|
+
const mainColumnStyle = {
|
|
2049
|
+
flex: showDiff ? "0 0 50%" : "1 1 100%",
|
|
2050
|
+
overflow: "auto",
|
|
2051
|
+
padding: "10px 12px",
|
|
2052
|
+
minWidth: 0
|
|
2053
|
+
};
|
|
2054
|
+
const diffColumnStyle = {
|
|
2055
|
+
flex: "0 0 50%",
|
|
2056
|
+
overflow: "auto",
|
|
2057
|
+
padding: "10px 12px",
|
|
2058
|
+
borderLeft: "1px solid rgba(255,255,255,0.08)",
|
|
2059
|
+
minWidth: 0
|
|
2060
|
+
};
|
|
2061
|
+
const sectionStyle = {
|
|
2062
|
+
marginBottom: 8
|
|
2063
|
+
};
|
|
2064
|
+
const headingStyle = {
|
|
2065
|
+
fontSize: 11,
|
|
2066
|
+
fontWeight: 600,
|
|
2067
|
+
textTransform: "uppercase",
|
|
2068
|
+
letterSpacing: "0.05em",
|
|
2069
|
+
opacity: 0.6,
|
|
2070
|
+
marginBottom: 6
|
|
2071
|
+
};
|
|
2072
|
+
const statRowStyle = {
|
|
2073
|
+
display: "flex",
|
|
2074
|
+
justifyContent: "space-between",
|
|
2075
|
+
alignItems: "center",
|
|
2076
|
+
fontSize: 12,
|
|
2077
|
+
padding: "2px 0"
|
|
2078
|
+
};
|
|
2079
|
+
const statValueStyle = {
|
|
2080
|
+
fontFamily: "monospace",
|
|
2081
|
+
fontSize: 11,
|
|
2082
|
+
opacity: 0.8
|
|
2083
|
+
};
|
|
2084
|
+
const badgeStyle = (color) => ({
|
|
2085
|
+
display: "inline-block",
|
|
2086
|
+
fontSize: 9,
|
|
2087
|
+
fontWeight: 700,
|
|
2088
|
+
fontFamily: "monospace",
|
|
2089
|
+
padding: "1px 5px",
|
|
2090
|
+
borderRadius: 3,
|
|
2091
|
+
backgroundColor: color + "22",
|
|
2092
|
+
color,
|
|
2093
|
+
marginRight: 6,
|
|
2094
|
+
flexShrink: 0
|
|
2095
|
+
});
|
|
2096
|
+
const eventListStyle = {
|
|
2097
|
+
maxHeight: 160,
|
|
2098
|
+
overflowY: "auto",
|
|
2099
|
+
fontSize: 11,
|
|
2100
|
+
lineHeight: "1.6"
|
|
2101
|
+
};
|
|
2102
|
+
const eventItemStyle = {
|
|
2103
|
+
display: "flex",
|
|
2104
|
+
alignItems: "flex-start",
|
|
2105
|
+
gap: 4,
|
|
2106
|
+
padding: "2px 0",
|
|
2107
|
+
borderBottom: "1px solid rgba(128,128,128,0.1)"
|
|
2108
|
+
};
|
|
2109
|
+
const timeStyle = {
|
|
2110
|
+
fontFamily: "monospace",
|
|
2111
|
+
fontSize: 10,
|
|
2112
|
+
opacity: 0.4,
|
|
2113
|
+
flexShrink: 0,
|
|
2114
|
+
minWidth: 52
|
|
2115
|
+
};
|
|
2116
|
+
const clearBtnStyle = {
|
|
2117
|
+
fontSize: 10,
|
|
2118
|
+
padding: "2px 8px",
|
|
2119
|
+
border: "1px solid rgba(128,128,128,0.3)",
|
|
2120
|
+
borderRadius: 4,
|
|
2121
|
+
background: "transparent",
|
|
2122
|
+
color: "#ccc",
|
|
2123
|
+
cursor: "pointer",
|
|
2124
|
+
opacity: 0.7
|
|
2125
|
+
};
|
|
2126
|
+
const EDGE_SIZE = 6;
|
|
2127
|
+
const resizeEdge = (edge) => {
|
|
2128
|
+
const base = {
|
|
2129
|
+
position: "absolute",
|
|
2130
|
+
zIndex: 1
|
|
2131
|
+
};
|
|
2132
|
+
const cursors = {
|
|
2133
|
+
n: "ns-resize",
|
|
2134
|
+
s: "ns-resize",
|
|
2135
|
+
e: "ew-resize",
|
|
2136
|
+
w: "ew-resize",
|
|
2137
|
+
ne: "nesw-resize",
|
|
2138
|
+
nw: "nwse-resize",
|
|
2139
|
+
se: "nwse-resize",
|
|
2140
|
+
sw: "nesw-resize"
|
|
2141
|
+
};
|
|
2142
|
+
const styles = {
|
|
2143
|
+
n: { top: 0, left: EDGE_SIZE, right: EDGE_SIZE, height: EDGE_SIZE },
|
|
2144
|
+
s: { bottom: 0, left: EDGE_SIZE, right: EDGE_SIZE, height: EDGE_SIZE },
|
|
2145
|
+
e: { right: 0, top: EDGE_SIZE, bottom: EDGE_SIZE, width: EDGE_SIZE },
|
|
2146
|
+
w: { left: 0, top: EDGE_SIZE, bottom: EDGE_SIZE, width: EDGE_SIZE },
|
|
2147
|
+
ne: { top: 0, right: 0, width: EDGE_SIZE * 2, height: EDGE_SIZE * 2 },
|
|
2148
|
+
nw: { top: 0, left: 0, width: EDGE_SIZE * 2, height: EDGE_SIZE * 2 },
|
|
2149
|
+
se: { bottom: 0, right: 0, width: EDGE_SIZE * 2, height: EDGE_SIZE * 2 },
|
|
2150
|
+
sw: { bottom: 0, left: 0, width: EDGE_SIZE * 2, height: EDGE_SIZE * 2 }
|
|
2151
|
+
};
|
|
2152
|
+
return { ...base, cursor: cursors[edge], ...styles[edge] };
|
|
2153
|
+
};
|
|
2154
|
+
const EDGES = ["n", "s", "e", "w", "ne", "nw", "se", "sw"];
|
|
2155
|
+
const diffRowColor = (ct) => {
|
|
2156
|
+
if (ct === "added") return { background: "rgba(16,185,129,0.1)", color: "#34d399" };
|
|
2157
|
+
if (ct === "moved") return { background: "rgba(245,158,11,0.1)", color: "#fbbf24" };
|
|
2158
|
+
return { color: "rgba(200,200,200,0.5)" };
|
|
2159
|
+
};
|
|
2160
|
+
return /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
2161
|
+
/* @__PURE__ */ jsxs("div", { style: { position: "fixed", left: btnPos.x, top: btnPos.y, zIndex: 99998 }, children: [
|
|
2162
|
+
/* @__PURE__ */ jsx(
|
|
2163
|
+
"button",
|
|
2164
|
+
{
|
|
2165
|
+
onPointerDown: handleBtnPointerDown,
|
|
2166
|
+
onPointerMove: handleBtnPointerMove,
|
|
2167
|
+
onPointerUp: handleBtnPointerUp,
|
|
2168
|
+
onMouseEnter: () => setShowTooltip(true),
|
|
2169
|
+
onMouseLeave: () => setShowTooltip(false),
|
|
2170
|
+
style: triggerBtnStyle,
|
|
2171
|
+
"aria-label": isOpen ? "Close DevTools" : "Open DevTools",
|
|
2172
|
+
children: /* @__PURE__ */ jsx(DevToolsLogo, { size: 20 })
|
|
2173
|
+
}
|
|
2174
|
+
),
|
|
2175
|
+
showTooltip && !isOpen && !btnDragging && /* @__PURE__ */ jsx("div", { style: tooltipStyle, children: "dnd-block-tree DevTools" })
|
|
2176
|
+
] }),
|
|
2177
|
+
isOpen && cardPos && /* @__PURE__ */ jsxs(
|
|
2178
|
+
"div",
|
|
2179
|
+
{
|
|
2180
|
+
ref: cardRef,
|
|
2181
|
+
style: cardStyle,
|
|
2182
|
+
"data-devtools-root": "",
|
|
2183
|
+
children: [
|
|
2184
|
+
EDGES.map((edge) => /* @__PURE__ */ jsx(
|
|
2185
|
+
"div",
|
|
2186
|
+
{
|
|
2187
|
+
style: resizeEdge(edge),
|
|
2188
|
+
onPointerDown: handleResizePointerDown(edge),
|
|
2189
|
+
onPointerMove: handleResizePointerMove,
|
|
2190
|
+
onPointerUp: handleResizePointerUp
|
|
2191
|
+
},
|
|
2192
|
+
edge
|
|
2193
|
+
)),
|
|
2194
|
+
/* @__PURE__ */ jsxs(
|
|
2195
|
+
"div",
|
|
2196
|
+
{
|
|
2197
|
+
style: titleBarStyle,
|
|
2198
|
+
onPointerDown: handleDragPointerDown,
|
|
2199
|
+
onPointerMove: handleDragPointerMove,
|
|
2200
|
+
onPointerUp: handleDragPointerUp,
|
|
2201
|
+
children: [
|
|
2202
|
+
/* @__PURE__ */ jsx("span", { style: titleTextStyle, children: "DevTools" }),
|
|
2203
|
+
/* @__PURE__ */ jsx("div", { style: { flex: 1 } }),
|
|
2204
|
+
/* @__PURE__ */ jsx(
|
|
2205
|
+
"button",
|
|
2206
|
+
{
|
|
2207
|
+
onClick: toggleDiff,
|
|
2208
|
+
style: titleBtnStyle(showDiff),
|
|
2209
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
2210
|
+
title: "Toggle structure diff",
|
|
2211
|
+
children: "Diff"
|
|
2212
|
+
}
|
|
2213
|
+
),
|
|
2214
|
+
/* @__PURE__ */ jsx(
|
|
2215
|
+
"button",
|
|
2216
|
+
{
|
|
2217
|
+
onClick: toggle,
|
|
2218
|
+
style: closeBtnStyle,
|
|
2219
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
2220
|
+
"aria-label": "Close DevTools",
|
|
2221
|
+
children: "\xD7"
|
|
2222
|
+
}
|
|
2223
|
+
)
|
|
2224
|
+
]
|
|
2225
|
+
}
|
|
2226
|
+
),
|
|
2227
|
+
/* @__PURE__ */ jsxs("div", { style: bodyStyle, children: [
|
|
2228
|
+
/* @__PURE__ */ jsxs("div", { style: mainColumnStyle, children: [
|
|
2229
|
+
/* @__PURE__ */ jsxs("div", { style: sectionStyle, "data-devtools-section": "tree-state", children: [
|
|
2230
|
+
/* @__PURE__ */ jsx("div", { style: headingStyle, title: "Live snapshot of the block tree structure", children: "Tree State" }),
|
|
2231
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Total number of blocks", children: [
|
|
2232
|
+
/* @__PURE__ */ jsx("span", { children: "Blocks" }),
|
|
2233
|
+
/* @__PURE__ */ jsx("span", { style: statValueStyle, children: treeStats.blockCount })
|
|
2234
|
+
] }),
|
|
2235
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Container blocks", children: [
|
|
2236
|
+
/* @__PURE__ */ jsx("span", { children: "Containers" }),
|
|
2237
|
+
/* @__PURE__ */ jsx("span", { style: statValueStyle, children: treeStats.containerCount })
|
|
2238
|
+
] }),
|
|
2239
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Deepest nesting level", children: [
|
|
2240
|
+
/* @__PURE__ */ jsx("span", { children: "Max Depth" }),
|
|
2241
|
+
/* @__PURE__ */ jsx("span", { style: statValueStyle, children: treeStats.maxDepth })
|
|
2242
|
+
] }),
|
|
2243
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Tree validation status", children: [
|
|
2244
|
+
/* @__PURE__ */ jsx("span", { children: "Validation" }),
|
|
2245
|
+
/* @__PURE__ */ jsx("span", { style: {
|
|
2246
|
+
...statValueStyle,
|
|
2247
|
+
color: treeStats.validation.valid ? "#10b981" : "#ef4444"
|
|
2248
|
+
}, children: treeStats.validation.valid ? "Valid" : `${treeStats.validation.issues.length} issue(s)` })
|
|
2249
|
+
] }),
|
|
2250
|
+
!treeStats.validation.valid && /* @__PURE__ */ jsx("div", { style: { fontSize: 10, color: "#ef4444", marginTop: 4 }, children: treeStats.validation.issues.map((issue, i) => /* @__PURE__ */ jsx("div", { children: issue }, i)) })
|
|
2251
|
+
] }),
|
|
2252
|
+
/* @__PURE__ */ jsxs("div", { style: sectionStyle, "data-devtools-section": "event-log", children: [
|
|
2253
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 6 }, children: [
|
|
2254
|
+
/* @__PURE__ */ jsxs("div", { style: headingStyle, title: "Event log", children: [
|
|
2255
|
+
"Event Log (",
|
|
2256
|
+
events.length,
|
|
2257
|
+
")"
|
|
2258
|
+
] }),
|
|
2259
|
+
events.length > 0 && /* @__PURE__ */ jsx("button", { onClick: onClearEvents, style: clearBtnStyle, children: "Clear" })
|
|
2260
|
+
] }),
|
|
2261
|
+
/* @__PURE__ */ jsx("div", { style: eventListStyle, children: events.length === 0 ? /* @__PURE__ */ jsx("div", { style: { fontSize: 11, opacity: 0.4, padding: "8px 0" }, children: "No events yet. Drag some blocks!" }) : events.map((event) => /* @__PURE__ */ jsxs("div", { style: eventItemStyle, children: [
|
|
2262
|
+
/* @__PURE__ */ jsx("span", { style: timeStyle, children: formatTime(event.timestamp) }),
|
|
2263
|
+
/* @__PURE__ */ jsx("span", { style: badgeStyle(TYPE_COLORS[event.type]), title: TYPE_TOOLTIPS[event.type], children: TYPE_LABELS[event.type] }),
|
|
2264
|
+
/* @__PURE__ */ jsx("span", { style: { wordBreak: "break-word" }, children: event.summary })
|
|
2265
|
+
] }, event.id)) })
|
|
2266
|
+
] }),
|
|
2267
|
+
/* @__PURE__ */ jsxs("div", { style: sectionStyle, "data-devtools-section": "performance", children: [
|
|
2268
|
+
/* @__PURE__ */ jsx("div", { style: headingStyle, title: "Render metrics", children: "Performance" }),
|
|
2269
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Render count", children: [
|
|
2270
|
+
/* @__PURE__ */ jsx("span", { children: "Render Count" }),
|
|
2271
|
+
/* @__PURE__ */ jsx("span", { style: statValueStyle, children: renderCountRef.current })
|
|
2272
|
+
] }),
|
|
2273
|
+
/* @__PURE__ */ jsxs("div", { style: statRowStyle, title: "Time since previous render", children: [
|
|
2274
|
+
/* @__PURE__ */ jsx("span", { children: "Last Render" }),
|
|
2275
|
+
/* @__PURE__ */ jsxs("span", { style: statValueStyle, children: [
|
|
2276
|
+
renderTime.toFixed(1),
|
|
2277
|
+
"ms"
|
|
2278
|
+
] })
|
|
2279
|
+
] }),
|
|
2280
|
+
/* @__PURE__ */ jsx("div", { style: statRowStyle, children: /* @__PURE__ */ jsxs("span", { style: {
|
|
2281
|
+
...statValueStyle,
|
|
2282
|
+
color: blockCountDelta > 0 ? "#10b981" : blockCountDelta < 0 ? "#ef4444" : void 0
|
|
2283
|
+
}, children: [
|
|
2284
|
+
blockCountDelta > 0 ? "+" : "",
|
|
2285
|
+
blockCountDelta
|
|
2286
|
+
] }) })
|
|
2287
|
+
] })
|
|
2288
|
+
] }),
|
|
2289
|
+
showDiff && /* @__PURE__ */ jsxs("div", { style: diffColumnStyle, children: [
|
|
2290
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 8 }, children: [
|
|
2291
|
+
/* @__PURE__ */ jsx("div", { style: headingStyle, title: "Tree structure diff", children: "Structure" }),
|
|
2292
|
+
/* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 10, fontSize: 11, fontWeight: 500 }, children: [
|
|
2293
|
+
diffStats.added > 0 && /* @__PURE__ */ jsxs("span", { style: { color: "#34d399", display: "flex", alignItems: "center", gap: 4 }, children: [
|
|
2294
|
+
/* @__PURE__ */ jsx("span", { style: { width: 5, height: 5, borderRadius: "50%", background: "#34d399", display: "inline-block" } }),
|
|
2295
|
+
diffStats.added,
|
|
2296
|
+
" new"
|
|
2297
|
+
] }),
|
|
2298
|
+
diffStats.moved > 0 && /* @__PURE__ */ jsxs("span", { style: { color: "#fbbf24", display: "flex", alignItems: "center", gap: 4 }, children: [
|
|
2299
|
+
/* @__PURE__ */ jsx("span", { style: { width: 5, height: 5, borderRadius: "50%", background: "#fbbf24", display: "inline-block" } }),
|
|
2300
|
+
diffStats.moved,
|
|
2301
|
+
" moved"
|
|
2302
|
+
] }),
|
|
2303
|
+
diffStats.added === 0 && diffStats.moved === 0 && /* @__PURE__ */ jsx("span", { style: { opacity: 0.4 }, children: "No changes" })
|
|
2304
|
+
] })
|
|
2305
|
+
] }),
|
|
2306
|
+
/* @__PURE__ */ jsx("div", { style: { fontFamily: "monospace", fontSize: 11, lineHeight: "1.7" }, children: diffTree.map(({ block, changeType, depth }) => /* @__PURE__ */ jsxs(
|
|
2307
|
+
"div",
|
|
2308
|
+
{
|
|
2309
|
+
style: {
|
|
2310
|
+
paddingLeft: depth * 14,
|
|
2311
|
+
padding: "2px 6px 2px " + (depth * 14 + 6) + "px",
|
|
2312
|
+
borderRadius: 4,
|
|
2313
|
+
display: "flex",
|
|
2314
|
+
alignItems: "center",
|
|
2315
|
+
gap: 6,
|
|
2316
|
+
...diffRowColor(changeType)
|
|
2317
|
+
},
|
|
2318
|
+
children: [
|
|
2319
|
+
/* @__PURE__ */ jsxs("span", { style: { width: 12, textAlign: "center", fontWeight: 700, fontSize: 10 }, children: [
|
|
2320
|
+
changeType === "added" && "+",
|
|
2321
|
+
changeType === "moved" && "~"
|
|
2322
|
+
] }),
|
|
2323
|
+
/* @__PURE__ */ jsx("span", { style: {
|
|
2324
|
+
textTransform: "uppercase",
|
|
2325
|
+
fontSize: 9,
|
|
2326
|
+
letterSpacing: "0.05em",
|
|
2327
|
+
width: 50,
|
|
2328
|
+
flexShrink: 0,
|
|
2329
|
+
opacity: changeType === "unchanged" ? 0.5 : 1
|
|
2330
|
+
}, children: block.type }),
|
|
2331
|
+
/* @__PURE__ */ jsx("span", { style: {
|
|
2332
|
+
overflow: "hidden",
|
|
2333
|
+
textOverflow: "ellipsis",
|
|
2334
|
+
whiteSpace: "nowrap",
|
|
2335
|
+
flex: 1,
|
|
2336
|
+
minWidth: 0
|
|
2337
|
+
}, children: getLabel(block) }),
|
|
2338
|
+
/* @__PURE__ */ jsx("span", { style: {
|
|
2339
|
+
fontFamily: "monospace",
|
|
2340
|
+
fontSize: 10,
|
|
2341
|
+
flexShrink: 0,
|
|
2342
|
+
padding: "1px 5px",
|
|
2343
|
+
borderRadius: 3,
|
|
2344
|
+
...changeType === "added" ? { background: "rgba(16,185,129,0.2)", color: "#34d399" } : changeType === "moved" ? { background: "rgba(245,158,11,0.2)", color: "#fbbf24" } : { background: "rgba(128,128,128,0.15)", color: "rgba(200,200,200,0.4)" }
|
|
2345
|
+
}, children: String(block.order) })
|
|
2346
|
+
]
|
|
2347
|
+
},
|
|
2348
|
+
block.id
|
|
2349
|
+
)) })
|
|
2350
|
+
] })
|
|
2351
|
+
] })
|
|
2352
|
+
]
|
|
2353
|
+
}
|
|
2354
|
+
)
|
|
2355
|
+
] });
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
export { BlockTree, BlockTreeDevTools, BlockTreeSSR, DragOverlay, DropZone, TreeRenderer, adaptCollisionDetection, createBlockState, createTreeState, getSensorConfig, triggerHaptic, useBlockHistory, useConfiguredSensors, useDevToolsCallbacks, useLayoutAnimation, useVirtualTree };
|
|
2359
|
+
//# sourceMappingURL=index.mjs.map
|
|
2360
|
+
//# sourceMappingURL=index.mjs.map
|