tyrell-components 1.0.0-TC8 → 1.0.0-rc.21
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/AGENTS.md +122 -0
- package/CHANGELOG.md +1531 -0
- package/README.md +71 -53
- package/css/tyrell-colors-static.css +1 -0
- package/css/tyrell-theme.css +1 -0
- package/css/tyrell.css +1 -1783
- package/dist/tyrell-colors-static.css +1 -0
- package/dist/tyrell-theme.css +1 -0
- package/dist/tyrell.css +1 -1783
- package/dist/tyrell.js +1 -1
- package/guides/AGENT_INSTRUCTIONS.md +124 -0
- package/guides/AI_GUIDE.md +53 -0
- package/guides/CSS_GUIDE.md +1016 -0
- package/guides/DATASTAR_TY_GUIDE.md +657 -0
- package/guides/ICONS_GUIDE.md +212 -0
- package/guides/TY_GUIDE.md +935 -0
- package/guides/clj/CLOJURESCRIPT_GUIDE.md +300 -0
- package/guides/clj/CODE_SPLITTING.md +279 -0
- package/guides/clj/COMPONENT_GUIDE.md +391 -0
- package/guides/clj/HELIX_TY_GUIDE.md +428 -0
- package/guides/clj/I18N_GUIDE.md +478 -0
- package/guides/clj/LAYOUT_GUIDE.md +273 -0
- package/guides/clj/QUICKSTART.md +266 -0
- package/guides/clj/REAGENT_TY_GUIDE.md +408 -0
- package/guides/clj/REPLICANT_TY_GUIDE.md +623 -0
- package/guides/clj/ROUTING_GUIDE.md +346 -0
- package/guides/clj/UIX_TY_GUIDE.md +319 -0
- package/guides/js/HTMX_TY_GUIDE.md +252 -0
- package/guides/js/JAVASCRIPT_GUIDE.md +388 -0
- package/guides/js/REACT_TY_GUIDE.md +983 -0
- package/guides/js/SVELTE_TY_GUIDE.md +682 -0
- package/guides/js/TYCOMPONENT_GUIDE.md +962 -0
- package/guides/js/VUE_TY_GUIDE.md +738 -0
- package/lib/base/ty-component.d.ts +57 -13
- package/lib/base/ty-component.js +266 -292
- package/lib/components/button.d.ts +42 -18
- package/lib/components/button.js +318 -235
- package/lib/components/calendar-month.d.ts +30 -47
- package/lib/components/calendar-month.js +455 -429
- package/lib/components/calendar-navigation.d.ts +41 -49
- package/lib/components/calendar-navigation.js +287 -346
- package/lib/components/calendar.d.ts +73 -83
- package/lib/components/calendar.js +837 -762
- package/lib/components/checkbox.d.ts +26 -75
- package/lib/components/checkbox.js +298 -391
- package/lib/components/copy.d.ts +20 -60
- package/lib/components/copy.js +276 -380
- package/lib/components/date-picker.d.ts +100 -183
- package/lib/components/date-picker.js +1155 -1570
- package/lib/components/file-upload.d.ts +122 -0
- package/lib/components/file-upload.js +451 -0
- package/lib/components/icon.d.ts +3 -24
- package/lib/components/icon.js +141 -230
- package/lib/components/input.d.ts +93 -108
- package/lib/components/input.js +734 -710
- package/lib/components/modal.d.ts +39 -29
- package/lib/components/modal.js +419 -438
- package/lib/components/option.d.ts +20 -35
- package/lib/components/option.js +184 -309
- package/lib/components/popup.d.ts +2 -17
- package/lib/components/popup.js +353 -347
- package/lib/components/radio.d.ts +33 -25
- package/lib/components/radio.js +433 -422
- package/lib/components/resize-observer.d.ts +3 -30
- package/lib/components/resize-observer.js +71 -103
- package/lib/components/scroll-container.d.ts +21 -14
- package/lib/components/scroll-container.js +292 -234
- package/lib/components/select.d.ts +446 -0
- package/lib/components/select.js +1381 -0
- package/lib/components/selected-tags.d.ts +60 -0
- package/lib/components/selected-tags.js +225 -0
- package/lib/components/sortable.d.ts +175 -0
- package/lib/components/sortable.js +1844 -0
- package/lib/components/step.d.ts +1 -6
- package/lib/components/step.js +62 -68
- package/lib/components/switch.d.ts +15 -15
- package/lib/components/switch.js +237 -231
- package/lib/components/tab.d.ts +50 -7
- package/lib/components/tab.js +203 -69
- package/lib/components/tabs.d.ts +19 -64
- package/lib/components/tabs.js +600 -618
- package/lib/components/tag.d.ts +17 -55
- package/lib/components/tag.js +235 -308
- package/lib/components/textarea.d.ts +58 -89
- package/lib/components/textarea.js +523 -573
- package/lib/components/tooltip.d.ts +8 -13
- package/lib/components/tooltip.js +322 -409
- package/lib/components/wizard.d.ts +7 -54
- package/lib/components/wizard.js +376 -606
- package/lib/icons/fontawesome/brands.d.ts +0 -1
- package/lib/icons/fontawesome/brands.js +0 -8
- package/lib/icons/fontawesome/regular.d.ts +0 -1
- package/lib/icons/fontawesome/regular.js +0 -8
- package/lib/icons/fontawesome/solid.d.ts +0 -1
- package/lib/icons/fontawesome/solid.js +0 -8
- package/lib/icons/heroicons/micro.d.ts +0 -1
- package/lib/icons/heroicons/micro.js +392 -400
- package/lib/icons/heroicons/mini.d.ts +0 -1
- package/lib/icons/heroicons/mini.js +382 -390
- package/lib/icons/heroicons/outline.d.ts +0 -1
- package/lib/icons/heroicons/outline.js +337 -345
- package/lib/icons/heroicons/solid.d.ts +0 -1
- package/lib/icons/heroicons/solid.js +407 -415
- package/lib/icons/lucide.d.ts +0 -1
- package/lib/icons/lucide.js +24479 -26351
- package/lib/icons/material/filled.d.ts +0 -1
- package/lib/icons/material/filled.js +9653 -9663
- package/lib/icons/material/outlined.d.ts +0 -1
- package/lib/icons/material/outlined.js +10271 -10281
- package/lib/icons/material/round.d.ts +0 -1
- package/lib/icons/material/round.js +10495 -10505
- package/lib/icons/material/sharp.d.ts +0 -1
- package/lib/icons/material/sharp.js +9905 -9915
- package/lib/icons/material/two-tone.d.ts +0 -1
- package/lib/icons/material/two-tone.js +12792 -12802
- package/lib/index.d.ts +15 -4
- package/lib/index.js +28 -38
- package/lib/styles/button.d.ts +24 -7
- package/lib/styles/button.js +298 -417
- package/lib/styles/calendar-month.d.ts +7 -6
- package/lib/styles/calendar-month.js +157 -179
- package/lib/styles/calendar-navigation.d.ts +2 -6
- package/lib/styles/calendar-navigation.js +108 -109
- package/lib/styles/calendar.d.ts +3 -3
- package/lib/styles/calendar.js +87 -23
- package/lib/styles/checkbox.d.ts +7 -6
- package/lib/styles/checkbox.js +101 -15
- package/lib/styles/copy.d.ts +3 -2
- package/lib/styles/copy.js +91 -71
- package/lib/styles/custom-scrollbar.d.ts +1 -2
- package/lib/styles/custom-scrollbar.js +80 -115
- package/lib/styles/date-picker.d.ts +4 -6
- package/lib/styles/date-picker.js +320 -352
- package/lib/styles/field-size.d.ts +6 -0
- package/lib/styles/field-size.js +91 -0
- package/lib/styles/file-upload.d.ts +1 -0
- package/lib/styles/file-upload.js +196 -0
- package/lib/styles/icon.d.ts +2 -6
- package/lib/styles/icon.js +92 -170
- package/lib/styles/input.d.ts +4 -7
- package/lib/styles/input.js +148 -643
- package/lib/styles/modal.d.ts +1 -2
- package/lib/styles/modal.js +127 -108
- package/lib/styles/option.d.ts +2 -6
- package/lib/styles/option.js +53 -96
- package/lib/styles/popup.d.ts +1 -2
- package/lib/styles/popup.js +47 -77
- package/lib/styles/radio.d.ts +3 -2
- package/lib/styles/radio.js +90 -116
- package/lib/styles/resize-observer.d.ts +1 -2
- package/lib/styles/resize-observer.js +2 -12
- package/lib/styles/scroll-container.d.ts +1 -2
- package/lib/styles/scroll-container.js +103 -146
- package/lib/styles/select-base.d.ts +8 -0
- package/lib/styles/select-base.js +747 -0
- package/lib/styles/select.d.ts +11 -0
- package/lib/styles/select.js +229 -0
- package/lib/styles/sortable.d.ts +16 -0
- package/lib/styles/sortable.js +85 -0
- package/lib/styles/step.d.ts +1 -2
- package/lib/styles/step.js +15 -34
- package/lib/styles/switch.d.ts +3 -2
- package/lib/styles/switch.js +52 -68
- package/lib/styles/tab.d.ts +1 -2
- package/lib/styles/tab.js +86 -34
- package/lib/styles/tabs.d.ts +3 -11
- package/lib/styles/tabs.js +204 -162
- package/lib/styles/tag.d.ts +2 -6
- package/lib/styles/tag.js +170 -359
- package/lib/styles/textarea.d.ts +1 -2
- package/lib/styles/textarea.js +238 -306
- package/lib/styles/tooltip.d.ts +4 -3
- package/lib/styles/tooltip.js +3 -131
- package/lib/styles/wizard.d.ts +8 -20
- package/lib/styles/wizard.js +306 -295
- package/lib/types/common.d.ts +9 -8
- package/lib/types/common.js +8 -5
- package/lib/utils/calendar-utils.d.ts +49 -65
- package/lib/utils/calendar-utils.js +158 -343
- package/lib/utils/custom-scrollbar.d.ts +6 -20
- package/lib/utils/custom-scrollbar.js +273 -317
- package/lib/utils/flavor-sheet.d.ts +31 -0
- package/lib/utils/flavor-sheet.js +17 -0
- package/lib/utils/icon-registry.d.ts +0 -15
- package/lib/utils/icon-registry.js +138 -279
- package/lib/utils/icons.d.ts +9 -0
- package/lib/utils/icons.js +1 -0
- package/lib/utils/loader-registry.d.ts +34 -0
- package/lib/utils/loader-registry.js +11 -0
- package/lib/utils/locale.d.ts +2 -80
- package/lib/utils/locale.js +30 -205
- package/lib/utils/mobile.d.ts +0 -1
- package/lib/utils/mobile.js +1 -18
- package/lib/utils/number-format.d.ts +6 -24
- package/lib/utils/number-format.js +53 -136
- package/lib/utils/positioning.d.ts +72 -22
- package/lib/utils/positioning.js +224 -298
- package/lib/utils/property-capture.d.ts +5 -27
- package/lib/utils/property-capture.js +36 -144
- package/lib/utils/property-manager.d.ts +6 -6
- package/lib/utils/property-manager.js +127 -196
- package/lib/utils/resize-observer.d.ts +0 -1
- package/lib/utils/resize-observer.js +23 -59
- package/lib/utils/scroll-lock.d.ts +10 -11
- package/lib/utils/scroll-lock.js +165 -173
- package/lib/utils/styles.d.ts +0 -6
- package/lib/utils/styles.js +17 -48
- package/lib/utils/upgrade-properties.d.ts +21 -0
- package/lib/utils/upgrade-properties.js +47 -0
- package/lib/utils/validity.d.ts +122 -0
- package/lib/utils/validity.js +289 -0
- package/lib/version.d.ts +1 -2
- package/lib/version.js +1 -11
- package/package.json +66 -18
- package/lib/base/ty-component.d.ts.map +0 -1
- package/lib/base/ty-component.js.map +0 -1
- package/lib/components/button.d.ts.map +0 -1
- package/lib/components/button.js.map +0 -1
- package/lib/components/calendar-month.d.ts.map +0 -1
- package/lib/components/calendar-month.js.map +0 -1
- package/lib/components/calendar-navigation.d.ts.map +0 -1
- package/lib/components/calendar-navigation.js.map +0 -1
- package/lib/components/calendar.d.ts.map +0 -1
- package/lib/components/calendar.js.map +0 -1
- package/lib/components/checkbox.d.ts.map +0 -1
- package/lib/components/checkbox.js.map +0 -1
- package/lib/components/copy.d.ts.map +0 -1
- package/lib/components/copy.js.map +0 -1
- package/lib/components/date-picker.d.ts.map +0 -1
- package/lib/components/date-picker.js.map +0 -1
- package/lib/components/dropdown.d.ts +0 -402
- package/lib/components/dropdown.d.ts.map +0 -1
- package/lib/components/dropdown.js +0 -1564
- package/lib/components/dropdown.js.map +0 -1
- package/lib/components/icon.d.ts.map +0 -1
- package/lib/components/icon.js.map +0 -1
- package/lib/components/input.d.ts.map +0 -1
- package/lib/components/input.js.map +0 -1
- package/lib/components/modal.d.ts.map +0 -1
- package/lib/components/modal.js.map +0 -1
- package/lib/components/multiselect.d.ts +0 -397
- package/lib/components/multiselect.d.ts.map +0 -1
- package/lib/components/multiselect.js +0 -1580
- package/lib/components/multiselect.js.map +0 -1
- package/lib/components/option.d.ts.map +0 -1
- package/lib/components/option.js.map +0 -1
- package/lib/components/popup.d.ts.map +0 -1
- package/lib/components/popup.js.map +0 -1
- package/lib/components/radio.d.ts.map +0 -1
- package/lib/components/radio.js.map +0 -1
- package/lib/components/resize-observer.d.ts.map +0 -1
- package/lib/components/resize-observer.js.map +0 -1
- package/lib/components/scroll-container.d.ts.map +0 -1
- package/lib/components/scroll-container.js.map +0 -1
- package/lib/components/step.d.ts.map +0 -1
- package/lib/components/step.js.map +0 -1
- package/lib/components/switch.d.ts.map +0 -1
- package/lib/components/switch.js.map +0 -1
- package/lib/components/tab.d.ts.map +0 -1
- package/lib/components/tab.js.map +0 -1
- package/lib/components/tabs.d.ts.map +0 -1
- package/lib/components/tabs.js.map +0 -1
- package/lib/components/tag.d.ts.map +0 -1
- package/lib/components/tag.js.map +0 -1
- package/lib/components/textarea.d.ts.map +0 -1
- package/lib/components/textarea.js.map +0 -1
- package/lib/components/tooltip.d.ts.map +0 -1
- package/lib/components/tooltip.js.map +0 -1
- package/lib/components/wizard.d.ts.map +0 -1
- package/lib/components/wizard.js.map +0 -1
- package/lib/icons/fontawesome/brands.d.ts.map +0 -1
- package/lib/icons/fontawesome/brands.js.map +0 -1
- package/lib/icons/fontawesome/regular.d.ts.map +0 -1
- package/lib/icons/fontawesome/regular.js.map +0 -1
- package/lib/icons/fontawesome/solid.d.ts.map +0 -1
- package/lib/icons/fontawesome/solid.js.map +0 -1
- package/lib/icons/heroicons/micro.d.ts.map +0 -1
- package/lib/icons/heroicons/micro.js.map +0 -1
- package/lib/icons/heroicons/mini.d.ts.map +0 -1
- package/lib/icons/heroicons/mini.js.map +0 -1
- package/lib/icons/heroicons/outline.d.ts.map +0 -1
- package/lib/icons/heroicons/outline.js.map +0 -1
- package/lib/icons/heroicons/solid.d.ts.map +0 -1
- package/lib/icons/heroicons/solid.js.map +0 -1
- package/lib/icons/lucide.d.ts.map +0 -1
- package/lib/icons/lucide.js.map +0 -1
- package/lib/icons/material/filled.d.ts.map +0 -1
- package/lib/icons/material/filled.js.map +0 -1
- package/lib/icons/material/outlined.d.ts.map +0 -1
- package/lib/icons/material/outlined.js.map +0 -1
- package/lib/icons/material/round.d.ts.map +0 -1
- package/lib/icons/material/round.js.map +0 -1
- package/lib/icons/material/sharp.d.ts.map +0 -1
- package/lib/icons/material/sharp.js.map +0 -1
- package/lib/icons/material/two-tone.d.ts.map +0 -1
- package/lib/icons/material/two-tone.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/styles/button.d.ts.map +0 -1
- package/lib/styles/button.js.map +0 -1
- package/lib/styles/calendar-month.d.ts.map +0 -1
- package/lib/styles/calendar-month.js.map +0 -1
- package/lib/styles/calendar-navigation.d.ts.map +0 -1
- package/lib/styles/calendar-navigation.js.map +0 -1
- package/lib/styles/calendar.d.ts.map +0 -1
- package/lib/styles/calendar.js.map +0 -1
- package/lib/styles/checkbox.d.ts.map +0 -1
- package/lib/styles/checkbox.js.map +0 -1
- package/lib/styles/copy.d.ts.map +0 -1
- package/lib/styles/copy.js.map +0 -1
- package/lib/styles/custom-scrollbar.d.ts.map +0 -1
- package/lib/styles/custom-scrollbar.js.map +0 -1
- package/lib/styles/date-picker.d.ts.map +0 -1
- package/lib/styles/date-picker.js.map +0 -1
- package/lib/styles/dropdown.d.ts +0 -12
- package/lib/styles/dropdown.d.ts.map +0 -1
- package/lib/styles/dropdown.js +0 -983
- package/lib/styles/dropdown.js.map +0 -1
- package/lib/styles/icon.d.ts.map +0 -1
- package/lib/styles/icon.js.map +0 -1
- package/lib/styles/input.d.ts.map +0 -1
- package/lib/styles/input.js.map +0 -1
- package/lib/styles/modal.d.ts.map +0 -1
- package/lib/styles/modal.js.map +0 -1
- package/lib/styles/multiselect.d.ts +0 -6
- package/lib/styles/multiselect.d.ts.map +0 -1
- package/lib/styles/multiselect.js +0 -774
- package/lib/styles/multiselect.js.map +0 -1
- package/lib/styles/option.d.ts.map +0 -1
- package/lib/styles/option.js.map +0 -1
- package/lib/styles/popup.d.ts.map +0 -1
- package/lib/styles/popup.js.map +0 -1
- package/lib/styles/radio.d.ts.map +0 -1
- package/lib/styles/radio.js.map +0 -1
- package/lib/styles/resize-observer.d.ts.map +0 -1
- package/lib/styles/resize-observer.js.map +0 -1
- package/lib/styles/scroll-container.d.ts.map +0 -1
- package/lib/styles/scroll-container.js.map +0 -1
- package/lib/styles/step.d.ts.map +0 -1
- package/lib/styles/step.js.map +0 -1
- package/lib/styles/switch.d.ts.map +0 -1
- package/lib/styles/switch.js.map +0 -1
- package/lib/styles/tab.d.ts.map +0 -1
- package/lib/styles/tab.js.map +0 -1
- package/lib/styles/tabs.d.ts.map +0 -1
- package/lib/styles/tabs.js.map +0 -1
- package/lib/styles/tag.d.ts.map +0 -1
- package/lib/styles/tag.js.map +0 -1
- package/lib/styles/textarea.d.ts.map +0 -1
- package/lib/styles/textarea.js.map +0 -1
- package/lib/styles/tooltip.d.ts.map +0 -1
- package/lib/styles/tooltip.js.map +0 -1
- package/lib/styles/wizard.d.ts.map +0 -1
- package/lib/styles/wizard.js.map +0 -1
- package/lib/types/common.d.ts.map +0 -1
- package/lib/types/common.js.map +0 -1
- package/lib/utils/calendar-utils.d.ts.map +0 -1
- package/lib/utils/calendar-utils.js.map +0 -1
- package/lib/utils/custom-scrollbar.d.ts.map +0 -1
- package/lib/utils/custom-scrollbar.js.map +0 -1
- package/lib/utils/icon-registry.d.ts.map +0 -1
- package/lib/utils/icon-registry.js.map +0 -1
- package/lib/utils/locale.d.ts.map +0 -1
- package/lib/utils/locale.js.map +0 -1
- package/lib/utils/mobile.d.ts.map +0 -1
- package/lib/utils/mobile.js.map +0 -1
- package/lib/utils/number-format.d.ts.map +0 -1
- package/lib/utils/number-format.js.map +0 -1
- package/lib/utils/parse-boolean.d.ts +0 -39
- package/lib/utils/parse-boolean.d.ts.map +0 -1
- package/lib/utils/parse-boolean.js +0 -58
- package/lib/utils/parse-boolean.js.map +0 -1
- package/lib/utils/positioning.d.ts.map +0 -1
- package/lib/utils/positioning.js.map +0 -1
- package/lib/utils/property-capture.d.ts.map +0 -1
- package/lib/utils/property-capture.js.map +0 -1
- package/lib/utils/property-manager.d.ts.map +0 -1
- package/lib/utils/property-manager.js.map +0 -1
- package/lib/utils/resize-observer.d.ts.map +0 -1
- package/lib/utils/resize-observer.js.map +0 -1
- package/lib/utils/scroll-lock.d.ts.map +0 -1
- package/lib/utils/scroll-lock.js.map +0 -1
- package/lib/utils/styles.d.ts.map +0 -1
- package/lib/utils/styles.js.map +0 -1
- package/lib/version.d.ts.map +0 -1
- package/lib/version.js.map +0 -1
|
@@ -0,0 +1,1844 @@
|
|
|
1
|
+
import { ensureStyles } from '../utils/styles.js';
|
|
2
|
+
import { upgradeProperties } from '../utils/upgrade-properties.js';
|
|
3
|
+
import { sortableStyles, sortableLightStyles, sortableCursorStyles } from '../styles/sortable.js';
|
|
4
|
+
const DEFAULT_STRINGS = {
|
|
5
|
+
pickedUp: 'Picked up {item}. Position {position} of {total} in {list}. Arrow keys move, Space drops, Escape cancels.',
|
|
6
|
+
moved: '{item}: position {position} of {total} in {list}.',
|
|
7
|
+
blocked: '{list} does not accept {item}.',
|
|
8
|
+
dropped: '{item} moved to position {position} of {total} in {list}.',
|
|
9
|
+
rejected: 'Move not saved. {item} is back at position {position} in {list}.',
|
|
10
|
+
reconciled: '{item} is now at position {position} of {total} in {list}.',
|
|
11
|
+
removed: '{item} was removed.',
|
|
12
|
+
unknown: 'The move of {item} may not have been saved.',
|
|
13
|
+
cancelled: 'Move cancelled. {item} is back at position {position} in {list}.',
|
|
14
|
+
pending: 'Saving, please wait.',
|
|
15
|
+
};
|
|
16
|
+
const TOUCH_DELAY = 250;
|
|
17
|
+
const TOUCH_TOLERANCE = 8;
|
|
18
|
+
const MOUSE_DISTANCE = 5;
|
|
19
|
+
const HANDLE_TOUCH_DISTANCE = 3;
|
|
20
|
+
const FLING_GUARD = 100;
|
|
21
|
+
const HOLD_TIMEOUT = 5000;
|
|
22
|
+
const BUSY_CAP = 30000;
|
|
23
|
+
const IDLE_GRACE = 250;
|
|
24
|
+
const HTMX_SETTLE_CAP = 3000;
|
|
25
|
+
const AUTOSCROLL_SPEED = 20;
|
|
26
|
+
const AUTOSCROLL_ARM = 16;
|
|
27
|
+
const INTERACTIVE = 'button, a[href], input, select, textarea, label, summary, [contenteditable]:not([contenteditable="false"]), [data-no-drag]';
|
|
28
|
+
const instances = new Set();
|
|
29
|
+
let session = null;
|
|
30
|
+
let keyHandledAt = 0;
|
|
31
|
+
let droppedBy = null;
|
|
32
|
+
let lastScrollAt = -Infinity;
|
|
33
|
+
document.addEventListener('scroll', () => { lastScrollAt = performance.now(); }, { capture: true, passive: true });
|
|
34
|
+
const styledRoots = new WeakSet();
|
|
35
|
+
const lightSheet = (() => {
|
|
36
|
+
let sheet = null;
|
|
37
|
+
return () => {
|
|
38
|
+
if (!sheet) {
|
|
39
|
+
sheet = new CSSStyleSheet();
|
|
40
|
+
sheet.replaceSync(sortableLightStyles);
|
|
41
|
+
}
|
|
42
|
+
return sheet;
|
|
43
|
+
};
|
|
44
|
+
})();
|
|
45
|
+
const handleRules = new Set(['[data-handle]']);
|
|
46
|
+
function adoptLightStyles(host) {
|
|
47
|
+
const root = host.getRootNode();
|
|
48
|
+
if (!('adoptedStyleSheets' in root))
|
|
49
|
+
return;
|
|
50
|
+
if (!styledRoots.has(root)) {
|
|
51
|
+
styledRoots.add(root);
|
|
52
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, lightSheet()];
|
|
53
|
+
}
|
|
54
|
+
const sel = host.handleSelector;
|
|
55
|
+
if (handleRules.has(sel))
|
|
56
|
+
return;
|
|
57
|
+
handleRules.add(sel);
|
|
58
|
+
try {
|
|
59
|
+
lightSheet().insertRule(`ty-sortable ${sel} { touch-action: none; cursor: grab; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }`);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function reducedMotion() {
|
|
65
|
+
return matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
66
|
+
}
|
|
67
|
+
function duration(el) {
|
|
68
|
+
if (reducedMotion())
|
|
69
|
+
return 0;
|
|
70
|
+
const v = getComputedStyle(el).getPropertyValue('--ty-sortable-duration').trim();
|
|
71
|
+
const n = parseFloat(v);
|
|
72
|
+
return Number.isFinite(n) ? (v.endsWith('ms') ? n : n * 1000) : 200;
|
|
73
|
+
}
|
|
74
|
+
function cssVar(el, name, fallback) {
|
|
75
|
+
return getComputedStyle(el).getPropertyValue(name).trim() || fallback;
|
|
76
|
+
}
|
|
77
|
+
function setState(el, name, on) {
|
|
78
|
+
const states = el._states;
|
|
79
|
+
if (!states)
|
|
80
|
+
return;
|
|
81
|
+
try {
|
|
82
|
+
if (on)
|
|
83
|
+
states.add(name);
|
|
84
|
+
else
|
|
85
|
+
states.delete(name);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function flatParent(n) {
|
|
91
|
+
return n.assignedSlot ?? n.parentElement ?? (n.getRootNode().host ?? null);
|
|
92
|
+
}
|
|
93
|
+
function openDialogOf(el) {
|
|
94
|
+
for (let n = el; n; n = flatParent(n)) {
|
|
95
|
+
if (n instanceof HTMLDialogElement && n.open)
|
|
96
|
+
return n;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
function deepElementFromPoint(x, y) {
|
|
101
|
+
let el = document.elementFromPoint(x, y);
|
|
102
|
+
const seen = new Set();
|
|
103
|
+
while (el?.shadowRoot && !seen.has(el)) {
|
|
104
|
+
seen.add(el);
|
|
105
|
+
const inner = el.shadowRoot.elementFromPoint(x, y);
|
|
106
|
+
if (!inner || seen.has(inner) || !el.shadowRoot.contains(inner))
|
|
107
|
+
break;
|
|
108
|
+
el = inner;
|
|
109
|
+
}
|
|
110
|
+
return el;
|
|
111
|
+
}
|
|
112
|
+
function members(group, self) {
|
|
113
|
+
if (!group)
|
|
114
|
+
return [self];
|
|
115
|
+
return [...instances].filter((i) => i.isConnected && i.group === group);
|
|
116
|
+
}
|
|
117
|
+
function resolveContainer(c, id, group) {
|
|
118
|
+
if (c.isConnected)
|
|
119
|
+
return c;
|
|
120
|
+
if (!id)
|
|
121
|
+
return null;
|
|
122
|
+
const next = [...instances].find((i) => i.isConnected && i.id === id && i.group === group) ?? null;
|
|
123
|
+
if (next) {
|
|
124
|
+
next.canDrop ?? (next.canDrop = c.canDrop);
|
|
125
|
+
if (!Object.keys(next.labels).length)
|
|
126
|
+
next.labels = c.labels;
|
|
127
|
+
}
|
|
128
|
+
return next;
|
|
129
|
+
}
|
|
130
|
+
function handleOf(c, item) {
|
|
131
|
+
try {
|
|
132
|
+
return item.querySelector(c.handleSelector);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function textOf(node, skip) {
|
|
139
|
+
if (node.nodeType === Node.TEXT_NODE)
|
|
140
|
+
return node.textContent ?? '';
|
|
141
|
+
if (!(node instanceof Element))
|
|
142
|
+
return '';
|
|
143
|
+
if (node === skip || node.matches('[aria-hidden="true"], button, input, select, textarea, style, script, template'))
|
|
144
|
+
return '';
|
|
145
|
+
return Array.from(node.childNodes).map((n) => textOf(n, skip)).join(' ');
|
|
146
|
+
}
|
|
147
|
+
function itemLabel(c, item) {
|
|
148
|
+
const handle = handleOf(c, item);
|
|
149
|
+
const handleName = handle?.getAttribute('aria-label')?.replace(/^move\s+/i, '');
|
|
150
|
+
const label = item.getAttribute('aria-label') || item.getAttribute('data-label') || handleName || textOf(item, handle);
|
|
151
|
+
const text = label.replace(/\s+/g, ' ').trim();
|
|
152
|
+
return text.length > 60 ? `${text.slice(0, 57)}…` : text || 'item';
|
|
153
|
+
}
|
|
154
|
+
function listLabel(c) {
|
|
155
|
+
return c.getAttribute('label') || c.getAttribute('aria-label') || c.id || 'list';
|
|
156
|
+
}
|
|
157
|
+
function sameTokens(a, b) {
|
|
158
|
+
return a.length === b.length && a.every((t, i) => t === b[i]);
|
|
159
|
+
}
|
|
160
|
+
function scrolls(v) {
|
|
161
|
+
return v === 'auto' || v === 'scroll' || v === 'overlay';
|
|
162
|
+
}
|
|
163
|
+
function scrollersOf(start) {
|
|
164
|
+
const out = [];
|
|
165
|
+
for (let el = start; el; el = flatParent(el)) {
|
|
166
|
+
if (el instanceof HTMLDialogElement && el.open)
|
|
167
|
+
return out;
|
|
168
|
+
if (el === document.documentElement || el === document.body)
|
|
169
|
+
break;
|
|
170
|
+
const cs = getComputedStyle(el);
|
|
171
|
+
if ((scrolls(cs.overflowY) && el.scrollHeight > el.clientHeight) || (scrolls(cs.overflowX) && el.scrollWidth > el.clientWidth)) {
|
|
172
|
+
out.push(el);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const page = document.scrollingElement;
|
|
176
|
+
if (page)
|
|
177
|
+
out.push(page);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
function viewportRect() {
|
|
181
|
+
return { top: 0, left: 0, bottom: innerHeight, right: innerWidth, width: innerWidth, height: innerHeight };
|
|
182
|
+
}
|
|
183
|
+
function visibleRect(c) {
|
|
184
|
+
const r = c.getBoundingClientRect();
|
|
185
|
+
let top = r.top, left = r.left, bottom = r.bottom, right = r.right;
|
|
186
|
+
const page = document.scrollingElement;
|
|
187
|
+
for (const s of scrollersOf(c)) {
|
|
188
|
+
const sr = s === page ? viewportRect() : s.getBoundingClientRect();
|
|
189
|
+
top = Math.max(top, sr.top);
|
|
190
|
+
left = Math.max(left, sr.left);
|
|
191
|
+
bottom = Math.min(bottom, sr.bottom);
|
|
192
|
+
right = Math.min(right, sr.right);
|
|
193
|
+
}
|
|
194
|
+
return bottom > top && right > left ? { top, left, bottom, right } : null;
|
|
195
|
+
}
|
|
196
|
+
function autoScroll(scrollers, x, y) {
|
|
197
|
+
const page = document.scrollingElement;
|
|
198
|
+
let moved = false;
|
|
199
|
+
for (const axis of ['y', 'x']) {
|
|
200
|
+
for (const el of scrollers) {
|
|
201
|
+
const r = el === page ? viewportRect() : el.getBoundingClientRect();
|
|
202
|
+
if (el !== page && (x < r.left || x > r.right || y < r.top || y > r.bottom))
|
|
203
|
+
continue;
|
|
204
|
+
const size = axis === 'y' ? r.height : r.width;
|
|
205
|
+
const zone = Math.min(Math.max(40, Math.min(size * 0.15, 100)), size * 0.25);
|
|
206
|
+
const p = axis === 'y' ? y : x;
|
|
207
|
+
const lo = axis === 'y' ? r.top : r.left;
|
|
208
|
+
const hi = axis === 'y' ? r.bottom : r.right;
|
|
209
|
+
const pos = axis === 'y' ? el.scrollTop : el.scrollLeft;
|
|
210
|
+
const max = axis === 'y' ? el.scrollHeight - el.clientHeight : el.scrollWidth - el.clientWidth;
|
|
211
|
+
let delta = 0;
|
|
212
|
+
if (p < lo + zone && pos > 0)
|
|
213
|
+
delta = -AUTOSCROLL_SPEED * ((lo + zone - p) / zone) ** 2;
|
|
214
|
+
else if (p > hi - zone && pos < max - 1)
|
|
215
|
+
delta = AUTOSCROLL_SPEED * ((p - (hi - zone)) / zone) ** 2;
|
|
216
|
+
if (!delta)
|
|
217
|
+
continue;
|
|
218
|
+
if (reducedMotion())
|
|
219
|
+
delta = Math.sign(delta) * AUTOSCROLL_SPEED / 2;
|
|
220
|
+
if (axis === 'y')
|
|
221
|
+
el.scrollTop += delta;
|
|
222
|
+
else
|
|
223
|
+
el.scrollLeft += delta;
|
|
224
|
+
moved = true;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return moved;
|
|
229
|
+
}
|
|
230
|
+
let suppressUntil = 0;
|
|
231
|
+
addEventListener('click', (e) => {
|
|
232
|
+
if (performance.now() < suppressUntil) {
|
|
233
|
+
suppressUntil = 0;
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
e.stopImmediatePropagation();
|
|
236
|
+
}
|
|
237
|
+
}, true);
|
|
238
|
+
addEventListener('pointerdown', () => { suppressUntil = 0; }, true);
|
|
239
|
+
function suppressNextRelease(pointerId) {
|
|
240
|
+
const done = () => {
|
|
241
|
+
removeEventListener('pointerup', onUp, true);
|
|
242
|
+
removeEventListener('pointerdown', done, true);
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
};
|
|
245
|
+
const onUp = (e) => {
|
|
246
|
+
if (e.pointerId !== pointerId)
|
|
247
|
+
return;
|
|
248
|
+
suppressUntil = performance.now() + 300;
|
|
249
|
+
done();
|
|
250
|
+
};
|
|
251
|
+
const timer = window.setTimeout(done, 5000);
|
|
252
|
+
addEventListener('pointerup', onUp, true);
|
|
253
|
+
addEventListener('pointerdown', done, true);
|
|
254
|
+
}
|
|
255
|
+
export class TySortable extends HTMLElement {
|
|
256
|
+
static get observedAttributes() {
|
|
257
|
+
return ['handle', 'disabled'];
|
|
258
|
+
}
|
|
259
|
+
constructor() {
|
|
260
|
+
super();
|
|
261
|
+
this._states = null;
|
|
262
|
+
this._press = null;
|
|
263
|
+
this._canDrop = null;
|
|
264
|
+
this._labels = {};
|
|
265
|
+
this._childObserver = null;
|
|
266
|
+
this._onPressMove = (e) => {
|
|
267
|
+
const press = this._press;
|
|
268
|
+
if (!press || e.pointerId !== press.pointerId)
|
|
269
|
+
return;
|
|
270
|
+
const d = Math.hypot(e.clientX - press.startX, e.clientY - press.startY);
|
|
271
|
+
if (press.pointerType === 'mouse') {
|
|
272
|
+
if (d >= MOUSE_DISTANCE)
|
|
273
|
+
this._activate(press, e.clientX, e.clientY);
|
|
274
|
+
}
|
|
275
|
+
else if (press.onHandle) {
|
|
276
|
+
if (d >= HANDLE_TOUCH_DISTANCE)
|
|
277
|
+
this._activate(press, e.clientX, e.clientY);
|
|
278
|
+
}
|
|
279
|
+
else if (d > TOUCH_TOLERANCE) {
|
|
280
|
+
this._endPress();
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
this._onPressEnd = (e) => {
|
|
284
|
+
if (this._press && e.pointerId === this._press.pointerId)
|
|
285
|
+
this._endPress();
|
|
286
|
+
};
|
|
287
|
+
try {
|
|
288
|
+
this._states = this.attachInternals().states;
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
this._states = null;
|
|
292
|
+
}
|
|
293
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
294
|
+
ensureStyles(root, { css: sortableStyles, id: 'ty-sortable' });
|
|
295
|
+
root.innerHTML = '<slot></slot><div class="live" part="live" aria-live="assertive" aria-atomic="true"></div>';
|
|
296
|
+
this._live = root.querySelector('.live');
|
|
297
|
+
this.addEventListener('pointerdown', (e) => this._onPointerDown(e));
|
|
298
|
+
this.addEventListener('keydown', (e) => this._onKeyDown(e));
|
|
299
|
+
this.addEventListener('keyup', (e) => this._onKeyUp(e));
|
|
300
|
+
this.addEventListener('click', (e) => this._onClick(e));
|
|
301
|
+
this.addEventListener('focusout', (e) => this._onFocusOut(e));
|
|
302
|
+
this.addEventListener('dragstart', (e) => {
|
|
303
|
+
if (this._itemFrom(e))
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
});
|
|
306
|
+
this.addEventListener('touchmove', blockTouchScroll, { passive: false });
|
|
307
|
+
this.addEventListener('touchstart', (e) => {
|
|
308
|
+
const t = e.target;
|
|
309
|
+
t.addEventListener('touchmove', blockTouchScroll, { passive: false });
|
|
310
|
+
const done = () => {
|
|
311
|
+
t.removeEventListener('touchmove', blockTouchScroll);
|
|
312
|
+
t.removeEventListener('touchend', done);
|
|
313
|
+
t.removeEventListener('touchcancel', done);
|
|
314
|
+
};
|
|
315
|
+
t.addEventListener('touchend', done);
|
|
316
|
+
t.addEventListener('touchcancel', done);
|
|
317
|
+
}, { passive: true });
|
|
318
|
+
}
|
|
319
|
+
get group() {
|
|
320
|
+
return this.getAttribute('group');
|
|
321
|
+
}
|
|
322
|
+
set group(v) {
|
|
323
|
+
if (v == null)
|
|
324
|
+
this.removeAttribute('group');
|
|
325
|
+
else
|
|
326
|
+
this.setAttribute('group', v);
|
|
327
|
+
}
|
|
328
|
+
get axis() {
|
|
329
|
+
return this.getAttribute('axis') === 'horizontal' ? 'horizontal' : 'vertical';
|
|
330
|
+
}
|
|
331
|
+
set axis(v) {
|
|
332
|
+
this.setAttribute('axis', v);
|
|
333
|
+
}
|
|
334
|
+
get disabled() {
|
|
335
|
+
return this.hasAttribute('disabled');
|
|
336
|
+
}
|
|
337
|
+
set disabled(v) {
|
|
338
|
+
this.toggleAttribute('disabled', !!v && String(v) !== 'false');
|
|
339
|
+
}
|
|
340
|
+
get dropLine() {
|
|
341
|
+
return this.hasAttribute('drop-line');
|
|
342
|
+
}
|
|
343
|
+
set dropLine(v) {
|
|
344
|
+
this.toggleAttribute('drop-line', !!v && String(v) !== 'false');
|
|
345
|
+
}
|
|
346
|
+
get flash() {
|
|
347
|
+
return this.hasAttribute('flash');
|
|
348
|
+
}
|
|
349
|
+
set flash(v) {
|
|
350
|
+
this.toggleAttribute('flash', !!v && String(v) !== 'false');
|
|
351
|
+
}
|
|
352
|
+
get handleSelector() {
|
|
353
|
+
return this.getAttribute('handle') || '[data-handle]';
|
|
354
|
+
}
|
|
355
|
+
get accepts() {
|
|
356
|
+
return this.getAttribute('accepts');
|
|
357
|
+
}
|
|
358
|
+
set accepts(v) {
|
|
359
|
+
if (v == null)
|
|
360
|
+
this.removeAttribute('accepts');
|
|
361
|
+
else
|
|
362
|
+
this.setAttribute('accepts', v);
|
|
363
|
+
}
|
|
364
|
+
get holdTimeout() {
|
|
365
|
+
const n = parseInt(this.getAttribute('hold-timeout') ?? '', 10);
|
|
366
|
+
return Number.isFinite(n) && n >= 0 ? n : HOLD_TIMEOUT;
|
|
367
|
+
}
|
|
368
|
+
get canDrop() {
|
|
369
|
+
return this._canDrop;
|
|
370
|
+
}
|
|
371
|
+
set canDrop(fn) {
|
|
372
|
+
this._canDrop = typeof fn === 'function' ? fn : null;
|
|
373
|
+
}
|
|
374
|
+
get labels() {
|
|
375
|
+
return this._labels;
|
|
376
|
+
}
|
|
377
|
+
set labels(v) {
|
|
378
|
+
this._labels = v && typeof v === 'object' ? v : {};
|
|
379
|
+
}
|
|
380
|
+
get items() {
|
|
381
|
+
return Array.from(this.children).filter((c) => c instanceof HTMLElement && !(c instanceof HTMLTemplateElement) && !c.hasAttribute('data-sortable-ignore'));
|
|
382
|
+
}
|
|
383
|
+
keyOf(item) {
|
|
384
|
+
const attr = this.getAttribute('key-attr');
|
|
385
|
+
return (attr && item.getAttribute(attr)) || item.getAttribute('data-key') || item.id || null;
|
|
386
|
+
}
|
|
387
|
+
connectedCallback() {
|
|
388
|
+
upgradeProperties(this);
|
|
389
|
+
instances.add(this);
|
|
390
|
+
adoptLightStyles(this);
|
|
391
|
+
this._syncStructure();
|
|
392
|
+
this._childObserver = new MutationObserver(() => this._onChildren());
|
|
393
|
+
this._childObserver.observe(this, { childList: true });
|
|
394
|
+
const s = session;
|
|
395
|
+
if (s?.phase === 'holding' && this.group === s.group && (s.group || this.id === s.sourceId || this.id === s.targetId)) {
|
|
396
|
+
observeForHold(s, this);
|
|
397
|
+
queueMicrotask(() => evaluateHold('mutation'));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
disconnectedCallback() {
|
|
401
|
+
instances.delete(this);
|
|
402
|
+
this._childObserver?.disconnect();
|
|
403
|
+
this._childObserver = null;
|
|
404
|
+
this._endPress();
|
|
405
|
+
if (session?.mode === 'keyboard' && session.phase === 'dragging')
|
|
406
|
+
queueMicrotask(checkKeyboard);
|
|
407
|
+
}
|
|
408
|
+
connectedMoveCallback() { }
|
|
409
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
410
|
+
if (oldValue === newValue || !this.isConnected)
|
|
411
|
+
return;
|
|
412
|
+
if (name === 'handle') {
|
|
413
|
+
adoptLightStyles(this);
|
|
414
|
+
this._syncStructure();
|
|
415
|
+
}
|
|
416
|
+
else if (name === 'disabled' && newValue !== null && session?.phase === 'dragging' && session.source === this) {
|
|
417
|
+
cancelDrag('disabled');
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
_onChildren() {
|
|
421
|
+
this._syncStructure();
|
|
422
|
+
if (session?.mode === 'keyboard' && session.phase === 'dragging')
|
|
423
|
+
checkKeyboard();
|
|
424
|
+
}
|
|
425
|
+
_syncStructure() {
|
|
426
|
+
const items = this.items;
|
|
427
|
+
setState(this, 'empty', items.length === 0);
|
|
428
|
+
setState(this, 'handles', items.some((i) => handleOf(this, i)));
|
|
429
|
+
}
|
|
430
|
+
move(itemOrKey, opts = {}) {
|
|
431
|
+
if (session)
|
|
432
|
+
return Promise.reject(new Error('ty-sortable: a move is still pending'));
|
|
433
|
+
const item = typeof itemOrKey === 'string' ? this.items.find((i) => this.keyOf(i) === itemOrKey) : itemOrKey;
|
|
434
|
+
if (!item || item.parentElement !== this)
|
|
435
|
+
return Promise.reject(new Error('ty-sortable: no such item'));
|
|
436
|
+
const target = typeof opts.to === 'string'
|
|
437
|
+
? members(this.group, this).find((m) => m.id === opts.to)
|
|
438
|
+
: opts.to ?? this;
|
|
439
|
+
if (!target || (target !== this && (!this.group || target.group !== this.group))) {
|
|
440
|
+
return Promise.reject(new Error('ty-sortable: no such list in this group'));
|
|
441
|
+
}
|
|
442
|
+
const s = newSession('api', this, item);
|
|
443
|
+
const others = target.items.filter((i) => i !== item);
|
|
444
|
+
let slot = others.length;
|
|
445
|
+
if (opts.before != null)
|
|
446
|
+
slot = others.findIndex((i) => target.keyOf(i) === opts.before);
|
|
447
|
+
else if (opts.after != null) {
|
|
448
|
+
const i = others.findIndex((o) => target.keyOf(o) === opts.after);
|
|
449
|
+
slot = i < 0 ? -1 : i + 1;
|
|
450
|
+
}
|
|
451
|
+
else if (opts.index != null)
|
|
452
|
+
slot = Math.max(0, Math.min(opts.index, others.length));
|
|
453
|
+
if (slot < 0)
|
|
454
|
+
return Promise.reject(new Error('ty-sortable: no such neighbour'));
|
|
455
|
+
setTargetOf(s, target);
|
|
456
|
+
s.slot = clampLocked(target, item, slot);
|
|
457
|
+
s.legal = legality(s, target, s.slot);
|
|
458
|
+
if (s.legal !== 'ok')
|
|
459
|
+
return Promise.reject(new Error('ty-sortable: drop not allowed'));
|
|
460
|
+
session = s;
|
|
461
|
+
return new Promise((resolve) => {
|
|
462
|
+
s.resolve = resolve;
|
|
463
|
+
if (!commit())
|
|
464
|
+
resolve('accepted');
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
settle(outcome) {
|
|
468
|
+
if (session?.phase !== 'holding')
|
|
469
|
+
return;
|
|
470
|
+
if (outcome)
|
|
471
|
+
release(outcome);
|
|
472
|
+
else
|
|
473
|
+
evaluateHold('settle');
|
|
474
|
+
}
|
|
475
|
+
_announce(name, vars = {}) {
|
|
476
|
+
const tpl = this._labels[name] ?? DEFAULT_STRINGS[name];
|
|
477
|
+
const text = tpl.replace(/\{(\w+)\}/g, (_, k) => String(vars[k] ?? ''));
|
|
478
|
+
this._live.textContent = '';
|
|
479
|
+
requestAnimationFrame(() => { this._live.textContent = text; });
|
|
480
|
+
}
|
|
481
|
+
_itemFrom(e) {
|
|
482
|
+
for (const n of e.composedPath()) {
|
|
483
|
+
if (n === this)
|
|
484
|
+
return null;
|
|
485
|
+
if (n instanceof HTMLElement && n.parentElement === this)
|
|
486
|
+
return this.items.includes(n) ? n : null;
|
|
487
|
+
}
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
_gripFrom(e) {
|
|
491
|
+
if (this.disabled)
|
|
492
|
+
return null;
|
|
493
|
+
const item = this._itemFrom(e);
|
|
494
|
+
if (!item || item.hasAttribute('data-locked'))
|
|
495
|
+
return null;
|
|
496
|
+
const origin = e.composedPath()[0];
|
|
497
|
+
if (!(origin instanceof Element))
|
|
498
|
+
return null;
|
|
499
|
+
const hasHandle = !!handleOf(this, item);
|
|
500
|
+
let handle = null;
|
|
501
|
+
try {
|
|
502
|
+
handle = origin.closest(this.handleSelector);
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
handle = null;
|
|
506
|
+
}
|
|
507
|
+
if (handle && !item.contains(handle))
|
|
508
|
+
handle = null;
|
|
509
|
+
if (hasHandle)
|
|
510
|
+
return handle ? { item, onHandle: true, handle } : null;
|
|
511
|
+
const control = origin.closest(INTERACTIVE);
|
|
512
|
+
if (control && item.contains(control) && control !== item)
|
|
513
|
+
return null;
|
|
514
|
+
return { item, onHandle: false, handle: null };
|
|
515
|
+
}
|
|
516
|
+
_onPointerDown(e) {
|
|
517
|
+
if (session || !e.isPrimary || (e.pointerType === 'mouse' && e.button !== 0) || this._press)
|
|
518
|
+
return;
|
|
519
|
+
const grip = this._gripFrom(e);
|
|
520
|
+
if (!grip)
|
|
521
|
+
return;
|
|
522
|
+
const touchLike = e.pointerType !== 'mouse';
|
|
523
|
+
if (touchLike && !grip.onHandle && performance.now() - lastScrollAt < FLING_GUARD)
|
|
524
|
+
return;
|
|
525
|
+
const press = {
|
|
526
|
+
pointerId: e.pointerId,
|
|
527
|
+
pointerType: e.pointerType,
|
|
528
|
+
item: grip.item,
|
|
529
|
+
onHandle: grip.onHandle,
|
|
530
|
+
startX: e.clientX,
|
|
531
|
+
startY: e.clientY,
|
|
532
|
+
timer: 0,
|
|
533
|
+
};
|
|
534
|
+
this._press = press;
|
|
535
|
+
if (!touchLike && grip.onHandle)
|
|
536
|
+
e.preventDefault();
|
|
537
|
+
if (touchLike && !grip.onHandle) {
|
|
538
|
+
press.timer = window.setTimeout(() => {
|
|
539
|
+
if (this._press === press)
|
|
540
|
+
this._activate(press, press.startX, press.startY);
|
|
541
|
+
}, TOUCH_DELAY);
|
|
542
|
+
}
|
|
543
|
+
addEventListener('pointermove', this._onPressMove, true);
|
|
544
|
+
addEventListener('pointerup', this._onPressEnd, true);
|
|
545
|
+
addEventListener('pointercancel', this._onPressEnd, true);
|
|
546
|
+
if (touchLike)
|
|
547
|
+
addEventListener('contextmenu', preventDefault, true);
|
|
548
|
+
}
|
|
549
|
+
_endPress() {
|
|
550
|
+
const press = this._press;
|
|
551
|
+
if (!press)
|
|
552
|
+
return;
|
|
553
|
+
clearTimeout(press.timer);
|
|
554
|
+
this._press = null;
|
|
555
|
+
removeEventListener('pointermove', this._onPressMove, true);
|
|
556
|
+
removeEventListener('pointerup', this._onPressEnd, true);
|
|
557
|
+
removeEventListener('pointercancel', this._onPressEnd, true);
|
|
558
|
+
if (session?.mode !== 'pointer')
|
|
559
|
+
removeEventListener('contextmenu', preventDefault, true);
|
|
560
|
+
}
|
|
561
|
+
_activate(press, x, y) {
|
|
562
|
+
const item = press.item;
|
|
563
|
+
const touchLike = press.pointerType !== 'mouse';
|
|
564
|
+
this._endPress();
|
|
565
|
+
if (session || !item.isConnected || item.parentElement !== this)
|
|
566
|
+
return;
|
|
567
|
+
const s = newSession('pointer', this, item);
|
|
568
|
+
const rect = item.getBoundingClientRect();
|
|
569
|
+
s.pointerId = press.pointerId;
|
|
570
|
+
s.x = x;
|
|
571
|
+
s.y = y;
|
|
572
|
+
s.startX = x;
|
|
573
|
+
s.startY = y;
|
|
574
|
+
s.offsetX = press.startX - rect.left;
|
|
575
|
+
s.offsetY = press.startY - rect.top;
|
|
576
|
+
session = s;
|
|
577
|
+
const active = document.activeElement;
|
|
578
|
+
if (active && item.contains(active) && active.matches('input, textarea, select, [contenteditable]'))
|
|
579
|
+
active.blur();
|
|
580
|
+
getSelection()?.removeAllRanges();
|
|
581
|
+
createLayer(s, rect);
|
|
582
|
+
markSource(s, 'dim');
|
|
583
|
+
setState(this, 'dragging', true);
|
|
584
|
+
addEventListener('pointermove', onDragMove, true);
|
|
585
|
+
addEventListener('pointerup', onDragEnd, true);
|
|
586
|
+
addEventListener('pointercancel', onDragCancel, true);
|
|
587
|
+
addEventListener('pointerdown', onSecondPointer, true);
|
|
588
|
+
addEventListener('selectstart', preventDefault, true);
|
|
589
|
+
addEventListener('keydown', onDragKey, true);
|
|
590
|
+
addEventListener('blur', onWindowBlur);
|
|
591
|
+
document.addEventListener('visibilitychange', onWindowBlur);
|
|
592
|
+
screen.orientation?.addEventListener('change', onWindowBlur);
|
|
593
|
+
if (touchLike)
|
|
594
|
+
addEventListener('contextmenu', preventDefault, true);
|
|
595
|
+
s.cleanup.push(() => {
|
|
596
|
+
removeEventListener('pointermove', onDragMove, true);
|
|
597
|
+
removeEventListener('pointerup', onDragEnd, true);
|
|
598
|
+
removeEventListener('pointercancel', onDragCancel, true);
|
|
599
|
+
removeEventListener('pointerdown', onSecondPointer, true);
|
|
600
|
+
removeEventListener('selectstart', preventDefault, true);
|
|
601
|
+
removeEventListener('keydown', onDragKey, true);
|
|
602
|
+
removeEventListener('blur', onWindowBlur);
|
|
603
|
+
document.removeEventListener('visibilitychange', onWindowBlur);
|
|
604
|
+
screen.orientation?.removeEventListener('change', onWindowBlur);
|
|
605
|
+
removeEventListener('contextmenu', preventDefault, true);
|
|
606
|
+
});
|
|
607
|
+
dispatchStart(s);
|
|
608
|
+
s.raf = requestAnimationFrame(frame);
|
|
609
|
+
}
|
|
610
|
+
_onKeyDown(e) {
|
|
611
|
+
if (session?.mode === 'keyboard' && session.phase === 'dragging')
|
|
612
|
+
return;
|
|
613
|
+
if (e.key !== ' ' && e.key !== 'Enter')
|
|
614
|
+
return;
|
|
615
|
+
if (e.repeat || e === droppedBy)
|
|
616
|
+
return;
|
|
617
|
+
const grip = this._gripFrom(e);
|
|
618
|
+
if (!grip)
|
|
619
|
+
return;
|
|
620
|
+
const origin = e.composedPath()[0];
|
|
621
|
+
if (!grip.onHandle && origin !== grip.item)
|
|
622
|
+
return;
|
|
623
|
+
e.preventDefault();
|
|
624
|
+
keyHandledAt = performance.now();
|
|
625
|
+
const s = session;
|
|
626
|
+
if (s) {
|
|
627
|
+
if (s.phase === 'holding')
|
|
628
|
+
this._announce('pending');
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
startKeyboard(this, grip.item);
|
|
632
|
+
}
|
|
633
|
+
_onKeyUp(e) {
|
|
634
|
+
if (e.key === ' ' && performance.now() - keyHandledAt < 1000)
|
|
635
|
+
e.preventDefault();
|
|
636
|
+
}
|
|
637
|
+
_onClick(e) {
|
|
638
|
+
if (e.detail !== 0 || performance.now() - keyHandledAt < 1000)
|
|
639
|
+
return;
|
|
640
|
+
const grip = this._gripFrom(e);
|
|
641
|
+
if (!grip?.onHandle)
|
|
642
|
+
return;
|
|
643
|
+
e.preventDefault();
|
|
644
|
+
const s = session;
|
|
645
|
+
if (s?.mode === 'keyboard' && s.phase === 'dragging' && s.item === grip.item) {
|
|
646
|
+
dropKeyboard(s);
|
|
647
|
+
}
|
|
648
|
+
else if (!s) {
|
|
649
|
+
grip.handle?.focus({ preventScroll: true });
|
|
650
|
+
startKeyboard(this, grip.item);
|
|
651
|
+
}
|
|
652
|
+
else if (s.phase === 'holding') {
|
|
653
|
+
this._announce('pending');
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
_onFocusOut(e) {
|
|
657
|
+
const s = session;
|
|
658
|
+
if (!s || s.mode !== 'keyboard' || s.phase !== 'dragging' || s.source !== this)
|
|
659
|
+
return;
|
|
660
|
+
const to = e.relatedTarget;
|
|
661
|
+
if (to && s.item.contains(to))
|
|
662
|
+
return;
|
|
663
|
+
if (to) {
|
|
664
|
+
cancelDrag('blur');
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
queueMicrotask(() => {
|
|
668
|
+
if (session !== s || s.phase !== 'dragging')
|
|
669
|
+
return;
|
|
670
|
+
if (s.item.isConnected && s.source.isConnected)
|
|
671
|
+
cancelDrag('blur');
|
|
672
|
+
else
|
|
673
|
+
checkKeyboard();
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
function newSession(mode, source, item) {
|
|
678
|
+
return {
|
|
679
|
+
mode,
|
|
680
|
+
phase: 'dragging',
|
|
681
|
+
source,
|
|
682
|
+
sourceId: source.id || null,
|
|
683
|
+
item,
|
|
684
|
+
key: source.keyOf(item),
|
|
685
|
+
group: source.group,
|
|
686
|
+
fromIndex: source.items.indexOf(item),
|
|
687
|
+
target: null,
|
|
688
|
+
targetId: null,
|
|
689
|
+
slot: -1,
|
|
690
|
+
legal: 'ok',
|
|
691
|
+
pointerId: -1,
|
|
692
|
+
x: 0,
|
|
693
|
+
y: 0,
|
|
694
|
+
offsetX: 0,
|
|
695
|
+
offsetY: 0,
|
|
696
|
+
startX: 0,
|
|
697
|
+
startY: 0,
|
|
698
|
+
armed: false,
|
|
699
|
+
raf: 0,
|
|
700
|
+
layer: null,
|
|
701
|
+
ghost: null,
|
|
702
|
+
indicator: null,
|
|
703
|
+
shifts: new Map(),
|
|
704
|
+
cursorMarker: null,
|
|
705
|
+
value: null,
|
|
706
|
+
snapshot: null,
|
|
707
|
+
observed: new WeakSet(),
|
|
708
|
+
observers: [],
|
|
709
|
+
busy: false,
|
|
710
|
+
htmx: false,
|
|
711
|
+
holdStart: 0,
|
|
712
|
+
painted: new Map(),
|
|
713
|
+
timer: 0,
|
|
714
|
+
focus: null,
|
|
715
|
+
resolve: null,
|
|
716
|
+
cleanup: [],
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
function setTargetOf(s, c) {
|
|
720
|
+
s.target = c;
|
|
721
|
+
s.targetId = c?.id || null;
|
|
722
|
+
}
|
|
723
|
+
function preventDefault(e) {
|
|
724
|
+
e.preventDefault();
|
|
725
|
+
}
|
|
726
|
+
function blockTouchScroll(e) {
|
|
727
|
+
if (session?.mode === 'pointer' && session.phase === 'dragging' && e.cancelable)
|
|
728
|
+
e.preventDefault();
|
|
729
|
+
}
|
|
730
|
+
function onWindowBlur() {
|
|
731
|
+
if (session?.phase === 'dragging')
|
|
732
|
+
cancelDrag('pointercancel');
|
|
733
|
+
}
|
|
734
|
+
function onSecondPointer(e) {
|
|
735
|
+
if (session?.mode === 'pointer' && session.phase === 'dragging' && e.pointerId !== session.pointerId)
|
|
736
|
+
cancelDrag('pointercancel');
|
|
737
|
+
}
|
|
738
|
+
function dragValue(s) {
|
|
739
|
+
return { key: s.key, group: s.group, source: s.mode, from: fromPos(s) };
|
|
740
|
+
}
|
|
741
|
+
function dispatchStart(s) {
|
|
742
|
+
s.source.dispatchEvent(new CustomEvent('ty-sortable-start', {
|
|
743
|
+
detail: { value: dragValue(s), item: s.item },
|
|
744
|
+
}));
|
|
745
|
+
}
|
|
746
|
+
function fromPos(s) {
|
|
747
|
+
const items = s.source.items;
|
|
748
|
+
const i = items.indexOf(s.item);
|
|
749
|
+
const index = i >= 0 ? i : s.fromIndex;
|
|
750
|
+
return {
|
|
751
|
+
list: s.sourceId,
|
|
752
|
+
index,
|
|
753
|
+
prev: items[index - 1] ? s.source.keyOf(items[index - 1]) : null,
|
|
754
|
+
next: items[index + 1] ? s.source.keyOf(items[index + 1]) : null,
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
function othersIn(c, item) {
|
|
758
|
+
return c.items.filter((i) => i !== item);
|
|
759
|
+
}
|
|
760
|
+
function clampLocked(c, item, slot) {
|
|
761
|
+
const others = othersIn(c, item);
|
|
762
|
+
let lead = 0;
|
|
763
|
+
while (lead < others.length && others[lead].hasAttribute('data-locked'))
|
|
764
|
+
lead++;
|
|
765
|
+
let trail = others.length;
|
|
766
|
+
while (trail > lead && others[trail - 1].hasAttribute('data-locked'))
|
|
767
|
+
trail--;
|
|
768
|
+
return Math.max(lead, Math.min(slot, trail));
|
|
769
|
+
}
|
|
770
|
+
function legality(s, c, slot) {
|
|
771
|
+
if (c !== s.source) {
|
|
772
|
+
if (c.disabled)
|
|
773
|
+
return 'blocked';
|
|
774
|
+
const accepts = c.accepts;
|
|
775
|
+
if (accepts) {
|
|
776
|
+
try {
|
|
777
|
+
if (!s.item.matches(accepts))
|
|
778
|
+
return 'blocked';
|
|
779
|
+
}
|
|
780
|
+
catch {
|
|
781
|
+
return 'blocked';
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
const fn = c.canDrop;
|
|
786
|
+
if (fn && !fn({ item: s.item, key: s.key, from: s.source, to: c, index: slot }))
|
|
787
|
+
return 'blocked';
|
|
788
|
+
return 'ok';
|
|
789
|
+
}
|
|
790
|
+
function revalidate(s) {
|
|
791
|
+
const source = resolveContainer(s.source, s.sourceId, s.group);
|
|
792
|
+
if (!source)
|
|
793
|
+
return false;
|
|
794
|
+
s.source = source;
|
|
795
|
+
let moved = false;
|
|
796
|
+
if (!s.item.isConnected || s.item.parentElement !== source) {
|
|
797
|
+
const again = s.key ? source.items.find((i) => source.keyOf(i) === s.key) : null;
|
|
798
|
+
if (!again)
|
|
799
|
+
return false;
|
|
800
|
+
s.item = again;
|
|
801
|
+
moved = true;
|
|
802
|
+
}
|
|
803
|
+
if (s.target && !s.target.isConnected) {
|
|
804
|
+
setTargetOf(s, resolveContainer(s.target, s.targetId, s.group));
|
|
805
|
+
moved = true;
|
|
806
|
+
}
|
|
807
|
+
for (const [el, sh] of s.shifts) {
|
|
808
|
+
if (el.isConnected)
|
|
809
|
+
continue;
|
|
810
|
+
sh.anim.cancel();
|
|
811
|
+
s.shifts.delete(el);
|
|
812
|
+
moved = true;
|
|
813
|
+
}
|
|
814
|
+
if (s.layer && !s.layer.isConnected) {
|
|
815
|
+
s.layer = null;
|
|
816
|
+
createLayer(s, s.ghost ? s.ghost.getBoundingClientRect() : s.item.getBoundingClientRect());
|
|
817
|
+
}
|
|
818
|
+
if (moved && s.phase === 'dragging') {
|
|
819
|
+
if (s.shifts.size || shiftPreview(s))
|
|
820
|
+
previewShifts(s);
|
|
821
|
+
else
|
|
822
|
+
markSource(s, 'dim');
|
|
823
|
+
}
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
function onDragMove(e) {
|
|
827
|
+
const s = session;
|
|
828
|
+
if (!s || s.phase !== 'dragging' || e.pointerId !== s.pointerId)
|
|
829
|
+
return;
|
|
830
|
+
s.x = e.clientX;
|
|
831
|
+
s.y = e.clientY;
|
|
832
|
+
if (!s.armed && Math.hypot(s.x - s.startX, s.y - s.startY) >= AUTOSCROLL_ARM)
|
|
833
|
+
s.armed = true;
|
|
834
|
+
}
|
|
835
|
+
function onDragEnd(e) {
|
|
836
|
+
const s = session;
|
|
837
|
+
if (!s || s.phase !== 'dragging' || e.pointerId !== s.pointerId)
|
|
838
|
+
return;
|
|
839
|
+
s.x = e.clientX;
|
|
840
|
+
s.y = e.clientY;
|
|
841
|
+
hitTest(s);
|
|
842
|
+
suppressUntil = performance.now() + 300;
|
|
843
|
+
if (!s.target)
|
|
844
|
+
cancelDrag('spill');
|
|
845
|
+
else if (s.legal !== 'ok')
|
|
846
|
+
cancelDrag('blocked');
|
|
847
|
+
else
|
|
848
|
+
commit();
|
|
849
|
+
}
|
|
850
|
+
function onDragCancel(e) {
|
|
851
|
+
if (session?.phase === 'dragging' && e.pointerId === session.pointerId)
|
|
852
|
+
cancelDrag('pointercancel');
|
|
853
|
+
}
|
|
854
|
+
function onDragKey(e) {
|
|
855
|
+
if (e.key === 'Escape' && session?.phase === 'dragging' && session.mode === 'pointer') {
|
|
856
|
+
e.preventDefault();
|
|
857
|
+
e.stopPropagation();
|
|
858
|
+
cancelDrag('escape');
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
function frame() {
|
|
862
|
+
const s = session;
|
|
863
|
+
if (!s || s.phase !== 'dragging' || s.mode !== 'pointer')
|
|
864
|
+
return;
|
|
865
|
+
if (!revalidate(s)) {
|
|
866
|
+
cancelDrag('gone');
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
if (s.armed)
|
|
870
|
+
autoScroll(scrollersOf(s.target ?? s.source), s.x, s.y);
|
|
871
|
+
hitTest(s);
|
|
872
|
+
if (s.ghost)
|
|
873
|
+
s.ghost.style.translate = `${s.x - s.offsetX}px ${s.y - s.offsetY}px`;
|
|
874
|
+
s.raf = requestAnimationFrame(frame);
|
|
875
|
+
}
|
|
876
|
+
function hitTest(s) {
|
|
877
|
+
let target = null;
|
|
878
|
+
const group = members(s.group, s.source);
|
|
879
|
+
const top = deepElementFromPoint(s.x, s.y);
|
|
880
|
+
for (let el = top; el; el = flatParent(el)) {
|
|
881
|
+
if (el instanceof TySortable && group.includes(el)) {
|
|
882
|
+
target = el;
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
if (!target) {
|
|
887
|
+
const dialog = top ? openDialogOf(top) : null;
|
|
888
|
+
target = group.find((c) => {
|
|
889
|
+
if (dialog && openDialogOf(c) !== dialog)
|
|
890
|
+
return false;
|
|
891
|
+
const r = visibleRect(c);
|
|
892
|
+
return !!r && s.x >= r.left - 12 && s.x <= r.right + 12 && s.y >= r.top - 12 && s.y <= r.bottom + 12;
|
|
893
|
+
}) ?? null;
|
|
894
|
+
}
|
|
895
|
+
if (!target) {
|
|
896
|
+
setTarget(s, null, -1, 'ok');
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
const others = othersIn(target, s.item);
|
|
900
|
+
const vertical = target.axis === 'vertical';
|
|
901
|
+
let slot = others.length;
|
|
902
|
+
for (let i = 0; i < others.length; i++) {
|
|
903
|
+
const r = baseRect(others[i]);
|
|
904
|
+
if (vertical ? s.y < r.top + r.height / 2 : s.x < r.left + r.width / 2) {
|
|
905
|
+
slot = i;
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
slot = clampLocked(target, s.item, slot);
|
|
910
|
+
setTarget(s, target, slot, legality(s, target, slot));
|
|
911
|
+
}
|
|
912
|
+
function setTarget(s, target, slot, legal) {
|
|
913
|
+
const changed = target !== s.target || slot !== s.slot || legal !== s.legal;
|
|
914
|
+
if (s.target && s.target !== target) {
|
|
915
|
+
setState(s.target, 'over', false);
|
|
916
|
+
setState(s.target, 'blocked', false);
|
|
917
|
+
}
|
|
918
|
+
setTargetOf(s, target);
|
|
919
|
+
s.slot = slot;
|
|
920
|
+
s.legal = legal;
|
|
921
|
+
if (target) {
|
|
922
|
+
setState(target, 'over', legal === 'ok');
|
|
923
|
+
setState(target, 'blocked', legal === 'blocked');
|
|
924
|
+
}
|
|
925
|
+
if (changed)
|
|
926
|
+
previewShifts(s);
|
|
927
|
+
if (s.mode === 'pointer')
|
|
928
|
+
setCursor(s, legal === 'blocked' ? 'blocked' : 'grabbing');
|
|
929
|
+
paintIndicator(s);
|
|
930
|
+
if (changed && s.mode === 'keyboard' && target) {
|
|
931
|
+
const total = othersIn(target, s.item).length + 1;
|
|
932
|
+
const vars = { item: itemLabel(s.source, s.item), position: slot + 1, total, list: listLabel(target) };
|
|
933
|
+
s.source._announce(legal === 'ok' ? 'moved' : 'blocked', vars);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
const LAYER_VARS = [
|
|
937
|
+
'--ty-sortable-indicator-color',
|
|
938
|
+
'--ty-sortable-blocked-color',
|
|
939
|
+
'--ty-sortable-ghost-shadow',
|
|
940
|
+
'--ty-sortable-ghost-scale',
|
|
941
|
+
'--ty-sortable-ghost-rotate',
|
|
942
|
+
'--ty-sortable-duration',
|
|
943
|
+
];
|
|
944
|
+
function createLayer(s, rect) {
|
|
945
|
+
const layer = document.createElement('div');
|
|
946
|
+
layer.setAttribute('popover', 'manual');
|
|
947
|
+
layer.setAttribute('data-ty-sortable-layer', '');
|
|
948
|
+
layer.style.cssText = 'position:fixed;inset:0;margin:0;padding:0;border:0;background:transparent;overflow:visible;pointer-events:none;width:auto;height:auto;max-width:none;max-height:none;color:inherit';
|
|
949
|
+
const cs = getComputedStyle(s.source);
|
|
950
|
+
for (const name of LAYER_VARS) {
|
|
951
|
+
const v = cs.getPropertyValue(name).trim();
|
|
952
|
+
if (v)
|
|
953
|
+
layer.style.setProperty(name, v);
|
|
954
|
+
}
|
|
955
|
+
const root = s.item.getRootNode();
|
|
956
|
+
(root instanceof ShadowRoot ? root : document.body).appendChild(layer);
|
|
957
|
+
try {
|
|
958
|
+
layer.showPopover();
|
|
959
|
+
}
|
|
960
|
+
catch {
|
|
961
|
+
layer.style.zIndex = '2147483647';
|
|
962
|
+
}
|
|
963
|
+
const indicator = document.createElement('div');
|
|
964
|
+
indicator.style.cssText = 'position:absolute;left:0;top:0;display:none;border-radius:999px;pointer-events:none';
|
|
965
|
+
layer.appendChild(indicator);
|
|
966
|
+
s.layer = layer;
|
|
967
|
+
s.indicator = indicator;
|
|
968
|
+
if (s.mode === 'pointer')
|
|
969
|
+
setCursor(s, 'grabbing');
|
|
970
|
+
if (s.mode === 'pointer') {
|
|
971
|
+
addGhost(s, rect);
|
|
972
|
+
lift(s);
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
function setCursor(s, cursor) {
|
|
976
|
+
adoptCursorSheet();
|
|
977
|
+
let marker = s.layer?.parentElement === document.body ? s.layer : s.cursorMarker;
|
|
978
|
+
if (!marker && cursor) {
|
|
979
|
+
marker = document.createElement('div');
|
|
980
|
+
marker.setAttribute('data-ty-sortable-layer', '');
|
|
981
|
+
marker.hidden = true;
|
|
982
|
+
document.body.appendChild(marker);
|
|
983
|
+
s.cursorMarker = marker;
|
|
984
|
+
}
|
|
985
|
+
if (!marker)
|
|
986
|
+
return;
|
|
987
|
+
if (cursor)
|
|
988
|
+
marker.setAttribute('data-cursor', cursor);
|
|
989
|
+
else
|
|
990
|
+
marker.removeAttribute('data-cursor');
|
|
991
|
+
}
|
|
992
|
+
let cursorSheet = null;
|
|
993
|
+
function adoptCursorSheet() {
|
|
994
|
+
if (cursorSheet)
|
|
995
|
+
return;
|
|
996
|
+
cursorSheet = new CSSStyleSheet();
|
|
997
|
+
cursorSheet.replaceSync(sortableCursorStyles);
|
|
998
|
+
document.adoptedStyleSheets = [...document.adoptedStyleSheets, cursorSheet];
|
|
999
|
+
}
|
|
1000
|
+
function addGhost(s, rect) {
|
|
1001
|
+
const ghost = makeGhost(s.item, rect);
|
|
1002
|
+
ghost.style.translate = `${rect.left}px ${rect.top}px`;
|
|
1003
|
+
s.layer.appendChild(ghost);
|
|
1004
|
+
s.ghost = ghost;
|
|
1005
|
+
}
|
|
1006
|
+
function makeGhost(item, rect) {
|
|
1007
|
+
const ghost = item.cloneNode(true);
|
|
1008
|
+
ghost.removeAttribute('id');
|
|
1009
|
+
ghost.querySelectorAll('[id]').forEach((n) => n.removeAttribute('id'));
|
|
1010
|
+
ghost.inert = true;
|
|
1011
|
+
ghost.setAttribute('aria-hidden', 'true');
|
|
1012
|
+
ghost.setAttribute('data-ty-sortable-ghost', '');
|
|
1013
|
+
const from = item.querySelectorAll('input, textarea, select');
|
|
1014
|
+
ghost.querySelectorAll('input, textarea, select').forEach((n, i) => {
|
|
1015
|
+
const src = from[i];
|
|
1016
|
+
if (src)
|
|
1017
|
+
n.value = src.value;
|
|
1018
|
+
});
|
|
1019
|
+
const cs = getComputedStyle(item);
|
|
1020
|
+
ghost.style.cssText += `;position:absolute;left:0;top:0;margin:0;box-sizing:border-box;width:${rect.width}px;height:${rect.height}px;pointer-events:none;will-change:translate;transform:none;transform-origin:${Math.min(rect.width / 2, 40)}px 50%;color:${cs.color};font:${cs.font}`;
|
|
1021
|
+
return ghost;
|
|
1022
|
+
}
|
|
1023
|
+
function lift(s) {
|
|
1024
|
+
const ghost = s.ghost;
|
|
1025
|
+
if (!ghost)
|
|
1026
|
+
return;
|
|
1027
|
+
const scale = cssVar(s.source, '--ty-sortable-ghost-scale', '1.02');
|
|
1028
|
+
const rotate = cssVar(s.source, '--ty-sortable-ghost-rotate', '0deg');
|
|
1029
|
+
ghost.animate([{ scale: '1', rotate: '0deg' }, { scale, rotate }], { duration: reducedMotion() ? 0 : 150, easing: 'cubic-bezier(.2, 0, 0, 1)', fill: 'forwards' });
|
|
1030
|
+
}
|
|
1031
|
+
function settleGhost(ghost, x, y, ms) {
|
|
1032
|
+
for (const a of ghost.getAnimations()) {
|
|
1033
|
+
a.commitStyles?.();
|
|
1034
|
+
a.cancel();
|
|
1035
|
+
}
|
|
1036
|
+
ghost.style.transition = ms ? `translate ${ms}ms ease, scale ${ms}ms ease, rotate ${ms}ms ease` : '';
|
|
1037
|
+
ghost.getBoundingClientRect();
|
|
1038
|
+
ghost.style.translate = `${x}px ${y}px`;
|
|
1039
|
+
ghost.style.scale = '1';
|
|
1040
|
+
ghost.style.rotate = '0deg';
|
|
1041
|
+
}
|
|
1042
|
+
function markSource(s, how) {
|
|
1043
|
+
const host = s.source;
|
|
1044
|
+
const root = host.shadowRoot;
|
|
1045
|
+
let sheet = sourceSheets.get(host);
|
|
1046
|
+
if (!sheet) {
|
|
1047
|
+
sheet = new CSSStyleSheet();
|
|
1048
|
+
sourceSheets.set(host, sheet);
|
|
1049
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
1050
|
+
}
|
|
1051
|
+
if (!how) {
|
|
1052
|
+
sheet.replaceSync('');
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
const attr = host.getAttribute('key-attr');
|
|
1056
|
+
let sel;
|
|
1057
|
+
if (s.key && attr && s.item.getAttribute(attr) === s.key)
|
|
1058
|
+
sel = `[${CSS.escape(attr)}="${CSS.escape(s.key)}"]`;
|
|
1059
|
+
else if (s.key && s.item.getAttribute('data-key') === s.key)
|
|
1060
|
+
sel = `[data-key="${CSS.escape(s.key)}"]`;
|
|
1061
|
+
else if (s.key && s.item.id === s.key)
|
|
1062
|
+
sel = `#${CSS.escape(s.key)}`;
|
|
1063
|
+
else
|
|
1064
|
+
sel = `:nth-child(${Array.from(host.children).indexOf(s.item) + 1})`;
|
|
1065
|
+
sheet.replaceSync(how === 'hide'
|
|
1066
|
+
? `::slotted(${sel}) { opacity: 0 !important; }`
|
|
1067
|
+
: `::slotted(${sel}) { opacity: var(--ty-sortable-source-opacity) !important; outline: var(--ty-sortable-source-outline); outline-offset: -2px; }`);
|
|
1068
|
+
}
|
|
1069
|
+
const sourceSheets = new WeakMap();
|
|
1070
|
+
function shiftOffset(sh) {
|
|
1071
|
+
if (!sh)
|
|
1072
|
+
return 0;
|
|
1073
|
+
const p = sh.anim.effect?.getComputedTiming().progress;
|
|
1074
|
+
return p == null ? sh.to : sh.from + (sh.to - sh.from) * p;
|
|
1075
|
+
}
|
|
1076
|
+
function baseRect(el, s = session) {
|
|
1077
|
+
const r = el.getBoundingClientRect();
|
|
1078
|
+
const sh = s?.shifts.get(el);
|
|
1079
|
+
const d = shiftOffset(sh);
|
|
1080
|
+
if (!d)
|
|
1081
|
+
return r;
|
|
1082
|
+
const dx = sh.vertical ? 0 : d;
|
|
1083
|
+
const dy = sh.vertical ? d : 0;
|
|
1084
|
+
return { top: r.top - dy, left: r.left - dx, bottom: r.bottom - dy, right: r.right - dx, width: r.width, height: r.height };
|
|
1085
|
+
}
|
|
1086
|
+
function gapOf(c) {
|
|
1087
|
+
const items = c.items;
|
|
1088
|
+
if (items.length < 2)
|
|
1089
|
+
return 0;
|
|
1090
|
+
const a = baseRect(items[0]);
|
|
1091
|
+
const b = baseRect(items[1]);
|
|
1092
|
+
return Math.max(0, c.axis === 'vertical' ? b.top - a.bottom : b.left - a.right);
|
|
1093
|
+
}
|
|
1094
|
+
function listGap(s, c) {
|
|
1095
|
+
if (c.items.length > 1)
|
|
1096
|
+
return gapOf(c);
|
|
1097
|
+
if (s.source.items.length > 1)
|
|
1098
|
+
return gapOf(s.source);
|
|
1099
|
+
const vertical = c.axis === 'vertical';
|
|
1100
|
+
const cs = getComputedStyle(c);
|
|
1101
|
+
const gap = parseFloat(vertical ? cs.rowGap : cs.columnGap);
|
|
1102
|
+
if (gap > 0)
|
|
1103
|
+
return gap;
|
|
1104
|
+
const ms = getComputedStyle(s.item);
|
|
1105
|
+
return Math.max(0, parseFloat(vertical ? ms.marginBottom : ms.marginRight) || 0, parseFloat(vertical ? ms.marginTop : ms.marginLeft) || 0);
|
|
1106
|
+
}
|
|
1107
|
+
function flowKids(c) {
|
|
1108
|
+
return Array.from(c.children).filter((k) => k instanceof HTMLElement && !(k instanceof HTMLTemplateElement));
|
|
1109
|
+
}
|
|
1110
|
+
function slotGeometry(c, item, slot) {
|
|
1111
|
+
const others = othersIn(c, item);
|
|
1112
|
+
const vertical = c.axis === 'vertical';
|
|
1113
|
+
const prev = others[slot - 1] ? baseRect(others[slot - 1]) : null;
|
|
1114
|
+
const next = others[slot] ? baseRect(others[slot]) : null;
|
|
1115
|
+
if (!prev && !next) {
|
|
1116
|
+
const r = c.getBoundingClientRect();
|
|
1117
|
+
const cs = getComputedStyle(c);
|
|
1118
|
+
return { x: r.left + parseFloat(cs.paddingLeft), y: r.top + parseFloat(cs.paddingTop), length: vertical ? r.width : r.height, empty: true };
|
|
1119
|
+
}
|
|
1120
|
+
const gap = gapOf(c) || 4;
|
|
1121
|
+
const ref = (next ?? prev);
|
|
1122
|
+
if (vertical) {
|
|
1123
|
+
const y = prev && next ? (prev.bottom + next.top) / 2 : next ? next.top - gap / 2 : prev.bottom + gap / 2;
|
|
1124
|
+
return { x: ref.left, y, length: ref.width, empty: false };
|
|
1125
|
+
}
|
|
1126
|
+
const x = prev && next ? (prev.right + next.left) / 2 : next ? next.left - gap / 2 : prev.right + gap / 2;
|
|
1127
|
+
return { x, y: ref.top, length: ref.height, empty: false };
|
|
1128
|
+
}
|
|
1129
|
+
function gapOrigin(s, t) {
|
|
1130
|
+
const others = othersIn(t, s.item);
|
|
1131
|
+
const next = others[s.slot] ? baseRect(others[s.slot]) : null;
|
|
1132
|
+
const prev = others[s.slot - 1] ? baseRect(others[s.slot - 1]) : null;
|
|
1133
|
+
const g = slotGeometry(t, s.item, s.slot);
|
|
1134
|
+
if (g.empty)
|
|
1135
|
+
return { x: g.x, y: g.y };
|
|
1136
|
+
const gap = listGap(s, t);
|
|
1137
|
+
if (t.axis === 'vertical')
|
|
1138
|
+
return { x: (next ?? prev).left, y: next ? next.top : prev.bottom + gap };
|
|
1139
|
+
return { x: next ? next.left : prev.right + gap, y: (next ?? prev).top };
|
|
1140
|
+
}
|
|
1141
|
+
function shiftPreview(s) {
|
|
1142
|
+
return !(s.target ?? s.source).hasAttribute('drop-line');
|
|
1143
|
+
}
|
|
1144
|
+
function paintIndicator(s) {
|
|
1145
|
+
const ind = s.indicator;
|
|
1146
|
+
if (!ind)
|
|
1147
|
+
return;
|
|
1148
|
+
const c = s.target;
|
|
1149
|
+
if (!c || s.slot < 0 || s.phase !== 'dragging') {
|
|
1150
|
+
ind.style.display = 'none';
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
if (shiftPreview(s) && s.legal === 'ok') {
|
|
1154
|
+
ind.style.display = 'none';
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
const g = slotGeometry(c, s.item, s.slot);
|
|
1158
|
+
if (g.empty) {
|
|
1159
|
+
ind.style.display = 'none';
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
const size = parseFloat(cssVar(c, '--ty-sortable-indicator-size', '2px')) || 2;
|
|
1163
|
+
const color = s.legal === 'ok'
|
|
1164
|
+
? cssVar(c, '--ty-sortable-indicator-color', '#3b82f6')
|
|
1165
|
+
: cssVar(c, '--ty-sortable-blocked-color', '#ef4444');
|
|
1166
|
+
const vertical = c.axis === 'vertical';
|
|
1167
|
+
ind.style.display = 'block';
|
|
1168
|
+
ind.style.background = color;
|
|
1169
|
+
ind.style.width = vertical ? `${g.length}px` : `${size}px`;
|
|
1170
|
+
ind.style.height = vertical ? `${size}px` : `${g.length}px`;
|
|
1171
|
+
ind.style.transform = vertical
|
|
1172
|
+
? `translate3d(${g.x}px, ${g.y - size / 2}px, 0)`
|
|
1173
|
+
: `translate3d(${g.x - size / 2}px, ${g.y}px, 0)`;
|
|
1174
|
+
}
|
|
1175
|
+
function shiftsFor(s, keepSource = false) {
|
|
1176
|
+
const m = new Map();
|
|
1177
|
+
const t = s.target;
|
|
1178
|
+
if (!t || s.slot < 0 || s.legal !== 'ok')
|
|
1179
|
+
return m;
|
|
1180
|
+
const source = s.source;
|
|
1181
|
+
const kids = flowKids(source);
|
|
1182
|
+
const from = kids.indexOf(s.item);
|
|
1183
|
+
if (from < 0)
|
|
1184
|
+
return m;
|
|
1185
|
+
const along = (c, r) => c.axis === 'vertical' ? [r.top, r.bottom] : [r.left, r.right];
|
|
1186
|
+
const ir = baseRect(s.item);
|
|
1187
|
+
const [iStart, iEnd] = along(source, ir);
|
|
1188
|
+
const size = iEnd - iStart;
|
|
1189
|
+
const others = othersIn(source, s.item);
|
|
1190
|
+
if (t === source) {
|
|
1191
|
+
const fromSlot = source.items.indexOf(s.item);
|
|
1192
|
+
if (s.slot === fromSlot)
|
|
1193
|
+
return m;
|
|
1194
|
+
const step = size + listGap(s, source);
|
|
1195
|
+
if (s.slot > fromSlot) {
|
|
1196
|
+
const last = others[s.slot - 1];
|
|
1197
|
+
const passed = kids.slice(from + 1, kids.indexOf(last) + 1);
|
|
1198
|
+
passed.forEach((k) => m.set(k, -step));
|
|
1199
|
+
m.set(s.item, along(source, baseRect(last))[1] - iEnd);
|
|
1200
|
+
}
|
|
1201
|
+
else {
|
|
1202
|
+
const first = others[s.slot];
|
|
1203
|
+
const passed = kids.slice(kids.indexOf(first), from);
|
|
1204
|
+
passed.forEach((k) => m.set(k, step));
|
|
1205
|
+
m.set(s.item, along(source, baseRect(first))[0] - iStart);
|
|
1206
|
+
}
|
|
1207
|
+
return m;
|
|
1208
|
+
}
|
|
1209
|
+
if (!keepSource) {
|
|
1210
|
+
const close = -(size + listGap(s, source));
|
|
1211
|
+
kids.slice(from + 1).forEach((k) => m.set(k, close));
|
|
1212
|
+
}
|
|
1213
|
+
const tKids = flowKids(t);
|
|
1214
|
+
const tOthers = othersIn(t, s.item);
|
|
1215
|
+
const start = s.slot < tOthers.length
|
|
1216
|
+
? tKids.indexOf(tOthers[s.slot])
|
|
1217
|
+
: tOthers.length ? tKids.indexOf(tOthers[tOthers.length - 1]) + 1 : tKids.length;
|
|
1218
|
+
const open = (t.axis === 'vertical' ? ir.height : ir.width) + listGap(s, t);
|
|
1219
|
+
tKids.slice(start).forEach((k) => m.set(k, open));
|
|
1220
|
+
return m;
|
|
1221
|
+
}
|
|
1222
|
+
const SHIFT_EASING = 'cubic-bezier(.2, 0, 0, 1)';
|
|
1223
|
+
function applyShifts(s, want, ms) {
|
|
1224
|
+
for (const el of new Set([...s.shifts.keys(), ...want.keys()])) {
|
|
1225
|
+
const to = want.get(el) ?? 0;
|
|
1226
|
+
const cur = s.shifts.get(el);
|
|
1227
|
+
if ((cur?.to ?? 0) === to)
|
|
1228
|
+
continue;
|
|
1229
|
+
const list = el.parentElement instanceof TySortable ? el.parentElement : s.source;
|
|
1230
|
+
const vertical = list.axis === 'vertical';
|
|
1231
|
+
const from = shiftOffset(cur);
|
|
1232
|
+
const at = (d) => (vertical ? `0 ${d}px` : `${d}px 0`);
|
|
1233
|
+
const anim = el.animate([{ translate: at(from) }, { translate: at(to) }], { duration: ms, easing: SHIFT_EASING, fill: 'forwards', composite: 'add' });
|
|
1234
|
+
cur?.anim.cancel();
|
|
1235
|
+
s.shifts.set(el, { from, to, vertical, anim });
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
function cancelShifts(s) {
|
|
1239
|
+
s.shifts.forEach(({ anim }) => anim.cancel());
|
|
1240
|
+
s.shifts.clear();
|
|
1241
|
+
}
|
|
1242
|
+
function previewShifts(s) {
|
|
1243
|
+
if (s.phase !== 'dragging')
|
|
1244
|
+
return;
|
|
1245
|
+
const ms = Math.min(duration(s.source), 200);
|
|
1246
|
+
if (!shiftPreview(s)) {
|
|
1247
|
+
if (s.shifts.size)
|
|
1248
|
+
applyShifts(s, new Map(), ms);
|
|
1249
|
+
markSource(s, 'dim');
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
const crossing = !!s.target && s.target !== s.source && s.legal === 'ok';
|
|
1253
|
+
const keyboard = s.mode !== 'pointer';
|
|
1254
|
+
markSource(s, crossing && !keyboard ? 'hide' : 'dim');
|
|
1255
|
+
applyShifts(s, shiftsFor(s, keyboard), ms);
|
|
1256
|
+
if (keyboard) {
|
|
1257
|
+
if (crossing) {
|
|
1258
|
+
if (!s.ghost && s.layer)
|
|
1259
|
+
addGhost(s, s.item.getBoundingClientRect());
|
|
1260
|
+
const o = gapOrigin(s, s.target);
|
|
1261
|
+
s.ghost.style.display = '';
|
|
1262
|
+
settleGhost(s.ghost, o.x, o.y, ms);
|
|
1263
|
+
}
|
|
1264
|
+
else if (s.ghost) {
|
|
1265
|
+
s.ghost.style.display = 'none';
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
function clearStates(s) {
|
|
1270
|
+
for (const c of new Set([s.source, s.target, ...members(s.group, s.source)])) {
|
|
1271
|
+
if (!c)
|
|
1272
|
+
continue;
|
|
1273
|
+
for (const st of ['over', 'blocked', 'dragging', 'pending'])
|
|
1274
|
+
setState(c, st, false);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
function teardown(s) {
|
|
1278
|
+
cancelAnimationFrame(s.raf);
|
|
1279
|
+
clearTimeout(s.timer);
|
|
1280
|
+
s.cleanup.forEach((fn) => fn());
|
|
1281
|
+
s.cleanup = [];
|
|
1282
|
+
s.observers.forEach((o) => o.disconnect());
|
|
1283
|
+
s.observers = [];
|
|
1284
|
+
cancelShifts(s);
|
|
1285
|
+
s.layer?.remove();
|
|
1286
|
+
s.cursorMarker?.remove();
|
|
1287
|
+
s.cursorMarker = null;
|
|
1288
|
+
if (session === s)
|
|
1289
|
+
session = null;
|
|
1290
|
+
if (!session || session.source !== s.source)
|
|
1291
|
+
markSource(s, false);
|
|
1292
|
+
}
|
|
1293
|
+
function flyBack(s, el, done) {
|
|
1294
|
+
const to = el?.isConnected ? baseRect(el, s) : null;
|
|
1295
|
+
const ghost = s.ghost;
|
|
1296
|
+
const ms = duration(s.source);
|
|
1297
|
+
if (s.indicator)
|
|
1298
|
+
s.indicator.style.display = 'none';
|
|
1299
|
+
setCursor(s, null);
|
|
1300
|
+
if (s.shifts.size) {
|
|
1301
|
+
applyShifts(s, new Map(), ms);
|
|
1302
|
+
if (s.source.items.includes(s.item))
|
|
1303
|
+
markSource(s, 'dim');
|
|
1304
|
+
}
|
|
1305
|
+
if (!ghost || !to || !ms) {
|
|
1306
|
+
if (ms && s.shifts.size)
|
|
1307
|
+
window.setTimeout(done, ms);
|
|
1308
|
+
else
|
|
1309
|
+
done();
|
|
1310
|
+
return;
|
|
1311
|
+
}
|
|
1312
|
+
settleGhost(ghost, to.left, to.top, ms);
|
|
1313
|
+
window.setTimeout(done, ms);
|
|
1314
|
+
}
|
|
1315
|
+
function cancelDrag(reason) {
|
|
1316
|
+
const s = session;
|
|
1317
|
+
if (!s || s.phase !== 'dragging')
|
|
1318
|
+
return;
|
|
1319
|
+
cancelAnimationFrame(s.raf);
|
|
1320
|
+
session = null;
|
|
1321
|
+
if (s.mode === 'pointer' && (reason === 'escape' || reason === 'gone' || reason === 'disabled'))
|
|
1322
|
+
suppressNextRelease(s.pointerId);
|
|
1323
|
+
const source = s.source.isConnected ? s.source : resolveContainer(s.source, s.sourceId, s.group) ?? s.source;
|
|
1324
|
+
source.dispatchEvent(new CustomEvent('ty-sortable-cancel', {
|
|
1325
|
+
detail: { value: dragValue(s), item: s.item, reason },
|
|
1326
|
+
}));
|
|
1327
|
+
if (s.mode === 'keyboard' && reason !== 'gone') {
|
|
1328
|
+
source._announce('cancelled', { item: itemLabel(source, s.item), position: fromPos(s).index + 1, list: listLabel(source) });
|
|
1329
|
+
}
|
|
1330
|
+
s.cleanup.forEach((fn) => fn());
|
|
1331
|
+
s.cleanup = [];
|
|
1332
|
+
clearStates(s);
|
|
1333
|
+
flyBack(s, s.item.isConnected ? s.item : null, () => teardown(s));
|
|
1334
|
+
}
|
|
1335
|
+
function startKeyboard(source, item) {
|
|
1336
|
+
const s = newSession('keyboard', source, item);
|
|
1337
|
+
session = s;
|
|
1338
|
+
createLayer(s, item.getBoundingClientRect());
|
|
1339
|
+
markSource(s, 'dim');
|
|
1340
|
+
setState(source, 'dragging', true);
|
|
1341
|
+
addEventListener('keydown', onKeyboardKey, true);
|
|
1342
|
+
addEventListener('pointerdown', onKeyboardPointer, true);
|
|
1343
|
+
s.cleanup.push(() => {
|
|
1344
|
+
removeEventListener('keydown', onKeyboardKey, true);
|
|
1345
|
+
removeEventListener('pointerdown', onKeyboardPointer, true);
|
|
1346
|
+
});
|
|
1347
|
+
dispatchStart(s);
|
|
1348
|
+
setTargetOf(s, source);
|
|
1349
|
+
s.slot = s.fromIndex;
|
|
1350
|
+
s.legal = 'ok';
|
|
1351
|
+
setState(source, 'over', true);
|
|
1352
|
+
paintIndicator(s);
|
|
1353
|
+
source._announce('pickedUp', {
|
|
1354
|
+
item: itemLabel(source, item),
|
|
1355
|
+
position: s.fromIndex + 1,
|
|
1356
|
+
total: source.items.length,
|
|
1357
|
+
list: listLabel(source),
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
function onKeyboardKey(e) {
|
|
1361
|
+
const s = session;
|
|
1362
|
+
if (!s || s.mode !== 'keyboard' || s.phase !== 'dragging')
|
|
1363
|
+
return;
|
|
1364
|
+
if (!checkKeyboard())
|
|
1365
|
+
return;
|
|
1366
|
+
keyboardMove(s, e);
|
|
1367
|
+
}
|
|
1368
|
+
function onKeyboardPointer() {
|
|
1369
|
+
if (session?.mode === 'keyboard' && session.phase === 'dragging')
|
|
1370
|
+
cancelDrag('blur');
|
|
1371
|
+
}
|
|
1372
|
+
function checkKeyboard() {
|
|
1373
|
+
const s = session;
|
|
1374
|
+
if (!s || s.mode !== 'keyboard' || s.phase !== 'dragging')
|
|
1375
|
+
return false;
|
|
1376
|
+
const before = s.item;
|
|
1377
|
+
const hadFocus = !document.activeElement || document.activeElement === document.body || before.contains(document.activeElement);
|
|
1378
|
+
if (!revalidate(s) || !s.target) {
|
|
1379
|
+
cancelDrag('gone');
|
|
1380
|
+
return false;
|
|
1381
|
+
}
|
|
1382
|
+
if (s.item !== before && hadFocus)
|
|
1383
|
+
(handleOf(s.source, s.item) ?? s.item).focus({ preventScroll: true });
|
|
1384
|
+
s.slot = clampLocked(s.target, s.item, Math.min(s.slot, othersIn(s.target, s.item).length));
|
|
1385
|
+
paintIndicator(s);
|
|
1386
|
+
return true;
|
|
1387
|
+
}
|
|
1388
|
+
function keyboardMove(s, e) {
|
|
1389
|
+
const target = s.target ?? s.source;
|
|
1390
|
+
const vertical = target.axis === 'vertical';
|
|
1391
|
+
const back = vertical ? 'ArrowUp' : 'ArrowLeft';
|
|
1392
|
+
const fwd = vertical ? 'ArrowDown' : 'ArrowRight';
|
|
1393
|
+
const prevList = vertical ? 'ArrowLeft' : 'ArrowUp';
|
|
1394
|
+
const nextList = vertical ? 'ArrowRight' : 'ArrowDown';
|
|
1395
|
+
const count = othersIn(target, s.item).length;
|
|
1396
|
+
switch (e.key) {
|
|
1397
|
+
case back:
|
|
1398
|
+
case fwd:
|
|
1399
|
+
case 'Home':
|
|
1400
|
+
case 'End': {
|
|
1401
|
+
e.preventDefault();
|
|
1402
|
+
const want = e.key === back ? s.slot - 1 : e.key === fwd ? s.slot + 1 : e.key === 'Home' ? 0 : count;
|
|
1403
|
+
const slot = clampLocked(target, s.item, Math.max(0, Math.min(want, count)));
|
|
1404
|
+
setTarget(s, target, slot, legality(s, target, slot));
|
|
1405
|
+
revealSlot(s);
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
case prevList:
|
|
1409
|
+
case nextList: {
|
|
1410
|
+
e.preventDefault();
|
|
1411
|
+
const lists = members(s.group, s.source)
|
|
1412
|
+
.filter((c) => !c.disabled || c === s.source)
|
|
1413
|
+
.sort((a, b) => {
|
|
1414
|
+
const ra = a.getBoundingClientRect();
|
|
1415
|
+
const rb = b.getBoundingClientRect();
|
|
1416
|
+
return vertical ? ra.left - rb.left || ra.top - rb.top : ra.top - rb.top || ra.left - rb.left;
|
|
1417
|
+
});
|
|
1418
|
+
const i = lists.indexOf(target);
|
|
1419
|
+
const dir = e.key === nextList ? 1 : -1;
|
|
1420
|
+
for (let j = i + dir; j >= 0 && j < lists.length; j += dir) {
|
|
1421
|
+
const c = lists[j];
|
|
1422
|
+
const slot = clampLocked(c, s.item, Math.min(s.slot, othersIn(c, s.item).length));
|
|
1423
|
+
if (legality(s, c, slot) === 'ok') {
|
|
1424
|
+
setTarget(s, c, slot, 'ok');
|
|
1425
|
+
revealSlot(s);
|
|
1426
|
+
return;
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
case ' ':
|
|
1432
|
+
case 'Enter':
|
|
1433
|
+
e.preventDefault();
|
|
1434
|
+
e.stopPropagation();
|
|
1435
|
+
keyHandledAt = performance.now();
|
|
1436
|
+
droppedBy = e;
|
|
1437
|
+
dropKeyboard(s);
|
|
1438
|
+
return;
|
|
1439
|
+
case 'Escape':
|
|
1440
|
+
e.preventDefault();
|
|
1441
|
+
e.stopPropagation();
|
|
1442
|
+
cancelDrag('escape');
|
|
1443
|
+
return;
|
|
1444
|
+
case 'Tab':
|
|
1445
|
+
cancelDrag('blur');
|
|
1446
|
+
return;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
function dropKeyboard(s) {
|
|
1450
|
+
if (!s.target || s.legal !== 'ok')
|
|
1451
|
+
cancelDrag('blocked');
|
|
1452
|
+
else
|
|
1453
|
+
commit();
|
|
1454
|
+
}
|
|
1455
|
+
function revealSlot(s) {
|
|
1456
|
+
const c = s.target;
|
|
1457
|
+
if (!c)
|
|
1458
|
+
return;
|
|
1459
|
+
const others = othersIn(c, s.item);
|
|
1460
|
+
const near = others[s.slot - 1] ?? others[s.slot] ?? c;
|
|
1461
|
+
near.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
1462
|
+
requestAnimationFrame(() => { if (session === s)
|
|
1463
|
+
paintIndicator(s); });
|
|
1464
|
+
}
|
|
1465
|
+
function liveSource(s) {
|
|
1466
|
+
return resolveContainer(s.source, s.sourceId, s.group) ?? s.source;
|
|
1467
|
+
}
|
|
1468
|
+
function liveTarget(s) {
|
|
1469
|
+
return (s.target && resolveContainer(s.target, s.targetId, s.group)) ?? s.target ?? liveSource(s);
|
|
1470
|
+
}
|
|
1471
|
+
function holdLists(s) {
|
|
1472
|
+
return s.group ? members(s.group, liveSource(s)) : [...new Set([liveSource(s), liveTarget(s)])];
|
|
1473
|
+
}
|
|
1474
|
+
function groupSnapshot(s) {
|
|
1475
|
+
const map = new Map();
|
|
1476
|
+
for (const c of holdLists(s))
|
|
1477
|
+
map.set(c.id || c, c.items.map((i) => c.keyOf(i) ?? i));
|
|
1478
|
+
return map;
|
|
1479
|
+
}
|
|
1480
|
+
function snapshotChanged(s) {
|
|
1481
|
+
const now = groupSnapshot(s);
|
|
1482
|
+
const before = s.snapshot;
|
|
1483
|
+
if (now.size !== before.size)
|
|
1484
|
+
return true;
|
|
1485
|
+
for (const [k, tokens] of before) {
|
|
1486
|
+
const t = now.get(k);
|
|
1487
|
+
if (!t || !sameTokens(t, tokens))
|
|
1488
|
+
return true;
|
|
1489
|
+
}
|
|
1490
|
+
return false;
|
|
1491
|
+
}
|
|
1492
|
+
function observeForHold(s, c) {
|
|
1493
|
+
if (s.observed.has(c))
|
|
1494
|
+
return;
|
|
1495
|
+
s.observed.add(c);
|
|
1496
|
+
const o = new MutationObserver(() => evaluateHold('mutation'));
|
|
1497
|
+
o.observe(c, { childList: true });
|
|
1498
|
+
const a = new MutationObserver(() => watchBusy());
|
|
1499
|
+
a.observe(c, { attributes: true, attributeFilter: ['aria-busy', 'class'] });
|
|
1500
|
+
s.observers.push(o, a);
|
|
1501
|
+
}
|
|
1502
|
+
function buildValue(s) {
|
|
1503
|
+
const target = s.target;
|
|
1504
|
+
const from = fromPos(s);
|
|
1505
|
+
const others = othersIn(target, s.item);
|
|
1506
|
+
const to = {
|
|
1507
|
+
list: target.id || null,
|
|
1508
|
+
index: s.slot,
|
|
1509
|
+
prev: others[s.slot - 1] ? target.keyOf(others[s.slot - 1]) : null,
|
|
1510
|
+
next: others[s.slot] ? target.keyOf(others[s.slot]) : null,
|
|
1511
|
+
};
|
|
1512
|
+
const order = {};
|
|
1513
|
+
const keys = (c, list) => list.map((i) => c.keyOf(i));
|
|
1514
|
+
if (target !== s.source && s.source.id)
|
|
1515
|
+
order[s.source.id] = keys(s.source, othersIn(s.source, s.item));
|
|
1516
|
+
if (target.id) {
|
|
1517
|
+
const after = keys(target, others);
|
|
1518
|
+
after.splice(s.slot, 0, s.key);
|
|
1519
|
+
order[target.id] = after;
|
|
1520
|
+
}
|
|
1521
|
+
return { key: s.key, group: s.group, source: s.mode, from, to, order };
|
|
1522
|
+
}
|
|
1523
|
+
function commit() {
|
|
1524
|
+
const s = session;
|
|
1525
|
+
const target = s.target;
|
|
1526
|
+
cancelAnimationFrame(s.raf);
|
|
1527
|
+
s.cleanup.forEach((fn) => fn());
|
|
1528
|
+
s.cleanup = [];
|
|
1529
|
+
if (target === s.source && s.slot === s.source.items.indexOf(s.item)) {
|
|
1530
|
+
session = null;
|
|
1531
|
+
clearStates(s);
|
|
1532
|
+
if (s.mode === 'keyboard') {
|
|
1533
|
+
s.source._announce('dropped', { item: itemLabel(s.source, s.item), position: s.slot + 1, total: target.items.length, list: listLabel(target) });
|
|
1534
|
+
}
|
|
1535
|
+
flyBack(s, s.item, () => teardown(s));
|
|
1536
|
+
return false;
|
|
1537
|
+
}
|
|
1538
|
+
const value = buildValue(s);
|
|
1539
|
+
s.value = value;
|
|
1540
|
+
s.snapshot = groupSnapshot(s);
|
|
1541
|
+
s.phase = 'holding';
|
|
1542
|
+
s.holdStart = performance.now();
|
|
1543
|
+
s.focus = document.activeElement;
|
|
1544
|
+
for (const c of holdLists(s)) {
|
|
1545
|
+
setState(c, 'over', false);
|
|
1546
|
+
setState(c, 'blocked', false);
|
|
1547
|
+
setState(c, 'dragging', false);
|
|
1548
|
+
setState(c, 'pending', true);
|
|
1549
|
+
}
|
|
1550
|
+
holdVisual(s);
|
|
1551
|
+
notePainted(s);
|
|
1552
|
+
const track = () => {
|
|
1553
|
+
if (session !== s || s.phase !== 'holding')
|
|
1554
|
+
return;
|
|
1555
|
+
notePainted(s);
|
|
1556
|
+
requestAnimationFrame(track);
|
|
1557
|
+
};
|
|
1558
|
+
requestAnimationFrame(track);
|
|
1559
|
+
for (const c of holdLists(s))
|
|
1560
|
+
observeForHold(s, c);
|
|
1561
|
+
s.busy = isBusy(s);
|
|
1562
|
+
const event = new CustomEvent('ty-sortable-change', {
|
|
1563
|
+
detail: { value, item: s.item, fromContainer: s.source, toContainer: target },
|
|
1564
|
+
bubbles: true,
|
|
1565
|
+
cancelable: true,
|
|
1566
|
+
});
|
|
1567
|
+
target.dispatchEvent(event);
|
|
1568
|
+
if (session !== s || s.phase !== 'holding')
|
|
1569
|
+
return true;
|
|
1570
|
+
if (event.defaultPrevented) {
|
|
1571
|
+
release('rejected');
|
|
1572
|
+
return true;
|
|
1573
|
+
}
|
|
1574
|
+
s.busy = isBusy(s);
|
|
1575
|
+
s.htmx = s.busy && holdLists(s).some((c) => c.classList.contains('htmx-request'));
|
|
1576
|
+
armTimer(s);
|
|
1577
|
+
return true;
|
|
1578
|
+
}
|
|
1579
|
+
function holdVisual(s) {
|
|
1580
|
+
const target = s.target;
|
|
1581
|
+
if (!s.layer)
|
|
1582
|
+
createLayer(s, s.item.getBoundingClientRect());
|
|
1583
|
+
if (!s.ghost && s.layer) {
|
|
1584
|
+
addGhost(s, s.item.getBoundingClientRect());
|
|
1585
|
+
s.ghost.getBoundingClientRect();
|
|
1586
|
+
}
|
|
1587
|
+
s.ghost.style.display = '';
|
|
1588
|
+
setCursor(s, null);
|
|
1589
|
+
if (s.indicator)
|
|
1590
|
+
s.indicator.style.display = 'none';
|
|
1591
|
+
const ms = duration(target);
|
|
1592
|
+
const vertical = target.axis === 'vertical';
|
|
1593
|
+
const keep = s.mode !== 'pointer';
|
|
1594
|
+
const want = shiftsFor(s, keep);
|
|
1595
|
+
let x;
|
|
1596
|
+
let y;
|
|
1597
|
+
if (target === s.source) {
|
|
1598
|
+
const r = baseRect(s.item);
|
|
1599
|
+
const d = want.get(s.item) ?? 0;
|
|
1600
|
+
x = r.left + (vertical ? 0 : d);
|
|
1601
|
+
y = r.top + (vertical ? d : 0);
|
|
1602
|
+
}
|
|
1603
|
+
else {
|
|
1604
|
+
;
|
|
1605
|
+
({ x, y } = gapOrigin(s, target));
|
|
1606
|
+
markSource(s, keep ? 'dim' : 'hide');
|
|
1607
|
+
}
|
|
1608
|
+
applyShifts(s, want, ms);
|
|
1609
|
+
settleGhost(s.ghost, x, y, ms);
|
|
1610
|
+
}
|
|
1611
|
+
function isBusy(s) {
|
|
1612
|
+
return holdLists(s).some((c) => c.getAttribute('aria-busy') === 'true' || c.classList.contains('htmx-request'));
|
|
1613
|
+
}
|
|
1614
|
+
function watchBusy() {
|
|
1615
|
+
const s = session;
|
|
1616
|
+
if (!s || s.phase !== 'holding')
|
|
1617
|
+
return;
|
|
1618
|
+
const busy = isBusy(s);
|
|
1619
|
+
if (s.busy && !busy) {
|
|
1620
|
+
s.busy = false;
|
|
1621
|
+
const decide = () => {
|
|
1622
|
+
if (session !== s || s.busy)
|
|
1623
|
+
return;
|
|
1624
|
+
s.observers.forEach((o) => o.takeRecords());
|
|
1625
|
+
evaluateHold('idle');
|
|
1626
|
+
};
|
|
1627
|
+
if (s.htmx) {
|
|
1628
|
+
s.htmx = false;
|
|
1629
|
+
const done = (e) => {
|
|
1630
|
+
const t = e?.target;
|
|
1631
|
+
if (t instanceof Element && t.isConnected && !holdLists(s).some((c) => t.contains(c) || c.contains(t)))
|
|
1632
|
+
return;
|
|
1633
|
+
document.removeEventListener('htmx:afterSettle', done);
|
|
1634
|
+
clearTimeout(cap);
|
|
1635
|
+
requestAnimationFrame(decide);
|
|
1636
|
+
};
|
|
1637
|
+
const cap = window.setTimeout(done, HTMX_SETTLE_CAP);
|
|
1638
|
+
document.addEventListener('htmx:afterSettle', done);
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
requestAnimationFrame(() => requestAnimationFrame(() => {
|
|
1642
|
+
window.setTimeout(decide, IDLE_GRACE);
|
|
1643
|
+
}));
|
|
1644
|
+
}
|
|
1645
|
+
else if (busy) {
|
|
1646
|
+
s.busy = true;
|
|
1647
|
+
s.htmx = holdLists(s).some((c) => c.classList.contains('htmx-request'));
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
function armTimer(s) {
|
|
1651
|
+
clearTimeout(s.timer);
|
|
1652
|
+
const timeout = s.source.holdTimeout;
|
|
1653
|
+
s.timer = window.setTimeout(function tick() {
|
|
1654
|
+
if (session !== s || s.phase !== 'holding')
|
|
1655
|
+
return;
|
|
1656
|
+
if (s.busy && performance.now() - s.holdStart < BUSY_CAP) {
|
|
1657
|
+
s.timer = window.setTimeout(tick, 250);
|
|
1658
|
+
return;
|
|
1659
|
+
}
|
|
1660
|
+
release('unknown');
|
|
1661
|
+
}, timeout);
|
|
1662
|
+
}
|
|
1663
|
+
function locate(s) {
|
|
1664
|
+
let found = null;
|
|
1665
|
+
let count = 0;
|
|
1666
|
+
for (const c of holdLists(s)) {
|
|
1667
|
+
const items = c.items;
|
|
1668
|
+
items.forEach((el, i) => {
|
|
1669
|
+
if (s.key ? c.keyOf(el) !== s.key : el !== s.item)
|
|
1670
|
+
return;
|
|
1671
|
+
count++;
|
|
1672
|
+
if (found && c !== liveTarget(s))
|
|
1673
|
+
return;
|
|
1674
|
+
found = {
|
|
1675
|
+
list: c,
|
|
1676
|
+
index: i,
|
|
1677
|
+
prev: items[i - 1] ? c.keyOf(items[i - 1]) : null,
|
|
1678
|
+
next: items[i + 1] ? c.keyOf(items[i + 1]) : null,
|
|
1679
|
+
};
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
return found ? { ...found, count } : null;
|
|
1683
|
+
}
|
|
1684
|
+
function evaluateHold(trigger) {
|
|
1685
|
+
const s = session;
|
|
1686
|
+
if (!s || s.phase !== 'holding')
|
|
1687
|
+
return;
|
|
1688
|
+
const settled = trigger !== 'mutation';
|
|
1689
|
+
if (!snapshotChanged(s)) {
|
|
1690
|
+
if (settled)
|
|
1691
|
+
release('rejected');
|
|
1692
|
+
return;
|
|
1693
|
+
}
|
|
1694
|
+
const v = s.value;
|
|
1695
|
+
const at = locate(s);
|
|
1696
|
+
if (!at) {
|
|
1697
|
+
release('reconciled');
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
const target = liveTarget(s);
|
|
1701
|
+
const source = liveSource(s);
|
|
1702
|
+
if (at.count === 1 && at.list === target && ((at.prev === v.to.prev && at.next === v.to.next) || at.index === v.to.index)) {
|
|
1703
|
+
release('accepted');
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
const atFrom = at.count === 1 && at.list === source && at.prev === v.from.prev && at.next === v.from.next;
|
|
1707
|
+
if (atFrom) {
|
|
1708
|
+
if (settled)
|
|
1709
|
+
release('rejected');
|
|
1710
|
+
else
|
|
1711
|
+
dropRenderedShifts(s);
|
|
1712
|
+
return;
|
|
1713
|
+
}
|
|
1714
|
+
if (at.count > 1 && !settled) {
|
|
1715
|
+
dropRenderedShifts(s);
|
|
1716
|
+
return;
|
|
1717
|
+
}
|
|
1718
|
+
release('reconciled');
|
|
1719
|
+
}
|
|
1720
|
+
function notePainted(s) {
|
|
1721
|
+
s.painted.clear();
|
|
1722
|
+
for (const el of s.shifts.keys()) {
|
|
1723
|
+
const list = el.parentElement instanceof TySortable ? el.parentElement : null;
|
|
1724
|
+
const k = list?.keyOf(el);
|
|
1725
|
+
if (k && k !== s.key && el.isConnected)
|
|
1726
|
+
s.painted.set(k, el.getBoundingClientRect());
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
function dropRenderedShifts(s) {
|
|
1730
|
+
const before = s.snapshot;
|
|
1731
|
+
for (const c of holdLists(s)) {
|
|
1732
|
+
const was = before.get(c.id || c);
|
|
1733
|
+
if (was && sameTokens(was, c.items.map((i) => c.keyOf(i) ?? i)))
|
|
1734
|
+
continue;
|
|
1735
|
+
for (const [el, sh] of s.shifts) {
|
|
1736
|
+
if (el.parentElement !== c)
|
|
1737
|
+
continue;
|
|
1738
|
+
sh.anim.cancel();
|
|
1739
|
+
s.shifts.delete(el);
|
|
1740
|
+
const k = c.keyOf(el);
|
|
1741
|
+
if (k)
|
|
1742
|
+
s.painted.delete(k);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
function release(outcome) {
|
|
1747
|
+
const s = session;
|
|
1748
|
+
if (!s || s.phase !== 'holding')
|
|
1749
|
+
return;
|
|
1750
|
+
session = null;
|
|
1751
|
+
clearTimeout(s.timer);
|
|
1752
|
+
s.observers.forEach((o) => o.disconnect());
|
|
1753
|
+
s.observers = [];
|
|
1754
|
+
clearStates(s);
|
|
1755
|
+
const value = s.value;
|
|
1756
|
+
const target = liveTarget(s);
|
|
1757
|
+
const source = liveSource(s);
|
|
1758
|
+
const keyed = (c) => (s.key ? c.items.find((i) => c.keyOf(i) === s.key) ?? null : null);
|
|
1759
|
+
const lists = holdLists(s);
|
|
1760
|
+
const keyboard = s.mode !== 'pointer';
|
|
1761
|
+
const findNow = () => {
|
|
1762
|
+
for (const c of [target, ...lists]) {
|
|
1763
|
+
const el = keyed(c) ?? (!s.key && s.item.parentElement === c ? s.item : null);
|
|
1764
|
+
if (el)
|
|
1765
|
+
return { list: c, el };
|
|
1766
|
+
}
|
|
1767
|
+
return null;
|
|
1768
|
+
};
|
|
1769
|
+
const unchanged = outcome === 'unknown' && !snapshotChanged(s);
|
|
1770
|
+
if (outcome === 'rejected' || unchanged) {
|
|
1771
|
+
const back = keyed(source) ?? (s.item.isConnected ? s.item : null);
|
|
1772
|
+
if (keyboard) {
|
|
1773
|
+
if (outcome === 'rejected')
|
|
1774
|
+
source._announce('rejected', { item: itemLabel(source, s.item), position: value.from.index + 1, list: listLabel(source) });
|
|
1775
|
+
else
|
|
1776
|
+
source._announce('unknown', { item: itemLabel(source, s.item) });
|
|
1777
|
+
if (back)
|
|
1778
|
+
restoreFocus(s, source, back);
|
|
1779
|
+
}
|
|
1780
|
+
flyBack(s, back, () => teardown(s));
|
|
1781
|
+
}
|
|
1782
|
+
else {
|
|
1783
|
+
const ghostRect = s.ghost?.getBoundingClientRect() ?? null;
|
|
1784
|
+
const seen = s.painted;
|
|
1785
|
+
teardown(s);
|
|
1786
|
+
const now = findNow();
|
|
1787
|
+
const ms = duration(target);
|
|
1788
|
+
const glide = (el, from) => {
|
|
1789
|
+
const r = el.getBoundingClientRect();
|
|
1790
|
+
const dx = from.left - r.left;
|
|
1791
|
+
const dy = from.top - r.top;
|
|
1792
|
+
if (Math.abs(dx) < 0.5 && Math.abs(dy) < 0.5)
|
|
1793
|
+
return;
|
|
1794
|
+
el.animate([{ translate: `${dx}px ${dy}px` }, { translate: '0 0' }], { duration: ms, easing: SHIFT_EASING, composite: 'add' });
|
|
1795
|
+
};
|
|
1796
|
+
if (ms && outcome !== 'unknown') {
|
|
1797
|
+
if (now && ghostRect && outcome === 'accepted')
|
|
1798
|
+
glide(now.el, ghostRect);
|
|
1799
|
+
for (const c of lists) {
|
|
1800
|
+
for (const el of c.items) {
|
|
1801
|
+
const k = c.keyOf(el);
|
|
1802
|
+
const from = k ? seen.get(k) : undefined;
|
|
1803
|
+
if (from)
|
|
1804
|
+
glide(el, from);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
}
|
|
1808
|
+
if (now && outcome !== 'unknown' && now.list.hasAttribute('flash'))
|
|
1809
|
+
flash(now.list, now.el);
|
|
1810
|
+
if (keyboard) {
|
|
1811
|
+
const name = itemLabel(source, now?.el ?? s.item);
|
|
1812
|
+
if (now) {
|
|
1813
|
+
const items = now.list.items;
|
|
1814
|
+
const vars = { item: name, position: items.indexOf(now.el) + 1, total: items.length, list: listLabel(now.list) };
|
|
1815
|
+
now.list._announce(outcome === 'accepted' ? 'dropped' : outcome === 'unknown' ? 'unknown' : 'reconciled', vars);
|
|
1816
|
+
restoreFocus(s, now.list, now.el);
|
|
1817
|
+
}
|
|
1818
|
+
else {
|
|
1819
|
+
source._announce(outcome === 'unknown' ? 'unknown' : 'removed', { item: name });
|
|
1820
|
+
const near = source.items[Math.min(value.from.index, source.items.length - 1)];
|
|
1821
|
+
if (near)
|
|
1822
|
+
restoreFocus(s, source, near);
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
target.dispatchEvent(new CustomEvent('ty-sortable-settle', { detail: { value, outcome } }));
|
|
1827
|
+
s.resolve?.(outcome);
|
|
1828
|
+
}
|
|
1829
|
+
function flash(c, el) {
|
|
1830
|
+
const color = cssVar(c, '--ty-sortable-flash-color', cssVar(c, '--ty-sortable-indicator-color', '#3b82f6'));
|
|
1831
|
+
el.animate([{ outline: `2px solid ${color}`, outlineOffset: '-2px' }, { outline: '2px solid transparent', outlineOffset: '-2px' }], { duration: 700, easing: 'ease-out' });
|
|
1832
|
+
}
|
|
1833
|
+
function restoreFocus(s, c, item) {
|
|
1834
|
+
const active = document.activeElement;
|
|
1835
|
+
const lost = !active || active === document.body || !s.focus || !s.focus.isConnected;
|
|
1836
|
+
if (!lost && active !== s.focus)
|
|
1837
|
+
return;
|
|
1838
|
+
if (active && item.contains(active))
|
|
1839
|
+
return;
|
|
1840
|
+
(handleOf(c, item) ?? item).focus({ preventScroll: true });
|
|
1841
|
+
}
|
|
1842
|
+
if (!customElements.get('ty-sortable')) {
|
|
1843
|
+
customElements.define('ty-sortable', TySortable);
|
|
1844
|
+
}
|