-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
single-axis-sort-strategy.ts
448 lines (387 loc) · 17.3 KB
/
single-axis-sort-strategy.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {Direction} from '@angular/cdk/bidi';
import {DragDropRegistry} from '../drag-drop-registry';
import {moveItemInArray} from '../drag-utils';
import {combineTransforms} from '../dom/styling';
import {adjustDomRect, getMutableClientRect, isInsideClientRect} from '../dom/dom-rect';
import {DropListSortStrategy, SortPredicate} from './drop-list-sort-strategy';
import type {DragRef} from '../drag-ref';
/**
* Entry in the position cache for draggable items.
* @docs-private
*/
interface CachedItemPosition<T> {
/** Instance of the drag item. */
drag: T;
/** Dimensions of the item. */
clientRect: DOMRect;
/** Amount by which the item has been moved since dragging started. */
offset: number;
/** Inline transform that the drag item had when dragging started. */
initialTransform: string;
}
/**
* Strategy that only supports sorting along a single axis.
* Items are reordered using CSS transforms which allows for sorting to be animated.
* @docs-private
*/
export class SingleAxisSortStrategy implements DropListSortStrategy {
/** Root element container of the drop list. */
private _element: HTMLElement;
/** Function used to determine if an item can be sorted into a specific index. */
private _sortPredicate: SortPredicate<DragRef>;
/** Cache of the dimensions of all the items inside the container. */
private _itemPositions: CachedItemPosition<DragRef>[] = [];
/**
* Draggable items that are currently active inside the container. Includes the items
* that were there at the start of the sequence, as well as any items that have been dragged
* in, but haven't been dropped yet.
*/
private _activeDraggables: DragRef[];
/** Direction in which the list is oriented. */
orientation: 'vertical' | 'horizontal' = 'vertical';
/** Layout direction of the drop list. */
direction: Direction;
constructor(private _dragDropRegistry: DragDropRegistry) {}
/**
* Keeps track of the item that was last swapped with the dragged item, as well as what direction
* the pointer was moving in when the swap occurred and whether the user's pointer continued to
* overlap with the swapped item after the swapping occurred.
*/
private _previousSwap = {
drag: null as DragRef | null,
delta: 0,
overlaps: false,
};
/**
* To be called when the drag sequence starts.
* @param items Items that are currently in the list.
*/
start(items: readonly DragRef[]) {
this.withItems(items);
}
/**
* To be called when an item is being sorted.
* @param item Item to be sorted.
* @param pointerX Position of the item along the X axis.
* @param pointerY Position of the item along the Y axis.
* @param pointerDelta Direction in which the pointer is moving along each axis.
*/
sort(item: DragRef, pointerX: number, pointerY: number, pointerDelta: {x: number; y: number}) {
const siblings = this._itemPositions;
const newIndex = this._getItemIndexFromPointerPosition(item, pointerX, pointerY, pointerDelta);
if (newIndex === -1 && siblings.length > 0) {
return null;
}
const isHorizontal = this.orientation === 'horizontal';
const currentIndex = siblings.findIndex(currentItem => currentItem.drag === item);
const siblingAtNewPosition = siblings[newIndex];
const currentPosition = siblings[currentIndex].clientRect;
const newPosition = siblingAtNewPosition.clientRect;
const delta = currentIndex > newIndex ? 1 : -1;
// How many pixels the item's placeholder should be offset.
const itemOffset = this._getItemOffsetPx(currentPosition, newPosition, delta);
// How many pixels all the other items should be offset.
const siblingOffset = this._getSiblingOffsetPx(currentIndex, siblings, delta);
// Save the previous order of the items before moving the item to its new index.
// We use this to check whether an item has been moved as a result of the sorting.
const oldOrder = siblings.slice();
// Shuffle the array in place.
moveItemInArray(siblings, currentIndex, newIndex);
siblings.forEach((sibling, index) => {
// Don't do anything if the position hasn't changed.
if (oldOrder[index] === sibling) {
return;
}
const isDraggedItem = sibling.drag === item;
const offset = isDraggedItem ? itemOffset : siblingOffset;
const elementToOffset = isDraggedItem
? item.getPlaceholderElement()
: sibling.drag.getRootElement();
// Update the offset to reflect the new position.
sibling.offset += offset;
const transformAmount = Math.round(sibling.offset * (1 / sibling.drag.scale));
// Since we're moving the items with a `transform`, we need to adjust their cached
// client rects to reflect their new position, as well as swap their positions in the cache.
// Note that we shouldn't use `getBoundingClientRect` here to update the cache, because the
// elements may be mid-animation which will give us a wrong result.
if (isHorizontal) {
// Round the transforms since some browsers will
// blur the elements, for sub-pixel transforms.
elementToOffset.style.transform = combineTransforms(
`translate3d(${transformAmount}px, 0, 0)`,
sibling.initialTransform,
);
adjustDomRect(sibling.clientRect, 0, offset);
} else {
elementToOffset.style.transform = combineTransforms(
`translate3d(0, ${transformAmount}px, 0)`,
sibling.initialTransform,
);
adjustDomRect(sibling.clientRect, offset, 0);
}
});
// Note that it's important that we do this after the client rects have been adjusted.
this._previousSwap.overlaps = isInsideClientRect(newPosition, pointerX, pointerY);
this._previousSwap.drag = siblingAtNewPosition.drag;
this._previousSwap.delta = isHorizontal ? pointerDelta.x : pointerDelta.y;
return {previousIndex: currentIndex, currentIndex: newIndex};
}
/**
* Called when an item is being moved into the container.
* @param item Item that was moved into the container.
* @param pointerX Position of the item along the X axis.
* @param pointerY Position of the item along the Y axis.
* @param index Index at which the item entered. If omitted, the container will try to figure it
* out automatically.
*/
enter(item: DragRef, pointerX: number, pointerY: number, index?: number): void {
const newIndex =
index == null || index < 0
? // We use the coordinates of where the item entered the drop
// zone to figure out at which index it should be inserted.
this._getItemIndexFromPointerPosition(item, pointerX, pointerY)
: index;
const activeDraggables = this._activeDraggables;
const currentIndex = activeDraggables.indexOf(item);
const placeholder = item.getPlaceholderElement();
let newPositionReference: DragRef | undefined = activeDraggables[newIndex];
// If the item at the new position is the same as the item that is being dragged,
// it means that we're trying to restore the item to its initial position. In this
// case we should use the next item from the list as the reference.
if (newPositionReference === item) {
newPositionReference = activeDraggables[newIndex + 1];
}
// If we didn't find a new position reference, it means that either the item didn't start off
// in this container, or that the item requested to be inserted at the end of the list.
if (
!newPositionReference &&
(newIndex == null || newIndex === -1 || newIndex < activeDraggables.length - 1) &&
this._shouldEnterAsFirstChild(pointerX, pointerY)
) {
newPositionReference = activeDraggables[0];
}
// Since the item may be in the `activeDraggables` already (e.g. if the user dragged it
// into another container and back again), we have to ensure that it isn't duplicated.
if (currentIndex > -1) {
activeDraggables.splice(currentIndex, 1);
}
// Don't use items that are being dragged as a reference, because
// their element has been moved down to the bottom of the body.
if (newPositionReference && !this._dragDropRegistry.isDragging(newPositionReference)) {
const element = newPositionReference.getRootElement();
element.parentElement!.insertBefore(placeholder, element);
activeDraggables.splice(newIndex, 0, item);
} else {
this._element.appendChild(placeholder);
activeDraggables.push(item);
}
// The transform needs to be cleared so it doesn't throw off the measurements.
placeholder.style.transform = '';
// Note that usually `start` is called together with `enter` when an item goes into a new
// container. This will cache item positions, but we need to refresh them since the amount
// of items has changed.
this._cacheItemPositions();
}
/** Sets the items that are currently part of the list. */
withItems(items: readonly DragRef[]): void {
this._activeDraggables = items.slice();
this._cacheItemPositions();
}
/** Assigns a sort predicate to the strategy. */
withSortPredicate(predicate: SortPredicate<DragRef>): void {
this._sortPredicate = predicate;
}
/** Resets the strategy to its initial state before dragging was started. */
reset() {
// TODO(crisbeto): may have to wait for the animations to finish.
this._activeDraggables?.forEach(item => {
const rootElement = item.getRootElement();
if (rootElement) {
const initialTransform = this._itemPositions.find(p => p.drag === item)?.initialTransform;
rootElement.style.transform = initialTransform || '';
}
});
this._itemPositions = [];
this._activeDraggables = [];
this._previousSwap.drag = null;
this._previousSwap.delta = 0;
this._previousSwap.overlaps = false;
}
/**
* Gets a snapshot of items currently in the list.
* Can include items that we dragged in from another list.
*/
getActiveItemsSnapshot(): readonly DragRef[] {
return this._activeDraggables;
}
/** Gets the index of a specific item. */
getItemIndex(item: DragRef): number {
// Items are sorted always by top/left in the cache, however they flow differently in RTL.
// The rest of the logic still stands no matter what orientation we're in, however
// we need to invert the array when determining the index.
const items =
this.orientation === 'horizontal' && this.direction === 'rtl'
? this._itemPositions.slice().reverse()
: this._itemPositions;
return items.findIndex(currentItem => currentItem.drag === item);
}
/** Used to notify the strategy that the scroll position has changed. */
updateOnScroll(topDifference: number, leftDifference: number) {
// Since we know the amount that the user has scrolled we can shift all of the
// client rectangles ourselves. This is cheaper than re-measuring everything and
// we can avoid inconsistent behavior where we might be measuring the element before
// its position has changed.
this._itemPositions.forEach(({clientRect}) => {
adjustDomRect(clientRect, topDifference, leftDifference);
});
// We need two loops for this, because we want all of the cached
// positions to be up-to-date before we re-sort the item.
this._itemPositions.forEach(({drag}) => {
if (this._dragDropRegistry.isDragging(drag)) {
// We need to re-sort the item manually, because the pointer move
// events won't be dispatched while the user is scrolling.
drag._sortFromLastPointerPosition();
}
});
}
withElementContainer(container: HTMLElement): void {
this._element = container;
}
/** Refreshes the position cache of the items and sibling containers. */
private _cacheItemPositions() {
const isHorizontal = this.orientation === 'horizontal';
this._itemPositions = this._activeDraggables
.map(drag => {
const elementToMeasure = drag.getVisibleElement();
return {
drag,
offset: 0,
initialTransform: elementToMeasure.style.transform || '',
clientRect: getMutableClientRect(elementToMeasure),
};
})
.sort((a, b) => {
return isHorizontal
? a.clientRect.left - b.clientRect.left
: a.clientRect.top - b.clientRect.top;
});
}
/**
* Gets the offset in pixels by which the item that is being dragged should be moved.
* @param currentPosition Current position of the item.
* @param newPosition Position of the item where the current item should be moved.
* @param delta Direction in which the user is moving.
*/
private _getItemOffsetPx(currentPosition: DOMRect, newPosition: DOMRect, delta: 1 | -1) {
const isHorizontal = this.orientation === 'horizontal';
let itemOffset = isHorizontal
? newPosition.left - currentPosition.left
: newPosition.top - currentPosition.top;
// Account for differences in the item width/height.
if (delta === -1) {
itemOffset += isHorizontal
? newPosition.width - currentPosition.width
: newPosition.height - currentPosition.height;
}
return itemOffset;
}
/**
* Gets the offset in pixels by which the items that aren't being dragged should be moved.
* @param currentIndex Index of the item currently being dragged.
* @param siblings All of the items in the list.
* @param delta Direction in which the user is moving.
*/
private _getSiblingOffsetPx(
currentIndex: number,
siblings: CachedItemPosition<DragRef>[],
delta: 1 | -1,
) {
const isHorizontal = this.orientation === 'horizontal';
const currentPosition = siblings[currentIndex].clientRect;
const immediateSibling = siblings[currentIndex + delta * -1];
let siblingOffset = currentPosition[isHorizontal ? 'width' : 'height'] * delta;
if (immediateSibling) {
const start = isHorizontal ? 'left' : 'top';
const end = isHorizontal ? 'right' : 'bottom';
// Get the spacing between the start of the current item and the end of the one immediately
// after it in the direction in which the user is dragging, or vice versa. We add it to the
// offset in order to push the element to where it will be when it's inline and is influenced
// by the `margin` of its siblings.
if (delta === -1) {
siblingOffset -= immediateSibling.clientRect[start] - currentPosition[end];
} else {
siblingOffset += currentPosition[start] - immediateSibling.clientRect[end];
}
}
return siblingOffset;
}
/**
* Checks if pointer is entering in the first position
* @param pointerX Position of the user's pointer along the X axis.
* @param pointerY Position of the user's pointer along the Y axis.
*/
private _shouldEnterAsFirstChild(pointerX: number, pointerY: number) {
if (!this._activeDraggables.length) {
return false;
}
const itemPositions = this._itemPositions;
const isHorizontal = this.orientation === 'horizontal';
// `itemPositions` are sorted by position while `activeDraggables` are sorted by child index
// check if container is using some sort of "reverse" ordering (eg: flex-direction: row-reverse)
const reversed = itemPositions[0].drag !== this._activeDraggables[0];
if (reversed) {
const lastItemRect = itemPositions[itemPositions.length - 1].clientRect;
return isHorizontal ? pointerX >= lastItemRect.right : pointerY >= lastItemRect.bottom;
} else {
const firstItemRect = itemPositions[0].clientRect;
return isHorizontal ? pointerX <= firstItemRect.left : pointerY <= firstItemRect.top;
}
}
/**
* Gets the index of an item in the drop container, based on the position of the user's pointer.
* @param item Item that is being sorted.
* @param pointerX Position of the user's pointer along the X axis.
* @param pointerY Position of the user's pointer along the Y axis.
* @param delta Direction in which the user is moving their pointer.
*/
private _getItemIndexFromPointerPosition(
item: DragRef,
pointerX: number,
pointerY: number,
delta?: {x: number; y: number},
): number {
const isHorizontal = this.orientation === 'horizontal';
const index = this._itemPositions.findIndex(({drag, clientRect}) => {
// Skip the item itself.
if (drag === item) {
return false;
}
if (delta) {
const direction = isHorizontal ? delta.x : delta.y;
// If the user is still hovering over the same item as last time, their cursor hasn't left
// the item after we made the swap, and they didn't change the direction in which they're
// dragging, we don't consider it a direction swap.
if (
drag === this._previousSwap.drag &&
this._previousSwap.overlaps &&
direction === this._previousSwap.delta
) {
return false;
}
}
return isHorizontal
? // Round these down since most browsers report client rects with
// sub-pixel precision, whereas the pointer coordinates are rounded to pixels.
pointerX >= Math.floor(clientRect.left) && pointerX < Math.floor(clientRect.right)
: pointerY >= Math.floor(clientRect.top) && pointerY < Math.floor(clientRect.bottom);
});
return index === -1 || !this._sortPredicate(index, item) ? -1 : index;
}
}