sheet-happens 0.0.23 → 0.0.25

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.
@@ -1,4 +1,4 @@
1
- import React, { useRef, useState, useMemo, useEffect } from 'react';
1
+ import React, { useState, useMemo, useRef, useCallback, useLayoutEffect, useEffect, forwardRef, useImperativeHandle } from 'react';
2
2
  import useResizeObserver from 'use-resize-observer';
3
3
 
4
4
  function _extends() {
@@ -63,29 +63,43 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
63
63
 
64
64
  var styles = {"sheetscroll":"_PxIi8"};
65
65
 
66
- var selBorderColor = '#1b73e7';
67
- var selBackColor = '#e9f0fd';
68
- var knobSize = 6;
69
- var gridColor = '#e2e3e3';
70
- var knobAreaBorderColor = '#707070';
71
- var kRowHeaderWidth = 50;
72
- var rowHeaderBackgroundColor = '#f8f9fa';
73
- var rowHeaderTextColor = '#666666';
74
- var rowHeaderSelectedBackgroundColor = '#e8eaed';
75
- var kColumnHeaderHeight = 22;
76
- var columnHeaderBackgroundColor = rowHeaderBackgroundColor;
77
- var columnHeaderSelectedBackgroundColor = rowHeaderSelectedBackgroundColor;
78
- var xBinSize = 10;
79
- var yBinSize = 10;
80
- var scrollSpeed = 30;
81
- var resizeColumnRowMouseThreshold = 4;
82
- var minimumColumnWidth = 50;
83
- var minimumRowHeight = 22;
84
- var rowColHeaderSelectionColor = '#cccccc';
85
- var maxSearchableRowOrCol = 65536;
86
- var defaultCellStyle = {
66
+ var INITIAL_MAX_SCROLL = [2000, 1000];
67
+ var ORIGIN = [0, 0];
68
+ var ONE_ONE = [1, 1];
69
+ var NO_CELL = [-1, -1];
70
+ var NO_SELECTION = [NO_CELL, NO_CELL];
71
+ var NO_SELECTIONS = [];
72
+ var NO_CLICKABLES = [];
73
+ var NO_STYLE = {};
74
+ var MAX_SEARCHABLE_INDEX = 65536;
75
+ var MAX_XY = [MAX_SEARCHABLE_INDEX, MAX_SEARCHABLE_INDEX];
76
+ var COLORS = {
77
+ selectionBorder: '#1a66ff',
78
+ selectionBackground: '#e8f0ff',
79
+ gridLine: '#0000001f',
80
+ dragGhost: '#1a66ff30',
81
+ dropTarget: '#1a66ff',
82
+ knobAreaBorder: '#707070',
83
+ headerBackground: '#f6f9fc',
84
+ headerText: '#666666',
85
+ headerActive: '#e8f0ff',
86
+ headerActiveText: '#1a66ff',
87
+ headerSelected: '#1a66ff',
88
+ headerSelectedText: '#ffffff'
89
+ };
90
+ var SIZES = {
91
+ knobArea: 6,
92
+ headerWidth: 50,
93
+ headerHeight: 22,
94
+ minimumWidth: 50,
95
+ minimumHeight: 22,
96
+ resizeZone: 4,
97
+ scrollZone: 50,
98
+ scrollSpeed: 30
99
+ };
100
+ var DEFAULT_CELL_STYLE = {
87
101
  textAlign: 'left',
88
- fontSize: 13,
102
+ fontSize: 12,
89
103
  marginRight: 5,
90
104
  marginLeft: 5,
91
105
  color: '#000',
@@ -94,9 +108,9 @@ var defaultCellStyle = {
94
108
  fillColor: '',
95
109
  backgroundColor: ''
96
110
  };
97
- var defaultColumnHeaderStyle = {
111
+ var DEFAULT_COLUMN_HEADER_STYLE = {
98
112
  textAlign: 'center',
99
- fontSize: 13,
113
+ fontSize: 12,
100
114
  marginRight: 5,
101
115
  marginLeft: 5,
102
116
  color: '#000',
@@ -105,39 +119,174 @@ var defaultColumnHeaderStyle = {
105
119
  fillColor: '',
106
120
  backgroundColor: ''
107
121
  };
122
+ var HEADER_ACTIVE_STYLE = {
123
+ color: COLORS.headerActiveText
124
+ };
125
+ var HEADER_SELECTED_STYLE = {
126
+ backgroundColor: COLORS.headerSelected,
127
+ color: COLORS.headerSelectedText
128
+ };
129
+ var ARROW_KEYS = {
130
+ 'ArrowRight': 'right',
131
+ 'ArrowLeft': 'left',
132
+ 'ArrowUp': 'up',
133
+ 'ArrowDown': 'down'
134
+ };
108
135
 
109
- function resizeCanvas(canvas) {
110
- var _canvas$getBoundingCl = canvas.getBoundingClientRect(),
111
- width = _canvas$getBoundingCl.width,
112
- height = _canvas$getBoundingCl.height;
113
-
114
- var _window = window,
115
- _window$devicePixelRa = _window.devicePixelRatio,
116
- ratio = _window$devicePixelRa === void 0 ? 1 : _window$devicePixelRa;
136
+ var clamp = function clamp(x, min, max) {
137
+ return Math.max(Math.min(max, x), min);
138
+ };
139
+ var seq = function seq(n, s, d) {
140
+ if (s === void 0) {
141
+ s = 0;
142
+ }
117
143
 
118
- if (ratio < 1) {
119
- ratio = 1;
144
+ if (d === void 0) {
145
+ d = 1;
120
146
  }
121
147
 
122
- var newCanvasWidth = Math.round(width * ratio);
123
- var newCanvasHeight = Math.round(height * ratio);
148
+ return Array.from({
149
+ length: n
150
+ }).map(function (_, i) {
151
+ return s + d * i;
152
+ });
153
+ };
154
+ var isInRange = function isInRange(x, min, max) {
155
+ return min <= x && x <= max;
156
+ };
157
+ var isInRangeLeft = function isInRangeLeft(x, min, max) {
158
+ return min <= x && x < max;
159
+ };
160
+ var isInRangeRight = function isInRangeRight(x, min, max) {
161
+ return min < x && x <= max;
162
+ };
163
+ var isInRangeCenter = function isInRangeCenter(x, min, max) {
164
+ return min < x && x < max;
165
+ };
124
166
 
125
- if (canvas.width !== newCanvasWidth || canvas.height !== newCanvasHeight) {
126
- var context = canvas.getContext('2d');
167
+ var addXY = function addXY(a, b) {
168
+ return [a[0] + b[0], a[1] + b[1]];
169
+ };
170
+ var subXY = function subXY(a, b) {
171
+ return [a[0] - b[0], a[1] - b[1]];
172
+ };
173
+ var mulXY = function mulXY(a, b) {
174
+ return [a[0] * b[0], a[1] * b[1]];
175
+ };
176
+ var maxXY = function maxXY(a, b) {
177
+ return [Math.max(a[0], b[0]), Math.max(a[1], b[1])];
178
+ };
179
+ var clampXY = function clampXY(p, min, max) {
180
+ if (max === void 0) {
181
+ max = [Infinity, Infinity];
182
+ }
127
183
 
128
- if (context) {
129
- canvas.width = newCanvasWidth;
130
- canvas.height = newCanvasHeight;
131
- context.scale(ratio, ratio);
132
- }
184
+ return [clamp(p[0], min[0], max[0]), clamp(p[1], min[1], max[1])];
185
+ };
186
+ var getDirectionStep = function getDirectionStep(direction) {
187
+ if (direction === 'left') return [-1, 0];
188
+ if (direction === 'right') return [1, 0];
189
+ if (direction === 'up') return [0, -1];
190
+ if (direction === 'down') return [0, 1];
191
+ return [0, 0];
192
+ };
193
+ var isSameXY = function isSameXY(a, b) {
194
+ return a[0] === b[0] && a[1] === b[1];
195
+ };
196
+ var isSameSelection = function isSameSelection(a, b) {
197
+ var a1 = a[0],
198
+ a2 = a[1];
199
+ var b1 = b[0],
200
+ b2 = b[1];
201
+ return isSameXY(a1, b1) && isSameXY(a2, b2);
202
+ };
203
+ var isMaybeRowSelection = function isMaybeRowSelection(selection) {
204
+ var _selection$ = selection[0],
205
+ left = _selection$[0],
206
+ _selection$2 = selection[1],
207
+ right = _selection$2[0];
208
+ return left === -1 && right === -1;
209
+ };
210
+ var isMaybeColumnSelection = function isMaybeColumnSelection(selection) {
211
+ var _selection$3 = selection[0],
212
+ top = _selection$3[1],
213
+ _selection$4 = selection[1],
214
+ bottom = _selection$4[1];
215
+ return top === -1 && bottom === -1;
216
+ };
217
+ var isRowSelection = function isRowSelection(selection) {
218
+ var _selection$5 = selection[0],
219
+ left = _selection$5[0],
220
+ top = _selection$5[1],
221
+ _selection$6 = selection[1],
222
+ right = _selection$6[0],
223
+ bottom = _selection$6[1];
224
+ return left === -1 && right === -1 && top !== -1 && bottom !== -1;
225
+ };
226
+ var isColumnSelection = function isColumnSelection(selection) {
227
+ var _selection$7 = selection[0],
228
+ left = _selection$7[0],
229
+ top = _selection$7[1],
230
+ _selection$8 = selection[1],
231
+ right = _selection$8[0],
232
+ bottom = _selection$8[1];
233
+ return top === -1 && bottom === -1 && left !== -1 && right !== -1;
234
+ };
235
+ var isCellSelection = function isCellSelection(selection) {
236
+ var _selection$9 = selection[0],
237
+ left = _selection$9[0],
238
+ top = _selection$9[1],
239
+ _selection$10 = selection[1],
240
+ right = _selection$10[0],
241
+ bottom = _selection$10[1];
242
+ return left !== -1 && right !== -1 && top !== -1 && bottom !== -1;
243
+ };
244
+ var isEmptySelection = function isEmptySelection(selection) {
245
+ var _selection$11 = selection[0],
246
+ left = _selection$11[0],
247
+ top = _selection$11[1],
248
+ _selection$12 = selection[1],
249
+ right = _selection$12[0],
250
+ bottom = _selection$12[1];
251
+ return left === -1 && right === -1 && top === -1 && bottom === -1;
252
+ };
253
+ var isPointInsideSelection = function isPointInsideSelection(selection, point) {
254
+ var _normalizeSelection = normalizeSelection(selection),
255
+ _normalizeSelection$ = _normalizeSelection[0],
256
+ left = _normalizeSelection$[0],
257
+ top = _normalizeSelection$[1],
258
+ _normalizeSelection$2 = _normalizeSelection[1],
259
+ right = _normalizeSelection$2[0],
260
+ bottom = _normalizeSelection$2[1];
261
+
262
+ var x = point[0],
263
+ y = point[1];
264
+ return x >= left && x <= right && y >= top && y <= bottom;
265
+ };
266
+ var normalizeSelection = function normalizeSelection(selection) {
267
+ var _selection$13 = selection[0],
268
+ left = _selection$13[0],
269
+ top = _selection$13[1],
270
+ _selection$14 = selection[1],
271
+ right = _selection$14[0],
272
+ bottom = _selection$14[1];
273
+
274
+ if (left > right) {
275
+ var _ref = [right, left];
276
+ left = _ref[0];
277
+ right = _ref[1];
278
+ }
133
279
 
134
- return true;
280
+ if (top > bottom) {
281
+ var _ref2 = [bottom, top];
282
+ top = _ref2[0];
283
+ bottom = _ref2[1];
135
284
  }
136
285
 
137
- return false;
138
- }
286
+ return [[left, top], [right, bottom]];
287
+ };
139
288
 
140
- function createRowOrColumnPropFunction(rowColProp, defaultValue) {
289
+ var createRowOrColumnProp = function createRowOrColumnProp(rowColProp, defaultValue) {
141
290
  if (Array.isArray(rowColProp)) {
142
291
  return function (rowOrColIndex) {
143
292
  if (rowOrColIndex >= 0 && rowOrColIndex < rowColProp.length) {
@@ -157,9 +306,8 @@ function createRowOrColumnPropFunction(rowColProp, defaultValue) {
157
306
  return defaultValue;
158
307
  };
159
308
  }
160
- }
161
-
162
- function createCellPropFunction(cellProp, defaultValue) {
309
+ };
310
+ var createCellProp = function createCellProp(cellProp, defaultValue) {
163
311
  if (Array.isArray(cellProp)) {
164
312
  return function (x, y) {
165
313
  if (y >= 0 && y < cellProp.length) {
@@ -183,217 +331,8 @@ function createCellPropFunction(cellProp, defaultValue) {
183
331
  return defaultValue;
184
332
  };
185
333
  }
186
- }
187
-
188
- function applyAlignment(start, cellSize, style, imageWidth, alignment) {
189
- if (!alignment) {
190
- alignment = style.textAlign;
191
- }
192
-
193
- if (alignment === 'left') {
194
- return start + style.marginLeft;
195
- } else if (alignment === 'center') {
196
- return start + cellSize * 0.5 - imageWidth / 2;
197
- } else if (alignment === 'right') {
198
- return start + (cellSize - style.marginRight - imageWidth);
199
- }
200
-
201
- return start;
202
- }
203
-
204
- function drawCell(context, cellContent, style, defaultCellStyle, xCoord, yCoord, cellWidth, cellHeight) {
205
- if (cellContent === null) {
206
- return;
207
- }
208
-
209
- var finalStyle = createStyleObject(style, defaultCellStyle);
210
- context.fillStyle = finalStyle.color;
211
- context.font = finalStyle.weight + ' ' + finalStyle.fontSize + 'px ' + finalStyle.fontFamily;
212
- context.textAlign = finalStyle.textAlign;
213
- var yy = yCoord + cellHeight * 0.5;
214
- context.save();
215
- context.beginPath();
216
- context.rect(xCoord, yCoord, cellWidth, cellHeight);
217
- context.clip();
218
-
219
- if (finalStyle.backgroundColor !== '') {
220
- context.fillStyle = finalStyle.backgroundColor;
221
- context.fillRect(xCoord, yCoord, cellWidth, cellHeight);
222
- context.fillStyle = finalStyle.color;
223
- }
224
-
225
- if (typeof cellContent === 'string' || typeof cellContent === 'number') {
226
- var xx = applyAlignment(xCoord, cellWidth, finalStyle, 0);
227
- context.fillText('' + cellContent, xx, yy);
228
- } else if (typeof cellContent === 'object') {
229
- for (var _iterator = _createForOfIteratorHelperLoose(cellContent.items), _step; !(_step = _iterator()).done;) {
230
- var obj = _step.value;
231
-
232
- if (obj.content instanceof HTMLImageElement) {
233
- var w = obj.width || cellWidth;
234
- var finalX = applyAlignment(xCoord, cellWidth, finalStyle, w, obj.horiozntalAlign);
235
- context.drawImage(obj.content, finalX + obj.x, yy + obj.y, obj.width || cellWidth, obj.height || cellHeight);
236
- } else if (typeof obj.content === 'string' || typeof obj.content === 'number') {
237
- if (obj.horiozntalAlign) {
238
- context.textAlign = obj.horiozntalAlign;
239
- }
240
-
241
- var _finalX = applyAlignment(xCoord, cellWidth, finalStyle, 0, obj.horiozntalAlign);
242
-
243
- context.fillText('' + obj.content, _finalX + obj.x, yy + obj.y);
244
- }
245
- }
246
- }
247
-
248
- context.restore();
249
- }
250
-
251
- function calculateRowsOrColsSizes(freezeCount, size, startingSize, startingIndex, visibleArea) {
252
- var visible = [];
253
- var start = [];
254
- var end = [];
255
- var prev = 0;
256
- start.push(startingSize);
257
- visible.push(freezeCount > 0 ? 0 : startingIndex);
258
- var firstSize = freezeCount > 0 ? size(0) : size(startingIndex);
259
- prev = startingSize + firstSize;
260
- end.push(prev);
261
- var ind = freezeCount > 0 ? 1 : startingIndex + 1;
262
-
263
- if (freezeCount > 0) {
264
- for (; ind < freezeCount; ind++) {
265
- visible.push(ind);
266
- start.push(prev);
267
- prev = prev + size(ind);
268
- end.push(prev);
269
- }
270
-
271
- ind = Math.max(startingIndex, freezeCount);
272
- }
273
-
274
- while (true) {
275
- visible.push(ind);
276
- start.push(prev);
277
- prev = prev + size(ind);
278
- end.push(prev);
279
-
280
- if (end[end.length - 1] >= visibleArea) {
281
- break;
282
- }
283
-
284
- ind++;
285
- }
286
-
287
- return {
288
- index: visible,
289
- start: start,
290
- end: end
291
- };
292
- }
293
-
294
- function createStyleObject(optionalStyle, defaultStyle) {
295
- return _extends({}, defaultStyle, optionalStyle);
296
- }
297
-
298
- function excelHeaderString(num) {
299
- var s = '';
300
- var t = 0;
301
-
302
- while (num > 0) {
303
- t = (num - 1) % 26;
304
- s = String.fromCharCode(65 + t) + s;
305
- num = (num - t) / 26 | 0;
306
- }
307
-
308
- return s || '';
309
- }
310
-
311
- function absCoordianteToCell(absX, absY, rowSizes, columnSizes) {
312
- var cellX = 0;
313
- var cellY = 0;
314
-
315
- for (var i = 0; i < columnSizes.index.length; i++) {
316
- if (absX >= columnSizes.start[i] && absX <= columnSizes.end[i]) {
317
- cellX = columnSizes.index[i];
318
- break;
319
- }
320
- }
321
-
322
- for (var _i = 0; _i < rowSizes.index.length; _i++) {
323
- if (absY >= rowSizes.start[_i] && absY <= rowSizes.end[_i]) {
324
- cellY = rowSizes.index[_i];
325
- break;
326
- }
327
- }
328
-
329
- return {
330
- x: cellX,
331
- y: cellY
332
- };
333
- }
334
-
335
- function normalizeSelection(selection) {
336
- var selx1 = selection.x1;
337
- var selx2 = selection.x2;
338
-
339
- if (selection.x1 > selection.x2) {
340
- selx1 = selection.x2;
341
- selx2 = selection.x1;
342
- }
343
-
344
- var sely1 = selection.y1;
345
- var sely2 = selection.y2;
346
-
347
- if (selection.y1 > selection.y2) {
348
- sely1 = selection.y2;
349
- sely2 = selection.y1;
350
- }
351
-
352
- return [selx1, sely1, selx2, sely2];
353
- }
354
-
355
- function cellToAbsCoordinate(cellX, cellY, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle) {
356
- var absX = sheetStyle.rowHeaderWidth;
357
- var indX = columnSizes.index.findIndex(function (i) {
358
- return i === cellX;
359
- });
360
-
361
- if (indX !== -1) {
362
- absX = columnSizes.start[indX];
363
- } else {
364
- for (var i = 0; i < dataOffset.x; i++) {
365
- absX -= cellWidth(i);
366
- }
367
-
368
- for (var _i2 = 0; _i2 < cellX; _i2++) {
369
- absX += cellWidth(_i2);
370
- }
371
- }
372
-
373
- var absY = sheetStyle.columnHeaderHeight;
374
- var indY = rowSizes.index.findIndex(function (i) {
375
- return i === cellY;
376
- });
377
-
378
- if (indY !== -1) {
379
- absY = rowSizes.start[indY];
380
- } else {
381
- for (var _i3 = 0; _i3 < dataOffset.y; _i3++) {
382
- absY -= cellHeight(_i3);
383
- }
384
-
385
- for (var _i4 = 0; _i4 < cellY; _i4++) {
386
- absY += cellHeight(_i4);
387
- }
388
- }
389
-
390
- return {
391
- x: absX,
392
- y: absY
393
- };
394
- }
395
-
396
- function findApproxMaxEditDataIndex(editData) {
334
+ };
335
+ var findApproxMaxEditDataIndex = function findApproxMaxEditDataIndex(editData) {
397
336
  var x = 0;
398
337
  var y = 0;
399
338
  var howManyEmpty = 0;
@@ -418,7 +357,7 @@ function findApproxMaxEditDataIndex(editData) {
418
357
 
419
358
  x += growthIncrement;
420
359
 
421
- if (x > maxSearchableRowOrCol) {
360
+ if (x > MAX_SEARCHABLE_INDEX) {
422
361
  break;
423
362
  }
424
363
 
@@ -447,1823 +386,2439 @@ function findApproxMaxEditDataIndex(editData) {
447
386
 
448
387
  y += growthIncrement;
449
388
 
450
- if (y > maxSearchableRowOrCol) {
389
+ if (y > MAX_SEARCHABLE_INDEX) {
451
390
  break;
452
391
  }
453
392
 
454
393
  growthIncrement = Math.floor(growthIncrement * growthIncrementFactor);
455
394
  }
456
395
 
457
- return {
458
- x: x,
459
- y: y
460
- };
461
- }
462
-
463
- function findInDisplayData(displayData, start, direction) {
464
- var i = _extends({}, start);
465
-
466
- var increment = {
467
- x: 0,
468
- y: 0
469
- };
470
-
471
- if (direction === 'up') {
472
- increment.y = -1;
473
- } else if (direction === 'down') {
474
- increment.y = 1;
475
- } else if (direction === 'left') {
476
- increment.x = -1;
477
- } else if (direction === 'right') {
478
- increment.x = 1;
479
- }
480
-
481
- var max = {
482
- x: maxSearchableRowOrCol,
483
- y: maxSearchableRowOrCol
484
- };
485
-
486
- if (i.x > max.x) {
487
- i.x = max.x;
488
- }
489
-
490
- if (i.y > max.y) {
491
- i.y = max.y;
492
- }
493
-
494
- var first = displayData(i.x + increment.x, i.y + increment.y);
396
+ return [x, y];
397
+ };
398
+ var findInDisplayData = function findInDisplayData(displayData, start, direction) {
399
+ var step = getDirectionStep(direction);
400
+ var cell = clampXY(start, ORIGIN, MAX_XY);
401
+ var first = displayData.apply(void 0, addXY(cell, step));
495
402
  var firstFilled = first !== '' && first !== null && first !== undefined;
496
403
 
497
404
  if (!firstFilled) {
498
- i.x += increment.x;
499
- i.y += increment.y;
405
+ cell = addXY(cell, step);
500
406
  }
501
407
 
502
- while (i.x <= max.x && i.y <= max.y && i.x >= 0 && i.y >= 0) {
503
- var data = displayData(i.x, i.y);
408
+ var _cell = cell,
409
+ cellX = _cell[0],
410
+ cellY = _cell[1];
411
+
412
+ while (cellX <= MAX_SEARCHABLE_INDEX && cellY <= MAX_SEARCHABLE_INDEX && cellX >= 0 && cellY >= 0) {
413
+ var data = displayData(cellX, cellY);
504
414
 
505
415
  if (firstFilled && (data === '' || data === null || data === undefined)) {
506
- return {
507
- x: i.x - increment.x,
508
- y: i.y - increment.y
509
- };
416
+ return subXY(cell, step);
510
417
  }
511
418
 
512
419
  if (!firstFilled && data !== '' && data !== null && data !== undefined) {
513
- return _extends({}, i);
420
+ return cell;
514
421
  }
515
422
 
516
- i.x += increment.x;
517
- i.y += increment.y;
518
- }
423
+ var _cell2 = cell = addXY(cell, step);
519
424
 
520
- return i;
521
- }
425
+ cellX = _cell2[0];
426
+ cellY = _cell2[1];
427
+ }
522
428
 
523
- function renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, secondarySelections, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle) {
524
- resizeCanvas(context.canvas);
525
- context.clearRect(0, 0, context.canvas.width, context.canvas.height);
526
- context.fillStyle = 'white';
527
- context.fillRect(0, 0, context.canvas.width, context.canvas.height);
528
- var yCoord1 = sheetStyle.columnHeaderHeight;
429
+ return maxXY(cell, [0, 0]);
430
+ };
529
431
 
530
- for (var _iterator2 = _createForOfIteratorHelperLoose(rowSizes.index), _step2; !(_step2 = _iterator2()).done;) {
531
- var y = _step2.value;
532
- var xCoord1 = sheetStyle.rowHeaderWidth;
432
+ var useMouse = function useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, onEdit, onCommit, onKnobAreaChange, onDragOffsetChange, onDropTargetChange, onSelectionChange, onInvalidateColumn, onInvalidateRow, onChange, onColumnOrderChange, onRowOrderChange, onCellWidthChange, onCellHeightChange, onRightClick, dontCommitEditOnSelectionChange) {
433
+ var _useState = useState(null),
434
+ hitTarget = _useState[0],
435
+ setHitTarget = _useState[1];
533
436
 
534
- for (var _iterator9 = _createForOfIteratorHelperLoose(columnSizes.index), _step9; !(_step9 = _iterator9()).done;) {
535
- var x = _step9.value;
536
- var style = cellStyle(x, y);
437
+ var _useState2 = useState(null),
438
+ columnResize = _useState2[0],
439
+ setColumnResize = _useState2[1];
537
440
 
538
- if (style.fillColor) {
539
- context.fillStyle = style.fillColor;
540
- context.fillRect(xCoord1, yCoord1, cellWidth(x), cellHeight(y));
541
- }
441
+ var _useState3 = useState(null),
442
+ rowResize = _useState3[0],
443
+ setRowResize = _useState3[1];
542
444
 
543
- xCoord1 += cellWidth(x);
544
- }
445
+ var _useState4 = useState(null),
446
+ columnDrag = _useState4[0],
447
+ setColumnDrag = _useState4[1];
545
448
 
546
- yCoord1 += cellHeight(y);
547
- }
449
+ var _useState5 = useState(null),
450
+ rowDrag = _useState5[0],
451
+ setRowDrag = _useState5[1];
548
452
 
549
- var hideKnob = false;
453
+ var _useState6 = useState(false),
454
+ draggingKnob = _useState6[0],
455
+ setDraggingKnob = _useState6[1];
550
456
 
551
- var _normalizeSelection = normalizeSelection(selection),
552
- selx1 = _normalizeSelection[0],
553
- sely1 = _normalizeSelection[1],
554
- selx2 = _normalizeSelection[2],
555
- sely2 = _normalizeSelection[3];
457
+ var _useState7 = useState(false),
458
+ draggingSelection = _useState7[0],
459
+ setDraggingSelection = _useState7[1];
556
460
 
557
- var selectionActive = selx1 !== -1 && selx2 !== -1 || sely1 !== -1 && sely2 !== -1;
558
- var rowSelectionActive = selx1 === -1 && selx2 === -1 && sely1 !== -1 && sely2 !== -1;
559
- var colSelectionActive = selx1 !== -1 && selx2 !== -1 && sely1 === -1 && sely2 === -1;
560
- var p1 = cellToAbsCoordinate(selx1, sely1, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
561
- var p2 = cellToAbsCoordinate(selx2, sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
562
- p2.x += cellWidth(selx2);
563
- p2.y += cellHeight(sely2);
461
+ var _useState8 = useState(false),
462
+ draggingRowSelection = _useState8[0],
463
+ setDraggingRowSelection = _useState8[1];
564
464
 
565
- if (p1.x >= p2.x) {
566
- p2.x = p1.x;
567
- var currentCol = selx1;
465
+ var _useState9 = useState(false),
466
+ draggingColumnSelection = _useState9[0],
467
+ setDraggingColumnSelection = _useState9[1];
468
+
469
+ var hideRowHeaders = sheetStyle.hideRowHeaders,
470
+ hideColumnHeaders = sheetStyle.hideColumnHeaders;
471
+ var cellToPixel = cellLayout.cellToPixel,
472
+ getVersion = cellLayout.getVersion;
473
+ var version = getVersion();
474
+ var knobPosition = useMemo(function () {
475
+ var _normalizeSelection = normalizeSelection(selection),
476
+ _normalizeSelection$ = _normalizeSelection[1],
477
+ maxX = _normalizeSelection$[0],
478
+ maxY = _normalizeSelection$[1];
479
+
480
+ if (isRowSelection(selection)) {
481
+ return subXY(addXY(cellToPixel([0, maxY], [0, 1]), [SIZES.knobArea * 0.5, 0]), ONE_ONE);
482
+ }
483
+
484
+ if (isColumnSelection(selection)) {
485
+ return subXY(addXY(cellToPixel([maxX, 0], [1, 0]), [0, SIZES.knobArea * 0.5]), ONE_ONE);
486
+ }
487
+
488
+ if (isCellSelection(selection)) {
489
+ return subXY(cellToPixel([maxX, maxY], ONE_ONE), ONE_ONE);
490
+ }
491
+
492
+ return null;
493
+ }, [selection, cellToPixel, version]);
494
+ var refState = {
495
+ selection: selection,
496
+ knobArea: knobArea,
497
+ editMode: editMode,
498
+ editData: editData,
499
+ sourceData: sourceData,
500
+ cellLayout: cellLayout,
501
+ visibleCells: visibleCells,
502
+ knobPosition: knobPosition,
503
+ columnResize: columnResize,
504
+ rowResize: rowResize,
505
+ columnDrag: columnDrag,
506
+ rowDrag: rowDrag,
507
+ draggingKnob: draggingKnob,
508
+ draggingSelection: draggingSelection,
509
+ draggingRowSelection: draggingRowSelection,
510
+ draggingColumnSelection: draggingColumnSelection
511
+ };
512
+ var ref = useRef(refState);
513
+ ref.current = refState;
514
+ var getMousePosition = useCallback(function (e) {
515
+ if (!e.target || !(e.target instanceof Element)) {
516
+ return null;
517
+ }
568
518
 
569
- while (columnSizes.index.includes(currentCol)) {
570
- p2.x += cellWidth(currentCol);
571
- currentCol++;
519
+ var rect = e.target.getBoundingClientRect();
520
+ var xy = [e.clientX - rect.left, e.clientY - rect.top];
521
+ return xy;
522
+ }, []);
523
+ var getScrollPosition = useCallback(function (e) {
524
+ if (!e.target || !(e.target instanceof Element)) {
525
+ return [0, 0];
572
526
  }
573
527
 
574
- hideKnob = true;
575
- }
528
+ var _e$target = e.target,
529
+ scrollLeft = _e$target.scrollLeft,
530
+ scrollTop = _e$target.scrollTop;
531
+ var xy = [scrollLeft, scrollTop];
532
+ return xy;
533
+ }, []);
534
+ var getMouseHit = useCallback(function (xy) {
535
+ var hitmap = hitmapRef.current;
536
+ if (!hitmap) return null;
576
537
 
577
- if (p1.y >= p2.y) {
578
- p2.y = p1.y;
579
- var currentRow = sely1;
538
+ for (var _iterator = _createForOfIteratorHelperLoose(hitmap), _step; !(_step = _iterator()).done;) {
539
+ var object = _step.value;
540
+ var rect = object.rect;
580
541
 
581
- while (rowSizes.index.includes(currentRow)) {
582
- p2.y += cellHeight(currentRow);
583
- currentRow++;
542
+ if (isPointInsideSelection(rect, xy)) {
543
+ return object;
544
+ }
584
545
  }
585
546
 
586
- hideKnob = true;
587
- }
588
-
589
- if (selectionActive) {
590
- context.fillStyle = selBackColor;
591
-
592
- if (rowSelectionActive) {
593
- var p1x = Math.max(-100, p1.x);
594
- context.fillRect(p1x, p1.y, 100000, p2.y - p1.y);
595
- } else if (colSelectionActive) {
596
- var p1y = Math.max(0, p1.y);
597
- context.fillRect(p1.x, p1y, p2.x - p1.x, 100000);
598
- } else {
599
- context.fillRect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
547
+ return null;
548
+ }, [hitmapRef]);
549
+ var onPointerLeave = useCallback(function () {
550
+ window.document.body.style.cursor = 'auto';
551
+ }, []);
552
+ var onPointerDown = useCallback(function (e) {
553
+ var _e$target2, _e$target2$setPointer;
554
+
555
+ var _ref$current = ref.current,
556
+ selection = _ref$current.selection,
557
+ _ref$current$cellLayo = _ref$current.cellLayout,
558
+ columnToPixel = _ref$current$cellLayo.columnToPixel,
559
+ rowToPixel = _ref$current$cellLayo.rowToPixel,
560
+ pixelToCell = _ref$current$cellLayo.pixelToCell,
561
+ getIndentX = _ref$current$cellLayo.getIndentX,
562
+ getIndentY = _ref$current$cellLayo.getIndentY,
563
+ _ref$current$visibleC = _ref$current.visibleCells,
564
+ columns = _ref$current$visibleC.columns,
565
+ rows = _ref$current$visibleC.rows,
566
+ knobPosition = _ref$current.knobPosition;
567
+ if (e.button !== 0) return;
568
+ (_e$target2 = e.target) === null || _e$target2 === void 0 ? void 0 : (_e$target2$setPointer = _e$target2.setPointerCapture) === null || _e$target2$setPointer === void 0 ? void 0 : _e$target2$setPointer.call(_e$target2, e.pointerId);
569
+ var xy = getMousePosition(e);
570
+ if (!xy) return;
571
+ var x = xy[0],
572
+ y = xy[1];
573
+ var hitTarget = getMouseHit(xy);
574
+
575
+ if (hitTarget) {
576
+ setHitTarget(hitTarget);
577
+ return;
600
578
  }
601
- }
602
579
 
603
- if (!sheetStyle.hideRowHeaders) {
604
- context.fillStyle = rowHeaderBackgroundColor;
605
- context.fillRect(0, 0, sheetStyle.rowHeaderWidth, context.canvas.height);
580
+ var _normalizeSelection2 = normalizeSelection(selection),
581
+ _normalizeSelection2$ = _normalizeSelection2[0],
582
+ minX = _normalizeSelection2$[0],
583
+ minY = _normalizeSelection2$[1],
584
+ _normalizeSelection2$2 = _normalizeSelection2[1],
585
+ maxX = _normalizeSelection2$2[0],
586
+ maxY = _normalizeSelection2$2[1];
606
587
 
607
- if (selectionActive) {
608
- context.fillStyle = rowHeaderSelectedBackgroundColor;
609
- context.fillRect(0, p1.y, sheetStyle.rowHeaderWidth, p2.y - p1.y);
610
- }
611
- }
588
+ var selectedColumns = [];
589
+ var selectedRows = [];
612
590
 
613
- if (!sheetStyle.hideColumnHeaders) {
614
- context.fillStyle = columnHeaderBackgroundColor;
615
- context.fillRect(0, 0, context.canvas.width, sheetStyle.columnHeaderHeight);
616
-
617
- if (selectionActive) {
618
- context.fillStyle = columnHeaderSelectedBackgroundColor;
619
- context.fillRect(p1.x, 0, p2.x - p1.x, sheetStyle.columnHeaderHeight);
591
+ for (var i = minX; i <= maxX; i++) {
592
+ selectedColumns.push(i);
620
593
  }
621
- }
622
594
 
623
- context.strokeStyle = gridColor;
624
- context.lineWidth = 1;
625
- var startX = sheetStyle.rowHeaderWidth;
626
- var startY = sheetStyle.columnHeaderHeight;
627
- var first = true;
628
- var yGridlineEnd = sheetStyle.hideGridlines ? sheetStyle.columnHeaderHeight : context.canvas.height;
595
+ for (var _i = minY; _i <= maxY; _i++) {
596
+ selectedRows.push(_i);
597
+ }
629
598
 
630
- for (var _iterator3 = _createForOfIteratorHelperLoose(columnSizes.index), _step3; !(_step3 = _iterator3()).done;) {
631
- var _col = _step3.value;
632
- context.beginPath();
633
- context.moveTo(startX, 0);
599
+ if (!hideColumnHeaders && y < getIndentY()) {
600
+ if (onColumnOrderChange) {
601
+ var start = columnToPixel(minX) + SIZES.resizeZone;
602
+ var end = columnToPixel(maxX, 1) - SIZES.resizeZone;
634
603
 
635
- if (first) {
636
- if (!sheetStyle.hideRowHeaders) {
637
- context.lineTo(startX, context.canvas.height);
638
- }
604
+ if (isInRange(x, start, end)) {
605
+ for (var _iterator2 = _createForOfIteratorHelperLoose(columns), _step2; !(_step2 = _iterator2()).done;) {
606
+ var index = _step2.value;
639
607
 
640
- first = false;
641
- } else {
642
- context.lineTo(startX, yGridlineEnd);
643
- }
608
+ var _start = columnToPixel(index, 0);
644
609
 
645
- context.stroke();
646
- startX += cellWidth(_col);
647
- }
610
+ var _end = columnToPixel(index, 1);
648
611
 
649
- var xGridlineEnd = sheetStyle.hideGridlines ? sheetStyle.rowHeaderWidth : context.canvas.width;
650
- first = true;
612
+ if (isColumnSelection(selection) && isInRange(x, _start, _end) && isInRange(index, minX, maxX) && canOrderColumn(index)) {
613
+ window.document.body.style.cursor = 'grabbing';
614
+ var indices = selectedColumns;
615
+ var size = columnToPixel(maxX, 1) - columnToPixel(minX);
651
616
 
652
- for (var _iterator4 = _createForOfIteratorHelperLoose(rowSizes.index), _step4; !(_step4 = _iterator4()).done;) {
653
- var _row = _step4.value;
654
- context.beginPath();
655
- context.moveTo(0, startY);
617
+ var _getScrollPosition = getScrollPosition(e),
618
+ scroll = _getScrollPosition[0];
656
619
 
657
- if (first) {
658
- if (!sheetStyle.hideColumnHeaders) {
659
- context.lineTo(context.canvas.width, startY);
620
+ setColumnDrag({
621
+ anchor: x,
622
+ scroll: scroll,
623
+ size: size,
624
+ indices: indices
625
+ });
626
+ onDragOffsetChange === null || onDragOffsetChange === void 0 ? void 0 : onDragOffsetChange([0, 0]);
627
+ return;
628
+ }
629
+ }
630
+ }
660
631
  }
661
632
 
662
- first = false;
663
- } else {
664
- context.lineTo(xGridlineEnd, startY);
665
- }
633
+ if (onCellWidthChange) {
634
+ for (var _iterator3 = _createForOfIteratorHelperLoose(columns), _step3; !(_step3 = _iterator3()).done;) {
635
+ var _index = _step3.value;
636
+ var edge = columnToPixel(_index, 1);
666
637
 
667
- context.stroke();
668
- startY += cellHeight(_row);
669
- }
638
+ if (Math.abs(edge - x) < SIZES.resizeZone && canSizeColumn(_index)) {
639
+ window.document.body.style.cursor = 'col-resize';
670
640
 
671
- if (!sheetStyle.hideRowHeaders) {
672
- startY = sheetStyle.columnHeaderHeight;
673
- context.textBaseline = 'middle';
674
- context.textAlign = 'center';
675
- context.font = defaultCellStyle.fontSize + 'px ' + defaultCellStyle.fontFamily;
676
- context.fillStyle = rowHeaderTextColor;
641
+ var asGroup = isColumnSelection(selection) && maxX === _index;
677
642
 
678
- for (var _iterator5 = _createForOfIteratorHelperLoose(rowSizes.index), _step5; !(_step5 = _iterator5()).done;) {
679
- var row = _step5.value;
680
- var cellContent = row + 1;
681
- var chStyle = {};
643
+ var _indices = asGroup ? selectedColumns : [_index];
682
644
 
683
- if (rowSelectionActive && sely1 <= row && sely2 >= row) {
684
- chStyle = _extends({}, chStyle, {
685
- backgroundColor: rowColHeaderSelectionColor
686
- });
687
- }
645
+ var _size = asGroup ? columnToPixel(maxX, 1) - columnToPixel(minX) : columnToPixel(_index, 1) - columnToPixel(_index);
688
646
 
689
- drawCell(context, '' + cellContent, chStyle, defaultColumnHeaderStyle, 0, startY, sheetStyle.rowHeaderWidth, cellHeight(row));
690
- startY += cellHeight(row);
647
+ var _getScrollPosition2 = getScrollPosition(e),
648
+ _scroll = _getScrollPosition2[0];
649
+
650
+ setColumnResize({
651
+ anchor: x,
652
+ scroll: _scroll,
653
+ size: _size,
654
+ indices: _indices
655
+ });
656
+ return;
657
+ }
658
+ }
659
+ }
691
660
  }
692
- }
693
661
 
694
- if (!sheetStyle.hideColumnHeaders) {
695
- startX = sheetStyle.rowHeaderWidth;
696
- context.textBaseline = 'middle';
697
- context.textAlign = 'center';
662
+ if (!hideRowHeaders && x < getIndentX()) {
663
+ if (onRowOrderChange) {
664
+ var _start2 = rowToPixel(minY) + SIZES.resizeZone;
698
665
 
699
- for (var _iterator6 = _createForOfIteratorHelperLoose(columnSizes.index), _step6; !(_step6 = _iterator6()).done;) {
700
- var col = _step6.value;
701
- var cw = cellWidth(col);
702
- var ch = columnHeaders(col);
703
- var chcontent = ch !== null ? ch : excelHeaderString(col + 1);
666
+ var _end2 = rowToPixel(maxY, 1) - SIZES.resizeZone;
704
667
 
705
- var _chStyle = columnHeaderStyle(col);
668
+ if (isInRange(y, _start2, _end2)) {
669
+ for (var _iterator4 = _createForOfIteratorHelperLoose(rows), _step4; !(_step4 = _iterator4()).done;) {
670
+ var _index2 = _step4.value;
706
671
 
707
- if (colSelectionActive && selx1 <= col && selx2 >= col) {
708
- _chStyle = _extends({}, _chStyle, {
709
- backgroundColor: rowColHeaderSelectionColor
710
- });
711
- }
672
+ var _start3 = rowToPixel(_index2, 0);
712
673
 
713
- drawCell(context, chcontent, _chStyle, defaultColumnHeaderStyle, startX, 0, cw, sheetStyle.columnHeaderHeight);
714
- startX += cw;
715
- }
716
- }
674
+ var _end3 = rowToPixel(_index2, 1);
717
675
 
718
- if (selectionActive) {
719
- context.strokeStyle = selBorderColor;
720
- context.lineWidth = 1;
721
- context.beginPath();
722
-
723
- if (rowSelectionActive) {
724
- var _p1x = Math.max(-100, p1.x);
676
+ if (isRowSelection(selection) && isInRange(y, _start3, _end3) && isInRange(_index2, minY, maxY) && canOrderRow(_index2)) {
677
+ window.document.body.style.cursor = 'grabbing';
678
+ var _indices2 = selectedRows;
725
679
 
726
- context.rect(_p1x, p1.y, 100000, p2.y - p1.y);
727
- } else if (colSelectionActive) {
728
- var _p1y = Math.max(-100, p1.y);
680
+ var _size2 = rowToPixel(maxY, 1) - rowToPixel(minY);
729
681
 
730
- context.rect(p1.x, _p1y, p2.x - p1.x, 100000);
731
- } else {
732
- context.rect(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y);
733
- }
682
+ var _getScrollPosition3 = getScrollPosition(e),
683
+ _scroll2 = _getScrollPosition3[1];
734
684
 
735
- context.stroke();
736
- }
685
+ setRowDrag({
686
+ anchor: y,
687
+ scroll: _scroll2,
688
+ size: _size2,
689
+ indices: _indices2
690
+ });
691
+ onDragOffsetChange === null || onDragOffsetChange === void 0 ? void 0 : onDragOffsetChange([0, 0]);
692
+ return;
693
+ }
694
+ }
695
+ }
696
+ }
737
697
 
738
- for (var _iterator7 = _createForOfIteratorHelperLoose(secondarySelections), _step7; !(_step7 = _iterator7()).done;) {
739
- var secondarySelection = _step7.value;
698
+ if (onCellHeightChange) {
699
+ for (var _iterator5 = _createForOfIteratorHelperLoose(rows), _step5; !(_step5 = _iterator5()).done;) {
700
+ var _index3 = _step5.value;
740
701
 
741
- var _normalizeSelection2 = normalizeSelection(secondarySelection.span),
742
- _selx = _normalizeSelection2[0],
743
- _sely = _normalizeSelection2[1],
744
- _selx2 = _normalizeSelection2[2],
745
- _sely2 = _normalizeSelection2[3];
702
+ var _edge = rowToPixel(_index3, 1);
746
703
 
747
- var secondarySelectionActive = _selx !== -1 && _selx2 !== -1 || _sely !== -1 && _sely2 !== -1;
704
+ if (Math.abs(_edge - y) < SIZES.resizeZone && canSizeRow(_index3)) {
705
+ window.document.body.style.cursor = 'row-resize';
748
706
 
749
- if (!secondarySelectionActive) {
750
- continue;
751
- }
707
+ var _asGroup = isRowSelection(selection) && maxY === _index3;
752
708
 
753
- var rowSecondarySelectionActive = _selx === -1 && _selx2 === -1 && _sely !== -1 && _sely2 !== -1;
754
- var colSecondarySelectionActive = _selx !== -1 && _selx2 !== -1 && _sely === -1 && _sely2 === -1;
709
+ var _indices3 = _asGroup ? selectedRows : [_index3];
755
710
 
756
- var _p = cellToAbsCoordinate(_selx, _sely, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
711
+ var _size3 = _asGroup ? rowToPixel(maxY, 1) - rowToPixel(minY) : rowToPixel(_index3, 1) - rowToPixel(_index3);
757
712
 
758
- var _p2 = cellToAbsCoordinate(_selx2, _sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
713
+ var _getScrollPosition4 = getScrollPosition(e),
714
+ _scroll3 = _getScrollPosition4[1];
759
715
 
760
- _p2.x += cellWidth(_selx2);
761
- _p2.y += cellHeight(_sely2);
716
+ setRowResize({
717
+ anchor: y,
718
+ scroll: _scroll3,
719
+ size: _size3,
720
+ indices: _indices3
721
+ });
722
+ return;
723
+ }
724
+ }
725
+ }
726
+ }
762
727
 
763
- if (_p.x >= _p2.x) {
764
- _p2.x = _p.x;
765
- var _currentCol = _selx;
728
+ if (knobPosition) {
729
+ var knobX = knobPosition[0],
730
+ knobY = knobPosition[1];
766
731
 
767
- while (columnSizes.index.includes(_currentCol)) {
768
- _p2.x += cellWidth(_currentCol);
769
- _currentCol++;
732
+ if (Math.abs(x - knobX) < SIZES.knobArea && Math.abs(y - knobY) < SIZES.knobArea) {
733
+ setDraggingKnob(true);
734
+ onKnobAreaChange === null || onKnobAreaChange === void 0 ? void 0 : onKnobAreaChange(selection);
735
+ return;
770
736
  }
771
737
  }
772
738
 
773
- if (_p.y >= _p2.y) {
774
- _p2.y = _p.y;
775
- var _currentRow = _sely;
739
+ var head = pixelToCell(xy);
740
+ var anchor = e.shiftKey ? [].concat(selection[0]) : head;
776
741
 
777
- while (rowSizes.index.includes(_currentRow)) {
778
- _p2.y += cellHeight(_currentRow);
779
- _currentRow++;
742
+ if (editMode) {
743
+ if (!dontCommitEditOnSelectionChange) {
744
+ onCommit === null || onCommit === void 0 ? void 0 : onCommit();
745
+ }
746
+ }
747
+
748
+ var scrollToHead = true;
749
+
750
+ if (!hideRowHeaders && x < getIndentX()) {
751
+ scrollToHead = false;
752
+ setDraggingRowSelection(true);
753
+ anchor[0] = -1;
754
+ head[0] = -1;
755
+ }
756
+
757
+ if (!hideColumnHeaders && y < getIndentY()) {
758
+ scrollToHead = false;
759
+ setDraggingColumnSelection(true);
760
+ anchor[1] = -1;
761
+ head[1] = -1;
762
+ }
763
+
764
+ setDraggingSelection(true);
765
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([anchor, head], scrollToHead);
766
+ }, [getMousePosition, getScrollPosition, getMouseHit, onColumnOrderChange, onRowOrderChange, onCellWidthChange, onCellHeightChange, onKnobAreaChange, onSelectionChange, onCommit, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow]);
767
+ var onPointerUp = useCallback(function (e) {
768
+ var _ref$current2 = ref.current,
769
+ knobArea = _ref$current2.knobArea,
770
+ selection = _ref$current2.selection,
771
+ sourceData = _ref$current2.sourceData,
772
+ editData = _ref$current2.editData,
773
+ columnDrag = _ref$current2.columnDrag,
774
+ rowDrag = _ref$current2.rowDrag,
775
+ draggingKnob = _ref$current2.draggingKnob,
776
+ _ref$current2$cellLay = _ref$current2.cellLayout,
777
+ pixelToColumn = _ref$current2$cellLay.pixelToColumn,
778
+ pixelToRow = _ref$current2$cellLay.pixelToRow,
779
+ getIndentX = _ref$current2$cellLay.getIndentX,
780
+ getIndentY = _ref$current2$cellLay.getIndentY;
781
+
782
+ if (knobArea && draggingKnob) {
783
+ var changes = parseKnobOperation(knobArea, selection, sourceData, editData);
784
+ onChange === null || onChange === void 0 ? void 0 : onChange(changes);
785
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(knobArea);
786
+ onKnobAreaChange === null || onKnobAreaChange === void 0 ? void 0 : onKnobAreaChange(null);
787
+ }
788
+
789
+ var xy = getMousePosition(e);
790
+
791
+ if (xy && (columnDrag || rowDrag)) {
792
+ window.document.body.style.cursor = 'auto';
793
+ onDragOffsetChange === null || onDragOffsetChange === void 0 ? void 0 : onDragOffsetChange(null);
794
+ onDropTargetChange === null || onDropTargetChange === void 0 ? void 0 : onDropTargetChange(null);
795
+ var x = xy[0],
796
+ y = xy[1];
797
+
798
+ var _normalizeSelection3 = normalizeSelection(selection),
799
+ _normalizeSelection3$ = _normalizeSelection3[0],
800
+ minX = _normalizeSelection3$[0],
801
+ minY = _normalizeSelection3$[1],
802
+ _normalizeSelection3$2 = _normalizeSelection3[1],
803
+ maxX = _normalizeSelection3$2[0],
804
+ maxY = _normalizeSelection3$2[1];
805
+
806
+ var cellX = pixelToColumn(Math.max(x, getIndentX()), 0.5);
807
+ var cellY = pixelToRow(Math.max(y, getIndentY()), 0.5);
808
+
809
+ if (columnDrag) {
810
+ var indices = columnDrag.indices;
811
+ var insideSelection = cellX >= minX && cellX <= maxX + 1;
812
+
813
+ if (!insideSelection) {
814
+ var order = cellX > minX ? cellX - indices.length : cellX;
815
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([[order, minY], [order + maxX - minX, maxY]]);
816
+ onColumnOrderChange === null || onColumnOrderChange === void 0 ? void 0 : onColumnOrderChange(indices, order);
817
+ onInvalidateColumn === null || onInvalidateColumn === void 0 ? void 0 : onInvalidateColumn(Math.min(minX, order));
818
+ }
780
819
  }
781
- }
782
820
 
783
- context.strokeStyle = secondarySelection.color;
784
- context.lineWidth = 1;
785
- context.beginPath();
821
+ if (rowDrag) {
822
+ var _indices4 = rowDrag.indices;
786
823
 
787
- if (rowSecondarySelectionActive) {
788
- var _p1x2 = Math.max(-100, _p.x);
824
+ var _insideSelection = cellY >= minY && cellY <= maxY + 1;
789
825
 
790
- context.rect(_p1x2, _p.y, 100000, _p2.y - _p.y);
791
- } else if (colSecondarySelectionActive) {
792
- var _p1y2 = Math.max(-100, _p.y);
826
+ if (!_insideSelection) {
827
+ var _order = cellY > minY ? cellY - _indices4.length : cellY;
793
828
 
794
- context.rect(_p.x, _p1y2, _p2.x - _p.x, 100000);
795
- } else {
796
- context.rect(_p.x, _p.y, _p2.x - _p.x, _p2.y - _p.y);
829
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([[minX, _order], [maxX, _order + maxY - minY]]);
830
+ onRowOrderChange === null || onRowOrderChange === void 0 ? void 0 : onRowOrderChange(_indices4, _order);
831
+ onInvalidateRow === null || onInvalidateRow === void 0 ? void 0 : onInvalidateRow(Math.min(minY, _order));
832
+ }
833
+ }
797
834
  }
798
835
 
799
- context.stroke();
800
- }
836
+ setDraggingSelection(false);
837
+ setDraggingRowSelection(false);
838
+ setDraggingColumnSelection(false);
839
+ setDraggingKnob(false);
840
+ setColumnResize(null);
841
+ setColumnDrag(null);
842
+ setRowResize(null);
843
+ setRowDrag(null);
844
+ if (!xy || !hitTarget) return;
845
+ setHitTarget(null);
846
+
847
+ if (hitTarget === getMouseHit(xy)) {
848
+ var _obj$onClick;
849
+
850
+ var obj = hitTarget.obj;
851
+ (_obj$onClick = obj.onClick) === null || _obj$onClick === void 0 ? void 0 : _obj$onClick.call(obj, e);
852
+ }
853
+ }, [getMousePosition, getMouseHit, onChange, onSelectionChange, onKnobAreaChange, onDropTargetChange, onColumnOrderChange, onRowOrderChange]);
854
+ var onPointerMove = useCallback(function (e) {
855
+ var _ref$current3 = ref.current,
856
+ selection = _ref$current3.selection,
857
+ visibleCells = _ref$current3.visibleCells,
858
+ knobPosition = _ref$current3.knobPosition,
859
+ columnResize = _ref$current3.columnResize,
860
+ columnDrag = _ref$current3.columnDrag,
861
+ rowResize = _ref$current3.rowResize,
862
+ rowDrag = _ref$current3.rowDrag,
863
+ draggingKnob = _ref$current3.draggingKnob,
864
+ draggingSelection = _ref$current3.draggingSelection,
865
+ draggingColumnSelection = _ref$current3.draggingColumnSelection,
866
+ draggingRowSelection = _ref$current3.draggingRowSelection,
867
+ _ref$current3$cellLay = _ref$current3.cellLayout,
868
+ columnToPixel = _ref$current3$cellLay.columnToPixel,
869
+ rowToPixel = _ref$current3$cellLay.rowToPixel,
870
+ pixelToCell = _ref$current3$cellLay.pixelToCell,
871
+ pixelToColumn = _ref$current3$cellLay.pixelToColumn,
872
+ pixelToRow = _ref$current3$cellLay.pixelToRow,
873
+ getIndentX = _ref$current3$cellLay.getIndentX,
874
+ getIndentY = _ref$current3$cellLay.getIndentY;
875
+ var xy = getMousePosition(e);
876
+ if (!xy) return;
877
+ window.document.body.style.cursor = 'auto';
878
+ var hitTarget = getMouseHit(xy);
879
+
880
+ if (hitTarget) {
881
+ window.document.body.style.cursor = 'pointer';
882
+ } else if (columnDrag || rowDrag) {
883
+ window.document.body.style.cursor = 'grabbing';
884
+ } else if (columnResize) {
885
+ window.document.body.style.cursor = 'col-resize';
886
+ e.preventDefault();
887
+ } else if (rowResize) {
888
+ window.document.body.style.cursor = 'row-resize';
889
+ e.preventDefault();
890
+ } else if (draggingRowSelection || draggingColumnSelection) {
891
+ e.preventDefault();
892
+ }
801
893
 
802
- if (knobDragInProgress) {
803
- var kx1 = knobArea.x1;
804
- var kx2 = knobArea.x2;
894
+ var columns = visibleCells.columns,
895
+ rows = visibleCells.rows;
896
+ var x = xy[0],
897
+ y = xy[1];
805
898
 
806
- if (knobArea.x1 > knobArea.x2) {
807
- kx1 = knobArea.x2;
808
- kx2 = knobArea.x1;
809
- }
899
+ var _normalizeSelection4 = normalizeSelection(selection),
900
+ _normalizeSelection4$ = _normalizeSelection4[0],
901
+ minX = _normalizeSelection4$[0],
902
+ minY = _normalizeSelection4$[1],
903
+ _normalizeSelection4$2 = _normalizeSelection4[1],
904
+ maxX = _normalizeSelection4$2[0],
905
+ maxY = _normalizeSelection4$2[1];
810
906
 
811
- var ky1 = knobArea.y1;
812
- var ky2 = knobArea.y2;
907
+ var isDragging = columnResize || columnDrag || rowResize || rowDrag || draggingRowSelection || draggingColumnSelection;
813
908
 
814
- if (knobArea.y1 > knobArea.y2) {
815
- ky1 = knobArea.y2;
816
- ky2 = knobArea.y1;
817
- }
909
+ if (!isDragging) {
910
+ if (!hideColumnHeaders && y < getIndentY()) {
911
+ if (onColumnOrderChange) {
912
+ var start = columnToPixel(minX) + SIZES.resizeZone;
913
+ var end = columnToPixel(maxX, 1) - SIZES.resizeZone;
818
914
 
819
- var knobPoint1 = cellToAbsCoordinate(kx1, ky1, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
820
- var knobPoint2 = cellToAbsCoordinate(kx2 + 1, ky2 + 1, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
821
- context.strokeStyle = knobAreaBorderColor;
822
- context.setLineDash([3, 3]);
823
- context.lineWidth = 1;
824
- context.beginPath();
915
+ if (isInRange(x, start, end)) {
916
+ for (var _iterator6 = _createForOfIteratorHelperLoose(columns), _step6; !(_step6 = _iterator6()).done;) {
917
+ var index = _step6.value;
825
918
 
826
- if (rowSelectionActive) {
827
- context.rect(knobPoint1.x, knobPoint1.y - 1, 100000, knobPoint2.y - knobPoint1.y);
828
- } else if (colSelectionActive) {
829
- context.rect(knobPoint1.x, knobPoint1.y - 1, knobPoint2.x - knobPoint1.x, 100000);
830
- } else {
831
- context.rect(knobPoint1.x, knobPoint1.y - 1, knobPoint2.x - knobPoint1.x, knobPoint2.y - knobPoint1.y);
832
- }
919
+ var _start4 = columnToPixel(index);
833
920
 
834
- context.stroke();
835
- context.setLineDash([]);
836
- }
921
+ var _end4 = columnToPixel(index, 1);
837
922
 
838
- if (selectionActive && !hideKnob) {
839
- context.fillStyle = selBorderColor;
840
- context.fillRect(knobCoordinates.x - knobSize * 0.5, knobCoordinates.y - knobSize * 0.5, knobSize, knobSize);
841
- }
923
+ if (!draggingColumnSelection && isColumnSelection(selection) && isInRange(x, _start4, _end4) && isInRange(index, minX, maxX) && canOrderColumn(index)) {
924
+ window.document.body.style.cursor = 'grab';
925
+ return;
926
+ }
927
+ }
928
+ }
929
+ }
842
930
 
843
- context.textBaseline = 'middle';
844
- var yCoord = sheetStyle.columnHeaderHeight;
931
+ if (onCellWidthChange) {
932
+ for (var _iterator7 = _createForOfIteratorHelperLoose(columns), _step7; !(_step7 = _iterator7()).done;) {
933
+ var _index4 = _step7.value;
934
+ var edge = columnToPixel(_index4, 1);
935
+
936
+ if (Math.abs(edge - x) < SIZES.resizeZone && canSizeColumn(_index4)) {
937
+ window.document.body.style.cursor = 'col-resize';
938
+ return;
939
+ }
940
+ }
941
+ }
942
+ }
943
+
944
+ if (!hideRowHeaders && x < getIndentX()) {
945
+ if (onRowOrderChange) {
946
+ var _start5 = rowToPixel(minY) + SIZES.resizeZone;
947
+
948
+ var _end5 = rowToPixel(maxY, 1) - SIZES.resizeZone;
845
949
 
846
- for (var _iterator8 = _createForOfIteratorHelperLoose(rowSizes.index), _step8; !(_step8 = _iterator8()).done;) {
847
- var _y = _step8.value;
848
- var xCoord = sheetStyle.rowHeaderWidth;
950
+ if (isInRange(y, _start5, _end5)) {
951
+ for (var _iterator8 = _createForOfIteratorHelperLoose(rows), _step8; !(_step8 = _iterator8()).done;) {
952
+ var _index5 = _step8.value;
849
953
 
850
- var _ch = cellHeight(_y);
954
+ var _start6 = rowToPixel(_index5);
851
955
 
852
- for (var _iterator10 = _createForOfIteratorHelperLoose(columnSizes.index), _step10; !(_step10 = _iterator10()).done;) {
853
- var _x = _step10.value;
956
+ var _end6 = rowToPixel(_index5, 1);
854
957
 
855
- var _cellContent = displayData(_x, _y);
958
+ if (!draggingRowSelection && isRowSelection(selection) && isInRange(y, _start6, _end6) && isInRange(_index5, minY, maxY) && canOrderRow(_index5)) {
959
+ window.document.body.style.cursor = 'grab';
960
+ return;
961
+ }
962
+ }
963
+ }
964
+ }
856
965
 
857
- var _cw = cellWidth(_x);
966
+ if (onCellHeightChange) {
967
+ for (var _iterator9 = _createForOfIteratorHelperLoose(rows), _step9; !(_step9 = _iterator9()).done;) {
968
+ var _index6 = _step9.value;
858
969
 
859
- if (_cellContent !== null && _cellContent !== undefined) {
860
- var _style = cellStyle(_x, _y);
970
+ var _edge2 = rowToPixel(_index6, 1);
861
971
 
862
- drawCell(context, _cellContent, _style, defaultCellStyle, xCoord, yCoord, _cw, _ch);
972
+ if (Math.abs(_edge2 - y) < SIZES.resizeZone && canSizeRow(_index6)) {
973
+ window.document.body.style.cursor = 'row-resize';
974
+ return;
975
+ }
976
+ }
977
+ }
863
978
  }
864
979
 
865
- xCoord += _cw;
980
+ if (knobPosition) {
981
+ var knobX = knobPosition[0],
982
+ knobY = knobPosition[1];
983
+
984
+ if (Math.abs(x - knobX) < SIZES.knobArea && Math.abs(y - knobY) < SIZES.knobArea) {
985
+ window.document.body.style.cursor = 'crosshair';
986
+ return;
987
+ }
988
+ }
866
989
  }
867
990
 
868
- yCoord += _ch;
869
- }
870
- }
991
+ if (columnResize) {
992
+ if (onCellWidthChange) {
993
+ var size = columnResize.size,
994
+ anchor = columnResize.anchor,
995
+ scroll = columnResize.scroll,
996
+ indices = columnResize.indices;
871
997
 
872
- function Sheet(props) {
873
- var _props$sheetStyle9, _props$sheetStyle10, _props$sheetStyle11, _props$sheetStyle12, _props$sheetStyle13, _props$sheetStyle14, _props$inputComponent;
998
+ var _getScrollPosition5 = getScrollPosition(e),
999
+ currentScroll = _getScrollPosition5[0];
874
1000
 
875
- var canvasRef = useRef(null);
876
- var overlayRef = useRef(null);
877
- var copyPasteTextAreaRef = useRef(null);
1001
+ var newWidth = Math.max(size + x - anchor + scroll - currentScroll, SIZES.minimumWidth * indices.length);
1002
+ onInvalidateColumn === null || onInvalidateColumn === void 0 ? void 0 : onInvalidateColumn(indices[0] - 1);
1003
+ onCellWidthChange(indices, newWidth / indices.length);
1004
+ }
878
1005
 
879
- var _useState = useState({
880
- x: 5000,
881
- y: 5000
882
- }),
883
- maxScroll = _useState[0],
884
- setMaxScroll = _useState[1];
1006
+ return;
1007
+ }
885
1008
 
886
- var _useState2 = useState({
887
- x: 0,
888
- y: 0
889
- }),
890
- dataOffset = _useState2[0],
891
- setDataOffset = _useState2[1];
1009
+ if (rowResize) {
1010
+ if (onCellHeightChange) {
1011
+ var _size4 = rowResize.size,
1012
+ _anchor = rowResize.anchor,
1013
+ _scroll4 = rowResize.scroll,
1014
+ _indices5 = rowResize.indices;
892
1015
 
893
- var _useState3 = useState({
894
- x1: -1,
895
- y1: -1,
896
- x2: -1,
897
- y2: -1
898
- }),
899
- selection = _useState3[0],
900
- setSelection = _useState3[1];
1016
+ var _getScrollPosition6 = getScrollPosition(e),
1017
+ _currentScroll = _getScrollPosition6[1];
901
1018
 
902
- var _useState4 = useState({
903
- x1: -1,
904
- y1: -1,
905
- x2: -1,
906
- y2: -1
907
- }),
908
- knobArea = _useState4[0],
909
- setKnobArea = _useState4[1];
1019
+ var newHeight = Math.max(_size4 + y - _anchor + _scroll4 - _currentScroll, SIZES.minimumHeight * _indices5.length);
1020
+ onInvalidateRow === null || onInvalidateRow === void 0 ? void 0 : onInvalidateRow(_indices5[0] - 1);
1021
+ onCellHeightChange(_indices5, newHeight / _indices5.length);
1022
+ }
910
1023
 
911
- var _useState5 = useState({
912
- x: -1,
913
- y: -1
914
- }),
915
- editCell = _useState5[0],
916
- setEditCell = _useState5[1];
1024
+ return;
1025
+ }
917
1026
 
918
- var _useState6 = useState(''),
919
- editValue = _useState6[0],
920
- setEditValue = _useState6[1];
1027
+ if (draggingSelection) {
1028
+ var _anchor2 = selection[0];
1029
+ var head = pixelToCell(xy);
1030
+ var anchorX = _anchor2[0],
1031
+ anchorY = _anchor2[1];
1032
+ var headX = head[0],
1033
+ headY = head[1];
921
1034
 
922
- var _useState7 = useState(''),
923
- editKey = _useState7[0],
924
- setEditKey = _useState7[1];
1035
+ if (draggingRowSelection) {
1036
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([[-1, anchorY], [-1, Math.max(0, headY)]], false);
1037
+ } else if (draggingColumnSelection) {
1038
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([[anchorX, -1], [Math.max(0, headX), -1]], false);
1039
+ } else {
1040
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([maxXY(_anchor2, ORIGIN), maxXY(head, ORIGIN)], false);
1041
+ }
1042
+ }
925
1043
 
926
- var _useState8 = useState(false),
927
- arrowKeyCommitMode = _useState8[0],
928
- setArrowKeyCommitMode = _useState8[1];
1044
+ if (draggingKnob) {
1045
+ window.document.body.style.cursor = 'crosshair';
929
1046
 
930
- var _useState9 = useState(false),
931
- shiftKeyDown = _useState9[0],
932
- setShiftKeyDown = _useState9[1];
1047
+ var _pixelToCell = pixelToCell(xy),
1048
+ cellX = _pixelToCell[0],
1049
+ cellY = _pixelToCell[1];
1050
+
1051
+ var _normalizeSelection5 = normalizeSelection(selection),
1052
+ _normalizeSelection5$ = _normalizeSelection5[0],
1053
+ _minX = _normalizeSelection5$[0],
1054
+ _minY = _normalizeSelection5$[1],
1055
+ _normalizeSelection5$2 = _normalizeSelection5[1],
1056
+ _maxX = _normalizeSelection5$2[0],
1057
+ _maxY = _normalizeSelection5$2[1];
1058
+
1059
+ var xCellDiff = Math.min(cellX - _minX, _maxX - cellX, 0);
1060
+ var yCellDiff = Math.min(cellY - _minY, _maxY - cellY, 0);
1061
+
1062
+ if (isMaybeRowSelection(selection) || xCellDiff > yCellDiff) {
1063
+ if (cellY < _minY) {
1064
+ _minY = cellY;
1065
+ } else if (cellY > _maxY) {
1066
+ _maxY = cellY;
1067
+ }
1068
+ } else {
1069
+ if (cellX < _minX) {
1070
+ _minX = cellX;
1071
+ } else if (cellX > _maxX) {
1072
+ _maxX = cellX;
1073
+ }
1074
+ }
933
1075
 
934
- var _useState10 = useState(false),
935
- knobDragInProgress = _useState10[0],
936
- setKnobDragInProgress = _useState10[1];
1076
+ onKnobAreaChange === null || onKnobAreaChange === void 0 ? void 0 : onKnobAreaChange([[_minX, _minY], [_maxX, _maxY]]);
1077
+ }
937
1078
 
938
- var _useState11 = useState(false),
939
- selectionInProgress = _useState11[0],
940
- setSelectionInProgress = _useState11[1];
1079
+ if (columnDrag || rowDrag) {
1080
+ var _x = xy[0],
1081
+ _y = xy[1];
941
1082
 
942
- var _useState12 = useState(null),
943
- columnResize = _useState12[0],
944
- setColumnResize = _useState12[1];
1083
+ if (columnDrag) {
1084
+ var _cellX = pixelToColumn(Math.max(_x, getIndentX()), 0.5);
945
1085
 
946
- var _useState13 = useState(null),
947
- rowResize = _useState13[0],
948
- setRowResize = _useState13[1];
1086
+ var insideSelection = _cellX >= minX && _cellX <= maxX + 1;
1087
+ var _anchor3 = columnDrag.anchor,
1088
+ _scroll5 = columnDrag.scroll;
1089
+ var shift = _x - _anchor3;
949
1090
 
950
- var _useState14 = useState(false),
951
- rowSelectionInProgress = _useState14[0],
952
- setRowSelectionInProgress = _useState14[1];
1091
+ var _getScrollPosition7 = getScrollPosition(e),
1092
+ _currentScroll2 = _getScrollPosition7[0];
953
1093
 
954
- var _useState15 = useState(false),
955
- columnSelectionInProgress = _useState15[0],
956
- setColumnSelectionInProgress = _useState15[1];
1094
+ onDragOffsetChange === null || onDragOffsetChange === void 0 ? void 0 : onDragOffsetChange([shift + _currentScroll2 - _scroll5, 0]);
1095
+ onDropTargetChange === null || onDropTargetChange === void 0 ? void 0 : onDropTargetChange(insideSelection ? null : [[_cellX, -1], [_cellX, -1]]);
1096
+ }
957
1097
 
958
- var _useState16 = useState({
959
- x: -1,
960
- y: -1,
961
- hitTarget: null
962
- }),
963
- buttonClickMouseDownCoordinates = _useState16[0],
964
- setButtonClickMouseDownCoordinates = _useState16[1];
1098
+ if (rowDrag) {
1099
+ var _cellY = pixelToRow(Math.max(_y, getIndentY()), 0.5);
965
1100
 
966
- var _useResizeObserver = useResizeObserver({
967
- ref: canvasRef
968
- }),
969
- _useResizeObserver$wi = _useResizeObserver.width,
970
- canvasWidth = _useResizeObserver$wi === void 0 ? 3000 : _useResizeObserver$wi,
971
- _useResizeObserver$he = _useResizeObserver.height,
972
- canvasHeight = _useResizeObserver$he === void 0 ? 3000 : _useResizeObserver$he;
1101
+ var _insideSelection2 = _cellY >= minY && _cellY <= maxY + 1;
973
1102
 
974
- var cellWidth = useMemo(function () {
975
- return createRowOrColumnPropFunction(props.cellWidth, 100);
976
- }, [props.cellWidth]);
977
- var cellHeight = useMemo(function () {
978
- return createRowOrColumnPropFunction(props.cellHeight, 22);
979
- }, [props.cellHeight]);
980
- var columnHeaders = useMemo(function () {
981
- return createRowOrColumnPropFunction(props.columnHeaders, null);
982
- }, [props.columnHeaders]);
983
- var columnHeaderStyle = useMemo(function () {
984
- return createRowOrColumnPropFunction(props.columnHeaderStyle, {});
985
- }, [props.columnHeaderStyle]);
986
- var cellReadOnly = useMemo(function () {
987
- return createCellPropFunction(props.readOnly, false);
988
- }, [props.readOnly]);
989
- var sourceData = useMemo(function () {
990
- return createCellPropFunction(props.sourceData, null);
991
- }, [props.sourceData]);
992
- var displayData = useMemo(function () {
993
- return createCellPropFunction(props.displayData, '');
994
- }, [props.displayData]);
995
- var editData = useMemo(function () {
996
- return createCellPropFunction(props.editData, '');
997
- }, [props.editData]);
998
- var editKeys = useMemo(function () {
999
- return createCellPropFunction(props.editKeys, '');
1000
- }, [props.editKeys]);
1001
- var cellStyle = useMemo(function () {
1002
- return createCellPropFunction(props.cellStyle, defaultCellStyle);
1003
- }, [props.cellStyle]);
1004
- var sheetStyle = useMemo(function () {
1005
- var _props$sheetStyle, _props$sheetStyle2, _props$sheetStyle3, _props$sheetStyle4, _props$sheetStyle5, _props$sheetStyle6, _props$sheetStyle7, _props$sheetStyle8;
1103
+ var _anchor4 = rowDrag.anchor,
1104
+ _scroll6 = rowDrag.scroll;
1006
1105
 
1007
- return {
1008
- freezeColumns: ((_props$sheetStyle = props.sheetStyle) === null || _props$sheetStyle === void 0 ? void 0 : _props$sheetStyle.freezeColumns) || 0,
1009
- freezeRows: ((_props$sheetStyle2 = props.sheetStyle) === null || _props$sheetStyle2 === void 0 ? void 0 : _props$sheetStyle2.freezeRows) || 0,
1010
- hideColumnHeaders: ((_props$sheetStyle3 = props.sheetStyle) === null || _props$sheetStyle3 === void 0 ? void 0 : _props$sheetStyle3.hideColumnHeaders) || false,
1011
- hideRowHeaders: ((_props$sheetStyle4 = props.sheetStyle) === null || _props$sheetStyle4 === void 0 ? void 0 : _props$sheetStyle4.hideRowHeaders) || false,
1012
- hideGridlines: ((_props$sheetStyle5 = props.sheetStyle) === null || _props$sheetStyle5 === void 0 ? void 0 : _props$sheetStyle5.hideGridlines) || false,
1013
- columnHeaderHeight: (_props$sheetStyle6 = props.sheetStyle) !== null && _props$sheetStyle6 !== void 0 && _props$sheetStyle6.hideColumnHeaders ? 1 : kColumnHeaderHeight,
1014
- rowHeaderWidth: (_props$sheetStyle7 = props.sheetStyle) !== null && _props$sheetStyle7 !== void 0 && _props$sheetStyle7.hideRowHeaders ? 1 : kRowHeaderWidth,
1015
- hideScrollBars: ((_props$sheetStyle8 = props.sheetStyle) === null || _props$sheetStyle8 === void 0 ? void 0 : _props$sheetStyle8.hideScrollBars) || false
1016
- };
1017
- }, [(_props$sheetStyle9 = props.sheetStyle) === null || _props$sheetStyle9 === void 0 ? void 0 : _props$sheetStyle9.freezeColumns, (_props$sheetStyle10 = props.sheetStyle) === null || _props$sheetStyle10 === void 0 ? void 0 : _props$sheetStyle10.freezeRows, (_props$sheetStyle11 = props.sheetStyle) === null || _props$sheetStyle11 === void 0 ? void 0 : _props$sheetStyle11.hideColumnHeaders, (_props$sheetStyle12 = props.sheetStyle) === null || _props$sheetStyle12 === void 0 ? void 0 : _props$sheetStyle12.hideGridlines, (_props$sheetStyle13 = props.sheetStyle) === null || _props$sheetStyle13 === void 0 ? void 0 : _props$sheetStyle13.hideRowHeaders, (_props$sheetStyle14 = props.sheetStyle) === null || _props$sheetStyle14 === void 0 ? void 0 : _props$sheetStyle14.hideScrollBars]);
1018
- useEffect(function () {
1019
- var currEditKey = editKeys ? editKeys(editCell.x, editCell.y) : '';
1106
+ var _shift = _y - _anchor4;
1020
1107
 
1021
- if (currEditKey !== editKey) {
1022
- setEditCell({
1023
- x: -1,
1024
- y: -1
1025
- });
1026
- setEditKey('');
1027
- }
1028
- }, [editKeys]);
1029
- var columnSizes = useMemo(function () {
1030
- return calculateRowsOrColsSizes(sheetStyle.freezeColumns, cellWidth, sheetStyle.rowHeaderWidth, dataOffset.x, canvasWidth);
1031
- }, [sheetStyle, cellWidth, dataOffset.x, canvasWidth]);
1032
- var rowSizes = useMemo(function () {
1033
- return calculateRowsOrColsSizes(sheetStyle.freezeRows, cellHeight, sheetStyle.columnHeaderHeight, dataOffset.y, canvasHeight);
1034
- }, [sheetStyle, cellHeight, dataOffset.y, canvasHeight]);
1035
- useEffect(function () {
1036
- if (props.onScrollChange) {
1037
- props.onScrollChange([].concat(rowSizes.index), [].concat(columnSizes.index));
1108
+ var _getScrollPosition8 = getScrollPosition(e),
1109
+ _currentScroll3 = _getScrollPosition8[1];
1110
+
1111
+ onDragOffsetChange === null || onDragOffsetChange === void 0 ? void 0 : onDragOffsetChange([0, _shift + _currentScroll3 - _scroll6]);
1112
+ onDropTargetChange === null || onDropTargetChange === void 0 ? void 0 : onDropTargetChange(_insideSelection2 ? null : [[-1, _cellY], [-1, _cellY]]);
1113
+ }
1038
1114
  }
1039
- }, [rowSizes, columnSizes, props.onScrollChange]);
1115
+ }, [getMousePosition, getScrollPosition, getMouseHit, onCellWidthChange, onCellHeightChange]);
1116
+ var onDoubleClick = useCallback(function (e) {
1117
+ var pixelToCell = ref.current.cellLayout.pixelToCell;
1118
+ e.preventDefault();
1119
+ if (e.shiftKey) return;
1120
+ var xy = getMousePosition(e);
1121
+ if (!xy) return;
1122
+ var hitTarget = getMouseHit(xy);
1040
1123
 
1041
- var changeSelection = function changeSelection(x1, y1, x2, y2, scrollToP2) {
1042
- if (scrollToP2 === void 0) {
1043
- scrollToP2 = true;
1124
+ if (hitTarget) {
1125
+ window.document.body.style.cursor = 'pointer';
1126
+ return;
1044
1127
  }
1045
1128
 
1046
- if (selection.x1 !== x1 || selection.x2 !== x2 || selection.y1 !== y1 || selection.y2 !== y2) {
1047
- setSelection({
1048
- x1: x1,
1049
- y1: y1,
1050
- x2: x2,
1051
- y2: y2
1052
- });
1129
+ var editCell = pixelToCell(xy);
1130
+ if (editMode) onCommit === null || onCommit === void 0 ? void 0 : onCommit();
1131
+ onEdit === null || onEdit === void 0 ? void 0 : onEdit(editCell);
1132
+ }, [getMousePosition, getMouseHit, onCommit, onEdit]);
1133
+ var onContextMenu = useCallback(function (e) {
1134
+ var _ref$current$cellLayo2 = ref.current.cellLayout,
1135
+ pixelToCell = _ref$current$cellLayo2.pixelToCell,
1136
+ getIndentX = _ref$current$cellLayo2.getIndentX,
1137
+ getIndentY = _ref$current$cellLayo2.getIndentY;
1138
+ var xy = getMousePosition(e);
1139
+ if (!xy) return;
1140
+ var x = xy[0],
1141
+ y = xy[1];
1142
+
1143
+ if (x <= getIndentX() || y <= getIndentY()) {
1144
+ return;
1053
1145
  }
1054
1146
 
1055
- if (scrollToP2) {
1056
- var newDataOffset = {
1057
- x: dataOffset.x,
1058
- y: dataOffset.y
1059
- };
1060
- var newScrollLeft = -1;
1061
- var newScrollTop = -1;
1062
-
1063
- if (x2 !== -1 && (!columnSizes.index.includes(x2) || columnSizes.index[columnSizes.index.length - 1] === x2)) {
1064
- var lastVisibleColumnIndex = columnSizes.index[columnSizes.index.length - 1];
1065
- var firstVisibleColumnIndex = columnSizes.index[sheetStyle.freezeColumns];
1066
- var increment = 0;
1067
-
1068
- if (x2 >= lastVisibleColumnIndex) {
1069
- increment = 1 + x2 - lastVisibleColumnIndex;
1070
- } else if (x2 < firstVisibleColumnIndex) {
1071
- increment = x2 - firstVisibleColumnIndex;
1072
- }
1147
+ var cell = pixelToCell(xy);
1073
1148
 
1074
- var newX = Math.max(dataOffset.x, sheetStyle.freezeColumns) + increment;
1075
- newDataOffset.x = newX;
1076
- newScrollLeft = newX * scrollSpeed;
1077
- }
1149
+ if (!isPointInsideSelection(selection, cell)) {
1150
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([cell, cell]);
1151
+ }
1078
1152
 
1079
- if (y2 !== -1 && (!rowSizes.index.includes(y2) || rowSizes.index[rowSizes.index.length - 1] === y2)) {
1080
- var firstVisibleRowIndex = rowSizes.index[sheetStyle.freezeRows];
1081
- var lastVisibleRowIndex = rowSizes.index[rowSizes.index.length - 1];
1082
- var _increment = 0;
1153
+ onPointerMove(e);
1154
+ var cellX = cell[0],
1155
+ cellY = cell[1];
1083
1156
 
1084
- if (y2 >= lastVisibleRowIndex) {
1085
- _increment = 1 + y2 - lastVisibleRowIndex;
1086
- } else if (y2 < firstVisibleRowIndex) {
1087
- _increment = y2 - firstVisibleRowIndex;
1088
- }
1157
+ var event = _extends({}, e, {
1158
+ cellX: cellX,
1159
+ cellY: cellY
1160
+ });
1089
1161
 
1090
- var newY = Math.max(dataOffset.y, sheetStyle.freezeRows) + _increment;
1162
+ onRightClick === null || onRightClick === void 0 ? void 0 : onRightClick(event);
1163
+ }, [getMousePosition, onSelectionChange, onPointerMove, onRightClick]);
1164
+ var mouseHandlers = {
1165
+ onPointerLeave: onPointerLeave,
1166
+ onPointerDown: onPointerDown,
1167
+ onPointerMove: onPointerMove,
1168
+ onPointerUp: onPointerUp,
1169
+ onDoubleClick: onDoubleClick,
1170
+ onContextMenu: onContextMenu
1171
+ };
1172
+ return {
1173
+ knobPosition: knobPosition,
1174
+ mouseHandlers: mouseHandlers
1175
+ };
1176
+ };
1091
1177
 
1092
- newDataOffset.y = newY;
1093
- newScrollTop = newY * scrollSpeed;
1094
- }
1178
+ var parseKnobOperation = function parseKnobOperation(knobArea, selection, sourceData, editData) {
1179
+ var _normalizeSelection6 = normalizeSelection(knobArea),
1180
+ _normalizeSelection6$ = _normalizeSelection6[0],
1181
+ kx1 = _normalizeSelection6$[0],
1182
+ ky1 = _normalizeSelection6$[1],
1183
+ _normalizeSelection6$2 = _normalizeSelection6[1],
1184
+ kx2 = _normalizeSelection6$2[0],
1185
+ ky2 = _normalizeSelection6$2[1];
1186
+
1187
+ var _normalizeSelection7 = normalizeSelection(selection),
1188
+ _normalizeSelection7$ = _normalizeSelection7[0],
1189
+ sx1 = _normalizeSelection7$[0],
1190
+ sy1 = _normalizeSelection7$[1],
1191
+ _normalizeSelection7$2 = _normalizeSelection7[1],
1192
+ sx2 = _normalizeSelection7$2[0],
1193
+ sy2 = _normalizeSelection7$2[1];
1194
+
1195
+ var fx1 = kx1;
1196
+ var fy1 = ky1;
1197
+ var fx2 = kx2;
1198
+ var fy2 = ky2;
1199
+ var changes = [];
1200
+
1201
+ if (fx2 - fx1 === sx2 - sx1) {
1202
+ if (fy1 === sy1) {
1203
+ fy1 = sy2 + 1;
1204
+ } else {
1205
+ fy2 = sy1 - 1;
1206
+ }
1095
1207
 
1096
- if (newDataOffset.x !== dataOffset.x || dataOffset.y !== newDataOffset.y) {
1097
- setDataOffset({
1098
- x: newDataOffset.x,
1099
- y: newDataOffset.y
1100
- });
1208
+ if (fx1 === -1 && fx2 === -1) {
1209
+ var _findApproxMaxEditDat = findApproxMaxEditDataIndex(editData),
1210
+ maxX = _findApproxMaxEditDat[0];
1101
1211
 
1102
- var newMaxScroll = _extends({}, maxScroll);
1212
+ fx1 = 0;
1213
+ fx2 = maxX;
1214
+ }
1103
1215
 
1104
- newMaxScroll.x = Math.max(newMaxScroll.x, newScrollLeft);
1105
- newMaxScroll.y = Math.max(newMaxScroll.y, newScrollTop);
1106
- setMaxScroll(newMaxScroll);
1107
- setTimeout(function () {
1108
- if (overlayRef.current) {
1109
- if (newScrollLeft !== -1) {
1110
- overlayRef.current.scrollLeft = newScrollLeft;
1111
- }
1216
+ var srcY = sy1;
1112
1217
 
1113
- if (newScrollTop !== -1) {
1114
- overlayRef.current.scrollTop = newScrollTop;
1115
- }
1218
+ for (var y = fy1; y <= fy2; y++) {
1219
+ for (var x = fx1; x <= fx2; x++) {
1220
+ var value = sourceData(x, srcY);
1221
+ changes.push({
1222
+ x: x,
1223
+ y: y,
1224
+ value: value,
1225
+ source: {
1226
+ x: x,
1227
+ y: srcY
1116
1228
  }
1117
- }, 0);
1229
+ });
1118
1230
  }
1119
- }
1120
1231
 
1121
- if (props.onSelectionChanged) {
1122
- var sx1 = x1;
1123
- var sy1 = y1;
1124
- var sx2 = x2;
1125
- var sy2 = y2;
1126
-
1127
- if (sx1 > sx2) {
1128
- sx1 = x2;
1129
- sx2 = x1;
1130
- }
1232
+ srcY = srcY + 1;
1131
1233
 
1132
- if (sy1 > sy2) {
1133
- sy1 = y2;
1134
- sy2 = y1;
1234
+ if (srcY > sy2) {
1235
+ srcY = sy1;
1135
1236
  }
1136
-
1137
- props.onSelectionChanged(sx1, sy1, sx2, sy2);
1138
1237
  }
1139
- };
1140
-
1141
- var knobCoordinates = useMemo(function () {
1142
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1143
- var sely2 = selection.y2;
1238
+ } else {
1239
+ if (fx1 === sx1) {
1240
+ fx1 = sx2 + 1;
1241
+ } else {
1242
+ fx2 = sx1 - 1;
1243
+ }
1144
1244
 
1145
- if (selection.y1 > selection.y2) {
1146
- sely2 = selection.y1;
1147
- }
1245
+ if (fy1 === -1 && fy2 === -1) {
1246
+ var _findApproxMaxEditDat2 = findApproxMaxEditDataIndex(editData),
1247
+ maxY = _findApproxMaxEditDat2[1];
1148
1248
 
1149
- var c = cellToAbsCoordinate(0, sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1150
- return {
1151
- x: c.x + knobSize * 0.5,
1152
- y: c.y + cellHeight(sely2)
1153
- };
1249
+ fy1 = 0;
1250
+ fy2 = maxY;
1154
1251
  }
1155
1252
 
1156
- if (selection.x1 !== -1 && selection.x2 !== -1 && selection.y1 === -1 && selection.y2 === -1) {
1157
- var selx2 = selection.x2;
1158
-
1159
- if (selection.x1 > selection.x2) {
1160
- selx2 = selection.x1;
1161
- }
1253
+ var srcX = sx1;
1162
1254
 
1163
- var _c = cellToAbsCoordinate(selx2, 0, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1255
+ for (var _x2 = fx1; _x2 <= fx2; _x2++) {
1256
+ for (var _y2 = fy1; _y2 <= fy2; _y2++) {
1257
+ var _value = sourceData(srcX, _y2);
1164
1258
 
1165
- return {
1166
- x: _c.x + cellWidth(selx2),
1167
- y: _c.y + knobSize * 0.5
1168
- };
1169
- }
1259
+ changes.push({
1260
+ x: _x2,
1261
+ y: _y2,
1262
+ value: _value,
1263
+ source: {
1264
+ x: srcX,
1265
+ y: _y2
1266
+ }
1267
+ });
1268
+ }
1170
1269
 
1171
- if (selection.x2 !== -1 && selection.y2 !== -1) {
1172
- var _selx3 = selection.x2;
1270
+ srcX = srcX + 1;
1173
1271
 
1174
- if (selection.x1 > selection.x2) {
1175
- _selx3 = selection.x1;
1272
+ if (srcX > sx2) {
1273
+ srcX = sx1;
1176
1274
  }
1275
+ }
1276
+ }
1177
1277
 
1178
- var _sely3 = selection.y2;
1278
+ return changes;
1279
+ };
1179
1280
 
1180
- if (selection.y1 > selection.y2) {
1181
- _sely3 = selection.y1;
1182
- }
1281
+ var useScroll = function useScroll(offset, maxScroll, cellLayout, onOffsetChange, onMaxScrollChange) {
1282
+ return useCallback(function (e) {
1283
+ if (!e.target || !(e.target instanceof Element)) {
1284
+ return;
1285
+ }
1183
1286
 
1184
- var _c2 = cellToAbsCoordinate(_selx3, _sely3, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1287
+ var xy = [e.target.scrollLeft, e.target.scrollTop];
1288
+ var absoluteToCell = cellLayout.absoluteToCell;
1289
+ var cell = absoluteToCell(xy);
1185
1290
 
1186
- return {
1187
- x: _c2.x + cellWidth(_selx3),
1188
- y: _c2.y + cellHeight(_sely3)
1189
- };
1291
+ if (!isSameXY(cell, offset)) {
1292
+ onOffsetChange === null || onOffsetChange === void 0 ? void 0 : onOffsetChange(cell);
1190
1293
  }
1191
1294
 
1192
- return {
1193
- x: -1,
1194
- y: -1
1195
- };
1196
- }, [selection, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle]);
1197
- var hitMap = useMemo(function () {
1198
- var hitM = {};
1199
- var canvas = canvasRef.current;
1295
+ var x = xy[0],
1296
+ y = xy[1];
1297
+ var maxScrollX = maxScroll[0],
1298
+ maxScrollY = maxScroll[1];
1299
+ var growX = maxScrollX < x + 1 ? 1.5 : 1;
1300
+ var growY = maxScrollY < y + 1 ? 1.5 : 1;
1200
1301
 
1201
- if (!canvas) {
1202
- return hitM;
1302
+ if (growX > 1 || growY > 1) {
1303
+ onMaxScrollChange === null || onMaxScrollChange === void 0 ? void 0 : onMaxScrollChange(mulXY(maxScroll, [growX, growY]));
1203
1304
  }
1305
+ }, [cellLayout, onOffsetChange, onMaxScrollChange]);
1306
+ };
1307
+ var scrollToCell = function scrollToCell(element, cell, view, freeze, offset, maxScroll, cellLayout, callback) {
1308
+ var x = cell[0],
1309
+ y = cell[1];
1310
+ var w = view[0],
1311
+ h = view[1];
1312
+ var offsetX = offset[0],
1313
+ offsetY = offset[1];
1314
+ var cellToAbsolute = cellLayout.cellToAbsolute,
1315
+ cellToPixel = cellLayout.cellToPixel,
1316
+ columnToPixel = cellLayout.columnToPixel,
1317
+ rowToPixel = cellLayout.rowToPixel;
1318
+
1319
+ var _cellToAbsolute = cellToAbsolute(freeze),
1320
+ frozenX = _cellToAbsolute[0],
1321
+ frozenY = _cellToAbsolute[1];
1322
+
1323
+ var _cellToPixel = cellToPixel(cell),
1324
+ left = _cellToPixel[0],
1325
+ top = _cellToPixel[1];
1326
+
1327
+ var _cellToPixel2 = cellToPixel(cell, ONE_ONE),
1328
+ right = _cellToPixel2[0],
1329
+ bottom = _cellToPixel2[1];
1330
+
1331
+ var newX = offset[0],
1332
+ newY = offset[1];
1333
+
1334
+ if (left <= frozenX) {
1335
+ newX = x;
1336
+ }
1204
1337
 
1205
- resizeCanvas(canvas);
1206
- var yCoord = sheetStyle.columnHeaderHeight;
1207
- var xCoord = sheetStyle.rowHeaderWidth;
1338
+ if (top <= frozenY) {
1339
+ newY = y;
1340
+ }
1208
1341
 
1209
- for (var _iterator11 = _createForOfIteratorHelperLoose(columnSizes.index), _step11; !(_step11 = _iterator11()).done;) {
1210
- var x = _step11.value;
1211
- var ch = columnHeaders(x);
1212
- var cellW = cellWidth(x);
1342
+ if (right > w) {
1343
+ var edge = right - w + columnToPixel(newX);
1213
1344
 
1214
- if (ch && typeof ch === 'object' && ch.items) {
1215
- var finalStyle = void 0;
1345
+ while (columnToPixel(++newX) < edge) {}
1346
+ }
1216
1347
 
1217
- for (var _iterator13 = _createForOfIteratorHelperLoose(ch.items), _step13; !(_step13 = _iterator13()).done;) {
1218
- var obj = _step13.value;
1348
+ if (bottom > h) {
1349
+ var _edge = bottom - h + rowToPixel(newY);
1219
1350
 
1220
- if (obj.onClick) {
1221
- if (!finalStyle) {
1222
- finalStyle = createStyleObject(columnHeaderStyle(x), defaultCellStyle);
1223
- }
1351
+ while (rowToPixel(++newY) < _edge) {}
1352
+ }
1224
1353
 
1225
- var w = obj.content instanceof HTMLImageElement ? obj.width || cellW : 0;
1226
- var absX1 = applyAlignment(xCoord, cellW, finalStyle, w, obj.horiozntalAlign) + obj.x;
1227
- var absY1 = sheetStyle.columnHeaderHeight * 0.5 + obj.y;
1228
- var absX2 = absX1 + (obj.width || 0);
1229
- var absY2 = absY1 + (obj.height || 0);
1230
- var hitTarget = {
1231
- x: absX1,
1232
- y: absY1,
1233
- w: obj.width,
1234
- h: obj.height,
1235
- onClick: obj.onClick
1236
- };
1237
- var x1key = Math.floor(absX1 / xBinSize);
1238
- var x2key = Math.floor(absX2 / xBinSize);
1239
- var y1key = Math.floor(absY1 / yBinSize);
1240
- var y2key = Math.floor(absY2 / yBinSize);
1241
-
1242
- for (var xkey = x1key; xkey <= x2key; xkey++) {
1243
- if (!hitM[xkey]) {
1244
- hitM[xkey] = {};
1245
- }
1354
+ var newOffset = [newX >= 0 ? newX : offsetX, newY >= 0 ? newY : offsetY];
1246
1355
 
1247
- var xbin = hitM[xkey];
1356
+ if (!isSameXY(newOffset, offset)) {
1357
+ var scroll = cellToAbsolute(newOffset);
1358
+ callback(newOffset, maxXY(maxScroll, scroll));
1359
+ setTimeout(function () {
1360
+ var scrollX = scroll[0],
1361
+ scrollY = scroll[1];
1362
+ element.scrollLeft = scrollX;
1363
+ element.scrollTop = scrollY;
1364
+ });
1365
+ }
1366
+ };
1248
1367
 
1249
- for (var ykey = y1key; ykey <= y2key; ykey++) {
1250
- if (!xbin[ykey]) {
1251
- xbin[ykey] = [];
1252
- }
1368
+ var useClipboardCopy = function useClipboardCopy(textAreaRef, selection, editMode, editData) {
1369
+ useLayoutEffect(function () {
1370
+ var textArea = textAreaRef.current;
1371
+ if (!textArea) return;
1372
+ if (editMode) return;
1373
+ if (isEmptySelection(selection)) return;
1374
+ textArea.value = formatSelectionAsTSV(selection, editData);
1375
+ }, [selection, editMode, editData, textAreaRef]);
1376
+ useLayoutEffect(function () {
1377
+ var textArea = textAreaRef.current;
1378
+ if (!textArea) return;
1379
+
1380
+ var focus = function focus() {
1381
+ textArea.focus({
1382
+ preventScroll: true
1383
+ });
1384
+ textArea.select();
1385
+ };
1253
1386
 
1254
- xbin[ykey].push(hitTarget);
1255
- }
1256
- }
1257
- }
1258
- }
1259
- }
1387
+ if (editMode) return;
1388
+ if (document.activeElement === textArea) return;
1389
+ var activeTagName = document.activeElement.tagName.toLowerCase();
1260
1390
 
1261
- xCoord += cellW;
1391
+ if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1392
+ focus();
1262
1393
  }
1394
+ });
1395
+ };
1396
+ var useClipboardPaste = function useClipboardPaste(textAreaRef, selection, onSelectionChange, onChange) {
1397
+ useEffect(function () {
1398
+ var onPaste = function onPaste(e) {
1399
+ var textArea = textAreaRef.current;
1400
+ if (!textArea) return;
1401
+ if (e.target !== textArea) return;
1402
+ e.preventDefault();
1403
+ var clipboardData = e.clipboardData || window.clipboardData;
1404
+ var types = clipboardData.types;
1405
+ var parsed;
1406
+
1407
+ if (types.includes('text/html')) {
1408
+ var pastedHtml = clipboardData.getData('text/html');
1409
+ parsed = parsePastedHtml(selection, pastedHtml);
1410
+ } else if (types.includes('text/plain')) {
1411
+ var text = clipboardData.getData('text/plain');
1412
+ parsed = parsePastedText(selection, text);
1413
+ }
1414
+
1415
+ if (!parsed) return;
1416
+ var _parsed = parsed,
1417
+ s = _parsed.selection,
1418
+ changes = _parsed.changes;
1419
+ onChange === null || onChange === void 0 ? void 0 : onChange(changes);
1420
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange(s);
1421
+ };
1422
+
1423
+ window.document.addEventListener('paste', onPaste);
1424
+ return function () {
1425
+ window.document.removeEventListener('paste', onPaste);
1426
+ };
1427
+ }, [textAreaRef, selection]);
1428
+ };
1429
+
1430
+ var formatTSV = function formatTSV(rows) {
1431
+ return rows.map(function (row) {
1432
+ return row.join('\t');
1433
+ }).join('\n');
1434
+ };
1263
1435
 
1264
- for (var _iterator12 = _createForOfIteratorHelperLoose(rowSizes.index), _step12; !(_step12 = _iterator12()).done;) {
1265
- var y = _step12.value;
1266
- xCoord = sheetStyle.rowHeaderWidth;
1436
+ var formatSelectionAsTSV = function formatSelectionAsTSV(selection, editData) {
1437
+ if (isEmptySelection(selection)) return '';
1267
1438
 
1268
- for (var _iterator14 = _createForOfIteratorHelperLoose(columnSizes.index), _step14; !(_step14 = _iterator14()).done;) {
1269
- var _x2 = _step14.value;
1270
- var cellContent = displayData(_x2, y);
1439
+ var _normalizeSelection = normalizeSelection(selection),
1440
+ _normalizeSelection$ = _normalizeSelection[0],
1441
+ minX = _normalizeSelection$[0],
1442
+ minY = _normalizeSelection$[1],
1443
+ _normalizeSelection$2 = _normalizeSelection[1],
1444
+ maxX = _normalizeSelection$2[0],
1445
+ maxY = _normalizeSelection$2[1];
1446
+
1447
+ if (isMaybeRowSelection(selection)) {
1448
+ var _findApproxMaxEditDat = findApproxMaxEditDataIndex(editData),
1449
+ cellX = _findApproxMaxEditDat[0];
1450
+
1451
+ minX = 0;
1452
+ maxX = cellX;
1453
+ }
1271
1454
 
1272
- var _cellW = cellWidth(_x2);
1455
+ if (isMaybeColumnSelection(selection)) {
1456
+ var _findApproxMaxEditDat2 = findApproxMaxEditDataIndex(editData),
1457
+ cellY = _findApproxMaxEditDat2[1];
1273
1458
 
1274
- if (cellContent === null || cellContent === undefined) {
1275
- xCoord += _cellW;
1276
- continue;
1277
- }
1459
+ minY = 0;
1460
+ maxY = cellY;
1461
+ }
1278
1462
 
1279
- var xx = xCoord;
1280
- var yy = yCoord + cellHeight(y) * 0.5;
1463
+ var rows = [];
1281
1464
 
1282
- if (typeof cellContent === 'object' && cellContent.items) {
1283
- var _finalStyle = void 0;
1465
+ for (var y = minY; y <= maxY; y++) {
1466
+ var row = [];
1284
1467
 
1285
- for (var _iterator15 = _createForOfIteratorHelperLoose(cellContent.items), _step15; !(_step15 = _iterator15()).done;) {
1286
- var _obj = _step15.value;
1468
+ for (var x = minX; x <= maxX; x++) {
1469
+ var value = editData(x, y);
1287
1470
 
1288
- if (_obj.onClick) {
1289
- if (!_finalStyle) {
1290
- _finalStyle = createStyleObject(cellStyle(_x2, y), defaultCellStyle);
1291
- }
1471
+ if (value !== null && value !== undefined) {
1472
+ row.push(value != null ? value : '');
1473
+ }
1474
+ }
1475
+
1476
+ rows.push(row);
1477
+ }
1292
1478
 
1293
- var _w = _obj.content instanceof HTMLImageElement ? _obj.width || _cellW : 0;
1479
+ return formatTSV(rows);
1480
+ };
1481
+
1482
+ var findTable = function findTable(element) {
1483
+ for (var _iterator = _createForOfIteratorHelperLoose(element.children), _step; !(_step = _iterator()).done;) {
1484
+ var child = _step.value;
1485
+
1486
+ if (child.nodeName === 'TABLE') {
1487
+ return child;
1488
+ }
1294
1489
 
1295
- var _absX = applyAlignment(xx, _cellW, _finalStyle, _w, _obj.horiozntalAlign) + _obj.x;
1490
+ var maybeTable = findTable(child);
1296
1491
 
1297
- var _absY = yy + _obj.y;
1492
+ if (maybeTable) {
1493
+ return maybeTable;
1494
+ }
1495
+ }
1496
+ };
1298
1497
 
1299
- var _absX2 = _absX + (_obj.width || 0);
1498
+ var parsePastedHtml = function parsePastedHtml(selection, html) {
1499
+ var div = document.createElement('div');
1500
+ div.innerHTML = html.trim();
1300
1501
 
1301
- var _absY2 = _absY + (_obj.height || 0);
1502
+ var _normalizeSelection2 = normalizeSelection(selection),
1503
+ _normalizeSelection2$ = _normalizeSelection2[0],
1504
+ minX = _normalizeSelection2$[0],
1505
+ minY = _normalizeSelection2$[1];
1302
1506
 
1303
- var _hitTarget = {
1304
- x: _absX,
1305
- y: _absY,
1306
- w: _obj.width,
1307
- h: _obj.height,
1308
- onClick: _obj.onClick
1309
- };
1507
+ var left = isMaybeRowSelection(selection) ? 0 : minX;
1508
+ var top = isMaybeColumnSelection(selection) ? 0 : minY;
1509
+ var changes = [];
1510
+ var tableNode = findTable(div);
1310
1511
 
1311
- var _x1key = Math.floor(_absX / xBinSize);
1512
+ if (!tableNode) {
1513
+ return null;
1514
+ }
1312
1515
 
1313
- var _x2key = Math.floor(_absX2 / xBinSize);
1516
+ var right = left;
1517
+ var bottom = top;
1518
+ var y = top;
1314
1519
 
1315
- var _y1key = Math.floor(_absY / yBinSize);
1520
+ for (var _iterator2 = _createForOfIteratorHelperLoose(tableNode.children), _step2; !(_step2 = _iterator2()).done;) {
1521
+ var tableChild = _step2.value;
1316
1522
 
1317
- var _y2key = Math.floor(_absY2 / yBinSize);
1523
+ if (tableChild.nodeName === 'TBODY') {
1524
+ for (var _iterator3 = _createForOfIteratorHelperLoose(tableChild.children), _step3; !(_step3 = _iterator3()).done;) {
1525
+ var tr = _step3.value;
1526
+ var x = left;
1318
1527
 
1319
- for (var _xkey = _x1key; _xkey <= _x2key; _xkey++) {
1320
- if (!hitM[_xkey]) {
1321
- hitM[_xkey] = {};
1322
- }
1528
+ if (tr.nodeName === 'TR') {
1529
+ for (var _iterator4 = _createForOfIteratorHelperLoose(tr.children), _step4; !(_step4 = _iterator4()).done;) {
1530
+ var td = _step4.value;
1323
1531
 
1324
- var _xbin = hitM[_xkey];
1532
+ if (td.nodeName === 'TD') {
1533
+ var str = '';
1325
1534
 
1326
- for (var _ykey = _y1key; _ykey <= _y2key; _ykey++) {
1327
- if (!_xbin[_ykey]) {
1328
- _xbin[_ykey] = [];
1329
- }
1535
+ if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1536
+ var p = td.children[0];
1330
1537
 
1331
- _xbin[_ykey].push(_hitTarget);
1538
+ if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1539
+ str = p.children[0].textContent.trim();
1540
+ } else {
1541
+ str = p.textContent.trim();
1332
1542
  }
1543
+ } else {
1544
+ str = td.textContent.trim();
1333
1545
  }
1546
+
1547
+ str = str.replaceAll('\n', '');
1548
+ str = str.replaceAll(/\s\s+/g, ' ');
1549
+ changes.push({
1550
+ x: x,
1551
+ y: y,
1552
+ value: str
1553
+ });
1554
+ x++;
1334
1555
  }
1335
1556
  }
1557
+
1558
+ y++;
1336
1559
  }
1337
1560
 
1338
- xCoord += _cellW;
1561
+ right = Math.max(right, x);
1339
1562
  }
1340
-
1341
- yCoord += cellHeight(y);
1342
1563
  }
1564
+ }
1343
1565
 
1344
- return hitM;
1345
- }, [displayData, props.cellWidth, props.cellHeight, canvasRef, columnSizes, rowSizes, dataOffset.x, dataOffset.y, sheetStyle]);
1346
- useEffect(function () {
1347
- var canvas = canvasRef.current;
1566
+ bottom = y;
1567
+ return {
1568
+ selection: [[left, top], [right, bottom]],
1569
+ changes: changes
1570
+ };
1571
+ };
1348
1572
 
1349
- if (!canvas) {
1350
- return;
1573
+ var parsePastedText = function parsePastedText(selection, text) {
1574
+ var _normalizeSelection3 = normalizeSelection(selection),
1575
+ _normalizeSelection3$ = _normalizeSelection3[0],
1576
+ minX = _normalizeSelection3$[0],
1577
+ minY = _normalizeSelection3$[1];
1578
+
1579
+ var left = isMaybeRowSelection(selection) ? 0 : minX;
1580
+ var top = isMaybeColumnSelection(selection) ? 0 : minY;
1581
+ var rows = text.split(/\r?\n/);
1582
+ var right = left;
1583
+ var bottom = top + rows.length - 1;
1584
+ var changes = [];
1585
+
1586
+ for (var y = 0; y < rows.length; y++) {
1587
+ var cols = rows[y].split('\t');
1588
+ right = Math.max(right, left + cols.length - 1);
1589
+
1590
+ for (var x = 0; x < cols.length; x++) {
1591
+ changes.push({
1592
+ x: left + x,
1593
+ y: top + y,
1594
+ value: cols[x]
1595
+ });
1351
1596
  }
1597
+ }
1352
1598
 
1353
- var context = canvas.getContext('2d');
1599
+ return {
1600
+ selection: [[left, top], [right, bottom]],
1601
+ changes: changes
1602
+ };
1603
+ };
1354
1604
 
1355
- if (!context) {
1356
- return;
1357
- }
1605
+ var INITIAL_SIZE = 256;
1606
+ var makeCellLayout = function makeCellLayout(freeze, indent, offset, columns, rows) {
1607
+ var freezeX = freeze[0],
1608
+ freezeY = freeze[1];
1609
+ var indentX = indent[0],
1610
+ indentY = indent[1];
1611
+ var offsetX = offset[0],
1612
+ offsetY = offset[1];
1613
+
1614
+ var getIndentX = function getIndentX() {
1615
+ return indentX;
1616
+ };
1358
1617
 
1359
- var animationFrameId = window.requestAnimationFrame(function () {
1360
- renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections || [], knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle);
1361
- });
1362
- return function () {
1363
- window.cancelAnimationFrame(animationFrameId);
1364
- };
1365
- }, [canvasRef, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle]);
1618
+ var getIndentY = function getIndentY() {
1619
+ return indentY;
1620
+ };
1366
1621
 
1367
- var setFocusToTextArea = function setFocusToTextArea() {
1368
- if (copyPasteTextAreaRef.current) {
1369
- copyPasteTextAreaRef.current.focus({
1370
- preventScroll: true
1371
- });
1372
- copyPasteTextAreaRef.current.select();
1373
- }
1622
+ var getBaseOriginFor = function getBaseOriginFor(index, freeze, offset) {
1623
+ return index < freeze ? 0 : offset;
1374
1624
  };
1375
1625
 
1376
- useEffect(function () {
1377
- if (!editMode) {
1378
- setCopyPasteText();
1626
+ var columnToPixel = function columnToPixel(column, anchor) {
1627
+ if (anchor === void 0) {
1628
+ anchor = 0;
1379
1629
  }
1380
- }, [selection, editData]);
1381
- useEffect(function () {
1382
- if (!editMode) {
1383
- if (document.activeElement === copyPasteTextAreaRef.current) {
1384
- setFocusToTextArea();
1385
- } else {
1386
- var activeTagName = document.activeElement.tagName.toLowerCase();
1387
1630
 
1388
- if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1389
- setFocusToTextArea();
1390
- }
1391
- }
1392
- }
1393
- });
1631
+ var base = getBaseOriginFor(column, freezeX, offsetX);
1632
+ var relative = columns.getStart(column) - columns.getStart(base);
1633
+ var size = column < 0 ? indentX : columns.getSize(column);
1634
+ return column < 0 ? 0 : indentX + relative + anchor * size;
1635
+ };
1394
1636
 
1395
- var onPaste = function onPaste(e) {
1396
- if (!copyPasteTextAreaRef) {
1397
- return;
1637
+ var rowToPixel = function rowToPixel(row, anchor) {
1638
+ if (anchor === void 0) {
1639
+ anchor = 0;
1398
1640
  }
1399
1641
 
1400
- if (e.target !== copyPasteTextAreaRef.current) {
1401
- return;
1642
+ var base = getBaseOriginFor(row, freezeY, offsetY);
1643
+ var relative = rows.getStart(row) - rows.getStart(base);
1644
+ var size = row < 0 ? indentY : rows.getSize(row);
1645
+ return row < 0 ? 0 : indentY + relative + anchor * size;
1646
+ };
1647
+
1648
+ var cellToPixel = function cellToPixel(cell, anchor) {
1649
+ if (anchor === void 0) {
1650
+ anchor = ORIGIN;
1402
1651
  }
1403
1652
 
1404
- e.preventDefault();
1405
- var clipboardData = e.clipboardData || window.clipboardData;
1406
- var types = clipboardData.types;
1653
+ var cellX = cell[0],
1654
+ cellY = cell[1];
1655
+ var _anchor = anchor,
1656
+ anchorX = _anchor[0],
1657
+ anchorY = _anchor[1];
1658
+ return [columnToPixel(cellX, anchorX), rowToPixel(cellY, anchorY)];
1659
+ };
1407
1660
 
1408
- if (types.includes('text/html')) {
1409
- var pastedHtml = clipboardData.getData('text/html');
1410
- parsePastedHtml(pastedHtml);
1411
- } else if (types.includes('text/plain')) {
1412
- var text = clipboardData.getData('text/plain');
1413
- parsePastedText(text);
1661
+ var columnToAbsolute = function columnToAbsolute(column, anchorX) {
1662
+ if (anchorX === void 0) {
1663
+ anchorX = 0;
1414
1664
  }
1665
+
1666
+ var relative = columns.getStart(column);
1667
+ var size = column < 0 ? 0 : columns.getSize(column);
1668
+ return relative + anchorX * size;
1415
1669
  };
1416
1670
 
1417
- useEffect(function () {
1418
- window.document.addEventListener('paste', onPaste);
1419
- return function () {
1420
- window.document.removeEventListener('paste', onPaste);
1421
- };
1422
- });
1671
+ var rowToAbsolute = function rowToAbsolute(row, anchorY) {
1672
+ if (anchorY === void 0) {
1673
+ anchorY = 0;
1674
+ }
1423
1675
 
1424
- var findTable = function findTable(element) {
1425
- for (var _iterator16 = _createForOfIteratorHelperLoose(element.children), _step16; !(_step16 = _iterator16()).done;) {
1426
- var child = _step16.value;
1676
+ var relative = rows.getStart(row);
1677
+ var size = row < 0 ? 0 : rows.getSize(row);
1678
+ return relative + anchorY * size;
1679
+ };
1427
1680
 
1428
- if (child.nodeName === 'TABLE') {
1429
- return child;
1430
- }
1681
+ var cellToAbsolute = function cellToAbsolute(cell, anchor) {
1682
+ if (anchor === void 0) {
1683
+ anchor = ORIGIN;
1684
+ }
1431
1685
 
1432
- var maybeTable = findTable(child);
1686
+ var cellX = cell[0],
1687
+ cellY = cell[1];
1688
+ var _anchor2 = anchor,
1689
+ anchorX = _anchor2[0],
1690
+ anchorY = _anchor2[1];
1691
+ return [columnToAbsolute(cellX, anchorX), rowToAbsolute(cellY, anchorY)];
1692
+ };
1433
1693
 
1434
- if (maybeTable) {
1435
- return maybeTable;
1436
- }
1694
+ var pixelToIndex = function pixelToIndex(pixel, anchor, indent, freeze, offset, layout) {
1695
+ var relative = pixel - indent;
1696
+ if (relative < 0) return -1;
1697
+ var getStart = layout.getStart,
1698
+ lookupIndex = layout.lookupIndex;
1699
+ var frozen = getStart(freeze);
1700
+
1701
+ if (relative < frozen) {
1702
+ return lookupIndex(relative, anchor);
1703
+ } else {
1704
+ var base = getStart(offset);
1705
+ return lookupIndex(base + relative, anchor);
1437
1706
  }
1438
1707
  };
1439
1708
 
1440
- var parsePastedHtml = function parsePastedHtml(html) {
1441
- var div = document.createElement('div');
1442
- div.innerHTML = html.trim();
1443
- var pasteLocX = -1;
1444
- var pasteLocY = -1;
1445
-
1446
- if (selection.x1 !== -1 && selection.x2 === -1) {
1447
- pasteLocX = selection.x1;
1709
+ var pixelToColumn = function pixelToColumn(pixelX, anchorX) {
1710
+ if (anchorX === void 0) {
1711
+ anchorX = 0;
1448
1712
  }
1449
1713
 
1450
- if (selection.y1 !== -1 && selection.y2 === -1) {
1451
- pasteLocY = selection.y1;
1714
+ return pixelToIndex(pixelX, anchorX, indentX, freezeX, offsetX, columns);
1715
+ };
1716
+
1717
+ var pixelToRow = function pixelToRow(pixelY, anchorY) {
1718
+ if (anchorY === void 0) {
1719
+ anchorY = 0;
1452
1720
  }
1453
1721
 
1454
- if (selection.x1 !== -1 && selection.x2 !== -1) {
1455
- pasteLocX = Math.min(selection.x1, selection.x2);
1722
+ return pixelToIndex(pixelY, anchorY, indentY, freezeY, offsetY, rows);
1723
+ };
1724
+
1725
+ var pixelToCell = function pixelToCell(pixel, anchor) {
1726
+ if (anchor === void 0) {
1727
+ anchor = ORIGIN;
1456
1728
  }
1457
1729
 
1458
- if (selection.y1 !== -1 && selection.y2 !== -1) {
1459
- pasteLocY = Math.min(selection.y1, selection.y2);
1730
+ var pixelX = pixel[0],
1731
+ pixelY = pixel[1];
1732
+ var _anchor3 = anchor,
1733
+ anchorX = _anchor3[0],
1734
+ anchorY = _anchor3[1];
1735
+ return [pixelToColumn(pixelX, anchorX), pixelToRow(pixelY, anchorY)];
1736
+ };
1737
+
1738
+ var absoluteToIndex = function absoluteToIndex(pixel, anchor, layout) {
1739
+ if (pixel < 0) return -1;
1740
+ var lookupIndex = layout.lookupIndex;
1741
+ return lookupIndex(pixel, anchor);
1742
+ };
1743
+
1744
+ var absoluteToColumn = function absoluteToColumn(pixelX, anchorX) {
1745
+ if (anchorX === void 0) {
1746
+ anchorX = 0;
1460
1747
  }
1461
1748
 
1462
- if (pasteLocX === -1 || pasteLocY === -1) {
1463
- return;
1749
+ return absoluteToIndex(pixelX, anchorX, columns);
1750
+ };
1751
+
1752
+ var absoluteToRow = function absoluteToRow(pixelY, anchorY) {
1753
+ if (anchorY === void 0) {
1754
+ anchorY = 0;
1464
1755
  }
1465
1756
 
1466
- var x = pasteLocX;
1467
- var y = pasteLocY;
1468
- var changes = [];
1469
- var tableNode = findTable(div);
1757
+ return absoluteToIndex(pixelY, anchorY, rows);
1758
+ };
1470
1759
 
1471
- if (!tableNode) {
1472
- return;
1760
+ var absoluteToCell = function absoluteToCell(pixel, anchor) {
1761
+ if (anchor === void 0) {
1762
+ anchor = ORIGIN;
1473
1763
  }
1474
1764
 
1475
- for (var _iterator17 = _createForOfIteratorHelperLoose(tableNode.children), _step17; !(_step17 = _iterator17()).done;) {
1476
- var tableChild = _step17.value;
1765
+ var pixelX = pixel[0],
1766
+ pixelY = pixel[1];
1767
+ var _anchor4 = anchor,
1768
+ anchorX = _anchor4[0],
1769
+ anchorY = _anchor4[1];
1770
+ return [absoluteToColumn(pixelX, anchorX), absoluteToRow(pixelY, anchorY)];
1771
+ };
1477
1772
 
1478
- if (tableChild.nodeName === 'TBODY') {
1479
- for (var _iterator18 = _createForOfIteratorHelperLoose(tableChild.children), _step18; !(_step18 = _iterator18()).done;) {
1480
- var tr = _step18.value;
1481
- x = pasteLocX;
1773
+ var getVisibleIndices = function getVisibleIndices(view, indent, freeze, offset, layout) {
1774
+ var indices = [].concat(seq(freeze));
1775
+ var getStart = layout.getStart;
1776
+ var relative = view - indent + getStart(offset);
1482
1777
 
1483
- if (tr.nodeName === 'TR') {
1484
- for (var _iterator19 = _createForOfIteratorHelperLoose(tr.children), _step19; !(_step19 = _iterator19()).done;) {
1485
- var td = _step19.value;
1778
+ for (var i = offset + freeze; getStart(i) <= relative; ++i) {
1779
+ indices.push(i);
1780
+ }
1486
1781
 
1487
- if (td.nodeName === 'TD') {
1488
- var str = '';
1782
+ return indices;
1783
+ };
1489
1784
 
1490
- if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1491
- var p = td.children[0];
1785
+ var getVisibleCells = function getVisibleCells(view) {
1786
+ var viewX = view[0],
1787
+ viewY = view[1];
1788
+ return {
1789
+ columns: getVisibleIndices(viewX, indentX, freezeX, offsetX, columns),
1790
+ rows: getVisibleIndices(viewY, indentY, freezeY, offsetY, rows)
1791
+ };
1792
+ };
1492
1793
 
1493
- if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1494
- str = p.children[0].textContent.trim();
1495
- } else {
1496
- str = p.textContent.trim();
1497
- }
1498
- } else {
1499
- str = td.textContent.trim();
1500
- }
1794
+ var getVersion = function getVersion() {
1795
+ return columns.getVersion() + rows.getVersion();
1796
+ };
1501
1797
 
1502
- str = str.replaceAll('\n', '');
1503
- str = str.replaceAll(/\s\s+/g, ' ');
1504
- changes.push({
1505
- y: y,
1506
- x: x,
1507
- value: str
1508
- });
1509
- x++;
1510
- }
1511
- }
1798
+ return {
1799
+ columnToPixel: columnToPixel,
1800
+ rowToPixel: rowToPixel,
1801
+ cellToPixel: cellToPixel,
1802
+ columnToAbsolute: columnToAbsolute,
1803
+ rowToAbsolute: rowToAbsolute,
1804
+ cellToAbsolute: cellToAbsolute,
1805
+ pixelToColumn: pixelToColumn,
1806
+ pixelToRow: pixelToRow,
1807
+ pixelToCell: pixelToCell,
1808
+ absoluteToColumn: absoluteToColumn,
1809
+ absoluteToRow: absoluteToRow,
1810
+ absoluteToCell: absoluteToCell,
1811
+ getVisibleCells: getVisibleCells,
1812
+ getIndentX: getIndentX,
1813
+ getIndentY: getIndentY,
1814
+ getVersion: getVersion
1815
+ };
1816
+ };
1817
+ var makeLayoutCache = function makeLayoutCache(sizer) {
1818
+ var offsets = makeIntMap(INITIAL_SIZE);
1819
+ var sizes = makeIntMap(INITIAL_SIZE);
1820
+ var version = 0;
1821
+ offsets.set(0, 0);
1822
+
1823
+ var getSize = function getSize(i) {
1824
+ if (i < 0) return 0;
1825
+ if (sizes.has(i)) return sizes.get(i);
1826
+ var size = sizer(i) || 0;
1827
+ sizes.set(i, size);
1828
+ return size;
1829
+ };
1512
1830
 
1513
- y++;
1514
- }
1515
- }
1516
- }
1517
- }
1831
+ var getOffset = function getOffset(i) {
1832
+ if (i < 0) return 0;
1833
+ if (offsets.has(i)) return offsets.get(i);
1834
+ var j = offsets.tail() || 0;
1518
1835
 
1519
- if (props.onChange) {
1520
- props.onChange(changes);
1836
+ while (j < i) {
1837
+ var size = getSize(j);
1838
+ var offset = (offsets.get(j) || 0) + size;
1839
+ offsets.set(++j, offset);
1521
1840
  }
1522
1841
 
1523
- var pasteX2 = x - 1;
1524
- var pasteY2 = y - 1;
1525
- changeSelection(pasteLocX, pasteLocY, pasteX2, pasteY2, false);
1842
+ return offsets.get(i);
1526
1843
  };
1527
1844
 
1528
- var parsePastedText = function parsePastedText(text) {
1529
- var pasteLocX = -1;
1530
- var pasteLocY = -1;
1845
+ var getStart = function getStart(i) {
1846
+ return getOffset(i);
1847
+ };
1531
1848
 
1532
- if (selection.x1 !== -1 && selection.x2 === -1) {
1533
- pasteLocX = selection.x1;
1534
- }
1849
+ var getEnd = function getEnd(i) {
1850
+ return getOffset(i + 1);
1851
+ };
1535
1852
 
1536
- if (selection.x1 !== -1 && selection.x2 !== -1) {
1537
- pasteLocX = Math.min(selection.x1, selection.x2);
1853
+ var lookupIndex = function lookupIndex(x, anchor) {
1854
+ if (anchor === void 0) {
1855
+ anchor = 0;
1538
1856
  }
1539
1857
 
1540
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1541
- pasteLocX = 0;
1542
- }
1858
+ var last = offsets.tail() || 0;
1543
1859
 
1544
- if (selection.y1 !== -1 && selection.y2 === -1) {
1545
- pasteLocY = selection.y1;
1860
+ while (getOffset(last) < x && getSize(last)) {
1861
+ last += 64;
1546
1862
  }
1547
1863
 
1548
- if (selection.y1 !== -1 && selection.y2 !== -1) {
1549
- pasteLocY = Math.min(selection.y1, selection.y2);
1550
- }
1864
+ var start = 0;
1865
+ var end = last;
1551
1866
 
1552
- if (selection.y1 === -1 && selection.y2 === -1 && selection.x2 !== -1 && selection.x2 !== -1) {
1553
- pasteLocY = 0;
1867
+ while (start < end) {
1868
+ var mid = start + Math.floor((end - start) / 2) + 1;
1869
+ var value = getOffset(mid) - (anchor ? anchor * getSize(mid - 1) : 0);
1870
+ if (value <= x) start = mid;else end = mid - 1;
1554
1871
  }
1555
1872
 
1556
- if (pasteLocX === -1 || pasteLocY === -1) {
1557
- return;
1558
- }
1873
+ return start;
1874
+ };
1559
1875
 
1560
- var rows = text.split(/\r?\n/);
1561
- var pasteX2 = pasteLocX;
1562
- var pasteY2 = pasteLocY + rows.length - 1;
1563
- var changes = [];
1876
+ var clearAfter = function clearAfter(index) {
1877
+ index = Math.max(0, index);
1878
+ offsets.truncate(index);
1879
+ sizes.truncate(index);
1880
+ version++;
1881
+ };
1564
1882
 
1565
- for (var y = 0; y < rows.length; y++) {
1566
- var cols = rows[y].split('\t');
1883
+ var setSizer = function setSizer(s) {
1884
+ sizer = s;
1885
+ };
1567
1886
 
1568
- if (pasteLocX + cols.length - 1 > pasteX2) {
1569
- pasteX2 = pasteLocX + cols.length - 1;
1570
- }
1887
+ var getVersion = function getVersion() {
1888
+ return version;
1889
+ };
1571
1890
 
1572
- for (var x = 0; x < cols.length; x++) {
1573
- changes.push({
1574
- y: pasteLocY + y,
1575
- x: pasteLocX + x,
1576
- value: cols[x]
1577
- });
1578
- }
1579
- }
1891
+ return {
1892
+ getSize: getSize,
1893
+ getStart: getStart,
1894
+ getEnd: getEnd,
1895
+ getVersion: getVersion,
1896
+ lookupIndex: lookupIndex,
1897
+ setSizer: setSizer,
1898
+ clearAfter: clearAfter
1899
+ };
1900
+ };
1580
1901
 
1581
- if (props.onChange) {
1582
- props.onChange(changes);
1583
- }
1902
+ var makeIntMap = function makeIntMap(initialSize) {
1903
+ if (initialSize === void 0) {
1904
+ initialSize = 128;
1905
+ }
1584
1906
 
1585
- changeSelection(pasteLocX, pasteLocY, pasteX2, pasteY2, false);
1907
+ var used;
1908
+ var values;
1909
+ var last = 0;
1910
+ var GROW = 1.2;
1911
+
1912
+ var allocate = function allocate(size) {
1913
+ var newUsed = new Uint8Array(size);
1914
+ var newValues = new Uint32Array(size);
1915
+ if (used) copy(used, newUsed);
1916
+ if (values) copy(values, newValues);
1917
+ used = newUsed;
1918
+ values = newValues;
1586
1919
  };
1587
1920
 
1588
- var setCopyPasteText = function setCopyPasteText() {
1589
- if (selection.x1 === -1 && selection.y1 === -1 && selection.x2 === -1 && selection.y2 === -1) {
1590
- return;
1591
- }
1921
+ allocate(initialSize);
1592
1922
 
1593
- var dy1 = selection.y1;
1594
- var dy2 = selection.y2;
1923
+ var copy = function copy(from, to) {
1924
+ var n = Math.min(from.length, to.length);
1595
1925
 
1596
- if (dy1 > dy2) {
1597
- dy1 = selection.y2;
1598
- dy2 = selection.y1;
1926
+ for (var i = 0; i < n; ++i) {
1927
+ to[i] = from[i];
1599
1928
  }
1929
+ };
1600
1930
 
1601
- var dx1 = selection.x1;
1602
- var dx2 = selection.x2;
1931
+ var ensure = function ensure(size) {
1932
+ var l = values.length;
1933
+ var grow = Math.round(l * GROW);
1934
+ if (l < size) allocate(Math.max(grow, size));
1935
+ };
1603
1936
 
1604
- if (dx1 > dx2) {
1605
- dx1 = selection.x2;
1606
- dx2 = selection.x1;
1607
- }
1937
+ var truncate = function truncate(size) {
1938
+ var l = values.length;
1939
+ if (l < size) return;
1940
+ var shrink = Math.round(size * GROW);
1941
+ if (l > shrink) allocate(size);else for (var i = size; i < l; ++i) {
1942
+ used[i] = 0;
1943
+ }
1944
+ last = Math.min(last, size);
1608
1945
 
1609
- if (dx1 === -1 && dx2 === -1) {
1610
- var max = findApproxMaxEditDataIndex(editData);
1611
- dx1 = 0;
1612
- dx2 = max.x;
1946
+ while (last > 0 && !used[last]) {
1947
+ last--;
1613
1948
  }
1949
+ };
1614
1950
 
1615
- if (dy1 === -1 && dy2 === -1) {
1616
- var _max = findApproxMaxEditDataIndex(editData);
1951
+ var getTail = function getTail() {
1952
+ return used[last] ? last : null;
1953
+ };
1617
1954
 
1618
- dy1 = 0;
1619
- dy2 = _max.y;
1620
- }
1955
+ var setValue = function setValue(i, value) {
1956
+ ensure(i + 1);
1957
+ values[i] = value;
1958
+ used[i] = 1;
1959
+ last = Math.max(last, i);
1960
+ };
1621
1961
 
1622
- var cptext = '';
1623
- var cptextTemp = '';
1962
+ var getValue = function getValue(i) {
1963
+ return used[i] ? values[i] : null;
1964
+ };
1624
1965
 
1625
- for (var y = dy1; y <= dy2; y++) {
1626
- var nonEmpty = false;
1966
+ var hasValue = function hasValue(i) {
1967
+ return !!used[i];
1968
+ };
1627
1969
 
1628
- for (var x = dx1; x <= dx2; x++) {
1629
- var value = editData(x, y);
1970
+ return {
1971
+ truncate: truncate,
1972
+ set: setValue,
1973
+ get: getValue,
1974
+ has: hasValue,
1975
+ tail: getTail
1976
+ };
1977
+ };
1630
1978
 
1631
- if (value !== null && value !== undefined) {
1632
- cptextTemp += value;
1633
- nonEmpty = true;
1634
- }
1979
+ var resolveSheetStyle = function resolveSheetStyle(sheetStyle) {
1980
+ return {
1981
+ freezeColumns: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeColumns) || 0,
1982
+ freezeRows: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.freezeRows) || 0,
1983
+ hideColumnHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideColumnHeaders) || false,
1984
+ hideRowHeaders: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideRowHeaders) || false,
1985
+ hideGridlines: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideGridlines) || false,
1986
+ hideScrollBars: (sheetStyle === null || sheetStyle === void 0 ? void 0 : sheetStyle.hideScrollBars) || false,
1987
+ columnHeaderHeight: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideColumnHeaders ? 1 : SIZES.headerHeight,
1988
+ rowHeaderWidth: sheetStyle !== null && sheetStyle !== void 0 && sheetStyle.hideRowHeaders ? 1 : SIZES.headerWidth
1989
+ };
1990
+ };
1991
+ var resolveCellStyle = function resolveCellStyle(optionalStyle, defaultStyle) {
1992
+ return _extends({}, defaultStyle, optionalStyle);
1993
+ };
1994
+ var applyAlignment = function applyAlignment(start, cellSize, style, imageWidth, alignment) {
1995
+ if (alignment === void 0) {
1996
+ alignment = style.textAlign;
1997
+ }
1635
1998
 
1636
- if (x !== dx2) {
1637
- cptextTemp += '\t';
1638
- }
1639
- }
1999
+ if (alignment === 'left') {
2000
+ return start + style.marginLeft;
2001
+ } else if (alignment === 'center') {
2002
+ return start + cellSize * 0.5 - imageWidth / 2;
2003
+ } else if (alignment === 'right') {
2004
+ return start + (cellSize - style.marginRight - imageWidth);
2005
+ }
1640
2006
 
1641
- if (y !== dy2) {
1642
- cptextTemp += '\n';
1643
- }
2007
+ return start;
2008
+ };
2009
+
2010
+ var renderSheet = function renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, dataOffset) {
2011
+ var canvas = context.canvas;
2012
+ var width = canvas.width,
2013
+ height = canvas.height;
2014
+ var hideGridlines = sheetStyle.hideGridlines,
2015
+ hideRowHeaders = sheetStyle.hideRowHeaders,
2016
+ hideColumnHeaders = sheetStyle.hideColumnHeaders,
2017
+ rowHeaderWidth = sheetStyle.rowHeaderWidth,
2018
+ columnHeaderHeight = sheetStyle.columnHeaderHeight,
2019
+ freezeColumns = sheetStyle.freezeColumns,
2020
+ freezeRows = sheetStyle.freezeRows;
2021
+ var columns = visibleCells.columns,
2022
+ rows = visibleCells.rows;
2023
+ var columnToPixel = cellLayout.columnToPixel,
2024
+ rowToPixel = cellLayout.rowToPixel;
2025
+ var clickables = [];
2026
+ var freeze = [freezeColumns, freezeRows];
2027
+ var indent = [rowHeaderWidth, columnHeaderHeight];
2028
+ resizeCanvas(canvas);
2029
+ context.clearRect(0, 0, width, height);
2030
+ context.fillStyle = 'white';
2031
+ context.fillRect(0, 0, width, height);
2032
+
2033
+ for (var _iterator = _createForOfIteratorHelperLoose(rows), _step; !(_step = _iterator()).done;) {
2034
+ var y = _step.value;
2035
+
2036
+ for (var _iterator8 = _createForOfIteratorHelperLoose(columns), _step8; !(_step8 = _iterator8()).done;) {
2037
+ var x = _step8.value;
2038
+
2039
+ var _left7 = columnToPixel(x);
1644
2040
 
1645
- if (nonEmpty) {
1646
- cptext += cptextTemp;
1647
- cptextTemp = '';
2041
+ var _right7 = columnToPixel(x, 1);
2042
+
2043
+ var _top7 = rowToPixel(y);
2044
+
2045
+ var _bottom7 = rowToPixel(y, 1);
2046
+
2047
+ var _cellStyle = cellStyle(x, y),
2048
+ fillColor = _cellStyle.fillColor;
2049
+
2050
+ if (fillColor) {
2051
+ context.fillStyle = fillColor;
2052
+ context.fillRect(_left7, _top7, _right7 - _left7, _bottom7 - _top7);
1648
2053
  }
1649
2054
  }
2055
+ }
1650
2056
 
1651
- if (copyPasteTextAreaRef.current) {
1652
- copyPasteTextAreaRef.current.value = cptext;
1653
- }
1654
- };
2057
+ var selectionActive = !isEmptySelection(selection);
2058
+ var rowSelectionActive = isRowSelection(selection);
2059
+ var columnSelectionActive = isColumnSelection(selection);
1655
2060
 
1656
- var commitEditingCell = function commitEditingCell(value) {
1657
- if (props.onChange) {
1658
- props.onChange([{
1659
- x: editCell.x,
1660
- y: editCell.y,
1661
- value: value !== undefined ? value : editValue
1662
- }]);
1663
- }
2061
+ var _resolveFrozenSelecti = resolveFrozenSelection(selection, cellLayout, freeze, indent, dataOffset),
2062
+ selected = _resolveFrozenSelecti[0],
2063
+ hideKnob = _resolveFrozenSelecti[1];
1664
2064
 
1665
- setEditCell({
1666
- x: -1,
1667
- y: -1
1668
- });
1669
- setEditKey('');
1670
- };
2065
+ if (selectionActive) {
2066
+ var _selected$ = selected[0],
2067
+ left = _selected$[0],
2068
+ top = _selected$[1],
2069
+ _selected$2 = selected[1],
2070
+ right = _selected$2[0],
2071
+ bottom = _selected$2[1];
2072
+ context.fillStyle = COLORS.selectionBackground;
2073
+ context.fillRect(left, top, right - left, bottom - top);
2074
+ }
1671
2075
 
1672
- var startEditingCell = function startEditingCell(editCell) {
1673
- if (cellReadOnly(editCell.x, editCell.y)) {
1674
- return;
2076
+ if (!hideRowHeaders) {
2077
+ context.fillStyle = COLORS.headerBackground;
2078
+ context.fillRect(0, 0, rowHeaderWidth, context.canvas.height);
2079
+
2080
+ if (selectionActive && !columnSelectionActive) {
2081
+ var _selected$3 = selected[0],
2082
+ _top = _selected$3[1],
2083
+ _selected$4 = selected[1],
2084
+ _bottom = _selected$4[1];
2085
+ context.fillStyle = COLORS.headerActive;
2086
+ context.fillRect(0, _top, rowHeaderWidth, _bottom - _top);
1675
2087
  }
2088
+ }
1676
2089
 
1677
- var editDataValue = editData(editCell.x, editCell.y);
1678
- var val = '';
2090
+ if (!hideColumnHeaders) {
2091
+ context.fillStyle = COLORS.headerBackground;
2092
+ context.fillRect(0, 0, context.canvas.width, columnHeaderHeight);
1679
2093
 
1680
- if (editDataValue !== null && editDataValue !== undefined) {
1681
- val = editDataValue;
2094
+ if (selectionActive && !rowSelectionActive) {
2095
+ var _selected$5 = selected[0],
2096
+ _left = _selected$5[0],
2097
+ _selected$6 = selected[1],
2098
+ _right = _selected$6[0];
2099
+ context.fillStyle = COLORS.headerActive;
2100
+ context.fillRect(_left, 0, _right - _left, columnHeaderHeight);
1682
2101
  }
2102
+ }
1683
2103
 
1684
- var editDataKey = editKeys ? editKeys(editCell.x, editCell.y) : '';
1685
- setEditCell(editCell);
1686
- setEditKey(editDataKey);
1687
- setEditValue(val);
1688
- setShiftKeyDown(false);
2104
+ context.strokeStyle = COLORS.gridLine;
2105
+ context.lineWidth = 1;
2106
+ var gridRight = hideGridlines ? rowHeaderWidth : context.canvas.width;
2107
+ var gridBottom = hideGridlines ? columnHeaderHeight : context.canvas.height;
2108
+
2109
+ var drawGridLineX = function drawGridLineX(x, height) {
2110
+ context.beginPath();
2111
+ context.moveTo(x - .5, 0);
2112
+ context.lineTo(x - .5, height);
2113
+ context.stroke();
1689
2114
  };
1690
2115
 
1691
- var onScroll = function onScroll(e) {
1692
- if (!e.target || !(e.target instanceof Element)) {
1693
- return;
1694
- }
2116
+ var drawGridLineY = function drawGridLineY(y, width) {
2117
+ context.beginPath();
2118
+ context.moveTo(0, y - .5);
2119
+ context.lineTo(width, y - .5);
2120
+ context.stroke();
2121
+ };
1695
2122
 
1696
- var absX = e.target.scrollLeft;
1697
- var absY = e.target.scrollTop;
1698
- var cellX = Math.floor(absX / scrollSpeed);
1699
- var cellY = Math.floor(absY / scrollSpeed);
2123
+ drawGridLineX(rowHeaderWidth, context.canvas.height);
2124
+ drawGridLineY(columnHeaderHeight, context.canvas.width);
1700
2125
 
1701
- if (cellX !== dataOffset.x || cellY !== dataOffset.y) {
1702
- setDataOffset({
1703
- x: cellX,
1704
- y: cellY
1705
- });
1706
- }
2126
+ for (var _iterator2 = _createForOfIteratorHelperLoose(columns), _step2; !(_step2 = _iterator2()).done;) {
2127
+ var _column = _step2.value;
1707
2128
 
1708
- var newMaxScroll = _extends({}, maxScroll);
2129
+ var _right8 = columnToPixel(_column, 1);
1709
2130
 
1710
- if (maxScroll.x / (absX + 0.5) < 1) {
1711
- newMaxScroll.x *= 1.5;
1712
- }
2131
+ drawGridLineX(_right8, gridBottom);
2132
+ }
2133
+
2134
+ for (var _iterator3 = _createForOfIteratorHelperLoose(rows), _step3; !(_step3 = _iterator3()).done;) {
2135
+ var _row = _step3.value;
2136
+
2137
+ var _bottom8 = rowToPixel(_row, 1);
2138
+
2139
+ drawGridLineY(_bottom8, gridRight);
2140
+ }
2141
+
2142
+ var _normalizeSelection = normalizeSelection(selection),
2143
+ _normalizeSelection$ = _normalizeSelection[0],
2144
+ minX = _normalizeSelection$[0],
2145
+ minY = _normalizeSelection$[1],
2146
+ _normalizeSelection$2 = _normalizeSelection[1],
2147
+ maxX = _normalizeSelection$2[0],
2148
+ maxY = _normalizeSelection$2[1];
2149
+
2150
+ if (!hideRowHeaders) {
2151
+ context.textBaseline = 'middle';
2152
+ context.textAlign = 'center';
2153
+ context.font = DEFAULT_CELL_STYLE.fontSize + 'px ' + DEFAULT_CELL_STYLE.fontFamily;
2154
+ context.fillStyle = COLORS.headerText;
2155
+
2156
+ for (var _iterator4 = _createForOfIteratorHelperLoose(rows), _step4; !(_step4 = _iterator4()).done;) {
2157
+ var row = _step4.value;
2158
+ var content = "" + (row + 1);
2159
+ var isActive = isInRange(row, minY, maxY);
2160
+ var isSelected = rowSelectionActive && !columnSelectionActive && isActive;
2161
+ var style = isSelected ? HEADER_SELECTED_STYLE : isActive ? HEADER_ACTIVE_STYLE : NO_STYLE;
2162
+
2163
+ var _top2 = rowToPixel(row);
1713
2164
 
1714
- if (maxScroll.y / (absY + 0.5) < 1) {
1715
- newMaxScroll.y *= 1.5;
2165
+ var _bottom2 = rowToPixel(row, 1);
2166
+
2167
+ clickables.push.apply(clickables, renderCell(context, content, style, DEFAULT_COLUMN_HEADER_STYLE, 0, _top2, rowHeaderWidth, _bottom2 - _top2));
1716
2168
  }
2169
+ }
2170
+
2171
+ if (!hideColumnHeaders) {
2172
+ context.textBaseline = 'middle';
2173
+ context.textAlign = 'center';
2174
+
2175
+ for (var _iterator5 = _createForOfIteratorHelperLoose(columns), _step5; !(_step5 = _iterator5()).done;) {
2176
+ var _columnHeaders;
2177
+
2178
+ var column = _step5.value;
2179
+
2180
+ var _content = (_columnHeaders = columnHeaders(column)) != null ? _columnHeaders : excelHeaderString(column + 1);
2181
+
2182
+ var _isActive = isInRange(column, minX, maxX);
2183
+
2184
+ var selectedStyle = columnSelectionActive && !rowSelectionActive && _isActive ? HEADER_SELECTED_STYLE : NO_STYLE;
2185
+ var activeStyle = _isActive ? HEADER_ACTIVE_STYLE : NO_STYLE;
2186
+
2187
+ var _style = _extends({}, columnHeaderStyle(column), activeStyle, selectedStyle);
1717
2188
 
1718
- if (newMaxScroll.x !== maxScroll.x || maxScroll.y !== newMaxScroll.y) {
1719
- setMaxScroll(_extends({}, newMaxScroll));
2189
+ var _left2 = columnToPixel(column);
2190
+
2191
+ var _right2 = columnToPixel(column, 1);
2192
+
2193
+ clickables.push.apply(clickables, renderCell(context, _content, _style, DEFAULT_COLUMN_HEADER_STYLE, _left2, 0, _right2 - _left2, columnHeaderHeight));
1720
2194
  }
1721
- };
2195
+ }
1722
2196
 
1723
- var onMouseLeave = function onMouseLeave() {
1724
- window.document.body.style.cursor = 'auto';
1725
- };
2197
+ if (selectionActive) {
2198
+ context.strokeStyle = COLORS.selectionBorder;
2199
+ context.lineWidth = 2;
2200
+ var _selected$7 = selected[0],
2201
+ _left3 = _selected$7[0],
2202
+ _top3 = _selected$7[1],
2203
+ _selected$8 = selected[1],
2204
+ _right3 = _selected$8[0],
2205
+ _bottom3 = _selected$8[1];
2206
+ context.strokeRect(_left3, _top3, _right3 - _left3 - 1, _bottom3 - _top3 - 1);
2207
+ }
1726
2208
 
1727
- var onMouseDown = function onMouseDown(e) {
1728
- if (e.button !== 0) {
1729
- return;
2209
+ for (var _iterator6 = _createForOfIteratorHelperLoose(secondarySelections), _step6; !(_step6 = _iterator6()).done;) {
2210
+ var secondarySelection = _step6.value;
2211
+ var _selection = secondarySelection.span;
2212
+ if (isEmptySelection(_selection)) continue;
2213
+
2214
+ var _resolveFrozenSelecti2 = resolveFrozenSelection(_selection, cellLayout, freeze, indent, dataOffset),
2215
+ _selected = _resolveFrozenSelecti2[0];
2216
+
2217
+ var _selected$9 = _selected[0],
2218
+ _left8 = _selected$9[0],
2219
+ _top8 = _selected$9[1],
2220
+ _selected$10 = _selected[1],
2221
+ _right9 = _selected$10[0],
2222
+ _bottom9 = _selected$10[1];
2223
+ context.strokeStyle = secondarySelection.color;
2224
+ context.lineWidth = 1;
2225
+ context.beginPath();
2226
+ context.strokeRect(_left8 - 1, _top8 - 1, _right9 - _left8 + 1, _bottom9 - _top8 + 1);
2227
+ }
2228
+
2229
+ if (knobArea) {
2230
+ var _normalizeSelection2 = normalizeSelection(knobArea),
2231
+ _normalizeSelection2$ = _normalizeSelection2[0],
2232
+ _minX = _normalizeSelection2$[0],
2233
+ _minY = _normalizeSelection2$[1],
2234
+ _normalizeSelection2$2 = _normalizeSelection2[1],
2235
+ _maxX = _normalizeSelection2$2[0],
2236
+ _maxY = _normalizeSelection2$2[1];
2237
+
2238
+ var _left4 = columnToPixel(_minX);
2239
+
2240
+ var _top4 = rowToPixel(_minY);
2241
+
2242
+ var _right4 = columnToPixel(_maxX, 1);
2243
+
2244
+ var _bottom4 = rowToPixel(_maxY, 1);
2245
+
2246
+ context.strokeStyle = COLORS.knobAreaBorder;
2247
+ context.setLineDash([3, 3]);
2248
+ context.lineWidth = 1;
2249
+ context.strokeRect(_left4 - 1, _top4 - 1, _right4 - _left4 + 1, _bottom4 - _top4 + 1);
2250
+ context.setLineDash([]);
2251
+ }
2252
+
2253
+ if (knobPosition && !hideKnob) {
2254
+ var knobX = knobPosition[0],
2255
+ knobY = knobPosition[1];
2256
+ context.fillStyle = COLORS.selectionBorder;
2257
+ context.fillRect(knobX - SIZES.knobArea * 0.5, knobY - SIZES.knobArea * 0.5, SIZES.knobArea, SIZES.knobArea);
2258
+ }
2259
+
2260
+ if (dragOffset) {
2261
+ var shiftX = dragOffset[0],
2262
+ shiftY = dragOffset[1];
2263
+
2264
+ var _resolveSelection = resolveSelection(selection, cellLayout),
2265
+ _resolveSelection$ = _resolveSelection[0],
2266
+ _left5 = _resolveSelection$[0],
2267
+ _top5 = _resolveSelection$[1],
2268
+ _resolveSelection$2 = _resolveSelection[1],
2269
+ _right5 = _resolveSelection$2[0],
2270
+ _bottom5 = _resolveSelection$2[1];
2271
+
2272
+ context.fillStyle = COLORS.dragGhost;
2273
+ context.fillRect(_left5 + shiftX, _top5 + shiftY, _right5 - _left5, _bottom5 - _top5);
2274
+ }
2275
+
2276
+ if (dropTarget) {
2277
+ var _resolveSelection2 = resolveSelection(dropTarget, cellLayout),
2278
+ _resolveSelection2$ = _resolveSelection2[0],
2279
+ _left6 = _resolveSelection2$[0],
2280
+ _top6 = _resolveSelection2$[1],
2281
+ _resolveSelection2$2 = _resolveSelection2[1],
2282
+ _right6 = _resolveSelection2$2[0],
2283
+ _bottom6 = _resolveSelection2$2[1];
2284
+
2285
+ context.strokeStyle = COLORS.dropTarget;
2286
+ context.lineWidth = 2;
2287
+
2288
+ if (isColumnSelection(dropTarget)) {
2289
+ _right6 = _left6;
1730
2290
  }
1731
2291
 
1732
- if (!e.target || !(e.target instanceof Element)) {
1733
- return;
2292
+ if (isRowSelection(dropTarget)) {
2293
+ _bottom6 = _top6;
1734
2294
  }
1735
2295
 
1736
- setSelectionInProgress(false);
1737
- var rect = e.target.getBoundingClientRect();
1738
- var x = e.clientX - rect.left;
1739
- var y = e.clientY - rect.top;
2296
+ context.strokeRect(_left6 - 1, _top6 - 1, _right6 - _left6, _bottom6 - _top6);
2297
+ }
1740
2298
 
1741
- if (x > canvasWidth || y > canvasHeight) {
1742
- return;
1743
- }
2299
+ context.textBaseline = 'middle';
1744
2300
 
1745
- var hitTargetKeyX = Math.floor(x / xBinSize);
1746
- var hitTargetKeyY = Math.floor(y / yBinSize);
2301
+ for (var _iterator7 = _createForOfIteratorHelperLoose(rows), _step7; !(_step7 = _iterator7()).done;) {
2302
+ var _y = _step7.value;
1747
2303
 
1748
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
1749
- for (var _iterator20 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step20; !(_step20 = _iterator20()).done;) {
1750
- var hitTarget = _step20.value;
2304
+ for (var _iterator9 = _createForOfIteratorHelperLoose(columns), _step9; !(_step9 = _iterator9()).done;) {
2305
+ var _x = _step9.value;
1751
2306
 
1752
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
1753
- setButtonClickMouseDownCoordinates({
1754
- x: x,
1755
- y: y,
1756
- hitTarget: hitTarget
1757
- });
1758
- return;
1759
- }
1760
- }
1761
- }
2307
+ var _left9 = columnToPixel(_x);
1762
2308
 
1763
- if (!sheetStyle.hideColumnHeaders && y < sheetStyle.columnHeaderHeight) {
1764
- for (var colIdx = 0; colIdx < columnSizes.index.length; colIdx++) {
1765
- var start = columnSizes.start[colIdx];
1766
- var end = columnSizes.end[colIdx];
1767
- var index = columnSizes.index[colIdx];
2309
+ var _right10 = columnToPixel(_x, 1);
1768
2310
 
1769
- if (Math.abs(start - x) < resizeColumnRowMouseThreshold || Math.abs(end - x) < resizeColumnRowMouseThreshold) {
1770
- window.document.body.style.cursor = 'col-resize';
1771
- var indices = [];
2311
+ var _top9 = rowToPixel(_y);
1772
2312
 
1773
- if (selection.x1 !== -1 && selection.x2 !== -1 && selection.y1 === -1 && selection.y2 === -1) {
1774
- var min = Math.min(selection.x1, selection.x2);
1775
- var max = Math.max(selection.x1, selection.x2);
2313
+ var _bottom10 = rowToPixel(_y, 1);
1776
2314
 
1777
- for (var i = min; i <= max; i++) {
1778
- indices.push(i);
1779
- }
1780
- }
2315
+ var cellContent = displayData(_x, _y);
1781
2316
 
1782
- if (indices.length === 0) {
1783
- indices.push(index);
1784
- }
2317
+ if (cellContent !== null && cellContent !== undefined) {
2318
+ var _style2 = cellStyle(_x, _y);
1785
2319
 
1786
- setColumnResize({
1787
- startX: end,
1788
- oldWidth: cellWidth(index),
1789
- colIndices: indices
1790
- });
1791
- return;
1792
- }
2320
+ clickables.push.apply(clickables, renderCell(context, cellContent, _style2, DEFAULT_CELL_STYLE, _left9, _top9, _right10 - _left9, _bottom10 - _top9));
1793
2321
  }
1794
2322
  }
2323
+ }
1795
2324
 
1796
- if (!sheetStyle.hideRowHeaders && x < sheetStyle.rowHeaderWidth) {
1797
- for (var rowIdx = 0; rowIdx < rowSizes.index.length; rowIdx++) {
1798
- var _start = rowSizes.start[rowIdx];
1799
- var _end = rowSizes.end[rowIdx];
1800
- var _index = rowSizes.index[rowIdx];
1801
-
1802
- if (Math.abs(_start - y) < resizeColumnRowMouseThreshold || Math.abs(_end - y) < resizeColumnRowMouseThreshold) {
1803
- window.document.body.style.cursor = 'row-resize';
1804
- var _indices = [];
2325
+ return clickables;
2326
+ };
2327
+ var renderCell = function renderCell(context, cellContent, style, defaultCellStyle, xCoord, yCoord, cellWidth, cellHeight) {
2328
+ var clickables = [];
1805
2329
 
1806
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1807
- var _min = Math.min(selection.y1, selection.y2);
2330
+ if (cellContent === null) {
2331
+ return clickables;
2332
+ }
1808
2333
 
1809
- var _max2 = Math.max(selection.y1, selection.y2);
2334
+ var finalStyle = resolveCellStyle(style, defaultCellStyle);
2335
+ context.fillStyle = finalStyle.color;
2336
+ context.font = finalStyle.weight + ' ' + finalStyle.fontSize + 'px ' + finalStyle.fontFamily;
2337
+ context.textAlign = finalStyle.textAlign;
2338
+ var yy = Math.floor(yCoord + cellHeight * 0.5);
2339
+ context.save();
2340
+ context.beginPath();
2341
+ context.rect(xCoord, yCoord, cellWidth, cellHeight);
2342
+ context.clip();
1810
2343
 
1811
- for (var _i5 = _min; _i5 <= _max2; _i5++) {
1812
- _indices.push(_i5);
1813
- }
1814
- }
2344
+ if (finalStyle.backgroundColor !== '') {
2345
+ context.fillStyle = finalStyle.backgroundColor;
2346
+ context.fillRect(xCoord, yCoord, cellWidth, cellHeight);
2347
+ context.fillStyle = finalStyle.color;
2348
+ }
1815
2349
 
1816
- if (_indices.length === 0) {
1817
- _indices.push(_index);
1818
- }
2350
+ if (typeof cellContent === 'string' || typeof cellContent === 'number') {
2351
+ var xx = applyAlignment(xCoord, cellWidth, finalStyle, 0);
2352
+ var text = '' + cellContent;
2353
+ context.fillText(text, xx, yy);
2354
+ } else if (typeof cellContent === 'object') {
2355
+ for (var _iterator10 = _createForOfIteratorHelperLoose(cellContent.items), _step10; !(_step10 = _iterator10()).done;) {
2356
+ var obj = _step10.value;
2357
+ var x = 0;
2358
+ var y = 0;
2359
+ var w = 0;
2360
+ var h = 0;
1819
2361
 
1820
- setRowResize({
1821
- startY: _end,
1822
- oldHeight: cellHeight(_index),
1823
- rowIndices: _indices
1824
- });
1825
- return;
2362
+ if (obj.content instanceof HTMLImageElement) {
2363
+ w = obj.width || cellWidth;
2364
+ h = obj.height || cellHeight;
2365
+ var finalX = applyAlignment(xCoord, cellWidth, finalStyle, w, obj.horizontalAlign);
2366
+ x = finalX + obj.x;
2367
+ y = yy + obj.y;
2368
+ context.drawImage(obj.content, x, y, w, h);
2369
+ } else if (typeof obj.content === 'string' || typeof obj.content === 'number') {
2370
+ if (obj.horizontalAlign) {
2371
+ context.textAlign = obj.horizontalAlign;
1826
2372
  }
1827
- }
1828
- }
1829
2373
 
1830
- if (Math.abs(x - knobCoordinates.x) < knobSize && Math.abs(y - knobCoordinates.y) < knobSize) {
1831
- setKnobDragInProgress(true);
1832
- setKnobArea({
1833
- x1: selection.x1,
1834
- y1: selection.y1,
1835
- x2: selection.x2,
1836
- y2: selection.y2
1837
- });
1838
- return;
1839
- }
2374
+ var _finalX = applyAlignment(xCoord, cellWidth, finalStyle, 0, obj.horizontalAlign);
1840
2375
 
1841
- var sel2 = absCoordianteToCell(x, y, rowSizes, columnSizes);
1842
- var sel1 = shiftKeyDown ? {
1843
- x: selection.x1,
1844
- y: selection.y1
1845
- } : _extends({}, sel2);
2376
+ var _text = '' + obj.content;
1846
2377
 
1847
- if (editMode) {
1848
- if (!props.dontCommitEditOnSelectionChange) {
1849
- commitEditingCell();
2378
+ var left = _finalX + obj.x;
2379
+ var top = yy + obj.y;
2380
+ context.fillText(_text, left, top);
2381
+ var measure = context.measureText(_text);
2382
+ x = left - measure.actualBoundingBoxLeft;
2383
+ y = top - measure.actualBoundingBoxAscent;
2384
+ w = left + measure.actualBoundingBoxRight - x;
2385
+ h = top + measure.actualBoundingBoxDescent - y;
2386
+ }
2387
+
2388
+ if (obj.onClick) {
2389
+ clickables.push({
2390
+ rect: [[x, y], [x + w, y + h]],
2391
+ obj: obj
2392
+ });
1850
2393
  }
1851
2394
  }
2395
+ }
1852
2396
 
1853
- var scrollToP2 = true;
2397
+ context.restore();
2398
+ return clickables;
2399
+ };
1854
2400
 
1855
- if (!sheetStyle.hideRowHeaders && x < sheetStyle.rowHeaderWidth) {
1856
- scrollToP2 = false;
1857
- setRowSelectionInProgress(true);
1858
- sel1.x = -1;
1859
- sel2.x = -1;
1860
- } else {
1861
- setRowSelectionInProgress(false);
1862
- }
2401
+ var resolveSelection = function resolveSelection(selection, cellLayout) {
2402
+ var cellToPixel = cellLayout.cellToPixel;
2403
+ var rowSelectionActive = isRowSelection(selection);
2404
+ var columnSelectionActive = isColumnSelection(selection);
1863
2405
 
1864
- if (!sheetStyle.hideColumnHeaders && y < sheetStyle.columnHeaderHeight) {
1865
- scrollToP2 = false;
1866
- setColumnSelectionInProgress(true);
1867
- sel1.y = -1;
1868
- sel2.y = -1;
1869
- } else {
1870
- setColumnSelectionInProgress(false);
1871
- }
2406
+ var _normalizeSelection3 = normalizeSelection(selection),
2407
+ min = _normalizeSelection3[0],
2408
+ max = _normalizeSelection3[1];
1872
2409
 
1873
- setSelectionInProgress(true);
1874
- changeSelection(sel1.x, sel1.y, sel2.x, sel2.y, scrollToP2);
2410
+ var _cellToPixel = cellToPixel(min),
2411
+ left = _cellToPixel[0],
2412
+ top = _cellToPixel[1];
1875
2413
 
1876
- if (!props.dontCommitEditOnSelectionChange) {
1877
- setEditCell({
1878
- x: -1,
1879
- y: -1
1880
- });
1881
- setEditKey('');
1882
- }
1883
- };
2414
+ var _cellToPixel2 = cellToPixel(max, ONE_ONE),
2415
+ right = _cellToPixel2[0],
2416
+ bottom = _cellToPixel2[1];
2417
+
2418
+ if (rowSelectionActive) {
2419
+ right = 1e5;
2420
+ }
1884
2421
 
1885
- var onMouseUp = function onMouseUp(e) {
1886
- if (knobDragInProgress) {
1887
- var sx1 = selection.x1;
1888
- var sx2 = selection.x2;
2422
+ if (columnSelectionActive) {
2423
+ bottom = 1e5;
2424
+ }
1889
2425
 
1890
- if (selection.x1 > selection.x2) {
1891
- sx1 = selection.x2;
1892
- sx2 = selection.x1;
1893
- }
2426
+ return [[left, top], [right, bottom]];
2427
+ };
2428
+
2429
+ var resolveFrozenSelection = function resolveFrozenSelection(selection, cellLayout, freeze, indent, offset) {
2430
+ var cellToPixel = cellLayout.cellToPixel,
2431
+ columnToAbsolute = cellLayout.columnToAbsolute,
2432
+ rowToAbsolute = cellLayout.rowToAbsolute;
2433
+ var rowSelectionActive = isRowSelection(selection);
2434
+ var columnSelectionActive = isColumnSelection(selection);
2435
+ var freezeX = freeze[0],
2436
+ freezeY = freeze[1];
2437
+ var indentX = indent[0],
2438
+ indentY = indent[1];
2439
+ var offsetX = offset[0],
2440
+ offsetY = offset[1];
2441
+
2442
+ var _normalizeSelection4 = normalizeSelection(selection),
2443
+ min = _normalizeSelection4[0],
2444
+ max = _normalizeSelection4[1];
2445
+
2446
+ var minX = min[0],
2447
+ minY = min[1];
2448
+ var maxX = max[0],
2449
+ maxY = max[1];
2450
+
2451
+ var _cellToPixel3 = cellToPixel(min),
2452
+ left = _cellToPixel3[0],
2453
+ top = _cellToPixel3[1];
2454
+
2455
+ var _cellToPixel4 = cellToPixel(max, ONE_ONE),
2456
+ right = _cellToPixel4[0],
2457
+ bottom = _cellToPixel4[1];
2458
+
2459
+ var frozenX = columnToAbsolute(freezeX);
2460
+ var frozenY = rowToAbsolute(freezeY);
2461
+ var hideKnob = false;
2462
+
2463
+ if (isInRangeCenter(freezeX, minX, maxX + 1)) {
2464
+ var edge = indentX + frozenX;
2465
+
2466
+ if (right <= edge) {
2467
+ right = edge;
2468
+ hideKnob = true;
2469
+ }
2470
+ }
1894
2471
 
1895
- var sy1 = selection.y1;
1896
- var sy2 = selection.y2;
2472
+ if (isInRangeCenter(freezeY, minY, maxY + 1)) {
2473
+ var _edge = indentY + frozenY;
1897
2474
 
1898
- if (selection.y1 > selection.y2) {
1899
- sy1 = selection.y2;
1900
- sy2 = selection.y1;
1901
- }
2475
+ if (bottom <= _edge) {
2476
+ bottom = _edge;
2477
+ hideKnob = true;
2478
+ }
2479
+ }
1902
2480
 
1903
- var kx1 = knobArea.x1;
1904
- var kx2 = knobArea.x2;
2481
+ if (isInRangeLeft(minX, freezeX, offsetX + freezeX)) {
2482
+ left = -1e5;
1905
2483
 
1906
- if (knobArea.x1 > knobArea.x2) {
1907
- kx1 = knobArea.x2;
1908
- kx2 = knobArea.x1;
1909
- }
2484
+ if (isInRangeRight(maxX + 1, freezeX, offsetX + freezeX)) {
2485
+ right = indentX;
2486
+ hideKnob = true;
2487
+ }
2488
+ }
1910
2489
 
1911
- var ky1 = knobArea.y1;
1912
- var ky2 = knobArea.y2;
2490
+ if (isInRangeLeft(minY, freezeY, offsetY + freezeY)) {
2491
+ top = -1e5;
1913
2492
 
1914
- if (knobArea.y1 > knobArea.y2) {
1915
- ky1 = knobArea.y2;
1916
- ky2 = knobArea.y1;
1917
- }
2493
+ if (isInRangeRight(maxY + 1, freezeY, offsetY + freezeY)) {
2494
+ bottom = indentY;
2495
+ hideKnob = true;
2496
+ }
2497
+ }
1918
2498
 
1919
- var fx1 = kx1;
1920
- var fy1 = ky1;
1921
- var fx2 = kx2;
1922
- var fy2 = ky2;
1923
- var changes = [];
2499
+ if (rowSelectionActive && offsetX > 0) {
2500
+ hideKnob = true;
2501
+ }
1924
2502
 
1925
- if (fx2 - fx1 === sx2 - sx1) {
1926
- if (fy1 === sy1) {
1927
- fy1 = sy2 + 1;
1928
- } else {
1929
- fy2 = sy1 - 1;
1930
- }
2503
+ if (columnSelectionActive && offsetY > 0) {
2504
+ hideKnob = true;
2505
+ }
1931
2506
 
1932
- if (fx1 === -1 && fx2 === -1) {
1933
- var max = findApproxMaxEditDataIndex(editData);
1934
- fx1 = 0;
1935
- fx2 = max.x;
1936
- }
2507
+ if (rowSelectionActive) {
2508
+ right = 1e5;
2509
+ }
1937
2510
 
1938
- var srcY = sy1;
2511
+ if (columnSelectionActive) {
2512
+ bottom = 1e5;
2513
+ }
1939
2514
 
1940
- for (var y = fy1; y <= fy2; y++) {
1941
- for (var x = fx1; x <= fx2; x++) {
1942
- var value = sourceData(x, srcY);
1943
- changes.push({
1944
- x: x,
1945
- y: y,
1946
- value: value,
1947
- source: {
1948
- x: x,
1949
- y: srcY
1950
- }
1951
- });
1952
- }
2515
+ return [[[left, top], [right, bottom]], hideKnob];
2516
+ };
1953
2517
 
1954
- srcY = srcY + 1;
2518
+ var resizeCanvas = function resizeCanvas(canvas) {
2519
+ var _canvas$getBoundingCl = canvas.getBoundingClientRect(),
2520
+ width = _canvas$getBoundingCl.width,
2521
+ height = _canvas$getBoundingCl.height;
1955
2522
 
1956
- if (srcY > sy2) {
1957
- srcY = sy1;
1958
- }
1959
- }
1960
- } else {
1961
- if (fx1 === sx1) {
1962
- fx1 = sx2 + 1;
1963
- } else {
1964
- fx2 = sx1 - 1;
1965
- }
2523
+ var _window = window,
2524
+ _window$devicePixelRa = _window.devicePixelRatio,
2525
+ ratio = _window$devicePixelRa === void 0 ? 1 : _window$devicePixelRa;
1966
2526
 
1967
- if (fy1 === -1 && fy2 === -1) {
1968
- var _max3 = findApproxMaxEditDataIndex(editData);
2527
+ if (ratio < 1) {
2528
+ ratio = 1;
2529
+ }
1969
2530
 
1970
- fy1 = 0;
1971
- fy2 = _max3.y;
1972
- }
2531
+ var newCanvasWidth = Math.round(width * ratio);
2532
+ var newCanvasHeight = Math.round(height * ratio);
1973
2533
 
1974
- var srcX = sx1;
2534
+ if (canvas.width !== newCanvasWidth || canvas.height !== newCanvasHeight) {
2535
+ var context = canvas.getContext('2d');
1975
2536
 
1976
- for (var _x3 = fx1; _x3 <= fx2; _x3++) {
1977
- for (var _y2 = fy1; _y2 <= fy2; _y2++) {
1978
- var _value = sourceData(srcX, _y2);
2537
+ if (context) {
2538
+ canvas.width = newCanvasWidth;
2539
+ canvas.height = newCanvasHeight;
2540
+ context.scale(ratio, ratio);
2541
+ }
1979
2542
 
1980
- changes.push({
1981
- x: _x3,
1982
- y: _y2,
1983
- value: _value,
1984
- source: {
1985
- x: srcX,
1986
- y: _y2
1987
- }
1988
- });
1989
- }
2543
+ return true;
2544
+ }
1990
2545
 
1991
- srcX = srcX + 1;
2546
+ return false;
2547
+ };
1992
2548
 
1993
- if (srcX > sx2) {
1994
- srcX = sx1;
1995
- }
1996
- }
1997
- }
2549
+ var excelHeaderString = function excelHeaderString(num) {
2550
+ var s = '';
2551
+ var t = 0;
1998
2552
 
1999
- if (props.onChange) {
2000
- props.onChange(changes);
2001
- }
2553
+ while (num > 0) {
2554
+ t = (num - 1) % 26;
2555
+ s = String.fromCharCode(65 + t) + s;
2556
+ num = (num - t) / 26 | 0;
2557
+ }
2002
2558
 
2003
- changeSelection(knobArea.x1, knobArea.y1, knobArea.x2, knobArea.y2);
2004
- }
2559
+ return s || '';
2560
+ };
2005
2561
 
2006
- setSelectionInProgress(false);
2007
- setRowSelectionInProgress(false);
2008
- setColumnSelectionInProgress(false);
2009
- setKnobDragInProgress(false);
2010
- setColumnResize(null);
2011
- setRowResize(null);
2562
+ var Sheet = forwardRef(function (props, ref) {
2563
+ var _props$secondarySelec, _props$inputComponent;
2012
2564
 
2013
- if (buttonClickMouseDownCoordinates.x !== -1 && buttonClickMouseDownCoordinates.y !== -1 && buttonClickMouseDownCoordinates.hitTarget !== null) {
2014
- if (!e.target || !(e.target instanceof Element)) {
2015
- return;
2016
- }
2565
+ var canvasRef = useRef(null);
2566
+ var overlayRef = useRef(null);
2017
2567
 
2018
- var rect = e.target.getBoundingClientRect();
2568
+ var _useState = useState(INITIAL_MAX_SCROLL),
2569
+ maxScroll = _useState[0],
2570
+ setMaxScroll = _useState[1];
2019
2571
 
2020
- var _x4 = e.clientX - rect.left;
2572
+ var _useState2 = useState(ORIGIN),
2573
+ dataOffset = _useState2[0],
2574
+ setDataOffset = _useState2[1];
2021
2575
 
2022
- var _y3 = e.clientY - rect.top;
2576
+ var _useState3 = useState(NO_SELECTION),
2577
+ selection = _useState3[0],
2578
+ setSelection = _useState3[1];
2023
2579
 
2024
- var hitTarget = buttonClickMouseDownCoordinates.hitTarget;
2580
+ var _useState4 = useState(null),
2581
+ knobArea = _useState4[0],
2582
+ setKnobArea = _useState4[1];
2025
2583
 
2026
- if (hitTarget.x <= _x4 && _x4 <= hitTarget.x + hitTarget.w && hitTarget.y <= _y3 && _y3 <= hitTarget.y + hitTarget.h) {
2027
- hitTarget.onClick(e);
2028
- }
2584
+ var _useState5 = useState(null),
2585
+ dragOffset = _useState5[0],
2586
+ setDragOffset = _useState5[1];
2029
2587
 
2030
- setButtonClickMouseDownCoordinates({
2031
- x: -1,
2032
- y: -1,
2033
- hitTarget: null
2034
- });
2035
- }
2036
- };
2588
+ var _useState6 = useState(null),
2589
+ dropTarget = _useState6[0],
2590
+ setDropTarget = _useState6[1];
2037
2591
 
2038
- useEffect(function () {
2039
- window.addEventListener('mouseup', onMouseUp);
2040
- return function () {
2041
- window.removeEventListener('mouseup', onMouseUp);
2042
- };
2043
- });
2592
+ var _useState7 = useState(NO_CELL),
2593
+ editCell = _useState7[0],
2594
+ setEditCell = _useState7[1];
2044
2595
 
2045
- var onMouseMove = function onMouseMove(e) {
2046
- if (!e.target || !(e.target instanceof Element)) {
2047
- return;
2048
- }
2596
+ var _useState8 = useState(''),
2597
+ editValue = _useState8[0],
2598
+ setEditValue = _useState8[1];
2049
2599
 
2050
- var rect = e.target.getBoundingClientRect();
2051
- var x = e.clientX - rect.left;
2052
- var y = e.clientY - rect.top;
2053
- window.document.body.style.cursor = 'auto';
2054
- var hitTargetKeyX = Math.floor(x / xBinSize);
2055
- var hitTargetKeyY = Math.floor(y / yBinSize);
2600
+ var _useState9 = useState(false),
2601
+ arrowKeyCommitMode = _useState9[0],
2602
+ setArrowKeyCommitMode = _useState9[1];
2056
2603
 
2057
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
2058
- for (var _iterator21 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step21; !(_step21 = _iterator21()).done;) {
2059
- var hitTarget = _step21.value;
2604
+ var _useResizeObserver = useResizeObserver({
2605
+ ref: canvasRef
2606
+ }),
2607
+ _useResizeObserver$wi = _useResizeObserver.width,
2608
+ canvasWidth = _useResizeObserver$wi === void 0 ? 3000 : _useResizeObserver$wi,
2609
+ _useResizeObserver$he = _useResizeObserver.height,
2610
+ canvasHeight = _useResizeObserver$he === void 0 ? 3000 : _useResizeObserver$he;
2060
2611
 
2061
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
2062
- window.document.body.style.cursor = 'pointer';
2063
- }
2064
- }
2612
+ var cellWidth = useMemo(function () {
2613
+ return createRowOrColumnProp(props.cellWidth, 100);
2614
+ }, [props.cellWidth]);
2615
+ var cellHeight = useMemo(function () {
2616
+ return createRowOrColumnProp(props.cellHeight, 22);
2617
+ }, [props.cellHeight]);
2618
+ var columnHeaders = useMemo(function () {
2619
+ return createRowOrColumnProp(props.columnHeaders, null);
2620
+ }, [props.columnHeaders]);
2621
+ var columnHeaderStyle = useMemo(function () {
2622
+ return createRowOrColumnProp(props.columnHeaderStyle, {});
2623
+ }, [props.columnHeaderStyle]);
2624
+ var canSizeColumn = useMemo(function () {
2625
+ return createRowOrColumnProp(props.canSizeColumn, true);
2626
+ }, [props.canSizeColumn]);
2627
+ var canSizeRow = useMemo(function () {
2628
+ return createRowOrColumnProp(props.canSizeRow, true);
2629
+ }, [props.canSizeRow]);
2630
+ var canOrderColumn = useMemo(function () {
2631
+ return createRowOrColumnProp(props.canOrderColumn, true);
2632
+ }, [props.canOrderColumn]);
2633
+ var canOrderRow = useMemo(function () {
2634
+ return createRowOrColumnProp(props.canOrderRow, true);
2635
+ }, [props.canOrderRow]);
2636
+ var cellReadOnly = useMemo(function () {
2637
+ return createCellProp(props.readOnly, false);
2638
+ }, [props.readOnly]);
2639
+ var sourceData = useMemo(function () {
2640
+ return createCellProp(props.sourceData, null);
2641
+ }, [props.sourceData]);
2642
+ var displayData = useMemo(function () {
2643
+ return createCellProp(props.displayData, '');
2644
+ }, [props.displayData]);
2645
+ var editData = useMemo(function () {
2646
+ return createCellProp(props.editData, '');
2647
+ }, [props.editData]);
2648
+ var editKeys = useMemo(function () {
2649
+ return createCellProp(props.editKeys, '');
2650
+ }, [props.editKeys]);
2651
+ var cellStyle = useMemo(function () {
2652
+ return createCellProp(props.cellStyle, DEFAULT_CELL_STYLE);
2653
+ }, [props.cellStyle]);
2654
+ var sheetStyle = useMemo(function () {
2655
+ return resolveSheetStyle(props.sheetStyle);
2656
+ }, [props.sheetStyle]);
2657
+ var secondarySelections = (_props$secondarySelec = props.secondarySelections) != null ? _props$secondarySelec : NO_SELECTIONS;
2658
+ var maxScrollX = maxScroll[0],
2659
+ maxScrollY = maxScroll[1];
2660
+ var editCellX = editCell[0],
2661
+ editCellY = editCell[1];
2662
+ var editMode = editCellX !== -1 && editCellY !== -1;
2663
+ var columnLayout = useMemo(function () {
2664
+ return makeLayoutCache(cellWidth);
2665
+ }, [props.cacheLayout ? null : cellWidth]);
2666
+ var rowLayout = useMemo(function () {
2667
+ return makeLayoutCache(cellHeight);
2668
+ }, [props.cacheLayout ? null : cellHeight]);
2669
+ useMemo(function () {
2670
+ if (!props.cacheLayout) return;
2671
+ columnLayout.setSizer(cellWidth);
2672
+ rowLayout.setSizer(cellHeight);
2673
+ }, [props.cacheLayout, cellWidth, cellHeight]);
2674
+ var freezeColumns = sheetStyle.freezeColumns,
2675
+ freezeRows = sheetStyle.freezeRows,
2676
+ rowHeaderWidth = sheetStyle.rowHeaderWidth,
2677
+ columnHeaderHeight = sheetStyle.columnHeaderHeight;
2678
+ var cellLayout = useMemo(function () {
2679
+ return makeCellLayout([freezeColumns, freezeRows], [rowHeaderWidth, columnHeaderHeight], dataOffset, columnLayout, rowLayout);
2680
+ }, [freezeColumns, freezeRows, rowHeaderWidth, columnHeaderHeight, dataOffset, columnLayout, rowLayout]);
2681
+ useImperativeHandle(ref, function () {
2682
+ return cellLayout;
2683
+ }, [cellLayout]);
2684
+ var getVisibleCells = cellLayout.getVisibleCells,
2685
+ cellToPixel = cellLayout.cellToPixel,
2686
+ getVersion = cellLayout.getVersion;
2687
+ var visibleCells = useMemo(function () {
2688
+ return getVisibleCells([canvasWidth, canvasHeight]);
2689
+ }, [getVisibleCells, canvasWidth, canvasHeight, getVersion()]);
2690
+ useLayoutEffect(function () {
2691
+ if (props.onScrollChange) {
2692
+ props.onScrollChange([].concat(visibleCells.rows), [].concat(visibleCells.columns));
2065
2693
  }
2694
+ }, [visibleCells, props.onScrollChange]);
2066
2695
 
2067
- if (!sheetStyle.hideColumnHeaders && props.onCellWidthChange && y < sheetStyle.columnHeaderHeight) {
2068
- var xx = sheetStyle.rowHeaderWidth;
2069
-
2070
- for (var _iterator22 = _createForOfIteratorHelperLoose(columnSizes.index), _step22; !(_step22 = _iterator22()).done;) {
2071
- var col = _step22.value;
2072
-
2073
- if (Math.abs(xx - x) < resizeColumnRowMouseThreshold) {
2074
- window.document.body.style.cursor = 'col-resize';
2075
- break;
2076
- }
2696
+ var changeSelection = function changeSelection(newSelection, scrollToAnchor) {
2697
+ if (scrollToAnchor === void 0) {
2698
+ scrollToAnchor = true;
2699
+ }
2077
2700
 
2078
- xx += cellWidth(col);
2079
- }
2701
+ if (!isSameSelection(selection, newSelection)) {
2702
+ setSelection(newSelection);
2080
2703
  }
2081
2704
 
2082
- if (!sheetStyle.hideRowHeaders && props.onCellHeightChange && x < sheetStyle.rowHeaderWidth) {
2083
- var yy = sheetStyle.columnHeaderHeight;
2705
+ var overlay = overlayRef.current;
2706
+ if (!overlay) return;
2084
2707
 
2085
- for (var _iterator23 = _createForOfIteratorHelperLoose(rowSizes.index), _step23; !(_step23 = _iterator23()).done;) {
2086
- var row = _step23.value;
2708
+ if (scrollToAnchor) {
2709
+ var anchor = newSelection[0];
2710
+ scrollToCell(overlay, anchor, [canvasWidth, canvasHeight], [freezeColumns, freezeRows], dataOffset, maxScroll, cellLayout, function (dataOffset, maxScroll) {
2711
+ setDataOffset(dataOffset);
2712
+ setMaxScroll(maxScroll);
2713
+ });
2714
+ }
2087
2715
 
2088
- if (Math.abs(yy - y) < resizeColumnRowMouseThreshold) {
2089
- window.document.body.style.cursor = 'row-resize';
2090
- break;
2091
- }
2716
+ if (props.onSelectionChanged) {
2717
+ var _normalizeSelection = normalizeSelection(newSelection),
2718
+ _normalizeSelection$ = _normalizeSelection[0],
2719
+ minX = _normalizeSelection$[0],
2720
+ minY = _normalizeSelection$[1],
2721
+ _normalizeSelection$2 = _normalizeSelection[1],
2722
+ maxX = _normalizeSelection$2[0],
2723
+ maxY = _normalizeSelection$2[1];
2092
2724
 
2093
- yy += cellHeight(row);
2094
- }
2725
+ props.onSelectionChanged(minX, minY, maxX, maxY);
2095
2726
  }
2727
+ };
2096
2728
 
2097
- if (Math.abs(x - knobCoordinates.x) < knobSize && Math.abs(y - knobCoordinates.y) < knobSize) {
2098
- window.document.body.style.cursor = 'crosshair';
2729
+ var commitEditingCell = function commitEditingCell(value) {
2730
+ if (props.onChange) {
2731
+ var cellX = editCell[0],
2732
+ cellY = editCell[1];
2733
+ props.onChange([{
2734
+ x: cellX,
2735
+ y: cellY,
2736
+ value: value !== undefined ? value : editValue
2737
+ }]);
2099
2738
  }
2100
2739
 
2101
- if (columnResize) {
2102
- if (props.onCellWidthChange) {
2103
- var newWidth = Math.max(columnResize.oldWidth + x - columnResize.startX, minimumColumnWidth);
2104
- props.onCellWidthChange(columnResize.colIndices, newWidth);
2105
- }
2740
+ setEditCell(NO_CELL);
2741
+ };
2106
2742
 
2107
- return;
2743
+ var startEditingCell = function startEditingCell(editCell, arrowKeyCommitMode) {
2744
+ if (arrowKeyCommitMode === void 0) {
2745
+ arrowKeyCommitMode = false;
2108
2746
  }
2109
2747
 
2110
- if (rowResize) {
2111
- if (props.onCellHeightChange) {
2112
- var newHeight = Math.max(rowResize.oldHeight + y - rowResize.startY, minimumRowHeight);
2113
- props.onCellHeightChange(rowResize.rowIndices, newHeight);
2114
- }
2748
+ var cellX = editCell[0],
2749
+ cellY = editCell[1];
2115
2750
 
2751
+ if (cellReadOnly(cellX, cellY)) {
2116
2752
  return;
2117
2753
  }
2118
2754
 
2119
- if (selectionInProgress) {
2120
- var sel2 = absCoordianteToCell(x, y, rowSizes, columnSizes);
2755
+ var editDataValue = editData(cellX, cellY);
2756
+ var val = '';
2121
2757
 
2122
- if (rowSelectionInProgress) {
2123
- changeSelection(-1, selection.y1, -1, sel2.y, false);
2124
- } else if (columnSelectionInProgress) {
2125
- changeSelection(selection.x1, -1, sel2.x, -1, false);
2126
- } else {
2127
- changeSelection(selection.x1, selection.y1, sel2.x, sel2.y);
2128
- }
2758
+ if (editDataValue !== null && editDataValue !== undefined) {
2759
+ val = editDataValue;
2129
2760
  }
2130
2761
 
2131
- if (knobDragInProgress) {
2132
- window.document.body.style.cursor = 'crosshair';
2133
- var cell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2134
- var x1 = selection.x1;
2135
- var y1 = selection.y1;
2136
- var x2 = selection.x2;
2137
- var y2 = selection.y2;
2138
-
2139
- if (x1 > x2) {
2140
- x1 = selection.x2;
2141
- x2 = selection.x1;
2142
- }
2143
-
2144
- if (y1 > y2) {
2145
- y1 = selection.y2;
2146
- y2 = selection.y1;
2147
- }
2148
-
2149
- var xCellDiff = 0;
2150
- if (cell.x < x1) xCellDiff = cell.x - x1;
2151
- if (cell.x > x2) xCellDiff = x2 - cell.x;
2152
- var yCellDiff = 0;
2153
- if (cell.y < y1) yCellDiff = cell.y - y1;
2154
- if (cell.y > y2) yCellDiff = y2 - cell.y;
2155
-
2156
- if (x1 === -1 && x2 === -1 || xCellDiff > yCellDiff) {
2157
- if (cell.y < y1) {
2158
- y1 = cell.y;
2159
- } else {
2160
- y2 = cell.y;
2161
- }
2162
- } else {
2163
- if (cell.x < x1) {
2164
- x1 = cell.x;
2165
- } else {
2166
- x2 = cell.x;
2167
- }
2168
- }
2169
-
2170
- setKnobArea({
2171
- x1: x1,
2172
- y1: y1,
2173
- x2: x2,
2174
- y2: y2
2175
- });
2176
- }
2762
+ setEditCell(editCell);
2763
+ setEditValue(val);
2764
+ setArrowKeyCommitMode(arrowKeyCommitMode);
2177
2765
  };
2178
2766
 
2179
- var onDoubleClick = function onDoubleClick(e) {
2180
- e.preventDefault();
2181
-
2182
- if (!e.target || !(e.target instanceof Element) || shiftKeyDown) {
2183
- return;
2184
- }
2767
+ var hitmapRef = useRef(NO_CLICKABLES);
2768
+ var textAreaRef = useRef(null);
2769
+ useClipboardCopy(textAreaRef, selection, editMode, editData);
2770
+ useClipboardPaste(textAreaRef, selection, changeSelection, props.onChange);
2771
+ var onScroll = useScroll(dataOffset, maxScroll, cellLayout, setDataOffset, setMaxScroll);
2185
2772
 
2186
- var rect = e.target.getBoundingClientRect();
2187
- var x = e.clientX - rect.left;
2188
- var y = e.clientY - rect.top;
2189
- var hitTargetKeyX = Math.floor(x / xBinSize);
2190
- var hitTargetKeyY = Math.floor(y / yBinSize);
2773
+ var _useMouse = useMouse(hitmapRef, selection, knobArea, editMode, editData, sourceData, canSizeColumn, canSizeRow, canOrderColumn, canOrderRow, cellLayout, visibleCells, sheetStyle, startEditingCell, commitEditingCell, setKnobArea, setDragOffset, setDropTarget, changeSelection, props.cacheLayout ? columnLayout.clearAfter : undefined, props.cacheLayout ? rowLayout.clearAfter : undefined, props.onChange, props.onColumnOrderChange, props.onRowOrderChange, props.onCellWidthChange, props.onCellHeightChange, props.onRightClick, props.dontCommitEditOnSelectionChange),
2774
+ mouseHandlers = _useMouse.mouseHandlers,
2775
+ knobPosition = _useMouse.knobPosition;
2191
2776
 
2192
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
2193
- for (var _iterator24 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step24; !(_step24 = _iterator24()).done;) {
2194
- var hitTarget = _step24.value;
2777
+ useLayoutEffect(function () {
2778
+ var canvas = canvasRef.current;
2195
2779
 
2196
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
2197
- return;
2198
- }
2199
- }
2780
+ if (!canvas) {
2781
+ return;
2200
2782
  }
2201
2783
 
2202
- var editCell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2203
- setArrowKeyCommitMode(false);
2784
+ var context = canvas.getContext('2d');
2204
2785
 
2205
- if (editMode) {
2206
- commitEditingCell();
2786
+ if (!context) {
2787
+ return;
2207
2788
  }
2208
2789
 
2209
- startEditingCell(editCell);
2210
- };
2790
+ var animationFrameId = window.requestAnimationFrame(function () {
2791
+ hitmapRef.current = renderSheet(context, cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, dataOffset);
2792
+ });
2793
+ return function () {
2794
+ window.cancelAnimationFrame(animationFrameId);
2795
+ };
2796
+ }, [cellLayout, visibleCells, sheetStyle, cellStyle, selection, secondarySelections, knobPosition, knobArea, dragOffset, dropTarget, columnHeaders, columnHeaderStyle, displayData, dataOffset]);
2211
2797
 
2212
2798
  var onKeyDown = function onKeyDown(e) {
2799
+ console.log('onKeyDown', e.key, e);
2800
+
2213
2801
  if (e.key === 'Escape') {
2214
- setEditCell({
2215
- x: -1,
2216
- y: -1
2217
- });
2218
- setEditKey('');
2802
+ setEditCell(NO_CELL);
2219
2803
  return;
2220
2804
  }
2221
2805
 
2222
- if (e.key === 'Enter') {
2223
- commitEditingCell();
2224
- changeSelection(selection.x1, selection.y1 + 1, selection.x1, selection.y1 + 1);
2225
- }
2226
-
2227
- if (e.key === 'Tab') {
2228
- e.preventDefault();
2229
- commitEditingCell();
2230
- changeSelection(selection.x1 + 1, selection.y1, selection.x1 + 1, selection.y1);
2231
- }
2806
+ var direction = e.key === 'Enter' || e.key === 'TAB' ? 'right' : arrowKeyCommitMode ? ARROW_KEYS[e.key] : null;
2232
2807
 
2233
- if (arrowKeyCommitMode && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2808
+ if (direction) {
2234
2809
  e.preventDefault();
2810
+ var step = getDirectionStep(direction);
2811
+ var head = maxXY(addXY(editCell, step), ORIGIN);
2235
2812
  commitEditingCell();
2236
- var x1 = selection.x1;
2237
- var y1 = selection.y1;
2238
- var x2 = selection.x1;
2239
- var y2 = selection.y1;
2240
-
2241
- if (e.key === 'ArrowRight') {
2242
- x1 = selection.x1 + 1;
2243
- x2 = selection.x1 + 1;
2244
- } else if (e.key === 'ArrowLeft') {
2245
- x1 = selection.x1 - 1;
2246
- x2 = selection.x1 - 1;
2247
- } else if (e.key === 'ArrowUp') {
2248
- y1 = selection.y1 - 1;
2249
- y2 = selection.y1 - 1;
2250
- } else if (e.key === 'ArrowDown') {
2251
- y1 = selection.y1 + 1;
2252
- y2 = selection.y1 + 1;
2253
- }
2254
-
2255
- changeSelection(x1, y1, x2, y2);
2813
+ changeSelection([head, head]);
2256
2814
  }
2257
2815
  };
2258
2816
 
2259
2817
  var onGridKeyDown = function onGridKeyDown(e) {
2260
- if (editMode && arrowKeyCommitMode && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2261
- commitEditingCell();
2262
- return;
2263
- }
2818
+ console.log('onGridKeydown', e.key, e);
2264
2819
 
2265
- if (e.key === 'Shift') {
2266
- setShiftKeyDown(true);
2820
+ if (editMode && arrowKeyCommitMode && e.key in ARROW_KEYS) {
2821
+ commitEditingCell();
2267
2822
  return;
2268
2823
  }
2269
2824
 
@@ -2272,33 +2827,28 @@ function Sheet(props) {
2272
2827
  }
2273
2828
 
2274
2829
  if ((e.metaKey || e.ctrlKey) && String.fromCharCode(e.which).toLowerCase() === 'c') {
2830
+ var textArea = textAreaRef.current;
2831
+ textArea === null || textArea === void 0 ? void 0 : textArea.select();
2275
2832
  return;
2276
2833
  }
2277
2834
 
2278
2835
  if (e.key === 'Backspace' || e.key === 'Delete') {
2279
- var x1 = selection.x1;
2280
- var y1 = selection.y1;
2281
- var x2 = selection.x2;
2282
- var y2 = selection.y2;
2283
-
2284
- if (x1 > x2) {
2285
- x1 = selection.x2;
2286
- x2 = selection.x1;
2287
- }
2288
-
2289
- if (y1 > y2) {
2290
- y1 = selection.y2;
2291
- y2 = selection.y1;
2292
- }
2293
-
2294
- if (x1 === -1 && x2 === -1 && y1 !== -1 && y2 !== -1) {
2836
+ var _normalizeSelection2 = normalizeSelection(selection),
2837
+ _normalizeSelection2$ = _normalizeSelection2[0],
2838
+ x1 = _normalizeSelection2$[0],
2839
+ y1 = _normalizeSelection2$[1],
2840
+ _normalizeSelection2$2 = _normalizeSelection2[1],
2841
+ x2 = _normalizeSelection2$2[0],
2842
+ y2 = _normalizeSelection2$2[1];
2843
+
2844
+ if (isRowSelection(selection)) {
2295
2845
  x1 = 0;
2296
- x2 = maxSearchableRowOrCol;
2846
+ x2 = MAX_SEARCHABLE_INDEX;
2297
2847
  }
2298
2848
 
2299
- if (x1 !== -1 && x2 !== -1 && y1 === -1 && y2 === -1) {
2849
+ if (isColumnSelection(selection)) {
2300
2850
  y1 = 0;
2301
- y2 = maxSearchableRowOrCol;
2851
+ y2 = MAX_SEARCHABLE_INDEX;
2302
2852
  }
2303
2853
 
2304
2854
  var changes = [];
@@ -2320,171 +2870,95 @@ function Sheet(props) {
2320
2870
  return;
2321
2871
  }
2322
2872
 
2323
- if (selection.x1 === -1 || selection.x2 === -1 || selection.y1 === -1 || selection.y2 === -1) {
2873
+ if (isEmptySelection(selection)) {
2324
2874
  return;
2325
2875
  }
2326
2876
 
2327
2877
  if (e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode >= 65 && e.keyCode <= 90 || e.key === 'Enter' || e.key === '-' || e.key === '.' || e.key === ',') {
2328
- if (cellReadOnly(selection.x1, selection.y1)) {
2878
+ var cell = selection[0];
2879
+ var cellX = cell[0],
2880
+ cellY = cell[1];
2881
+
2882
+ if (cellReadOnly(cellX, cellY)) {
2329
2883
  e.preventDefault();
2330
2884
  return;
2331
2885
  }
2332
2886
 
2333
- startEditingCell({
2334
- x: selection.x1,
2335
- y: selection.y1
2336
- });
2337
- setArrowKeyCommitMode(e.key !== 'Enter');
2887
+ startEditingCell(cell, e.key !== 'Enter');
2338
2888
  return;
2339
2889
  }
2340
2890
 
2341
- if (['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2342
- var sel1 = {
2343
- x: selection.x1,
2344
- y: selection.y1
2345
- };
2346
- var sel2 = {
2347
- x: selection.x2,
2348
- y: selection.y2
2349
- };
2350
- var incr = {
2351
- x: 0,
2352
- y: 0
2353
- };
2354
- var direction = 'right';
2355
-
2356
- if (e.key === 'ArrowRight' || e.key === 'Tab') {
2357
- incr.x = 1;
2358
- direction = 'right';
2359
- } else if (e.key === 'ArrowLeft') {
2360
- incr.x = -1;
2361
- direction = 'left';
2362
- } else if (e.key === 'ArrowUp') {
2363
- incr.y = -1;
2364
- direction = 'up';
2365
- } else if (e.key === 'ArrowDown') {
2366
- incr.y = 1;
2367
- direction = 'down';
2368
- }
2891
+ if (e.key in ARROW_KEYS) {
2892
+ var anchor = selection[0],
2893
+ head = selection[1];
2894
+ var direction = ARROW_KEYS[e.key];
2895
+ var step = getDirectionStep(direction);
2369
2896
 
2370
2897
  if (e.metaKey || e.ctrlKey) {
2371
- var val = findInDisplayData(displayData, sel2, direction);
2372
- incr.x = val.x - sel2.x;
2373
- incr.y = val.y - sel2.y;
2374
- }
2375
-
2376
- sel2.x += incr.x;
2377
- sel2.y += incr.y;
2378
-
2379
- if (sel2.x < 0) {
2380
- sel2.x = 0;
2381
- }
2382
-
2383
- if (sel2.y < 0) {
2384
- sel2.y = 0;
2898
+ head = findInDisplayData(displayData, head, direction);
2899
+ } else {
2900
+ head = maxXY(addXY(head, step), ORIGIN);
2385
2901
  }
2386
2902
 
2387
2903
  if (!e.shiftKey) {
2388
- sel1 = _extends({}, sel2);
2904
+ anchor = head;
2389
2905
  }
2390
2906
 
2391
- changeSelection(sel1.x, sel1.y, sel2.x, sel2.y);
2907
+ changeSelection([anchor, head]);
2392
2908
  return;
2393
2909
  }
2394
2910
 
2395
2911
  e.preventDefault();
2396
2912
  };
2397
2913
 
2398
- var onGridKeyUp = function onGridKeyUp(e) {
2399
- setShiftKeyDown(e.shiftKey);
2400
- };
2401
-
2402
- var onContextMenu = function onContextMenu(e) {
2403
- var _props$onRightClick;
2404
-
2405
- if (!e.target || !(e.target instanceof Element)) {
2406
- return;
2407
- }
2408
-
2409
- var rect = e.target.getBoundingClientRect();
2410
- var x = e.clientX - rect.left;
2411
- var y = e.clientY - rect.top;
2412
- var cell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2413
- var cellX = cell.x;
2414
- var cellY = cell.y;
2415
- var x1 = selection.x1,
2416
- x2 = selection.x2,
2417
- y1 = selection.y1,
2418
- y2 = selection.y2;
2419
-
2420
- if (x1 > x2) {
2421
- var _ref = [x2, x1];
2422
- x1 = _ref[0];
2423
- x2 = _ref[1];
2424
- }
2425
-
2426
- if (y1 > y2) {
2427
- var _ref2 = [y2, y1];
2428
- y1 = _ref2[0];
2429
- y2 = _ref2[1];
2430
- }
2431
-
2432
- if (!(y > sheetStyle.columnHeaderHeight && x > sheetStyle.rowHeaderWidth)) {
2433
- return;
2434
- }
2435
-
2436
- if (!(cellX >= x1 && cellX <= x2) || !(cellY >= y1 && cellY <= y2)) {
2437
- changeSelection(cellX, cellY, cellX, cellY);
2438
- }
2439
-
2440
- onMouseMove(e);
2914
+ var editKey = editKeys ? editKeys.apply(void 0, editCell) : '';
2441
2915
 
2442
- var ev = _extends({}, e, {
2443
- cellX: cellX,
2444
- cellY: cellY
2445
- });
2916
+ var _useState10 = useState(editKey),
2917
+ lastEditKey = _useState10[0],
2918
+ setLastEditKey = _useState10[1];
2446
2919
 
2447
- (_props$onRightClick = props.onRightClick) === null || _props$onRightClick === void 0 ? void 0 : _props$onRightClick.call(props, ev);
2448
- };
2920
+ if (lastEditKey !== editKey) {
2921
+ setLastEditKey(editKey);
2922
+ setEditCell(NO_CELL);
2923
+ }
2449
2924
 
2450
- var editMode = editCell.x !== -1 && editCell.y !== -1;
2451
- var editTextPosition = {
2452
- x: 0,
2453
- y: 0
2454
- };
2925
+ var editTextPosition = ORIGIN;
2455
2926
  var editTextWidth = 0;
2456
2927
  var editTextHeight = 0;
2457
2928
  var editTextTextAlign = 'right';
2458
2929
 
2459
2930
  if (editMode) {
2460
- editTextPosition = cellToAbsCoordinate(editCell.x, editCell.y, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
2461
- var style = cellStyle(editCell.x, editCell.y);
2462
- editTextPosition.x += 1;
2463
- editTextPosition.y += 1;
2464
- editTextWidth = cellWidth(editCell.x) - 2;
2465
- editTextHeight = cellHeight(editCell.y) - 2;
2466
- editTextTextAlign = style.textAlign || defaultCellStyle.textAlign || 'left';
2931
+ var style = cellStyle.apply(void 0, editCell);
2932
+ editTextPosition = cellToPixel(editCell);
2933
+ editTextPosition = addXY(editTextPosition, ONE_ONE);
2934
+ editTextWidth = cellWidth(editCellX) - 3;
2935
+ editTextHeight = cellHeight(editCellY) - 3;
2936
+ editTextTextAlign = style.textAlign || DEFAULT_CELL_STYLE.textAlign || 'left';
2467
2937
  }
2468
2938
 
2939
+ var _editTextPosition = editTextPosition,
2940
+ textX = _editTextPosition[0],
2941
+ textY = _editTextPosition[1];
2469
2942
  var inputProps = {
2470
2943
  value: editValue,
2471
2944
  autoFocus: true,
2472
2945
  onKeyDown: onKeyDown,
2473
2946
  style: {
2474
2947
  position: 'absolute',
2475
- top: editTextPosition.y,
2476
- left: editTextPosition.x,
2948
+ left: textX,
2949
+ top: textY,
2950
+ padding: '0px 4px',
2477
2951
  width: editTextWidth,
2478
2952
  height: editTextHeight,
2479
2953
  outline: 'none',
2480
2954
  border: 'none',
2481
2955
  textAlign: editTextTextAlign,
2482
2956
  color: 'black',
2483
- fontSize: defaultCellStyle.fontSize,
2957
+ fontSize: DEFAULT_CELL_STYLE.fontSize,
2484
2958
  fontFamily: 'sans-serif'
2485
2959
  }
2486
2960
  };
2487
- var input = (_props$inputComponent = props.inputComponent) === null || _props$inputComponent === void 0 ? void 0 : _props$inputComponent.call(props, editCell.x, editCell.y, _extends({}, inputProps, {
2961
+ var input = (_props$inputComponent = props.inputComponent) === null || _props$inputComponent === void 0 ? void 0 : _props$inputComponent.call(props, editCellX, editCellY, _extends({}, inputProps, {
2488
2962
  onChange: setEditValue
2489
2963
  }), commitEditingCell);
2490
2964
  var overlayDivClassName = styles.sheetscroll;
@@ -2518,23 +2992,19 @@ function Sheet(props) {
2518
2992
  }, React.createElement("canvas", {
2519
2993
  style: canvasStyles,
2520
2994
  ref: canvasRef
2521
- }), React.createElement("div", {
2522
- ref: overlayRef,
2523
- onDoubleClick: onDoubleClick,
2524
- onMouseDown: onMouseDown,
2525
- onMouseMove: onMouseMove,
2526
- onMouseLeave: onMouseLeave,
2527
- onContextMenu: onContextMenu,
2995
+ }), React.createElement("div", Object.assign({
2996
+ ref: overlayRef
2997
+ }, mouseHandlers, {
2528
2998
  onScroll: onScroll,
2529
2999
  className: overlayDivClassName,
2530
3000
  style: overlayDivStyles
2531
- }, React.createElement("div", {
3001
+ }), React.createElement("div", {
2532
3002
  style: {
2533
3003
  position: 'absolute',
2534
3004
  left: 0,
2535
3005
  top: 0,
2536
3006
  width: 1,
2537
- height: maxScroll.y + 2000,
3007
+ height: maxScrollY + 2000,
2538
3008
  backgroundColor: 'rgba(0,0,0,0.0)'
2539
3009
  }
2540
3010
  }), React.createElement("div", {
@@ -2542,7 +3012,7 @@ function Sheet(props) {
2542
3012
  position: 'absolute',
2543
3013
  left: 0,
2544
3014
  top: 0,
2545
- width: maxScroll.x + 5000,
3015
+ width: maxScrollX + 5000,
2546
3016
  height: 1,
2547
3017
  backgroundColor: 'rgba(0,0,0,0.0)'
2548
3018
  }
@@ -2555,7 +3025,7 @@ function Sheet(props) {
2555
3025
  height: 1,
2556
3026
  opacity: 0.01
2557
3027
  },
2558
- ref: copyPasteTextAreaRef,
3028
+ ref: textAreaRef,
2559
3029
  autoComplete: "off",
2560
3030
  autoCorrect: "off",
2561
3031
  autoCapitalize: "off",
@@ -2564,8 +3034,7 @@ function Sheet(props) {
2564
3034
  return e.target.select();
2565
3035
  },
2566
3036
  tabIndex: 0,
2567
- onKeyDown: onGridKeyDown,
2568
- onKeyUp: onGridKeyUp
3037
+ onKeyDown: onGridKeyDown
2569
3038
  }), editMode && (input !== undefined ? input : React.createElement("input", Object.assign({}, inputProps, {
2570
3039
  type: "text",
2571
3040
  onFocus: function onFocus(e) {
@@ -2575,7 +3044,7 @@ function Sheet(props) {
2575
3044
  return setEditValue(e.target.value);
2576
3045
  }
2577
3046
  }))));
2578
- }
3047
+ });
2579
3048
 
2580
3049
  export default Sheet;
2581
3050
  //# sourceMappingURL=index.modern.js.map