From b70c5f9feb7d23b0eddf6316a5c2b07c1e1e142a Mon Sep 17 00:00:00 2001 From: Jared Kirschner Date: Wed, 9 Sep 2020 19:19:15 -0400 Subject: [PATCH] Handle SHIFT key when moving cell reference range If the user is editing a cell reference range within a formula and is holding the shift key while using the direction arrows to move the range, the start of the range will be fixed. This mirrors the behavior of Excel when editing a cell reference range. --- src/component/formula.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/component/formula.js b/src/component/formula.js index 3586f9d5..89b3e066 100644 --- a/src/component/formula.js +++ b/src/component/formula.js @@ -98,6 +98,7 @@ export default class Formula { this.el.addEventListener("keydown", (e) => { const keyCode = e.keyCode || e.which; + if ([37, 38, 39, 40].indexOf(keyCode) == -1) return; if (!this.cell || this.cell.from == this.cell.to) return; @@ -105,36 +106,39 @@ export default class Formula { e.preventDefault(); e.stopPropagation(); - // Get values before merge cells applied - const cellRangeArgs = this.getCellRangeArgsFromSelectStartEnd(); - - // Account for merge cells - let cellRange = new CellRange(...cellRangeArgs); + let rowShift = 0; + let colShift = 0; // Left if (keyCode == 37) { - cellRange.translate(0, -1); - this.cellSelectStartRowCol[1] = Math.max(0, this.cellSelectStartRowCol[1] - 1); - this.cellSelectEndRowCol[1] = Math.max(0, this.cellSelectEndRowCol[1] - 1); + colShift = -1; } // Up else if (keyCode == 38) { - cellRange.translate(-1, 0); - this.cellSelectStartRowCol[0] = Math.max(0, this.cellSelectStartRowCol[0] - 1); - this.cellSelectEndRowCol[0] = Math.max(0, this.cellSelectEndRowCol[0] - 1); + rowShift = -1; } // Right else if (keyCode == 39) { - cellRange.translate(0, 1); - this.cellSelectStartRowCol[1] = this.cellSelectStartRowCol[1] + 1; - this.cellSelectEndRowCol[1] = this.cellSelectEndRowCol[1] + 1; + colShift = 1; } // Down else if (keyCode == 40) { - cellRange.translate(1, 0); - this.cellSelectStartRowCol[0] = this.cellSelectStartRowCol[0] + 1; - this.cellSelectEndRowCol[0] = this.cellSelectEndRowCol[0] + 1; + rowShift = 1; + } + + // If the shift key is applied, hold the start position fixed + if (!e.shiftKey) { + this.cellSelectStartRowCol[0] = Math.max(0, this.cellSelectStartRowCol[0] + rowShift); + this.cellSelectStartRowCol[1] = Math.max(0, this.cellSelectStartRowCol[1] + colShift); } + this.cellSelectEndRowCol[0] = Math.max(0, this.cellSelectEndRowCol[0] + rowShift); + this.cellSelectEndRowCol[1] = Math.max(0, this.cellSelectEndRowCol[1] + colShift); + + // Get values before merge cells applied + const cellRangeArgs = this.getCellRangeArgsFromSelectStartEnd(); + + // Account for merge cells + let cellRange = new CellRange(...cellRangeArgs); // Reapply merge cells after translation cellRange = this.editor.data.merges.union(cellRange)