forked from atlassian/react-beautiful-dnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
264 lines (216 loc) · 7.25 KB
/
utils.js
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
// @flow
import invariant from 'tiny-invariant';
import type { Column, ColumnMap, Entities } from './types';
import type { Id } from '../types';
import type { DraggableLocation } from '../../../src/types';
import reorder from '../reorder';
type Args = {|
entities: Entities,
selectedTaskIds: Id[],
source: DraggableLocation,
destination: DraggableLocation,
|};
export type Result = {|
entities: Entities,
// a drop operations can change the order of the selected task array
selectedTaskIds: Id[],
|};
const withNewTaskIds = (column: Column, taskIds: Id[]): Column => ({
id: column.id,
title: column.title,
taskIds,
});
const reorderSingleDrag = ({
entities,
selectedTaskIds,
source,
destination,
}: Args): Result => {
// moving in the same list
if (source.droppableId === destination.droppableId) {
const column: Column = entities.columns[source.droppableId];
const reordered: Id[] = reorder(
column.taskIds,
source.index,
destination.index,
);
const updated: Entities = {
...entities,
columns: {
...entities.columns,
[column.id]: withNewTaskIds(column, reordered),
},
};
return {
entities: updated,
selectedTaskIds,
};
}
// moving to a new list
const home: Column = entities.columns[source.droppableId];
const foreign: Column = entities.columns[destination.droppableId];
// the id of the task to be moved
const taskId: Id = home.taskIds[source.index];
// remove from home column
const newHomeTaskIds: Id[] = [...home.taskIds];
newHomeTaskIds.splice(source.index, 1);
// add to foreign column
const newForeignTaskIds: Id[] = [...foreign.taskIds];
newForeignTaskIds.splice(destination.index, 0, taskId);
const updated: Entities = {
...entities,
columns: {
...entities.columns,
[home.id]: withNewTaskIds(home, newHomeTaskIds),
[foreign.id]: withNewTaskIds(foreign, newForeignTaskIds),
},
};
return {
entities: updated,
selectedTaskIds,
};
};
type TaskId = Id;
export const getHomeColumn = (entities: Entities, taskId: TaskId): Column => {
const columnId: ?Id = entities.columnOrder.find((id: Id) => {
const column: Column = entities.columns[id];
return column.taskIds.includes(taskId);
});
invariant(columnId, 'Count not find column for task');
return entities.columns[columnId];
};
const reorderMultiDrag = ({
entities,
selectedTaskIds,
source,
destination,
}: Args): Result => {
const start: Column = entities.columns[source.droppableId];
const dragged: TaskId = start.taskIds[source.index];
const insertAtIndex: number = (() => {
const destinationIndexOffset: number = selectedTaskIds.reduce(
(previous: number, current: TaskId): number => {
if (current === dragged) {
return previous;
}
const final: Column = entities.columns[destination.droppableId];
const column: Column = getHomeColumn(entities, current);
if (column !== final) {
return previous;
}
const index: number = column.taskIds.indexOf(current);
if (index >= destination.index) {
return previous;
}
// the selected item is before the destination index
// we need to account for this when inserting into the new location
return previous + 1;
},
0,
);
const result: number = destination.index - destinationIndexOffset;
return result;
})();
// doing the ordering now as we are required to look up columns
// and know original ordering
const orderedSelectedTaskIds: TaskId[] = [...selectedTaskIds];
orderedSelectedTaskIds.sort((a: TaskId, b: TaskId): number => {
// moving the dragged item to the top of the list
if (a === dragged) {
return -1;
}
if (b === dragged) {
return 1;
}
// sorting by their natural indexes
const columnForA: Column = getHomeColumn(entities, a);
const indexOfA: number = columnForA.taskIds.indexOf(a);
const columnForB: Column = getHomeColumn(entities, b);
const indexOfB: number = columnForB.taskIds.indexOf(b);
if (indexOfA !== indexOfB) {
return indexOfA - indexOfB;
}
// sorting by their order in the selectedTaskIds list
return -1;
});
// we need to remove all of the selected tasks from their columns
const withRemovedTasks: ColumnMap = entities.columnOrder.reduce(
(previous: ColumnMap, columnId: Id): ColumnMap => {
const column: Column = entities.columns[columnId];
// remove the id's of the items that are selected
const remainingTaskIds: TaskId[] = column.taskIds.filter(
(id: TaskId): boolean => !selectedTaskIds.includes(id),
);
previous[column.id] = withNewTaskIds(column, remainingTaskIds);
return previous;
},
entities.columns,
);
const final: Column = withRemovedTasks[destination.droppableId];
const withInserted: TaskId[] = (() => {
const base: TaskId[] = [...final.taskIds];
base.splice(insertAtIndex, 0, ...orderedSelectedTaskIds);
return base;
})();
// insert all selected tasks into final column
const withAddedTasks: ColumnMap = {
...withRemovedTasks,
[final.id]: withNewTaskIds(final, withInserted),
};
const updated: Entities = {
...entities,
columns: withAddedTasks,
};
return {
entities: updated,
selectedTaskIds: orderedSelectedTaskIds,
};
};
export const mutliDragAwareReorder = (args: Args): Result => {
if (args.selectedTaskIds.length > 1) {
return reorderMultiDrag(args);
}
return reorderSingleDrag(args);
};
export const multiSelectTo = (
entities: Entities,
selectedTaskIds: Id[],
newTaskId: TaskId,
): ?(Id[]) => {
// Nothing already selected
if (!selectedTaskIds.length) {
return [newTaskId];
}
const columnOfNew: Column = getHomeColumn(entities, newTaskId);
const indexOfNew: number = columnOfNew.taskIds.indexOf(newTaskId);
const lastSelected: Id = selectedTaskIds[selectedTaskIds.length - 1];
const columnOfLast: Column = getHomeColumn(entities, lastSelected);
const indexOfLast: number = columnOfLast.taskIds.indexOf(lastSelected);
// multi selecting to another column
// select everything up to the index of the current item
if (columnOfNew !== columnOfLast) {
return columnOfNew.taskIds.slice(0, indexOfNew + 1);
}
// multi selecting in the same column
// need to select everything between the last index and the current index inclusive
// nothing to do here
if (indexOfNew === indexOfLast) {
return null;
}
const isSelectingForwards: boolean = indexOfNew > indexOfLast;
const start: number = isSelectingForwards ? indexOfLast : indexOfNew;
const end: number = isSelectingForwards ? indexOfNew : indexOfLast;
const inBetween: Id[] = columnOfNew.taskIds.slice(start, end + 1);
// everything inbetween needs to have it's selection toggled.
// with the exception of the start and end values which will always be selected
const toAdd: Id[] = inBetween.filter((taskId: Id): boolean => {
// if already selected: then no need to select it again
if (selectedTaskIds.includes(taskId)) {
return false;
}
return true;
});
const sorted: Id[] = isSelectingForwards ? toAdd : [...toAdd].reverse();
const combined: Id[] = [...selectedTaskIds, ...sorted];
return combined;
};