@alaarab/ogrid-angular 2.0.23 → 2.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 (33) hide show
  1. package/dist/esm/components/base-column-header-filter.component.js +26 -11
  2. package/dist/esm/components/base-datagrid-table.component.js +64 -46
  3. package/dist/esm/components/base-ogrid.component.js +36 -0
  4. package/dist/esm/components/base-popover-cell-editor.component.js +6 -7
  5. package/dist/esm/components/empty-state.component.js +2 -2
  6. package/dist/esm/components/grid-context-menu.component.js +17 -17
  7. package/dist/esm/components/ogrid-layout.component.js +2 -49
  8. package/dist/esm/components/sidebar.component.js +2 -6
  9. package/dist/esm/components/status-bar.component.js +6 -2
  10. package/dist/esm/index.js +5 -1
  11. package/dist/esm/services/datagrid-editing.service.js +52 -0
  12. package/dist/esm/services/datagrid-interaction.service.js +667 -0
  13. package/dist/esm/services/datagrid-layout.service.js +151 -0
  14. package/dist/esm/services/datagrid-state.service.js +130 -865
  15. package/dist/esm/services/ogrid.service.js +61 -26
  16. package/dist/esm/utils/index.js +1 -1
  17. package/dist/esm/utils/latestRef.js +0 -5
  18. package/dist/types/components/base-column-header-filter.component.d.ts +2 -0
  19. package/dist/types/components/base-datagrid-table.component.d.ts +21 -5
  20. package/dist/types/components/base-ogrid.component.d.ts +10 -0
  21. package/dist/types/components/grid-context-menu.component.d.ts +5 -5
  22. package/dist/types/components/sidebar.component.d.ts +0 -2
  23. package/dist/types/components/status-bar.component.d.ts +4 -1
  24. package/dist/types/index.d.ts +5 -1
  25. package/dist/types/services/datagrid-editing.service.d.ts +31 -0
  26. package/dist/types/services/datagrid-interaction.service.d.ts +86 -0
  27. package/dist/types/services/datagrid-layout.service.d.ts +36 -0
  28. package/dist/types/services/datagrid-state.service.d.ts +20 -39
  29. package/dist/types/services/ogrid.service.d.ts +8 -3
  30. package/dist/types/types/dataGridTypes.d.ts +1 -1
  31. package/dist/types/utils/index.d.ts +1 -1
  32. package/dist/types/utils/latestRef.d.ts +0 -5
  33. package/package.json +10 -3
@@ -0,0 +1,667 @@
1
+ import { signal, computed } from '@angular/core';
2
+ import { getCellValue, normalizeSelectionRange, parseValue, UndoRedoStack, findCtrlArrowTarget, computeTabNavigation, formatSelectionAsTsv, parseTsvClipboard, rangesEqual, } from '@alaarab/ogrid-core';
3
+ /**
4
+ * Manages cell selection, keyboard navigation, clipboard, fill handle, and undo/redo.
5
+ * Extracted from DataGridStateService for modularity.
6
+ *
7
+ * Not @Injectable — instantiated and owned by DataGridStateService.
8
+ */
9
+ export class DataGridInteractionHelper {
10
+ constructor() {
11
+ // --- Signals ---
12
+ this.activeCellSig = signal(null);
13
+ this.selectionRangeSig = signal(null);
14
+ this.isDraggingSig = signal(false);
15
+ this.contextMenuPositionSig = signal(null);
16
+ this.cutRangeSig = signal(null);
17
+ this.copyRangeSig = signal(null);
18
+ this.internalClipboard = null;
19
+ // Undo/redo
20
+ this.undoRedoStack = new UndoRedoStack(100);
21
+ this.undoLengthSig = signal(0);
22
+ this.redoLengthSig = signal(0);
23
+ this.canUndo = computed(() => this.undoLengthSig() > 0);
24
+ this.canRedo = computed(() => this.redoLengthSig() > 0);
25
+ this.hasCellSelection = computed(() => this.selectionRangeSig() != null || this.activeCellSig() != null);
26
+ // Fill handle state
27
+ this.fillDragStart = null;
28
+ this.fillRafId = 0;
29
+ this.fillMoveHandler = null;
30
+ this.fillUpHandler = null;
31
+ // Drag selection refs
32
+ this.dragStartPos = null;
33
+ this.dragMoved = false;
34
+ this.isDraggingRef = false;
35
+ this.liveDragRange = null;
36
+ this.rafId = 0;
37
+ this.lastMousePos = null;
38
+ this.autoScrollInterval = null;
39
+ }
40
+ setActiveCell(cell) {
41
+ const prev = this.activeCellSig();
42
+ if (prev === cell)
43
+ return;
44
+ if (prev && cell && prev.rowIndex === cell.rowIndex && prev.columnIndex === cell.columnIndex)
45
+ return;
46
+ this.activeCellSig.set(cell);
47
+ }
48
+ setSelectionRange(range) {
49
+ const prev = this.selectionRangeSig();
50
+ if (rangesEqual(prev, range))
51
+ return;
52
+ this.selectionRangeSig.set(range);
53
+ }
54
+ // --- Context menu ---
55
+ setContextMenuPosition(pos) {
56
+ this.contextMenuPositionSig.set(pos);
57
+ }
58
+ handleCellContextMenu(e) {
59
+ e.preventDefault?.();
60
+ this.contextMenuPositionSig.set({ x: e.clientX, y: e.clientY });
61
+ }
62
+ closeContextMenu() {
63
+ this.contextMenuPositionSig.set(null);
64
+ }
65
+ // --- Clipboard ---
66
+ handleCopy(items, visibleCols, colOffset) {
67
+ const range = this.getEffectiveRange(colOffset);
68
+ if (range == null)
69
+ return;
70
+ const norm = normalizeSelectionRange(range);
71
+ const tsv = formatSelectionAsTsv(items, visibleCols, norm);
72
+ this.internalClipboard = tsv;
73
+ this.copyRangeSig.set(norm);
74
+ void navigator.clipboard.writeText(tsv).catch(() => { });
75
+ }
76
+ handleCut(items, visibleCols, colOffset, editable, wrappedOnCellValueChanged) {
77
+ if (editable === false)
78
+ return;
79
+ const range = this.getEffectiveRange(colOffset);
80
+ if (range == null || !wrappedOnCellValueChanged)
81
+ return;
82
+ const norm = normalizeSelectionRange(range);
83
+ this.cutRangeSig.set(norm);
84
+ this.copyRangeSig.set(null);
85
+ this.handleCopy(items, visibleCols, colOffset);
86
+ this.copyRangeSig.set(null);
87
+ }
88
+ async handlePaste(items, visibleCols, colOffset, editable, wrappedOnCellValueChanged) {
89
+ if (editable === false)
90
+ return;
91
+ if (!wrappedOnCellValueChanged)
92
+ return;
93
+ let text;
94
+ try {
95
+ text = await navigator.clipboard.readText();
96
+ }
97
+ catch {
98
+ text = '';
99
+ }
100
+ if (!text.trim() && this.internalClipboard != null) {
101
+ text = this.internalClipboard;
102
+ }
103
+ if (!text.trim())
104
+ return;
105
+ const norm = this.getEffectiveRange(colOffset);
106
+ const anchorRow = norm ? norm.startRow : 0;
107
+ const anchorCol = norm ? norm.startCol : 0;
108
+ const parsedRows = parseTsvClipboard(text);
109
+ this.beginBatch();
110
+ for (let r = 0; r < parsedRows.length; r++) {
111
+ const cells = parsedRows[r];
112
+ for (let c = 0; c < cells.length; c++) {
113
+ const targetRow = anchorRow + r;
114
+ const targetCol = anchorCol + c;
115
+ if (targetRow >= items.length || targetCol >= visibleCols.length)
116
+ continue;
117
+ const item = items[targetRow];
118
+ const col = visibleCols[targetCol];
119
+ const colEditable = col.editable === true || (typeof col.editable === 'function' && col.editable(item));
120
+ if (!colEditable)
121
+ continue;
122
+ const rawValue = cells[c] ?? '';
123
+ const oldValue = getCellValue(item, col);
124
+ const result = parseValue(rawValue, oldValue, item, col);
125
+ if (!result.valid)
126
+ continue;
127
+ wrappedOnCellValueChanged({ item, columnId: col.columnId, oldValue, newValue: result.value, rowIndex: targetRow });
128
+ }
129
+ }
130
+ const cutRange = this.cutRangeSig();
131
+ if (cutRange) {
132
+ for (let r = cutRange.startRow; r <= cutRange.endRow; r++) {
133
+ for (let c = cutRange.startCol; c <= cutRange.endCol; c++) {
134
+ if (r >= items.length || c >= visibleCols.length)
135
+ continue;
136
+ const item = items[r];
137
+ const col = visibleCols[c];
138
+ const colEditable = col.editable === true || (typeof col.editable === 'function' && col.editable(item));
139
+ if (!colEditable)
140
+ continue;
141
+ const oldValue = getCellValue(item, col);
142
+ const result = parseValue('', oldValue, item, col);
143
+ if (!result.valid)
144
+ continue;
145
+ wrappedOnCellValueChanged({ item, columnId: col.columnId, oldValue, newValue: result.value, rowIndex: r });
146
+ }
147
+ }
148
+ this.cutRangeSig.set(null);
149
+ }
150
+ this.endBatch();
151
+ this.copyRangeSig.set(null);
152
+ }
153
+ clearClipboardRanges() {
154
+ this.copyRangeSig.set(null);
155
+ this.cutRangeSig.set(null);
156
+ }
157
+ // --- Undo/Redo ---
158
+ beginBatch() {
159
+ this.undoRedoStack.beginBatch();
160
+ }
161
+ endBatch() {
162
+ this.undoRedoStack.endBatch();
163
+ this.undoLengthSig.set(this.undoRedoStack.historyLength);
164
+ this.redoLengthSig.set(this.undoRedoStack.redoLength);
165
+ }
166
+ undo(originalOnCellValueChanged) {
167
+ if (!originalOnCellValueChanged)
168
+ return;
169
+ const lastBatch = this.undoRedoStack.undo();
170
+ if (!lastBatch)
171
+ return;
172
+ this.undoLengthSig.set(this.undoRedoStack.historyLength);
173
+ this.redoLengthSig.set(this.undoRedoStack.redoLength);
174
+ for (let i = lastBatch.length - 1; i >= 0; i--) {
175
+ const ev = lastBatch[i];
176
+ originalOnCellValueChanged({ ...ev, oldValue: ev.newValue, newValue: ev.oldValue });
177
+ }
178
+ }
179
+ redo(originalOnCellValueChanged) {
180
+ if (!originalOnCellValueChanged)
181
+ return;
182
+ const nextBatch = this.undoRedoStack.redo();
183
+ if (!nextBatch)
184
+ return;
185
+ this.undoLengthSig.set(this.undoRedoStack.historyLength);
186
+ this.redoLengthSig.set(this.undoRedoStack.redoLength);
187
+ for (const ev of nextBatch) {
188
+ originalOnCellValueChanged(ev);
189
+ }
190
+ }
191
+ // --- Cell selection / mouse handling ---
192
+ handleCellMouseDown(e, rowIndex, globalColIndex, colOffset, wrapperEl) {
193
+ if (e.button !== 0)
194
+ return;
195
+ wrapperEl?.focus({ preventScroll: true });
196
+ this.clearClipboardRanges();
197
+ if (globalColIndex < colOffset)
198
+ return;
199
+ e.preventDefault();
200
+ const dataColIndex = globalColIndex - colOffset;
201
+ const currentRange = this.selectionRangeSig();
202
+ if (e.shiftKey && currentRange != null) {
203
+ this.setSelectionRange(normalizeSelectionRange({
204
+ startRow: currentRange.startRow,
205
+ startCol: currentRange.startCol,
206
+ endRow: rowIndex,
207
+ endCol: dataColIndex,
208
+ }));
209
+ this.setActiveCell({ rowIndex, columnIndex: globalColIndex });
210
+ }
211
+ else {
212
+ this.dragStartPos = { row: rowIndex, col: dataColIndex };
213
+ this.dragMoved = false;
214
+ const initial = {
215
+ startRow: rowIndex, startCol: dataColIndex,
216
+ endRow: rowIndex, endCol: dataColIndex,
217
+ };
218
+ this.setSelectionRange(initial);
219
+ this.liveDragRange = initial;
220
+ this.setActiveCell({ rowIndex, columnIndex: globalColIndex });
221
+ this.isDraggingRef = true;
222
+ }
223
+ }
224
+ handleSelectAllCells(rowCount, visibleColCount, colOffset) {
225
+ if (rowCount === 0 || visibleColCount === 0)
226
+ return;
227
+ this.setSelectionRange({
228
+ startRow: 0, startCol: 0,
229
+ endRow: rowCount - 1, endCol: visibleColCount - 1,
230
+ });
231
+ this.setActiveCell({ rowIndex: 0, columnIndex: colOffset });
232
+ }
233
+ // --- Fill handle ---
234
+ handleFillHandleMouseDown(e) {
235
+ e.preventDefault();
236
+ e.stopPropagation();
237
+ const range = this.selectionRangeSig();
238
+ if (!range)
239
+ return;
240
+ this.fillDragStart = { startRow: range.startRow, startCol: range.startCol };
241
+ }
242
+ // --- Keyboard navigation ---
243
+ handleGridKeyDown(e, items, getRowId, visibleCols, colOffset, hasCheckboxCol, visibleColumnCount, editable, wrappedOnCellValueChanged, originalOnCellValueChanged, rowSelection, selectedRowIds, wrapperEl, handleRowCheckboxChange, editingCell, setEditingCell) {
244
+ const activeCell = this.activeCellSig();
245
+ const selectionRange = this.selectionRangeSig();
246
+ const maxRowIndex = items.length - 1;
247
+ const maxColIndex = visibleColumnCount - 1 + colOffset;
248
+ if (items.length === 0)
249
+ return;
250
+ if (activeCell === null) {
251
+ if (['ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight', 'Tab', 'Enter', 'Home', 'End'].includes(e.key)) {
252
+ this.setActiveCell({ rowIndex: 0, columnIndex: colOffset });
253
+ e.preventDefault();
254
+ }
255
+ return;
256
+ }
257
+ const { rowIndex, columnIndex } = activeCell;
258
+ const dataColIndex = columnIndex - colOffset;
259
+ const shift = e.shiftKey;
260
+ const ctrl = e.ctrlKey || e.metaKey;
261
+ const isEmptyAt = (r, c) => {
262
+ if (r < 0 || r >= items.length || c < 0 || c >= visibleCols.length)
263
+ return true;
264
+ const v = getCellValue(items[r], visibleCols[c]);
265
+ return v == null || v === '';
266
+ };
267
+ const findCtrlTarget = findCtrlArrowTarget;
268
+ switch (e.key) {
269
+ case 'c':
270
+ if (ctrl) {
271
+ if (editingCell != null)
272
+ break;
273
+ e.preventDefault();
274
+ this.handleCopy(items, visibleCols, colOffset);
275
+ }
276
+ break;
277
+ case 'x':
278
+ if (ctrl) {
279
+ if (editingCell != null)
280
+ break;
281
+ e.preventDefault();
282
+ this.handleCut(items, visibleCols, colOffset, editable, wrappedOnCellValueChanged);
283
+ }
284
+ break;
285
+ case 'v':
286
+ if (ctrl) {
287
+ if (editingCell != null)
288
+ break;
289
+ e.preventDefault();
290
+ void this.handlePaste(items, visibleCols, colOffset, editable, wrappedOnCellValueChanged);
291
+ }
292
+ break;
293
+ case 'ArrowDown': {
294
+ e.preventDefault();
295
+ const newRow = ctrl
296
+ ? findCtrlTarget(rowIndex, maxRowIndex, 1, (r) => isEmptyAt(r, Math.max(0, dataColIndex)))
297
+ : Math.min(rowIndex + 1, maxRowIndex);
298
+ if (shift) {
299
+ this.setSelectionRange(normalizeSelectionRange({
300
+ startRow: selectionRange?.startRow ?? rowIndex,
301
+ startCol: selectionRange?.startCol ?? dataColIndex,
302
+ endRow: newRow,
303
+ endCol: selectionRange?.endCol ?? dataColIndex,
304
+ }));
305
+ }
306
+ else {
307
+ this.setSelectionRange({ startRow: newRow, startCol: dataColIndex, endRow: newRow, endCol: dataColIndex });
308
+ }
309
+ this.setActiveCell({ rowIndex: newRow, columnIndex });
310
+ break;
311
+ }
312
+ case 'ArrowUp': {
313
+ e.preventDefault();
314
+ const newRowUp = ctrl
315
+ ? findCtrlTarget(rowIndex, 0, -1, (r) => isEmptyAt(r, Math.max(0, dataColIndex)))
316
+ : Math.max(rowIndex - 1, 0);
317
+ if (shift) {
318
+ this.setSelectionRange(normalizeSelectionRange({
319
+ startRow: selectionRange?.startRow ?? rowIndex,
320
+ startCol: selectionRange?.startCol ?? dataColIndex,
321
+ endRow: newRowUp,
322
+ endCol: selectionRange?.endCol ?? dataColIndex,
323
+ }));
324
+ }
325
+ else {
326
+ this.setSelectionRange({ startRow: newRowUp, startCol: dataColIndex, endRow: newRowUp, endCol: dataColIndex });
327
+ }
328
+ this.setActiveCell({ rowIndex: newRowUp, columnIndex });
329
+ break;
330
+ }
331
+ case 'ArrowRight': {
332
+ e.preventDefault();
333
+ let newCol;
334
+ if (ctrl && dataColIndex >= 0) {
335
+ newCol = findCtrlTarget(dataColIndex, visibleCols.length - 1, 1, (c) => isEmptyAt(rowIndex, c)) + colOffset;
336
+ }
337
+ else {
338
+ newCol = Math.min(columnIndex + 1, maxColIndex);
339
+ }
340
+ const newDataCol = newCol - colOffset;
341
+ if (shift) {
342
+ this.setSelectionRange(normalizeSelectionRange({
343
+ startRow: selectionRange?.startRow ?? rowIndex,
344
+ startCol: selectionRange?.startCol ?? dataColIndex,
345
+ endRow: selectionRange?.endRow ?? rowIndex,
346
+ endCol: newDataCol,
347
+ }));
348
+ }
349
+ else {
350
+ this.setSelectionRange({ startRow: rowIndex, startCol: newDataCol, endRow: rowIndex, endCol: newDataCol });
351
+ }
352
+ this.setActiveCell({ rowIndex, columnIndex: newCol });
353
+ break;
354
+ }
355
+ case 'ArrowLeft': {
356
+ e.preventDefault();
357
+ let newColLeft;
358
+ if (ctrl && dataColIndex >= 0) {
359
+ newColLeft = findCtrlTarget(dataColIndex, 0, -1, (c) => isEmptyAt(rowIndex, c)) + colOffset;
360
+ }
361
+ else {
362
+ newColLeft = Math.max(columnIndex - 1, colOffset);
363
+ }
364
+ const newDataColLeft = newColLeft - colOffset;
365
+ if (shift) {
366
+ this.setSelectionRange(normalizeSelectionRange({
367
+ startRow: selectionRange?.startRow ?? rowIndex,
368
+ startCol: selectionRange?.startCol ?? dataColIndex,
369
+ endRow: selectionRange?.endRow ?? rowIndex,
370
+ endCol: newDataColLeft,
371
+ }));
372
+ }
373
+ else {
374
+ this.setSelectionRange({ startRow: rowIndex, startCol: newDataColLeft, endRow: rowIndex, endCol: newDataColLeft });
375
+ }
376
+ this.setActiveCell({ rowIndex, columnIndex: newColLeft });
377
+ break;
378
+ }
379
+ case 'Tab': {
380
+ e.preventDefault();
381
+ const tabResult = computeTabNavigation(rowIndex, columnIndex, maxRowIndex, maxColIndex, colOffset, e.shiftKey);
382
+ const newDataColTab = tabResult.columnIndex - colOffset;
383
+ this.setSelectionRange({ startRow: tabResult.rowIndex, startCol: newDataColTab, endRow: tabResult.rowIndex, endCol: newDataColTab });
384
+ this.setActiveCell({ rowIndex: tabResult.rowIndex, columnIndex: tabResult.columnIndex });
385
+ break;
386
+ }
387
+ case 'Home': {
388
+ e.preventDefault();
389
+ const newRowHome = ctrl ? 0 : rowIndex;
390
+ this.setSelectionRange({ startRow: newRowHome, startCol: 0, endRow: newRowHome, endCol: 0 });
391
+ this.setActiveCell({ rowIndex: newRowHome, columnIndex: colOffset });
392
+ break;
393
+ }
394
+ case 'End': {
395
+ e.preventDefault();
396
+ const newRowEnd = ctrl ? maxRowIndex : rowIndex;
397
+ this.setSelectionRange({ startRow: newRowEnd, startCol: visibleColumnCount - 1, endRow: newRowEnd, endCol: visibleColumnCount - 1 });
398
+ this.setActiveCell({ rowIndex: newRowEnd, columnIndex: maxColIndex });
399
+ break;
400
+ }
401
+ case 'Enter':
402
+ case 'F2': {
403
+ e.preventDefault();
404
+ if (dataColIndex >= 0 && dataColIndex < visibleCols.length) {
405
+ const col = visibleCols[dataColIndex];
406
+ const item = items[rowIndex];
407
+ if (item && col) {
408
+ const colEditable = col.editable === true || (typeof col.editable === 'function' && col.editable(item));
409
+ if (editable !== false && colEditable && wrappedOnCellValueChanged != null) {
410
+ setEditingCell({ rowId: getRowId(item), columnId: col.columnId });
411
+ }
412
+ }
413
+ }
414
+ break;
415
+ }
416
+ case 'Escape':
417
+ e.preventDefault();
418
+ if (editingCell != null) {
419
+ setEditingCell(null);
420
+ }
421
+ else {
422
+ this.clearClipboardRanges();
423
+ this.setActiveCell(null);
424
+ this.setSelectionRange(null);
425
+ }
426
+ break;
427
+ case ' ':
428
+ if (rowSelection !== 'none' && columnIndex === 0 && hasCheckboxCol) {
429
+ e.preventDefault();
430
+ const item = items[rowIndex];
431
+ if (item) {
432
+ const id = getRowId(item);
433
+ const isSelected = selectedRowIds.has(id);
434
+ handleRowCheckboxChange(id, !isSelected, rowIndex, e.shiftKey);
435
+ }
436
+ }
437
+ break;
438
+ case 'z':
439
+ if (ctrl) {
440
+ if (editingCell == null) {
441
+ if (e.shiftKey) {
442
+ e.preventDefault();
443
+ this.redo(originalOnCellValueChanged);
444
+ }
445
+ else {
446
+ e.preventDefault();
447
+ this.undo(originalOnCellValueChanged);
448
+ }
449
+ }
450
+ }
451
+ break;
452
+ case 'y':
453
+ if (ctrl && editingCell == null) {
454
+ e.preventDefault();
455
+ this.redo(originalOnCellValueChanged);
456
+ }
457
+ break;
458
+ case 'a':
459
+ if (ctrl) {
460
+ if (editingCell != null)
461
+ break;
462
+ e.preventDefault();
463
+ if (items.length > 0 && visibleColumnCount > 0) {
464
+ this.setSelectionRange({ startRow: 0, startCol: 0, endRow: items.length - 1, endCol: visibleColumnCount - 1 });
465
+ this.setActiveCell({ rowIndex: 0, columnIndex: colOffset });
466
+ }
467
+ }
468
+ break;
469
+ case 'Delete':
470
+ case 'Backspace': {
471
+ if (editingCell != null)
472
+ break;
473
+ if (editable === false)
474
+ break;
475
+ if (wrappedOnCellValueChanged == null)
476
+ break;
477
+ const range = selectionRange ?? (activeCell != null
478
+ ? { startRow: activeCell.rowIndex, startCol: activeCell.columnIndex - colOffset, endRow: activeCell.rowIndex, endCol: activeCell.columnIndex - colOffset }
479
+ : null);
480
+ if (range == null)
481
+ break;
482
+ e.preventDefault();
483
+ const norm = normalizeSelectionRange(range);
484
+ for (let r = norm.startRow; r <= norm.endRow; r++) {
485
+ for (let c = norm.startCol; c <= norm.endCol; c++) {
486
+ if (r >= items.length || c >= visibleCols.length)
487
+ continue;
488
+ const item = items[r];
489
+ const col = visibleCols[c];
490
+ const colEditable = col.editable === true || (typeof col.editable === 'function' && col.editable(item));
491
+ if (!colEditable)
492
+ continue;
493
+ const oldValue = getCellValue(item, col);
494
+ const result = parseValue('', oldValue, item, col);
495
+ if (!result.valid)
496
+ continue;
497
+ wrappedOnCellValueChanged({ item, columnId: col.columnId, oldValue, newValue: result.value, rowIndex: r });
498
+ }
499
+ }
500
+ break;
501
+ }
502
+ case 'F10':
503
+ if (e.shiftKey) {
504
+ e.preventDefault();
505
+ if (activeCell != null && wrapperEl) {
506
+ const sel = `[data-row-index="${activeCell.rowIndex}"][data-col-index="${activeCell.columnIndex}"]`;
507
+ const cell = wrapperEl.querySelector(sel);
508
+ if (cell) {
509
+ const rect = cell.getBoundingClientRect();
510
+ this.setContextMenuPosition({ x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 });
511
+ }
512
+ else {
513
+ this.setContextMenuPosition({ x: 100, y: 100 });
514
+ }
515
+ }
516
+ else {
517
+ this.setContextMenuPosition({ x: 100, y: 100 });
518
+ }
519
+ }
520
+ break;
521
+ default:
522
+ break;
523
+ }
524
+ }
525
+ // --- Drag helpers ---
526
+ onWindowMouseMove(e, colOffset, wrapperEl) {
527
+ if (!this.isDraggingRef || !this.dragStartPos)
528
+ return;
529
+ if (!this.dragMoved) {
530
+ this.dragMoved = true;
531
+ this.isDraggingSig.set(true);
532
+ }
533
+ this.lastMousePos = { cx: e.clientX, cy: e.clientY };
534
+ if (this.rafId)
535
+ cancelAnimationFrame(this.rafId);
536
+ this.rafId = requestAnimationFrame(() => {
537
+ this.rafId = 0;
538
+ const pos = this.lastMousePos;
539
+ if (!pos)
540
+ return;
541
+ const newRange = this.resolveRangeFromMouse(pos.cx, pos.cy, colOffset);
542
+ if (!newRange)
543
+ return;
544
+ const prev = this.liveDragRange;
545
+ if (prev && prev.startRow === newRange.startRow && prev.startCol === newRange.startCol &&
546
+ prev.endRow === newRange.endRow && prev.endCol === newRange.endCol)
547
+ return;
548
+ this.liveDragRange = newRange;
549
+ this.applyDragAttrs(newRange, colOffset, wrapperEl);
550
+ });
551
+ }
552
+ onWindowMouseUp(colOffset, wrapperEl) {
553
+ if (!this.isDraggingRef)
554
+ return;
555
+ if (this.autoScrollInterval) {
556
+ clearInterval(this.autoScrollInterval);
557
+ this.autoScrollInterval = null;
558
+ }
559
+ if (this.rafId) {
560
+ cancelAnimationFrame(this.rafId);
561
+ this.rafId = 0;
562
+ }
563
+ this.isDraggingRef = false;
564
+ const wasDrag = this.dragMoved;
565
+ if (wasDrag) {
566
+ const pos = this.lastMousePos;
567
+ if (pos) {
568
+ const flushed = this.resolveRangeFromMouse(pos.cx, pos.cy, colOffset);
569
+ if (flushed)
570
+ this.liveDragRange = flushed;
571
+ }
572
+ const finalRange = this.liveDragRange;
573
+ if (finalRange) {
574
+ this.setSelectionRange(finalRange);
575
+ this.setActiveCell({
576
+ rowIndex: finalRange.endRow,
577
+ columnIndex: finalRange.endCol + colOffset,
578
+ });
579
+ }
580
+ }
581
+ this.clearDragAttrs(wrapperEl);
582
+ this.liveDragRange = null;
583
+ this.lastMousePos = null;
584
+ this.dragStartPos = null;
585
+ if (wasDrag)
586
+ this.isDraggingSig.set(false);
587
+ }
588
+ resolveRangeFromMouse(cx, cy, colOffset) {
589
+ if (!this.dragStartPos)
590
+ return null;
591
+ const target = document.elementFromPoint(cx, cy);
592
+ const cell = target?.closest?.('[data-row-index][data-col-index]');
593
+ if (!cell)
594
+ return null;
595
+ const r = parseInt(cell.getAttribute('data-row-index') ?? '', 10);
596
+ const c = parseInt(cell.getAttribute('data-col-index') ?? '', 10);
597
+ if (Number.isNaN(r) || Number.isNaN(c) || c < colOffset)
598
+ return null;
599
+ const dataCol = c - colOffset;
600
+ const start = this.dragStartPos;
601
+ return normalizeSelectionRange({
602
+ startRow: start.row, startCol: start.col,
603
+ endRow: r, endCol: dataCol,
604
+ });
605
+ }
606
+ applyDragAttrs(range, colOff, wrapper) {
607
+ if (!wrapper)
608
+ return;
609
+ const minR = Math.min(range.startRow, range.endRow);
610
+ const maxR = Math.max(range.startRow, range.endRow);
611
+ const minC = Math.min(range.startCol, range.endCol);
612
+ const maxC = Math.max(range.startCol, range.endCol);
613
+ const cells = wrapper.querySelectorAll('[data-row-index][data-col-index]');
614
+ for (let i = 0; i < cells.length; i++) {
615
+ const el = cells[i];
616
+ const r = parseInt(el.getAttribute('data-row-index') ?? '', 10);
617
+ const c = parseInt(el.getAttribute('data-col-index') ?? '', 10) - colOff;
618
+ const inRange = r >= minR && r <= maxR && c >= minC && c <= maxC;
619
+ if (inRange) {
620
+ if (!el.hasAttribute('data-drag-range'))
621
+ el.setAttribute('data-drag-range', '');
622
+ }
623
+ else {
624
+ if (el.hasAttribute('data-drag-range'))
625
+ el.removeAttribute('data-drag-range');
626
+ }
627
+ }
628
+ }
629
+ clearDragAttrs(wrapper) {
630
+ if (!wrapper)
631
+ return;
632
+ const marked = wrapper.querySelectorAll('[data-drag-range]');
633
+ for (let i = 0; i < marked.length; i++)
634
+ marked[i].removeAttribute('data-drag-range');
635
+ }
636
+ // --- Private helpers ---
637
+ getEffectiveRange(colOffset) {
638
+ const sel = this.selectionRangeSig();
639
+ const ac = this.activeCellSig();
640
+ return sel ?? (ac != null
641
+ ? { startRow: ac.rowIndex, startCol: ac.columnIndex - colOffset, endRow: ac.rowIndex, endCol: ac.columnIndex - colOffset }
642
+ : null);
643
+ }
644
+ destroy() {
645
+ if (this.rafId) {
646
+ cancelAnimationFrame(this.rafId);
647
+ this.rafId = 0;
648
+ }
649
+ if (this.fillRafId) {
650
+ cancelAnimationFrame(this.fillRafId);
651
+ this.fillRafId = 0;
652
+ }
653
+ if (this.autoScrollInterval) {
654
+ clearInterval(this.autoScrollInterval);
655
+ this.autoScrollInterval = null;
656
+ }
657
+ if (this.fillMoveHandler) {
658
+ window.removeEventListener('mousemove', this.fillMoveHandler, true);
659
+ this.fillMoveHandler = null;
660
+ }
661
+ if (this.fillUpHandler) {
662
+ window.removeEventListener('mouseup', this.fillUpHandler, true);
663
+ this.fillUpHandler = null;
664
+ }
665
+ this.undoRedoStack.clear();
666
+ }
667
+ }