@hkdigital/lib-sveltekit 0.1.82 → 0.1.84
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.
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
import {
|
10
10
|
// findDraggableSource,
|
11
|
-
getDraggableIdFromEvent
|
11
|
+
getDraggableIdFromEvent
|
12
12
|
// processDropWithData
|
13
13
|
} from './util.js';
|
14
14
|
|
@@ -16,7 +16,7 @@
|
|
16
16
|
READY,
|
17
17
|
DRAG_OVER,
|
18
18
|
CAN_DROP,
|
19
|
-
CANNOT_DROP
|
19
|
+
CANNOT_DROP
|
20
20
|
// DROP_DISABLED
|
21
21
|
} from '../../constants/state-labels/drop-states.js';
|
22
22
|
|
@@ -75,7 +75,7 @@
|
|
75
75
|
base = '',
|
76
76
|
classes = '',
|
77
77
|
height = 'h-min',
|
78
|
-
autoHeight= false,
|
78
|
+
autoHeight = false,
|
79
79
|
children,
|
80
80
|
contextKey,
|
81
81
|
dropPreviewSnippet,
|
@@ -90,7 +90,6 @@
|
|
90
90
|
...attrs
|
91
91
|
} = $props();
|
92
92
|
|
93
|
-
|
94
93
|
const dragState = createOrGetDragState(contextKey);
|
95
94
|
|
96
95
|
// console.debug('DropZone contextKey:', contextKey);
|
@@ -136,11 +135,7 @@
|
|
136
135
|
|
137
136
|
// Update bindable props
|
138
137
|
$effect(() => {
|
139
|
-
isDragOver = [
|
140
|
-
DRAG_OVER,
|
141
|
-
CAN_DROP,
|
142
|
-
CANNOT_DROP
|
143
|
-
].includes(currentState);
|
138
|
+
isDragOver = [DRAG_OVER, CAN_DROP, CANNOT_DROP].includes(currentState);
|
144
139
|
|
145
140
|
canDrop = currentState === CAN_DROP;
|
146
141
|
});
|
@@ -162,96 +157,90 @@
|
|
162
157
|
return true;
|
163
158
|
}
|
164
159
|
|
165
|
-
/**
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
function handleDragEnter(event) {
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
// If we're already in a drag-over state, don't reset anything
|
174
|
-
if (isCurrentlyOver) return;
|
160
|
+
/**
|
161
|
+
* Handle drag enter with improved DOM traversal check
|
162
|
+
* @param {DragEvent} event
|
163
|
+
*/
|
164
|
+
function handleDragEnter(event) {
|
165
|
+
// Prevent default to allow drop
|
166
|
+
event.preventDefault();
|
175
167
|
|
176
|
-
|
177
|
-
|
168
|
+
// If we're already in a drag-over state, don't reset anything
|
169
|
+
if (isCurrentlyOver) return;
|
178
170
|
|
179
|
-
|
180
|
-
|
171
|
+
// Now we're over this dropzone
|
172
|
+
isCurrentlyOver = true;
|
181
173
|
|
182
|
-
|
183
|
-
|
184
|
-
const dragData = dragState.getDraggable(draggableId);
|
174
|
+
// Get the draggable ID from the event
|
175
|
+
const draggableId = getDraggableIdFromEvent(event);
|
185
176
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
const dragData = dragState.current;
|
197
|
-
|
198
|
-
if (dragData) {
|
199
|
-
currentState = canAcceptDrop(dragData)
|
200
|
-
? CAN_DROP
|
201
|
-
: CANNOT_DROP;
|
177
|
+
if (draggableId) {
|
178
|
+
// Get the drag data for this specific draggable
|
179
|
+
const dragData = dragState.getDraggable(draggableId);
|
180
|
+
|
181
|
+
// Update state based on acceptance
|
182
|
+
if (dragData) {
|
183
|
+
currentState = canAcceptDrop(dragData) ? CAN_DROP : CANNOT_DROP;
|
184
|
+
} else {
|
185
|
+
currentState = DRAG_OVER;
|
186
|
+
}
|
202
187
|
} else {
|
203
|
-
|
188
|
+
// Fallback to the current drag data (for compatibility)
|
189
|
+
const dragData = dragState.current;
|
190
|
+
|
191
|
+
if (dragData) {
|
192
|
+
currentState = canAcceptDrop(dragData) ? CAN_DROP : CANNOT_DROP;
|
193
|
+
} else {
|
194
|
+
currentState = DRAG_OVER;
|
195
|
+
}
|
204
196
|
}
|
205
|
-
}
|
206
197
|
|
207
|
-
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
/**
|
212
|
-
* Handle drag over
|
213
|
-
* @param {DragEvent} event
|
214
|
-
*/
|
215
|
-
function handleDragOver(event) {
|
216
|
-
// Prevent default to allow drop
|
217
|
-
event.preventDefault();
|
218
|
-
|
219
|
-
// If we're not currently over this dropzone (despite dragover firing),
|
220
|
-
// treat it as an enter event
|
221
|
-
if (!isCurrentlyOver) {
|
222
|
-
handleDragEnter(event);
|
223
|
-
return;
|
198
|
+
// Notify listeners
|
199
|
+
onDragEnter?.({ event, zone, canDrop: currentState === CAN_DROP });
|
224
200
|
}
|
225
201
|
|
226
|
-
|
227
|
-
|
228
|
-
|
202
|
+
/**
|
203
|
+
* Handle drag over
|
204
|
+
* @param {DragEvent} event
|
205
|
+
*/
|
206
|
+
function handleDragOver(event) {
|
207
|
+
// Prevent default to allow drop
|
208
|
+
event.preventDefault();
|
209
|
+
|
210
|
+
// If we're not currently over this dropzone (despite dragover firing),
|
211
|
+
// treat it as an enter event
|
212
|
+
if (!isCurrentlyOver) {
|
213
|
+
handleDragEnter(event);
|
214
|
+
return;
|
215
|
+
}
|
229
216
|
|
230
|
-
|
231
|
-
|
232
|
-
dragData
|
233
|
-
} else {
|
234
|
-
// Fallback to the current drag data (for compatibility)
|
235
|
-
dragData = dragState.current;
|
236
|
-
}
|
217
|
+
// Get the draggable ID from the event
|
218
|
+
const draggableId = getDraggableIdFromEvent(event);
|
219
|
+
let dragData;
|
237
220
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
221
|
+
if (draggableId) {
|
222
|
+
// Get the drag data for this specific draggable
|
223
|
+
dragData = dragState.getDraggable(draggableId);
|
224
|
+
} else {
|
225
|
+
// Fallback to the current drag data (for compatibility)
|
226
|
+
dragData = dragState.current;
|
227
|
+
}
|
244
228
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
229
|
+
// Re-evaluate acceptance
|
230
|
+
if (dragData && [DRAG_OVER, CAN_DROP, CANNOT_DROP].includes(currentState)) {
|
231
|
+
currentState = canAcceptDrop(dragData) ? CAN_DROP : CANNOT_DROP;
|
232
|
+
}
|
233
|
+
|
234
|
+
// Set visual feedback based on drop acceptance
|
235
|
+
if (currentState === CAN_DROP) {
|
236
|
+
event.dataTransfer.dropEffect = 'move';
|
237
|
+
} else if (currentState === CANNOT_DROP) {
|
238
|
+
event.dataTransfer.dropEffect = 'none';
|
239
|
+
}
|
251
240
|
|
252
|
-
|
253
|
-
|
254
|
-
}
|
241
|
+
// Notify listeners
|
242
|
+
onDragOver?.({ event, zone });
|
243
|
+
}
|
255
244
|
|
256
245
|
/**
|
257
246
|
* Handle drag leave with improved DOM traversal check
|
@@ -265,8 +254,8 @@ function handleDragOver(event) {
|
|
265
254
|
const relatedTarget = event.relatedTarget;
|
266
255
|
|
267
256
|
// If relatedTarget is null or outside our dropzone, we're truly leaving
|
268
|
-
const isActuallyLeaving =
|
269
|
-
|
257
|
+
const isActuallyLeaving =
|
258
|
+
!relatedTarget || !dropzoneElement.contains(relatedTarget);
|
270
259
|
|
271
260
|
if (isActuallyLeaving) {
|
272
261
|
isCurrentlyOver = false;
|
@@ -275,86 +264,96 @@ function handleDragOver(event) {
|
|
275
264
|
}
|
276
265
|
}
|
277
266
|
|
278
|
-
/**
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
function handleDrop(event) {
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
// Reset our tracking state
|
287
|
-
isCurrentlyOver = false;
|
267
|
+
/**
|
268
|
+
* Handle drop
|
269
|
+
* @param {DragEvent} event
|
270
|
+
*/
|
271
|
+
function handleDrop(event) {
|
272
|
+
// Prevent default browser actions
|
273
|
+
event.preventDefault();
|
288
274
|
|
289
|
-
|
290
|
-
|
291
|
-
const draggableId = getDraggableIdFromEvent(event);
|
292
|
-
let dragData;
|
275
|
+
// Reset our tracking state
|
276
|
+
isCurrentlyOver = false;
|
293
277
|
|
294
|
-
|
295
|
-
//
|
296
|
-
|
297
|
-
|
278
|
+
try {
|
279
|
+
// First try to get the draggable ID from the event
|
280
|
+
const draggableId = getDraggableIdFromEvent(event);
|
281
|
+
let dragData;
|
298
282
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
const jsonData = event.dataTransfer.getData('application/json');
|
303
|
-
if (jsonData) {
|
304
|
-
dragData = JSON.parse(jsonData);
|
283
|
+
if (draggableId) {
|
284
|
+
// Get the drag data from state using the draggable ID
|
285
|
+
dragData = dragState.getDraggable(draggableId);
|
305
286
|
}
|
306
|
-
}
|
307
|
-
|
308
|
-
// Check if we can accept this drop
|
309
|
-
if (dragData && canAcceptDrop(dragData)) {
|
310
|
-
// Notify listener
|
311
|
-
onDropStart?.({ event, zone, data: dragData });
|
312
|
-
|
313
|
-
const style = window.getComputedStyle(dropzoneElement);
|
314
|
-
|
315
|
-
// Parse border widths from computed style
|
316
|
-
const borderLeftWidth = parseInt(style.borderLeftWidth, 10) || 0;
|
317
|
-
const borderTopWidth = parseInt(style.borderTopWidth, 10) || 0;
|
318
|
-
|
319
|
-
// Get dropzone rectangle
|
320
|
-
const dropzoneRect = dropzoneElement.getBoundingClientRect();
|
321
|
-
|
322
|
-
// Calculate position with both dragData.offsetX/Y adjustment and border adjustment
|
323
|
-
// This combines your current approach with the border adjustment
|
324
|
-
const offsetX = event.clientX - dropzoneRect.left - borderLeftWidth - (dragData.offsetX ?? 0);
|
325
287
|
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
zone,
|
335
|
-
item: dragData.item,
|
336
|
-
source: dragData.source,
|
337
|
-
metadata: dragData.metadata
|
338
|
-
});
|
288
|
+
// If we couldn't get it from the element attribute, try dataTransfer
|
289
|
+
if (!dragData) {
|
290
|
+
// Parse the JSON data from the dataTransfer object (only works during drop)
|
291
|
+
const jsonData = event.dataTransfer.getData('application/json');
|
292
|
+
if (jsonData) {
|
293
|
+
dragData = JSON.parse(jsonData);
|
294
|
+
}
|
295
|
+
}
|
339
296
|
|
340
|
-
//
|
341
|
-
|
297
|
+
// Check if we can accept this drop
|
298
|
+
if (dragData && canAcceptDrop(dragData)) {
|
299
|
+
// Notify listener
|
300
|
+
onDropStart?.({ event, zone, data: dragData });
|
301
|
+
|
302
|
+
const style = window.getComputedStyle(dropzoneElement);
|
303
|
+
|
304
|
+
// Parse border widths from computed style
|
305
|
+
const borderLeftWidth = parseInt(style.borderLeftWidth, 10) || 0;
|
306
|
+
const borderTopWidth = parseInt(style.borderTopWidth, 10) || 0;
|
307
|
+
|
308
|
+
// Get dropzone rectangle
|
309
|
+
const dropzoneRect = dropzoneElement.getBoundingClientRect();
|
310
|
+
|
311
|
+
// Calculate position with both dragData.offsetX/Y adjustment and border adjustment
|
312
|
+
// This combines your current approach with the border adjustment
|
313
|
+
const offsetX =
|
314
|
+
event.clientX -
|
315
|
+
dropzoneRect.left -
|
316
|
+
borderLeftWidth -
|
317
|
+
(dragData.offsetX ?? 0);
|
318
|
+
|
319
|
+
const offsetY =
|
320
|
+
event.clientY -
|
321
|
+
dropzoneRect.top -
|
322
|
+
borderTopWidth -
|
323
|
+
(dragData.offsetY ?? 0);
|
324
|
+
|
325
|
+
// console.debug("dragData", dragData);
|
326
|
+
|
327
|
+
const dropResult = onDrop?.({
|
328
|
+
event,
|
329
|
+
offsetX,
|
330
|
+
offsetY,
|
331
|
+
zone,
|
332
|
+
item: dragData.item,
|
333
|
+
source: dragData.source,
|
334
|
+
metadata: dragData.metadata
|
335
|
+
});
|
336
|
+
|
337
|
+
// Handle async or sync results
|
338
|
+
Promise.resolve(dropResult)
|
339
|
+
.then(() => {
|
340
|
+
currentState = READY;
|
341
|
+
onDropEnd?.({ event, zone, data: dragData, success: true });
|
342
|
+
})
|
343
|
+
.catch((error) => {
|
344
|
+
currentState = READY;
|
345
|
+
onDropEnd?.({ event, zone, data: dragData, success: false, error });
|
346
|
+
});
|
347
|
+
} else {
|
348
|
+
// Not a valid drop, reset state
|
342
349
|
currentState = READY;
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
});
|
348
|
-
} else {
|
349
|
-
// Not a valid drop, reset state
|
350
|
+
}
|
351
|
+
} catch (error) {
|
352
|
+
// Handle parsing errors
|
353
|
+
console.error('Drop error:', error);
|
350
354
|
currentState = READY;
|
351
355
|
}
|
352
|
-
} catch (error) {
|
353
|
-
// Handle parsing errors
|
354
|
-
console.error('Drop error:', error);
|
355
|
-
currentState = READY;
|
356
356
|
}
|
357
|
-
}
|
358
357
|
</script>
|
359
358
|
|
360
359
|
<div
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hkdigital/lib-sveltekit",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.84",
|
4
4
|
"author": {
|
5
5
|
"name": "HKdigital",
|
6
6
|
"url": "https://hkdigital.nl"
|
@@ -53,7 +53,8 @@
|
|
53
53
|
"svelte": "./dist/index.js",
|
54
54
|
"default": "./dist/index.js"
|
55
55
|
},
|
56
|
-
"./*": "./dist/*"
|
56
|
+
"./*": "./dist/*",
|
57
|
+
"./typedef": "./dist/typedef/index.js"
|
57
58
|
},
|
58
59
|
"peerDependencies": {
|
59
60
|
"@steeze-ui/heroicons": "^2.4.2",
|