@svgrid/grid 1.1.0 → 1.1.1

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 (117) hide show
  1. package/dist/GridMenus.svelte +205 -127
  2. package/dist/SvGrid.controller.svelte.d.ts +44 -1
  3. package/dist/SvGrid.controller.svelte.js +223 -38
  4. package/dist/SvGrid.css +2012 -1709
  5. package/dist/SvGrid.helpers.test.d.ts +1 -0
  6. package/dist/SvGrid.helpers.test.js +298 -0
  7. package/dist/SvGrid.svelte +2346 -2043
  8. package/dist/SvGrid.types.d.ts +91 -4
  9. package/dist/aligned-grids.d.ts +6 -0
  10. package/dist/aligned-grids.js +84 -0
  11. package/dist/aligned-grids.test.d.ts +1 -0
  12. package/dist/aligned-grids.test.js +75 -0
  13. package/dist/build-api.coverage.test.d.ts +20 -0
  14. package/dist/build-api.coverage.test.js +505 -0
  15. package/dist/build-api.js +59 -29
  16. package/dist/cell-render.test.d.ts +1 -0
  17. package/dist/cell-render.test.js +338 -0
  18. package/dist/chart-export.test.d.ts +1 -0
  19. package/dist/chart-export.test.js +302 -0
  20. package/dist/chart.coverage.test.d.ts +1 -0
  21. package/dist/chart.coverage.test.js +748 -0
  22. package/dist/clipboard.js +88 -24
  23. package/dist/clipboard.test.d.ts +1 -0
  24. package/dist/clipboard.test.js +700 -0
  25. package/dist/collaboration.coverage.test.d.ts +1 -0
  26. package/dist/collaboration.coverage.test.js +200 -0
  27. package/dist/column-groups.d.ts +19 -0
  28. package/dist/column-groups.js +62 -0
  29. package/dist/column-groups.test.d.ts +1 -0
  30. package/dist/column-groups.test.js +56 -0
  31. package/dist/column-types.d.ts +10 -0
  32. package/dist/column-types.js +63 -0
  33. package/dist/column-types.test.d.ts +1 -0
  34. package/dist/column-types.test.js +62 -0
  35. package/dist/columns.test.d.ts +1 -0
  36. package/dist/columns.test.js +625 -0
  37. package/dist/core.d.ts +85 -0
  38. package/dist/editing.d.ts +7 -0
  39. package/dist/editing.js +191 -5
  40. package/dist/editing.test.d.ts +1 -0
  41. package/dist/editing.test.js +732 -0
  42. package/dist/editors/cell-editors.coverage.test.d.ts +1 -0
  43. package/dist/editors/cell-editors.coverage.test.js +139 -0
  44. package/dist/facet-buckets.test.d.ts +1 -0
  45. package/dist/facet-buckets.test.js +296 -0
  46. package/dist/filter-operators.test.d.ts +1 -0
  47. package/dist/filter-operators.test.js +135 -0
  48. package/dist/hyperformula-adapter.test.d.ts +1 -0
  49. package/dist/hyperformula-adapter.test.js +205 -0
  50. package/dist/index.d.ts +5 -2
  51. package/dist/index.js +4 -1
  52. package/dist/keyboard-handlers.coverage.test.d.ts +1 -0
  53. package/dist/keyboard-handlers.coverage.test.js +495 -0
  54. package/dist/keyboard-handlers.js +7 -2
  55. package/dist/menus.js +1 -0
  56. package/dist/menus.test.d.ts +1 -0
  57. package/dist/menus.test.js +560 -0
  58. package/dist/named-views.coverage.test.d.ts +1 -0
  59. package/dist/named-views.coverage.test.js +180 -0
  60. package/dist/row-drag.d.ts +49 -0
  61. package/dist/row-drag.js +221 -0
  62. package/dist/row-drag.test.d.ts +1 -0
  63. package/dist/row-drag.test.js +142 -0
  64. package/dist/row-resize.test.d.ts +1 -0
  65. package/dist/row-resize.test.js +329 -0
  66. package/dist/scroll-sync.js +3 -0
  67. package/dist/scroll-sync.test.d.ts +1 -0
  68. package/dist/scroll-sync.test.js +290 -0
  69. package/dist/selection.d.ts +7 -1
  70. package/dist/selection.js +76 -36
  71. package/dist/selection.multi-range.test.d.ts +1 -0
  72. package/dist/selection.multi-range.test.js +55 -0
  73. package/dist/selection.test.d.ts +1 -0
  74. package/dist/selection.test.js +647 -0
  75. package/dist/server-data-source.coverage.test.d.ts +1 -0
  76. package/dist/server-data-source.coverage.test.js +154 -0
  77. package/dist/spreadsheet.d.ts +30 -0
  78. package/dist/spreadsheet.js +48 -0
  79. package/dist/spreadsheet.test.d.ts +1 -0
  80. package/dist/spreadsheet.test.js +446 -0
  81. package/dist/sv-grid-scrollbar.js +13 -1
  82. package/dist/svgrid-wrapper.types.d.ts +19 -0
  83. package/dist/svgrid.behavior.test.js +20 -0
  84. package/dist/svgrid.interaction.test.js +31 -0
  85. package/dist/svgrid.new-features.wrapper.test.js +34 -2
  86. package/dist/test-setup.js +9 -3
  87. package/dist/virtualization/scroll-scaling.d.ts +17 -0
  88. package/dist/virtualization/scroll-scaling.js +35 -0
  89. package/dist/virtualization/scroll-scaling.test.js +42 -1
  90. package/package.json +2 -1
  91. package/src/GridMenus.svelte +205 -127
  92. package/src/SvGrid.controller.svelte.ts +2352 -2195
  93. package/src/SvGrid.css +2012 -1747
  94. package/src/SvGrid.svelte +2346 -2047
  95. package/src/SvGrid.types.ts +537 -456
  96. package/src/aligned-grids.test.ts +80 -0
  97. package/src/aligned-grids.ts +87 -0
  98. package/src/build-api.ts +683 -663
  99. package/src/clipboard.test.ts +49 -0
  100. package/src/clipboard.ts +51 -23
  101. package/src/column-groups.test.ts +59 -0
  102. package/src/column-groups.ts +80 -0
  103. package/src/column-types.test.ts +68 -0
  104. package/src/column-types.ts +82 -0
  105. package/src/core.ts +78 -0
  106. package/src/editing.ts +669 -513
  107. package/src/index.ts +12 -0
  108. package/src/menus.ts +1 -0
  109. package/src/row-drag.test.ts +168 -0
  110. package/src/row-drag.ts +255 -0
  111. package/src/scroll-sync.ts +2 -0
  112. package/src/selection.multi-range.test.ts +61 -0
  113. package/src/selection.ts +71 -37
  114. package/src/spreadsheet.test.ts +489 -445
  115. package/src/spreadsheet.ts +304 -246
  116. package/src/svgrid-wrapper.types.ts +19 -0
  117. package/src/svgrid.new-features.wrapper.test.ts +2 -2
@@ -0,0 +1,180 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { attachAutoSavedView, createNamedViews, localStorageViews, memoryViews, } from './named-views';
3
+ function fakeHost(initial = {}) {
4
+ let state = { sorting: [], columnFilters: [], ...initial };
5
+ return {
6
+ getState: () => state,
7
+ setState: vi.fn((s) => {
8
+ state = { ...state, ...s };
9
+ }),
10
+ set: (s) => {
11
+ state = s;
12
+ },
13
+ };
14
+ }
15
+ describe('createNamedViews - list ordering and edge cases', () => {
16
+ it('lists views sorted by createdAt ascending', () => {
17
+ const store = memoryViews([
18
+ { name: 'late', state: {}, createdAt: 200 },
19
+ { name: 'early', state: {}, createdAt: 100 },
20
+ ]);
21
+ const views = createNamedViews(fakeHost(), { storage: store });
22
+ expect(views.list().map((v) => v.name)).toEqual(['early', 'late']);
23
+ });
24
+ it('defaults to memory storage when none is passed', () => {
25
+ const views = createNamedViews(fakeHost({ sorting: [{ id: 'a', desc: true }] }));
26
+ views.save('only');
27
+ expect(views.has('only')).toBe(true);
28
+ expect(views.list()).toHaveLength(1);
29
+ });
30
+ it('rename returns false when the source name is unknown', () => {
31
+ const views = createNamedViews(fakeHost(), { storage: memoryViews() });
32
+ views.save('a');
33
+ expect(views.rename('missing', 'b')).toBe(false);
34
+ expect(views.list().map((v) => v.name)).toEqual(['a']);
35
+ });
36
+ it('load returns false and does not call setState for unknown name', () => {
37
+ const host = fakeHost();
38
+ const views = createNamedViews(host, { storage: memoryViews() });
39
+ host.setState.mockClear();
40
+ expect(views.load('nope')).toBe(false);
41
+ expect(host.setState).not.toHaveBeenCalled();
42
+ });
43
+ it('save captures the current host state snapshot', () => {
44
+ const host = fakeHost({ sorting: [{ id: 'z', desc: false }] });
45
+ const views = createNamedViews(host, { storage: memoryViews() });
46
+ const v = views.save('snap');
47
+ expect(v.state).toMatchObject({ sorting: [{ id: 'z', desc: false }] });
48
+ expect(typeof v.createdAt).toBe('number');
49
+ });
50
+ });
51
+ describe('localStorageViews', () => {
52
+ beforeEach(() => localStorage.clear());
53
+ afterEach(() => localStorage.clear());
54
+ it('reads [] when the key is absent', () => {
55
+ const store = localStorageViews('svgrid:absent');
56
+ expect(store.read()).toEqual([]);
57
+ });
58
+ it('round-trips views through localStorage', () => {
59
+ const store = localStorageViews('svgrid:rt');
60
+ const views = [{ name: 'x', state: {}, createdAt: 1 }];
61
+ store.write(views);
62
+ expect(store.read()).toEqual(views);
63
+ // raw JSON is actually present
64
+ expect(JSON.parse(localStorage.getItem('svgrid:rt'))).toEqual(views);
65
+ });
66
+ it('returns [] for corrupt JSON', () => {
67
+ localStorage.setItem('svgrid:bad', '{not valid json');
68
+ expect(localStorageViews('svgrid:bad').read()).toEqual([]);
69
+ });
70
+ it('returns [] when the stored value is not an array', () => {
71
+ localStorage.setItem('svgrid:obj', JSON.stringify({ foo: 'bar' }));
72
+ expect(localStorageViews('svgrid:obj').read()).toEqual([]);
73
+ });
74
+ it('swallows write errors (quota / private mode)', () => {
75
+ const store = localStorageViews('svgrid:quota');
76
+ const spy = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
77
+ throw new DOMException('quota', 'QuotaExceededError');
78
+ });
79
+ expect(() => store.write([{ name: 'a', state: {}, createdAt: 1 }])).not.toThrow();
80
+ spy.mockRestore();
81
+ });
82
+ it('works end-to-end as a NamedViews adapter', () => {
83
+ const views = createNamedViews(fakeHost({ sorting: [{ id: 'p', desc: true }] }), {
84
+ storage: localStorageViews('svgrid:adapter'),
85
+ });
86
+ views.save('persisted');
87
+ const reopened = createNamedViews(fakeHost(), {
88
+ storage: localStorageViews('svgrid:adapter'),
89
+ });
90
+ expect(reopened.has('persisted')).toBe(true);
91
+ expect(reopened.load('persisted')).toBe(true);
92
+ });
93
+ });
94
+ describe('attachAutoSavedView', () => {
95
+ beforeEach(() => vi.useFakeTimers());
96
+ afterEach(() => vi.useRealTimers());
97
+ it('restores an existing autosave slot on attach', () => {
98
+ const host = fakeHost();
99
+ const store = memoryViews([
100
+ { name: '__autosave', state: { sorting: [{ id: 'restored', desc: true }] }, createdAt: 1 },
101
+ ]);
102
+ const views = createNamedViews(host, { storage: store });
103
+ const off = attachAutoSavedView(host, views);
104
+ expect(host.setState).toHaveBeenCalledWith(expect.objectContaining({ sorting: [{ id: 'restored', desc: true }] }));
105
+ off();
106
+ });
107
+ it('skips restore when skipRestore is set', () => {
108
+ const host = fakeHost();
109
+ const store = memoryViews([{ name: '__autosave', state: { sorting: [] }, createdAt: 1 }]);
110
+ const views = createNamedViews(host, { storage: store });
111
+ const off = attachAutoSavedView(host, views, { skipRestore: true });
112
+ expect(host.setState).not.toHaveBeenCalled();
113
+ off();
114
+ });
115
+ it('saves when the state snapshot changes after an interval', () => {
116
+ const host = fakeHost({ sorting: [] });
117
+ const views = createNamedViews(host, { storage: memoryViews() });
118
+ const off = attachAutoSavedView(host, views, { intervalMs: 500 });
119
+ expect(views.has('__autosave')).toBe(false);
120
+ // mutate the host state, then let the poller tick
121
+ host.set({ sorting: [{ id: 'new', desc: false }], columnFilters: [] });
122
+ vi.advanceTimersByTime(500);
123
+ expect(views.has('__autosave')).toBe(true);
124
+ off();
125
+ });
126
+ it('does not save when the snapshot is unchanged', () => {
127
+ const host = fakeHost({ sorting: [] });
128
+ const views = createNamedViews(host, { storage: memoryViews() });
129
+ const saveSpy = vi.spyOn(views, 'save');
130
+ const off = attachAutoSavedView(host, views, { intervalMs: 300 });
131
+ vi.advanceTimersByTime(900); // several ticks, no state change
132
+ expect(saveSpy).not.toHaveBeenCalled();
133
+ off();
134
+ });
135
+ it('uses a custom slot name', () => {
136
+ const host = fakeHost({ sorting: [] });
137
+ const views = createNamedViews(host, { storage: memoryViews() });
138
+ const off = attachAutoSavedView(host, views, { name: 'mySlot', intervalMs: 200 });
139
+ host.set({ sorting: [{ id: 'q', desc: true }], columnFilters: [] });
140
+ vi.advanceTimersByTime(200);
141
+ expect(views.has('mySlot')).toBe(true);
142
+ off();
143
+ });
144
+ it('clamps the interval to a minimum of 100ms', () => {
145
+ const host = fakeHost({ sorting: [] });
146
+ const views = createNamedViews(host, { storage: memoryViews() });
147
+ const setIntervalSpy = vi.spyOn(globalThis, 'setInterval');
148
+ const off = attachAutoSavedView(host, views, { intervalMs: 5 });
149
+ expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 100);
150
+ off();
151
+ setIntervalSpy.mockRestore();
152
+ });
153
+ it('recovers when getState throws inside the poll loop', () => {
154
+ let throwing = false;
155
+ const host = {
156
+ getState: () => {
157
+ if (throwing)
158
+ throw new Error('boom');
159
+ return { sorting: [], columnFilters: [] };
160
+ },
161
+ setState: vi.fn(),
162
+ };
163
+ const views = createNamedViews(host, { storage: memoryViews() });
164
+ const saveSpy = vi.spyOn(views, 'save');
165
+ const off = attachAutoSavedView(host, views, { intervalMs: 200 });
166
+ throwing = true;
167
+ expect(() => vi.advanceTimersByTime(400)).not.toThrow();
168
+ expect(saveSpy).not.toHaveBeenCalled();
169
+ off();
170
+ });
171
+ it('detach stops the polling interval', () => {
172
+ const host = fakeHost({ sorting: [] });
173
+ const views = createNamedViews(host, { storage: memoryViews() });
174
+ const clearSpy = vi.spyOn(globalThis, 'clearInterval');
175
+ const off = attachAutoSavedView(host, views, { intervalMs: 200 });
176
+ off();
177
+ expect(clearSpy).toHaveBeenCalled();
178
+ clearSpy.mockRestore();
179
+ });
180
+ });
@@ -0,0 +1,49 @@
1
+ export type RowDragEndEvent<TData = unknown> = {
2
+ row: TData;
3
+ toIndex: number;
4
+ sameGrid: boolean;
5
+ fromGridId: number;
6
+ toGridId: number;
7
+ };
8
+ export declare function createRowDrag<TFeatures, TData>(ctx: any): {
9
+ onRowDragStart: (e: DragEvent, rowIndex: number) => void;
10
+ onRowDragOver: (e: DragEvent, rowIndex: number) => void;
11
+ onRowDragLeave: (rowIndex: number) => void;
12
+ onRowDrop: (e: DragEvent, rowIndex: number) => void;
13
+ onRowsContainerDragOver: (e: DragEvent) => void;
14
+ onRowsContainerDrop: (e: DragEvent) => void;
15
+ onRowDragEnd: () => void;
16
+ rowDragGridId: number;
17
+ };
18
+ /** Options for the {@link rowDropZone} action. */
19
+ export type RowDropZoneOptions<TData = unknown> = {
20
+ /**
21
+ * Only accept rows dragged from a grid with this `rowDragGroup`. Omit to
22
+ * accept a managed row drag from any grid.
23
+ */
24
+ group?: string;
25
+ /**
26
+ * Called when a managed row is dropped on the element. The row has already
27
+ * been removed from its source grid; add it to your own list/state here.
28
+ */
29
+ onDrop: (payload: {
30
+ row: TData;
31
+ fromGridId: number;
32
+ }) => void;
33
+ /** Class toggled on the element while a matching row is dragged over it. */
34
+ overClass?: string;
35
+ };
36
+ /**
37
+ * Svelte action turning ANY element into an external drop target for managed
38
+ * row dragging - a trash can, an "assign to" bucket, a second pane. Attach with
39
+ * `use:rowDropZone={{ group, onDrop }}`. On drop the dragged row is removed from
40
+ * its source grid and handed to `onDrop`.
41
+ *
42
+ * ```svelte
43
+ * <div use:rowDropZone={{ group: 'board', onDrop: (e) => archive(e.row) }}>Archive</div>
44
+ * ```
45
+ */
46
+ export declare function rowDropZone<TData = unknown>(node: HTMLElement, options: RowDropZoneOptions<TData>): {
47
+ update(next: RowDropZoneOptions<TData>): void;
48
+ destroy(): void;
49
+ };
@@ -0,0 +1,221 @@
1
+ // Managed row dragging - reorder rows within a grid and move rows between
2
+ // grids that share a `rowDragGroup`. "Managed" means the grid mutates its own
3
+ // `internalData` on drop (splice out of the source, splice into the target) so
4
+ // consumers get working drag-and-drop with zero wiring; an optional
5
+ // `onRowDragEnd` callback lets them mirror the change into their own state.
6
+ //
7
+ // Cross-grid coordination rides on a module-level singleton (`bus`). Because
8
+ // every SvGrid instance in a bundle shares this module, the source grid can
9
+ // hand the target grid both the dragged row and a closure that removes it from
10
+ // the source - the missing half of an HTML5 drag payload, which only carries
11
+ // strings. Rows are matched by object identity, so this works without
12
+ // `getRowId`.
13
+ // Shared across all grids in the bundle - this is what makes grid-to-grid work.
14
+ let bus = null;
15
+ let gridSeq = 0;
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ export function createRowDrag(ctx) {
18
+ const gridId = ++gridSeq;
19
+ const managed = () => ctx.props.rowDragManaged === true;
20
+ const group = () => ctx.props.rowDragGroup ?? null;
21
+ function originalAt(rowIndex) {
22
+ const r = ctx.allRows[rowIndex];
23
+ return r ? r.original : null;
24
+ }
25
+ // Can THIS grid accept the row currently on the bus? Same grid always may
26
+ // (a reorder); a different grid only when both opt into the same group.
27
+ function canAccept() {
28
+ if (!managed() || !bus)
29
+ return false;
30
+ if (bus.gridId === gridId)
31
+ return true;
32
+ return bus.group != null && bus.group === group();
33
+ }
34
+ function clearIndicators() {
35
+ ctx.rowDragActive = false;
36
+ ctx.rowDropIndex = null;
37
+ ctx.rowDropSide = null;
38
+ }
39
+ function emitEnd(row, toIndex, sameGrid, fromGridId) {
40
+ ctx.props.onRowDragEnd?.({
41
+ row,
42
+ toIndex,
43
+ sameGrid,
44
+ fromGridId,
45
+ toGridId: gridId,
46
+ });
47
+ }
48
+ function onRowDragStart(e, rowIndex) {
49
+ if (!managed())
50
+ return;
51
+ const row = originalAt(rowIndex);
52
+ if (row == null)
53
+ return;
54
+ bus = {
55
+ group: group(),
56
+ gridId,
57
+ row,
58
+ sourceProps: ctx.props,
59
+ removeFromSource: () => {
60
+ ctx.internalData = ctx.internalData.filter((r) => r !== row);
61
+ },
62
+ };
63
+ ctx.rowDragActive = true;
64
+ try {
65
+ e.dataTransfer?.setData("text/plain", String(rowIndex));
66
+ if (e.dataTransfer)
67
+ e.dataTransfer.effectAllowed = "move";
68
+ }
69
+ catch {
70
+ // Some environments (tests) provide a partial dataTransfer.
71
+ }
72
+ }
73
+ function onRowDragOver(e, rowIndex) {
74
+ if (!canAccept())
75
+ return;
76
+ e.preventDefault();
77
+ if (e.dataTransfer)
78
+ e.dataTransfer.dropEffect = "move";
79
+ const rect = e.currentTarget.getBoundingClientRect();
80
+ ctx.rowDropIndex = rowIndex;
81
+ ctx.rowDropSide = e.clientY < rect.top + rect.height / 2 ? "before" : "after";
82
+ }
83
+ function onRowDragLeave(rowIndex) {
84
+ if (ctx.rowDropIndex === rowIndex) {
85
+ ctx.rowDropIndex = null;
86
+ ctx.rowDropSide = null;
87
+ }
88
+ }
89
+ function onRowDrop(e, rowIndex) {
90
+ if (!canAccept() || !bus)
91
+ return;
92
+ e.preventDefault();
93
+ e.stopPropagation(); // don't let the tbody handler also fire (end-append)
94
+ const side = ctx.rowDropSide ?? "before";
95
+ const dragged = bus.row;
96
+ const target = originalAt(rowIndex);
97
+ const sourceGridId = bus.gridId;
98
+ const sameGrid = sourceGridId === gridId;
99
+ clearIndicators();
100
+ const data = ctx.internalData.slice();
101
+ if (sameGrid) {
102
+ const from = data.indexOf(dragged);
103
+ if (from < 0) {
104
+ bus = null;
105
+ return;
106
+ }
107
+ data.splice(from, 1);
108
+ }
109
+ let to = target != null ? data.indexOf(target) : data.length;
110
+ if (to < 0)
111
+ to = data.length;
112
+ if (side === "after")
113
+ to += 1;
114
+ data.splice(to, 0, dragged);
115
+ ctx.internalData = data;
116
+ if (!sameGrid)
117
+ bus.removeFromSource();
118
+ emitEnd(dragged, to, sameGrid, sourceGridId);
119
+ bus = null;
120
+ }
121
+ // Dropping in the empty area below the last row, or into an empty target
122
+ // grid, appends. Row-level drops call stopPropagation so this only fires for
123
+ // genuine empty-space drops.
124
+ function onRowsContainerDragOver(e) {
125
+ if (!canAccept())
126
+ return;
127
+ e.preventDefault();
128
+ if (e.dataTransfer)
129
+ e.dataTransfer.dropEffect = "move";
130
+ }
131
+ function onRowsContainerDrop(e) {
132
+ if (!canAccept() || !bus)
133
+ return;
134
+ e.preventDefault();
135
+ const dragged = bus.row;
136
+ const sourceGridId = bus.gridId;
137
+ const sameGrid = sourceGridId === gridId;
138
+ clearIndicators();
139
+ const data = ctx.internalData.slice();
140
+ if (sameGrid) {
141
+ const from = data.indexOf(dragged);
142
+ if (from >= 0)
143
+ data.splice(from, 1);
144
+ }
145
+ data.push(dragged);
146
+ ctx.internalData = data;
147
+ if (!sameGrid)
148
+ bus.removeFromSource();
149
+ emitEnd(dragged, data.length - 1, sameGrid, sourceGridId);
150
+ bus = null;
151
+ }
152
+ function onRowDragEnd() {
153
+ clearIndicators();
154
+ bus = null;
155
+ }
156
+ return {
157
+ onRowDragStart,
158
+ onRowDragOver,
159
+ onRowDragLeave,
160
+ onRowDrop,
161
+ onRowsContainerDragOver,
162
+ onRowsContainerDrop,
163
+ onRowDragEnd,
164
+ rowDragGridId: gridId,
165
+ };
166
+ }
167
+ /**
168
+ * Svelte action turning ANY element into an external drop target for managed
169
+ * row dragging - a trash can, an "assign to" bucket, a second pane. Attach with
170
+ * `use:rowDropZone={{ group, onDrop }}`. On drop the dragged row is removed from
171
+ * its source grid and handed to `onDrop`.
172
+ *
173
+ * ```svelte
174
+ * <div use:rowDropZone={{ group: 'board', onDrop: (e) => archive(e.row) }}>Archive</div>
175
+ * ```
176
+ */
177
+ export function rowDropZone(node, options) {
178
+ let opts = options;
179
+ const cls = () => opts.overClass ?? "sv-grid-row-dropzone-over";
180
+ function matches() {
181
+ if (!bus)
182
+ return false;
183
+ return opts.group == null || bus.group === opts.group;
184
+ }
185
+ function onOver(e) {
186
+ if (!matches())
187
+ return;
188
+ e.preventDefault();
189
+ if (e.dataTransfer)
190
+ e.dataTransfer.dropEffect = "move";
191
+ node.classList.add(cls());
192
+ }
193
+ function onLeave() {
194
+ node.classList.remove(cls());
195
+ }
196
+ function onDrop(e) {
197
+ if (!matches() || !bus)
198
+ return;
199
+ e.preventDefault();
200
+ e.stopPropagation();
201
+ node.classList.remove(cls());
202
+ const row = bus.row;
203
+ const fromGridId = bus.gridId;
204
+ bus.removeFromSource();
205
+ bus = null;
206
+ opts.onDrop({ row, fromGridId });
207
+ }
208
+ node.addEventListener("dragover", onOver);
209
+ node.addEventListener("dragleave", onLeave);
210
+ node.addEventListener("drop", onDrop);
211
+ return {
212
+ update(next) {
213
+ opts = next;
214
+ },
215
+ destroy() {
216
+ node.removeEventListener("dragover", onOver);
217
+ node.removeEventListener("dragleave", onLeave);
218
+ node.removeEventListener("drop", onDrop);
219
+ },
220
+ };
221
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Unit tests for the managed row-drag handlers produced by createRowDrag(ctx).
3
+ *
4
+ * createRowDrag is a factory closing over a mutable `ctx` handle and a
5
+ * module-level singleton "bus" that carries the dragged row between grids.
6
+ * Because the bus is shared, we can create TWO fake ctxs (two grids) and drive
7
+ * a genuine grid-to-grid move without mounting anything.
8
+ */
9
+ import { describe, expect, it, vi } from 'vitest';
10
+ import { createRowDrag, rowDropZone } from './row-drag';
11
+ function makeCtx(rows, props = {}) {
12
+ return {
13
+ props,
14
+ internalData: rows,
15
+ get allRows() {
16
+ // allRows tracks internalData order; each Row wraps `.original`.
17
+ return this.internalData.map((r) => ({ original: r }));
18
+ },
19
+ rowDragActive: false,
20
+ rowDropIndex: null,
21
+ rowDropSide: null,
22
+ };
23
+ }
24
+ function fakeEvent(clientY = 0, rectTop = 0, rectHeight = 40) {
25
+ return {
26
+ clientY,
27
+ preventDefault: vi.fn(),
28
+ stopPropagation: vi.fn(),
29
+ dataTransfer: { setData: vi.fn(), effectAllowed: '', dropEffect: '' },
30
+ currentTarget: {
31
+ getBoundingClientRect: () => ({ top: rectTop, height: rectHeight, bottom: rectTop + rectHeight, left: 0 }),
32
+ },
33
+ };
34
+ }
35
+ const P = { rowDragManaged: true };
36
+ describe('createRowDrag - same-grid reorder', () => {
37
+ it('moves a dragged row after the drop target', () => {
38
+ const rows = [
39
+ { id: 1, title: 'a' },
40
+ { id: 2, title: 'b' },
41
+ { id: 3, title: 'c' },
42
+ { id: 4, title: 'd' },
43
+ ];
44
+ const ctx = makeCtx(rows, { ...P });
45
+ const h = createRowDrag(ctx);
46
+ h.onRowDragStart(fakeEvent(), 0); // drag "a"
47
+ ctx.rowDropSide = 'after';
48
+ h.onRowDrop(fakeEvent(), 2); // drop after "c"
49
+ expect(ctx.internalData.map((r) => r.title)).toEqual(['b', 'c', 'a', 'd']);
50
+ });
51
+ it('drops before the target when the side is "before"', () => {
52
+ const rows = [
53
+ { id: 1, title: 'a' },
54
+ { id: 2, title: 'b' },
55
+ { id: 3, title: 'c' },
56
+ ];
57
+ const ctx = makeCtx(rows, { ...P });
58
+ const h = createRowDrag(ctx);
59
+ h.onRowDragStart(fakeEvent(), 2); // drag "c"
60
+ ctx.rowDropSide = 'before';
61
+ h.onRowDrop(fakeEvent(), 0); // before "a"
62
+ expect(ctx.internalData.map((r) => r.title)).toEqual(['c', 'a', 'b']);
63
+ });
64
+ });
65
+ describe('createRowDrag - grid-to-grid', () => {
66
+ it('moves a row from source into target when groups match', () => {
67
+ const a = makeCtx([{ id: 1, title: 'a1' }, { id: 2, title: 'a2' }], { ...P, rowDragGroup: 'g' });
68
+ const b = makeCtx([{ id: 9, title: 'b1' }], { ...P, rowDragGroup: 'g', onRowDragEnd: vi.fn() });
69
+ const ha = createRowDrag(a);
70
+ const hb = createRowDrag(b);
71
+ ha.onRowDragStart(fakeEvent(), 1); // drag "a2" out of grid A
72
+ b.rowDropSide = 'before';
73
+ hb.onRowDrop(fakeEvent(), 0); // drop onto grid B before "b1"
74
+ expect(a.internalData.map((r) => r.title)).toEqual(['a1']);
75
+ expect(b.internalData.map((r) => r.title)).toEqual(['a2', 'b1']);
76
+ expect(b.props.onRowDragEnd).toHaveBeenCalledWith(expect.objectContaining({ sameGrid: false, toIndex: 0, row: expect.objectContaining({ title: 'a2' }) }));
77
+ });
78
+ it('does NOT accept a drop when groups differ', () => {
79
+ const a = makeCtx([{ id: 1, title: 'a1' }, { id: 2, title: 'a2' }], { ...P, rowDragGroup: 'g' });
80
+ const b = makeCtx([{ id: 9, title: 'b1' }], { ...P, rowDragGroup: 'other' });
81
+ const ha = createRowDrag(a);
82
+ const hb = createRowDrag(b);
83
+ ha.onRowDragStart(fakeEvent(), 0);
84
+ const ev = fakeEvent();
85
+ hb.onRowDragOver(ev, 0);
86
+ expect(ev.preventDefault).not.toHaveBeenCalled(); // over rejected
87
+ hb.onRowDrop(fakeEvent(), 0);
88
+ expect(b.internalData.map((r) => r.title)).toEqual(['b1']); // unchanged
89
+ expect(a.internalData.length).toBe(2); // source untouched
90
+ });
91
+ it('appends to the target on an empty-space (container) drop', () => {
92
+ const a = makeCtx([{ id: 1, title: 'a1' }, { id: 2, title: 'a2' }], { ...P, rowDragGroup: 'g' });
93
+ const b = makeCtx([{ id: 9, title: 'b1' }], { ...P, rowDragGroup: 'g' });
94
+ const ha = createRowDrag(a);
95
+ const hb = createRowDrag(b);
96
+ ha.onRowDragStart(fakeEvent(), 0); // drag "a1"
97
+ hb.onRowsContainerDrop(fakeEvent());
98
+ expect(b.internalData.map((r) => r.title)).toEqual(['b1', 'a1']);
99
+ expect(a.internalData.map((r) => r.title)).toEqual(['a2']);
100
+ });
101
+ });
102
+ describe('createRowDrag - disabled', () => {
103
+ it('is inert when rowDragManaged is off', () => {
104
+ const ctx = makeCtx([{ id: 1, title: 'a' }, { id: 2, title: 'b' }], {});
105
+ const h = createRowDrag(ctx);
106
+ h.onRowDragStart(fakeEvent(), 0);
107
+ ctx.rowDropSide = 'after';
108
+ h.onRowDrop(fakeEvent(), 1);
109
+ expect(ctx.internalData.map((r) => r.title)).toEqual(['a', 'b']);
110
+ });
111
+ });
112
+ describe('rowDropZone (external drop target)', () => {
113
+ function dropOn(el) {
114
+ const ev = new Event('drop', { bubbles: true, cancelable: true });
115
+ el.dispatchEvent(ev);
116
+ }
117
+ it('removes the dragged row from its source grid and calls onDrop', () => {
118
+ const a = makeCtx([{ id: 1, title: 'a1' }, { id: 2, title: 'a2' }], { ...P, rowDragGroup: 'g' });
119
+ const ha = createRowDrag(a);
120
+ ha.onRowDragStart(fakeEvent(), 0); // start dragging "a1"
121
+ const el = document.createElement('div');
122
+ const dropped = [];
123
+ const action = rowDropZone(el, { group: 'g', onDrop: (e) => dropped.push(e) });
124
+ dropOn(el);
125
+ expect(dropped.length).toBe(1);
126
+ expect(dropped[0].row.title).toBe('a1');
127
+ expect(a.internalData.map((r) => r.title)).toEqual(['a2']); // removed from source
128
+ action.destroy();
129
+ });
130
+ it('ignores drops when the group does not match', () => {
131
+ const a = makeCtx([{ id: 1, title: 'a1' }], { ...P, rowDragGroup: 'g' });
132
+ const ha = createRowDrag(a);
133
+ ha.onRowDragStart(fakeEvent(), 0);
134
+ const el = document.createElement('div');
135
+ const dropped = [];
136
+ const action = rowDropZone(el, { group: 'other', onDrop: (e) => dropped.push(e) });
137
+ dropOn(el);
138
+ expect(dropped.length).toBe(0);
139
+ expect(a.internalData.length).toBe(1); // untouched
140
+ action.destroy();
141
+ });
142
+ });
@@ -0,0 +1 @@
1
+ export {};