-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathutil.ts
207 lines (190 loc) · 5.19 KB
/
util.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
import { PropsWithChildren } from "react";
import Sortable, { Options } from "sortablejs";
import { MultiDragEvent } from "./react-sortable";
import { AllMethodNames, ItemInterface, ReactSortableProps } from "./types";
/**
* Removes the `node` from the DOM
* @param node
*/
export function removeNode(node: HTMLElement): void {
if (node.parentElement !== null) node.parentElement.removeChild(node);
}
/**
* Inserts the `newChild` node at the given index in a parent
* @param parent The parent HTML Element.
* @param newChild A HTML eement to add as a child of the parent.
* @param index index of the parent to place the new child in.
*/
export function insertNodeAt(
parent: HTMLElement,
newChild: HTMLElement,
index: number
): void {
const refChild = parent.children[index] || null;
parent.insertBefore(newChild, refChild);
}
// @todo - create a dom handler function for arrays or not at all
/** removes stuff from the dom in a nice order */
// @todo - do I need parenElement?
export function handleDOMChanges<T extends ItemInterface>(
customs: Normalized<T>[]
): void {
removeNodes(customs);
insertNodes(customs);
}
export function removeNodes<T extends ItemInterface>(
customs: Normalized<T>[]
): void {
customs.forEach((curr) => removeNode(curr.element));
}
export function insertNodes<T extends ItemInterface>(
customs: Normalized<T>[]
): void {
customs.forEach((curr) => {
insertNodeAt(curr.parentElement, curr.element, curr.oldIndex);
});
}
export function createCustoms<T extends ItemInterface>(
evt: MultiDragEvent,
list: T[]
): Normalized<T>[] {
const mode = getMode(evt);
const parentElement = { parentElement: evt.from };
let custom = [];
switch (mode) {
case "normal":
/* eslint-disable */
const item = {
element: evt.item,
newIndex: evt.newIndex!,
oldIndex: evt.oldIndex!,
parentElement: evt.from,
};
custom = [item];
break;
case "swap":
const drag: Input = {
element: evt.item,
oldIndex: evt.oldIndex!,
newIndex: evt.newIndex!,
...parentElement,
};
const swap: Input = {
element: evt.swapItem!,
oldIndex: evt.newIndex!,
newIndex: evt.oldIndex!,
...parentElement,
};
custom = [drag, swap];
break;
case "multidrag":
custom = evt.oldIndicies.map<Input>((curr, index) => ({
element: curr.multiDragElement,
oldIndex: curr.index,
newIndex: evt.newIndicies[index].index,
...parentElement,
}));
break;
}
/* eslint-enable */
return createNormalized(custom, list);
}
/** moves items form old index to new index without breaking anything ideally. */
export function handleStateChanges<T extends ItemInterface>(
normalized: Normalized<T>[],
list: T[]
): T[] {
const a = handleStateRemove(normalized, list);
const b = handleStateAdd(normalized, a);
return b;
}
export function handleStateRemove<T extends ItemInterface>(
normalized: Normalized<T>[],
list: T[]
): T[] {
const newList = [...list];
normalized
.concat()
.reverse()
.forEach((curr) => newList.splice(curr.oldIndex, 1));
return newList;
}
export function handleStateAdd<T extends ItemInterface>(
normalized: Normalized<T>[],
list: T[],
evt?: Sortable.SortableEvent,
clone?: ((currentItem: T, evt: Sortable.SortableEvent) => T) | undefined
): T[] {
const newList = [...list];
normalized.forEach((curr) => {
const newItem = clone && evt && clone(curr.item, evt);
newList.splice(curr.newIndex, 0, newItem || curr.item);
});
return newList;
}
export function getMode(evt: MultiDragEvent): "multidrag" | "swap" | "normal" {
if (evt.oldIndicies && evt.oldIndicies.length > 0) return "multidrag";
if (evt.swapItem) return "swap";
return "normal";
}
export function createNormalized<T extends ItemInterface>(
inputs: Input[],
list: T[]
): Normalized<T>[] {
return inputs
.map<Normalized<T>>((curr) => ({ ...curr, item: list[curr.oldIndex] }))
.sort((a, b) => a.oldIndex - b.oldIndex);
}
export interface Input {
parentElement: HTMLElement;
element: HTMLElement;
oldIndex: number;
newIndex: number;
}
export interface Normalized<T> extends Input {
item: T;
}
/**
* Removes the following group of properties from `props`,
* leaving only `Sortable.Options` without any `on` methods.
* @param props `ReactSortable.Props`
*/
export function destructurePropsForOptions<T>(
props: PropsWithChildren<ReactSortableProps<T>>
): Exclude<Options, AllMethodNames> {
/* eslint-disable */
const {
// react sortable props
list,
setList,
children,
tag,
style,
className,
clone,
// sortable options that have methods we want to overwrite
onAdd,
onChange,
onChoose,
onClone,
onEnd,
onFilter,
onRemove,
onSort,
onStart,
onUnchoose,
onUpdate,
onMove,
onSpill,
onSelect,
onDeselect,
...options
} = props;
/* eslint-enable */
return options;
}
/**
* Construct a type with the properties of T except for those in type K.
* Including this allows for backwards compatibility with earlier versions of TS.
*/
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;