@bevelo/dnd 0.0.0-stage → 0.1.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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/a11y-BOpF8vx8.d.cts +30 -0
  4. package/dist/a11y-Bbm_a0mw.d.ts +30 -0
  5. package/dist/index-DDuIvuzA.d.cts +258 -0
  6. package/dist/index-kH3SGg1R.d.ts +258 -0
  7. package/dist/index.cjs +413 -0
  8. package/dist/index.d.cts +139 -0
  9. package/dist/index.d.ts +139 -0
  10. package/dist/index.js +357 -0
  11. package/dist/live/animate-settle.cjs +20 -0
  12. package/dist/live/animate-settle.d.cts +14 -0
  13. package/dist/live/animate-settle.d.ts +14 -0
  14. package/dist/live/animate-settle.js +19 -0
  15. package/dist/live/flip.cjs +39 -0
  16. package/dist/live/flip.d.cts +9 -0
  17. package/dist/live/flip.d.ts +9 -0
  18. package/dist/live/flip.js +38 -0
  19. package/dist/live/index.cjs +22 -0
  20. package/dist/live/index.d.cts +4 -0
  21. package/dist/live/index.d.ts +4 -0
  22. package/dist/live/index.js +4 -0
  23. package/dist/live-BUYwbSUp.cjs +899 -0
  24. package/dist/live-Clc3aswx.js +840 -0
  25. package/dist/pure/a11y.cjs +85 -0
  26. package/dist/pure/a11y.d.cts +2 -0
  27. package/dist/pure/a11y.d.ts +2 -0
  28. package/dist/pure/a11y.js +81 -0
  29. package/dist/pure/apply.cjs +130 -0
  30. package/dist/pure/apply.d.cts +29 -0
  31. package/dist/pure/apply.d.ts +29 -0
  32. package/dist/pure/apply.js +127 -0
  33. package/dist/pure/reorder.cjs +121 -0
  34. package/dist/pure/reorder.d.cts +77 -0
  35. package/dist/pure/reorder.d.ts +77 -0
  36. package/dist/pure/reorder.js +117 -0
  37. package/dist/pure/sensors.cjs +243 -0
  38. package/dist/pure/sensors.d.cts +2 -0
  39. package/dist/pure/sensors.d.ts +2 -0
  40. package/dist/pure/sensors.js +240 -0
  41. package/dist/pure/spatial.cjs +121 -0
  42. package/dist/pure/spatial.d.cts +64 -0
  43. package/dist/pure/spatial.d.ts +64 -0
  44. package/dist/pure/spatial.js +119 -0
  45. package/dist/pure/state.cjs +15 -0
  46. package/dist/pure/state.d.cts +2 -0
  47. package/dist/pure/state.d.ts +2 -0
  48. package/dist/pure/state.js +1 -0
  49. package/dist/pure/tree.cjs +164 -0
  50. package/dist/pure/tree.d.cts +36 -0
  51. package/dist/pure/tree.d.ts +36 -0
  52. package/dist/pure/tree.js +161 -0
  53. package/dist/rows-B2C7Yf94.cjs +56 -0
  54. package/dist/rows-BIT_Rkhr.js +45 -0
  55. package/dist/sensors-C9RnMr6P.d.cts +212 -0
  56. package/dist/sensors-C9RnMr6P.d.ts +212 -0
  57. package/dist/settle-BiwZ0ZDt.js +235 -0
  58. package/dist/settle-COFsW7ul.cjs +282 -0
  59. package/dist/settle-Dl5Y9uOw.d.ts +59 -0
  60. package/dist/settle-DzdqbH4S.d.cts +59 -0
  61. package/dist/state-BEKGNZY1.d.cts +323 -0
  62. package/dist/state-BEKGNZY1.d.ts +323 -0
  63. package/dist/state-BK9W_RZn.cjs +672 -0
  64. package/dist/state-CYXS6GIb.js +595 -0
  65. package/package.json +89 -3
@@ -0,0 +1,85 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_rows = require("../rows-B2C7Yf94.cjs");
3
+ // src/pure/a11y.ts
4
+ const VERB = {
5
+ move: "moved",
6
+ copy: "copied",
7
+ swap: "swapped",
8
+ link: "linked"
9
+ };
10
+ function subject(c) {
11
+ return c.count > 1 ? c.label + " and " + (c.count - 1) + " more" : c.label;
12
+ }
13
+ function where(c) {
14
+ if (c.targetLabel !== null) return (c.operation === "swap" ? "with " : "onto ") + c.targetLabel;
15
+ const into = c.parentLabel === null ? "" : "into " + c.parentLabel + ", ";
16
+ const level = c.parentLabel === null && c.depth === 0 ? "" : ", level " + (c.depth + 1);
17
+ return into + "position " + c.position + " of " + c.total + level;
18
+ }
19
+ /** English. */
20
+ const defaultMessages = {
21
+ lift: (c) => "Picked up " + subject(c) + ", position " + c.position + " of " + c.total + ".",
22
+ move: (c) => subject(c) + ", " + VERB[c.operation ?? "move"] + " " + where(c) + ".",
23
+ blocked: (c) => subject(c) + " cannot be dropped here.",
24
+ drop: (c) => subject(c) + " dropped, " + VERB[c.operation ?? "move"] + " " + where(c) + ".",
25
+ cancel: (c) => "Drag cancelled. " + subject(c) + " returned to its starting position."
26
+ };
27
+ const keyboardInstructions = "Press space to lift. Arrow keys move, Alt copies, space drops, escape cancels.";
28
+ /** One message for a state change, or undefined when nothing a listener can notice changed. */
29
+ function announcement(prev, next, messages, label) {
30
+ if (next.phase === "dragging") {
31
+ if (prev.phase !== "dragging") return messages.lift(contextOf(next, label));
32
+ if (prev.target === next.target && prev.operation === next.operation) return void 0;
33
+ return (next.target === null ? messages.blocked : messages.move)(contextOf(next, label));
34
+ }
35
+ if (next.phase !== "dropping" || prev.phase !== "dragging") return void 0;
36
+ return (next.result === null ? messages.cancel : messages.drop)(contextOf(prev, label));
37
+ }
38
+ function contextOf(state, label) {
39
+ const { source, target, operation, layout } = state;
40
+ const zone = layout.zones.find((z) => z.id === (target ?? source).zoneId);
41
+ const item = target?.kind === "item" ? target.key : null;
42
+ const gap = target?.kind === "gap" ? target : null;
43
+ let position = 1;
44
+ let total = 1;
45
+ let parentLabel = null;
46
+ if (zone !== void 0) {
47
+ const { track } = zone;
48
+ const carried = new Set(target === null ? [] : source.carried);
49
+ const rows = require_rows.rowsOf(track, carried);
50
+ const at = gap === null ? track.indexOf(item ?? source.primary) : gap.index < rows.count ? rows.index(gap.index) : track.count;
51
+ if (zone.kind === "tree") {
52
+ ({position, total} = zone.outline.siblings(zone, at, gap?.depth ?? zone.depthOf(item ?? source.primary), carried));
53
+ const parent = gap?.placement.parentId;
54
+ if (parent != null) parentLabel = label(parent);
55
+ } else {
56
+ position = rows.rankOf(at) + 1;
57
+ total = rows.count;
58
+ }
59
+ if (gap !== null) total += 1;
60
+ }
61
+ return {
62
+ label: label(source.primary),
63
+ count: source.ids.length,
64
+ operation,
65
+ position,
66
+ total,
67
+ parentLabel,
68
+ targetLabel: item === null ? null : label(item),
69
+ depth: gap?.depth ?? 0
70
+ };
71
+ }
72
+ /** role, tabindex, aria-roledescription, aria-describedby, and aria-pressed while lifted. */
73
+ function handleAttributes(state, key, instructionsId) {
74
+ return {
75
+ role: "button",
76
+ tabindex: "0",
77
+ "aria-roledescription": "draggable",
78
+ "aria-describedby": instructionsId,
79
+ "aria-pressed": String(state.phase === "dragging" && state.source.carried.includes(key))
80
+ };
81
+ }
82
+ exports.announcement = announcement;
83
+ exports.defaultMessages = defaultMessages;
84
+ exports.handleAttributes = handleAttributes;
85
+ exports.keyboardInstructions = keyboardInstructions;
@@ -0,0 +1,2 @@
1
+ import { a as handleAttributes, i as defaultMessages, n as MessageContext, o as keyboardInstructions, r as announcement, t as DragMessages } from "../a11y-BOpF8vx8.cjs";
2
+ export { DragMessages, MessageContext, announcement, defaultMessages, handleAttributes, keyboardInstructions };
@@ -0,0 +1,2 @@
1
+ import { a as handleAttributes, i as defaultMessages, n as MessageContext, o as keyboardInstructions, r as announcement, t as DragMessages } from "../a11y-Bbm_a0mw.js";
2
+ export { DragMessages, MessageContext, announcement, defaultMessages, handleAttributes, keyboardInstructions };
@@ -0,0 +1,81 @@
1
+ import { n as rowsOf } from "../rows-BIT_Rkhr.js";
2
+ // src/pure/a11y.ts
3
+ const VERB = {
4
+ move: "moved",
5
+ copy: "copied",
6
+ swap: "swapped",
7
+ link: "linked"
8
+ };
9
+ function subject(c) {
10
+ return c.count > 1 ? c.label + " and " + (c.count - 1) + " more" : c.label;
11
+ }
12
+ function where(c) {
13
+ if (c.targetLabel !== null) return (c.operation === "swap" ? "with " : "onto ") + c.targetLabel;
14
+ const into = c.parentLabel === null ? "" : "into " + c.parentLabel + ", ";
15
+ const level = c.parentLabel === null && c.depth === 0 ? "" : ", level " + (c.depth + 1);
16
+ return into + "position " + c.position + " of " + c.total + level;
17
+ }
18
+ /** English. */
19
+ const defaultMessages = {
20
+ lift: (c) => "Picked up " + subject(c) + ", position " + c.position + " of " + c.total + ".",
21
+ move: (c) => subject(c) + ", " + VERB[c.operation ?? "move"] + " " + where(c) + ".",
22
+ blocked: (c) => subject(c) + " cannot be dropped here.",
23
+ drop: (c) => subject(c) + " dropped, " + VERB[c.operation ?? "move"] + " " + where(c) + ".",
24
+ cancel: (c) => "Drag cancelled. " + subject(c) + " returned to its starting position."
25
+ };
26
+ const keyboardInstructions = "Press space to lift. Arrow keys move, Alt copies, space drops, escape cancels.";
27
+ /** One message for a state change, or undefined when nothing a listener can notice changed. */
28
+ function announcement(prev, next, messages, label) {
29
+ if (next.phase === "dragging") {
30
+ if (prev.phase !== "dragging") return messages.lift(contextOf(next, label));
31
+ if (prev.target === next.target && prev.operation === next.operation) return void 0;
32
+ return (next.target === null ? messages.blocked : messages.move)(contextOf(next, label));
33
+ }
34
+ if (next.phase !== "dropping" || prev.phase !== "dragging") return void 0;
35
+ return (next.result === null ? messages.cancel : messages.drop)(contextOf(prev, label));
36
+ }
37
+ function contextOf(state, label) {
38
+ const { source, target, operation, layout } = state;
39
+ const zone = layout.zones.find((z) => z.id === (target ?? source).zoneId);
40
+ const item = target?.kind === "item" ? target.key : null;
41
+ const gap = target?.kind === "gap" ? target : null;
42
+ let position = 1;
43
+ let total = 1;
44
+ let parentLabel = null;
45
+ if (zone !== void 0) {
46
+ const { track } = zone;
47
+ const carried = new Set(target === null ? [] : source.carried);
48
+ const rows = rowsOf(track, carried);
49
+ const at = gap === null ? track.indexOf(item ?? source.primary) : gap.index < rows.count ? rows.index(gap.index) : track.count;
50
+ if (zone.kind === "tree") {
51
+ ({position, total} = zone.outline.siblings(zone, at, gap?.depth ?? zone.depthOf(item ?? source.primary), carried));
52
+ const parent = gap?.placement.parentId;
53
+ if (parent != null) parentLabel = label(parent);
54
+ } else {
55
+ position = rows.rankOf(at) + 1;
56
+ total = rows.count;
57
+ }
58
+ if (gap !== null) total += 1;
59
+ }
60
+ return {
61
+ label: label(source.primary),
62
+ count: source.ids.length,
63
+ operation,
64
+ position,
65
+ total,
66
+ parentLabel,
67
+ targetLabel: item === null ? null : label(item),
68
+ depth: gap?.depth ?? 0
69
+ };
70
+ }
71
+ /** role, tabindex, aria-roledescription, aria-describedby, and aria-pressed while lifted. */
72
+ function handleAttributes(state, key, instructionsId) {
73
+ return {
74
+ role: "button",
75
+ tabindex: "0",
76
+ "aria-roledescription": "draggable",
77
+ "aria-describedby": instructionsId,
78
+ "aria-pressed": String(state.phase === "dragging" && state.source.carried.includes(key))
79
+ };
80
+ }
81
+ export { announcement, defaultMessages, handleAttributes, keyboardInstructions };
@@ -0,0 +1,130 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ // src/pure/apply.ts
3
+ /** A missing `before` key falls back to the end. Link returns a copy of `items`. */
4
+ function applyListDrop(items, result, options) {
5
+ const { key } = options;
6
+ switch (result.operation) {
7
+ case "move":
8
+ case "copy": {
9
+ const byKey = new Map(items.map((item) => [key(item), item]));
10
+ const found = result.ids.flatMap((id) => byKey.has(id) ? [byKey.get(id)] : []);
11
+ const { before } = result.to;
12
+ if (result.operation === "copy") return insertBefore(items, found.map(options.copy), before, key);
13
+ const moving = new Set(result.ids);
14
+ return insertBefore(items.filter((item) => !moving.has(key(item))), found, before, key);
15
+ }
16
+ case "swap": return swapKeys(items, result.ids[0], result.with, key);
17
+ case "link": return items.slice();
18
+ }
19
+ }
20
+ /** Column id -> items. Untouched columns keep their reference. */
21
+ function applyBoardDrop(columns, result, options) {
22
+ const { key } = options;
23
+ const next = { ...columns };
24
+ const has = (id) => id !== null && Object.prototype.hasOwnProperty.call(columns, id);
25
+ switch (result.operation) {
26
+ case "move":
27
+ case "copy": {
28
+ const target = result.to.parentId;
29
+ if (!has(target)) return next;
30
+ const touched = /* @__PURE__ */ new Set([target]);
31
+ for (const from of result.from) if (has(from.parentId)) touched.add(from.parentId);
32
+ const byKey = /* @__PURE__ */ new Map();
33
+ for (const column of touched) for (const item of columns[column]) byKey.set(key(item), item);
34
+ const found = result.ids.flatMap((id) => byKey.has(id) ? [byKey.get(id)] : []);
35
+ if (result.operation === "copy") {
36
+ const copy = options.copy;
37
+ next[target] = insertBefore(columns[target], found.map(copy), result.to.before, key);
38
+ return next;
39
+ }
40
+ const moving = new Set(result.ids);
41
+ for (const column of touched) next[column] = columns[column].filter((item) => !moving.has(key(item)));
42
+ next[target] = insertBefore(next[target], found, result.to.before, key);
43
+ return next;
44
+ }
45
+ case "swap": {
46
+ const a = result.ids[0];
47
+ const b = result.with;
48
+ const columnOf = (id) => Object.keys(columns).find((column) => columns[column].some((item) => key(item) === id));
49
+ const first = columnOf(a);
50
+ const second = columnOf(b);
51
+ if (first === void 0 || second === void 0) return next;
52
+ if (first === second) {
53
+ next[first] = swapKeys(columns[first], a, b, key);
54
+ return next;
55
+ }
56
+ const itemA = columns[first].find((item) => key(item) === a);
57
+ const itemB = columns[second].find((item) => key(item) === b);
58
+ next[first] = columns[first].map((item) => key(item) === a ? itemB : item);
59
+ next[second] = columns[second].map((item) => key(item) === b ? itemA : item);
60
+ return next;
61
+ }
62
+ case "link": return next;
63
+ }
64
+ }
65
+ /**
66
+ * Moves and copies re-parent the ids under `to.parentId` before `to.before`; their
67
+ * descendants follow through `parentOf`. A drop that would put a node inside its own
68
+ * subtree, or swap a node with its ancestor, returns the nodes unchanged.
69
+ */
70
+ function applyTreeDrop(nodes, result, options) {
71
+ const { key, parentOf, withParent } = options;
72
+ const parentById = new Map(nodes.map((node) => [key(node), parentOf(node)]));
73
+ const within = (id, ancestor) => {
74
+ for (let at = id; at !== null && at !== void 0; at = parentById.get(at) ?? null) if (at === ancestor) return true;
75
+ return false;
76
+ };
77
+ switch (result.operation) {
78
+ case "move": {
79
+ const parentId = result.to.parentId;
80
+ if (result.ids.some((id) => within(parentId, id))) return nodes.slice();
81
+ const moving = new Set(result.ids);
82
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
83
+ const moved = result.ids.flatMap((id) => byKey.has(id) ? [withParent(byKey.get(id), parentId)] : []);
84
+ return insertBefore(nodes.filter((node) => !moving.has(key(node))), moved, result.to.before, key);
85
+ }
86
+ case "copy": {
87
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
88
+ const copy = options.copy;
89
+ return insertBefore(nodes, result.ids.flatMap((id) => {
90
+ const node = byKey.get(id);
91
+ if (node === void 0) return [];
92
+ const [root, ...rest] = copy(node);
93
+ return root === void 0 ? [] : [withParent(root, result.to.parentId), ...rest];
94
+ }), result.to.before, key);
95
+ }
96
+ case "swap": {
97
+ const a = result.ids[0];
98
+ const b = result.with;
99
+ if (!parentById.has(a) || !parentById.has(b) || within(a, b) || within(b, a)) return nodes.slice();
100
+ const parentA = parentById.get(a) ?? null;
101
+ const parentB = parentById.get(b) ?? null;
102
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
103
+ const nodeA = withParent(byKey.get(a), parentB);
104
+ const nodeB = withParent(byKey.get(b), parentA);
105
+ return nodes.map((node) => key(node) === a ? nodeB : key(node) === b ? nodeA : node);
106
+ }
107
+ case "link": return nodes.slice();
108
+ }
109
+ }
110
+ /** `inserted` placed before the item keyed `before`, or at the end when it is null or absent. */
111
+ function insertBefore(items, inserted, before, key) {
112
+ const at = before === null ? -1 : items.findIndex((item) => key(item) === before);
113
+ return at < 0 ? [...items, ...inserted] : [
114
+ ...items.slice(0, at),
115
+ ...inserted,
116
+ ...items.slice(at)
117
+ ];
118
+ }
119
+ function swapKeys(items, a, b, key) {
120
+ const i = items.findIndex((item) => key(item) === a);
121
+ const j = items.findIndex((item) => key(item) === b);
122
+ const next = items.slice();
123
+ if (i < 0 || j < 0) return next;
124
+ next[i] = items[j];
125
+ next[j] = items[i];
126
+ return next;
127
+ }
128
+ exports.applyBoardDrop = applyBoardDrop;
129
+ exports.applyListDrop = applyListDrop;
130
+ exports.applyTreeDrop = applyTreeDrop;
@@ -0,0 +1,29 @@
1
+ import { b as Operation, d as DropResult } from "../state-BEKGNZY1.cjs";
2
+ /** `copy` is required exactly when the result type admits "copy". */
3
+ export type ApplyOptions<T, Op extends Operation> = {
4
+ readonly key: (item: T) => string;
5
+ } & ("copy" extends Op ? {
6
+ readonly copy: (item: T) => T;
7
+ } : {
8
+ readonly copy?: never;
9
+ });
10
+ /** A missing `before` key falls back to the end. Link returns a copy of `items`. */
11
+ export declare function applyListDrop<T, R extends DropResult>(items: readonly T[], result: R, options: ApplyOptions<T, R["operation"]>): T[];
12
+ /** Column id -> items. Untouched columns keep their reference. */
13
+ export declare function applyBoardDrop<T, K extends string, R extends DropResult>(columns: Readonly<Record<K, readonly T[]>>, result: R, options: ApplyOptions<T, R["operation"]>): Record<K, T[]>;
14
+ /** Flat node array, sibling order = array order; descendants follow through parentOf. Copy returns a whole subtree, root first. */
15
+ export type TreeApplyOptions<N, Op extends Operation> = {
16
+ readonly key: (node: N) => string;
17
+ readonly parentOf: (node: N) => string | null;
18
+ readonly withParent: (node: N, parentId: string | null) => N;
19
+ } & ("copy" extends Op ? {
20
+ readonly copy: (node: N) => N[];
21
+ } : {
22
+ readonly copy?: never;
23
+ });
24
+ /**
25
+ * Moves and copies re-parent the ids under `to.parentId` before `to.before`; their
26
+ * descendants follow through `parentOf`. A drop that would put a node inside its own
27
+ * subtree, or swap a node with its ancestor, returns the nodes unchanged.
28
+ */
29
+ export declare function applyTreeDrop<N, R extends DropResult>(nodes: readonly N[], result: R, options: TreeApplyOptions<N, R["operation"]>): N[];
@@ -0,0 +1,29 @@
1
+ import { b as Operation, d as DropResult } from "../state-BEKGNZY1.js";
2
+ /** `copy` is required exactly when the result type admits "copy". */
3
+ export type ApplyOptions<T, Op extends Operation> = {
4
+ readonly key: (item: T) => string;
5
+ } & ("copy" extends Op ? {
6
+ readonly copy: (item: T) => T;
7
+ } : {
8
+ readonly copy?: never;
9
+ });
10
+ /** A missing `before` key falls back to the end. Link returns a copy of `items`. */
11
+ export declare function applyListDrop<T, R extends DropResult>(items: readonly T[], result: R, options: ApplyOptions<T, R["operation"]>): T[];
12
+ /** Column id -> items. Untouched columns keep their reference. */
13
+ export declare function applyBoardDrop<T, K extends string, R extends DropResult>(columns: Readonly<Record<K, readonly T[]>>, result: R, options: ApplyOptions<T, R["operation"]>): Record<K, T[]>;
14
+ /** Flat node array, sibling order = array order; descendants follow through parentOf. Copy returns a whole subtree, root first. */
15
+ export type TreeApplyOptions<N, Op extends Operation> = {
16
+ readonly key: (node: N) => string;
17
+ readonly parentOf: (node: N) => string | null;
18
+ readonly withParent: (node: N, parentId: string | null) => N;
19
+ } & ("copy" extends Op ? {
20
+ readonly copy: (node: N) => N[];
21
+ } : {
22
+ readonly copy?: never;
23
+ });
24
+ /**
25
+ * Moves and copies re-parent the ids under `to.parentId` before `to.before`; their
26
+ * descendants follow through `parentOf`. A drop that would put a node inside its own
27
+ * subtree, or swap a node with its ancestor, returns the nodes unchanged.
28
+ */
29
+ export declare function applyTreeDrop<N, R extends DropResult>(nodes: readonly N[], result: R, options: TreeApplyOptions<N, R["operation"]>): N[];
@@ -0,0 +1,127 @@
1
+ // src/pure/apply.ts
2
+ /** A missing `before` key falls back to the end. Link returns a copy of `items`. */
3
+ function applyListDrop(items, result, options) {
4
+ const { key } = options;
5
+ switch (result.operation) {
6
+ case "move":
7
+ case "copy": {
8
+ const byKey = new Map(items.map((item) => [key(item), item]));
9
+ const found = result.ids.flatMap((id) => byKey.has(id) ? [byKey.get(id)] : []);
10
+ const { before } = result.to;
11
+ if (result.operation === "copy") return insertBefore(items, found.map(options.copy), before, key);
12
+ const moving = new Set(result.ids);
13
+ return insertBefore(items.filter((item) => !moving.has(key(item))), found, before, key);
14
+ }
15
+ case "swap": return swapKeys(items, result.ids[0], result.with, key);
16
+ case "link": return items.slice();
17
+ }
18
+ }
19
+ /** Column id -> items. Untouched columns keep their reference. */
20
+ function applyBoardDrop(columns, result, options) {
21
+ const { key } = options;
22
+ const next = { ...columns };
23
+ const has = (id) => id !== null && Object.prototype.hasOwnProperty.call(columns, id);
24
+ switch (result.operation) {
25
+ case "move":
26
+ case "copy": {
27
+ const target = result.to.parentId;
28
+ if (!has(target)) return next;
29
+ const touched = /* @__PURE__ */ new Set([target]);
30
+ for (const from of result.from) if (has(from.parentId)) touched.add(from.parentId);
31
+ const byKey = /* @__PURE__ */ new Map();
32
+ for (const column of touched) for (const item of columns[column]) byKey.set(key(item), item);
33
+ const found = result.ids.flatMap((id) => byKey.has(id) ? [byKey.get(id)] : []);
34
+ if (result.operation === "copy") {
35
+ const copy = options.copy;
36
+ next[target] = insertBefore(columns[target], found.map(copy), result.to.before, key);
37
+ return next;
38
+ }
39
+ const moving = new Set(result.ids);
40
+ for (const column of touched) next[column] = columns[column].filter((item) => !moving.has(key(item)));
41
+ next[target] = insertBefore(next[target], found, result.to.before, key);
42
+ return next;
43
+ }
44
+ case "swap": {
45
+ const a = result.ids[0];
46
+ const b = result.with;
47
+ const columnOf = (id) => Object.keys(columns).find((column) => columns[column].some((item) => key(item) === id));
48
+ const first = columnOf(a);
49
+ const second = columnOf(b);
50
+ if (first === void 0 || second === void 0) return next;
51
+ if (first === second) {
52
+ next[first] = swapKeys(columns[first], a, b, key);
53
+ return next;
54
+ }
55
+ const itemA = columns[first].find((item) => key(item) === a);
56
+ const itemB = columns[second].find((item) => key(item) === b);
57
+ next[first] = columns[first].map((item) => key(item) === a ? itemB : item);
58
+ next[second] = columns[second].map((item) => key(item) === b ? itemA : item);
59
+ return next;
60
+ }
61
+ case "link": return next;
62
+ }
63
+ }
64
+ /**
65
+ * Moves and copies re-parent the ids under `to.parentId` before `to.before`; their
66
+ * descendants follow through `parentOf`. A drop that would put a node inside its own
67
+ * subtree, or swap a node with its ancestor, returns the nodes unchanged.
68
+ */
69
+ function applyTreeDrop(nodes, result, options) {
70
+ const { key, parentOf, withParent } = options;
71
+ const parentById = new Map(nodes.map((node) => [key(node), parentOf(node)]));
72
+ const within = (id, ancestor) => {
73
+ for (let at = id; at !== null && at !== void 0; at = parentById.get(at) ?? null) if (at === ancestor) return true;
74
+ return false;
75
+ };
76
+ switch (result.operation) {
77
+ case "move": {
78
+ const parentId = result.to.parentId;
79
+ if (result.ids.some((id) => within(parentId, id))) return nodes.slice();
80
+ const moving = new Set(result.ids);
81
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
82
+ const moved = result.ids.flatMap((id) => byKey.has(id) ? [withParent(byKey.get(id), parentId)] : []);
83
+ return insertBefore(nodes.filter((node) => !moving.has(key(node))), moved, result.to.before, key);
84
+ }
85
+ case "copy": {
86
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
87
+ const copy = options.copy;
88
+ return insertBefore(nodes, result.ids.flatMap((id) => {
89
+ const node = byKey.get(id);
90
+ if (node === void 0) return [];
91
+ const [root, ...rest] = copy(node);
92
+ return root === void 0 ? [] : [withParent(root, result.to.parentId), ...rest];
93
+ }), result.to.before, key);
94
+ }
95
+ case "swap": {
96
+ const a = result.ids[0];
97
+ const b = result.with;
98
+ if (!parentById.has(a) || !parentById.has(b) || within(a, b) || within(b, a)) return nodes.slice();
99
+ const parentA = parentById.get(a) ?? null;
100
+ const parentB = parentById.get(b) ?? null;
101
+ const byKey = new Map(nodes.map((node) => [key(node), node]));
102
+ const nodeA = withParent(byKey.get(a), parentB);
103
+ const nodeB = withParent(byKey.get(b), parentA);
104
+ return nodes.map((node) => key(node) === a ? nodeB : key(node) === b ? nodeA : node);
105
+ }
106
+ case "link": return nodes.slice();
107
+ }
108
+ }
109
+ /** `inserted` placed before the item keyed `before`, or at the end when it is null or absent. */
110
+ function insertBefore(items, inserted, before, key) {
111
+ const at = before === null ? -1 : items.findIndex((item) => key(item) === before);
112
+ return at < 0 ? [...items, ...inserted] : [
113
+ ...items.slice(0, at),
114
+ ...inserted,
115
+ ...items.slice(at)
116
+ ];
117
+ }
118
+ function swapKeys(items, a, b, key) {
119
+ const i = items.findIndex((item) => key(item) === a);
120
+ const j = items.findIndex((item) => key(item) === b);
121
+ const next = items.slice();
122
+ if (i < 0 || j < 0) return next;
123
+ next[i] = items[j];
124
+ next[j] = items[i];
125
+ return next;
126
+ }
127
+ export { applyBoardDrop, applyListDrop, applyTreeDrop };
@@ -0,0 +1,121 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _bevelo_runtime_geometry = require("@bevelo/runtime/geometry");
3
+ // src/pure/reorder.ts
4
+ /**
5
+ * The order after moving the item at `from` to `to`.
6
+ *
7
+ * Pure and stable: the input array is never touched, and the moved item keeps
8
+ * its relative order against everything else. Out-of-range indices and a move
9
+ * onto the item's own slot both return a copy of the input unchanged — the
10
+ * answer a caller can use without a branch, matching `applySort`'s treatment
11
+ * of an unknown id.
12
+ */
13
+ function reorder(items, from, to) {
14
+ const next = [...items];
15
+ if (!Number.isInteger(from) || !Number.isInteger(to)) return next;
16
+ if (from < 0 || from >= items.length || to < 0 || to >= items.length || from === to) return next;
17
+ const [moved] = next.splice(from, 1);
18
+ if (moved === void 0) return [...items];
19
+ next.splice(to, 0, moved);
20
+ return next;
21
+ }
22
+ /**
23
+ * The order after moving one item, with relative indices allowed.
24
+ *
25
+ * `arrayMove` is `reorder` with one extra rule: a negative `to`
26
+ * counts back from the end, so -1 is the last slot, and a `to` equal to the
27
+ * length appends (the slot after the last one a host reads from the list). Both
28
+ * are normalised to an index and handed to `reorder`, so there is one
29
+ * implementation of the move and one contract for its edges: an index that names
30
+ * a slot in the list moves the item, and an index that names nothing returns a
31
+ * copy unchanged, rather than splicing `undefined` into the list.
32
+ */
33
+ function arrayMove(items, from, to) {
34
+ const target = to < 0 ? items.length + to : to;
35
+ if (!Number.isInteger(from) || !Number.isInteger(target)) return [...items];
36
+ if (target === items.length) return reorder(items, from, items.length - 1);
37
+ return reorder(items, from, target);
38
+ }
39
+ /**
40
+ * The offsets that animate a move inside one list.
41
+ *
42
+ * The moved item travels from its own slot's centre to the destination slot's
43
+ * centre. Every slot it passes is filled by its neighbour shifting one place
44
+ * towards the vacated slot: moving down, the rows after it move up; moving up,
45
+ * the rows before it move down. Because the offsets are centres rather than
46
+ * rect origins, they also carry a perpendicular component when the rects are
47
+ * laid out in a grid.
48
+ *
49
+ * Returns undefined for an out-of-range move (nothing sensible to animate), and
50
+ * a zero-length move for `from === to` (the caller may skip work, but the answer
51
+ * is still usable). Broken (NaN) rects give NaN offsets rather than throwing:
52
+ * this runs inside a pointer handler.
53
+ */
54
+ function moveWithin(rects, from, to) {
55
+ if (!Number.isInteger(from) || !Number.isInteger(to)) return void 0;
56
+ if (from < 0 || from >= rects.length || to < 0 || to >= rects.length) return void 0;
57
+ const origin = rects[from];
58
+ const destination = rects[to];
59
+ if (origin === void 0 || destination === void 0) return void 0;
60
+ const moved = offsetBetween(origin, destination, from, to);
61
+ if (from === to) return {
62
+ moved,
63
+ displaced: []
64
+ };
65
+ const displaced = [];
66
+ if (from < to) for (let index = from + 1; index <= to; index += 1) {
67
+ const rect = rects[index];
68
+ const target = rects[index - 1];
69
+ if (rect !== void 0 && target !== void 0) displaced.push(offsetBetween(rect, target, index, index - 1));
70
+ }
71
+ else for (let index = to; index < from; index += 1) {
72
+ const rect = rects[index];
73
+ const target = rects[index + 1];
74
+ if (rect !== void 0 && target !== void 0) displaced.push(offsetBetween(rect, target, index, index + 1));
75
+ }
76
+ return {
77
+ moved,
78
+ displaced
79
+ };
80
+ }
81
+ /** The centre-to-centre offset from one slot's rect to another's. */
82
+ function offsetBetween(from, to, fromIndex, toIndex) {
83
+ const start = (0, _bevelo_runtime_geometry.center)(from);
84
+ const end = (0, _bevelo_runtime_geometry.center)(to);
85
+ return {
86
+ from: fromIndex,
87
+ to: toIndex,
88
+ dx: end.x - start.x,
89
+ dy: end.y - start.y
90
+ };
91
+ }
92
+ /**
93
+ * The slot a point would drop into, by the midpoint rule: an item owns the half
94
+ * of its rect before its centre, so the index is the number of rects whose
95
+ * centre the point has passed. That is the insertion index in the list as it is
96
+ * rendered (before the dragged item is removed); a same-list caller adjusts for
97
+ * the removal the same way `applyBoardMove` does.
98
+ *
99
+ * A point exactly on a midpoint counts as past it, which is the tie-break a
100
+ * pointer expects: dropping on the line between two items goes after the first
101
+ * one. The axis names the coordinate that orders the rects — horizontal for a
102
+ * rail, vertical for a column. Broken rects are skipped rather than allowed to
103
+ * decide where an item lands, and a non-finite point has no slot to report and
104
+ * returns 0 (the insertion that cannot put the item on the wrong side of a
105
+ * valid midpoint).
106
+ */
107
+ function dropIndexAt(rects, point, axis) {
108
+ const coordinate = axis === "vertical" ? point.y : point.x;
109
+ if (!Number.isFinite(coordinate)) return 0;
110
+ let index = 0;
111
+ for (const rect of rects) {
112
+ const midpoint = axis === "vertical" ? (0, _bevelo_runtime_geometry.center)(rect).y : (0, _bevelo_runtime_geometry.center)(rect).x;
113
+ if (!Number.isFinite(midpoint)) continue;
114
+ if (midpoint <= coordinate) index += 1;
115
+ }
116
+ return index;
117
+ }
118
+ exports.arrayMove = arrayMove;
119
+ exports.dropIndexAt = dropIndexAt;
120
+ exports.moveWithin = moveWithin;
121
+ exports.reorder = reorder;