@svgrid/grid 2.6.22 → 2.6.23
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/dist/GridMenus.svelte +184 -6
- package/dist/SvAutoComplete.svelte +4 -0
- package/dist/SvComboBox.svelte +5 -1
- package/dist/SvDateRangeInput.svelte +25 -3
- package/dist/SvDropDownList.svelte +4 -0
- package/dist/SvForm.svelte +8 -8
- package/dist/SvGrid.controller.svelte.d.ts +48 -1
- package/dist/SvGrid.controller.svelte.js +214 -52
- package/dist/SvGrid.css +221 -0
- package/dist/SvGrid.helpers.d.ts +55 -0
- package/dist/SvGrid.helpers.js +83 -0
- package/dist/SvGrid.svelte +145 -0
- package/dist/SvGrid.types.d.ts +105 -0
- package/dist/SvGridCellEditor.svelte +99 -17
- package/dist/SvGridSelect.svelte +6 -2
- package/dist/SvGridSelect.svelte.d.ts +1 -1
- package/dist/SvListBox.svelte +8 -0
- package/dist/SvMultiSelect.svelte +6 -2
- package/dist/SvMultiSelect.svelte.d.ts +1 -1
- package/dist/SvNumberInput.svelte +4 -0
- package/dist/SvRichCell.svelte +125 -0
- package/dist/SvRichCell.svelte.d.ts +15 -0
- package/dist/SvTextArea.svelte +5 -1
- package/dist/SvTreeSelect.svelte +6 -2
- package/dist/SvTreeSelect.svelte.d.ts +1 -1
- package/dist/builtin-editors.d.ts +2 -2
- package/dist/builtin-editors.js +19 -3
- package/dist/cdn/GridMenus-BzWXQjv3.js +600 -0
- package/dist/cdn/GridMenus-xr_rHx8F.js +601 -0
- package/dist/cdn/SvDateRangeInput-CaOuMs8O.js +198 -0
- package/dist/cdn/SvDateRangeInput-DMLKmGEc.js +199 -0
- package/dist/cdn/SvDateTimePicker-CCbDZNZB.js +816 -0
- package/dist/cdn/SvDateTimePicker-sonaH0oh.js +812 -0
- package/dist/cdn/SvGridCellEditor-BAPSC7EL.js +550 -0
- package/dist/cdn/SvGridCellEditor-BwzmUWtL.js +549 -0
- package/dist/cdn/date-format-BNii4zeD.js +1393 -0
- package/dist/cdn/date-format-BnnHlqGw.js +1395 -0
- package/dist/cdn/{editor-registry-CiI0xlKg.js → editor-registry-BhgE3S84.js} +51 -25
- package/dist/cdn/{src-D1lXwq1l.js → src-DGo7IHug.js} +5730 -5198
- package/dist/cdn/{src-C9Hihx1W.js → src-skQN5eqh.js} +7216 -6684
- package/dist/cdn/svgrid.js +14 -12
- package/dist/cdn/svgrid.svelte-external.js +14 -12
- package/dist/cell-values.d.ts +16 -0
- package/dist/cell-values.js +28 -0
- package/dist/clipboard.d.ts +22 -0
- package/dist/clipboard.js +405 -27
- package/dist/editing.js +12 -7
- package/dist/editors/cell-editors.d.ts +11 -0
- package/dist/editors/cell-editors.js +18 -1
- package/dist/grid-messages.d.ts +15 -0
- package/dist/grid-messages.js +15 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/list-option.d.ts +6 -0
- package/dist/menus.js +40 -7
- package/dist/sanitize-html.d.ts +37 -0
- package/dist/sanitize-html.js +234 -0
- package/dist/selection-bar-view.svelte.d.ts +25 -0
- package/dist/selection-bar-view.svelte.js +13 -0
- package/dist/selection.d.ts +2 -1
- package/dist/selection.js +129 -13
- package/package.json +1 -1
- package/src/GridMenus.svelte +184 -6
- package/src/SvAutoComplete.svelte +4 -0
- package/src/SvComboBox.svelte +5 -1
- package/src/SvDateRangeInput.svelte +25 -3
- package/src/SvDropDownList.svelte +4 -0
- package/src/SvForm.svelte +8 -8
- package/src/SvGrid.controller.svelte.ts +251 -61
- package/src/SvGrid.css +221 -0
- package/src/SvGrid.helpers.ts +87 -0
- package/src/SvGrid.svelte +145 -0
- package/src/SvGrid.types.ts +112 -0
- package/src/SvGridCellEditor.svelte +99 -17
- package/src/SvGridSelect.svelte +6 -2
- package/src/SvListBox.svelte +8 -0
- package/src/SvMultiSelect.svelte +6 -2
- package/src/SvNumberInput.svelte +4 -0
- package/src/SvRichCell.svelte +125 -0
- package/src/SvTextArea.svelte +5 -1
- package/src/SvTreeSelect.svelte +6 -2
- package/src/builtin-editors.test.ts +18 -1
- package/src/builtin-editors.ts +19 -3
- package/src/cell-values.ts +30 -0
- package/src/clipboard.test.ts +93 -20
- package/src/clipboard.ts +433 -33
- package/src/date-string-column.test.ts +87 -0
- package/src/editing.test.ts +25 -0
- package/src/editing.ts +12 -9
- package/src/editor-block-prop.test.ts +132 -0
- package/src/editors/cell-editors.ts +27 -1
- package/src/grid-messages.ts +34 -0
- package/src/index.ts +13 -0
- package/src/list-option.ts +6 -0
- package/src/menus.test.ts +43 -0
- package/src/menus.ts +37 -7
- package/src/move-cells.test.ts +850 -0
- package/src/number-editor-literal.test.ts +84 -0
- package/src/number-editor.grid.test.ts +160 -0
- package/src/sanitize-html.test.ts +182 -0
- package/src/sanitize-html.ts +235 -0
- package/src/selection-bar-view.svelte.ts +36 -0
- package/src/selection.test.ts +157 -0
- package/src/selection.ts +131 -13
- package/src/svgrid.filter-menu-listbox.svelte.test.ts +56 -10
- package/src/svgrid.in-suggest-snapshot.svelte.test.ts +91 -0
- package/src/svgrid.menu-scroll-close.test.ts +204 -0
- package/src/svgrid.selection-bar-seam.test.ts +185 -0
- package/src/svgrid.upsell-license.test.ts +117 -0
- package/dist/cdn/GridMenus-BuoBPqxx.js +0 -493
- package/dist/cdn/GridMenus-n4llxoOI.js +0 -494
- package/dist/cdn/SvDateTimePicker-CHnUkWcH.js +0 -2206
- package/dist/cdn/SvDateTimePicker-CRQH_U7E.js +0 -2201
- package/dist/cdn/SvGridCellEditor-BGUCzuPB.js +0 -523
- package/dist/cdn/SvGridCellEditor-G8IMWIws.js +0 -522
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createClipboard } from './clipboard'
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Excel-style range move / copy (the `moveCells` prop): grab a selection
|
|
6
|
+
// border, drag it somewhere else, drop.
|
|
7
|
+
//
|
|
8
|
+
// Same hand-rolled `ctx` approach as clipboard.test.ts - the handlers talk to
|
|
9
|
+
// the controller only through this handle - but with the extra surface the
|
|
10
|
+
// move path needs: history, an editability predicate that can differ per cell,
|
|
11
|
+
// and a selection-rect view that can hold more than one range.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
interface FakeOptions {
|
|
15
|
+
columns?: Array<{ id: string; field?: string }>
|
|
16
|
+
data?: Array<Record<string, unknown>>
|
|
17
|
+
editableAt?: (r: number, c: number) => boolean
|
|
18
|
+
moveCells?: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function makeCtx(opts: FakeOptions = {}) {
|
|
22
|
+
const columns = opts.columns ?? [
|
|
23
|
+
{ id: 'a', field: 'a' },
|
|
24
|
+
{ id: 'b', field: 'b' },
|
|
25
|
+
{ id: 'c', field: 'c' },
|
|
26
|
+
]
|
|
27
|
+
let backing = (
|
|
28
|
+
opts.data ?? [
|
|
29
|
+
{ a: 'a0', b: 'b0', c: 'c0' },
|
|
30
|
+
{ a: 'a1', b: 'b1', c: 'c1' },
|
|
31
|
+
{ a: 'a2', b: 'b2', c: 'c2' },
|
|
32
|
+
{ a: 'a3', b: 'b3', c: 'c3' },
|
|
33
|
+
]
|
|
34
|
+
).map((d) => ({ ...d }))
|
|
35
|
+
|
|
36
|
+
const allColumns = columns.map((c) => ({
|
|
37
|
+
id: c.id,
|
|
38
|
+
columnDef: { field: c.field },
|
|
39
|
+
}))
|
|
40
|
+
|
|
41
|
+
const buildRows = (rows: Array<Record<string, unknown>>) =>
|
|
42
|
+
rows.map((row, index) => ({
|
|
43
|
+
id: String(index),
|
|
44
|
+
original: row,
|
|
45
|
+
getCanExpand: () => false,
|
|
46
|
+
getCellValueByColumnId: (colId: string) => row[colId],
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
let rows = buildRows(backing)
|
|
50
|
+
|
|
51
|
+
const ctx: any = {
|
|
52
|
+
allColumns,
|
|
53
|
+
selectionRange: { anchor: null, focus: null } as any,
|
|
54
|
+
selectionRanges: [] as any[],
|
|
55
|
+
fillDrag: null,
|
|
56
|
+
moveDrag: null,
|
|
57
|
+
moveGrabHover: false,
|
|
58
|
+
moveCellsEffective: opts.moveCells ?? true,
|
|
59
|
+
editedCellValues: {} as Record<string, unknown>,
|
|
60
|
+
userHasActivatedCell: false,
|
|
61
|
+
gridRootEl: null,
|
|
62
|
+
props: {} as Record<string, unknown>,
|
|
63
|
+
history: [] as any[],
|
|
64
|
+
historyPtr: -1,
|
|
65
|
+
historyVersion: 0,
|
|
66
|
+
UNDO_LIMIT: 200,
|
|
67
|
+
activeCell: null as { rowIndex: number; colIndex: number } | null,
|
|
68
|
+
setActiveCell(rowIndex: number, colIndex: number) {
|
|
69
|
+
ctx.activeCell = { rowIndex, colIndex }
|
|
70
|
+
},
|
|
71
|
+
findColumnById: (id: string) => allColumns.find((c) => c.id === id),
|
|
72
|
+
isCellEditableAt: opts.editableAt ?? (() => true),
|
|
73
|
+
getCellDisplayValue: (_r: string, _c: string, base: unknown) => base,
|
|
74
|
+
grid: { getState: () => ({ activeCell: ctx.activeCell }), store: { setState: vi.fn() } },
|
|
75
|
+
getSelectionRects() {
|
|
76
|
+
const norm = (r: any) =>
|
|
77
|
+
r?.anchor && r?.focus
|
|
78
|
+
? {
|
|
79
|
+
minRow: Math.min(r.anchor.rowIndex, r.focus.rowIndex),
|
|
80
|
+
maxRow: Math.max(r.anchor.rowIndex, r.focus.rowIndex),
|
|
81
|
+
minCol: Math.min(r.anchor.colIndex, r.focus.colIndex),
|
|
82
|
+
maxCol: Math.max(r.anchor.colIndex, r.focus.colIndex),
|
|
83
|
+
}
|
|
84
|
+
: null
|
|
85
|
+
const rects: any[] = []
|
|
86
|
+
for (const r of ctx.selectionRanges) {
|
|
87
|
+
const n = norm(r)
|
|
88
|
+
if (n) rects.push(n)
|
|
89
|
+
}
|
|
90
|
+
const active = norm(ctx.selectionRange)
|
|
91
|
+
if (active) rects.push(active)
|
|
92
|
+
return rects
|
|
93
|
+
},
|
|
94
|
+
// Mirrors the real getCellRangeEdges: first containing rect wins.
|
|
95
|
+
getCellRangeEdges(rowIndex: number, colIndex: number) {
|
|
96
|
+
for (const rect of ctx.getSelectionRects()) {
|
|
97
|
+
if (
|
|
98
|
+
rowIndex < rect.minRow ||
|
|
99
|
+
rowIndex > rect.maxRow ||
|
|
100
|
+
colIndex < rect.minCol ||
|
|
101
|
+
colIndex > rect.maxCol
|
|
102
|
+
)
|
|
103
|
+
continue
|
|
104
|
+
return {
|
|
105
|
+
top: rowIndex === rect.minRow,
|
|
106
|
+
bottom: rowIndex === rect.maxRow,
|
|
107
|
+
left: colIndex === rect.minCol,
|
|
108
|
+
right: colIndex === rect.maxCol,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return null
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
Object.defineProperty(ctx, 'internalData', {
|
|
116
|
+
get: () => backing,
|
|
117
|
+
set: (next: Array<Record<string, unknown>>) => {
|
|
118
|
+
backing = next
|
|
119
|
+
rows = buildRows(next)
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
Object.defineProperty(ctx, 'allRows', { get: () => rows })
|
|
123
|
+
|
|
124
|
+
return ctx
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Snapshot the grid as a plain matrix so assertions read like the screen. */
|
|
128
|
+
function grid(ctx: any) {
|
|
129
|
+
return ctx.internalData.map((row: Record<string, unknown>) =>
|
|
130
|
+
ctx.allColumns.map((col: any) => row[col.columnDef.field as string]),
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Select the rectangle (r0,c0)-(r1,c1) as the ACTIVE range. */
|
|
135
|
+
function select(ctx: any, r0: number, c0: number, r1: number, c1: number) {
|
|
136
|
+
ctx.selectionRange = {
|
|
137
|
+
anchor: { rowIndex: r0, colIndex: c0 },
|
|
138
|
+
focus: { rowIndex: r1, colIndex: c1 },
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Run a whole drag: grab (grabRow,grabCol), drop on (targetRow,targetCol). */
|
|
143
|
+
function drop(
|
|
144
|
+
cb: ReturnType<typeof createClipboard>,
|
|
145
|
+
ctx: any,
|
|
146
|
+
grab: [number, number],
|
|
147
|
+
target: [number, number],
|
|
148
|
+
copy = false,
|
|
149
|
+
) {
|
|
150
|
+
ctx.moveDrag = {
|
|
151
|
+
sourceMinRow: Math.min(ctx.selectionRange.anchor.rowIndex, ctx.selectionRange.focus.rowIndex),
|
|
152
|
+
sourceMaxRow: Math.max(ctx.selectionRange.anchor.rowIndex, ctx.selectionRange.focus.rowIndex),
|
|
153
|
+
sourceMinCol: Math.min(ctx.selectionRange.anchor.colIndex, ctx.selectionRange.focus.colIndex),
|
|
154
|
+
sourceMaxCol: Math.max(ctx.selectionRange.anchor.colIndex, ctx.selectionRange.focus.colIndex),
|
|
155
|
+
grabRow: grab[0],
|
|
156
|
+
grabCol: grab[1],
|
|
157
|
+
targetRow: target[0],
|
|
158
|
+
targetCol: target[1],
|
|
159
|
+
copy,
|
|
160
|
+
}
|
|
161
|
+
cb.onMovePointerUp()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
describe('moveDestRect', () => {
|
|
165
|
+
const base = {
|
|
166
|
+
sourceMinRow: 1,
|
|
167
|
+
sourceMaxRow: 2,
|
|
168
|
+
sourceMinCol: 0,
|
|
169
|
+
sourceMaxCol: 1,
|
|
170
|
+
grabRow: 1,
|
|
171
|
+
grabCol: 0,
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
it('offsets the source rectangle by how far the pointer travelled', () => {
|
|
175
|
+
const cb = createClipboard(makeCtx())
|
|
176
|
+
expect(cb.moveDestRect({ ...base, targetRow: 2, targetCol: 1 })).toEqual({
|
|
177
|
+
minRow: 2,
|
|
178
|
+
maxRow: 3,
|
|
179
|
+
minCol: 1,
|
|
180
|
+
maxCol: 2,
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
it('measures the offset from the GRAB point, not the rectangle corner', () => {
|
|
185
|
+
const cb = createClipboard(makeCtx())
|
|
186
|
+
// Grabbed the bottom-right cell of the range and dropped it one row down:
|
|
187
|
+
// the whole rectangle shifts by one, it does not jump to the pointer.
|
|
188
|
+
expect(
|
|
189
|
+
cb.moveDestRect({ ...base, grabRow: 2, grabCol: 1, targetRow: 3, targetCol: 1 }),
|
|
190
|
+
).toEqual({ minRow: 2, maxRow: 3, minCol: 0, maxCol: 1 })
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('is null when the drag never left the cell it started on', () => {
|
|
194
|
+
const cb = createClipboard(makeCtx())
|
|
195
|
+
expect(cb.moveDestRect({ ...base, targetRow: 1, targetCol: 0 })).toBeNull()
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('is null past every edge of the grid rather than clamping', () => {
|
|
199
|
+
// 4 rows, 3 columns. The source is rows 1-2 x cols 0-1, so each of these
|
|
200
|
+
// is a drag a user could really make - the destination just does not fit.
|
|
201
|
+
const cb = createClipboard(makeCtx())
|
|
202
|
+
// Grab the bottom row, drop it on the top row: the rectangle starts at -1.
|
|
203
|
+
expect(cb.moveDestRect({ ...base, grabRow: 2, targetRow: 0, targetCol: 0 })).toBeNull()
|
|
204
|
+
// Grab the right column, drop it on the left one: starts at column -1.
|
|
205
|
+
expect(cb.moveDestRect({ ...base, grabCol: 1, targetRow: 1, targetCol: 0 })).toBeNull()
|
|
206
|
+
expect(cb.moveDestRect({ ...base, targetRow: 3, targetCol: 0 })).toBeNull() // past row 3
|
|
207
|
+
expect(cb.moveDestRect({ ...base, targetRow: 1, targetCol: 2 })).toBeNull() // past column 2
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('allows a shift that lands exactly on the first row', () => {
|
|
211
|
+
// The guard is "outside the grid", not "close to the edge" - one row up
|
|
212
|
+
// from row 1 is row 0, which is a perfectly good destination.
|
|
213
|
+
const cb = createClipboard(makeCtx())
|
|
214
|
+
expect(cb.moveDestRect({ ...base, targetRow: 0, targetCol: 0 })).toEqual({
|
|
215
|
+
minRow: 0,
|
|
216
|
+
maxRow: 1,
|
|
217
|
+
minCol: 0,
|
|
218
|
+
maxCol: 1,
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
describe('applyMoveRange - moving', () => {
|
|
224
|
+
it('moves the values and blanks the cells they came from', () => {
|
|
225
|
+
const ctx = makeCtx()
|
|
226
|
+
const cb = createClipboard(ctx)
|
|
227
|
+
select(ctx, 0, 0, 1, 0)
|
|
228
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
229
|
+
expect(grid(ctx)).toEqual([
|
|
230
|
+
[null, 'a0', 'c0'],
|
|
231
|
+
[null, 'a1', 'c1'],
|
|
232
|
+
['a2', 'b2', 'c2'],
|
|
233
|
+
['a3', 'b3', 'c3'],
|
|
234
|
+
])
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('leaves no ghost when the source and destination overlap', () => {
|
|
238
|
+
const ctx = makeCtx()
|
|
239
|
+
const cb = createClipboard(ctx)
|
|
240
|
+
// Three rows of column a, nudged down by one: rows 1-3 take the old
|
|
241
|
+
// 0-2 values and row 0 is emptied. If the source were read lazily instead
|
|
242
|
+
// of snapshotted, row 2 would receive the value row 1 had just been given.
|
|
243
|
+
select(ctx, 0, 0, 2, 0)
|
|
244
|
+
drop(cb, ctx, [0, 0], [1, 0])
|
|
245
|
+
expect(grid(ctx).map((r: unknown[]) => r[0])).toEqual([null, 'a0', 'a1', 'a2'])
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('carries a 2x2 block across intact', () => {
|
|
249
|
+
const ctx = makeCtx()
|
|
250
|
+
const cb = createClipboard(ctx)
|
|
251
|
+
select(ctx, 0, 0, 1, 1)
|
|
252
|
+
drop(cb, ctx, [0, 0], [2, 1])
|
|
253
|
+
expect(grid(ctx)).toEqual([
|
|
254
|
+
[null, null, 'c0'],
|
|
255
|
+
[null, null, 'c1'],
|
|
256
|
+
['a2', 'a0', 'b0'],
|
|
257
|
+
['a3', 'a1', 'b1'],
|
|
258
|
+
])
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('moves the range whose border was grabbed, not the active one', () => {
|
|
262
|
+
const ctx = makeCtx()
|
|
263
|
+
const cb = createClipboard(ctx)
|
|
264
|
+
// A committed range on column a, plus an active one on column c.
|
|
265
|
+
ctx.selectionRanges = [
|
|
266
|
+
{ anchor: { rowIndex: 0, colIndex: 0 }, focus: { rowIndex: 0, colIndex: 0 } },
|
|
267
|
+
]
|
|
268
|
+
select(ctx, 3, 2, 3, 2)
|
|
269
|
+
// startMoveDrag resolves the source through getSelectionRects, so seed the
|
|
270
|
+
// drag the same way the pointer path would for the COMMITTED rectangle.
|
|
271
|
+
ctx.moveDrag = {
|
|
272
|
+
sourceMinRow: 0,
|
|
273
|
+
sourceMaxRow: 0,
|
|
274
|
+
sourceMinCol: 0,
|
|
275
|
+
sourceMaxCol: 0,
|
|
276
|
+
grabRow: 0,
|
|
277
|
+
grabCol: 0,
|
|
278
|
+
targetRow: 1,
|
|
279
|
+
targetCol: 0,
|
|
280
|
+
copy: false,
|
|
281
|
+
}
|
|
282
|
+
cb.onMovePointerUp()
|
|
283
|
+
expect(grid(ctx)[0]![0]).toBeNull()
|
|
284
|
+
expect(grid(ctx)[1]![0]).toBe('a0')
|
|
285
|
+
// The active range's own cell is untouched.
|
|
286
|
+
expect(grid(ctx)[3]![2]).toBe('c3')
|
|
287
|
+
})
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
describe('applyMoveRange - copying', () => {
|
|
291
|
+
it('leaves the source in place when copy is held', () => {
|
|
292
|
+
const ctx = makeCtx()
|
|
293
|
+
const cb = createClipboard(ctx)
|
|
294
|
+
select(ctx, 0, 0, 1, 0)
|
|
295
|
+
drop(cb, ctx, [0, 0], [0, 1], true)
|
|
296
|
+
expect(grid(ctx)).toEqual([
|
|
297
|
+
['a0', 'a0', 'c0'],
|
|
298
|
+
['a1', 'a1', 'c1'],
|
|
299
|
+
['a2', 'b2', 'c2'],
|
|
300
|
+
['a3', 'b3', 'c3'],
|
|
301
|
+
])
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
it('copies even out of read-only source cells', () => {
|
|
305
|
+
// Nothing is written to the source, so its editability is irrelevant -
|
|
306
|
+
// a read-only column is still copyable, which is the point of copy.
|
|
307
|
+
const ctx = makeCtx({ editableAt: (_r, c) => c !== 0 })
|
|
308
|
+
const cb = createClipboard(ctx)
|
|
309
|
+
select(ctx, 0, 0, 0, 0)
|
|
310
|
+
drop(cb, ctx, [0, 0], [0, 1], true)
|
|
311
|
+
expect(grid(ctx)[0]).toEqual(['a0', 'a0', 'c0'])
|
|
312
|
+
})
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
describe('applyMoveRange - refusals', () => {
|
|
316
|
+
it('changes nothing when the destination runs off the grid', () => {
|
|
317
|
+
const ctx = makeCtx()
|
|
318
|
+
const cb = createClipboard(ctx)
|
|
319
|
+
const before = grid(ctx)
|
|
320
|
+
select(ctx, 2, 0, 3, 0)
|
|
321
|
+
drop(cb, ctx, [2, 0], [3, 0]) // would need rows 3-4; there is no row 4
|
|
322
|
+
expect(grid(ctx)).toEqual(before)
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('refuses the whole move when ANY destination cell is read-only', () => {
|
|
326
|
+
// Half-applying would split the range across two places with nothing on
|
|
327
|
+
// screen to say which half landed.
|
|
328
|
+
const ctx = makeCtx({ editableAt: (r, c) => !(c === 1 && r === 1) })
|
|
329
|
+
const cb = createClipboard(ctx)
|
|
330
|
+
const before = grid(ctx)
|
|
331
|
+
select(ctx, 0, 0, 1, 0)
|
|
332
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
333
|
+
expect(grid(ctx)).toEqual(before)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
it('refuses a move out of a read-only source, which would have to be blanked', () => {
|
|
337
|
+
const ctx = makeCtx({ editableAt: (_r, c) => c !== 0 })
|
|
338
|
+
const cb = createClipboard(ctx)
|
|
339
|
+
const before = grid(ctx)
|
|
340
|
+
select(ctx, 0, 0, 0, 0)
|
|
341
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
342
|
+
expect(grid(ctx)).toEqual(before)
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
it('refuses when a column on either side has no field to write back to', () => {
|
|
346
|
+
const ctx = makeCtx({
|
|
347
|
+
columns: [{ id: 'a', field: 'a' }, { id: 'computed' }, { id: 'c', field: 'c' }],
|
|
348
|
+
})
|
|
349
|
+
const cb = createClipboard(ctx)
|
|
350
|
+
const before = grid(ctx)
|
|
351
|
+
select(ctx, 0, 0, 0, 0)
|
|
352
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
353
|
+
expect(grid(ctx)).toEqual(before)
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
it('clears the drag even when it refuses, so the pointer stops being tracked', () => {
|
|
357
|
+
const ctx = makeCtx()
|
|
358
|
+
const cb = createClipboard(ctx)
|
|
359
|
+
select(ctx, 2, 0, 3, 0)
|
|
360
|
+
drop(cb, ctx, [2, 0], [3, 0])
|
|
361
|
+
expect(ctx.moveDrag).toBeNull()
|
|
362
|
+
})
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
describe('applyMoveRange - aftermath', () => {
|
|
366
|
+
it('records one undo step per changed cell', () => {
|
|
367
|
+
const ctx = makeCtx()
|
|
368
|
+
const cb = createClipboard(ctx)
|
|
369
|
+
select(ctx, 0, 0, 1, 0)
|
|
370
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
371
|
+
// 2 source cells blanked + 2 destination cells written.
|
|
372
|
+
expect(ctx.history).toHaveLength(4)
|
|
373
|
+
expect(ctx.historyPtr).toBe(3)
|
|
374
|
+
expect(ctx.history[0]).toMatchObject({ rowId: '0', columnId: 'a', before: 'a0', after: null })
|
|
375
|
+
expect(ctx.history[2]).toMatchObject({ rowId: '0', columnId: 'b', before: 'b0', after: 'a0' })
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
it('truncates any redo history the way a normal edit does', () => {
|
|
379
|
+
const ctx = makeCtx()
|
|
380
|
+
ctx.history = [{ rowId: 'x', columnId: 'a', field: 'a', before: 1, after: 2 }]
|
|
381
|
+
ctx.historyPtr = -1 // an undone step, still redoable
|
|
382
|
+
const cb = createClipboard(ctx)
|
|
383
|
+
select(ctx, 0, 0, 0, 0)
|
|
384
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
385
|
+
expect(ctx.history.map((s: any) => s.rowId)).not.toContain('x')
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
it('leaves the moved range selected where it landed', () => {
|
|
389
|
+
const ctx = makeCtx()
|
|
390
|
+
const cb = createClipboard(ctx)
|
|
391
|
+
select(ctx, 0, 0, 1, 1)
|
|
392
|
+
drop(cb, ctx, [0, 0], [2, 1])
|
|
393
|
+
expect(ctx.selectionRange).toEqual({
|
|
394
|
+
anchor: { rowIndex: 2, colIndex: 1 },
|
|
395
|
+
focus: { rowIndex: 3, colIndex: 2 },
|
|
396
|
+
})
|
|
397
|
+
expect(ctx.selectionRanges).toEqual([])
|
|
398
|
+
expect(ctx.activeCell).toEqual({ rowIndex: 2, colIndex: 1 })
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
it('reports every write through onCellValueChange', () => {
|
|
402
|
+
const ctx = makeCtx()
|
|
403
|
+
const onCellValueChange = vi.fn()
|
|
404
|
+
ctx.props.onCellValueChange = onCellValueChange
|
|
405
|
+
const cb = createClipboard(ctx)
|
|
406
|
+
select(ctx, 0, 0, 0, 0)
|
|
407
|
+
drop(cb, ctx, [0, 0], [0, 1])
|
|
408
|
+
expect(onCellValueChange).toHaveBeenCalledTimes(2)
|
|
409
|
+
expect(onCellValueChange).toHaveBeenCalledWith(
|
|
410
|
+
expect.objectContaining({ columnId: 'a', oldValue: 'a0', newValue: null }),
|
|
411
|
+
)
|
|
412
|
+
expect(onCellValueChange).toHaveBeenCalledWith(
|
|
413
|
+
expect.objectContaining({ columnId: 'b', oldValue: 'b0', newValue: 'a0' }),
|
|
414
|
+
)
|
|
415
|
+
})
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
// ---------------------------------------------------------------------------
|
|
419
|
+
// The grab strip. jsdom gives every element a zero rect, so the cell's
|
|
420
|
+
// geometry is stubbed - these assertions are about the 4px arithmetic, not
|
|
421
|
+
// about layout.
|
|
422
|
+
// ---------------------------------------------------------------------------
|
|
423
|
+
|
|
424
|
+
function cellEl(width = 100, height = 24, left = 0, top = 0) {
|
|
425
|
+
return {
|
|
426
|
+
getBoundingClientRect: () => ({ left, top, width, height, right: left + width, bottom: top + height }),
|
|
427
|
+
} as unknown as HTMLElement
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
describe('isOnMoveGrabStrip', () => {
|
|
431
|
+
it('hits the outer 4px of each edge the cell sits on', () => {
|
|
432
|
+
const ctx = makeCtx()
|
|
433
|
+
const cb = createClipboard(ctx)
|
|
434
|
+
select(ctx, 1, 1, 2, 2) // the cell at (1,1) is on the top and left edges
|
|
435
|
+
const el = cellEl()
|
|
436
|
+
expect(cb.isOnMoveGrabStrip(el, 1, 1, 50, 2)).toBe(true) // top strip
|
|
437
|
+
expect(cb.isOnMoveGrabStrip(el, 1, 1, 3, 12)).toBe(true) // left strip
|
|
438
|
+
expect(cb.isOnMoveGrabStrip(el, 2, 2, 50, 22)).toBe(true) // bottom strip
|
|
439
|
+
expect(cb.isOnMoveGrabStrip(el, 2, 2, 98, 12)).toBe(true) // right strip
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
it('misses the interior, so a plain click still starts a new selection', () => {
|
|
443
|
+
const ctx = makeCtx()
|
|
444
|
+
const cb = createClipboard(ctx)
|
|
445
|
+
select(ctx, 1, 1, 2, 2)
|
|
446
|
+
expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 12)).toBe(false)
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
it('misses the edge a cell is NOT on', () => {
|
|
450
|
+
const ctx = makeCtx()
|
|
451
|
+
const cb = createClipboard(ctx)
|
|
452
|
+
select(ctx, 1, 1, 2, 2)
|
|
453
|
+
// (1,1) is the top-LEFT corner: its bottom and right sides are interior.
|
|
454
|
+
expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 22)).toBe(false)
|
|
455
|
+
expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 98, 12)).toBe(false)
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
it('misses every cell outside the selection', () => {
|
|
459
|
+
const ctx = makeCtx()
|
|
460
|
+
const cb = createClipboard(ctx)
|
|
461
|
+
select(ctx, 1, 1, 2, 2)
|
|
462
|
+
expect(cb.isOnMoveGrabStrip(cellEl(), 0, 0, 1, 1)).toBe(false)
|
|
463
|
+
})
|
|
464
|
+
|
|
465
|
+
it('is off entirely when moveCells is disabled', () => {
|
|
466
|
+
const ctx = makeCtx({ moveCells: false })
|
|
467
|
+
const cb = createClipboard(ctx)
|
|
468
|
+
select(ctx, 1, 1, 2, 2)
|
|
469
|
+
expect(cb.isOnMoveGrabStrip(cellEl(), 1, 1, 50, 2)).toBe(false)
|
|
470
|
+
})
|
|
471
|
+
|
|
472
|
+
it('accounts for the cell offset, not just its size', () => {
|
|
473
|
+
const ctx = makeCtx()
|
|
474
|
+
const cb = createClipboard(ctx)
|
|
475
|
+
select(ctx, 1, 1, 1, 1)
|
|
476
|
+
const el = cellEl(100, 24, 300, 500)
|
|
477
|
+
expect(cb.isOnMoveGrabStrip(el, 1, 1, 350, 502)).toBe(true)
|
|
478
|
+
expect(cb.isOnMoveGrabStrip(el, 1, 1, 350, 512)).toBe(false)
|
|
479
|
+
})
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
describe('startMoveDrag', () => {
|
|
483
|
+
function pointerEvent(x: number, y: number, target: unknown, extra: Record<string, unknown> = {}) {
|
|
484
|
+
return {
|
|
485
|
+
button: 0,
|
|
486
|
+
clientX: x,
|
|
487
|
+
clientY: y,
|
|
488
|
+
target,
|
|
489
|
+
ctrlKey: false,
|
|
490
|
+
metaKey: false,
|
|
491
|
+
stopPropagation: vi.fn(),
|
|
492
|
+
preventDefault: vi.fn(),
|
|
493
|
+
...extra,
|
|
494
|
+
} as unknown as PointerEvent
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** A stub `td` that answers `closest()` with itself, as the real one would. */
|
|
498
|
+
function td(width = 100, height = 24) {
|
|
499
|
+
const el: any = cellEl(width, height)
|
|
500
|
+
el.closest = () => el
|
|
501
|
+
return el as HTMLElement
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
it('starts a drag on the border and reports that it claimed the event', () => {
|
|
505
|
+
const ctx = makeCtx()
|
|
506
|
+
const cb = createClipboard(ctx)
|
|
507
|
+
select(ctx, 1, 0, 2, 1)
|
|
508
|
+
const el = td()
|
|
509
|
+
expect(cb.startMoveDrag(pointerEvent(50, 2, el), 1, 0)).toBe(true)
|
|
510
|
+
expect(ctx.moveDrag).toMatchObject({
|
|
511
|
+
sourceMinRow: 1,
|
|
512
|
+
sourceMaxRow: 2,
|
|
513
|
+
sourceMinCol: 0,
|
|
514
|
+
sourceMaxCol: 1,
|
|
515
|
+
grabRow: 1,
|
|
516
|
+
grabCol: 0,
|
|
517
|
+
targetRow: 1,
|
|
518
|
+
targetCol: 0,
|
|
519
|
+
copy: false,
|
|
520
|
+
})
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
it('declines an interior pointerdown and leaves the selection path alone', () => {
|
|
524
|
+
const ctx = makeCtx()
|
|
525
|
+
const cb = createClipboard(ctx)
|
|
526
|
+
select(ctx, 1, 0, 2, 1)
|
|
527
|
+
const event = pointerEvent(50, 12, td())
|
|
528
|
+
expect(cb.startMoveDrag(event, 1, 0)).toBe(false)
|
|
529
|
+
expect(ctx.moveDrag).toBeNull()
|
|
530
|
+
expect(event.preventDefault).not.toHaveBeenCalled()
|
|
531
|
+
})
|
|
532
|
+
|
|
533
|
+
it('declines touch, which would otherwise block the page from scrolling', () => {
|
|
534
|
+
// This path calls preventDefault, so a finger landing within 4px of a
|
|
535
|
+
// selection border would start a move AND kill native scroll for that
|
|
536
|
+
// gesture. Drag-select already refuses touch for a milder version of the
|
|
537
|
+
// same problem (issue #23).
|
|
538
|
+
const ctx = makeCtx()
|
|
539
|
+
const cb = createClipboard(ctx)
|
|
540
|
+
select(ctx, 1, 0, 2, 1)
|
|
541
|
+
const event = pointerEvent(50, 2, td(), { pointerType: 'touch' })
|
|
542
|
+
expect(cb.startMoveDrag(event, 1, 0)).toBe(false)
|
|
543
|
+
expect(ctx.moveDrag).toBeNull()
|
|
544
|
+
expect(event.preventDefault).not.toHaveBeenCalled()
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
it('still accepts a pen, which has hover and does not scroll the page', () => {
|
|
548
|
+
const ctx = makeCtx()
|
|
549
|
+
const cb = createClipboard(ctx)
|
|
550
|
+
select(ctx, 1, 0, 2, 1)
|
|
551
|
+
expect(cb.startMoveDrag(pointerEvent(50, 2, td(), { pointerType: 'pen' }), 1, 0)).toBe(true)
|
|
552
|
+
})
|
|
553
|
+
|
|
554
|
+
it('declines anything but the primary button', () => {
|
|
555
|
+
const ctx = makeCtx()
|
|
556
|
+
const cb = createClipboard(ctx)
|
|
557
|
+
select(ctx, 1, 0, 2, 1)
|
|
558
|
+
expect(cb.startMoveDrag(pointerEvent(50, 2, td(), { button: 2 }), 1, 0)).toBe(false)
|
|
559
|
+
})
|
|
560
|
+
|
|
561
|
+
it('reads Ctrl (and Cmd) as copy', () => {
|
|
562
|
+
const ctx = makeCtx()
|
|
563
|
+
const cb = createClipboard(ctx)
|
|
564
|
+
select(ctx, 1, 0, 1, 0)
|
|
565
|
+
cb.startMoveDrag(pointerEvent(50, 2, td(), { ctrlKey: true }), 1, 0)
|
|
566
|
+
expect(ctx.moveDrag.copy).toBe(true)
|
|
567
|
+
ctx.moveDrag = null
|
|
568
|
+
cb.startMoveDrag(pointerEvent(50, 2, td(), { metaKey: true }), 1, 0)
|
|
569
|
+
expect(ctx.moveDrag.copy).toBe(true)
|
|
570
|
+
})
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
describe('onMovePointerMove', () => {
|
|
574
|
+
function moveEvent(extra: Record<string, unknown> = {}) {
|
|
575
|
+
return { clientX: 10, clientY: 10, ctrlKey: false, metaKey: false, ...extra } as PointerEvent
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** jsdom does not implement `document.elementFromPoint` at all - it is not
|
|
579
|
+
* a stub returning null, the property is absent - so `vi.spyOn` has
|
|
580
|
+
* nothing to wrap. Install it for the duration of one test. */
|
|
581
|
+
function stubElementFromPoint(result: Element | null) {
|
|
582
|
+
const calls = { count: 0 }
|
|
583
|
+
Object.defineProperty(document, 'elementFromPoint', {
|
|
584
|
+
configurable: true,
|
|
585
|
+
writable: true,
|
|
586
|
+
value: () => {
|
|
587
|
+
calls.count += 1
|
|
588
|
+
return result
|
|
589
|
+
},
|
|
590
|
+
})
|
|
591
|
+
return calls
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
afterEach(() => {
|
|
595
|
+
delete (document as unknown as Record<string, unknown>).elementFromPoint
|
|
596
|
+
})
|
|
597
|
+
|
|
598
|
+
function seed(ctx: any) {
|
|
599
|
+
ctx.moveDrag = {
|
|
600
|
+
sourceMinRow: 0,
|
|
601
|
+
sourceMaxRow: 0,
|
|
602
|
+
sourceMinCol: 0,
|
|
603
|
+
sourceMaxCol: 0,
|
|
604
|
+
grabRow: 0,
|
|
605
|
+
grabCol: 0,
|
|
606
|
+
targetRow: 0,
|
|
607
|
+
targetCol: 0,
|
|
608
|
+
copy: false,
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
it('follows the cell under the pointer', () => {
|
|
613
|
+
const ctx = makeCtx()
|
|
614
|
+
const cb = createClipboard(ctx)
|
|
615
|
+
seed(ctx)
|
|
616
|
+
const cell: any = { dataset: { svgridRow: '2', svgridCol: '1' } }
|
|
617
|
+
cell.closest = () => cell
|
|
618
|
+
stubElementFromPoint(cell)
|
|
619
|
+
cb.onMovePointerMove(moveEvent())
|
|
620
|
+
expect(ctx.moveDrag).toMatchObject({ targetRow: 2, targetCol: 1 })
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
it('picks up the copy modifier mid-drag, even between cells', () => {
|
|
624
|
+
const ctx = makeCtx()
|
|
625
|
+
const cb = createClipboard(ctx)
|
|
626
|
+
seed(ctx)
|
|
627
|
+
stubElementFromPoint(null)
|
|
628
|
+
cb.onMovePointerMove(moveEvent({ ctrlKey: true }))
|
|
629
|
+
expect(ctx.moveDrag.copy).toBe(true)
|
|
630
|
+
cb.onMovePointerMove(moveEvent({ ctrlKey: false }))
|
|
631
|
+
expect(ctx.moveDrag.copy).toBe(false)
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
it('takes the copy modifier from the release, not just the last move', () => {
|
|
635
|
+
// 'drag, then press Ctrl, then let go' is the natural way to do it, and
|
|
636
|
+
// pointermove has stopped firing by then.
|
|
637
|
+
const ctx = makeCtx()
|
|
638
|
+
const cb = createClipboard(ctx)
|
|
639
|
+
select(ctx, 0, 0, 0, 0)
|
|
640
|
+
ctx.moveDrag = {
|
|
641
|
+
sourceMinRow: 0, sourceMaxRow: 0, sourceMinCol: 0, sourceMaxCol: 0,
|
|
642
|
+
grabRow: 0, grabCol: 0, targetRow: 0, targetCol: 1, copy: false,
|
|
643
|
+
}
|
|
644
|
+
cb.onMovePointerUp({ ctrlKey: true, metaKey: false } as PointerEvent)
|
|
645
|
+
expect(grid(ctx)[0]).toEqual(['a0', 'a0', 'c0'])
|
|
646
|
+
})
|
|
647
|
+
|
|
648
|
+
it('does nothing at all when no move is in flight', () => {
|
|
649
|
+
const ctx = makeCtx()
|
|
650
|
+
const cb = createClipboard(ctx)
|
|
651
|
+
const calls = stubElementFromPoint(null)
|
|
652
|
+
cb.onMovePointerMove(moveEvent())
|
|
653
|
+
expect(calls.count).toBe(0)
|
|
654
|
+
})
|
|
655
|
+
})
|
|
656
|
+
|
|
657
|
+
// ---------------------------------------------------------------------------
|
|
658
|
+
// Edge auto-scroll. Shared by the fill drag, the range move and plain
|
|
659
|
+
// drag-select: without it a drag can only ever reach cells that were already
|
|
660
|
+
// on screen when it started.
|
|
661
|
+
// ---------------------------------------------------------------------------
|
|
662
|
+
|
|
663
|
+
describe('edge auto-scroll', () => {
|
|
664
|
+
const HOVERED = { row: 3, col: 2 }
|
|
665
|
+
|
|
666
|
+
beforeEach(() => {
|
|
667
|
+
const cell: any = { dataset: { svgridRow: String(HOVERED.row), svgridCol: String(HOVERED.col) } }
|
|
668
|
+
cell.closest = () => cell
|
|
669
|
+
Object.defineProperty(document, 'elementFromPoint', {
|
|
670
|
+
configurable: true,
|
|
671
|
+
writable: true,
|
|
672
|
+
value: () => cell,
|
|
673
|
+
})
|
|
674
|
+
})
|
|
675
|
+
afterEach(() => {
|
|
676
|
+
delete (document as unknown as Record<string, unknown>).elementFromPoint
|
|
677
|
+
})
|
|
678
|
+
|
|
679
|
+
/** A stand-in scroll container with a real rect and settable scroll offsets. */
|
|
680
|
+
function scroller(over: Record<string, unknown> = {}): any {
|
|
681
|
+
return {
|
|
682
|
+
scrollTop: 100,
|
|
683
|
+
scrollLeft: 100,
|
|
684
|
+
getBoundingClientRect: () => ({ left: 0, top: 0, right: 500, bottom: 300, width: 500, height: 300 }),
|
|
685
|
+
...over,
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/** Drive N animation frames synchronously. */
|
|
690
|
+
type Flush = (n?: number) => void
|
|
691
|
+
function withFrames<T>(run: (flush: Flush, queue: Array<FrameRequestCallback | null>) => T): T {
|
|
692
|
+
const queue: Array<FrameRequestCallback | null> = []
|
|
693
|
+
const raf = globalThis.requestAnimationFrame
|
|
694
|
+
const caf = globalThis.cancelAnimationFrame
|
|
695
|
+
globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => { queue.push(cb); return queue.length }
|
|
696
|
+
globalThis.cancelAnimationFrame = (id: number) => { queue[id - 1] = null }
|
|
697
|
+
const flush: Flush = (n = 1) => {
|
|
698
|
+
for (let i = 0; i < n; i += 1) {
|
|
699
|
+
const cb = queue.shift()
|
|
700
|
+
if (cb) cb(0)
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
try { return run(flush, queue) } finally {
|
|
704
|
+
globalThis.requestAnimationFrame = raf
|
|
705
|
+
globalThis.cancelAnimationFrame = caf
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function movingCtx(over: Record<string, unknown> = {}) {
|
|
710
|
+
const ctx = makeCtx()
|
|
711
|
+
ctx.scrollContainer = scroller(over)
|
|
712
|
+
ctx.isDraggingSelection = false
|
|
713
|
+
ctx.extendSelection = vi.fn()
|
|
714
|
+
ctx.moveDrag = {
|
|
715
|
+
sourceMinRow: 0, sourceMaxRow: 0, sourceMinCol: 0, sourceMaxCol: 0,
|
|
716
|
+
grabRow: 0, grabCol: 0, targetRow: 0, targetCol: 0, copy: false,
|
|
717
|
+
}
|
|
718
|
+
return ctx
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
it('scrolls down while the pointer sits near the bottom edge', () => {
|
|
722
|
+
const ctx = movingCtx()
|
|
723
|
+
const cb = createClipboard(ctx)
|
|
724
|
+
withFrames((flush) => {
|
|
725
|
+
// Container bottom is 300; 295 is well inside the 40px band.
|
|
726
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
727
|
+
flush()
|
|
728
|
+
expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(100)
|
|
729
|
+
})
|
|
730
|
+
})
|
|
731
|
+
|
|
732
|
+
it('scrolls up, left and right from the matching edges', () => {
|
|
733
|
+
const cases: Array<[number, number, 'scrollTop' | 'scrollLeft', number]> = [
|
|
734
|
+
[250, 5, 'scrollTop', -1],
|
|
735
|
+
[5, 150, 'scrollLeft', -1],
|
|
736
|
+
[495, 150, 'scrollLeft', 1],
|
|
737
|
+
]
|
|
738
|
+
for (const [x, y, axis, dir] of cases) {
|
|
739
|
+
const ctx = movingCtx()
|
|
740
|
+
const cb = createClipboard(ctx)
|
|
741
|
+
withFrames((flush) => {
|
|
742
|
+
cb.trackEdgeScroll({ clientX: x, clientY: y } as PointerEvent)
|
|
743
|
+
flush()
|
|
744
|
+
const moved = ctx.scrollContainer[axis] - 100
|
|
745
|
+
expect(Math.sign(moved), `${axis} at (${x},${y})`).toBe(dir)
|
|
746
|
+
})
|
|
747
|
+
}
|
|
748
|
+
})
|
|
749
|
+
|
|
750
|
+
it('does nothing while the pointer is in the middle', () => {
|
|
751
|
+
const ctx = movingCtx()
|
|
752
|
+
const cb = createClipboard(ctx)
|
|
753
|
+
withFrames((flush) => {
|
|
754
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 150 } as PointerEvent)
|
|
755
|
+
flush()
|
|
756
|
+
expect(ctx.scrollContainer.scrollTop).toBe(100)
|
|
757
|
+
expect(ctx.scrollContainer.scrollLeft).toBe(100)
|
|
758
|
+
})
|
|
759
|
+
})
|
|
760
|
+
|
|
761
|
+
it('ramps: deeper into the band scrolls faster', () => {
|
|
762
|
+
const at = (y: number) => {
|
|
763
|
+
const ctx = movingCtx()
|
|
764
|
+
const cb = createClipboard(ctx)
|
|
765
|
+
return withFrames((flush) => {
|
|
766
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: y } as PointerEvent)
|
|
767
|
+
flush()
|
|
768
|
+
return ctx.scrollContainer.scrollTop - 100
|
|
769
|
+
})
|
|
770
|
+
}
|
|
771
|
+
// 270 is 10px into the 40px band, 299 is at the very edge.
|
|
772
|
+
expect(at(299)).toBeGreaterThan(at(270))
|
|
773
|
+
})
|
|
774
|
+
|
|
775
|
+
it('keeps scrolling frame after frame with the pointer parked still', () => {
|
|
776
|
+
const ctx = movingCtx()
|
|
777
|
+
const cb = createClipboard(ctx)
|
|
778
|
+
withFrames((flush) => {
|
|
779
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
780
|
+
flush()
|
|
781
|
+
const after1 = ctx.scrollContainer.scrollTop
|
|
782
|
+
flush()
|
|
783
|
+
expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(after1)
|
|
784
|
+
})
|
|
785
|
+
})
|
|
786
|
+
|
|
787
|
+
it('stops as soon as the drag ends', () => {
|
|
788
|
+
const ctx = movingCtx()
|
|
789
|
+
const cb = createClipboard(ctx)
|
|
790
|
+
withFrames((flush) => {
|
|
791
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
792
|
+
flush()
|
|
793
|
+
const parked = ctx.scrollContainer.scrollTop
|
|
794
|
+
ctx.moveDrag = null
|
|
795
|
+
flush()
|
|
796
|
+
flush()
|
|
797
|
+
expect(ctx.scrollContainer.scrollTop).toBe(parked)
|
|
798
|
+
})
|
|
799
|
+
})
|
|
800
|
+
|
|
801
|
+
it('re-targets the drag at whatever cell the scroll brought under the pointer', () => {
|
|
802
|
+
const ctx = movingCtx()
|
|
803
|
+
const cb = createClipboard(ctx)
|
|
804
|
+
withFrames((flush) => {
|
|
805
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
806
|
+
flush()
|
|
807
|
+
})
|
|
808
|
+
expect(ctx.moveDrag).toMatchObject({ targetRow: HOVERED.row, targetCol: HOVERED.col })
|
|
809
|
+
})
|
|
810
|
+
|
|
811
|
+
it('does not re-target when the scroll is already clamped at the end', () => {
|
|
812
|
+
// scrollTop never moves, so the pointer is parked over one cell and
|
|
813
|
+
// re-targeting every frame would flicker the selection.
|
|
814
|
+
const stuck = scroller()
|
|
815
|
+
Object.defineProperty(stuck, 'scrollTop', { get: () => 0, set: () => {} })
|
|
816
|
+
const ctx = movingCtx()
|
|
817
|
+
ctx.scrollContainer = stuck
|
|
818
|
+
const cb = createClipboard(ctx)
|
|
819
|
+
const before = { ...ctx.moveDrag }
|
|
820
|
+
withFrames((flush) => {
|
|
821
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
822
|
+
flush()
|
|
823
|
+
flush()
|
|
824
|
+
})
|
|
825
|
+
expect(ctx.moveDrag).toEqual(before)
|
|
826
|
+
})
|
|
827
|
+
|
|
828
|
+
it('drives a plain drag-select too, not just fill and move', () => {
|
|
829
|
+
const ctx = movingCtx()
|
|
830
|
+
ctx.moveDrag = null
|
|
831
|
+
ctx.isDraggingSelection = true
|
|
832
|
+
const cb = createClipboard(ctx)
|
|
833
|
+
withFrames((flush) => {
|
|
834
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
835
|
+
flush()
|
|
836
|
+
expect(ctx.scrollContainer.scrollTop).toBeGreaterThan(100)
|
|
837
|
+
})
|
|
838
|
+
})
|
|
839
|
+
|
|
840
|
+
it('never starts when nothing is being dragged', () => {
|
|
841
|
+
const ctx = movingCtx()
|
|
842
|
+
ctx.moveDrag = null
|
|
843
|
+
const cb = createClipboard(ctx)
|
|
844
|
+
withFrames((flush) => {
|
|
845
|
+
cb.trackEdgeScroll({ clientX: 250, clientY: 295 } as PointerEvent)
|
|
846
|
+
flush()
|
|
847
|
+
expect(ctx.scrollContainer.scrollTop).toBe(100)
|
|
848
|
+
})
|
|
849
|
+
})
|
|
850
|
+
})
|