-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.svelte
222 lines (213 loc) · 9.51 KB
/
index.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
<script>
import Sortable from "sortablejs";
import { createEventDispatcher } from "svelte";
import store from "./store.js";
// var sortable = new Sortable(el, {
// group: "name", // or { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] }
// sort: true, // sorting inside list
// delay: 0, // time in milliseconds to define when the sorting should start
// delayOnTouchOnly: false, // only delay if user is using touch
// touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
// disabled: false, // Disables the sortable if set to true.
// store: null, // @see Store
// animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
// easing: "cubic-bezier(1, 0, 0, 1)", // Easing for animation. Defaults to null. See https://easings.net/ for examples.
// handle: ".my-handle", // Drag handle selector within list items
// filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
// preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
// draggable: ".item", // Specifies which items inside the element should be draggable
//
// dataIdAttr: 'data-id',
//
// ghostClass: "sortable-ghost", // Class name for the drop placeholder
// chosenClass: "sortable-chosen", // Class name for the chosen item
// dragClass: "sortable-drag", // Class name for the dragging item
//
// swapThreshold: 1, // Threshold of the swap zone
// invertSwap: false, // Will always use inverted swap zone if set to true
// invertedSwapThreshold: 1, // Threshold of the inverted swap zone (will be set to swapThreshold value by default)
// direction: 'horizontal', // Direction of Sortable (will be detected automatically if not given)
//
// forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
//
// fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback
// fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body
// fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.
//
// dragoverBubble: false,
// removeCloneOnHide: true, // Remove the clone element when it is not showing, rather than just hiding it
// emptyInsertThreshold: 5, // px, distance mouse must be from empty sortable to insert drag element into it
//
//
// setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {
// dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent
// },
//
// // Element is chosen
// onChoose: function (/**Event*/evt) {
// evt.oldIndex; // element index within parent
// },
//
// // Element is unchosen
// onUnchoose: function(/**Event*/evt) {
// // same properties as onEnd
// },
//
// // Element dragging started
// onStart: function (/**Event*/evt) {
// evt.oldIndex; // element index within parent
// },
//
// // Element dragging ended
// onEnd: function (/**Event*/evt) {
// var itemEl = evt.item; // dragged HTMLElement
// evt.to; // target list
// evt.from; // previous list
// evt.oldIndex; // element's old index within old parent
// evt.newIndex; // element's new index within new parent
// evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
// evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
// evt.clone // the clone element
// evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
// },
//
// // Element is dropped into the list from another list
// onAdd: function (/**Event*/evt) {
// // same properties as onEnd
// },
//
// // Changed sorting within list
// onUpdate: function (/**Event*/evt) {
// // same properties as onEnd
// },
//
// // Called by any change to the list (add / update / remove)
// onSort: function (/**Event*/evt) {
// // same properties as onEnd
// },
//
// // Element is removed from the list into another list
// onRemove: function (/**Event*/evt) {
// // same properties as onEnd
// },
//
// // Attempt to drag a filtered element
// onFilter: function (/**Event*/evt) {
// var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.
// },
//
// // Event when you move an item in the list or between lists
// onMove: function (/**Event*/evt, /**Event*/originalEvent) {
// // Example: https://jsbin.com/nawahef/edit?js,output
// evt.dragged; // dragged HTMLElement
// evt.draggedRect; // DOMRect {left, top, right, bottom}
// evt.related; // HTMLElement on which have guided
// evt.relatedRect; // DOMRect
// evt.willInsertAfter; // Boolean that is true if Sortable will insert drag element after target by default
// originalEvent.clientY; // mouse position
// // return false; — for cancel
// // return -1; — insert before target
// // return 1; — insert after target
// },
//
// // Called when creating a clone of element
// onClone: function (/**Event*/evt) {
// var origEl = evt.item;
// var cloneEl = evt.clone;
// },
//
// // Called when dragging element changes position
// onChange: function(/**Event*/evt) {
// evt.newIndex // most likely why this event is used is to get the dragging element's current index
// // same properties as onEnd
// }
// });
const dispatch = createEventDispatcher();
export let options = {};
export let items = [];
function create(node) {
const sortable = Sortable.create(
node,
Object.assign(options, {
onRemove: (ev) => {
if (ev.oldIndicies.length > 0) {
for (let i = ev.oldIndicies.length - 1; i >= 0; i--) {
items.splice(ev.oldIndicies[i].index, 1);
}
} else {
items.splice(ev.oldIndex, 1);
}
},
onAdd: (ev) => {
if (ev.newIndicies.length > 0) {
for (let i = 0; i < ev.newIndicies.length; i++) {
items.splice(
ev.newIndicies[i].index,
0,
$store.items[i]
);
}
} else {
items.splice(ev.newIndex, 0, $store.items);
}
},
onStart: (ev) => {
store.set({
from: ev.from,
items:
ev.oldIndicies.length > 0
? ev.oldIndicies.map((i) => items[i.index])
: items[ev.oldIndex],
});
},
onUpdate: (ev) => {
// guard against 'update' event fired when there is more than one list -
// source list is reordered, and this breaks 'remove' event fired later,
// as indicies values are not valid any longer
if (ev.pullMode !== true && ev.oldIndicies.length > 0) {
moveArrayElements(
items,
ev.oldIndicies,
ev.newIndicies
);
} else {
moveArrayElement(items, ev.oldIndex, ev.newIndex);
}
},
onEnd: () => {
dispatch("change");
},
})
);
return {
destroy() {
sortable.destroy();
},
};
}
function moveArrayElement(array, from, to) {
const item = array[from];
array.splice(from, 1);
array.splice(to, 0, item);
}
function moveArrayElements(array, oldIndicies, newIndicies) {
const itemsNo = oldIndicies.length;
// mappings [ [src idx1, dest idx2], ... , [src idxn, dest idxn]]
let mappings = new Array(itemsNo);
for (let i = 0; i < itemsNo; i++) {
mappings[i] = [oldIndicies[i].index, newIndicies[i].index];
}
// first extract items to be moved
let tmp = new Array(itemsNo);
for (i = itemsNo - 1; i >= 0; i--) {
tmp[i] = array.splice(mappings[i][0], 1)[0];
}
// moved items form a continous block, so it is easy to insert them
array.splice(mappings[0][1], 0, ...tmp);
}
</script>
<div use:create>
{#each items as item}
<slot {item} />
{/each}
</div>