@editora/ui-sortable 0.1.13 → 0.2.0
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 +155 -1
- package/dist/headless-react.cjs.js +1 -0
- package/dist/headless-react.esm.js +41 -0
- package/dist/headless.cjs.js +1 -0
- package/dist/headless.esm.js +426 -0
- package/dist/react.esm.js +18 -18
- package/package.json +48 -13
- package/types/headless-react.d.ts +16 -0
- package/types/headless.d.ts +106 -0
- package/types/index.d.ts +2 -0
- package/types/react-internals.d.ts +22 -0
- package/types/react.d.ts +134 -0
- package/index.d.ts +0 -27
- package/react.d.ts +0 -122
package/README.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
<p align="center">
|
|
4
4
|
Drag-and-drop lists and boards as one web component, <code><ui-sortable></code>: reorder within a list, move
|
|
5
5
|
between lists, nest, clone and select several items, with full keyboard support. A small React wrapper is included.
|
|
6
|
+
Or make <a href="#headless-your-own-markup">your own markup</a> sortable with <code>createSortable</code> or the
|
|
7
|
+
<code>useSortable</code> React hook (5 KB).
|
|
6
8
|
</p>
|
|
7
9
|
|
|
8
10
|
<p align="center">
|
|
@@ -43,6 +45,9 @@
|
|
|
43
45
|
- **Your own markup, with or without React**: custom cards, list headers and empty states, and a free `data` field on
|
|
44
46
|
every item and list.
|
|
45
47
|
- **Translatable and quiet**: every text it shows or announces can be replaced (`labels`), and the hint line can be hidden.
|
|
48
|
+
- **Headless, for the markup you already have**: `createSortable(element)` and the `useSortable()` React hook add
|
|
49
|
+
dragging (mouse, touch, keyboard, between lists, auto-scroll) to your own list, grid or board, in 5 KB gzipped and with
|
|
50
|
+
no stylesheet. See [Headless](#headless-your-own-markup).
|
|
46
51
|
|
|
47
52
|
## Install
|
|
48
53
|
|
|
@@ -421,6 +426,153 @@ Ids must be unique across the group (a drop onto a board that already has one of
|
|
|
421
426
|
id there). Dragging between boards is for the pointer: for the keyboard and for code there is
|
|
422
427
|
`moveSelectionTo(otherSortable, { targetListId, beforeId?, parentId?, clone? })`.
|
|
423
428
|
|
|
429
|
+
## Headless: your own markup
|
|
430
|
+
|
|
431
|
+
When you already render the list yourself (your components, your CSS, your state), use the headless entry instead of the
|
|
432
|
+
component. It adds dragging to the children of an element, and nothing else: no markup, no stylesheet, no ui-core. It is
|
|
433
|
+
5 KB gzipped (the component is 24 KB).
|
|
434
|
+
|
|
435
|
+
```html
|
|
436
|
+
<ul id="tasks" aria-label="Tasks">
|
|
437
|
+
<li data-id="brief">Write the brief</li>
|
|
438
|
+
<li data-id="copy">Review the copy</li>
|
|
439
|
+
<li data-id="ship">Ship it</li>
|
|
440
|
+
</ul>
|
|
441
|
+
|
|
442
|
+
<style>
|
|
443
|
+
#tasks { list-style: none; padding: 0; width: 280px; font: 15px system-ui, sans-serif; }
|
|
444
|
+
#tasks li { padding: 10px 14px; margin: 6px 0; border: 1px solid #d0d7de; border-radius: 8px; background: #fff; cursor: grab; }
|
|
445
|
+
</style>
|
|
446
|
+
|
|
447
|
+
<script type="module">
|
|
448
|
+
import { createSortable } from '@editora/ui-sortable/headless';
|
|
449
|
+
|
|
450
|
+
// Your own list: drag an item, or focus one and press Space, then the arrow keys.
|
|
451
|
+
const list = createSortable(document.getElementById('tasks'), {
|
|
452
|
+
onSort: () => console.log('order', list.toArray().join(', '))
|
|
453
|
+
});
|
|
454
|
+
</script>
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
<p align="center">
|
|
458
|
+
<img src="https://raw.githubusercontent.com/ajaykr089/Editora/main/packages/ui-sortable/docs/images/headless.png" alt="A plain list of three tasks; the second, Review the copy, is lifted and follows the pointer with a shadow while its place in the list stays faded" width="320">
|
|
459
|
+
</p>
|
|
460
|
+
|
|
461
|
+
A drag follows the pointer with a copy of the item while the others slide aside; Escape puts it back. On touch, an item
|
|
462
|
+
lifts after a short press, so a swipe still scrolls the page. From the keyboard, Space picks up the focused item, the
|
|
463
|
+
arrow keys move it (Up and Down in a column, Left and Right in a row, all four in a grid), Home and End jump, Space drops
|
|
464
|
+
and Escape cancels; each step is announced to screen readers. Near the edge of a scrolling list or of the window, the
|
|
465
|
+
list scrolls. A click on an item is still a click, and a press in a field or a link inside it is left alone.
|
|
466
|
+
|
|
467
|
+
### In React
|
|
468
|
+
|
|
469
|
+
`useSortable` gives you a `ref` for the list element and calls `onChange` with the reordered array. React keeps
|
|
470
|
+
rendering the list: after a drop the elements go back where React put them, and your state decides the order.
|
|
471
|
+
|
|
472
|
+
```tsx
|
|
473
|
+
import { useState } from 'react';
|
|
474
|
+
import { useSortable } from '@editora/ui-sortable/headless/react';
|
|
475
|
+
|
|
476
|
+
type Task = { id: string; title: string };
|
|
477
|
+
|
|
478
|
+
const card = { padding: '10px 14px', margin: '6px 0', border: '1px solid #d0d7de', borderRadius: 8, background: '#fff', cursor: 'grab' };
|
|
479
|
+
|
|
480
|
+
export default function Tasks() {
|
|
481
|
+
const [tasks, setTasks] = useState<Task[]>([
|
|
482
|
+
{ id: 'brief', title: 'Write the brief' },
|
|
483
|
+
{ id: 'copy', title: 'Review the copy' },
|
|
484
|
+
{ id: 'ship', title: 'Ship it' }
|
|
485
|
+
]);
|
|
486
|
+
// Your markup and your state: after a drop, onChange gets the reordered array.
|
|
487
|
+
const { ref } = useSortable({ items: tasks, onChange: setTasks });
|
|
488
|
+
|
|
489
|
+
return (
|
|
490
|
+
<ul ref={ref} aria-label="Tasks" style={{ listStyle: 'none', padding: 0, width: 280 }}>
|
|
491
|
+
{tasks.map((task) => (
|
|
492
|
+
<li key={task.id} style={card}>
|
|
493
|
+
{task.title}
|
|
494
|
+
</li>
|
|
495
|
+
))}
|
|
496
|
+
</ul>
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Lists with the same `group` take each other's items. Each list keeps its own state: a move between two lists calls the
|
|
502
|
+
`onChange` of both, one without the item and one with it.
|
|
503
|
+
|
|
504
|
+
```tsx
|
|
505
|
+
import { useState } from 'react';
|
|
506
|
+
import { useSortable } from '@editora/ui-sortable/headless/react';
|
|
507
|
+
|
|
508
|
+
type Card = { id: string; title: string };
|
|
509
|
+
|
|
510
|
+
function Column({ title, initial }: { title: string; initial: Card[] }) {
|
|
511
|
+
const [cards, setCards] = useState(initial);
|
|
512
|
+
// Columns with the same group take each other's cards; each one keeps its own state.
|
|
513
|
+
const { ref } = useSortable({ items: cards, onChange: setCards, group: 'board' });
|
|
514
|
+
|
|
515
|
+
return (
|
|
516
|
+
<section style={{ width: 220, padding: 12, borderRadius: 10, background: '#f3f4f6' }}>
|
|
517
|
+
<h3 style={{ margin: '0 0 8px', font: '600 14px system-ui' }}>{title}</h3>
|
|
518
|
+
<ul ref={ref} aria-label={title} style={{ listStyle: 'none', margin: 0, padding: 0, minHeight: 48 }}>
|
|
519
|
+
{cards.map((card) => (
|
|
520
|
+
<li key={card.id} style={{ padding: '10px 12px', margin: '6px 0', borderRadius: 8, background: '#fff', boxShadow: '0 1px 2px rgba(0,0,0,0.08)', cursor: 'grab' }}>
|
|
521
|
+
{card.title}
|
|
522
|
+
</li>
|
|
523
|
+
))}
|
|
524
|
+
</ul>
|
|
525
|
+
</section>
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export default function Board() {
|
|
530
|
+
return (
|
|
531
|
+
<div style={{ display: 'flex', gap: 16, font: '14px system-ui' }}>
|
|
532
|
+
<Column title="To do" initial={[{ id: 'brief', title: 'Write the brief' }, { id: 'copy', title: 'Review the copy' }]} />
|
|
533
|
+
<Column title="Done" initial={[{ id: 'kickoff', title: 'Kick-off call' }]} />
|
|
534
|
+
</div>
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
<p align="center">
|
|
540
|
+
<img src="https://raw.githubusercontent.com/ajaykr089/Editora/main/packages/ui-sortable/docs/images/headless-board.png" alt="Two columns, To do and Done; the card Review the copy is being dragged from To do into Done" width="520">
|
|
541
|
+
</p>
|
|
542
|
+
|
|
543
|
+
### Headless options
|
|
544
|
+
|
|
545
|
+
`createSortable(element, options)` and `useSortable({ items, onChange, ...options })` take:
|
|
546
|
+
|
|
547
|
+
| Option | |
|
|
548
|
+
| --- | --- |
|
|
549
|
+
| `itemSelector` | Which children are items (a selector); the others (a header, say) stay put. All of them by default. |
|
|
550
|
+
| `handle` | Start a pointer drag only from this part of an item, such as a grip icon. The handle is also what keyboard users focus. |
|
|
551
|
+
| `group` | Lists with the same group exchange items. |
|
|
552
|
+
| `id` | The list's id in events; its `data-sortable-id` or `id` otherwise. |
|
|
553
|
+
| `direction` | `'vertical'`, `'horizontal'` or `'grid'`, for the arrow keys. Worked out from the layout otherwise. |
|
|
554
|
+
| `disabled` | Turn it off for now. |
|
|
555
|
+
| `canMove(move)` | Return `false` to refuse a move (`{ item, from, to, newIndex }`): a full list, a locked item. |
|
|
556
|
+
| `animation` | How long the others take to slide aside, in ms. `150`; none with `prefers-reduced-motion`. |
|
|
557
|
+
| `touchDelay`, `threshold` | The press before a touch drag (`200` ms), and how far a mouse moves before a press is a drag (`4` px). |
|
|
558
|
+
| `keyboard` | `false` leaves the keys alone, and the items are not made focusable. |
|
|
559
|
+
| `ghostClass`, `placeholderClass` | Classes for the copy that follows the pointer and for the item left in place, instead of the built-in look. |
|
|
560
|
+
| `getId(item)` | Each item's id; its `data-id` or `id` otherwise. |
|
|
561
|
+
| `announcements` | What screen readers are told (`lifted`, `moved`, `movedToList`, `dropped`, `cancelled`, `instructions`), for another language. |
|
|
562
|
+
| `revert` | `createSortable` only: put the elements back after a drop and leave the reordering to you (`useSortable` does this). |
|
|
563
|
+
|
|
564
|
+
The callbacks, for `createSortable` (`useSortable` takes `onStart` and `onEnd` too):
|
|
565
|
+
|
|
566
|
+
| Callback | |
|
|
567
|
+
| --- | --- |
|
|
568
|
+
| `onSort(event)` | The order changed: `{ type, item, itemId, from, to, fromId, toId, oldIndex, newIndex, source }`. `type` is `'update'` within a list; between lists the list it left gets `'remove'` and the list it joined `'add'`. |
|
|
569
|
+
| `onStart(event)`, `onEnd(event)` | A drag or keyboard move began, and ended; `onEnd` also says whether anything `changed` and whether it was `cancelled`. |
|
|
570
|
+
|
|
571
|
+
The list it returns has `toArray()` (the ids in order), `sort(ids)`, `update(options)` and `destroy()`. Style the item
|
|
572
|
+
being dragged with `[data-sortable-dragging]` and the copy with `[data-sortable-ghost]`. `moveItem(array, from, to)`,
|
|
573
|
+
`moveBetween(from, to, fromIndex, toIndex)` and `applySortEvent(lists, event)` reorder plain arrays, for state kept outside
|
|
574
|
+
React. Both entries import on the server, where `createSortable` returns a list that does nothing.
|
|
575
|
+
|
|
424
576
|
## Items and lists
|
|
425
577
|
|
|
426
578
|
| Item field | |
|
|
@@ -529,7 +681,9 @@ explains the keys.
|
|
|
529
681
|
`UISortableBeforeChangeDetail`, `UISortableDropInfo`, `UISortableLabels`, `UISortableNestTrigger` and
|
|
530
682
|
`UISortableDropzoneStyle`, among others. `@editora/ui-sortable/react` exports `Sortable`, `SortableList`, `SortableItem`,
|
|
531
683
|
`SortableChangeDetail`, `SortableBeforeChangeDetail`, `SortableDropInfo`, `SortableLabels`, `SortableNestTrigger` and
|
|
532
|
-
`SortableDropzoneStyle`.
|
|
684
|
+
`SortableDropzoneStyle`. `@editora/ui-sortable/headless` exports `Sortable`, `SortableOptions`, `SortableEvent`,
|
|
685
|
+
`SortableEndEvent`, `SortableMove` and `SortableAnnouncements`, and `@editora/ui-sortable/headless/react` the same plus
|
|
686
|
+
`UseSortableOptions`. The typings are built from the source.
|
|
533
687
|
|
|
534
688
|
## Server rendering
|
|
535
689
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const I=require("react"),s=require("./headless.cjs.js");function M(t){if(t&&t.__esModule)return t;const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const n in t)if(n!=="default"){const c=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(r,n,c.get?c:{enumerable:!0,get:()=>t[n]})}}return r.default=t,Object.freeze(r)}const o=M(I),a=new WeakMap;function O(t){const r=o.useRef(t);r.current=t;const n=o.useRef(null),[c,m]=o.useState(null),f=()=>{const{items:l,onChange:w,onStart:S,onEnd:b,canMove:g,..._}=r.current;return{..._,revert:!0,onStart:S&&(e=>r.current.onStart?.(e)),onEnd:b&&(e=>r.current.onEnd?.(e)),canMove:g&&(e=>r.current.canMove?.(e)!==!1),onSort:e=>{const{items:u,onChange:i}=r.current;if(e.type==="update")i(s.moveItem(u,e.oldIndex,e.newIndex),e);else if(e.type==="remove")a.set(e.item,u[e.oldIndex]),i(u.filter((d,y)=>y!==e.oldIndex),e);else{if(!a.has(e.item))return;const d=u.slice();d.splice(e.newIndex,0,a.get(e.item)),a.delete(e.item),i(d,e)}}}},p=o.useCallback(l=>{n.current?.destroy(),n.current=l?s.createSortable(l,f()):null,m(n.current)},[]);return o.useEffect(()=>{n.current?.update(f())}),{ref:p,sortable:c}}exports.applySortEvent=s.applySortEvent;exports.moveBetween=s.moveBetween;exports.moveItem=s.moveItem;exports.useSortable=O;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as r from "react";
|
|
2
|
+
import { createSortable as g, moveItem as E } from "./headless.esm.js";
|
|
3
|
+
import { applySortEvent as R, moveBetween as _ } from "./headless.esm.js";
|
|
4
|
+
const s = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
function y(l) {
|
|
6
|
+
const t = r.useRef(l);
|
|
7
|
+
t.current = l;
|
|
8
|
+
const n = r.useRef(null), [d, m] = r.useState(null), i = () => {
|
|
9
|
+
const { items: c, onChange: h, onStart: p, onEnd: S, canMove: x, ...I } = t.current;
|
|
10
|
+
return {
|
|
11
|
+
...I,
|
|
12
|
+
revert: !0,
|
|
13
|
+
onStart: p && ((e) => t.current.onStart?.(e)),
|
|
14
|
+
onEnd: S && ((e) => t.current.onEnd?.(e)),
|
|
15
|
+
canMove: x && ((e) => t.current.canMove?.(e) !== !1),
|
|
16
|
+
onSort: (e) => {
|
|
17
|
+
const { items: o, onChange: a } = t.current;
|
|
18
|
+
if (e.type === "update")
|
|
19
|
+
a(E(o, e.oldIndex, e.newIndex), e);
|
|
20
|
+
else if (e.type === "remove")
|
|
21
|
+
s.set(e.item, o[e.oldIndex]), a(o.filter((u, b) => b !== e.oldIndex), e);
|
|
22
|
+
else {
|
|
23
|
+
if (!s.has(e.item)) return;
|
|
24
|
+
const u = o.slice();
|
|
25
|
+
u.splice(e.newIndex, 0, s.get(e.item)), s.delete(e.item), a(u, e);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}, f = r.useCallback((c) => {
|
|
30
|
+
n.current?.destroy(), n.current = c ? g(c, i()) : null, m(n.current);
|
|
31
|
+
}, []);
|
|
32
|
+
return r.useEffect(() => {
|
|
33
|
+
n.current?.update(i());
|
|
34
|
+
}), { ref: f, sortable: d };
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
R as applySortEvent,
|
|
38
|
+
_ as moveBetween,
|
|
39
|
+
E as moveItem,
|
|
40
|
+
y as useSortable
|
|
41
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const D={instructions:"Press Space to pick up. While it is picked up, use the arrow keys to move it, Space to drop it, Escape to cancel.",lifted:({itemLabel:i,position:t,total:e})=>`Picked up ${i}. Position ${t} of ${e}.`,moved:({itemLabel:i,position:t,total:e})=>`${i}: position ${t} of ${e}.`,movedToList:({itemLabel:i,listLabel:t,position:e,total:n})=>`${i}: ${t}, position ${e} of ${n}.`,dropped:({itemLabel:i,listLabel:t,position:e,total:n})=>`Dropped ${i}${t?` in ${t}`:""}, position ${e} of ${n}.`,cancelled:({itemLabel:i})=>`Cancelled. ${i} is back where it was.`},w=new Map;let y=null;const T=new Map;function M(i){if(typeof document>"u"||!i)return;(!y||!y.isConnected)&&(y=document.createElement("div"),y.setAttribute("aria-live","assertive"),y.setAttribute("aria-atomic","true"),y.setAttribute("data-sortable-announcer",""),$(y),document.body.appendChild(y)),y.textContent="";const t=y;setTimeout(()=>{t.textContent=i},20)}function F(i){let t=T.get(i);return(!t||!t.isConnected)&&(t=document.createElement("div"),t.id=`sortable-instructions-${T.size+1}`,t.textContent=i,$(t),document.body.appendChild(t),T.set(i,t)),t.id}function $(i){Object.assign(i.style,{position:"absolute",width:"1px",height:"1px",margin:"-1px",padding:"0",overflow:"hidden",clip:"rect(0 0 0 0)",whiteSpace:"nowrap",border:"0"})}const k=()=>typeof matchMedia=="function"&&matchMedia("(prefers-reduced-motion: reduce)").matches,S='input, textarea, select, option, [contenteditable=""], [contenteditable="true"], [data-sortable-ignore]',z='a[href], button, input, select, textarea, [tabindex], [contenteditable=""], [contenteditable="true"]';function L(i,t,e){const n=i.slice();if(t<0||t>=n.length)return n;const[o]=n.splice(t,1);return n.splice(Math.max(0,Math.min(e,n.length)),0,o),n}function P(i,t,e,n){const o=i.slice(),l=t.slice(),[c]=o.splice(e,1);return c!==void 0&&l.splice(Math.max(0,Math.min(n,l.length)),0,c),[o,l]}function H(i,t){const e={};for(const l of Object.keys(i))e[l]=i[l].slice();if(t.type==="remove")return e;const n=t.fromId??"",o=t.toId??"";return t.type==="update"?e[n]=L(i[n]||[],t.oldIndex,t.newIndex):[e[n],e[o]]=P(i[n]||[],i[o]||[],t.oldIndex,t.newIndex),e}let s=null;class W{constructor(t,e){this.observer=null,this.added=new WeakMap,this.pending=null,this.listeners=[],this.onPointerDown=n=>{if(this.options.disabled||s||this.pending||n.pointerType==="mouse"&&n.button!==0||n.__sortable)return;const o=n.target,l=this.itemOf(o);if(l){if(n.__sortable=!0,this.options.handle){const c=o.closest(this.options.handle);if(!c||!l.contains(c))return}else if(o.closest(S))return;this.pending={pointerId:n.pointerId,item:l,target:o,x:n.clientX,y:n.clientY,type:n.pointerType},n.pointerType==="touch"&&(this.pending.timer=window.setTimeout(()=>{this.pending&&this.begin(this.pending.item,"pointer",this.pending.x,this.pending.y)},this.options.touchDelay??200)),this.listen(window,"pointermove",this.onPointerMove,{passive:!1}),this.listen(window,"pointerup",this.onPointerUp),this.listen(window,"pointercancel",this.onPointerCancel),this.listen(window,"keydown",this.onDragKeyDown)}},this.onDragKeyDown=n=>{n.key!=="Escape"||!s||s.source!=="pointer"||s.from!==this||(n.preventDefault(),this.cancelPending(),x(!0))},this.onTouchMove=n=>{s&&s.source==="pointer"&&n.cancelable&&n.preventDefault()},this.onPointerMove=n=>{const o=this.pending;if(!(o&&n.pointerId!==o.pointerId)){if(!s&&o){const l=Math.hypot(n.clientX-o.x,n.clientY-o.y);if(o.type==="touch"){l>8&&this.cancelPending();return}if(l<(this.options.threshold??4))return;this.begin(o.item,"pointer",o.x,o.y)}s&&s.source==="pointer"&&s.from===this&&(n.preventDefault(),R(n.clientX,n.clientY))}},this.onPointerUp=()=>{const n=!!(s&&s.source==="pointer"&&s.from===this);this.cancelPending(),n&&(x(!1),Y())},this.onPointerCancel=()=>{const n=!!(s&&s.source==="pointer"&&s.from===this);this.cancelPending(),n&&x(!0)},this.onKeyDown=n=>{if(this.options.keyboard===!1||this.options.disabled)return;const o=n.target,l=this.itemOf(o);if(!l||o!==this.focusTargetOf(l)&&o.closest(S))return;if(!(s&&s.source==="keyboard"&&s.item===l)){(n.key===" "||n.key==="Enter")&&o===this.focusTargetOf(l)&&!s&&(n.preventDefault(),this.begin(l,"keyboard"));return}const r=n.key;r===" "||r==="Enter"?(n.preventDefault(),x(!1)):r==="Escape"?(n.preventDefault(),x(!0)):r==="Tab"?x(!1):["ArrowUp","ArrowDown","ArrowLeft","ArrowRight","Home","End"].includes(r)&&(n.preventDefault(),q(r))},this.element=t,this.options=e,this.listen(t,"pointerdown",this.onPointerDown),this.listen(t,"keydown",this.onKeyDown),this.listen(t,"touchmove",this.onTouchMove,{passive:!1}),this.listen(t,"dragstart",(n=>{this.itemOf(n.target)&&n.preventDefault()})),this.listen(t,"focusout",(n=>{if(!s||s.source!=="keyboard"||s.from!==this)return;const o=n.relatedTarget;o&&s.item.contains(o)||setTimeout(()=>{s&&s.source==="keyboard"&&!s.item.contains(document.activeElement)&&x(!1)},0)})),this.prepareItems(),typeof MutationObserver<"u"&&(this.observer=new MutationObserver(()=>this.prepareItems()),this.observer.observe(t,{childList:!0}))}items(){const t=this.options.itemSelector;return Array.from(this.element.children).filter(e=>e instanceof HTMLElement&&!e.hasAttribute("data-sortable-ghost")&&(!t||e.matches(t)))}itemOf(t){let e=t;for(;e&&e!==this.element;){if(e.parentNode===this.element&&e instanceof HTMLElement)return this.items().includes(e)?e:null;e=e.parentNode}return null}idOf(t){return this.options.getId?this.options.getId(t):t.getAttribute("data-id")||t.id||null}listId(){return this.options.id??(this.element.getAttribute("data-sortable-id")||this.element.id||null)}listLabel(){const t=this.element.getAttribute("aria-labelledby"),e=t?document.getElementById(t.split(" ")[0]):null;return this.element.getAttribute("aria-label")||e?.textContent?.trim()||this.listId()||""}announcements(){return{...D,...this.options.announcements}}prepareItems(){if(this.options.keyboard===!1)return;const t=F(this.announcements().instructions);for(const e of this.items()){const n=this.focusTargetOf(e),o=this.added.get(n)||[];n.matches(z)||(n.setAttribute("tabindex","0"),o.includes("tabindex")||o.push("tabindex")),n.hasAttribute("aria-describedby")||(n.setAttribute("aria-describedby",t),o.push("aria-describedby")),o.length&&this.added.set(n,o)}}focusTargetOf(t){return(this.options.handle?t.querySelector(this.options.handle):null)||t}context(t){const e=this.items(),n=t.getAttribute("aria-label")||(t.textContent||"").trim().replace(/\s+/g," ").slice(0,80);return{item:t,itemLabel:n||"Item",listLabel:this.listLabel(),position:e.indexOf(t)+1,total:e.length}}peers(){const t=this.options.group;return t?Array.from(w.values()).filter(e=>e===this||e.options.group===t&&!e.options.disabled):[this]}cancelPending(){this.pending?.timer&&clearTimeout(this.pending.timer),this.pending=null,(!s||s.from!==this)&&this.unlistenWindow()}unlistenWindow(){this.listeners=this.listeners.filter(([t,e,n,o])=>t!==window?!0:(t.removeEventListener(e,n,o),!1))}listen(t,e,n,o){t.addEventListener(e,n,o),this.listeners.push([t,e,n,o])}destroy(){s&&s.from===this&&x(!0),this.observer?.disconnect(),this.listeners.forEach(([t,e,n,o])=>t.removeEventListener(e,n,o)),this.listeners=[];for(const t of this.items())for(const e of[t,...Array.from(t.querySelectorAll("[tabindex], [aria-describedby]"))])this.added.get(e)?.forEach(n=>e.removeAttribute(n)),this.added.delete(e);w.get(this.element)===this&&w.delete(this.element)}begin(t,e,n=0,o=0){if(s)return;const l=this.items().indexOf(t);if(s={item:t,source:e,from:this,oldIndex:l,home:{container:this.element,next:t.nextSibling},focusTarget:e==="keyboard"?document.activeElement:null},t.setAttribute("data-sortable-dragging",e),document.documentElement.setAttribute("data-sortable-active",""),e==="pointer"){const c=t.getBoundingClientRect(),r=t.cloneNode(!0);r.removeAttribute("id"),r.querySelectorAll("[id]").forEach(a=>a.removeAttribute("id")),r.setAttribute("data-sortable-ghost",""),r.removeAttribute("data-sortable-dragging"),r.setAttribute("aria-hidden","true"),Object.assign(r.style,{position:"fixed",left:`${c.left}px`,top:`${c.top}px`,width:`${c.width}px`,height:`${c.height}px`,margin:"0",boxSizing:"border-box",pointerEvents:"none",zIndex:"2147483647",transition:"none",cursor:"grabbing",willChange:"transform"}),(this.options.ghostParent||this.element).appendChild(r);const f=r.getBoundingClientRect(),d={x:c.left-f.left,y:c.top-f.top};(d.x||d.y)&&(r.style.left=`${c.left+d.x}px`,r.style.top=`${c.top+d.y}px`),this.options.ghostClass?r.classList.add(...this.options.ghostClass.split(/\s+/).filter(Boolean)):Object.assign(r.style,{opacity:"0.95",boxShadow:"0 12px 28px rgba(15, 23, 42, 0.22), 0 2px 6px rgba(15, 23, 42, 0.12)",transform:"scale(1.02)"}),s.ghost=r,s.offset={x:n-c.left-d.x,y:o-c.top-d.y},s.pointer={x:n,y:o},this.options.placeholderClass?(s.placeholderClass=this.options.placeholderClass.split(/\s+/).filter(Boolean),t.classList.add(...s.placeholderClass)):(s.placeholderStyle={opacity:t.style.opacity},t.style.opacity="0.35"),document.documentElement.style.setProperty("user-select","none"),document.documentElement.style.setProperty("-webkit-user-select","none"),window.getSelection?.()?.removeAllRanges(),this.pending?.timer&&clearTimeout(this.pending.timer),this.pending=null}else t.setAttribute("data-sortable-lifted","");this.options.onStart?.({item:t,from:this.element,oldIndex:l,source:e}),e==="keyboard"&&M(this.announcements().lifted(this.context(t)))}}function N(i,t){if(!s)return null;const e=s.from.peers();let o=document.elementFromPoint(i,t);for(;o;){const r=w.get(o);if(r&&e.includes(r))return r;o=o.parentElement}let l=null,c=U;for(const r of e){const f=r.element.getBoundingClientRect(),d=Math.max(f.left-i,0,i-f.right),a=Math.max(f.top-t,0,t-f.bottom),u=Math.hypot(d,a);u<=c&&(c=u,l=r)}return l}const U=24;function _(i,t,e,n){const o=e.options.canMove||s?.from.options.canMove;return!o||o({item:i,from:t,to:e.element,newIndex:n})!==!1}function C(i,t){if(!s)return!1;const e=s.item,n=e.parentElement,o=i.items().filter(u=>u!==e),l=Math.max(0,Math.min(t,o.length));if((i.element===n?i.items().indexOf(e):-1)===l||!_(e,s.home.container,i,l))return!1;const r=new Set([...i.items(),...w.get(n)?.items()||[]]),f=new Map,d=k()?0:i.options.animation??150;d>0&&r.forEach(u=>f.set(u,u.getBoundingClientRect()));const a=o[l]||null;if(a)i.element.insertBefore(e,a);else{const u=o[o.length-1];i.element.insertBefore(e,u?u.nextSibling:null)}return d>0&&f.forEach((u,p)=>{if(p===e&&s?.source==="pointer")return;const h=p.getBoundingClientRect(),g=u.left-h.left,m=u.top-h.top;(g||m)&&typeof p.animate=="function"&&p.animate([{transform:`translate(${g}px, ${m}px)`},{transform:"none"}],{duration:d,easing:"cubic-bezier(0.2, 0, 0, 1)"})}),!0}function R(i,t){if(!s||!s.ghost||!s.offset)return;s.pointer={x:i,y:t},s.ghost.style.left=`${i-s.offset.x}px`,s.ghost.style.top=`${t-s.offset.y}px`,j();const e=N(i,t);if(!e)return;const n=e.items(),o=document.elementFromPoint(i,t),l=o?e.itemOf(o):null;if(l&&l!==s.item){if(s.lastTarget===l)return;const r=n.indexOf(l);let f;if(e.element===s.item.parentElement)f=r;else{const d=l.getBoundingClientRect(),u=B(e)==="horizontal"?i>d.left+d.width/2:t>d.top+d.height/2;f=r+(u?1:0)}C(e,f)&&(s.lastTarget=l);return}if(l===s.item)return;s.lastTarget=null;const c=n.filter(r=>r!==s.item);e.element!==s.item.parentElement?(c.length===0||O(e,c,i,t))&&C(e,c.length):c.length&&O(e,c,i,t)&&C(e,c.length)}function O(i,t,e,n){const o=t[t.length-1].getBoundingClientRect();return B(i)==="horizontal"?e>o.right:n>o.bottom}function B(i){if(i.options.direction)return i.options.direction;const t=i.items();if(t.length<2)return"vertical";const e=t[0].getBoundingClientRect(),n=t[1].getBoundingClientRect();return!e.width&&!e.height?"vertical":Math.abs(e.top-n.top)<Math.max(4,e.height/2)?t.some(l=>l.getBoundingClientRect().top>e.bottom-1)?"grid":"horizontal":"vertical"}function j(){if(!s||s.scrollFrame||!s.pointer)return;const i=()=>{if(!s||!s.pointer)return;s.scrollFrame=0;const{x:t,y:e}=s.pointer,n=48,o=r=>Math.ceil((n-r)/n*18),l=document.elementFromPoint(t,e);let c=!1;for(let r=l;r&&!(r===document.body||r===document.documentElement);r=r.parentElement){const f=getComputedStyle(r),d=/(auto|scroll)/.test(f.overflowY)&&r.scrollHeight>r.clientHeight,a=/(auto|scroll)/.test(f.overflowX)&&r.scrollWidth>r.clientWidth;if(!a&&!d)continue;const u=r.getBoundingClientRect();if(d&&e-u.top<n&&r.scrollTop>0?(r.scrollTop-=o(e-u.top),c=!0):d&&u.bottom-e<n&&r.scrollTop<r.scrollHeight-r.clientHeight&&(r.scrollTop+=o(u.bottom-e),c=!0),a&&t-u.left<n&&r.scrollLeft>0?(r.scrollLeft-=o(t-u.left),c=!0):a&&u.right-t<n&&r.scrollLeft<r.scrollWidth-r.clientWidth&&(r.scrollLeft+=o(u.right-t),c=!0),c)break}if(!c){const r=document.scrollingElement||document.documentElement;e<n&&r.scrollTop>0?(window.scrollBy(0,-o(e)),c=!0):innerHeight-e<n&&r.scrollTop<r.scrollHeight-innerHeight&&(window.scrollBy(0,o(innerHeight-e)),c=!0)}c&&(s.lastTarget=null,R(t,e),s.scrollFrame=requestAnimationFrame(i))};s.scrollFrame=requestAnimationFrame(i)}function q(i){if(!s)return;const t=s.item,e=w.get(t.parentElement);if(!e)return;const n=e.items(),o=n.indexOf(t),l=B(e),c=l==="horizontal"?"ArrowLeft":"ArrowUp",r=l==="horizontal"?"ArrowRight":"ArrowDown",f=l==="horizontal"?["ArrowUp","ArrowDown"]:["ArrowLeft","ArrowRight"];let d=e,a=o;if(i==="Home")a=0;else if(i==="End")a=n.length-1;else if(l==="grid"){const g=X(n);if(i==="ArrowLeft"?a=o-1:i==="ArrowRight"?a=o+1:i==="ArrowUp"?a=o-g:a=o+g,a<0||a>=n.length)return}else if(i===c){if(o===0)return;a=o-1}else if(i===r){if(o===n.length-1)return;a=o+1}else if(f.includes(i)){const g=s.from.peers().sort((v,I)=>{const A=v.element.getBoundingClientRect(),E=I.element.getBoundingClientRect();return l==="horizontal"?A.top-E.top||A.left-E.left:A.left-E.left||A.top-E.top}),m=g.indexOf(e),b=i===f[1];if(d=g[m+(b?1:-1)],!d)return;a=Math.min(o,d.items().length)}else return;const u=document.activeElement,p=C(d,a);if(u&&u!==document.activeElement&&u.focus({preventScroll:!0}),t.scrollIntoView?.({block:"nearest",inline:"nearest"}),!p)return;const h=d.context(t);M(d===e?d.announcements().moved(h):d.announcements().movedToList(h))}function X(i){if(!i.length)return 1;const t=i[0].getBoundingClientRect().top,e=i.filter(n=>Math.abs(n.getBoundingClientRect().top-t)<2).length;return Math.max(1,e)}function Y(){const i=t=>{t.stopPropagation(),t.preventDefault()};window.addEventListener("click",i,{capture:!0,once:!0}),setTimeout(()=>window.removeEventListener("click",i,{capture:!0}),50)}function x(i){if(!s)return;const t=s;s=null;const{item:e,from:n,source:o,oldIndex:l,home:c}=t;t.scrollFrame&&cancelAnimationFrame(t.scrollFrame),n.unlistenWindow();const r=w.get(e.parentElement)||n;let f=r.items().indexOf(e);const d=r!==n||f!==l,a=()=>c.container.insertBefore(e,c.next&&c.next.parentNode===c.container?c.next:null);i&&d&&a();const u=!i&&d,p=i?n:r;i&&(f=l);const h=t.ghost;if(e.removeAttribute("data-sortable-dragging"),e.removeAttribute("data-sortable-lifted"),document.documentElement.removeAttribute("data-sortable-active"),document.documentElement.style.removeProperty("user-select"),document.documentElement.style.removeProperty("-webkit-user-select"),t.placeholderStyle&&(e.style.opacity=t.placeholderStyle.opacity),t.placeholderClass&&e.classList.remove(...t.placeholderClass),h){const m=e.getBoundingClientRect(),b=k()?0:Math.min(n.options.animation??150,200);if(b&&typeof h.animate=="function"){const v=parseFloat(h.style.left),I=parseFloat(h.style.top);h.animate([{transform:h.style.transform||"none"},{transform:`translate(${m.left-v}px, ${m.top-I}px)`}],{duration:b,easing:"cubic-bezier(0.2, 0, 0, 1)",fill:"forwards"}).onfinish=()=>h.remove(),setTimeout(()=>h.remove(),b+100)}else h.remove()}u&&(n.options.revert||p.options.revert)&&a();const g=n.idOf(e);if(u){const m={item:e,itemId:g,from:n.element,to:p.element,fromId:n.listId(),toId:p.listId(),oldIndex:l,newIndex:f,source:o};p===n?n.options.onSort?.({...m,type:"update"}):(n.options.onSort?.({...m,type:"remove"}),p.options.onSort?.({...m,type:"add"}))}if(n.options.onEnd?.({item:e,itemId:g,from:n.element,to:p.element,oldIndex:l,newIndex:f,changed:u,cancelled:i,source:o}),o==="keyboard"){const m=p.context(e);M(i?n.announcements().cancelled(m):p.announcements().dropped(m));const b=t.focusTarget;if(b){b.focus({preventScroll:!0});const v=()=>{b.isConnected&&document.activeElement!==b&&(document.activeElement===document.body||!document.activeElement)&&b.focus({preventScroll:!0})};setTimeout(v,0),setTimeout(v,50)}}}function K(i,t={}){if(typeof document>"u")return{element:i,toArray:()=>[],sort:()=>{},update:()=>{},destroy:()=>{}};w.get(i)?.destroy();const e=new W(i,t);return w.set(i,e),{element:i,toArray:()=>e.items().map(o=>e.idOf(o)).filter(o=>!!o),sort(o){const l=e.items(),c=new Map(l.map(a=>[e.idOf(a),a])),r=o.map(a=>c.get(a)).filter(a=>!!a),f=l.filter(a=>!r.includes(a)),d=l[l.length-1]?.nextSibling||null;[...r,...f].forEach(a=>i.insertBefore(a,d))},update(o){e.options={...e.options,...o}},destroy(){e.destroy()}}}exports.applySortEvent=H;exports.createSortable=K;exports.moveBetween=P;exports.moveItem=L;
|