-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortableContext.ts
338 lines (280 loc) · 10.4 KB
/
SortableContext.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { Sensor } from './sensors';
import { Sortable, SortableDragEvent } from './Sortable';
import { centerOfRectangle } from './utils/geometry';
import { Listeners } from './utils/listeners';
import { getClientRectWithoutTransform } from './utils/transform';
export class SortableContext {
public overItem?: HTMLElement;
public overlay: HTMLElement;
public pointer: DOMPoint = new DOMPoint(0, 0);
public pointerOffset: DOMPoint = new DOMPoint(0, 0);
public position: DOMRect;
public itemsByContainer: Map<HTMLElement, Set<HTMLElement>>;
public rects: Map<HTMLElement, DOMRect> = new Map();
public originalContainer?: HTMLElement;
public originalIndex: number = -1;
private documentListeners = new Listeners(document);
private windowListeners = new Listeners(window);
constructor(
private instance: Sortable,
private sensor: Sensor,
public activeItem: HTMLElement,
public activator: HTMLElement,
private onFinished: () => void,
pointer?: DOMPoint
) {
this.itemsByContainer = new Map();
for (let container of instance.containers) {
const items = (
Array.from(container.children) as HTMLElement[]
).filter(this.instance.options.filter);
const activeIndex = items.indexOf(activeItem);
if (activeIndex !== -1) {
this.originalContainer = container;
this.originalIndex = activeIndex;
}
this.itemsByContainer.set(container, new Set(items));
}
this.updateRects();
this.position = DOMRect.fromRect(this.rects.get(this.activeItem)!);
this.pointer = pointer || centerOfRectangle(this.position);
this.pointerOffset = new DOMPoint(
this.position.x - this.pointer.x,
this.position.y - this.pointer.y
);
this.overlay = this.createOverlay(activeItem);
this.updateOverlay();
document.body.appendChild(this.overlay);
this.windowListeners.add('scroll', this.onScroll, {
capture: true,
passive: true,
});
this.documentListeners.add('keydown', this.onKeyDown);
this.instance.announce(
this.instance.options.announcements.onDragStart(this)
);
}
public get options() {
return this.instance.options;
}
public get container() {
for (let [container, items] of this.itemsByContainer) {
if (items.has(this.activeItem)) return container;
}
return Array.from(this.itemsByContainer.keys())[0];
}
public get activeIndex() {
for (let [, items] of this.itemsByContainer) {
if (!items.has(this.activeItem)) continue;
return Array.from(items).indexOf(this.activeItem);
}
return -1;
}
public get overIndex() {
if (this.overItem) {
for (let [, items] of this.itemsByContainer) {
if (!items.has(this.overItem)) continue;
return Array.from(items).indexOf(this.overItem);
}
}
return -1;
}
public onMove = (pointer: DOMPoint, immediate?: boolean) => {
this.pointer = pointer;
this.position.x = pointer.x + this.pointerOffset.x;
this.position.y = pointer.y + this.pointerOffset.y;
const event = new CustomEvent('dragmove', {
detail: this,
}) as SortableDragEvent;
this.instance.dispatchEvent(event);
this.updateOverlay(immediate);
let rects = new Map();
// If the pointer is over a container, only take that container's
// items into consideration (or if it's empty, the container itself.
this.itemsByContainer.forEach((items, container) => {
const rect = this.rects.get(container)!;
if (
pointer.x > rect.left &&
pointer.x < rect.right &&
pointer.y > rect.top &&
pointer.y < rect.bottom
) {
if (items.size) {
items.forEach((item) =>
rects.set(item, this.rects.get(item))
);
} else {
rects.set(container, this.rects.get(container));
}
}
});
if (!rects.size) {
rects = this.rects;
}
const overItem = this.options.collisionDetection(this.position, rects);
if (overItem !== this.overItem) {
const event = new CustomEvent('dragover', {
cancelable: true,
detail: { ...this, overItem },
}) as SortableDragEvent;
this.instance.dispatchEvent(event);
if (event.defaultPrevented) return;
this.overItem = overItem;
}
// Move to another container
this.itemsByContainer.forEach((items, container) => {
if (items.has(this.activeItem) && !container.contains(overItem)) {
items.delete(this.activeItem);
} else if (
container.contains(overItem) &&
!items.has(this.activeItem)
) {
items.add(this.activeItem);
container.appendChild(this.activeItem);
this.updateRects();
}
});
this.instance.announce(
this.instance.options.announcements.onDragOver(this)
);
this.itemsByContainer.forEach((items) => {
const itemsArray = Array.from(items);
const activeIndex = itemsArray.indexOf(this.activeItem);
const overIndex = itemsArray.indexOf(overItem);
const rects = itemsArray.map((item) => this.rects.get(item)!);
itemsArray.forEach((item, index) => {
item.style.transform = 'none';
if (overIndex === -1) return;
const result = this.options.strategy({
rects,
activeIndex,
overIndex,
index,
});
if (!result) return;
const x = Math.round(result.x);
const y = Math.round(result.y);
item.style.transform = `translate(${x}px, ${y}px)`;
});
});
};
public onDrop = () => {
const { activeItem, overItem } = this;
if (activeItem && overItem) {
const event = new CustomEvent('drop', {
cancelable: true,
detail: this,
}) as SortableDragEvent;
this.instance.dispatchEvent(event);
if (!event.defaultPrevented) {
if (this.instance.containers.has(overItem)) {
overItem.appendChild(activeItem);
} else if (
activeItem.compareDocumentPosition(overItem) ===
Node.DOCUMENT_POSITION_PRECEDING
) {
overItem.before(activeItem);
} else {
overItem.after(activeItem);
}
this.instance.announce(
this.instance.options.announcements.onDrop(this)
);
}
}
this.destroy();
};
public onCancel = () => {
if (
this.activeItem &&
this.originalContainer &&
this.originalIndex !== -1
) {
this.originalContainer.insertBefore(
this.activeItem,
this.originalContainer.children[this.originalIndex]
);
}
const event = new CustomEvent('dragcancel', {
cancelable: false,
detail: this,
}) as SortableDragEvent;
this.instance.dispatchEvent(event);
this.instance.announce(
this.instance.options.announcements.onDragCancel(this)
);
this.activator.focus();
// Wait for the scroll position to update after re-focusing the activator.
setTimeout(() => this.destroy());
};
private destroy() {
this.sensor.deactivate();
this.itemsByContainer.forEach((items) => {
items.forEach((item) => {
item.style.transition = 'none';
item.style.transform = 'none';
void item.offsetWidth;
item.style.transition = '';
});
});
const { activeItem, overlay } = this;
if (activeItem && overlay) {
const rect = activeItem.getBoundingClientRect();
overlay.style.transform = `translate(${rect.x}px, ${rect.y}px)`;
Promise.all(
overlay.getAnimations().map((animation) => animation.finished)
).then(() => {
overlay.remove();
const event = new CustomEvent('dragend', {
cancelable: false,
detail: this,
}) as SortableDragEvent;
this.instance.dispatchEvent(event);
});
}
this.windowListeners.removeAll();
this.documentListeners.removeAll();
this.onFinished();
}
private updateRects() {
this.rects.clear();
this.itemsByContainer.forEach((items, container) => {
[container, ...items].forEach((el) => {
this.rects.set(el, getClientRectWithoutTransform(el));
});
});
}
private createOverlay(item: HTMLElement) {
const overlay = item.cloneNode(true) as HTMLElement;
Object.assign(overlay.style, {
position: 'fixed',
top: 0,
left: 0,
margin: 0,
});
return overlay;
}
private updateOverlay(immediate?: boolean) {
Object.assign(this.overlay.style, {
transform: `translate(${this.position.x}px, ${this.position.y}px)`,
width: this.position.width + 'px',
height: this.position.height + 'px',
});
if (immediate) {
const transition = this.overlay.style.transition;
this.overlay.style.transition = 'none';
void this.overlay.offsetWidth;
this.overlay.style.transition = transition;
}
}
private onScroll = () => {
this.updateRects();
this.onMove(this.pointer);
};
private onKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
e.preventDefault();
this.onCancel();
}
};
}