@mk-drag-and-drop/dom 0.4.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +203 -476
- package/dist/controller/create-drag-controller.d.ts +1 -0
- package/dist/controller/create-drag-controller.js +3 -0
- package/dist/index.d.ts +1 -1
- package/dist/integration/index.d.ts +1 -1
- package/dist/runtime/drag-runtime-scope.d.ts +2 -0
- package/dist/runtime/drag-runtime-scope.js +2 -0
- package/dist/runtime/drag-runtime.d.ts +4 -1
- package/dist/runtime/drag-runtime.js +162 -71
- package/dist/runtime/drop-target-registry.d.ts +3 -10
- package/dist/runtime/drop-target-registry.js +52 -96
- package/dist/runtime/lifecycle.d.ts +15 -0
- package/dist/runtime/sortable-coordinator.d.ts +32 -0
- package/dist/runtime/sortable-coordinator.js +11 -0
- package/dist/runtime/types.d.ts +1 -0
- package/dist/sortable/create-sortable.js +1 -0
- package/dist/sortable/sortable-preview.d.ts +3 -2
- package/dist/sortable/sortable-preview.js +48 -43
- package/dist/sortable/sortable-registry.d.ts +10 -54
- package/dist/sortable/sortable-registry.js +59 -46
- package/package.json +10 -10
|
@@ -20,57 +20,6 @@ export type DomSortableRuntime = DomDraggableRuntime & {
|
|
|
20
20
|
sortable: boolean;
|
|
21
21
|
};
|
|
22
22
|
} | null;
|
|
23
|
-
subscribe: (subscription: {
|
|
24
|
-
onDragStart?: (event: {
|
|
25
|
-
draggableId: string;
|
|
26
|
-
source: "pointer" | "keyboard";
|
|
27
|
-
pointerPosition: {
|
|
28
|
-
x: number;
|
|
29
|
-
y: number;
|
|
30
|
-
};
|
|
31
|
-
placementPosition?: {
|
|
32
|
-
x: number;
|
|
33
|
-
y: number;
|
|
34
|
-
};
|
|
35
|
-
movementPosition?: {
|
|
36
|
-
x: number;
|
|
37
|
-
y: number;
|
|
38
|
-
};
|
|
39
|
-
}) => void;
|
|
40
|
-
onDragUpdate?: (event: {
|
|
41
|
-
draggableId: string;
|
|
42
|
-
source: "pointer" | "keyboard";
|
|
43
|
-
pointerPosition: {
|
|
44
|
-
x: number;
|
|
45
|
-
y: number;
|
|
46
|
-
};
|
|
47
|
-
placementPosition?: {
|
|
48
|
-
x: number;
|
|
49
|
-
y: number;
|
|
50
|
-
};
|
|
51
|
-
movementPosition?: {
|
|
52
|
-
x: number;
|
|
53
|
-
y: number;
|
|
54
|
-
};
|
|
55
|
-
activeDropTargetId: string | null;
|
|
56
|
-
previousDropTargetId: string | null;
|
|
57
|
-
}) => void;
|
|
58
|
-
onDragEnd?: (event: {
|
|
59
|
-
draggableId: string;
|
|
60
|
-
source: "pointer" | "keyboard";
|
|
61
|
-
result: "dropped" | "no-target" | "invalid-target" | "canceled";
|
|
62
|
-
dropTargetId: string | null;
|
|
63
|
-
}) => void;
|
|
64
|
-
onDrop?: (event: {
|
|
65
|
-
draggableId: string;
|
|
66
|
-
source: "pointer" | "keyboard";
|
|
67
|
-
dropTargetId: string;
|
|
68
|
-
}) => void;
|
|
69
|
-
onActiveDragReset?: (event: {
|
|
70
|
-
draggableId: string;
|
|
71
|
-
source: "pointer" | "keyboard";
|
|
72
|
-
}) => void;
|
|
73
|
-
}) => () => void;
|
|
74
23
|
remeasureDropTargets: (input?: RemeasureDropTargetsInput) => void;
|
|
75
24
|
};
|
|
76
25
|
export type SortableRegistry = {
|
|
@@ -79,11 +28,14 @@ export type SortableRegistry = {
|
|
|
79
28
|
};
|
|
80
29
|
export type SortableRegistrationState = {
|
|
81
30
|
registrationAttributeSnapshots: Map<string, SortableAttributeSnapshot>;
|
|
82
|
-
sortableElementIds: WeakMap<HTMLElement,
|
|
31
|
+
sortableElementIds: WeakMap<HTMLElement, SortableRegistrationIdentity>;
|
|
83
32
|
sortableElementRefs: Map<string, WeakRef<HTMLElement>>;
|
|
84
|
-
sortableGroups: Map<string, string>;
|
|
85
33
|
sortableOptions: Map<string, NormalizedSortableOptions>;
|
|
86
34
|
};
|
|
35
|
+
export type SortableRegistrationIdentity = {
|
|
36
|
+
draggableId: string;
|
|
37
|
+
group: string;
|
|
38
|
+
};
|
|
87
39
|
export type SortableActiveDragState = {
|
|
88
40
|
previewPlacement: SortablePreviewPlacementState | null;
|
|
89
41
|
pointerMovement: SortablePointerMovementState | null;
|
|
@@ -115,16 +67,20 @@ export declare function unregisterSortableElement(input: {
|
|
|
115
67
|
registry: SortableRegistry;
|
|
116
68
|
runtime: DomSortableRuntime;
|
|
117
69
|
draggableId: string | null;
|
|
70
|
+
group: string;
|
|
118
71
|
element: HTMLElement | null;
|
|
119
72
|
}): void;
|
|
120
73
|
export declare function restoreSortableInternalAttributes(input: {
|
|
121
74
|
registry: SortableRegistry;
|
|
122
75
|
draggableId: string | null;
|
|
76
|
+
group: string;
|
|
123
77
|
element: HTMLElement;
|
|
124
78
|
}): void;
|
|
125
79
|
export declare function restoreSortableDraggedAttribute(input: {
|
|
126
80
|
registry: SortableRegistry;
|
|
127
81
|
draggableId: string;
|
|
82
|
+
group: string;
|
|
128
83
|
element: HTMLElement;
|
|
129
84
|
}): void;
|
|
130
|
-
export declare function getRegisteredSortableElement(registry: SortableRegistry, draggableId: string): HTMLElement | null;
|
|
85
|
+
export declare function getRegisteredSortableElement(registry: SortableRegistry, draggableId: string, group: string): HTMLElement | null;
|
|
86
|
+
export declare function getSortableRegistrationKey(draggableId: string, group: string): string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { setSortableCoordinator } from "../runtime/sortable-coordinator.js";
|
|
1
2
|
import { clearSortableDraggedState, clearSortablePointerMovement, initializeSortablePointerMovement, moveSortablePreview, restoreSortableSnapshot, snapshotSortableElement, isSortablePreviewTarget, updateSortablePointerMovement, clearSortablePreviewPlacement, } from "./sortable-preview.js";
|
|
2
3
|
import { normalizeSortableOptions, } from "./sortable-options.js";
|
|
3
4
|
const sortableRegistries = new WeakMap();
|
|
@@ -11,7 +12,6 @@ export function getSortableRegistry(runtime) {
|
|
|
11
12
|
registrationAttributeSnapshots: new Map(),
|
|
12
13
|
sortableElementIds: new WeakMap(),
|
|
13
14
|
sortableElementRefs: new Map(),
|
|
14
|
-
sortableGroups: new Map(),
|
|
15
15
|
sortableOptions: new Map(),
|
|
16
16
|
},
|
|
17
17
|
activeDrag: {
|
|
@@ -20,47 +20,50 @@ export function getSortableRegistry(runtime) {
|
|
|
20
20
|
snapshots: new Map(),
|
|
21
21
|
},
|
|
22
22
|
};
|
|
23
|
-
runtime
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
setSortableCoordinator(runtime, {
|
|
24
|
+
start: (event) => {
|
|
25
|
+
initializeSortablePointerMovement(registry, event.movementPosition, event.placementPosition);
|
|
26
|
+
if (isActiveSortableRegistration(registry, runtime, event)) {
|
|
27
|
+
snapshotSortableElement(registry, event.draggableId, event.group);
|
|
28
|
+
}
|
|
28
29
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
update: (event) => {
|
|
31
|
+
const isActiveSortable = isActiveSortableRegistration(registry, runtime, event);
|
|
32
|
+
if (isActiveSortable &&
|
|
33
|
+
isSortablePreviewTarget({
|
|
34
|
+
activeDropTargetId: event.activeDropTargetId,
|
|
35
|
+
})) {
|
|
34
36
|
moveSortablePreview({
|
|
35
37
|
registry,
|
|
36
38
|
runtime,
|
|
37
39
|
draggedDraggableId: event.draggableId,
|
|
38
40
|
activeDropTargetId: event.activeDropTargetId,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
draggedGroup: event.group,
|
|
42
|
+
placementPosition: event.placementPosition,
|
|
43
|
+
movementPosition: event.movementPosition,
|
|
44
|
+
options: getSortableOptions(registry, event.draggableId, event.group),
|
|
42
45
|
});
|
|
43
46
|
}
|
|
44
|
-
else {
|
|
47
|
+
else if (!isActiveSortable) {
|
|
45
48
|
clearSortablePreviewPlacement(registry);
|
|
46
49
|
}
|
|
47
|
-
|
|
48
|
-
updateSortablePointerMovement(registry, event.movementPosition ?? placementPosition, placementPosition);
|
|
49
|
-
},
|
|
50
|
-
onDragEnd: (event) => {
|
|
51
|
-
releaseSortableActiveDragState(registry, event.draggableId);
|
|
52
|
-
},
|
|
53
|
-
onDrop: (event) => {
|
|
54
|
-
releaseSortableActiveDragState(registry, event.draggableId);
|
|
50
|
+
updateSortablePointerMovement(registry, event.movementPosition, event.placementPosition);
|
|
55
51
|
},
|
|
56
|
-
|
|
57
|
-
releaseSortableActiveDragState(registry,
|
|
52
|
+
release: ({ draggableId, group }) => {
|
|
53
|
+
releaseSortableActiveDragState(registry, draggableId, group);
|
|
58
54
|
},
|
|
59
55
|
});
|
|
60
56
|
sortableRegistries.set(runtime, registry);
|
|
61
57
|
return registry;
|
|
62
58
|
}
|
|
63
|
-
function
|
|
59
|
+
function isActiveSortableRegistration(registry, runtime, input) {
|
|
60
|
+
const element = getRegisteredSortableElement(registry, input.draggableId, input.group);
|
|
61
|
+
const registration = runtime.getDropTargetRegistration(input.draggableId, input.group);
|
|
62
|
+
return (element !== null &&
|
|
63
|
+
registration?.element === element &&
|
|
64
|
+
registration.capabilities.sortable);
|
|
65
|
+
}
|
|
66
|
+
function releaseSortableActiveDragState(registry, draggableId, group) {
|
|
64
67
|
let releaseError;
|
|
65
68
|
let hasReleaseError = false;
|
|
66
69
|
try {
|
|
@@ -71,7 +74,7 @@ function releaseSortableActiveDragState(registry, draggableId) {
|
|
|
71
74
|
hasReleaseError = true;
|
|
72
75
|
}
|
|
73
76
|
try {
|
|
74
|
-
clearSortableDraggedState(registry, draggableId);
|
|
77
|
+
clearSortableDraggedState(registry, draggableId, group);
|
|
75
78
|
}
|
|
76
79
|
catch (error) {
|
|
77
80
|
if (!hasReleaseError) {
|
|
@@ -89,10 +92,11 @@ function releaseSortableActiveDragState(registry, draggableId) {
|
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
export function registerSortableElement(input) {
|
|
92
|
-
const
|
|
95
|
+
const registrationKey = getSortableRegistrationKey(input.draggableId, input.group);
|
|
96
|
+
const existingSnapshot = input.registry.registration.registrationAttributeSnapshots.get(registrationKey);
|
|
93
97
|
const existingSnapshotElement = existingSnapshot?.elementRef.deref() ?? null;
|
|
94
98
|
if (!existingSnapshot || existingSnapshotElement !== input.element) {
|
|
95
|
-
input.registry.registration.registrationAttributeSnapshots.set(
|
|
99
|
+
input.registry.registration.registrationAttributeSnapshots.set(registrationKey, {
|
|
96
100
|
elementRef: new WeakRef(input.element),
|
|
97
101
|
sortableDraggable: input.element.getAttribute("data-dnd-sortable-draggable"),
|
|
98
102
|
dragged: input.element.getAttribute("data-dnd-dragged"),
|
|
@@ -100,10 +104,12 @@ export function registerSortableElement(input) {
|
|
|
100
104
|
}
|
|
101
105
|
input.element.setAttribute("data-dnd-sortable-draggable", "true");
|
|
102
106
|
const normalizedOptions = normalizeSortableOptions(input.options);
|
|
103
|
-
input.registry.registration.sortableElementIds.set(input.element,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
input.registry.registration.sortableElementIds.set(input.element, {
|
|
108
|
+
draggableId: input.draggableId,
|
|
109
|
+
group: input.group,
|
|
110
|
+
});
|
|
111
|
+
input.registry.registration.sortableElementRefs.set(registrationKey, new WeakRef(input.element));
|
|
112
|
+
input.registry.registration.sortableOptions.set(registrationKey, normalizedOptions);
|
|
107
113
|
input.runtime.registerDropTarget(input.draggableId, input.element, input.group, {
|
|
108
114
|
containerId: input.containerId ?? null,
|
|
109
115
|
sortable: true,
|
|
@@ -112,50 +118,57 @@ export function registerSortableElement(input) {
|
|
|
112
118
|
}
|
|
113
119
|
export function unregisterSortableElement(input) {
|
|
114
120
|
if (input.draggableId !== null) {
|
|
121
|
+
const registrationKey = getSortableRegistrationKey(input.draggableId, input.group);
|
|
115
122
|
input.runtime.unregisterDropTarget(input.draggableId, input.element ?? undefined);
|
|
116
123
|
const registeredElement = input.registry.registration.sortableElementRefs
|
|
117
|
-
.get(
|
|
124
|
+
.get(registrationKey)
|
|
118
125
|
?.deref() ?? null;
|
|
119
126
|
if (registeredElement === input.element || input.element === null) {
|
|
120
127
|
if (registeredElement) {
|
|
121
128
|
input.registry.registration.sortableElementIds.delete(registeredElement);
|
|
122
129
|
}
|
|
123
|
-
input.registry.registration.sortableElementRefs.delete(
|
|
124
|
-
input.registry.registration.
|
|
125
|
-
input.registry.registration.sortableOptions.delete(input.draggableId);
|
|
130
|
+
input.registry.registration.sortableElementRefs.delete(registrationKey);
|
|
131
|
+
input.registry.registration.sortableOptions.delete(registrationKey);
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
134
|
if (input.element) {
|
|
129
135
|
restoreSortableInternalAttributes({
|
|
130
136
|
registry: input.registry,
|
|
131
137
|
draggableId: input.draggableId,
|
|
138
|
+
group: input.group,
|
|
132
139
|
element: input.element,
|
|
133
140
|
});
|
|
134
141
|
}
|
|
135
142
|
}
|
|
136
143
|
export function restoreSortableInternalAttributes(input) {
|
|
137
144
|
const snapshot = input.draggableId
|
|
138
|
-
? input.registry.registration.registrationAttributeSnapshots.get(input.draggableId)
|
|
145
|
+
? input.registry.registration.registrationAttributeSnapshots.get(getSortableRegistrationKey(input.draggableId, input.group))
|
|
139
146
|
: null;
|
|
140
147
|
const snapshotElement = snapshot?.elementRef.deref() ?? null;
|
|
141
|
-
restoreAttribute(input.element, "data-dnd-sortable-draggable", snapshot && snapshotElement === input.element
|
|
148
|
+
restoreAttribute(input.element, "data-dnd-sortable-draggable", snapshot && snapshotElement === input.element
|
|
149
|
+
? snapshot.sortableDraggable
|
|
150
|
+
: null);
|
|
142
151
|
restoreAttribute(input.element, "data-dnd-dragged", snapshot && snapshotElement === input.element ? snapshot.dragged : null);
|
|
143
152
|
if (input.draggableId) {
|
|
144
|
-
input.registry.registration.registrationAttributeSnapshots.delete(input.draggableId);
|
|
153
|
+
input.registry.registration.registrationAttributeSnapshots.delete(getSortableRegistrationKey(input.draggableId, input.group));
|
|
145
154
|
}
|
|
146
155
|
}
|
|
147
156
|
export function restoreSortableDraggedAttribute(input) {
|
|
148
|
-
const snapshot = input.registry.registration.registrationAttributeSnapshots.get(input.draggableId);
|
|
157
|
+
const snapshot = input.registry.registration.registrationAttributeSnapshots.get(getSortableRegistrationKey(input.draggableId, input.group));
|
|
149
158
|
const snapshotElement = snapshot?.elementRef.deref() ?? null;
|
|
150
159
|
restoreAttribute(input.element, "data-dnd-dragged", snapshot && snapshotElement === input.element ? snapshot.dragged : null);
|
|
151
160
|
}
|
|
152
|
-
export function getRegisteredSortableElement(registry, draggableId) {
|
|
153
|
-
const element = registry.registration.sortableElementRefs
|
|
161
|
+
export function getRegisteredSortableElement(registry, draggableId, group) {
|
|
162
|
+
const element = registry.registration.sortableElementRefs
|
|
163
|
+
.get(getSortableRegistrationKey(draggableId, group))
|
|
164
|
+
?.deref() ?? null;
|
|
154
165
|
return element?.isConnected ? element : null;
|
|
155
166
|
}
|
|
156
|
-
function getSortableOptions(registry, draggableId) {
|
|
157
|
-
return (registry.registration.sortableOptions.get(draggableId) ??
|
|
158
|
-
|
|
167
|
+
function getSortableOptions(registry, draggableId, group) {
|
|
168
|
+
return (registry.registration.sortableOptions.get(getSortableRegistrationKey(draggableId, group)) ?? normalizeSortableOptions(undefined));
|
|
169
|
+
}
|
|
170
|
+
export function getSortableRegistrationKey(draggableId, group) {
|
|
171
|
+
return `${group}\u0000${draggableId}`;
|
|
159
172
|
}
|
|
160
173
|
function restoreAttribute(element, attribute, value) {
|
|
161
174
|
if (value === null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mk-drag-and-drop/dom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Headless DOM drag-and-drop primitives for draggable, droppable, sortable, and grouped interactions.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://mkramer.dev",
|
|
@@ -42,14 +42,6 @@
|
|
|
42
42
|
"dist"
|
|
43
43
|
],
|
|
44
44
|
"sideEffects": false,
|
|
45
|
-
"devDependencies": {
|
|
46
|
-
"eslint": "^8.57.0",
|
|
47
|
-
"jsdom": "^24.1.3",
|
|
48
|
-
"typescript": "5.5.4",
|
|
49
|
-
"vitest": "^2.1.9",
|
|
50
|
-
"@repo/eslint-config": "0.0.0",
|
|
51
|
-
"@repo/typescript-config": "0.0.0"
|
|
52
|
-
},
|
|
53
45
|
"scripts": {
|
|
54
46
|
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
55
47
|
"build": "pnpm run clean && tsc -p tsconfig.json",
|
|
@@ -57,5 +49,13 @@
|
|
|
57
49
|
"lint": "eslint \"src/**/*.ts\"",
|
|
58
50
|
"test": "vitest run",
|
|
59
51
|
"test:watch": "vitest"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@repo/eslint-config": "workspace:*",
|
|
55
|
+
"@repo/typescript-config": "workspace:*",
|
|
56
|
+
"eslint": "^8.57.0",
|
|
57
|
+
"jsdom": "^24.1.3",
|
|
58
|
+
"typescript": "5.5.4",
|
|
59
|
+
"vitest": "^2.1.9"
|
|
60
60
|
}
|
|
61
|
-
}
|
|
61
|
+
}
|