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