@dnd-block-tree/vanilla 2.1.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 +85 -0
- package/dist/index.d.mts +314 -0
- package/dist/index.d.ts +314 -0
- package/dist/index.js +1034 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +888 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
import { historyReducer, createStickyCollision, createBlockTree, EventEmitter, getBlockDepth } 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
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
|
|
6
|
+
// src/collision-bridge.ts
|
|
7
|
+
function measureDropZoneRects(zones) {
|
|
8
|
+
const rects = /* @__PURE__ */ new Map();
|
|
9
|
+
for (const [id, el] of zones) {
|
|
10
|
+
const domRect = el.getBoundingClientRect();
|
|
11
|
+
rects.set(id, {
|
|
12
|
+
top: domRect.top,
|
|
13
|
+
left: domRect.left,
|
|
14
|
+
width: domRect.width,
|
|
15
|
+
height: domRect.height,
|
|
16
|
+
right: domRect.right,
|
|
17
|
+
bottom: domRect.bottom
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return rects;
|
|
21
|
+
}
|
|
22
|
+
function buildCandidates(snapshotRects) {
|
|
23
|
+
const candidates = [];
|
|
24
|
+
for (const [id, rect] of snapshotRects) {
|
|
25
|
+
candidates.push({ id, rect });
|
|
26
|
+
}
|
|
27
|
+
return candidates;
|
|
28
|
+
}
|
|
29
|
+
function pointerToRect(x, y) {
|
|
30
|
+
return {
|
|
31
|
+
top: y,
|
|
32
|
+
left: x,
|
|
33
|
+
width: 1,
|
|
34
|
+
height: 1,
|
|
35
|
+
right: x + 1,
|
|
36
|
+
bottom: y + 1
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function detectCollision(detector, snapshotRects, pointerX, pointerY) {
|
|
40
|
+
const candidates = buildCandidates(snapshotRects);
|
|
41
|
+
const pointerRect = pointerToRect(pointerX, pointerY);
|
|
42
|
+
const results = detector(candidates, pointerRect);
|
|
43
|
+
return results.length > 0 ? results[0].id : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/drag-overlay.ts
|
|
47
|
+
var DragOverlay = class {
|
|
48
|
+
constructor(options = {}) {
|
|
49
|
+
this.overlay = null;
|
|
50
|
+
this.options = options;
|
|
51
|
+
}
|
|
52
|
+
show(block, sourceEl, x, y) {
|
|
53
|
+
this.hide();
|
|
54
|
+
const overlay = document.createElement("div");
|
|
55
|
+
overlay.setAttribute("data-drag-overlay", "true");
|
|
56
|
+
overlay.style.position = "fixed";
|
|
57
|
+
overlay.style.zIndex = "9999";
|
|
58
|
+
overlay.style.pointerEvents = "none";
|
|
59
|
+
overlay.style.opacity = "0.7";
|
|
60
|
+
overlay.style.backdropFilter = "blur(4px)";
|
|
61
|
+
overlay.style.willChange = "transform";
|
|
62
|
+
if (this.options.renderOverlay) {
|
|
63
|
+
overlay.appendChild(this.options.renderOverlay(block));
|
|
64
|
+
} else {
|
|
65
|
+
const rect = sourceEl.getBoundingClientRect();
|
|
66
|
+
overlay.style.width = `${rect.width}px`;
|
|
67
|
+
const clone = sourceEl.cloneNode(true);
|
|
68
|
+
clone.style.margin = "0";
|
|
69
|
+
overlay.appendChild(clone);
|
|
70
|
+
}
|
|
71
|
+
this.setPosition(overlay, x, y);
|
|
72
|
+
document.body.appendChild(overlay);
|
|
73
|
+
this.overlay = overlay;
|
|
74
|
+
}
|
|
75
|
+
move(x, y) {
|
|
76
|
+
if (this.overlay) {
|
|
77
|
+
this.setPosition(this.overlay, x, y);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
hide() {
|
|
81
|
+
if (this.overlay) {
|
|
82
|
+
this.overlay.remove();
|
|
83
|
+
this.overlay = null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
setPosition(el, x, y) {
|
|
87
|
+
el.style.transform = `translate(${x}px, ${y}px)`;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/utils/dom.ts
|
|
92
|
+
function createElement(tag, attrs, children) {
|
|
93
|
+
const el = document.createElement(tag);
|
|
94
|
+
if (attrs) {
|
|
95
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
96
|
+
el.setAttribute(key, value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (children) {
|
|
100
|
+
for (const child of children) {
|
|
101
|
+
if (typeof child === "string") {
|
|
102
|
+
el.appendChild(document.createTextNode(child));
|
|
103
|
+
} else {
|
|
104
|
+
el.appendChild(child);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return el;
|
|
109
|
+
}
|
|
110
|
+
function setDataAttributes(el, data) {
|
|
111
|
+
for (const [key, value] of Object.entries(data)) {
|
|
112
|
+
if (value === false) {
|
|
113
|
+
el.removeAttribute(`data-${key}`);
|
|
114
|
+
} else {
|
|
115
|
+
el.setAttribute(`data-${key}`, String(value));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function closestWithData(el, dataAttr) {
|
|
120
|
+
return el.closest(`[data-${dataAttr}]`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/sensors/pointer-sensor.ts
|
|
124
|
+
var PointerSensor = class {
|
|
125
|
+
constructor(callbacks, options = {}) {
|
|
126
|
+
this.container = null;
|
|
127
|
+
this.isDragging = false;
|
|
128
|
+
this.startX = 0;
|
|
129
|
+
this.startY = 0;
|
|
130
|
+
this.blockId = null;
|
|
131
|
+
this.callbacks = callbacks;
|
|
132
|
+
this.activationDistance = options.activationDistance ?? 8;
|
|
133
|
+
this.boundPointerDown = this.onPointerDown.bind(this);
|
|
134
|
+
this.boundPointerMove = this.onPointerMove.bind(this);
|
|
135
|
+
this.boundPointerUp = this.onPointerUp.bind(this);
|
|
136
|
+
}
|
|
137
|
+
attach(container) {
|
|
138
|
+
this.container = container;
|
|
139
|
+
container.addEventListener("pointerdown", this.boundPointerDown);
|
|
140
|
+
}
|
|
141
|
+
detach() {
|
|
142
|
+
this.cleanup();
|
|
143
|
+
this.container?.removeEventListener("pointerdown", this.boundPointerDown);
|
|
144
|
+
this.container = null;
|
|
145
|
+
}
|
|
146
|
+
onPointerDown(e) {
|
|
147
|
+
if (e.button !== 0) return;
|
|
148
|
+
const draggable = closestWithData(e.target, "draggable-id");
|
|
149
|
+
if (!draggable) return;
|
|
150
|
+
const id = draggable.getAttribute("data-draggable-id");
|
|
151
|
+
if (!id) return;
|
|
152
|
+
this.blockId = id;
|
|
153
|
+
this.startX = e.clientX;
|
|
154
|
+
this.startY = e.clientY;
|
|
155
|
+
this.isDragging = false;
|
|
156
|
+
document.addEventListener("pointermove", this.boundPointerMove);
|
|
157
|
+
document.addEventListener("pointerup", this.boundPointerUp);
|
|
158
|
+
}
|
|
159
|
+
onPointerMove(e) {
|
|
160
|
+
const dx = e.clientX - this.startX;
|
|
161
|
+
const dy = e.clientY - this.startY;
|
|
162
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
163
|
+
if (!this.isDragging) {
|
|
164
|
+
if (distance >= this.activationDistance) {
|
|
165
|
+
this.isDragging = true;
|
|
166
|
+
this.callbacks.onDragStart(this.blockId, this.startX, this.startY);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
this.callbacks.onDragMove(e.clientX, e.clientY);
|
|
171
|
+
}
|
|
172
|
+
onPointerUp(e) {
|
|
173
|
+
if (this.isDragging) {
|
|
174
|
+
this.callbacks.onDragEnd(e.clientX, e.clientY);
|
|
175
|
+
}
|
|
176
|
+
this.cleanup();
|
|
177
|
+
}
|
|
178
|
+
cleanup() {
|
|
179
|
+
this.isDragging = false;
|
|
180
|
+
this.blockId = null;
|
|
181
|
+
document.removeEventListener("pointermove", this.boundPointerMove);
|
|
182
|
+
document.removeEventListener("pointerup", this.boundPointerUp);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/utils/haptic.ts
|
|
187
|
+
function triggerHaptic(durationMs = 10) {
|
|
188
|
+
if (typeof navigator !== "undefined" && typeof navigator.vibrate === "function") {
|
|
189
|
+
navigator.vibrate(durationMs);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// src/sensors/touch-sensor.ts
|
|
194
|
+
var TouchSensor = class {
|
|
195
|
+
constructor(callbacks, options = {}) {
|
|
196
|
+
this.container = null;
|
|
197
|
+
this.isDragging = false;
|
|
198
|
+
this.pressTimer = null;
|
|
199
|
+
this.blockId = null;
|
|
200
|
+
this.callbacks = callbacks;
|
|
201
|
+
this.longPressDelay = options.longPressDelay ?? 200;
|
|
202
|
+
this.hapticFeedback = options.hapticFeedback ?? true;
|
|
203
|
+
this.boundTouchStart = this.onTouchStart.bind(this);
|
|
204
|
+
this.boundTouchMove = this.onTouchMove.bind(this);
|
|
205
|
+
this.boundTouchEnd = this.onTouchEnd.bind(this);
|
|
206
|
+
}
|
|
207
|
+
attach(container) {
|
|
208
|
+
this.container = container;
|
|
209
|
+
container.addEventListener("touchstart", this.boundTouchStart, { passive: false });
|
|
210
|
+
}
|
|
211
|
+
detach() {
|
|
212
|
+
this.cleanup();
|
|
213
|
+
this.container?.removeEventListener("touchstart", this.boundTouchStart);
|
|
214
|
+
this.container = null;
|
|
215
|
+
}
|
|
216
|
+
onTouchStart(e) {
|
|
217
|
+
if (e.touches.length !== 1) return;
|
|
218
|
+
const draggable = closestWithData(e.target, "draggable-id");
|
|
219
|
+
if (!draggable) return;
|
|
220
|
+
const id = draggable.getAttribute("data-draggable-id");
|
|
221
|
+
if (!id) return;
|
|
222
|
+
this.blockId = id;
|
|
223
|
+
const touch = e.touches[0];
|
|
224
|
+
this.pressTimer = setTimeout(() => {
|
|
225
|
+
this.isDragging = true;
|
|
226
|
+
if (this.hapticFeedback) triggerHaptic();
|
|
227
|
+
this.callbacks.onDragStart(this.blockId, touch.clientX, touch.clientY);
|
|
228
|
+
}, this.longPressDelay);
|
|
229
|
+
document.addEventListener("touchmove", this.boundTouchMove, { passive: false });
|
|
230
|
+
document.addEventListener("touchend", this.boundTouchEnd);
|
|
231
|
+
document.addEventListener("touchcancel", this.boundTouchEnd);
|
|
232
|
+
}
|
|
233
|
+
onTouchMove(e) {
|
|
234
|
+
const touch = e.touches[0];
|
|
235
|
+
if (!this.isDragging) {
|
|
236
|
+
if (this.pressTimer) {
|
|
237
|
+
clearTimeout(this.pressTimer);
|
|
238
|
+
this.pressTimer = null;
|
|
239
|
+
this.cleanup();
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
this.callbacks.onDragMove(touch.clientX, touch.clientY);
|
|
245
|
+
}
|
|
246
|
+
onTouchEnd(e) {
|
|
247
|
+
if (this.isDragging) {
|
|
248
|
+
const touch = e.changedTouches[0];
|
|
249
|
+
this.callbacks.onDragEnd(touch.clientX, touch.clientY);
|
|
250
|
+
}
|
|
251
|
+
this.cleanup();
|
|
252
|
+
}
|
|
253
|
+
cleanup() {
|
|
254
|
+
if (this.pressTimer) {
|
|
255
|
+
clearTimeout(this.pressTimer);
|
|
256
|
+
this.pressTimer = null;
|
|
257
|
+
}
|
|
258
|
+
this.isDragging = false;
|
|
259
|
+
this.blockId = null;
|
|
260
|
+
document.removeEventListener("touchmove", this.boundTouchMove);
|
|
261
|
+
document.removeEventListener("touchend", this.boundTouchEnd);
|
|
262
|
+
document.removeEventListener("touchcancel", this.boundTouchEnd);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// src/sensors/keyboard-sensor.ts
|
|
267
|
+
var KeyboardSensor = class {
|
|
268
|
+
constructor(callbacks) {
|
|
269
|
+
this.container = null;
|
|
270
|
+
this.callbacks = callbacks;
|
|
271
|
+
this.boundKeyDown = this.onKeyDown.bind(this);
|
|
272
|
+
}
|
|
273
|
+
attach(container) {
|
|
274
|
+
this.container = container;
|
|
275
|
+
container.addEventListener("keydown", this.boundKeyDown);
|
|
276
|
+
}
|
|
277
|
+
detach() {
|
|
278
|
+
this.container?.removeEventListener("keydown", this.boundKeyDown);
|
|
279
|
+
this.container = null;
|
|
280
|
+
}
|
|
281
|
+
onKeyDown(e) {
|
|
282
|
+
switch (e.key) {
|
|
283
|
+
case "ArrowUp":
|
|
284
|
+
e.preventDefault();
|
|
285
|
+
this.callbacks.onFocusPrev();
|
|
286
|
+
break;
|
|
287
|
+
case "ArrowDown":
|
|
288
|
+
e.preventDefault();
|
|
289
|
+
this.callbacks.onFocusNext();
|
|
290
|
+
break;
|
|
291
|
+
case "ArrowRight":
|
|
292
|
+
e.preventDefault();
|
|
293
|
+
this.callbacks.onExpand();
|
|
294
|
+
break;
|
|
295
|
+
case "ArrowLeft":
|
|
296
|
+
e.preventDefault();
|
|
297
|
+
this.callbacks.onCollapse();
|
|
298
|
+
break;
|
|
299
|
+
case "Home":
|
|
300
|
+
e.preventDefault();
|
|
301
|
+
this.callbacks.onFocusFirst();
|
|
302
|
+
break;
|
|
303
|
+
case "End":
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
this.callbacks.onFocusLast();
|
|
306
|
+
break;
|
|
307
|
+
case "Enter":
|
|
308
|
+
case " ":
|
|
309
|
+
e.preventDefault();
|
|
310
|
+
this.callbacks.onToggleExpand();
|
|
311
|
+
break;
|
|
312
|
+
case "Escape":
|
|
313
|
+
this.callbacks.onDragCancel();
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
function createBlockHistory(initialBlocks, options = {}) {
|
|
319
|
+
const { maxSteps = 50 } = options;
|
|
320
|
+
let state = {
|
|
321
|
+
past: [],
|
|
322
|
+
present: initialBlocks,
|
|
323
|
+
future: []
|
|
324
|
+
};
|
|
325
|
+
return {
|
|
326
|
+
push(blocks) {
|
|
327
|
+
state = historyReducer(state, { type: "SET", payload: blocks, maxSteps });
|
|
328
|
+
},
|
|
329
|
+
undo() {
|
|
330
|
+
if (state.past.length === 0) return null;
|
|
331
|
+
state = historyReducer(state, { type: "UNDO" });
|
|
332
|
+
return state.present;
|
|
333
|
+
},
|
|
334
|
+
redo() {
|
|
335
|
+
if (state.future.length === 0) return null;
|
|
336
|
+
state = historyReducer(state, { type: "REDO" });
|
|
337
|
+
return state.present;
|
|
338
|
+
},
|
|
339
|
+
canUndo: () => state.past.length > 0,
|
|
340
|
+
canRedo: () => state.future.length > 0,
|
|
341
|
+
getPresent: () => state.present,
|
|
342
|
+
clear(blocks) {
|
|
343
|
+
state = { past: [], present: blocks, future: [] };
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/controller.ts
|
|
349
|
+
function createBlockTreeController(options = {}) {
|
|
350
|
+
const {
|
|
351
|
+
initialBlocks = [],
|
|
352
|
+
containerTypes = [],
|
|
353
|
+
orderingStrategy,
|
|
354
|
+
maxDepth,
|
|
355
|
+
previewDebounce,
|
|
356
|
+
canDrag,
|
|
357
|
+
canDrop,
|
|
358
|
+
idGenerator,
|
|
359
|
+
initialExpanded,
|
|
360
|
+
sensors: sensorConfig,
|
|
361
|
+
onChange,
|
|
362
|
+
callbacks
|
|
363
|
+
} = options;
|
|
364
|
+
const stickyCollision = createStickyCollision(15);
|
|
365
|
+
const tree = createBlockTree({
|
|
366
|
+
initialBlocks,
|
|
367
|
+
containerTypes,
|
|
368
|
+
orderingStrategy,
|
|
369
|
+
maxDepth,
|
|
370
|
+
previewDebounce,
|
|
371
|
+
canDrag,
|
|
372
|
+
canDrop,
|
|
373
|
+
idGenerator,
|
|
374
|
+
initialExpanded,
|
|
375
|
+
collisionDetection: stickyCollision
|
|
376
|
+
});
|
|
377
|
+
const emitter = new EventEmitter();
|
|
378
|
+
let container = null;
|
|
379
|
+
const draggableElements = /* @__PURE__ */ new Map();
|
|
380
|
+
const dropZoneElements = /* @__PURE__ */ new Map();
|
|
381
|
+
let snapshotRects = null;
|
|
382
|
+
const selectedIds = /* @__PURE__ */ new Set();
|
|
383
|
+
let lastSelectedId = null;
|
|
384
|
+
const activeSensors = [];
|
|
385
|
+
const overlay = new DragOverlay();
|
|
386
|
+
let overlayRenderer = null;
|
|
387
|
+
let history = null;
|
|
388
|
+
tree.on("blocks:change", (blocks) => {
|
|
389
|
+
onChange?.(blocks);
|
|
390
|
+
if (history) {
|
|
391
|
+
history.push(blocks);
|
|
392
|
+
}
|
|
393
|
+
emitter.emit("render", blocks, tree.getExpandedMap());
|
|
394
|
+
});
|
|
395
|
+
tree.on("expand:change", () => {
|
|
396
|
+
emitter.emit("render", tree.getBlocks(), tree.getExpandedMap());
|
|
397
|
+
});
|
|
398
|
+
if (callbacks?.onDragStart) {
|
|
399
|
+
tree.on("drag:start", (e) => callbacks.onDragStart(e));
|
|
400
|
+
}
|
|
401
|
+
if (callbacks?.onDragEnd) {
|
|
402
|
+
tree.on("drag:end", (e) => callbacks.onDragEnd(e));
|
|
403
|
+
}
|
|
404
|
+
if (callbacks?.onBlockAdd) {
|
|
405
|
+
tree.on("block:add", (e) => callbacks.onBlockAdd(e));
|
|
406
|
+
}
|
|
407
|
+
if (callbacks?.onBlockDelete) {
|
|
408
|
+
tree.on("block:delete", (e) => callbacks.onBlockDelete(e));
|
|
409
|
+
}
|
|
410
|
+
if (callbacks?.onExpandChange) {
|
|
411
|
+
tree.on("expand:change", (e) => callbacks.onExpandChange(e));
|
|
412
|
+
}
|
|
413
|
+
if (callbacks?.onHoverChange) {
|
|
414
|
+
tree.on("hover:change", (e) => callbacks.onHoverChange(e));
|
|
415
|
+
}
|
|
416
|
+
const sensorCallbacks = {
|
|
417
|
+
onDragStart(blockId, x, y) {
|
|
418
|
+
const draggedIds = selectedIds.has(blockId) && selectedIds.size > 1 ? [...selectedIds] : [blockId];
|
|
419
|
+
const started = tree.startDrag(blockId, draggedIds);
|
|
420
|
+
if (!started) return;
|
|
421
|
+
stickyCollision.reset();
|
|
422
|
+
snapshotRects = measureDropZoneRects(dropZoneElements);
|
|
423
|
+
const block = tree.getBlock(blockId);
|
|
424
|
+
const el = draggableElements.get(blockId);
|
|
425
|
+
if (block && el) {
|
|
426
|
+
if (overlayRenderer) {
|
|
427
|
+
overlay.show(block, el, x, y);
|
|
428
|
+
} else {
|
|
429
|
+
overlay.show(block, el, x, y);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
emitter.emit("drag:statechange", getDragState());
|
|
433
|
+
},
|
|
434
|
+
onDragMove(x, y) {
|
|
435
|
+
if (!snapshotRects) return;
|
|
436
|
+
overlay.move(x, y);
|
|
437
|
+
const detector = tree.getCollisionDetection();
|
|
438
|
+
if (!detector) return;
|
|
439
|
+
const targetZone = detectCollision(detector, snapshotRects, x, y);
|
|
440
|
+
if (targetZone) {
|
|
441
|
+
tree.updateDrag(targetZone);
|
|
442
|
+
}
|
|
443
|
+
},
|
|
444
|
+
onDragEnd(_x, _y) {
|
|
445
|
+
const result = tree.endDrag();
|
|
446
|
+
overlay.hide();
|
|
447
|
+
snapshotRects = null;
|
|
448
|
+
if (result && callbacks?.onBlockMove) {
|
|
449
|
+
callbacks.onBlockMove({
|
|
450
|
+
block: tree.getBlock(tree.getBlocks()[0]?.id),
|
|
451
|
+
from: { parentId: null, index: 0 },
|
|
452
|
+
to: { parentId: null, index: 0 },
|
|
453
|
+
blocks: result.blocks,
|
|
454
|
+
movedIds: []
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
emitter.emit("drag:statechange", getDragState());
|
|
458
|
+
emitter.emit("render", tree.getBlocks(), tree.getExpandedMap());
|
|
459
|
+
},
|
|
460
|
+
onDragCancel() {
|
|
461
|
+
tree.cancelDrag();
|
|
462
|
+
overlay.hide();
|
|
463
|
+
snapshotRects = null;
|
|
464
|
+
emitter.emit("drag:statechange", getDragState());
|
|
465
|
+
emitter.emit("render", tree.getBlocks(), tree.getExpandedMap());
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
function getDragState() {
|
|
469
|
+
return {
|
|
470
|
+
isDragging: tree.getActiveId() !== null,
|
|
471
|
+
activeId: tree.getActiveId(),
|
|
472
|
+
hoverZone: tree.getHoverZone()
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function setupSensors() {
|
|
476
|
+
const config = sensorConfig ?? {};
|
|
477
|
+
if (!container) return;
|
|
478
|
+
if (config.pointer !== false) {
|
|
479
|
+
const pointer = new PointerSensor(sensorCallbacks, {
|
|
480
|
+
activationDistance: config.activationDistance
|
|
481
|
+
});
|
|
482
|
+
pointer.attach(container);
|
|
483
|
+
activeSensors.push(pointer);
|
|
484
|
+
}
|
|
485
|
+
if (config.touch !== false) {
|
|
486
|
+
const touch = new TouchSensor(sensorCallbacks, {
|
|
487
|
+
longPressDelay: config.longPressDelay,
|
|
488
|
+
hapticFeedback: config.hapticFeedback
|
|
489
|
+
});
|
|
490
|
+
touch.attach(container);
|
|
491
|
+
activeSensors.push(touch);
|
|
492
|
+
}
|
|
493
|
+
if (config.keyboard) {
|
|
494
|
+
const visibleBlocks = () => tree.getEffectiveBlocks();
|
|
495
|
+
let focusedIndex = -1;
|
|
496
|
+
const keyboard = new KeyboardSensor({
|
|
497
|
+
...sensorCallbacks,
|
|
498
|
+
onFocusPrev() {
|
|
499
|
+
const blocks = visibleBlocks();
|
|
500
|
+
focusedIndex = Math.max(0, focusedIndex - 1);
|
|
501
|
+
const block = blocks[focusedIndex];
|
|
502
|
+
if (block) focusDraggable(block.id);
|
|
503
|
+
},
|
|
504
|
+
onFocusNext() {
|
|
505
|
+
const blocks = visibleBlocks();
|
|
506
|
+
focusedIndex = Math.min(blocks.length - 1, focusedIndex + 1);
|
|
507
|
+
const block = blocks[focusedIndex];
|
|
508
|
+
if (block) focusDraggable(block.id);
|
|
509
|
+
},
|
|
510
|
+
onExpand() {
|
|
511
|
+
const blocks = visibleBlocks();
|
|
512
|
+
const block = blocks[focusedIndex];
|
|
513
|
+
if (block && containerTypes.includes(block.type) && !tree.isExpanded(block.id)) {
|
|
514
|
+
tree.toggleExpand(block.id);
|
|
515
|
+
}
|
|
516
|
+
},
|
|
517
|
+
onCollapse() {
|
|
518
|
+
const blocks = visibleBlocks();
|
|
519
|
+
const block = blocks[focusedIndex];
|
|
520
|
+
if (block && containerTypes.includes(block.type) && tree.isExpanded(block.id)) {
|
|
521
|
+
tree.toggleExpand(block.id);
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
onFocusFirst() {
|
|
525
|
+
focusedIndex = 0;
|
|
526
|
+
const blocks = visibleBlocks();
|
|
527
|
+
if (blocks[0]) focusDraggable(blocks[0].id);
|
|
528
|
+
},
|
|
529
|
+
onFocusLast() {
|
|
530
|
+
const blocks = visibleBlocks();
|
|
531
|
+
focusedIndex = blocks.length - 1;
|
|
532
|
+
if (blocks[focusedIndex]) focusDraggable(blocks[focusedIndex].id);
|
|
533
|
+
},
|
|
534
|
+
onToggleExpand() {
|
|
535
|
+
const blocks = visibleBlocks();
|
|
536
|
+
const block = blocks[focusedIndex];
|
|
537
|
+
if (block && containerTypes.includes(block.type)) {
|
|
538
|
+
tree.toggleExpand(block.id);
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
onSelect() {
|
|
542
|
+
const blocks = visibleBlocks();
|
|
543
|
+
const block = blocks[focusedIndex];
|
|
544
|
+
if (block) {
|
|
545
|
+
controller.select(block.id, "toggle");
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
keyboard.attach(container);
|
|
550
|
+
activeSensors.push(keyboard);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
function focusDraggable(id) {
|
|
554
|
+
const el = draggableElements.get(id);
|
|
555
|
+
if (el) el.focus();
|
|
556
|
+
}
|
|
557
|
+
function teardownSensors() {
|
|
558
|
+
for (const sensor of activeSensors) {
|
|
559
|
+
sensor.detach();
|
|
560
|
+
}
|
|
561
|
+
activeSensors.length = 0;
|
|
562
|
+
}
|
|
563
|
+
const controller = {
|
|
564
|
+
mount(el) {
|
|
565
|
+
container = el;
|
|
566
|
+
setupSensors();
|
|
567
|
+
emitter.emit("render", tree.getBlocks(), tree.getExpandedMap());
|
|
568
|
+
},
|
|
569
|
+
unmount() {
|
|
570
|
+
teardownSensors();
|
|
571
|
+
overlay.hide();
|
|
572
|
+
container = null;
|
|
573
|
+
},
|
|
574
|
+
registerDraggable(id, element) {
|
|
575
|
+
element.setAttribute("data-draggable-id", id);
|
|
576
|
+
element.setAttribute("data-block-id", id);
|
|
577
|
+
draggableElements.set(id, element);
|
|
578
|
+
return () => {
|
|
579
|
+
draggableElements.delete(id);
|
|
580
|
+
};
|
|
581
|
+
},
|
|
582
|
+
registerDropZone(id, element) {
|
|
583
|
+
element.setAttribute("data-zone-id", id);
|
|
584
|
+
dropZoneElements.set(id, element);
|
|
585
|
+
return () => {
|
|
586
|
+
dropZoneElements.delete(id);
|
|
587
|
+
};
|
|
588
|
+
},
|
|
589
|
+
getDragState,
|
|
590
|
+
getBlocks: () => tree.getBlocks(),
|
|
591
|
+
getEffectiveBlocks: () => tree.getEffectiveBlocks(),
|
|
592
|
+
getExpandedMap: () => tree.getExpandedMap(),
|
|
593
|
+
getBlock: (id) => tree.getBlock(id),
|
|
594
|
+
toggleExpand(id) {
|
|
595
|
+
tree.toggleExpand(id);
|
|
596
|
+
},
|
|
597
|
+
setExpandAll(expanded) {
|
|
598
|
+
tree.setExpandAll(expanded);
|
|
599
|
+
},
|
|
600
|
+
addBlock(type, parentId = null) {
|
|
601
|
+
return tree.addBlock(type, parentId);
|
|
602
|
+
},
|
|
603
|
+
deleteBlock(id) {
|
|
604
|
+
tree.deleteBlock(id);
|
|
605
|
+
},
|
|
606
|
+
setBlocks(blocks) {
|
|
607
|
+
tree.setBlocks(blocks);
|
|
608
|
+
},
|
|
609
|
+
select(id, mode) {
|
|
610
|
+
if (mode === "single") {
|
|
611
|
+
selectedIds.clear();
|
|
612
|
+
selectedIds.add(id);
|
|
613
|
+
} else if (mode === "toggle") {
|
|
614
|
+
if (selectedIds.has(id)) {
|
|
615
|
+
selectedIds.delete(id);
|
|
616
|
+
} else {
|
|
617
|
+
selectedIds.add(id);
|
|
618
|
+
}
|
|
619
|
+
} else if (mode === "range" && lastSelectedId) {
|
|
620
|
+
const blocks = tree.getBlocks();
|
|
621
|
+
const startIdx = blocks.findIndex((b) => b.id === lastSelectedId);
|
|
622
|
+
const endIdx = blocks.findIndex((b) => b.id === id);
|
|
623
|
+
if (startIdx !== -1 && endIdx !== -1) {
|
|
624
|
+
const [from, to] = startIdx < endIdx ? [startIdx, endIdx] : [endIdx, startIdx];
|
|
625
|
+
for (let i = from; i <= to; i++) {
|
|
626
|
+
selectedIds.add(blocks[i].id);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
lastSelectedId = id;
|
|
631
|
+
emitter.emit("selection:change", new Set(selectedIds));
|
|
632
|
+
},
|
|
633
|
+
clearSelection() {
|
|
634
|
+
selectedIds.clear();
|
|
635
|
+
lastSelectedId = null;
|
|
636
|
+
emitter.emit("selection:change", /* @__PURE__ */ new Set());
|
|
637
|
+
},
|
|
638
|
+
getSelectedIds: () => new Set(selectedIds),
|
|
639
|
+
enableHistory(maxSteps = 50) {
|
|
640
|
+
history = createBlockHistory(tree.getBlocks(), { maxSteps });
|
|
641
|
+
},
|
|
642
|
+
undo() {
|
|
643
|
+
if (!history) return null;
|
|
644
|
+
const blocks = history.undo();
|
|
645
|
+
if (blocks) tree.setBlocks(blocks);
|
|
646
|
+
return blocks;
|
|
647
|
+
},
|
|
648
|
+
redo() {
|
|
649
|
+
if (!history) return null;
|
|
650
|
+
const blocks = history.redo();
|
|
651
|
+
if (blocks) tree.setBlocks(blocks);
|
|
652
|
+
return blocks;
|
|
653
|
+
},
|
|
654
|
+
canUndo: () => history?.canUndo() ?? false,
|
|
655
|
+
canRedo: () => history?.canRedo() ?? false,
|
|
656
|
+
on(event, handler) {
|
|
657
|
+
return emitter.on(event, handler);
|
|
658
|
+
},
|
|
659
|
+
setOverlayRenderer(render) {
|
|
660
|
+
overlayRenderer = render;
|
|
661
|
+
},
|
|
662
|
+
getTree: () => tree,
|
|
663
|
+
destroy() {
|
|
664
|
+
teardownSensors();
|
|
665
|
+
overlay.hide();
|
|
666
|
+
tree.destroy();
|
|
667
|
+
emitter.removeAllListeners();
|
|
668
|
+
draggableElements.clear();
|
|
669
|
+
dropZoneElements.clear();
|
|
670
|
+
selectedIds.clear();
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
return controller;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
// src/layout-animation.ts
|
|
677
|
+
var LayoutAnimation = class {
|
|
678
|
+
constructor(options = {}) {
|
|
679
|
+
this.snapshots = /* @__PURE__ */ new Map();
|
|
680
|
+
this.duration = options.duration ?? 200;
|
|
681
|
+
this.easing = options.easing ?? "ease";
|
|
682
|
+
}
|
|
683
|
+
/** Record current positions of all block elements in a container */
|
|
684
|
+
snapshot(container) {
|
|
685
|
+
this.snapshots.clear();
|
|
686
|
+
const blocks = container.querySelectorAll("[data-block-id]");
|
|
687
|
+
for (const el of blocks) {
|
|
688
|
+
const id = el.getAttribute("data-block-id");
|
|
689
|
+
if (id) {
|
|
690
|
+
this.snapshots.set(id, el.getBoundingClientRect());
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
/** After DOM update, animate elements from old to new positions */
|
|
695
|
+
animate(container) {
|
|
696
|
+
if (this.snapshots.size === 0) return;
|
|
697
|
+
const blocks = container.querySelectorAll("[data-block-id]");
|
|
698
|
+
for (const el of blocks) {
|
|
699
|
+
const id = el.getAttribute("data-block-id");
|
|
700
|
+
if (!id) continue;
|
|
701
|
+
const oldRect = this.snapshots.get(id);
|
|
702
|
+
if (!oldRect) continue;
|
|
703
|
+
const newRect = el.getBoundingClientRect();
|
|
704
|
+
const deltaX = oldRect.left - newRect.left;
|
|
705
|
+
const deltaY = oldRect.top - newRect.top;
|
|
706
|
+
if (Math.abs(deltaX) < 1 && Math.abs(deltaY) < 1) continue;
|
|
707
|
+
el.style.transform = `translate(${deltaX}px, ${deltaY}px)`;
|
|
708
|
+
el.style.transition = "none";
|
|
709
|
+
requestAnimationFrame(() => {
|
|
710
|
+
el.style.transition = `transform ${this.duration}ms ${this.easing}`;
|
|
711
|
+
el.style.transform = "";
|
|
712
|
+
const onEnd = () => {
|
|
713
|
+
el.style.transition = "";
|
|
714
|
+
el.removeEventListener("transitionend", onEnd);
|
|
715
|
+
};
|
|
716
|
+
el.addEventListener("transitionend", onEnd, { once: true });
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
this.snapshots.clear();
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// src/virtual-scroller.ts
|
|
724
|
+
var VirtualScroller = class {
|
|
725
|
+
constructor(options) {
|
|
726
|
+
this.itemHeight = options.itemHeight;
|
|
727
|
+
this.overscan = options.overscan ?? 5;
|
|
728
|
+
}
|
|
729
|
+
/** Calculate the visible range for a given scroll state */
|
|
730
|
+
calculate(scrollTop, viewportHeight, totalItems, blockIds) {
|
|
731
|
+
const startIndex = Math.max(0, Math.floor(scrollTop / this.itemHeight) - this.overscan);
|
|
732
|
+
const visibleCount = Math.ceil(viewportHeight / this.itemHeight) + 2 * this.overscan;
|
|
733
|
+
const endIndex = Math.min(totalItems, startIndex + visibleCount);
|
|
734
|
+
const visibleIds = /* @__PURE__ */ new Set();
|
|
735
|
+
for (let i = startIndex; i < endIndex && i < blockIds.length; i++) {
|
|
736
|
+
visibleIds.add(blockIds[i]);
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
startIndex,
|
|
740
|
+
endIndex,
|
|
741
|
+
offsetY: startIndex * this.itemHeight,
|
|
742
|
+
totalHeight: totalItems * this.itemHeight,
|
|
743
|
+
visibleIds
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
// src/renderer/drop-zone.ts
|
|
749
|
+
function createDropZoneElement(options) {
|
|
750
|
+
const { id, height = 4 } = options;
|
|
751
|
+
const el = createElement("div");
|
|
752
|
+
setDataAttributes(el, {
|
|
753
|
+
"zone-id": id
|
|
754
|
+
});
|
|
755
|
+
el.style.height = `${height}px`;
|
|
756
|
+
el.style.minHeight = `${height}px`;
|
|
757
|
+
el.style.transition = "background-color 150ms ease";
|
|
758
|
+
return el;
|
|
759
|
+
}
|
|
760
|
+
function setDropZoneActive(el, active) {
|
|
761
|
+
setDataAttributes(el, { "zone-active": active });
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// src/renderer/tree-renderer.ts
|
|
765
|
+
function renderTree(blocks, expandedMap, controller, options, parentId = null, depth = 0) {
|
|
766
|
+
const { renderBlock, containerTypes, dropZoneHeight } = options;
|
|
767
|
+
const container = createElement("div", {
|
|
768
|
+
role: parentId === null ? "tree" : "group"
|
|
769
|
+
});
|
|
770
|
+
if (parentId === null) {
|
|
771
|
+
container.setAttribute("data-dnd-tree-root", "true");
|
|
772
|
+
}
|
|
773
|
+
const children = blocks.filter((b) => b.parentId === parentId);
|
|
774
|
+
const activeId = controller.getDragState().activeId;
|
|
775
|
+
const selectedIds = controller.getSelectedIds();
|
|
776
|
+
const index = controller.getTree().getBlockIndex();
|
|
777
|
+
if (parentId !== null) {
|
|
778
|
+
const startZone = createDropZoneElement({ id: `into-${parentId}`, height: dropZoneHeight });
|
|
779
|
+
controller.registerDropZone(`into-${parentId}`, startZone);
|
|
780
|
+
container.appendChild(startZone);
|
|
781
|
+
} else {
|
|
782
|
+
const rootStart = createDropZoneElement({ id: "root-start", height: dropZoneHeight });
|
|
783
|
+
controller.registerDropZone("root-start", rootStart);
|
|
784
|
+
container.appendChild(rootStart);
|
|
785
|
+
}
|
|
786
|
+
for (const block of children) {
|
|
787
|
+
if (block.id === activeId) continue;
|
|
788
|
+
const beforeZone = createDropZoneElement({ id: `before-${block.id}`, height: dropZoneHeight });
|
|
789
|
+
controller.registerDropZone(`before-${block.id}`, beforeZone);
|
|
790
|
+
container.appendChild(beforeZone);
|
|
791
|
+
const isContainer = containerTypes.includes(block.type);
|
|
792
|
+
const isExpanded = expandedMap[block.id] !== false;
|
|
793
|
+
const blockDepth = getBlockDepth(index, block.id);
|
|
794
|
+
let childrenEl = null;
|
|
795
|
+
if (isContainer && isExpanded) {
|
|
796
|
+
childrenEl = renderTree(blocks, expandedMap, controller, options, block.id, depth + 1);
|
|
797
|
+
}
|
|
798
|
+
const ctx = {
|
|
799
|
+
children: childrenEl,
|
|
800
|
+
depth: blockDepth,
|
|
801
|
+
isExpanded,
|
|
802
|
+
isDragging: block.id === activeId,
|
|
803
|
+
isSelected: selectedIds.has(block.id),
|
|
804
|
+
onToggleExpand: isContainer ? () => controller.toggleExpand(block.id) : null
|
|
805
|
+
};
|
|
806
|
+
const blockEl = renderBlock(block, ctx);
|
|
807
|
+
setDataAttributes(blockEl, {
|
|
808
|
+
"block-id": block.id,
|
|
809
|
+
"block-type": block.type,
|
|
810
|
+
depth: String(blockDepth),
|
|
811
|
+
dragging: block.id === activeId,
|
|
812
|
+
selected: selectedIds.has(block.id)
|
|
813
|
+
});
|
|
814
|
+
controller.registerDraggable(block.id, blockEl);
|
|
815
|
+
blockEl.setAttribute("role", "treeitem");
|
|
816
|
+
blockEl.setAttribute("aria-level", String(blockDepth + 1));
|
|
817
|
+
if (isContainer) {
|
|
818
|
+
blockEl.setAttribute("aria-expanded", String(isExpanded));
|
|
819
|
+
}
|
|
820
|
+
blockEl.setAttribute("tabindex", "-1");
|
|
821
|
+
container.appendChild(blockEl);
|
|
822
|
+
}
|
|
823
|
+
if (parentId !== null) {
|
|
824
|
+
const endZone = createDropZoneElement({ id: `end-${parentId}`, height: dropZoneHeight });
|
|
825
|
+
controller.registerDropZone(`end-${parentId}`, endZone);
|
|
826
|
+
container.appendChild(endZone);
|
|
827
|
+
} else {
|
|
828
|
+
const rootEnd = createDropZoneElement({ id: "root-end", height: dropZoneHeight });
|
|
829
|
+
controller.registerDropZone("root-end", rootEnd);
|
|
830
|
+
container.appendChild(rootEnd);
|
|
831
|
+
}
|
|
832
|
+
return container;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// src/renderer/default-renderer.ts
|
|
836
|
+
function createDefaultRenderer(controller, options) {
|
|
837
|
+
const { container, renderBlock, dropZoneHeight, animateExpand } = options;
|
|
838
|
+
const containerTypes = controller.getTree().containerTypes ?? [];
|
|
839
|
+
function render(blocks, expandedMap) {
|
|
840
|
+
container.innerHTML = "";
|
|
841
|
+
const tree = renderTree(blocks, expandedMap, controller, {
|
|
842
|
+
renderBlock,
|
|
843
|
+
containerTypes,
|
|
844
|
+
dropZoneHeight
|
|
845
|
+
});
|
|
846
|
+
container.appendChild(tree);
|
|
847
|
+
}
|
|
848
|
+
const unsubRender = controller.on("render", render);
|
|
849
|
+
render(controller.getBlocks(), controller.getExpandedMap());
|
|
850
|
+
const renderer = () => {
|
|
851
|
+
unsubRender();
|
|
852
|
+
};
|
|
853
|
+
renderer.refresh = () => {
|
|
854
|
+
render(controller.getBlocks(), controller.getExpandedMap());
|
|
855
|
+
};
|
|
856
|
+
return renderer;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// src/renderer/ghost-preview.ts
|
|
860
|
+
function createGhostPreview(sourceEl) {
|
|
861
|
+
const ghost = sourceEl.cloneNode(true);
|
|
862
|
+
setDataAttributes(ghost, { "dnd-ghost": "true" });
|
|
863
|
+
ghost.style.opacity = "0.3";
|
|
864
|
+
ghost.style.pointerEvents = "none";
|
|
865
|
+
ghost.removeAttribute("data-draggable-id");
|
|
866
|
+
ghost.removeAttribute("data-block-id");
|
|
867
|
+
return ghost;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// src/utils/disposable.ts
|
|
871
|
+
function createDisposable() {
|
|
872
|
+
const cleanups = [];
|
|
873
|
+
return {
|
|
874
|
+
add(fn) {
|
|
875
|
+
cleanups.push(fn);
|
|
876
|
+
},
|
|
877
|
+
dispose() {
|
|
878
|
+
for (const fn of cleanups.reverse()) {
|
|
879
|
+
fn();
|
|
880
|
+
}
|
|
881
|
+
cleanups.length = 0;
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
export { DragOverlay, KeyboardSensor, LayoutAnimation, PointerSensor, TouchSensor, VirtualScroller, buildCandidates, closestWithData, createBlockHistory, createBlockTreeController, createDefaultRenderer, createDisposable, createDropZoneElement, createElement, createGhostPreview, detectCollision, measureDropZoneRects, pointerToRect, renderTree, setDataAttributes, setDropZoneActive, triggerHaptic };
|
|
887
|
+
//# sourceMappingURL=index.mjs.map
|
|
888
|
+
//# sourceMappingURL=index.mjs.map
|