@c2n/reorder-list 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Reorder List
2
+
3
+ This component supports for drag-and-drop for changering the order of items in the list
4
+
5
+ ## Theme properties
@@ -0,0 +1,473 @@
1
+ import { nothing as E } from "lit/html.js";
2
+ import { directive as k, PartType as D } from "lit/directive.js";
3
+ import { AsyncDirective as M } from "lit/async-directive.js";
4
+ import { unsafeCSS as $, LitElement as R, html as u } from "lit";
5
+ import { query as S, queryAll as T, property as y, state as O, eventOptions as B, customElement as L } from "lit/decorators.js";
6
+ import { classMap as f } from "lit/directives/class-map.js";
7
+ import { styleMap as F } from "lit/directives/style-map.js";
8
+ const z = /* @__PURE__ */ new WeakMap();
9
+ let x = 0;
10
+ const v = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new WeakSet(), P = () => new Promise((i) => requestAnimationFrame(i)), C = (i, t) => {
11
+ const e = i - t;
12
+ return e === 0 ? void 0 : e;
13
+ }, w = (i, t) => {
14
+ const e = i / t;
15
+ return e === 1 ? void 0 : e;
16
+ }, b = { left: (i, t) => {
17
+ const e = C(i, t);
18
+ return { value: e, transform: e && `translateX(${e}px)` };
19
+ }, top: (i, t) => {
20
+ const e = C(i, t);
21
+ return { value: e, transform: e && `translateY(${e}px)` };
22
+ }, width: (i, t) => {
23
+ const e = w(i, t);
24
+ return { value: e, transform: e && `scaleX(${e})` };
25
+ }, height: (i, t) => {
26
+ const e = w(i, t);
27
+ return { value: e, transform: e && `scaleY(${e})` };
28
+ } }, U = { duration: 333, easing: "ease-in-out" }, _ = ["left", "top", "width", "height", "opacity", "color", "background"], A = /* @__PURE__ */ new WeakMap();
29
+ class X extends M {
30
+ constructor(t) {
31
+ if (super(t), this.t = null, this.i = null, this.o = !0, this.shouldLog = !1, t.type === D.CHILD)
32
+ throw Error("The `animate` directive must be used in attribute position.");
33
+ this.createFinished();
34
+ }
35
+ createFinished() {
36
+ var t;
37
+ (t = this.resolveFinished) === null || t === void 0 || t.call(this), this.finished = new Promise((e) => {
38
+ this.h = e;
39
+ });
40
+ }
41
+ async resolveFinished() {
42
+ var t;
43
+ (t = this.h) === null || t === void 0 || t.call(this), this.h = void 0;
44
+ }
45
+ render(t) {
46
+ return E;
47
+ }
48
+ getController() {
49
+ return z.get(this.l);
50
+ }
51
+ isDisabled() {
52
+ var t;
53
+ return this.options.disabled || ((t = this.getController()) === null || t === void 0 ? void 0 : t.disabled);
54
+ }
55
+ update(t, [e]) {
56
+ var s;
57
+ const o = this.l === void 0;
58
+ return o && (this.l = (s = t.options) === null || s === void 0 ? void 0 : s.host, this.l.addController(this), this.element = t.element, A.set(this.element, this)), this.optionsOrCallback = e, (o || typeof e != "function") && this.u(e), this.render(e);
59
+ }
60
+ u(t) {
61
+ var e, s;
62
+ t = t != null ? t : {};
63
+ const o = this.getController();
64
+ o !== void 0 && ((t = { ...o.defaultOptions, ...t }).keyframeOptions = { ...o.defaultOptions.keyframeOptions, ...t.keyframeOptions }), (e = (s = t).properties) !== null && e !== void 0 || (s.properties = _), this.options = t;
65
+ }
66
+ v() {
67
+ const t = {}, e = this.element.getBoundingClientRect(), s = getComputedStyle(this.element);
68
+ return this.options.properties.forEach((o) => {
69
+ var r;
70
+ const n = (r = e[o]) !== null && r !== void 0 ? r : b[o] ? void 0 : s[o], l = Number(n);
71
+ t[o] = isNaN(l) ? n + "" : l;
72
+ }), t;
73
+ }
74
+ p() {
75
+ let t, e = !0;
76
+ return this.options.guard && (t = this.options.guard(), e = ((s, o) => {
77
+ if (Array.isArray(s)) {
78
+ if (Array.isArray(o) && o.length === s.length && s.every((r, n) => r === o[n]))
79
+ return !1;
80
+ } else if (o === s)
81
+ return !1;
82
+ return !0;
83
+ })(t, this.m)), this.o = this.l.hasUpdated && !this.isDisabled() && !this.isAnimating() && e && this.element.isConnected, this.o && (this.m = Array.isArray(t) ? Array.from(t) : t), this.o;
84
+ }
85
+ hostUpdate() {
86
+ var t;
87
+ typeof this.optionsOrCallback == "function" && this.u(this.optionsOrCallback()), this.p() && (this.g = this.v(), this.t = (t = this.t) !== null && t !== void 0 ? t : this.element.parentNode, this.i = this.element.nextSibling);
88
+ }
89
+ async hostUpdated() {
90
+ if (!this.o || !this.element.isConnected || this.options.skipInitial && !this.isHostRendered)
91
+ return;
92
+ let t;
93
+ this.prepare(), await P;
94
+ const e = this._(), s = this.A(this.options.keyframeOptions, e), o = this.v();
95
+ if (this.g !== void 0) {
96
+ const { from: r, to: n } = this.O(this.g, o, e);
97
+ this.log("measured", [this.g, o, r, n]), t = this.calculateKeyframes(r, n);
98
+ } else {
99
+ const r = v.get(this.options.inId);
100
+ if (r) {
101
+ v.delete(this.options.inId);
102
+ const { from: n, to: l } = this.O(r, o, e);
103
+ t = this.calculateKeyframes(n, l), t = this.options.in ? [{ ...this.options.in[0], ...t[0] }, ...this.options.in.slice(1), t[1]] : t, x++, t.forEach((d) => d.zIndex = x);
104
+ } else
105
+ this.options.in && (t = [...this.options.in, {}]);
106
+ }
107
+ this.animate(t, s);
108
+ }
109
+ resetStyles() {
110
+ var t;
111
+ this.P !== void 0 && (this.element.setAttribute("style", (t = this.P) !== null && t !== void 0 ? t : ""), this.P = void 0);
112
+ }
113
+ commitStyles() {
114
+ var t, e;
115
+ this.P = this.element.getAttribute("style"), (t = this.webAnimation) === null || t === void 0 || t.commitStyles(), (e = this.webAnimation) === null || e === void 0 || e.cancel();
116
+ }
117
+ reconnected() {
118
+ }
119
+ async disconnected() {
120
+ var t;
121
+ if (!this.o || (this.options.id !== void 0 && v.set(this.options.id, this.g), this.options.out === void 0))
122
+ return;
123
+ if (this.prepare(), await P(), (t = this.t) === null || t === void 0 ? void 0 : t.isConnected) {
124
+ const s = this.i && this.i.parentNode === this.t ? this.i : null;
125
+ if (this.t.insertBefore(this.element, s), this.options.stabilizeOut) {
126
+ const o = this.v();
127
+ this.log("stabilizing out");
128
+ const r = this.g.left - o.left, n = this.g.top - o.top;
129
+ getComputedStyle(this.element).position !== "static" || r === 0 && n === 0 || (this.element.style.position = "relative"), r !== 0 && (this.element.style.left = r + "px"), n !== 0 && (this.element.style.top = n + "px");
130
+ }
131
+ }
132
+ const e = this.A(this.options.keyframeOptions);
133
+ await this.animate(this.options.out, e), this.element.remove();
134
+ }
135
+ prepare() {
136
+ this.createFinished();
137
+ }
138
+ start() {
139
+ var t, e;
140
+ (e = (t = this.options).onStart) === null || e === void 0 || e.call(t, this);
141
+ }
142
+ didFinish(t) {
143
+ var e, s;
144
+ t && ((s = (e = this.options).onComplete) === null || s === void 0 || s.call(e, this)), this.g = void 0, this.animatingProperties = void 0, this.frames = void 0, this.resolveFinished();
145
+ }
146
+ _() {
147
+ const t = [];
148
+ for (let e = this.element.parentNode; e; e = e == null ? void 0 : e.parentNode) {
149
+ const s = A.get(e);
150
+ s && !s.isDisabled() && s && t.push(s);
151
+ }
152
+ return t;
153
+ }
154
+ get isHostRendered() {
155
+ const t = I.has(this.l);
156
+ return t || this.l.updateComplete.then(() => {
157
+ I.add(this.l);
158
+ }), t;
159
+ }
160
+ A(t, e = this._()) {
161
+ const s = { ...U };
162
+ return e.forEach((o) => Object.assign(s, o.options.keyframeOptions)), Object.assign(s, t), s;
163
+ }
164
+ O(t, e, s) {
165
+ t = { ...t }, e = { ...e };
166
+ const o = s.map((l) => l.animatingProperties).filter((l) => l !== void 0);
167
+ let r = 1, n = 1;
168
+ return o !== void 0 && (o.forEach((l) => {
169
+ l.width && (r /= l.width), l.height && (n /= l.height);
170
+ }), t.left !== void 0 && e.left !== void 0 && (t.left = r * t.left, e.left = r * e.left), t.top !== void 0 && e.top !== void 0 && (t.top = n * t.top, e.top = n * e.top)), { from: t, to: e };
171
+ }
172
+ calculateKeyframes(t, e, s = !1) {
173
+ var o;
174
+ const r = {}, n = {};
175
+ let l = !1;
176
+ const d = {};
177
+ for (const p in e) {
178
+ const c = t[p], g = e[p];
179
+ if (p in b) {
180
+ const N = b[p];
181
+ if (c === void 0 || g === void 0)
182
+ continue;
183
+ const m = N(c, g);
184
+ m.transform !== void 0 && (d[p] = m.value, l = !0, r.transform = `${(o = r.transform) !== null && o !== void 0 ? o : ""} ${m.transform}`);
185
+ } else
186
+ c !== g && c !== void 0 && g !== void 0 && (l = !0, r[p] = c, n[p] = g);
187
+ }
188
+ return r.transformOrigin = n.transformOrigin = s ? "center center" : "top left", this.animatingProperties = d, l ? [r, n] : void 0;
189
+ }
190
+ async animate(t, e = this.options.keyframeOptions) {
191
+ this.start(), this.frames = t;
192
+ let s = !1;
193
+ if (!this.isAnimating() && !this.isDisabled() && (this.options.onFrames && (this.frames = t = this.options.onFrames(this), this.log("modified frames", t)), t !== void 0)) {
194
+ this.log("animate", [t, e]), s = !0, this.webAnimation = this.element.animate(t, e);
195
+ const o = this.getController();
196
+ o == null || o.add(this);
197
+ try {
198
+ await this.webAnimation.finished;
199
+ } catch {
200
+ }
201
+ o == null || o.remove(this);
202
+ }
203
+ return this.didFinish(s), s;
204
+ }
205
+ isAnimating() {
206
+ var t, e;
207
+ return ((t = this.webAnimation) === null || t === void 0 ? void 0 : t.playState) === "running" || ((e = this.webAnimation) === null || e === void 0 ? void 0 : e.pending);
208
+ }
209
+ log(t, e) {
210
+ this.shouldLog && !this.isDisabled() && console.log(t, this.options.id, e);
211
+ }
212
+ }
213
+ const Y = k(X), j = ["top", "right", "bottom", "left"];
214
+ class H extends M {
215
+ constructor(t) {
216
+ if (super(t), t.type !== D.ELEMENT)
217
+ throw Error("The `position` directive must be used in attribute position.");
218
+ }
219
+ render(t, e) {
220
+ return E;
221
+ }
222
+ update(t, [e, s]) {
223
+ var o;
224
+ return this.l === void 0 && (this.l = (o = t.options) === null || o === void 0 ? void 0 : o.host, this.l.addController(this)), this.j = t.element, this.S = e, this.C = s != null ? s : ["left", "top", "width", "height"], this.render(e, s);
225
+ }
226
+ hostUpdated() {
227
+ this.F();
228
+ }
229
+ F() {
230
+ var t, e;
231
+ const s = typeof this.S == "function" ? this.S() : (t = this.S) === null || t === void 0 ? void 0 : t.value, o = s.offsetParent;
232
+ if (s === void 0 || !o)
233
+ return;
234
+ const r = s.getBoundingClientRect(), n = o.getBoundingClientRect();
235
+ (e = this.C) === null || e === void 0 || e.forEach((l) => {
236
+ const d = j.includes(l) ? r[l] - n[l] : r[l];
237
+ this.j.style[l] = d + "px";
238
+ });
239
+ }
240
+ }
241
+ k(H);
242
+ const q = `:host{position:relative}.c2-reorder-list-container{display:flex;flex-direction:column;position:relative;gap:var(--c2-reorder-list-container-gap, 0px);border-width:var(--c2-reorder-list-container-border-width, 0px);border-color:var(--c2-reorder-list-container-border-color, #bcbcc6);border-style:solid;border-radius:var(--c2-reorder-list-container-border-radius, 8px);padding:var(--c2-reorder-list-container-padding, 0px);margin:var(--c2-reorder-list-container-margin, 0px)}.c2-reorder-list-container .list-item{background:var(--c2-reorder-list-item-background, transparent);padding:var(--c2-reorder-list-item-padding, 0px);border-radius:initial;box-sizing:border-box;position:relative}.c2-reorder-list-container .list-item:not(:last-child){border-bottom:var(--c2-reorder-list-divider-height, 0px) solid var(--c2-reorder-list-divider-color, #bcbcc6)}.c2-reorder-list-container .list-item:first-child{border-top-left-radius:inherit;border-top-right-radius:inherit}.c2-reorder-list-container .list-item:last-child{border-bottom-left-radius:inherit;border-bottom-right-radius:inherit}.c2-reorder-list-container .list-item--reorderable{cursor:pointer}.c2-reorder-list-container .list-item--reorderable ::slotted(*){touch-action:none;-webkit-user-drag:none;-webkit-tap-highlight-color:transparent;user-select:none}.c2-reorder-list-container .dragging-placeholder{background-color:var(--c2-reorder-list-placeholder-background, transparent);z-index:1}.dragging-item{box-sizing:border-box;position:fixed;background-color:var(--c2-reorder-list-dragging-item-background, transparent);cursor:pointer;z-index:100000;display:none}.dragging-item.dragging-item--active{display:flex}
243
+ `;
244
+ var K = Object.defineProperty, W = Object.getOwnPropertyDescriptor, h = (i, t, e, s) => {
245
+ for (var o = s > 1 ? void 0 : s ? W(t, e) : t, r = i.length - 1, n; r >= 0; r--)
246
+ (n = i[r]) && (o = (s ? n(t, e, o) : n(o)) || o);
247
+ return s && o && K(t, e, o), o;
248
+ };
249
+ let a = class extends R {
250
+ constructor() {
251
+ super(...arguments), this.editable = !1, this.dragStartThreshold = 10, this.autoScrollDisabled = !1, this.placeholderSlotIndex = -1, this.placeholderSize = { width: "0px", height: "0px" }, this.childListObserver = null, this.hostBoundingClientRect = null, this.scrollableParent = null, this.scrollableParentRect = void 0, this.pickupPosition = { x: 0, y: 0 }, this.itemPositions = [], this.currentDraggingItem = null, this.currentDraggingSlotIndex = -1, this.skipAnimation = !1, this.hasStartedDragging = !1, this.scrollingTimer = 0, this.cacheMouseEvent = null, this.handleMouseMove = async (i) => {
252
+ var t;
253
+ if (!this.isUpdatePending)
254
+ if (this.hasStartedDragging) {
255
+ this.cacheMouseEvent = { pageX: i.pageX, pageY: i.pageY }, this.applyTransform(i);
256
+ const e = this.getItemIndexFromPointerPosition(i);
257
+ e > -1 && e !== this.placeholderSlotIndex && (this.placeholderSlotIndex = e);
258
+ } else {
259
+ const e = this.getPointerPosition(i), s = Math.abs(e.x - this.pickupPosition.x), o = Math.abs(e.y - this.pickupPosition.y);
260
+ if (!(s + o >= this.dragStartThreshold))
261
+ return;
262
+ this.hasStartedDragging = !0, this.skipAnimation = !0;
263
+ const n = this.getSlottedTargetIndex(i);
264
+ if (n == -1)
265
+ return;
266
+ const l = i.composedPath()[n], d = l.getAttribute("slot"), p = i.composedPath()[n + 2];
267
+ if (!d) {
268
+ this.stopDrag();
269
+ return;
270
+ }
271
+ this.pickupPosition = this.getPointerPosition(i);
272
+ const c = p.getBoundingClientRect(), g = l.getBoundingClientRect();
273
+ this.currentDraggingItem = l, this.placeholderSlotIndex = parseInt(d), this.currentDraggingSlotIndex = parseInt(d), this.placeholderSize = { width: `${g.width}px`, height: `${g.height}px` }, this.draggingElement.style.width = `${c.width}px`, this.draggingElement.style.height = `${c.height}px`, this.draggingElement.style.top = `${c.top}px`, this.draggingElement.style.left = `${c.left}px`, l.setAttribute("slot", "dragging-item"), this.applyTransform(i), this.updateItemPositions(), this.scrollableParent = this.getNearestScrollableParent(), this.scrollableParentRect = (t = this.scrollableParent) == null ? void 0 : t.getBoundingClientRect(), this.scrollableParent && this.scrollableParent.addEventListener("scroll", this.handleScroll, { passive: !0 });
274
+ }
275
+ }, this.handleScroll = () => {
276
+ if (this.cacheMouseEvent) {
277
+ this.updateItemPositions(), this.applyTransform(this.cacheMouseEvent);
278
+ const i = this.getItemIndexFromPointerPosition(this.cacheMouseEvent);
279
+ i > -1 && i !== this.placeholderSlotIndex && (this.placeholderSlotIndex = i);
280
+ }
281
+ }, this.handleMouseUp = () => {
282
+ if (this.hasStartedDragging && this.currentDraggingItem) {
283
+ const i = this.getSortChildrenBySlot(), t = this.placeholderSlotIndex;
284
+ for (let e = 0; e < i.length; e++) {
285
+ const s = parseInt(i[e].getAttribute("slot"));
286
+ t > this.currentDraggingSlotIndex && s > this.currentDraggingSlotIndex && s <= t && i[e].setAttribute("slot", (s - 1).toString()), t < this.currentDraggingSlotIndex && s >= t && s < this.currentDraggingSlotIndex && i[e].setAttribute("slot", (s + 1).toString());
287
+ }
288
+ this.currentDraggingItem.setAttribute("slot", this.placeholderSlotIndex.toString()), this.skipAnimation = !0, this.dispatchChangeEvent();
289
+ }
290
+ this.stopDrag();
291
+ }, this.startScrollInterval = (i) => {
292
+ this.stopScrolling();
293
+ const t = 2;
294
+ this.scrollingTimer = window.requestAnimationFrame(() => {
295
+ var e;
296
+ this.skipAnimation = !0, (e = this.scrollableParent) == null || e.scrollBy(0, i === "up" ? -t : t), this.startScrollInterval(i);
297
+ });
298
+ };
299
+ }
300
+ connectedCallback() {
301
+ super.connectedCallback(), this.observeChildList(), this.updateSlotMapping();
302
+ }
303
+ disconnectedCallback() {
304
+ super.disconnectedCallback(), this.childListObserver && this.childListObserver.disconnect();
305
+ }
306
+ renderSlots() {
307
+ const i = { "list-item--reorderable": this.editable }, t = this.createPlaceHolder(i), e = this.getChildItems().length, s = Array.from({ length: e }).map((o, r) => {
308
+ const n = {
309
+ id: r,
310
+ inId: r,
311
+ skipInitial: !0,
312
+ disabled: this.skipAnimation || r === e - 1
313
+ };
314
+ return this.placeholderSlotIndex !== -1 && this.currentDraggingSlotIndex === r ? u`` : u`<div class="list-item ${f(i)}" ${Y(n)}>
315
+ <slot name="${r}"></slot>
316
+ </div>`;
317
+ });
318
+ return this.placeholderSlotIndex !== -1 && (this.placeholderSlotIndex === this.currentDraggingSlotIndex ? s[this.placeholderSlotIndex] = t : this.placeholderSlotIndex <= this.currentDraggingSlotIndex ? s.splice(this.placeholderSlotIndex, 0, t) : s.splice(this.placeholderSlotIndex + 1, 0, t)), s;
319
+ }
320
+ createPlaceHolder(i) {
321
+ return u` <div class="list-item placeholder ${f(i)}">
322
+ <slot name="placeholder">
323
+ <div class="dragging-placeholder" style=${F(this.placeholderSize)}></div>
324
+ </slot>
325
+ </div>`;
326
+ }
327
+ render() {
328
+ const i = { "dragging-item--active": this.placeholderSlotIndex > -1 };
329
+ return u`
330
+ <div class="c2-reorder-list-container" @mousedown=${this.handleMouseDown}>${this.renderSlots()}</div>
331
+ <div class="dragging-item ${f(i)}">
332
+ <slot name="dragging-item"></slot>
333
+ </div>
334
+ `;
335
+ }
336
+ handleMouseDown(i) {
337
+ const t = this.getSlottedTargetIndex(i);
338
+ if (t == -1)
339
+ return;
340
+ i.composedPath()[t].getAttribute("slot") && (this.hostBoundingClientRect = this.getBoundingClientRect(), this.pickupPosition = this.getPointerPosition(i), document.addEventListener("mousemove", this.handleMouseMove, { passive: !0 }), document.addEventListener("mouseup", this.handleMouseUp, { passive: !0 }));
341
+ }
342
+ getItemIndexFromPointerPosition(i) {
343
+ return this.itemPositions.findIndex((s) => {
344
+ const o = s.x + 5, r = s.y + 5, n = s.width - 5, l = s.height - 5;
345
+ return o < i.pageX && r < i.pageY && o + n > i.pageX && r + l > i.pageY;
346
+ });
347
+ }
348
+ dispatchChangeEvent() {
349
+ const i = this.getChildItems().map((t) => parseInt(t.getAttribute("slot")));
350
+ this.dispatchEvent(
351
+ new CustomEvent("change", {
352
+ bubbles: !0,
353
+ cancelable: !0,
354
+ detail: i
355
+ })
356
+ );
357
+ }
358
+ stopDrag() {
359
+ document.removeEventListener("mousemove", this.handleMouseMove), document.removeEventListener("mouseup", this.handleMouseUp), this.scrollableParent && this.scrollableParent.removeEventListener("scroll", this.handleScroll), this.pickupPosition = { x: 0, y: 0 }, this.itemPositions = [], this.hasStartedDragging = !1, this.placeholderSlotIndex = -1, this.scrollableParent = null, this.cacheMouseEvent = null;
360
+ }
361
+ getPointerPosition(i) {
362
+ var s, o;
363
+ const t = i.pageX - (((s = this.hostBoundingClientRect) == null ? void 0 : s.left) || 0), e = i.pageY - (((o = this.hostBoundingClientRect) == null ? void 0 : o.top) || 0);
364
+ return { x: t, y: e };
365
+ }
366
+ observeChildList() {
367
+ this.childListObserver = new MutationObserver(() => {
368
+ this.updateSlotMapping();
369
+ }), this.childListObserver.observe(this, { subtree: !1, childList: !0 });
370
+ }
371
+ async firstUpdated() {
372
+ this.updateSlotMapping();
373
+ }
374
+ updated(i) {
375
+ i.has("placeholderSlotIndex") && (this.skipAnimation = !1, this.startScrollingIfNecessary(), this.updateItemPositions()), super.updated(i);
376
+ }
377
+ startScrollingIfNecessary() {
378
+ if (this.placeholderElement && this.scrollableParent) {
379
+ const i = this.placeholderElement.getBoundingClientRect(), t = i.top;
380
+ t < this.scrollableParentRect.top ? this.startScrollInterval("up") : t + i.height > this.scrollableParentRect.bottom ? this.startScrollInterval("down") : this.stopScrolling();
381
+ }
382
+ }
383
+ stopScrolling() {
384
+ window.cancelAnimationFrame(this.scrollingTimer);
385
+ }
386
+ updateSlotMapping() {
387
+ const i = this.getChildItems();
388
+ let t = /* @__PURE__ */ new Map(), e = 0, s = !1;
389
+ for (const n of i) {
390
+ const l = n.getAttribute("slot");
391
+ l && (Number.isNaN(parseInt(l)) ? l === "dragging-item" && t.set(e, this.currentDraggingSlotIndex) : t.set(e, parseInt(l))), e++;
392
+ }
393
+ const o = Array.from(t.keys()).sort(
394
+ (n, l) => t.get(n) - t.get(l)
395
+ );
396
+ o.forEach((n, l) => {
397
+ t.get(n), i[n].setAttribute("slot", l.toString()), s = !0;
398
+ });
399
+ let r = o.length;
400
+ for (const n of i)
401
+ n.getAttribute("slot") || (n.setAttribute("slot", r.toString()), r++, s = !0);
402
+ s && (this.requestUpdate(), this.dispatchChangeEvent());
403
+ }
404
+ updateItemPositions() {
405
+ this.itemPositions = [], this.listItemElements.forEach((i) => {
406
+ this.itemPositions.push(i.getBoundingClientRect());
407
+ });
408
+ }
409
+ applyTransform(i) {
410
+ const t = this.getPointerPosition(i), e = t.x - this.pickupPosition.x, s = t.y - this.pickupPosition.y;
411
+ this.draggingElement.style.transform = this.getTransform(e, s);
412
+ }
413
+ getTransform(i, t) {
414
+ return `translate3d(${Math.round(i)}px, ${Math.round(t)}px, 0)`;
415
+ }
416
+ getSlottedTargetIndex(i) {
417
+ return i.composedPath().findIndex((t) => t instanceof Element && !!t.getAttribute("slot"));
418
+ }
419
+ getChildItems() {
420
+ const i = [];
421
+ for (const t of this.children) {
422
+ const e = t.getAttribute("slot");
423
+ (e === "dragging-item" || !e || !Number.isNaN(parseInt(e))) && i.push(t);
424
+ }
425
+ return i;
426
+ }
427
+ getSortChildrenBySlot() {
428
+ return this.getChildItems().filter((i) => i !== this.currentDraggingItem).sort((i, t) => {
429
+ const e = i.getAttribute("slot"), s = t.getAttribute("slot");
430
+ return parseInt(e || "") - parseInt(s || "");
431
+ });
432
+ }
433
+ getNearestScrollableParent(i = this) {
434
+ return i == null ? null : i.scrollHeight > i.clientHeight ? i : this.getNearestScrollableParent(i.parentNode);
435
+ }
436
+ };
437
+ a.styles = $(q);
438
+ h([
439
+ S(".dragging-item")
440
+ ], a.prototype, "draggingElement", 2);
441
+ h([
442
+ S(".placeholder")
443
+ ], a.prototype, "placeholderElement", 2);
444
+ h([
445
+ S(".c2-reorder-list-container")
446
+ ], a.prototype, "containerElement", 2);
447
+ h([
448
+ T(".list-item")
449
+ ], a.prototype, "listItemElements", 2);
450
+ h([
451
+ y({ type: Boolean, reflect: !0 })
452
+ ], a.prototype, "editable", 2);
453
+ h([
454
+ y({ type: Number })
455
+ ], a.prototype, "dragStartThreshold", 2);
456
+ h([
457
+ y({ type: Boolean })
458
+ ], a.prototype, "autoScrollDisabled", 2);
459
+ h([
460
+ O()
461
+ ], a.prototype, "placeholderSlotIndex", 2);
462
+ h([
463
+ O()
464
+ ], a.prototype, "placeholderSize", 2);
465
+ h([
466
+ B({ passive: !0 })
467
+ ], a.prototype, "handleMouseDown", 1);
468
+ a = h([
469
+ L("c2-reorder-list")
470
+ ], a);
471
+ export {
472
+ a as ReorderList
473
+ };
package/package.json ADDED
@@ -0,0 +1,112 @@
1
+ {
2
+ "name": "@c2n/reorder-list",
3
+ "version": "0.0.3",
4
+ "type": "module",
5
+ "main": "dist/reorder-list.js",
6
+ "exports": {
7
+ ".": "./dist/reorder-list.js"
8
+ },
9
+ "types": "types/src/index.d.ts",
10
+ "files": [
11
+ "dist",
12
+ "types"
13
+ ],
14
+ "keywords": [
15
+ "reorder-list",
16
+ "web component",
17
+ "lit"
18
+ ],
19
+ "author": "code2nguyen@gmail.com",
20
+ "publishConfig": {
21
+ "registry": "https://registry.npmjs.org",
22
+ "access": "public"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/code2nguyen/web-components.git"
27
+ },
28
+ "scripts": {
29
+ "lint": "npm run lint:lit-analyzer && npm run lint:eslint",
30
+ "lint:eslint": "eslint 'src/**/*.ts'",
31
+ "lint:lit-analyzer": "lit-analyzer",
32
+ "dev": "vite",
33
+ "build": "tsc && vite build"
34
+ },
35
+ "dependencies": {
36
+ "@c2n/wc-utils": "0.0.3",
37
+ "@lit-labs/motion": "1.0.3",
38
+ "lit": "2.3.0"
39
+ },
40
+ "devDependencies": {
41
+ "@typescript-eslint/eslint-plugin": "5.33.1",
42
+ "@typescript-eslint/parser": "5.33.1",
43
+ "eslint": "8.22.0",
44
+ "lit-analyzer": "1.2.1",
45
+ "prettier": "2.7.1",
46
+ "typescript": "4.7.4",
47
+ "vite": "3.0.8"
48
+ },
49
+ "prettier": {
50
+ "singleQuote": true,
51
+ "printWidth": 160,
52
+ "tabWidth": 2,
53
+ "semi": false
54
+ },
55
+ "eslintConfig": {
56
+ "root": true,
57
+ "extends": [
58
+ "eslint:recommended",
59
+ "plugin:@typescript-eslint/eslint-recommended",
60
+ "plugin:@typescript-eslint/recommended"
61
+ ],
62
+ "parser": "@typescript-eslint/parser",
63
+ "parserOptions": {
64
+ "ecmaVersion": 2020,
65
+ "sourceType": "module"
66
+ },
67
+ "plugins": [
68
+ "@typescript-eslint"
69
+ ],
70
+ "env": {
71
+ "browser": true
72
+ },
73
+ "rules": {
74
+ "no-prototype-builtins": "off",
75
+ "@typescript-eslint/ban-types": "off",
76
+ "@typescript-eslint/explicit-function-return-type": "off",
77
+ "@typescript-eslint/explicit-module-boundary-types": "off",
78
+ "@typescript-eslint/no-explicit-any": "error",
79
+ "@typescript-eslint/no-empty-function": "off",
80
+ "@typescript-eslint/no-non-null-assertion": "off",
81
+ "@typescript-eslint/no-unused-vars": [
82
+ "warn",
83
+ {
84
+ "argsIgnorePattern": "^_"
85
+ }
86
+ ]
87
+ },
88
+ "overrides": [
89
+ {
90
+ "files": [
91
+ "rollup.config.js",
92
+ "web-test-runner.config.js"
93
+ ],
94
+ "env": {
95
+ "node": true
96
+ }
97
+ },
98
+ {
99
+ "files": [
100
+ "*_test.ts",
101
+ "**/custom_typings/*.ts",
102
+ "packages/labs/ssr/src/test/integration/tests/**",
103
+ "packages/labs/ssr/src/lib/util/parse5-utils.ts"
104
+ ],
105
+ "rules": {
106
+ "@typescript-eslint/no-explicit-any": "off"
107
+ }
108
+ }
109
+ ]
110
+ },
111
+ "gitHead": "da28e508ee15f842709ffa98937b20fef02a9e1d"
112
+ }
@@ -0,0 +1,2 @@
1
+ export * from './reorder-list';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,67 @@
1
+ import { LitElement, PropertyValueMap, TemplateResult } from 'lit';
2
+ export interface Point {
3
+ x: number;
4
+ y: number;
5
+ }
6
+ export interface MutateMouseEvent {
7
+ pageX: number;
8
+ pageY: number;
9
+ }
10
+ export declare class ReorderList extends LitElement {
11
+ protected draggingElement: HTMLElement;
12
+ protected placeholderElement: HTMLElement;
13
+ protected containerElement: HTMLElement;
14
+ protected listItemElements: NodeListOf<HTMLDivElement>;
15
+ editable: boolean;
16
+ dragStartThreshold: number;
17
+ autoScrollDisabled: boolean;
18
+ private placeholderSlotIndex;
19
+ private placeholderSize;
20
+ private childListObserver;
21
+ private hostBoundingClientRect;
22
+ private scrollableParent;
23
+ private scrollableParentRect;
24
+ private pickupPosition;
25
+ private itemPositions;
26
+ private currentDraggingItem;
27
+ private currentDraggingSlotIndex;
28
+ private skipAnimation;
29
+ private hasStartedDragging;
30
+ private scrollingTimer;
31
+ private cacheMouseEvent;
32
+ /** Cached positions of scrollable parent elements. */
33
+ connectedCallback(): void;
34
+ disconnectedCallback(): void;
35
+ renderSlots(): TemplateResult<1 | 2>[];
36
+ private createPlaceHolder;
37
+ render(): TemplateResult<1>;
38
+ private handleMouseDown;
39
+ handleMouseMove: (event: MouseEvent) => Promise<void>;
40
+ handleScroll: () => void;
41
+ private getItemIndexFromPointerPosition;
42
+ handleMouseUp: () => void;
43
+ private dispatchChangeEvent;
44
+ private stopDrag;
45
+ private getPointerPosition;
46
+ private observeChildList;
47
+ protected firstUpdated(): Promise<void>;
48
+ protected updated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void;
49
+ private startScrollingIfNecessary;
50
+ private startScrollInterval;
51
+ private stopScrolling;
52
+ private updateSlotMapping;
53
+ private updateItemPositions;
54
+ private applyTransform;
55
+ private getTransform;
56
+ private getSlottedTargetIndex;
57
+ private getChildItems;
58
+ private getSortChildrenBySlot;
59
+ private getNearestScrollableParent;
60
+ static styles: import("lit").CSSResult;
61
+ }
62
+ declare global {
63
+ interface HTMLElementTagNameMap {
64
+ 'c2-reorder-list': ReorderList;
65
+ }
66
+ }
67
+ //# sourceMappingURL=reorder-list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reorder-list.d.ts","sourceRoot":"","sources":["../../src/reorder-list.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,UAAU,EAAE,gBAAgB,EAAE,cAAc,EAAa,MAAM,KAAK,CAAA;AAMnF,MAAM,WAAW,KAAK;IACpB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV;AACD,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd;AAED,qBACa,WAAY,SAAQ,UAAU;IAChB,SAAS,CAAC,eAAe,EAAG,WAAW,CAAA;IACzC,SAAS,CAAC,kBAAkB,EAAG,WAAW,CAAA;IAC5B,SAAS,CAAC,gBAAgB,EAAG,WAAW,CAAA;IACrD,SAAS,CAAC,gBAAgB,EAAG,UAAU,CAAC,cAAc,CAAC,CAAA;IAEnC,QAAQ,UAAQ;IAChC,kBAAkB,SAAK;IACtB,kBAAkB,UAAQ;IAGvD,OAAO,CAAC,oBAAoB,CAAK;IAGjC,OAAO,CAAC,eAAe,CAAkC;IAEzD,OAAO,CAAC,iBAAiB,CAAgC;IACzD,OAAO,CAAC,sBAAsB,CAAuB;IACrD,OAAO,CAAC,gBAAgB,CAA2B;IACnD,OAAO,CAAC,oBAAoB,CAAiC;IAC7D,OAAO,CAAC,cAAc,CAAwB;IAC9C,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,mBAAmB,CAAuB;IAClD,OAAO,CAAC,wBAAwB,CAAK;IAErC,OAAO,CAAC,aAAa,CAAQ;IAC7B,OAAO,CAAC,kBAAkB,CAAQ;IAClC,OAAO,CAAC,cAAc,CAAI;IAC1B,OAAO,CAAC,eAAe,CAAgC;IACvD,sDAAsD;IAG7C,iBAAiB,IAAI,IAAI;IAOzB,oBAAoB,IAAI,IAAI;IAQrC,WAAW;IAiCX,OAAO,CAAC,iBAAiB;IAQhB,MAAM;IAWf,OAAO,CAAC,eAAe;IAcvB,eAAe,UAAiB,UAAU,mBA+CzC;IAED,YAAY,aAcX;IAED,OAAO,CAAC,+BAA+B;IAYvC,aAAa,aAkBZ;IAED,OAAO,CAAC,mBAAmB;IAa3B,OAAO,CAAC,QAAQ;IAahB,OAAO,CAAC,kBAAkB;IAM1B,OAAO,CAAC,gBAAgB;cAOC,YAAY;cAKlB,OAAO,CAAC,kBAAkB,EAAE,gBAAgB,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,IAAI;IASvG,OAAO,CAAC,yBAAyB;IAcjC,OAAO,CAAC,mBAAmB,CAQ1B;IAED,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,iBAAiB;IAyCzB,OAAO,CAAC,mBAAmB;IAO3B,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,qBAAqB;IAU7B,OAAO,CAAC,0BAA0B;IAWlC,OAAgB,MAAM,0BAAoB;CAC3C;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,iBAAiB,EAAE,WAAW,CAAA;KAC/B;CACF"}