sheet-happens 0.0.22 → 0.0.24

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,1822 +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);
725
-
726
- context.rect(_p1x, p1.y, 100000, p2.y - p1.y);
727
- } else if (colSelectionActive) {
728
- var _p1y = Math.max(-100, p1.y);
729
-
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
- }
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;
734
679
 
735
- context.stroke();
736
- }
680
+ var _size2 = rowToPixel(maxY, 1) - rowToPixel(minY);
737
681
 
738
- for (var _iterator7 = _createForOfIteratorHelperLoose(secondarySelections), _step7; !(_step7 = _iterator7()).done;) {
739
- var secondarySelection = _step7.value;
682
+ var _getScrollPosition3 = getScrollPosition(e),
683
+ _scroll2 = _getScrollPosition3[1];
740
684
 
741
- var _normalizeSelection2 = normalizeSelection(secondarySelection.span),
742
- _selx = _normalizeSelection2[0],
743
- _sely = _normalizeSelection2[1],
744
- _selx2 = _normalizeSelection2[2],
745
- _sely2 = _normalizeSelection2[3];
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
+ }
746
697
 
747
- var secondarySelectionActive = _selx !== -1 && _selx2 !== -1 || _sely !== -1 && _sely2 !== -1;
698
+ if (onCellHeightChange) {
699
+ for (var _iterator5 = _createForOfIteratorHelperLoose(rows), _step5; !(_step5 = _iterator5()).done;) {
700
+ var _index3 = _step5.value;
748
701
 
749
- if (!secondarySelectionActive) {
750
- continue;
751
- }
702
+ var _edge = rowToPixel(_index3, 1);
752
703
 
753
- var rowSecondarySelectionActive = _selx === -1 && _selx2 === -1 && _sely !== -1 && _sely2 !== -1;
754
- var colSecondarySelectionActive = _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';
755
706
 
756
- var _p = cellToAbsCoordinate(_selx, _sely, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
707
+ var _asGroup = isRowSelection(selection) && maxY === _index3;
757
708
 
758
- var _p2 = cellToAbsCoordinate(_selx2, _sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
709
+ var _indices3 = _asGroup ? selectedRows : [_index3];
759
710
 
760
- _p2.x += cellWidth(_selx2);
761
- _p2.y += cellHeight(_sely2);
711
+ var _size3 = _asGroup ? rowToPixel(maxY, 1) - rowToPixel(minY) : rowToPixel(_index3, 1) - rowToPixel(_index3);
762
712
 
763
- if (_p.x >= _p2.x) {
764
- _p2.x = _p.x;
765
- var _currentCol = _selx;
713
+ var _getScrollPosition4 = getScrollPosition(e),
714
+ _scroll3 = _getScrollPosition4[1];
766
715
 
767
- while (columnSizes.index.includes(_currentCol)) {
768
- _p2.x += cellWidth(_currentCol);
769
- _currentCol++;
716
+ setRowResize({
717
+ anchor: y,
718
+ scroll: _scroll3,
719
+ size: _size3,
720
+ indices: _indices3
721
+ });
722
+ return;
723
+ }
724
+ }
770
725
  }
771
726
  }
772
727
 
773
- if (_p.y >= _p2.y) {
774
- _p2.y = _p.y;
775
- var _currentRow = _sely;
728
+ if (knobPosition) {
729
+ var knobX = knobPosition[0],
730
+ knobY = knobPosition[1];
776
731
 
777
- while (rowSizes.index.includes(_currentRow)) {
778
- _p2.y += cellHeight(_currentRow);
779
- _currentRow++;
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;
780
736
  }
781
737
  }
782
738
 
783
- context.strokeStyle = secondarySelection.color;
784
- context.lineWidth = 1;
785
- context.beginPath();
786
-
787
- if (rowSecondarySelectionActive) {
788
- var _p1x2 = Math.max(-100, _p.x);
789
-
790
- context.rect(_p1x2, _p.y, 100000, _p2.y - _p.y);
791
- } else if (colSecondarySelectionActive) {
792
- var _p1y2 = Math.max(-100, _p.y);
739
+ var head = pixelToCell(xy);
740
+ var anchor = e.shiftKey ? [].concat(selection[0]) : head;
793
741
 
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);
797
- }
798
-
799
- context.stroke();
800
- }
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
+ }
819
+ }
801
820
 
802
- if (knobDragInProgress) {
803
- var kx1 = knobArea.x1;
804
- var kx2 = knobArea.x2;
821
+ if (rowDrag) {
822
+ var _indices4 = rowDrag.indices;
805
823
 
806
- if (knobArea.x1 > knobArea.x2) {
807
- kx1 = knobArea.x2;
808
- kx2 = knobArea.x1;
809
- }
824
+ var _insideSelection = cellY >= minY && cellY <= maxY + 1;
810
825
 
811
- var ky1 = knobArea.y1;
812
- var ky2 = knobArea.y2;
826
+ if (!_insideSelection) {
827
+ var _order = cellY > minY ? cellY - _indices4.length : cellY;
813
828
 
814
- if (knobArea.y1 > knobArea.y2) {
815
- ky1 = knobArea.y2;
816
- ky2 = knobArea.y1;
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
+ }
817
834
  }
818
835
 
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();
825
-
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);
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();
832
892
  }
833
893
 
834
- context.stroke();
835
- context.setLineDash([]);
836
- }
894
+ var columns = visibleCells.columns,
895
+ rows = visibleCells.rows;
896
+ var x = xy[0],
897
+ y = xy[1];
837
898
 
838
- if (selectionActive && !hideKnob) {
839
- context.fillStyle = selBorderColor;
840
- context.fillRect(knobCoordinates.x - knobSize * 0.5, knobCoordinates.y - knobSize * 0.5, knobSize, knobSize);
841
- }
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];
842
906
 
843
- context.textBaseline = 'middle';
844
- var yCoord = sheetStyle.columnHeaderHeight;
907
+ var isDragging = columnResize || columnDrag || rowResize || rowDrag || draggingRowSelection || draggingColumnSelection;
845
908
 
846
- for (var _iterator8 = _createForOfIteratorHelperLoose(rowSizes.index), _step8; !(_step8 = _iterator8()).done;) {
847
- var _y = _step8.value;
848
- var xCoord = sheetStyle.rowHeaderWidth;
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;
849
914
 
850
- var _ch = cellHeight(_y);
915
+ if (isInRange(x, start, end)) {
916
+ for (var _iterator6 = _createForOfIteratorHelperLoose(columns), _step6; !(_step6 = _iterator6()).done;) {
917
+ var index = _step6.value;
851
918
 
852
- for (var _iterator10 = _createForOfIteratorHelperLoose(columnSizes.index), _step10; !(_step10 = _iterator10()).done;) {
853
- var _x = _step10.value;
919
+ var _start4 = columnToPixel(index);
854
920
 
855
- var _cellContent = displayData(_x, _y);
921
+ var _end4 = columnToPixel(index, 1);
856
922
 
857
- var _cw = cellWidth(_x);
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
+ }
858
930
 
859
- if (_cellContent !== null && _cellContent !== undefined) {
860
- var _style = cellStyle(_x, _y);
931
+ if (onCellWidthChange) {
932
+ for (var _iterator7 = _createForOfIteratorHelperLoose(columns), _step7; !(_step7 = _iterator7()).done;) {
933
+ var _index4 = _step7.value;
934
+ var edge = columnToPixel(_index4, 1);
861
935
 
862
- drawCell(context, _cellContent, _style, defaultCellStyle, xCoord, yCoord, _cw, _ch);
936
+ if (Math.abs(edge - x) < SIZES.resizeZone && canSizeColumn(_index4)) {
937
+ window.document.body.style.cursor = 'col-resize';
938
+ return;
939
+ }
940
+ }
941
+ }
863
942
  }
864
943
 
865
- xCoord += _cw;
866
- }
944
+ if (!hideRowHeaders && x < getIndentX()) {
945
+ if (onRowOrderChange) {
946
+ var _start5 = rowToPixel(minY) + SIZES.resizeZone;
867
947
 
868
- yCoord += _ch;
869
- }
870
- }
948
+ var _end5 = rowToPixel(maxY, 1) - SIZES.resizeZone;
871
949
 
872
- function Sheet(props) {
873
- var _props$sheetStyle9, _props$sheetStyle10, _props$sheetStyle11, _props$sheetStyle12, _props$sheetStyle13, _props$sheetStyle14, _props$inputComponent;
950
+ if (isInRange(y, _start5, _end5)) {
951
+ for (var _iterator8 = _createForOfIteratorHelperLoose(rows), _step8; !(_step8 = _iterator8()).done;) {
952
+ var _index5 = _step8.value;
874
953
 
875
- var canvasRef = useRef(null);
876
- var overlayRef = useRef(null);
877
- var copyPasteTextAreaRef = useRef(null);
954
+ var _start6 = rowToPixel(_index5);
878
955
 
879
- var _useState = useState({
880
- x: 5000,
881
- y: 5000
882
- }),
883
- maxScroll = _useState[0],
884
- setMaxScroll = _useState[1];
956
+ var _end6 = rowToPixel(_index5, 1);
885
957
 
886
- var _useState2 = useState({
887
- x: 0,
888
- y: 0
889
- }),
890
- dataOffset = _useState2[0],
891
- setDataOffset = _useState2[1];
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
+ }
892
965
 
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];
966
+ if (onCellHeightChange) {
967
+ for (var _iterator9 = _createForOfIteratorHelperLoose(rows), _step9; !(_step9 = _iterator9()).done;) {
968
+ var _index6 = _step9.value;
901
969
 
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];
970
+ var _edge2 = rowToPixel(_index6, 1);
910
971
 
911
- var _useState5 = useState({
912
- x: -1,
913
- y: -1
914
- }),
915
- editCell = _useState5[0],
916
- setEditCell = _useState5[1];
972
+ if (Math.abs(_edge2 - y) < SIZES.resizeZone && canSizeRow(_index6)) {
973
+ window.document.body.style.cursor = 'row-resize';
974
+ return;
975
+ }
976
+ }
977
+ }
978
+ }
917
979
 
918
- var _useState6 = useState(''),
919
- editValue = _useState6[0],
920
- setEditValue = _useState6[1];
980
+ if (knobPosition) {
981
+ var knobX = knobPosition[0],
982
+ knobY = knobPosition[1];
921
983
 
922
- var _useState7 = useState(''),
923
- editKey = _useState7[0],
924
- setEditKey = _useState7[1];
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
+ }
989
+ }
925
990
 
926
- var _useState8 = useState(false),
927
- arrowKeyCommitMode = _useState8[0],
928
- setArrowKeyCommitMode = _useState8[1];
991
+ if (columnResize) {
992
+ if (onCellWidthChange) {
993
+ var size = columnResize.size,
994
+ anchor = columnResize.anchor,
995
+ scroll = columnResize.scroll,
996
+ indices = columnResize.indices;
929
997
 
930
- var _useState9 = useState(false),
931
- shiftKeyDown = _useState9[0],
932
- setShiftKeyDown = _useState9[1];
998
+ var _getScrollPosition5 = getScrollPosition(e),
999
+ currentScroll = _getScrollPosition5[0];
933
1000
 
934
- var _useState10 = useState(false),
935
- knobDragInProgress = _useState10[0],
936
- setKnobDragInProgress = _useState10[1];
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
+ }
937
1005
 
938
- var _useState11 = useState(false),
939
- selectionInProgress = _useState11[0],
940
- setSelectionInProgress = _useState11[1];
1006
+ return;
1007
+ }
941
1008
 
942
- var _useState12 = useState(null),
943
- columnResize = _useState12[0],
944
- setColumnResize = _useState12[1];
1009
+ if (rowResize) {
1010
+ if (onCellHeightChange) {
1011
+ var _size4 = rowResize.size,
1012
+ _anchor = rowResize.anchor,
1013
+ _scroll4 = rowResize.scroll,
1014
+ _indices5 = rowResize.indices;
945
1015
 
946
- var _useState13 = useState(null),
947
- rowResize = _useState13[0],
948
- setRowResize = _useState13[1];
1016
+ var _getScrollPosition6 = getScrollPosition(e),
1017
+ _currentScroll = _getScrollPosition6[1];
949
1018
 
950
- var _useState14 = useState(false),
951
- rowSelectionInProgress = _useState14[0],
952
- setRowSelectionInProgress = _useState14[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
+ }
953
1023
 
954
- var _useState15 = useState(false),
955
- columnSelectionInProgress = _useState15[0],
956
- setColumnSelectionInProgress = _useState15[1];
1024
+ return;
1025
+ }
957
1026
 
958
- var _useState16 = useState({
959
- x: -1,
960
- y: -1,
961
- hitTarget: null
962
- }),
963
- buttonClickMouseDownCoordinates = _useState16[0],
964
- setButtonClickMouseDownCoordinates = _useState16[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];
965
1034
 
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;
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
+ }
973
1043
 
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;
1044
+ if (draggingKnob) {
1045
+ window.document.body.style.cursor = 'crosshair';
1006
1046
 
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) : '';
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
+ }
1020
1075
 
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
-
1036
- var changeSelection = function changeSelection(x1, y1, x2, y2, scrollToP2) {
1037
- if (scrollToP2 === void 0) {
1038
- scrollToP2 = true;
1039
- }
1040
-
1041
- if (selection.x1 !== x1 || selection.x2 !== x2 || selection.y1 !== y1 || selection.y2 !== y2) {
1042
- setSelection({
1043
- x1: x1,
1044
- y1: y1,
1045
- x2: x2,
1046
- y2: y2
1047
- });
1076
+ onKnobAreaChange === null || onKnobAreaChange === void 0 ? void 0 : onKnobAreaChange([[_minX, _minY], [_maxX, _maxY]]);
1048
1077
  }
1049
1078
 
1050
- if (scrollToP2) {
1051
- var newDataOffset = {
1052
- x: dataOffset.x,
1053
- y: dataOffset.y
1054
- };
1055
- var newScrollLeft = -1;
1056
- var newScrollTop = -1;
1057
-
1058
- if (x2 !== -1 && (!columnSizes.index.includes(x2) || columnSizes.index[columnSizes.index.length - 1] === x2)) {
1059
- var lastVisibleColumnIndex = columnSizes.index[columnSizes.index.length - 1];
1060
- var firstVisibleColumnIndex = columnSizes.index[sheetStyle.freezeColumns];
1061
- var increment = 0;
1062
-
1063
- if (x2 >= lastVisibleColumnIndex) {
1064
- increment = 1 + x2 - lastVisibleColumnIndex;
1065
- } else if (x2 < firstVisibleColumnIndex) {
1066
- increment = x2 - firstVisibleColumnIndex;
1067
- }
1068
-
1069
- var newX = Math.max(dataOffset.x, sheetStyle.freezeColumns) + increment;
1070
- newDataOffset.x = newX;
1071
- newScrollLeft = newX * scrollSpeed;
1072
- }
1079
+ if (columnDrag || rowDrag) {
1080
+ var _x = xy[0],
1081
+ _y = xy[1];
1073
1082
 
1074
- if (y2 !== -1 && (!rowSizes.index.includes(y2) || rowSizes.index[rowSizes.index.length - 1] === y2)) {
1075
- var firstVisibleRowIndex = rowSizes.index[sheetStyle.freezeRows];
1076
- var lastVisibleRowIndex = rowSizes.index[rowSizes.index.length - 1];
1077
- var _increment = 0;
1083
+ if (columnDrag) {
1084
+ var _cellX = pixelToColumn(Math.max(_x, getIndentX()), 0.5);
1078
1085
 
1079
- if (y2 >= lastVisibleRowIndex) {
1080
- _increment = 1 + y2 - lastVisibleRowIndex;
1081
- } else if (y2 < firstVisibleRowIndex) {
1082
- _increment = y2 - firstVisibleRowIndex;
1083
- }
1086
+ var insideSelection = _cellX >= minX && _cellX <= maxX + 1;
1087
+ var _anchor3 = columnDrag.anchor,
1088
+ _scroll5 = columnDrag.scroll;
1089
+ var shift = _x - _anchor3;
1084
1090
 
1085
- var newY = Math.max(dataOffset.y, sheetStyle.freezeRows) + _increment;
1091
+ var _getScrollPosition7 = getScrollPosition(e),
1092
+ _currentScroll2 = _getScrollPosition7[0];
1086
1093
 
1087
- newDataOffset.y = newY;
1088
- newScrollTop = newY * scrollSpeed;
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]]);
1089
1096
  }
1090
1097
 
1091
- if (newDataOffset.x !== dataOffset.x || dataOffset.y !== newDataOffset.y) {
1092
- setDataOffset({
1093
- x: newDataOffset.x,
1094
- y: newDataOffset.y
1095
- });
1098
+ if (rowDrag) {
1099
+ var _cellY = pixelToRow(Math.max(_y, getIndentY()), 0.5);
1096
1100
 
1097
- var newMaxScroll = _extends({}, maxScroll);
1101
+ var _insideSelection2 = _cellY >= minY && _cellY <= maxY + 1;
1098
1102
 
1099
- newMaxScroll.x = Math.max(newMaxScroll.x, newScrollLeft);
1100
- newMaxScroll.y = Math.max(newMaxScroll.y, newScrollTop);
1101
- setMaxScroll(newMaxScroll);
1102
- setTimeout(function () {
1103
- if (overlayRef.current) {
1104
- if (newScrollLeft !== -1) {
1105
- overlayRef.current.scrollLeft = newScrollLeft;
1106
- }
1103
+ var _anchor4 = rowDrag.anchor,
1104
+ _scroll6 = rowDrag.scroll;
1107
1105
 
1108
- if (newScrollTop !== -1) {
1109
- overlayRef.current.scrollTop = newScrollTop;
1110
- }
1111
- }
1112
- }, 0);
1113
- }
1114
- }
1106
+ var _shift = _y - _anchor4;
1115
1107
 
1116
- if (props.onSelectionChanged) {
1117
- var sx1 = x1;
1118
- var sy1 = y1;
1119
- var sx2 = x2;
1120
- var sy2 = y2;
1121
-
1122
- if (sx1 > sx2) {
1123
- sx1 = x2;
1124
- sx2 = x1;
1125
- }
1108
+ var _getScrollPosition8 = getScrollPosition(e),
1109
+ _currentScroll3 = _getScrollPosition8[1];
1126
1110
 
1127
- if (sy1 > sy2) {
1128
- sy1 = y2;
1129
- sy2 = y1;
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]]);
1130
1113
  }
1114
+ }
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);
1131
1123
 
1132
- props.onSelectionChanged(sx1, sy1, sx2, sy2);
1124
+ if (hitTarget) {
1125
+ window.document.body.style.cursor = 'pointer';
1126
+ return;
1133
1127
  }
1134
- };
1135
1128
 
1136
- var knobCoordinates = useMemo(function () {
1137
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1138
- var sely2 = selection.y2;
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;
1145
+ }
1139
1146
 
1140
- if (selection.y1 > selection.y2) {
1141
- sely2 = selection.y1;
1142
- }
1147
+ var cell = pixelToCell(xy);
1143
1148
 
1144
- var c = cellToAbsCoordinate(0, sely2, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1145
- return {
1146
- x: c.x + knobSize * 0.5,
1147
- y: c.y + cellHeight(sely2)
1148
- };
1149
+ if (!isPointInsideSelection(selection, cell)) {
1150
+ onSelectionChange === null || onSelectionChange === void 0 ? void 0 : onSelectionChange([cell, cell]);
1149
1151
  }
1150
1152
 
1151
- if (selection.x1 !== -1 && selection.x2 !== -1 && selection.y1 === -1 && selection.y2 === -1) {
1152
- var selx2 = selection.x2;
1153
+ onPointerMove(e);
1154
+ var cellX = cell[0],
1155
+ cellY = cell[1];
1153
1156
 
1154
- if (selection.x1 > selection.x2) {
1155
- selx2 = selection.x1;
1156
- }
1157
+ var event = _extends({}, e, {
1158
+ cellX: cellX,
1159
+ cellY: cellY
1160
+ });
1157
1161
 
1158
- var _c = cellToAbsCoordinate(selx2, 0, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
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
+ };
1159
1177
 
1160
- return {
1161
- x: _c.x + cellWidth(selx2),
1162
- y: _c.y + knobSize * 0.5
1163
- };
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;
1164
1206
  }
1165
1207
 
1166
- if (selection.x2 !== -1 && selection.y2 !== -1) {
1167
- var _selx3 = selection.x2;
1208
+ if (fx1 === -1 && fx2 === -1) {
1209
+ var _findApproxMaxEditDat = findApproxMaxEditDataIndex(editData),
1210
+ maxX = _findApproxMaxEditDat[0];
1168
1211
 
1169
- if (selection.x1 > selection.x2) {
1170
- _selx3 = selection.x1;
1171
- }
1212
+ fx1 = 0;
1213
+ fx2 = maxX;
1214
+ }
1172
1215
 
1173
- var _sely3 = selection.y2;
1216
+ var srcY = sy1;
1174
1217
 
1175
- if (selection.y1 > selection.y2) {
1176
- _sely3 = selection.y1;
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
1228
+ }
1229
+ });
1177
1230
  }
1178
1231
 
1179
- var _c2 = cellToAbsCoordinate(_selx3, _sely3, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
1232
+ srcY = srcY + 1;
1180
1233
 
1181
- return {
1182
- x: _c2.x + cellWidth(_selx3),
1183
- y: _c2.y + cellHeight(_sely3)
1184
- };
1234
+ if (srcY > sy2) {
1235
+ srcY = sy1;
1236
+ }
1237
+ }
1238
+ } else {
1239
+ if (fx1 === sx1) {
1240
+ fx1 = sx2 + 1;
1241
+ } else {
1242
+ fx2 = sx1 - 1;
1185
1243
  }
1186
1244
 
1187
- return {
1188
- x: -1,
1189
- y: -1
1190
- };
1191
- }, [selection, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle]);
1192
- var hitMap = useMemo(function () {
1193
- var hitM = {};
1194
- var canvas = canvasRef.current;
1245
+ if (fy1 === -1 && fy2 === -1) {
1246
+ var _findApproxMaxEditDat2 = findApproxMaxEditDataIndex(editData),
1247
+ maxY = _findApproxMaxEditDat2[1];
1195
1248
 
1196
- if (!canvas) {
1197
- return hitM;
1249
+ fy1 = 0;
1250
+ fy2 = maxY;
1198
1251
  }
1199
1252
 
1200
- resizeCanvas(canvas);
1201
- var yCoord = sheetStyle.columnHeaderHeight;
1202
- var xCoord = sheetStyle.rowHeaderWidth;
1253
+ var srcX = sx1;
1203
1254
 
1204
- for (var _iterator11 = _createForOfIteratorHelperLoose(columnSizes.index), _step11; !(_step11 = _iterator11()).done;) {
1205
- var x = _step11.value;
1206
- var ch = columnHeaders(x);
1207
- var cellW = cellWidth(x);
1255
+ for (var _x2 = fx1; _x2 <= fx2; _x2++) {
1256
+ for (var _y2 = fy1; _y2 <= fy2; _y2++) {
1257
+ var _value = sourceData(srcX, _y2);
1208
1258
 
1209
- if (ch && typeof ch === 'object' && ch.items) {
1210
- var finalStyle = void 0;
1259
+ changes.push({
1260
+ x: _x2,
1261
+ y: _y2,
1262
+ value: _value,
1263
+ source: {
1264
+ x: srcX,
1265
+ y: _y2
1266
+ }
1267
+ });
1268
+ }
1211
1269
 
1212
- for (var _iterator13 = _createForOfIteratorHelperLoose(ch.items), _step13; !(_step13 = _iterator13()).done;) {
1213
- var obj = _step13.value;
1270
+ srcX = srcX + 1;
1214
1271
 
1215
- if (obj.onClick) {
1216
- if (!finalStyle) {
1217
- finalStyle = createStyleObject(columnHeaderStyle(x), defaultCellStyle);
1218
- }
1272
+ if (srcX > sx2) {
1273
+ srcX = sx1;
1274
+ }
1275
+ }
1276
+ }
1219
1277
 
1220
- var w = obj.content instanceof HTMLImageElement ? obj.width || cellW : 0;
1221
- var absX1 = applyAlignment(xCoord, cellW, finalStyle, w, obj.horiozntalAlign) + obj.x;
1222
- var absY1 = sheetStyle.columnHeaderHeight * 0.5 + obj.y;
1223
- var absX2 = absX1 + (obj.width || 0);
1224
- var absY2 = absY1 + (obj.height || 0);
1225
- var hitTarget = {
1226
- x: absX1,
1227
- y: absY1,
1228
- w: obj.width,
1229
- h: obj.height,
1230
- onClick: obj.onClick
1231
- };
1232
- var x1key = Math.floor(absX1 / xBinSize);
1233
- var x2key = Math.floor(absX2 / xBinSize);
1234
- var y1key = Math.floor(absY1 / yBinSize);
1235
- var y2key = Math.floor(absY2 / yBinSize);
1236
-
1237
- for (var xkey = x1key; xkey <= x2key; xkey++) {
1238
- if (!hitM[xkey]) {
1239
- hitM[xkey] = {};
1240
- }
1278
+ return changes;
1279
+ };
1280
+
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
+ }
1241
1286
 
1242
- var xbin = hitM[xkey];
1287
+ var xy = [e.target.scrollLeft, e.target.scrollTop];
1288
+ var absoluteToCell = cellLayout.absoluteToCell;
1289
+ var cell = absoluteToCell(xy);
1243
1290
 
1244
- for (var ykey = y1key; ykey <= y2key; ykey++) {
1245
- if (!xbin[ykey]) {
1246
- xbin[ykey] = [];
1247
- }
1291
+ if (!isSameXY(cell, offset)) {
1292
+ onOffsetChange === null || onOffsetChange === void 0 ? void 0 : onOffsetChange(cell);
1293
+ }
1248
1294
 
1249
- xbin[ykey].push(hitTarget);
1250
- }
1251
- }
1252
- }
1253
- }
1254
- }
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;
1255
1301
 
1256
- xCoord += cellW;
1302
+ if (growX > 1 || growY > 1) {
1303
+ onMaxScrollChange === null || onMaxScrollChange === void 0 ? void 0 : onMaxScrollChange(mulXY(maxScroll, [growX, growY]));
1257
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
+ }
1258
1337
 
1259
- for (var _iterator12 = _createForOfIteratorHelperLoose(rowSizes.index), _step12; !(_step12 = _iterator12()).done;) {
1260
- var y = _step12.value;
1261
- xCoord = sheetStyle.rowHeaderWidth;
1338
+ if (top <= frozenY) {
1339
+ newY = y;
1340
+ }
1262
1341
 
1263
- for (var _iterator14 = _createForOfIteratorHelperLoose(columnSizes.index), _step14; !(_step14 = _iterator14()).done;) {
1264
- var _x2 = _step14.value;
1265
- var cellContent = displayData(_x2, y);
1342
+ if (right > w) {
1343
+ var edge = right - w + columnToPixel(newX);
1266
1344
 
1267
- var _cellW = cellWidth(_x2);
1345
+ while (columnToPixel(++newX) < edge) {}
1346
+ }
1268
1347
 
1269
- if (cellContent === null || cellContent === undefined) {
1270
- xCoord += _cellW;
1271
- continue;
1272
- }
1348
+ if (bottom > h) {
1349
+ var _edge = bottom - h + rowToPixel(newY);
1273
1350
 
1274
- var xx = xCoord;
1275
- var yy = yCoord + cellHeight(y) * 0.5;
1351
+ while (rowToPixel(++newY) < _edge) {}
1352
+ }
1276
1353
 
1277
- if (typeof cellContent === 'object' && cellContent.items) {
1278
- var _finalStyle = void 0;
1354
+ var newOffset = [newX >= 0 ? newX : offsetX, newY >= 0 ? newY : offsetY];
1279
1355
 
1280
- for (var _iterator15 = _createForOfIteratorHelperLoose(cellContent.items), _step15; !(_step15 = _iterator15()).done;) {
1281
- var _obj = _step15.value;
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
+ };
1282
1367
 
1283
- if (_obj.onClick) {
1284
- if (!_finalStyle) {
1285
- _finalStyle = createStyleObject(cellStyle(_x2, y), defaultCellStyle);
1286
- }
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
+ };
1287
1386
 
1288
- var _w = _obj.content instanceof HTMLImageElement ? _obj.width || _cellW : 0;
1387
+ if (editMode) return;
1388
+ if (document.activeElement === textArea) return;
1389
+ var activeTagName = document.activeElement.tagName.toLowerCase();
1289
1390
 
1290
- var _absX = applyAlignment(xx, _cellW, _finalStyle, _w, _obj.horiozntalAlign) + _obj.x;
1391
+ if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1392
+ focus();
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
+ };
1291
1422
 
1292
- var _absY = yy + _obj.y;
1423
+ window.document.addEventListener('paste', onPaste);
1424
+ return function () {
1425
+ window.document.removeEventListener('paste', onPaste);
1426
+ };
1427
+ }, [textAreaRef, selection]);
1428
+ };
1293
1429
 
1294
- var _absX2 = _absX + (_obj.width || 0);
1430
+ var formatTSV = function formatTSV(rows) {
1431
+ return rows.map(function (row) {
1432
+ return row.join('\t');
1433
+ }).join('\n');
1434
+ };
1295
1435
 
1296
- var _absY2 = _absY + (_obj.height || 0);
1436
+ var formatSelectionAsTSV = function formatSelectionAsTSV(selection, editData) {
1437
+ if (isEmptySelection(selection)) return '';
1297
1438
 
1298
- var _hitTarget = {
1299
- x: _absX,
1300
- y: _absY,
1301
- w: _obj.width,
1302
- h: _obj.height,
1303
- onClick: _obj.onClick
1304
- };
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
+ }
1305
1454
 
1306
- var _x1key = Math.floor(_absX / xBinSize);
1455
+ if (isMaybeColumnSelection(selection)) {
1456
+ var _findApproxMaxEditDat2 = findApproxMaxEditDataIndex(editData),
1457
+ cellY = _findApproxMaxEditDat2[1];
1307
1458
 
1308
- var _x2key = Math.floor(_absX2 / xBinSize);
1459
+ minY = 0;
1460
+ maxY = cellY;
1461
+ }
1309
1462
 
1310
- var _y1key = Math.floor(_absY / yBinSize);
1463
+ var rows = [];
1311
1464
 
1312
- var _y2key = Math.floor(_absY2 / yBinSize);
1465
+ for (var y = minY; y <= maxY; y++) {
1466
+ var row = [];
1313
1467
 
1314
- for (var _xkey = _x1key; _xkey <= _x2key; _xkey++) {
1315
- if (!hitM[_xkey]) {
1316
- hitM[_xkey] = {};
1317
- }
1468
+ for (var x = minX; x <= maxX; x++) {
1469
+ var value = editData(x, y);
1318
1470
 
1319
- var _xbin = hitM[_xkey];
1471
+ if (value !== null && value !== undefined) {
1472
+ row.push(value != null ? value : '');
1473
+ }
1474
+ }
1320
1475
 
1321
- for (var _ykey = _y1key; _ykey <= _y2key; _ykey++) {
1322
- if (!_xbin[_ykey]) {
1323
- _xbin[_ykey] = [];
1324
- }
1476
+ rows.push(row);
1477
+ }
1325
1478
 
1326
- _xbin[_ykey].push(_hitTarget);
1327
- }
1328
- }
1329
- }
1330
- }
1331
- }
1479
+ return formatTSV(rows);
1480
+ };
1332
1481
 
1333
- xCoord += _cellW;
1334
- }
1482
+ var findTable = function findTable(element) {
1483
+ for (var _iterator = _createForOfIteratorHelperLoose(element.children), _step; !(_step = _iterator()).done;) {
1484
+ var child = _step.value;
1335
1485
 
1336
- yCoord += cellHeight(y);
1486
+ if (child.nodeName === 'TABLE') {
1487
+ return child;
1337
1488
  }
1338
1489
 
1339
- return hitM;
1340
- }, [displayData, props.cellWidth, props.cellHeight, canvasRef, columnSizes, rowSizes, dataOffset.x, dataOffset.y, sheetStyle]);
1341
- useEffect(function () {
1342
- var canvas = canvasRef.current;
1490
+ var maybeTable = findTable(child);
1343
1491
 
1344
- if (!canvas) {
1345
- return;
1492
+ if (maybeTable) {
1493
+ return maybeTable;
1346
1494
  }
1495
+ }
1496
+ };
1347
1497
 
1348
- var context = canvas.getContext('2d');
1498
+ var parsePastedHtml = function parsePastedHtml(selection, html) {
1499
+ var div = document.createElement('div');
1500
+ div.innerHTML = html.trim();
1349
1501
 
1350
- if (!context) {
1351
- return;
1352
- }
1502
+ var _normalizeSelection2 = normalizeSelection(selection),
1503
+ _normalizeSelection2$ = _normalizeSelection2[0],
1504
+ minX = _normalizeSelection2$[0],
1505
+ minY = _normalizeSelection2$[1];
1353
1506
 
1354
- var animationFrameId = window.requestAnimationFrame(function () {
1355
- renderOnCanvas(context, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections || [], knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle);
1356
- });
1357
- return function () {
1358
- window.cancelAnimationFrame(animationFrameId);
1359
- };
1360
- }, [canvasRef, rowSizes, columnSizes, cellStyle, cellWidth, cellHeight, selection, props.secondarySelections, knobDragInProgress, columnHeaders, columnHeaderStyle, knobArea, displayData, dataOffset, knobCoordinates, sheetStyle]);
1507
+ var left = isMaybeRowSelection(selection) ? 0 : minX;
1508
+ var top = isMaybeColumnSelection(selection) ? 0 : minY;
1509
+ var changes = [];
1510
+ var tableNode = findTable(div);
1361
1511
 
1362
- var setFocusToTextArea = function setFocusToTextArea() {
1363
- if (copyPasteTextAreaRef.current) {
1364
- copyPasteTextAreaRef.current.focus({
1365
- preventScroll: true
1366
- });
1367
- copyPasteTextAreaRef.current.select();
1368
- }
1369
- };
1512
+ if (!tableNode) {
1513
+ return null;
1514
+ }
1370
1515
 
1371
- useEffect(function () {
1372
- if (!editMode) {
1373
- setCopyPasteText();
1374
- }
1375
- }, [selection, editData]);
1376
- useEffect(function () {
1377
- if (!editMode) {
1378
- if (document.activeElement === copyPasteTextAreaRef.current) {
1379
- setFocusToTextArea();
1380
- } else {
1381
- var activeTagName = document.activeElement.tagName.toLowerCase();
1516
+ var right = left;
1517
+ var bottom = top;
1518
+ var y = top;
1382
1519
 
1383
- if (!(activeTagName === 'div' && document.activeElement.contentEditable === 'true' || activeTagName === 'input' || activeTagName === 'textarea' || activeTagName === 'select')) {
1384
- setFocusToTextArea();
1385
- }
1386
- }
1387
- }
1388
- });
1520
+ for (var _iterator2 = _createForOfIteratorHelperLoose(tableNode.children), _step2; !(_step2 = _iterator2()).done;) {
1521
+ var tableChild = _step2.value;
1389
1522
 
1390
- var onPaste = function onPaste(e) {
1391
- if (!copyPasteTextAreaRef) {
1392
- return;
1393
- }
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;
1394
1527
 
1395
- if (e.target !== copyPasteTextAreaRef.current) {
1396
- return;
1397
- }
1528
+ if (tr.nodeName === 'TR') {
1529
+ for (var _iterator4 = _createForOfIteratorHelperLoose(tr.children), _step4; !(_step4 = _iterator4()).done;) {
1530
+ var td = _step4.value;
1398
1531
 
1399
- e.preventDefault();
1400
- var clipboardData = e.clipboardData || window.clipboardData;
1401
- var types = clipboardData.types;
1532
+ if (td.nodeName === 'TD') {
1533
+ var str = '';
1402
1534
 
1403
- if (types.includes('text/html')) {
1404
- var pastedHtml = clipboardData.getData('text/html');
1405
- parsePastedHtml(pastedHtml);
1406
- } else if (types.includes('text/plain')) {
1407
- var text = clipboardData.getData('text/plain');
1408
- parsePastedText(text);
1409
- }
1410
- };
1535
+ if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1536
+ var p = td.children[0];
1411
1537
 
1412
- useEffect(function () {
1413
- window.document.addEventListener('paste', onPaste);
1414
- return function () {
1415
- window.document.removeEventListener('paste', onPaste);
1416
- };
1417
- });
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();
1542
+ }
1543
+ } else {
1544
+ str = td.textContent.trim();
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++;
1555
+ }
1556
+ }
1418
1557
 
1419
- var findTable = function findTable(element) {
1420
- for (var _iterator16 = _createForOfIteratorHelperLoose(element.children), _step16; !(_step16 = _iterator16()).done;) {
1421
- var child = _step16.value;
1558
+ y++;
1559
+ }
1422
1560
 
1423
- if (child.nodeName === 'TABLE') {
1424
- return child;
1561
+ right = Math.max(right, x);
1425
1562
  }
1563
+ }
1564
+ }
1426
1565
 
1427
- var maybeTable = findTable(child);
1566
+ bottom = y;
1567
+ return {
1568
+ selection: [[left, top], [right, bottom]],
1569
+ changes: changes
1570
+ };
1571
+ };
1428
1572
 
1429
- if (maybeTable) {
1430
- return maybeTable;
1431
- }
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
+ });
1432
1596
  }
1597
+ }
1598
+
1599
+ return {
1600
+ selection: [[left, top], [right, bottom]],
1601
+ changes: changes
1433
1602
  };
1603
+ };
1434
1604
 
1435
- var parsePastedHtml = function parsePastedHtml(html) {
1436
- var div = document.createElement('div');
1437
- div.innerHTML = html.trim();
1438
- var pasteLocX = -1;
1439
- var pasteLocY = -1;
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
+ };
1440
1617
 
1441
- if (selection.x1 !== -1 && selection.x2 === -1) {
1442
- pasteLocX = selection.x1;
1443
- }
1618
+ var getIndentY = function getIndentY() {
1619
+ return indentY;
1620
+ };
1621
+
1622
+ var getBaseOriginFor = function getBaseOriginFor(index, freeze, offset) {
1623
+ return index < freeze ? 0 : offset;
1624
+ };
1444
1625
 
1445
- if (selection.y1 !== -1 && selection.y2 === -1) {
1446
- pasteLocY = selection.y1;
1626
+ var columnToPixel = function columnToPixel(column, anchor) {
1627
+ if (anchor === void 0) {
1628
+ anchor = 0;
1447
1629
  }
1448
1630
 
1449
- if (selection.x1 !== -1 && selection.x2 !== -1) {
1450
- pasteLocX = Math.min(selection.x1, selection.x2);
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
+ };
1636
+
1637
+ var rowToPixel = function rowToPixel(row, anchor) {
1638
+ if (anchor === void 0) {
1639
+ anchor = 0;
1451
1640
  }
1452
1641
 
1453
- if (selection.y1 !== -1 && selection.y2 !== -1) {
1454
- pasteLocY = Math.min(selection.y1, selection.y2);
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;
1455
1651
  }
1456
1652
 
1457
- if (pasteLocX === -1 || pasteLocY === -1) {
1458
- return;
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
+ };
1660
+
1661
+ var columnToAbsolute = function columnToAbsolute(column, anchorX) {
1662
+ if (anchorX === void 0) {
1663
+ anchorX = 0;
1459
1664
  }
1460
1665
 
1461
- var x = pasteLocX;
1462
- var y = pasteLocY;
1463
- var changes = [];
1464
- var tableNode = findTable(div);
1666
+ var relative = columns.getStart(column);
1667
+ var size = column < 0 ? 0 : columns.getSize(column);
1668
+ return relative + anchorX * size;
1669
+ };
1465
1670
 
1466
- if (!tableNode) {
1467
- return;
1671
+ var rowToAbsolute = function rowToAbsolute(row, anchorY) {
1672
+ if (anchorY === void 0) {
1673
+ anchorY = 0;
1468
1674
  }
1469
1675
 
1470
- for (var _iterator17 = _createForOfIteratorHelperLoose(tableNode.children), _step17; !(_step17 = _iterator17()).done;) {
1471
- var tableChild = _step17.value;
1676
+ var relative = rows.getStart(row);
1677
+ var size = row < 0 ? 0 : rows.getSize(row);
1678
+ return relative + anchorY * size;
1679
+ };
1472
1680
 
1473
- if (tableChild.nodeName === 'TBODY') {
1474
- for (var _iterator18 = _createForOfIteratorHelperLoose(tableChild.children), _step18; !(_step18 = _iterator18()).done;) {
1475
- var tr = _step18.value;
1476
- x = pasteLocX;
1681
+ var cellToAbsolute = function cellToAbsolute(cell, anchor) {
1682
+ if (anchor === void 0) {
1683
+ anchor = ORIGIN;
1684
+ }
1477
1685
 
1478
- if (tr.nodeName === 'TR') {
1479
- for (var _iterator19 = _createForOfIteratorHelperLoose(tr.children), _step19; !(_step19 = _iterator19()).done;) {
1480
- var td = _step19.value;
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
+ };
1481
1693
 
1482
- if (td.nodeName === 'TD') {
1483
- var str = '';
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);
1484
1700
 
1485
- if (td.children.length !== 0 && td.children[0].nodeName === 'P') {
1486
- var p = td.children[0];
1701
+ if (relative < frozen) {
1702
+ return lookupIndex(relative, anchor);
1703
+ } else {
1704
+ var base = getStart(offset);
1705
+ return lookupIndex(base + relative, anchor);
1706
+ }
1707
+ };
1487
1708
 
1488
- if (p.children.length !== 0 && p.children[0].nodeName === 'FONT') {
1489
- str = p.children[0].textContent.trim();
1490
- } else {
1491
- str = p.textContent.trim();
1492
- }
1493
- } else {
1494
- str = td.textContent.trim();
1495
- }
1709
+ var pixelToColumn = function pixelToColumn(pixelX, anchorX) {
1710
+ if (anchorX === void 0) {
1711
+ anchorX = 0;
1712
+ }
1496
1713
 
1497
- str = str.replaceAll('\n', '');
1498
- str = str.replaceAll(/\s\s+/g, ' ');
1499
- changes.push({
1500
- y: y,
1501
- x: x,
1502
- value: str
1503
- });
1504
- x++;
1505
- }
1506
- }
1714
+ return pixelToIndex(pixelX, anchorX, indentX, freezeX, offsetX, columns);
1715
+ };
1507
1716
 
1508
- y++;
1509
- }
1510
- }
1511
- }
1717
+ var pixelToRow = function pixelToRow(pixelY, anchorY) {
1718
+ if (anchorY === void 0) {
1719
+ anchorY = 0;
1512
1720
  }
1513
1721
 
1514
- if (props.onChange) {
1515
- props.onChange(changes);
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;
1516
1728
  }
1517
1729
 
1518
- var pasteX2 = x - 1;
1519
- var pasteY2 = y - 1;
1520
- changeSelection(pasteLocX, pasteLocY, pasteX2, pasteY2, false);
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)];
1521
1736
  };
1522
1737
 
1523
- var parsePastedText = function parsePastedText(text) {
1524
- var pasteLocX = -1;
1525
- var pasteLocY = -1;
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
+ };
1526
1743
 
1527
- if (selection.x1 !== -1 && selection.x2 === -1) {
1528
- pasteLocX = selection.x1;
1744
+ var absoluteToColumn = function absoluteToColumn(pixelX, anchorX) {
1745
+ if (anchorX === void 0) {
1746
+ anchorX = 0;
1529
1747
  }
1530
1748
 
1531
- if (selection.x1 !== -1 && selection.x2 !== -1) {
1532
- pasteLocX = Math.min(selection.x1, selection.x2);
1533
- }
1749
+ return absoluteToIndex(pixelX, anchorX, columns);
1750
+ };
1534
1751
 
1535
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1536
- pasteLocX = 0;
1752
+ var absoluteToRow = function absoluteToRow(pixelY, anchorY) {
1753
+ if (anchorY === void 0) {
1754
+ anchorY = 0;
1537
1755
  }
1538
1756
 
1539
- if (selection.y1 !== -1 && selection.y2 === -1) {
1540
- pasteLocY = selection.y1;
1541
- }
1757
+ return absoluteToIndex(pixelY, anchorY, rows);
1758
+ };
1542
1759
 
1543
- if (selection.y1 !== -1 && selection.y2 !== -1) {
1544
- pasteLocY = Math.min(selection.y1, selection.y2);
1760
+ var absoluteToCell = function absoluteToCell(pixel, anchor) {
1761
+ if (anchor === void 0) {
1762
+ anchor = ORIGIN;
1545
1763
  }
1546
1764
 
1547
- if (selection.y1 === -1 && selection.y2 === -1 && selection.x2 !== -1 && selection.x2 !== -1) {
1548
- pasteLocY = 0;
1549
- }
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
+ };
1550
1772
 
1551
- if (pasteLocX === -1 || pasteLocY === -1) {
1552
- return;
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);
1777
+
1778
+ for (var i = offset + freeze; getStart(i) <= relative; ++i) {
1779
+ indices.push(i);
1553
1780
  }
1554
1781
 
1555
- var rows = text.split(/\r?\n/);
1556
- var pasteX2 = pasteLocX;
1557
- var pasteY2 = pasteLocY + rows.length - 1;
1558
- var changes = [];
1782
+ return indices;
1783
+ };
1559
1784
 
1560
- for (var y = 0; y < rows.length; y++) {
1561
- var cols = rows[y].split('\t');
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
+ };
1562
1793
 
1563
- if (pasteLocX + cols.length - 1 > pasteX2) {
1564
- pasteX2 = pasteLocX + cols.length - 1;
1565
- }
1794
+ var getVersion = function getVersion() {
1795
+ return columns.getVersion() + rows.getVersion();
1796
+ };
1566
1797
 
1567
- for (var x = 0; x < cols.length; x++) {
1568
- changes.push({
1569
- y: pasteLocY + y,
1570
- x: pasteLocX + x,
1571
- value: cols[x]
1572
- });
1573
- }
1574
- }
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
+ };
1575
1830
 
1576
- if (props.onChange) {
1577
- props.onChange(changes);
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;
1835
+
1836
+ while (j < i) {
1837
+ var size = getSize(j);
1838
+ var offset = (offsets.get(j) || 0) + size;
1839
+ offsets.set(++j, offset);
1578
1840
  }
1579
1841
 
1580
- changeSelection(pasteLocX, pasteLocY, pasteX2, pasteY2, false);
1842
+ return offsets.get(i);
1581
1843
  };
1582
1844
 
1583
- var setCopyPasteText = function setCopyPasteText() {
1584
- if (selection.x1 === -1 && selection.y1 === -1 && selection.x2 === -1 && selection.y2 === -1) {
1585
- return;
1586
- }
1845
+ var getStart = function getStart(i) {
1846
+ return getOffset(i);
1847
+ };
1587
1848
 
1588
- var dy1 = selection.y1;
1589
- var dy2 = selection.y2;
1849
+ var getEnd = function getEnd(i) {
1850
+ return getOffset(i + 1);
1851
+ };
1590
1852
 
1591
- if (dy1 > dy2) {
1592
- dy1 = selection.y2;
1593
- dy2 = selection.y1;
1853
+ var lookupIndex = function lookupIndex(x, anchor) {
1854
+ if (anchor === void 0) {
1855
+ anchor = 0;
1594
1856
  }
1595
1857
 
1596
- var dx1 = selection.x1;
1597
- var dx2 = selection.x2;
1858
+ var last = offsets.tail() || 0;
1598
1859
 
1599
- if (dx1 > dx2) {
1600
- dx1 = selection.x2;
1601
- dx2 = selection.x1;
1860
+ while (getOffset(last) < x && getSize(last)) {
1861
+ last += 64;
1602
1862
  }
1603
1863
 
1604
- if (dx1 === -1 && dx2 === -1) {
1605
- var max = findApproxMaxEditDataIndex(editData);
1606
- dx1 = 0;
1607
- dx2 = max.x;
1864
+ var start = 0;
1865
+ var end = last;
1866
+
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;
1608
1871
  }
1609
1872
 
1610
- if (dy1 === -1 && dy2 === -1) {
1611
- var _max = findApproxMaxEditDataIndex(editData);
1873
+ return start;
1874
+ };
1612
1875
 
1613
- dy1 = 0;
1614
- dy2 = _max.y;
1615
- }
1876
+ var clearAfter = function clearAfter(index) {
1877
+ index = Math.max(0, index);
1878
+ offsets.truncate(index);
1879
+ sizes.truncate(index);
1880
+ version++;
1881
+ };
1616
1882
 
1617
- var cptext = '';
1618
- var cptextTemp = '';
1883
+ var setSizer = function setSizer(s) {
1884
+ sizer = s;
1885
+ };
1619
1886
 
1620
- for (var y = dy1; y <= dy2; y++) {
1621
- var nonEmpty = false;
1887
+ var getVersion = function getVersion() {
1888
+ return version;
1889
+ };
1622
1890
 
1623
- for (var x = dx1; x <= dx2; x++) {
1624
- var value = editData(x, y);
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
+ };
1625
1901
 
1626
- if (value !== null && value !== undefined) {
1627
- cptextTemp += value;
1628
- nonEmpty = true;
1629
- }
1902
+ var makeIntMap = function makeIntMap(initialSize) {
1903
+ if (initialSize === void 0) {
1904
+ initialSize = 128;
1905
+ }
1630
1906
 
1631
- if (x !== dx2) {
1632
- cptextTemp += '\t';
1633
- }
1634
- }
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;
1919
+ };
1635
1920
 
1636
- if (y !== dy2) {
1637
- cptextTemp += '\n';
1638
- }
1921
+ allocate(initialSize);
1639
1922
 
1640
- if (nonEmpty) {
1641
- cptext += cptextTemp;
1642
- cptextTemp = '';
1643
- }
1644
- }
1923
+ var copy = function copy(from, to) {
1924
+ var n = Math.min(from.length, to.length);
1645
1925
 
1646
- if (copyPasteTextAreaRef.current) {
1647
- copyPasteTextAreaRef.current.value = cptext;
1926
+ for (var i = 0; i < n; ++i) {
1927
+ to[i] = from[i];
1648
1928
  }
1649
1929
  };
1650
1930
 
1651
- var commitEditingCell = function commitEditingCell(value) {
1652
- if (props.onChange) {
1653
- props.onChange([{
1654
- x: editCell.x,
1655
- y: editCell.y,
1656
- value: value !== undefined ? value : editValue
1657
- }]);
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
+ };
1936
+
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);
1945
+
1946
+ while (last > 0 && !used[last]) {
1947
+ last--;
1658
1948
  }
1949
+ };
1659
1950
 
1660
- setEditCell({
1661
- x: -1,
1662
- y: -1
1663
- });
1664
- setEditKey('');
1951
+ var getTail = function getTail() {
1952
+ return used[last] ? last : null;
1665
1953
  };
1666
1954
 
1667
- var startEditingCell = function startEditingCell(editCell) {
1668
- if (cellReadOnly(editCell.x, editCell.y)) {
1669
- return;
1670
- }
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
+ };
1671
1961
 
1672
- var editDataValue = editData(editCell.x, editCell.y);
1673
- var val = '';
1962
+ var getValue = function getValue(i) {
1963
+ return used[i] ? values[i] : null;
1964
+ };
1674
1965
 
1675
- if (editDataValue !== null && editDataValue !== undefined) {
1676
- val = editDataValue;
1677
- }
1966
+ var hasValue = function hasValue(i) {
1967
+ return !!used[i];
1968
+ };
1678
1969
 
1679
- var editDataKey = editKeys ? editKeys(editCell.x, editCell.y) : '';
1680
- setEditCell(editCell);
1681
- setEditKey(editDataKey);
1682
- setEditValue(val);
1683
- setShiftKeyDown(false);
1970
+ return {
1971
+ truncate: truncate,
1972
+ set: setValue,
1973
+ get: getValue,
1974
+ has: hasValue,
1975
+ tail: getTail
1684
1976
  };
1977
+ };
1685
1978
 
1686
- var onScroll = function onScroll(e) {
1687
- if (!e.target || !(e.target instanceof Element)) {
1688
- return;
1689
- }
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
+ }
1690
1998
 
1691
- var absX = e.target.scrollLeft;
1692
- var absY = e.target.scrollTop;
1693
- var cellX = Math.floor(absX / scrollSpeed);
1694
- var cellY = Math.floor(absY / scrollSpeed);
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
+ }
1695
2006
 
1696
- if (cellX !== dataOffset.x || cellY !== dataOffset.y) {
1697
- setDataOffset({
1698
- x: cellX,
1699
- y: cellY
1700
- });
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);
1701
2032
 
1702
- if (props.onScrollChange) {
1703
- props.onScrollChange(cellX, cellY);
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);
2040
+
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);
1704
2053
  }
1705
2054
  }
2055
+ }
1706
2056
 
1707
- var newMaxScroll = _extends({}, maxScroll);
2057
+ var selectionActive = !isEmptySelection(selection);
2058
+ var rowSelectionActive = isRowSelection(selection);
2059
+ var columnSelectionActive = isColumnSelection(selection);
1708
2060
 
1709
- if (maxScroll.x / (absX + 0.5) < 1) {
1710
- newMaxScroll.x *= 1.5;
1711
- }
2061
+ var _resolveFrozenSelecti = resolveFrozenSelection(selection, cellLayout, freeze, indent, dataOffset),
2062
+ selected = _resolveFrozenSelecti[0],
2063
+ hideKnob = _resolveFrozenSelecti[1];
1712
2064
 
1713
- if (maxScroll.y / (absY + 0.5) < 1) {
1714
- newMaxScroll.y *= 1.5;
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
+ }
2075
+
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);
1715
2087
  }
2088
+ }
2089
+
2090
+ if (!hideColumnHeaders) {
2091
+ context.fillStyle = COLORS.headerBackground;
2092
+ context.fillRect(0, 0, context.canvas.width, columnHeaderHeight);
1716
2093
 
1717
- if (newMaxScroll.x !== maxScroll.x || maxScroll.y !== newMaxScroll.y) {
1718
- setMaxScroll(_extends({}, newMaxScroll));
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);
1719
2101
  }
2102
+ }
2103
+
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();
1720
2114
  };
1721
2115
 
1722
- var onMouseLeave = function onMouseLeave() {
1723
- window.document.body.style.cursor = 'auto';
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();
1724
2121
  };
1725
2122
 
1726
- var onMouseDown = function onMouseDown(e) {
1727
- if (e.button !== 0) {
1728
- return;
1729
- }
2123
+ drawGridLineX(rowHeaderWidth, context.canvas.height);
2124
+ drawGridLineY(columnHeaderHeight, context.canvas.width);
1730
2125
 
1731
- if (!e.target || !(e.target instanceof Element)) {
1732
- return;
1733
- }
2126
+ for (var _iterator2 = _createForOfIteratorHelperLoose(columns), _step2; !(_step2 = _iterator2()).done;) {
2127
+ var _column = _step2.value;
1734
2128
 
1735
- setSelectionInProgress(false);
1736
- var rect = e.target.getBoundingClientRect();
1737
- var x = e.clientX - rect.left;
1738
- var y = e.clientY - rect.top;
2129
+ var _right8 = columnToPixel(_column, 1);
1739
2130
 
1740
- if (x > canvasWidth || y > canvasHeight) {
1741
- return;
1742
- }
2131
+ drawGridLineX(_right8, gridBottom);
2132
+ }
1743
2133
 
1744
- var hitTargetKeyX = Math.floor(x / xBinSize);
1745
- var hitTargetKeyY = Math.floor(y / yBinSize);
2134
+ for (var _iterator3 = _createForOfIteratorHelperLoose(rows), _step3; !(_step3 = _iterator3()).done;) {
2135
+ var _row = _step3.value;
1746
2136
 
1747
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
1748
- for (var _iterator20 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step20; !(_step20 = _iterator20()).done;) {
1749
- var hitTarget = _step20.value;
2137
+ var _bottom8 = rowToPixel(_row, 1);
1750
2138
 
1751
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
1752
- setButtonClickMouseDownCoordinates({
1753
- x: x,
1754
- y: y,
1755
- hitTarget: hitTarget
1756
- });
1757
- return;
1758
- }
1759
- }
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);
2164
+
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));
1760
2168
  }
2169
+ }
1761
2170
 
1762
- if (!sheetStyle.hideColumnHeaders && y < sheetStyle.columnHeaderHeight) {
1763
- for (var colIdx = 0; colIdx < columnSizes.index.length; colIdx++) {
1764
- var start = columnSizes.start[colIdx];
1765
- var end = columnSizes.end[colIdx];
1766
- var index = columnSizes.index[colIdx];
2171
+ if (!hideColumnHeaders) {
2172
+ context.textBaseline = 'middle';
2173
+ context.textAlign = 'center';
1767
2174
 
1768
- if (Math.abs(start - x) < resizeColumnRowMouseThreshold || Math.abs(end - x) < resizeColumnRowMouseThreshold) {
1769
- window.document.body.style.cursor = 'col-resize';
1770
- var indices = [];
2175
+ for (var _iterator5 = _createForOfIteratorHelperLoose(columns), _step5; !(_step5 = _iterator5()).done;) {
2176
+ var _columnHeaders;
1771
2177
 
1772
- if (selection.x1 !== -1 && selection.x2 !== -1 && selection.y1 === -1 && selection.y2 === -1) {
1773
- var min = Math.min(selection.x1, selection.x2);
1774
- var max = Math.max(selection.x1, selection.x2);
2178
+ var column = _step5.value;
1775
2179
 
1776
- for (var i = min; i <= max; i++) {
1777
- indices.push(i);
1778
- }
1779
- }
2180
+ var _content = (_columnHeaders = columnHeaders(column)) != null ? _columnHeaders : excelHeaderString(column + 1);
1780
2181
 
1781
- if (indices.length === 0) {
1782
- indices.push(index);
1783
- }
2182
+ var _isActive = isInRange(column, minX, maxX);
1784
2183
 
1785
- setColumnResize({
1786
- startX: end,
1787
- oldWidth: cellWidth(index),
1788
- colIndices: indices
1789
- });
1790
- return;
1791
- }
1792
- }
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);
2188
+
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));
1793
2194
  }
2195
+ }
1794
2196
 
1795
- if (!sheetStyle.hideRowHeaders && x < sheetStyle.rowHeaderWidth) {
1796
- for (var rowIdx = 0; rowIdx < rowSizes.index.length; rowIdx++) {
1797
- var _start = rowSizes.start[rowIdx];
1798
- var _end = rowSizes.end[rowIdx];
1799
- var _index = rowSizes.index[rowIdx];
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
+ }
1800
2208
 
1801
- if (Math.abs(_start - y) < resizeColumnRowMouseThreshold || Math.abs(_end - y) < resizeColumnRowMouseThreshold) {
1802
- window.document.body.style.cursor = 'row-resize';
1803
- var _indices = [];
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;
1804
2213
 
1805
- if (selection.x1 === -1 && selection.x2 === -1 && selection.y1 !== -1 && selection.y2 !== -1) {
1806
- var _min = Math.min(selection.y1, selection.y2);
2214
+ var _resolveFrozenSelecti2 = resolveFrozenSelection(_selection, cellLayout, freeze, indent, dataOffset),
2215
+ _selected = _resolveFrozenSelecti2[0];
1807
2216
 
1808
- var _max2 = Math.max(selection.y1, selection.y2);
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
+ }
1809
2228
 
1810
- for (var _i5 = _min; _i5 <= _max2; _i5++) {
1811
- _indices.push(_i5);
1812
- }
1813
- }
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];
1814
2237
 
1815
- if (_indices.length === 0) {
1816
- _indices.push(_index);
1817
- }
2238
+ var _left4 = columnToPixel(_minX);
1818
2239
 
1819
- setRowResize({
1820
- startY: _end,
1821
- oldHeight: cellHeight(_index),
1822
- rowIndices: _indices
1823
- });
1824
- return;
1825
- }
1826
- }
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;
1827
2290
  }
1828
2291
 
1829
- if (Math.abs(x - knobCoordinates.x) < knobSize && Math.abs(y - knobCoordinates.y) < knobSize) {
1830
- setKnobDragInProgress(true);
1831
- setKnobArea({
1832
- x1: selection.x1,
1833
- y1: selection.y1,
1834
- x2: selection.x2,
1835
- y2: selection.y2
1836
- });
1837
- return;
2292
+ if (isRowSelection(dropTarget)) {
2293
+ _bottom6 = _top6;
1838
2294
  }
1839
2295
 
1840
- var sel2 = absCoordianteToCell(x, y, rowSizes, columnSizes);
1841
- var sel1 = shiftKeyDown ? {
1842
- x: selection.x1,
1843
- y: selection.y1
1844
- } : _extends({}, sel2);
2296
+ context.strokeRect(_left6 - 1, _top6 - 1, _right6 - _left6, _bottom6 - _top6);
2297
+ }
1845
2298
 
1846
- if (editMode) {
1847
- if (!props.dontCommitEditOnSelectionChange) {
1848
- commitEditingCell();
2299
+ context.textBaseline = 'middle';
2300
+
2301
+ for (var _iterator7 = _createForOfIteratorHelperLoose(rows), _step7; !(_step7 = _iterator7()).done;) {
2302
+ var _y = _step7.value;
2303
+
2304
+ for (var _iterator9 = _createForOfIteratorHelperLoose(columns), _step9; !(_step9 = _iterator9()).done;) {
2305
+ var _x = _step9.value;
2306
+
2307
+ var _left9 = columnToPixel(_x);
2308
+
2309
+ var _right10 = columnToPixel(_x, 1);
2310
+
2311
+ var _top9 = rowToPixel(_y);
2312
+
2313
+ var _bottom10 = rowToPixel(_y, 1);
2314
+
2315
+ var cellContent = displayData(_x, _y);
2316
+
2317
+ if (cellContent !== null && cellContent !== undefined) {
2318
+ var _style2 = cellStyle(_x, _y);
2319
+
2320
+ clickables.push.apply(clickables, renderCell(context, cellContent, _style2, DEFAULT_CELL_STYLE, _left9, _top9, _right10 - _left9, _bottom10 - _top9));
1849
2321
  }
1850
2322
  }
2323
+ }
1851
2324
 
1852
- var scrollToP2 = true;
2325
+ return clickables;
2326
+ };
2327
+ var renderCell = function renderCell(context, cellContent, style, defaultCellStyle, xCoord, yCoord, cellWidth, cellHeight) {
2328
+ var clickables = [];
1853
2329
 
1854
- if (!sheetStyle.hideRowHeaders && x < sheetStyle.rowHeaderWidth) {
1855
- scrollToP2 = false;
1856
- setRowSelectionInProgress(true);
1857
- sel1.x = -1;
1858
- sel2.x = -1;
1859
- } else {
1860
- setRowSelectionInProgress(false);
1861
- }
2330
+ if (cellContent === null) {
2331
+ return clickables;
2332
+ }
1862
2333
 
1863
- if (!sheetStyle.hideColumnHeaders && y < sheetStyle.columnHeaderHeight) {
1864
- scrollToP2 = false;
1865
- setColumnSelectionInProgress(true);
1866
- sel1.y = -1;
1867
- sel2.y = -1;
1868
- } else {
1869
- setColumnSelectionInProgress(false);
1870
- }
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();
1871
2343
 
1872
- setSelectionInProgress(true);
1873
- changeSelection(sel1.x, sel1.y, sel2.x, sel2.y, scrollToP2);
2344
+ if (finalStyle.backgroundColor !== '') {
2345
+ context.fillStyle = finalStyle.backgroundColor;
2346
+ context.fillRect(xCoord, yCoord, cellWidth, cellHeight);
2347
+ context.fillStyle = finalStyle.color;
2348
+ }
1874
2349
 
1875
- if (!props.dontCommitEditOnSelectionChange) {
1876
- setEditCell({
1877
- x: -1,
1878
- y: -1
1879
- });
1880
- setEditKey('');
1881
- }
1882
- };
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;
1883
2361
 
1884
- var onMouseUp = function onMouseUp(e) {
1885
- if (knobDragInProgress) {
1886
- var sx1 = selection.x1;
1887
- var sx2 = selection.x2;
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;
2372
+ }
1888
2373
 
1889
- if (selection.x1 > selection.x2) {
1890
- sx1 = selection.x2;
1891
- sx2 = selection.x1;
1892
- }
2374
+ var _finalX = applyAlignment(xCoord, cellWidth, finalStyle, 0, obj.horizontalAlign);
1893
2375
 
1894
- var sy1 = selection.y1;
1895
- var sy2 = selection.y2;
2376
+ var _text = '' + obj.content;
2377
+
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
+ }
1896
2387
 
1897
- if (selection.y1 > selection.y2) {
1898
- sy1 = selection.y2;
1899
- sy2 = selection.y1;
2388
+ if (obj.onClick) {
2389
+ clickables.push({
2390
+ rect: [[x, y], [x + w, y + h]],
2391
+ obj: obj
2392
+ });
1900
2393
  }
2394
+ }
2395
+ }
2396
+
2397
+ context.restore();
2398
+ return clickables;
2399
+ };
2400
+
2401
+ var resolveSelection = function resolveSelection(selection, cellLayout) {
2402
+ var cellToPixel = cellLayout.cellToPixel;
2403
+ var rowSelectionActive = isRowSelection(selection);
2404
+ var columnSelectionActive = isColumnSelection(selection);
2405
+
2406
+ var _normalizeSelection3 = normalizeSelection(selection),
2407
+ min = _normalizeSelection3[0],
2408
+ max = _normalizeSelection3[1];
2409
+
2410
+ var _cellToPixel = cellToPixel(min),
2411
+ left = _cellToPixel[0],
2412
+ top = _cellToPixel[1];
2413
+
2414
+ var _cellToPixel2 = cellToPixel(max, ONE_ONE),
2415
+ right = _cellToPixel2[0],
2416
+ bottom = _cellToPixel2[1];
2417
+
2418
+ if (rowSelectionActive) {
2419
+ right = 1e5;
2420
+ }
2421
+
2422
+ if (columnSelectionActive) {
2423
+ bottom = 1e5;
2424
+ }
2425
+
2426
+ return [[left, top], [right, bottom]];
2427
+ };
1901
2428
 
1902
- var kx1 = knobArea.x1;
1903
- var kx2 = knobArea.x2;
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;
1904
2462
 
1905
- if (knobArea.x1 > knobArea.x2) {
1906
- kx1 = knobArea.x2;
1907
- kx2 = knobArea.x1;
1908
- }
2463
+ if (isInRangeCenter(freezeX, minX, maxX + 1)) {
2464
+ var edge = indentX + frozenX;
1909
2465
 
1910
- var ky1 = knobArea.y1;
1911
- var ky2 = knobArea.y2;
2466
+ if (right <= edge) {
2467
+ right = edge;
2468
+ hideKnob = true;
2469
+ }
2470
+ }
1912
2471
 
1913
- if (knobArea.y1 > knobArea.y2) {
1914
- ky1 = knobArea.y2;
1915
- ky2 = knobArea.y1;
1916
- }
2472
+ if (isInRangeCenter(freezeY, minY, maxY + 1)) {
2473
+ var _edge = indentY + frozenY;
1917
2474
 
1918
- var fx1 = kx1;
1919
- var fy1 = ky1;
1920
- var fx2 = kx2;
1921
- var fy2 = ky2;
1922
- var changes = [];
2475
+ if (bottom <= _edge) {
2476
+ bottom = _edge;
2477
+ hideKnob = true;
2478
+ }
2479
+ }
1923
2480
 
1924
- if (fx2 - fx1 === sx2 - sx1) {
1925
- if (fy1 === sy1) {
1926
- fy1 = sy2 + 1;
1927
- } else {
1928
- fy2 = sy1 - 1;
1929
- }
2481
+ if (isInRangeLeft(minX, freezeX, offsetX + freezeX)) {
2482
+ left = -1e5;
1930
2483
 
1931
- if (fx1 === -1 && fx2 === -1) {
1932
- var max = findApproxMaxEditDataIndex(editData);
1933
- fx1 = 0;
1934
- fx2 = max.x;
1935
- }
2484
+ if (isInRangeRight(maxX + 1, freezeX, offsetX + freezeX)) {
2485
+ right = indentX;
2486
+ hideKnob = true;
2487
+ }
2488
+ }
1936
2489
 
1937
- var srcY = sy1;
2490
+ if (isInRangeLeft(minY, freezeY, offsetY + freezeY)) {
2491
+ top = -1e5;
1938
2492
 
1939
- for (var y = fy1; y <= fy2; y++) {
1940
- for (var x = fx1; x <= fx2; x++) {
1941
- var value = sourceData(x, srcY);
1942
- changes.push({
1943
- x: x,
1944
- y: y,
1945
- value: value,
1946
- source: {
1947
- x: x,
1948
- y: srcY
1949
- }
1950
- });
1951
- }
2493
+ if (isInRangeRight(maxY + 1, freezeY, offsetY + freezeY)) {
2494
+ bottom = indentY;
2495
+ hideKnob = true;
2496
+ }
2497
+ }
1952
2498
 
1953
- srcY = srcY + 1;
2499
+ if (rowSelectionActive && offsetX > 0) {
2500
+ hideKnob = true;
2501
+ }
1954
2502
 
1955
- if (srcY > sy2) {
1956
- srcY = sy1;
1957
- }
1958
- }
1959
- } else {
1960
- if (fx1 === sx1) {
1961
- fx1 = sx2 + 1;
1962
- } else {
1963
- fx2 = sx1 - 1;
1964
- }
2503
+ if (columnSelectionActive && offsetY > 0) {
2504
+ hideKnob = true;
2505
+ }
1965
2506
 
1966
- if (fy1 === -1 && fy2 === -1) {
1967
- var _max3 = findApproxMaxEditDataIndex(editData);
2507
+ if (rowSelectionActive) {
2508
+ right = 1e5;
2509
+ }
1968
2510
 
1969
- fy1 = 0;
1970
- fy2 = _max3.y;
1971
- }
2511
+ if (columnSelectionActive) {
2512
+ bottom = 1e5;
2513
+ }
1972
2514
 
1973
- var srcX = sx1;
2515
+ return [[[left, top], [right, bottom]], hideKnob];
2516
+ };
1974
2517
 
1975
- for (var _x3 = fx1; _x3 <= fx2; _x3++) {
1976
- for (var _y2 = fy1; _y2 <= fy2; _y2++) {
1977
- var _value = sourceData(srcX, _y2);
2518
+ var resizeCanvas = function resizeCanvas(canvas) {
2519
+ var _canvas$getBoundingCl = canvas.getBoundingClientRect(),
2520
+ width = _canvas$getBoundingCl.width,
2521
+ height = _canvas$getBoundingCl.height;
1978
2522
 
1979
- changes.push({
1980
- x: _x3,
1981
- y: _y2,
1982
- value: _value,
1983
- source: {
1984
- x: srcX,
1985
- y: _y2
1986
- }
1987
- });
1988
- }
2523
+ var _window = window,
2524
+ _window$devicePixelRa = _window.devicePixelRatio,
2525
+ ratio = _window$devicePixelRa === void 0 ? 1 : _window$devicePixelRa;
1989
2526
 
1990
- srcX = srcX + 1;
2527
+ if (ratio < 1) {
2528
+ ratio = 1;
2529
+ }
1991
2530
 
1992
- if (srcX > sx2) {
1993
- srcX = sx1;
1994
- }
1995
- }
1996
- }
2531
+ var newCanvasWidth = Math.round(width * ratio);
2532
+ var newCanvasHeight = Math.round(height * ratio);
1997
2533
 
1998
- if (props.onChange) {
1999
- props.onChange(changes);
2000
- }
2534
+ if (canvas.width !== newCanvasWidth || canvas.height !== newCanvasHeight) {
2535
+ var context = canvas.getContext('2d');
2001
2536
 
2002
- changeSelection(knobArea.x1, knobArea.y1, knobArea.x2, knobArea.y2);
2537
+ if (context) {
2538
+ canvas.width = newCanvasWidth;
2539
+ canvas.height = newCanvasHeight;
2540
+ context.scale(ratio, ratio);
2003
2541
  }
2004
2542
 
2005
- setSelectionInProgress(false);
2006
- setRowSelectionInProgress(false);
2007
- setColumnSelectionInProgress(false);
2008
- setKnobDragInProgress(false);
2009
- setColumnResize(null);
2010
- setRowResize(null);
2011
-
2012
- if (buttonClickMouseDownCoordinates.x !== -1 && buttonClickMouseDownCoordinates.y !== -1 && buttonClickMouseDownCoordinates.hitTarget !== null) {
2013
- if (!e.target || !(e.target instanceof Element)) {
2014
- return;
2015
- }
2543
+ return true;
2544
+ }
2016
2545
 
2017
- var rect = e.target.getBoundingClientRect();
2546
+ return false;
2547
+ };
2018
2548
 
2019
- var _x4 = e.clientX - rect.left;
2549
+ var excelHeaderString = function excelHeaderString(num) {
2550
+ var s = '';
2551
+ var t = 0;
2020
2552
 
2021
- var _y3 = e.clientY - rect.top;
2553
+ while (num > 0) {
2554
+ t = (num - 1) % 26;
2555
+ s = String.fromCharCode(65 + t) + s;
2556
+ num = (num - t) / 26 | 0;
2557
+ }
2022
2558
 
2023
- var hitTarget = buttonClickMouseDownCoordinates.hitTarget;
2559
+ return s || '';
2560
+ };
2024
2561
 
2025
- if (hitTarget.x <= _x4 && _x4 <= hitTarget.x + hitTarget.w && hitTarget.y <= _y3 && _y3 <= hitTarget.y + hitTarget.h) {
2026
- hitTarget.onClick(e);
2027
- }
2562
+ var Sheet = forwardRef(function (props, ref) {
2563
+ var _props$secondarySelec, _props$inputComponent;
2028
2564
 
2029
- setButtonClickMouseDownCoordinates({
2030
- x: -1,
2031
- y: -1,
2032
- hitTarget: null
2033
- });
2034
- }
2035
- };
2565
+ var canvasRef = useRef(null);
2566
+ var overlayRef = useRef(null);
2036
2567
 
2037
- useEffect(function () {
2038
- window.addEventListener('mouseup', onMouseUp);
2039
- return function () {
2040
- window.removeEventListener('mouseup', onMouseUp);
2041
- };
2042
- });
2568
+ var _useState = useState(INITIAL_MAX_SCROLL),
2569
+ maxScroll = _useState[0],
2570
+ setMaxScroll = _useState[1];
2043
2571
 
2044
- var onMouseMove = function onMouseMove(e) {
2045
- if (!e.target || !(e.target instanceof Element)) {
2046
- return;
2047
- }
2572
+ var _useState2 = useState(ORIGIN),
2573
+ dataOffset = _useState2[0],
2574
+ setDataOffset = _useState2[1];
2048
2575
 
2049
- var rect = e.target.getBoundingClientRect();
2050
- var x = e.clientX - rect.left;
2051
- var y = e.clientY - rect.top;
2052
- window.document.body.style.cursor = 'auto';
2053
- var hitTargetKeyX = Math.floor(x / xBinSize);
2054
- var hitTargetKeyY = Math.floor(y / yBinSize);
2576
+ var _useState3 = useState(NO_SELECTION),
2577
+ selection = _useState3[0],
2578
+ setSelection = _useState3[1];
2055
2579
 
2056
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
2057
- for (var _iterator21 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step21; !(_step21 = _iterator21()).done;) {
2058
- var hitTarget = _step21.value;
2580
+ var _useState4 = useState(null),
2581
+ knobArea = _useState4[0],
2582
+ setKnobArea = _useState4[1];
2059
2583
 
2060
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
2061
- window.document.body.style.cursor = 'pointer';
2062
- }
2063
- }
2064
- }
2584
+ var _useState5 = useState(null),
2585
+ dragOffset = _useState5[0],
2586
+ setDragOffset = _useState5[1];
2065
2587
 
2066
- if (!sheetStyle.hideColumnHeaders && props.onCellWidthChange && y < sheetStyle.columnHeaderHeight) {
2067
- var xx = sheetStyle.rowHeaderWidth;
2588
+ var _useState6 = useState(null),
2589
+ dropTarget = _useState6[0],
2590
+ setDropTarget = _useState6[1];
2068
2591
 
2069
- for (var _iterator22 = _createForOfIteratorHelperLoose(columnSizes.index), _step22; !(_step22 = _iterator22()).done;) {
2070
- var col = _step22.value;
2592
+ var _useState7 = useState(NO_CELL),
2593
+ editCell = _useState7[0],
2594
+ setEditCell = _useState7[1];
2071
2595
 
2072
- if (Math.abs(xx - x) < resizeColumnRowMouseThreshold) {
2073
- window.document.body.style.cursor = 'col-resize';
2074
- break;
2075
- }
2596
+ var _useState8 = useState(''),
2597
+ editValue = _useState8[0],
2598
+ setEditValue = _useState8[1];
2076
2599
 
2077
- xx += cellWidth(col);
2078
- }
2079
- }
2600
+ var _useState9 = useState(false),
2601
+ arrowKeyCommitMode = _useState9[0],
2602
+ setArrowKeyCommitMode = _useState9[1];
2080
2603
 
2081
- if (!sheetStyle.hideRowHeaders && props.onCellHeightChange && x < sheetStyle.rowHeaderWidth) {
2082
- var yy = sheetStyle.columnHeaderHeight;
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;
2083
2611
 
2084
- for (var _iterator23 = _createForOfIteratorHelperLoose(rowSizes.index), _step23; !(_step23 = _iterator23()).done;) {
2085
- var row = _step23.value;
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));
2693
+ }
2694
+ }, [visibleCells, props.onScrollChange]);
2695
+
2696
+ var changeSelection = function changeSelection(newSelection, scrollToAnchor) {
2697
+ if (scrollToAnchor === void 0) {
2698
+ scrollToAnchor = true;
2699
+ }
2700
+
2701
+ if (!isSameSelection(selection, newSelection)) {
2702
+ setSelection(newSelection);
2703
+ }
2704
+
2705
+ var overlay = overlayRef.current;
2706
+ if (!overlay) return;
2707
+
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
+ }
2086
2715
 
2087
- if (Math.abs(yy - y) < resizeColumnRowMouseThreshold) {
2088
- window.document.body.style.cursor = 'row-resize';
2089
- break;
2090
- }
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];
2091
2724
 
2092
- yy += cellHeight(row);
2093
- }
2725
+ props.onSelectionChanged(minX, minY, maxX, maxY);
2094
2726
  }
2727
+ };
2095
2728
 
2096
- if (Math.abs(x - knobCoordinates.x) < knobSize && Math.abs(y - knobCoordinates.y) < knobSize) {
2097
- 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
+ }]);
2098
2738
  }
2099
2739
 
2100
- if (columnResize) {
2101
- if (props.onCellWidthChange) {
2102
- var newWidth = Math.max(columnResize.oldWidth + x - columnResize.startX, minimumColumnWidth);
2103
- props.onCellWidthChange(columnResize.colIndices, newWidth);
2104
- }
2740
+ setEditCell(NO_CELL);
2741
+ };
2105
2742
 
2106
- return;
2743
+ var startEditingCell = function startEditingCell(editCell, arrowKeyCommitMode) {
2744
+ if (arrowKeyCommitMode === void 0) {
2745
+ arrowKeyCommitMode = false;
2107
2746
  }
2108
2747
 
2109
- if (rowResize) {
2110
- if (props.onCellHeightChange) {
2111
- var newHeight = Math.max(rowResize.oldHeight + y - rowResize.startY, minimumRowHeight);
2112
- props.onCellHeightChange(rowResize.rowIndices, newHeight);
2113
- }
2748
+ var cellX = editCell[0],
2749
+ cellY = editCell[1];
2114
2750
 
2751
+ if (cellReadOnly(cellX, cellY)) {
2115
2752
  return;
2116
2753
  }
2117
2754
 
2118
- if (selectionInProgress) {
2119
- var sel2 = absCoordianteToCell(x, y, rowSizes, columnSizes);
2755
+ var editDataValue = editData(cellX, cellY);
2756
+ var val = '';
2120
2757
 
2121
- if (rowSelectionInProgress) {
2122
- changeSelection(-1, selection.y1, -1, sel2.y, false);
2123
- } else if (columnSelectionInProgress) {
2124
- changeSelection(selection.x1, -1, sel2.x, -1, false);
2125
- } else {
2126
- changeSelection(selection.x1, selection.y1, sel2.x, sel2.y);
2127
- }
2758
+ if (editDataValue !== null && editDataValue !== undefined) {
2759
+ val = editDataValue;
2128
2760
  }
2129
2761
 
2130
- if (knobDragInProgress) {
2131
- window.document.body.style.cursor = 'crosshair';
2132
- var cell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2133
- var x1 = selection.x1;
2134
- var y1 = selection.y1;
2135
- var x2 = selection.x2;
2136
- var y2 = selection.y2;
2137
-
2138
- if (x1 > x2) {
2139
- x1 = selection.x2;
2140
- x2 = selection.x1;
2141
- }
2142
-
2143
- if (y1 > y2) {
2144
- y1 = selection.y2;
2145
- y2 = selection.y1;
2146
- }
2147
-
2148
- var xCellDiff = 0;
2149
- if (cell.x < x1) xCellDiff = cell.x - x1;
2150
- if (cell.x > x2) xCellDiff = x2 - cell.x;
2151
- var yCellDiff = 0;
2152
- if (cell.y < y1) yCellDiff = cell.y - y1;
2153
- if (cell.y > y2) yCellDiff = y2 - cell.y;
2154
-
2155
- if (x1 === -1 && x2 === -1 || xCellDiff > yCellDiff) {
2156
- if (cell.y < y1) {
2157
- y1 = cell.y;
2158
- } else {
2159
- y2 = cell.y;
2160
- }
2161
- } else {
2162
- if (cell.x < x1) {
2163
- x1 = cell.x;
2164
- } else {
2165
- x2 = cell.x;
2166
- }
2167
- }
2168
-
2169
- setKnobArea({
2170
- x1: x1,
2171
- y1: y1,
2172
- x2: x2,
2173
- y2: y2
2174
- });
2175
- }
2762
+ setEditCell(editCell);
2763
+ setEditValue(val);
2764
+ setArrowKeyCommitMode(arrowKeyCommitMode);
2176
2765
  };
2177
2766
 
2178
- var onDoubleClick = function onDoubleClick(e) {
2179
- e.preventDefault();
2180
-
2181
- if (!e.target || !(e.target instanceof Element) || shiftKeyDown) {
2182
- return;
2183
- }
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);
2184
2772
 
2185
- var rect = e.target.getBoundingClientRect();
2186
- var x = e.clientX - rect.left;
2187
- var y = e.clientY - rect.top;
2188
- var hitTargetKeyX = Math.floor(x / xBinSize);
2189
- 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;
2190
2776
 
2191
- if (hitMap[hitTargetKeyX] && hitMap[hitTargetKeyX][hitTargetKeyY]) {
2192
- for (var _iterator24 = _createForOfIteratorHelperLoose(hitMap[hitTargetKeyX][hitTargetKeyY]), _step24; !(_step24 = _iterator24()).done;) {
2193
- var hitTarget = _step24.value;
2777
+ useLayoutEffect(function () {
2778
+ var canvas = canvasRef.current;
2194
2779
 
2195
- if (hitTarget.x <= x && x <= hitTarget.x + hitTarget.w && hitTarget.y <= y && y <= hitTarget.y + hitTarget.h) {
2196
- return;
2197
- }
2198
- }
2780
+ if (!canvas) {
2781
+ return;
2199
2782
  }
2200
2783
 
2201
- var editCell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2202
- setArrowKeyCommitMode(false);
2784
+ var context = canvas.getContext('2d');
2203
2785
 
2204
- if (editMode) {
2205
- commitEditingCell();
2786
+ if (!context) {
2787
+ return;
2206
2788
  }
2207
2789
 
2208
- startEditingCell(editCell);
2209
- };
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]);
2210
2797
 
2211
2798
  var onKeyDown = function onKeyDown(e) {
2799
+ console.log('onKeyDown', e.key, e);
2800
+
2212
2801
  if (e.key === 'Escape') {
2213
- setEditCell({
2214
- x: -1,
2215
- y: -1
2216
- });
2217
- setEditKey('');
2802
+ setEditCell(NO_CELL);
2218
2803
  return;
2219
2804
  }
2220
2805
 
2221
- if (e.key === 'Enter') {
2222
- commitEditingCell();
2223
- changeSelection(selection.x1, selection.y1 + 1, selection.x1, selection.y1 + 1);
2224
- }
2225
-
2226
- if (e.key === 'Tab') {
2227
- e.preventDefault();
2228
- commitEditingCell();
2229
- changeSelection(selection.x1 + 1, selection.y1, selection.x1 + 1, selection.y1);
2230
- }
2806
+ var direction = e.key === 'Enter' || e.key === 'TAB' ? 'right' : arrowKeyCommitMode ? ARROW_KEYS[e.key] : null;
2231
2807
 
2232
- if (arrowKeyCommitMode && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2808
+ if (direction) {
2233
2809
  e.preventDefault();
2810
+ var step = getDirectionStep(direction);
2811
+ var head = maxXY(addXY(editCell, step), ORIGIN);
2234
2812
  commitEditingCell();
2235
- var x1 = selection.x1;
2236
- var y1 = selection.y1;
2237
- var x2 = selection.x1;
2238
- var y2 = selection.y1;
2239
-
2240
- if (e.key === 'ArrowRight') {
2241
- x1 = selection.x1 + 1;
2242
- x2 = selection.x1 + 1;
2243
- } else if (e.key === 'ArrowLeft') {
2244
- x1 = selection.x1 - 1;
2245
- x2 = selection.x1 - 1;
2246
- } else if (e.key === 'ArrowUp') {
2247
- y1 = selection.y1 - 1;
2248
- y2 = selection.y1 - 1;
2249
- } else if (e.key === 'ArrowDown') {
2250
- y1 = selection.y1 + 1;
2251
- y2 = selection.y1 + 1;
2252
- }
2253
-
2254
- changeSelection(x1, y1, x2, y2);
2813
+ changeSelection([head, head]);
2255
2814
  }
2256
2815
  };
2257
2816
 
2258
2817
  var onGridKeyDown = function onGridKeyDown(e) {
2259
- if (editMode && arrowKeyCommitMode && ['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2260
- commitEditingCell();
2261
- return;
2262
- }
2818
+ console.log('onGridKeydown', e.key, e);
2263
2819
 
2264
- if (e.key === 'Shift') {
2265
- setShiftKeyDown(true);
2820
+ if (editMode && arrowKeyCommitMode && e.key in ARROW_KEYS) {
2821
+ commitEditingCell();
2266
2822
  return;
2267
2823
  }
2268
2824
 
@@ -2271,33 +2827,28 @@ function Sheet(props) {
2271
2827
  }
2272
2828
 
2273
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();
2274
2832
  return;
2275
2833
  }
2276
2834
 
2277
2835
  if (e.key === 'Backspace' || e.key === 'Delete') {
2278
- var x1 = selection.x1;
2279
- var y1 = selection.y1;
2280
- var x2 = selection.x2;
2281
- var y2 = selection.y2;
2282
-
2283
- if (x1 > x2) {
2284
- x1 = selection.x2;
2285
- x2 = selection.x1;
2286
- }
2287
-
2288
- if (y1 > y2) {
2289
- y1 = selection.y2;
2290
- y2 = selection.y1;
2291
- }
2292
-
2293
- 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)) {
2294
2845
  x1 = 0;
2295
- x2 = maxSearchableRowOrCol;
2846
+ x2 = MAX_SEARCHABLE_INDEX;
2296
2847
  }
2297
2848
 
2298
- if (x1 !== -1 && x2 !== -1 && y1 === -1 && y2 === -1) {
2849
+ if (isColumnSelection(selection)) {
2299
2850
  y1 = 0;
2300
- y2 = maxSearchableRowOrCol;
2851
+ y2 = MAX_SEARCHABLE_INDEX;
2301
2852
  }
2302
2853
 
2303
2854
  var changes = [];
@@ -2319,171 +2870,95 @@ function Sheet(props) {
2319
2870
  return;
2320
2871
  }
2321
2872
 
2322
- if (selection.x1 === -1 || selection.x2 === -1 || selection.y1 === -1 || selection.y2 === -1) {
2873
+ if (isEmptySelection(selection)) {
2323
2874
  return;
2324
2875
  }
2325
2876
 
2326
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 === ',') {
2327
- 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)) {
2328
2883
  e.preventDefault();
2329
2884
  return;
2330
2885
  }
2331
2886
 
2332
- startEditingCell({
2333
- x: selection.x1,
2334
- y: selection.y1
2335
- });
2336
- setArrowKeyCommitMode(e.key !== 'Enter');
2887
+ startEditingCell(cell, e.key !== 'Enter');
2337
2888
  return;
2338
2889
  }
2339
2890
 
2340
- if (['ArrowRight', 'ArrowLeft', 'ArrowUp', 'ArrowDown'].includes(e.key)) {
2341
- var sel1 = {
2342
- x: selection.x1,
2343
- y: selection.y1
2344
- };
2345
- var sel2 = {
2346
- x: selection.x2,
2347
- y: selection.y2
2348
- };
2349
- var incr = {
2350
- x: 0,
2351
- y: 0
2352
- };
2353
- var direction = 'right';
2354
-
2355
- if (e.key === 'ArrowRight' || e.key === 'Tab') {
2356
- incr.x = 1;
2357
- direction = 'right';
2358
- } else if (e.key === 'ArrowLeft') {
2359
- incr.x = -1;
2360
- direction = 'left';
2361
- } else if (e.key === 'ArrowUp') {
2362
- incr.y = -1;
2363
- direction = 'up';
2364
- } else if (e.key === 'ArrowDown') {
2365
- incr.y = 1;
2366
- direction = 'down';
2367
- }
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);
2368
2896
 
2369
2897
  if (e.metaKey || e.ctrlKey) {
2370
- var val = findInDisplayData(displayData, sel2, direction);
2371
- incr.x = val.x - sel2.x;
2372
- incr.y = val.y - sel2.y;
2373
- }
2374
-
2375
- sel2.x += incr.x;
2376
- sel2.y += incr.y;
2377
-
2378
- if (sel2.x < 0) {
2379
- sel2.x = 0;
2380
- }
2381
-
2382
- if (sel2.y < 0) {
2383
- sel2.y = 0;
2898
+ head = findInDisplayData(displayData, head, direction);
2899
+ } else {
2900
+ head = maxXY(addXY(head, step), ORIGIN);
2384
2901
  }
2385
2902
 
2386
2903
  if (!e.shiftKey) {
2387
- sel1 = _extends({}, sel2);
2904
+ anchor = head;
2388
2905
  }
2389
2906
 
2390
- changeSelection(sel1.x, sel1.y, sel2.x, sel2.y);
2907
+ changeSelection([anchor, head]);
2391
2908
  return;
2392
2909
  }
2393
2910
 
2394
2911
  e.preventDefault();
2395
2912
  };
2396
2913
 
2397
- var onGridKeyUp = function onGridKeyUp(e) {
2398
- setShiftKeyDown(e.shiftKey);
2399
- };
2400
-
2401
- var onContextMenu = function onContextMenu(e) {
2402
- var _props$onRightClick;
2403
-
2404
- if (!e.target || !(e.target instanceof Element)) {
2405
- return;
2406
- }
2407
-
2408
- var rect = e.target.getBoundingClientRect();
2409
- var x = e.clientX - rect.left;
2410
- var y = e.clientY - rect.top;
2411
- var cell = absCoordianteToCell(x, y, rowSizes, columnSizes);
2412
- var cellX = cell.x;
2413
- var cellY = cell.y;
2414
- var x1 = selection.x1,
2415
- x2 = selection.x2,
2416
- y1 = selection.y1,
2417
- y2 = selection.y2;
2418
-
2419
- if (x1 > x2) {
2420
- var _ref = [x2, x1];
2421
- x1 = _ref[0];
2422
- x2 = _ref[1];
2423
- }
2424
-
2425
- if (y1 > y2) {
2426
- var _ref2 = [y2, y1];
2427
- y1 = _ref2[0];
2428
- y2 = _ref2[1];
2429
- }
2430
-
2431
- if (!(y > sheetStyle.columnHeaderHeight && x > sheetStyle.rowHeaderWidth)) {
2432
- return;
2433
- }
2434
-
2435
- if (!(cellX >= x1 && cellX <= x2) || !(cellY >= y1 && cellY <= y2)) {
2436
- changeSelection(cellX, cellY, cellX, cellY);
2437
- }
2438
-
2439
- onMouseMove(e);
2914
+ var editKey = editKeys ? editKeys.apply(void 0, editCell) : '';
2440
2915
 
2441
- var ev = _extends({}, e, {
2442
- cellX: cellX,
2443
- cellY: cellY
2444
- });
2916
+ var _useState10 = useState(editKey),
2917
+ lastEditKey = _useState10[0],
2918
+ setLastEditKey = _useState10[1];
2445
2919
 
2446
- (_props$onRightClick = props.onRightClick) === null || _props$onRightClick === void 0 ? void 0 : _props$onRightClick.call(props, ev);
2447
- };
2920
+ if (lastEditKey !== editKey) {
2921
+ setLastEditKey(editKey);
2922
+ setEditCell(NO_CELL);
2923
+ }
2448
2924
 
2449
- var editMode = editCell.x !== -1 && editCell.y !== -1;
2450
- var editTextPosition = {
2451
- x: 0,
2452
- y: 0
2453
- };
2925
+ var editTextPosition = ORIGIN;
2454
2926
  var editTextWidth = 0;
2455
2927
  var editTextHeight = 0;
2456
2928
  var editTextTextAlign = 'right';
2457
2929
 
2458
2930
  if (editMode) {
2459
- editTextPosition = cellToAbsCoordinate(editCell.x, editCell.y, rowSizes, columnSizes, dataOffset, cellWidth, cellHeight, sheetStyle);
2460
- var style = cellStyle(editCell.x, editCell.y);
2461
- editTextPosition.x += 1;
2462
- editTextPosition.y += 1;
2463
- editTextWidth = cellWidth(editCell.x) - 2;
2464
- editTextHeight = cellHeight(editCell.y) - 2;
2465
- 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';
2466
2937
  }
2467
2938
 
2939
+ var _editTextPosition = editTextPosition,
2940
+ textX = _editTextPosition[0],
2941
+ textY = _editTextPosition[1];
2468
2942
  var inputProps = {
2469
2943
  value: editValue,
2470
2944
  autoFocus: true,
2471
2945
  onKeyDown: onKeyDown,
2472
2946
  style: {
2473
2947
  position: 'absolute',
2474
- top: editTextPosition.y,
2475
- left: editTextPosition.x,
2948
+ left: textX,
2949
+ top: textY,
2950
+ padding: '0px 4px',
2476
2951
  width: editTextWidth,
2477
2952
  height: editTextHeight,
2478
2953
  outline: 'none',
2479
2954
  border: 'none',
2480
2955
  textAlign: editTextTextAlign,
2481
2956
  color: 'black',
2482
- fontSize: defaultCellStyle.fontSize,
2957
+ fontSize: DEFAULT_CELL_STYLE.fontSize,
2483
2958
  fontFamily: 'sans-serif'
2484
2959
  }
2485
2960
  };
2486
- 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, {
2487
2962
  onChange: setEditValue
2488
2963
  }), commitEditingCell);
2489
2964
  var overlayDivClassName = styles.sheetscroll;
@@ -2517,23 +2992,19 @@ function Sheet(props) {
2517
2992
  }, React.createElement("canvas", {
2518
2993
  style: canvasStyles,
2519
2994
  ref: canvasRef
2520
- }), React.createElement("div", {
2521
- ref: overlayRef,
2522
- onDoubleClick: onDoubleClick,
2523
- onMouseDown: onMouseDown,
2524
- onMouseMove: onMouseMove,
2525
- onMouseLeave: onMouseLeave,
2526
- onContextMenu: onContextMenu,
2995
+ }), React.createElement("div", Object.assign({
2996
+ ref: overlayRef
2997
+ }, mouseHandlers, {
2527
2998
  onScroll: onScroll,
2528
2999
  className: overlayDivClassName,
2529
3000
  style: overlayDivStyles
2530
- }, React.createElement("div", {
3001
+ }), React.createElement("div", {
2531
3002
  style: {
2532
3003
  position: 'absolute',
2533
3004
  left: 0,
2534
3005
  top: 0,
2535
3006
  width: 1,
2536
- height: maxScroll.y + 2000,
3007
+ height: maxScrollY + 2000,
2537
3008
  backgroundColor: 'rgba(0,0,0,0.0)'
2538
3009
  }
2539
3010
  }), React.createElement("div", {
@@ -2541,7 +3012,7 @@ function Sheet(props) {
2541
3012
  position: 'absolute',
2542
3013
  left: 0,
2543
3014
  top: 0,
2544
- width: maxScroll.x + 5000,
3015
+ width: maxScrollX + 5000,
2545
3016
  height: 1,
2546
3017
  backgroundColor: 'rgba(0,0,0,0.0)'
2547
3018
  }
@@ -2554,7 +3025,7 @@ function Sheet(props) {
2554
3025
  height: 1,
2555
3026
  opacity: 0.01
2556
3027
  },
2557
- ref: copyPasteTextAreaRef,
3028
+ ref: textAreaRef,
2558
3029
  autoComplete: "off",
2559
3030
  autoCorrect: "off",
2560
3031
  autoCapitalize: "off",
@@ -2563,8 +3034,7 @@ function Sheet(props) {
2563
3034
  return e.target.select();
2564
3035
  },
2565
3036
  tabIndex: 0,
2566
- onKeyDown: onGridKeyDown,
2567
- onKeyUp: onGridKeyUp
3037
+ onKeyDown: onGridKeyDown
2568
3038
  }), editMode && (input !== undefined ? input : React.createElement("input", Object.assign({}, inputProps, {
2569
3039
  type: "text",
2570
3040
  onFocus: function onFocus(e) {
@@ -2574,7 +3044,7 @@ function Sheet(props) {
2574
3044
  return setEditValue(e.target.value);
2575
3045
  }
2576
3046
  }))));
2577
- }
3047
+ });
2578
3048
 
2579
3049
  export default Sheet;
2580
3050
  //# sourceMappingURL=index.modern.js.map