sheet-happens 0.0.23 → 0.0.25

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