forked from jwlarocque/svelte-dragdroplist
/
DragDropList.svelte
285 lines (256 loc) · 10.2 KB
/
DragDropList.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<script>
import { onMount, afterUpdate } from "svelte";
import {flip} from "svelte/animate";
import DefaultItem from "./DefaultItem.svelte";
import { setIntervalWithMax } from './utils.js';
export let data = [];
export let ItemComponent = DefaultItem;
export let itemIdName = 'id';
$: data && generateWrappedItems(data);
$: console.log('wrappedItems', wrappedItems[0].data, wrappedItems[0].element, wrappedItems[0].element && wrappedItems[0].element.getBoundingClientRect().top);
// $: wrappedItems && debounce();
let wrappedItems = [];
function generateWrappedItems (itemsToWrap) {
wrappedItems = itemsToWrap.map((datum, i) => {
return {
index: i,
id: datum[itemIdName] ? datum[itemIdName] : JSON.stringify(datum),
data: datum,
};
});
}
// afterUpdate(() => {
// console.log('afterUpdate wrappedItems', wrappedItems[0].data, wrappedItems[0].element, wrappedItems[0].element && wrappedItems[0].element.getBoundingClientRect().top);
// // setTimeout(() => {
// // console.log('afterUpdate wrappedItems0', wrappedItems[0].data, wrappedItems[0].element, wrappedItems[0].element && wrappedItems[0].element.getBoundingClientRect().top);
// // }, 0);
// let count = 0;
// let interval = setInterval(() => {
// console.log('afterUpdate wrappedItems1', wrappedItems[0].element && wrappedItems[0].element.getBoundingClientRect().top);
// count++;
// if (count > 30) clearInterval(interval);
// }, 100);
// // setTimeout(() => {
// // console.log('afterUpdate wrappedItems2', wrappedItems[0].data, wrappedItems[0].element, wrappedItems[0].element && wrappedItems[0].element.getBoundingClientRect().top);
// // }, 2000);
// })
// function debounce () {
// debouncing = true;
// // All wrappedItems should have an element when the re-rendering is done
// if (wrappedItems.filter(v => v.element).length === data.length) {
// debouncing = false;
// }
// console.log('debounce', debouncing);
// }
let ghost;
let grabbed;
let lastTarget;
let itemBounds = [];
let mouseY = 0; // pointer y coordinate within client
let offsetY = 0; // y distance from top of grabbed element to pointer
let layerY = 0; // distance from top of list to top of client
let debouncing = {};
// Updates the ghost's position
$: ghost && grabbed && mouseY && offsetY && layerY && (ghost.style.top = (mouseY + offsetY - layerY) + "px");
function grab(clientY, element) {
// modify grabbed element
grabbed = {
id: element.dataset.id,
index: element.dataset.index,
element: element,
graby: clientY,
};
// modify ghost element (which is actually dragged) to match original item
ghost.innerHTML = element.innerHTML;
ghost.style.width = element.offsetWidth + 'px';
ghost.style.height = element.offsetHeight + 'px';
// record offset from cursor to top of element
// (used for positioning ghost)
offsetY = element.getBoundingClientRect().y - clientY;
drag(clientY);
}
// drag handler updates cursor position
function drag(clientY) {
if (!grabbed) return;
mouseY = clientY;
layerY = ghost.parentNode.getBoundingClientRect().y;
let called = false;
overWhichItem(mouseY, (onItem) => {
if (called) console.log('already called', called.id, onItem.id);
called = onItem;
if (onItem.index === grabbed.index) return;
moveDatum(grabbed.index, onItem.index);
});
}
// touchEnter handler emulates the mouseenter event for touch input
// (more or less)
function touchMove(ev) {
if (!grabbed) return;
ev.stopPropagation(); ev.preventDefault();
ev = ev.touches[0];
let target = document.elementFromPoint(ev.clientX, ev.clientY).closest(".drag-drop-list-item");
drag(ev.clientY);
// trigger dragEnter the first time the cursor moves over a list item
if (target && target !== lastTarget) {
lastTarget = target;
dragEnter(target.dataset.index, target.dataset.id);
}
}
function dragEnter(targetIndex, targetId) {
// swap items in data
if (grabbed && targetId !== grabbed.id) {
// moveDatum(grabbed.index, targetIndex);
}
}
// does the actual moving of items in data
function moveDatum(from, to) {
from = parseInt(from);
to = parseInt(to);
if (to < 0 || to >= data.length || from === to) return;
if (grabbed) grabbed.index = to;
debounceItem(from);
debounceItem(to);
let temp = data[from];
data = [...data.slice(0, from), ...data.slice(from + 1)];
data = [...data.slice(0, to), temp, ...data.slice(to)];
// notifyDataChange();
}
function debounceItem(index) {
let id = wrappedItems[index].id;
let element = wrappedItems[index].element;
let startPos = element.getBoundingClientRect()['top'];
debouncing[id] = true;
let lastPos;
let interval = setIntervalWithMax((reachedMaxTime) => {
let currentPos = element.getBoundingClientRect()['top'];
// if the element has settled || if it's been too long
if (lastPos === currentPos || reachedMaxTime) {
if (reachedMaxTime) console.log('give up', id, startPos, lastPos, currentPos);
clearInterval(interval);
debouncing[id] = false;
}
// we started moving, record new position
if (currentPos !== startPos) lastPos = currentPos;
}, 200, 3000);
}
function notifyDataChange () {
// Notify data binding that an item has been added/moved/deleted
data = wrappedItems.map(item => item.data);
}
function release(ev) {
if (grabbed) notifyDataChange();
grabbed = null;
}
function removeDatum(index) {
wrappedItems = [...wrappedItems.slice(0, index), ...wrappedItems.slice(index + 1)];
}
let overWhichItemRetryTimeout; // TODO clear on release
function overWhichItem (cursorLocation, callback, totalRetryTime = 0) {
// If we have been triggered non-recursively that means the mouse has moved
// so kill the retry timeout
if (!totalRetryTime) clearTimeout(overWhichItemRetryTimeout);
if (totalRetryTime > 3000) {
console.log('exceeded retry', debouncing);
return;
}
for (const item of wrappedItems) {
const bounds = item.element && item.element.getBoundingClientRect();
if (!bounds) continue;
const smallerBound = bounds.top;
const largerBound = bounds.bottom;
if (cursorLocation > smallerBound && cursorLocation < largerBound) {
// We are on this item
if (debouncing[item.id]) {
// but if this item is debouncing, we should wait and try again
// console.log(`id ${item.id} ${debouncing[item.id]}`);
const retryInterval = 10;
overWhichItemRetryTimeout = setTimeout(() => {
overWhichItem(cursorLocation, callback, totalRetryTime + retryInterval)
}, retryInterval);
break;
}
if (totalRetryTime) console.log('IS RETRY AND FOUND', item.id);
callback(item);
}
}
}
onMount(() => {
// setItemBounds();
})
</script>
<!-- All the documentation has to go up here, sorry.
(otherwise it conflicts with the HTML or svelte/animate)
The .list has handlers for pointer movement and pointer up/release/end.
Each .item has a handler for pointer down/click/start, which assigns that
element as the item currently being "grabbed". They also have a handler
for pointer enter (the touchmove handler has extra logic to behave like the
no longer extant 'touchenter'), which swaps the entered element with the
grabbed element when triggered.
You'll also find reactive styling below, which keeps it from being directly
part of the imperative javascript handlers. -->
<svelte:window
on:mouseup={release}
on:touchend={release}
on:mousemove={(ev) => {drag(ev.clientY);}}
/>
<div
class="list {$$props.class || ''}"
style="{$$props.style || ''}"
on:touchmove={(ev) => {touchMove(ev);}}
>
{#each wrappedItems as wrappedItem, i (wrappedItem.id)}
<div
bind:this={wrappedItem.element}
class:grabbed={grabbed && (wrappedItem.id === grabbed.id)}
class="drag-drop-list-item"
data-index={i}
data-id={wrappedItem.id}
data-grabY="0"
on:mousedown={function(ev) {console.log('lin mousedown');grab(ev.clientY, this);}}
on:touchstart={function(ev) {console.log('lin touchstart'); grab(ev.touches[0].clientY, this);}}
on:mouseenter={function(ev) {console.log('lin mouseenter');ev.stopPropagation(); dragEnter(i, wrappedItem.id);}}
animate:flip|local={{duration: 200}}>
<svelte:component
this={ItemComponent}
data={wrappedItem.data}
index={i}
allItems={data}
on:moveup={function(ev) {moveDatum(i, i - 1)}}
on:movedown={function(ev) {moveDatum(i, i + 1)}}
on:remove={function(ev) {removeDatum(i);}}
/>
</div>
{/each}
<div
bind:this={ghost}
class="ghost"
class:haunting={grabbed}
>
</div>
</div>
<style>
.list {
position: relative;
display: flex;
flex-direction: column;
overflow: hidden;
}
.drag-drop-list-item {
z-index: 10;
}
.grabbed {
opacity: 0;
}
.ghost {
display: none;
pointer-events: none;
z-index: 20;
position: absolute;
}
.ghost * {
pointer-events: none;
}
.ghost.haunting {
display: unset;
}
</style>