Skip to content

Commit 8a2f8ff

Browse files
Jared Kirschnerjkirschner
authored andcommitted
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.
1 parent bddbe4b commit 8a2f8ff

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/component/formula.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,43 +98,47 @@ export default class Formula {
9898

9999
this.el.addEventListener("keydown", (e) => {
100100
const keyCode = e.keyCode || e.which;
101+
101102
if ([37, 38, 39, 40].indexOf(keyCode) == -1) return;
102103

103104
if (!this.cell || this.cell.from == this.cell.to) return;
104105

105106
e.preventDefault();
106107
e.stopPropagation();
107108

108-
// Get values before merge cells applied
109-
const cellRangeArgs = this.getCellRangeArgsFromSelectStartEnd();
110-
111-
// Account for merge cells
112-
let cellRange = new CellRange(...cellRangeArgs);
109+
let rowShift = 0;
110+
let colShift = 0;
113111

114112
// Left
115113
if (keyCode == 37) {
116-
cellRange.translate(0, -1);
117-
this.cellSelectStartRowCol[1] = Math.max(0, this.cellSelectStartRowCol[1] - 1);
118-
this.cellSelectEndRowCol[1] = Math.max(0, this.cellSelectEndRowCol[1] - 1);
114+
colShift = -1;
119115
}
120116
// Up
121117
else if (keyCode == 38) {
122-
cellRange.translate(-1, 0);
123-
this.cellSelectStartRowCol[0] = Math.max(0, this.cellSelectStartRowCol[0] - 1);
124-
this.cellSelectEndRowCol[0] = Math.max(0, this.cellSelectEndRowCol[0] - 1);
118+
rowShift = -1;
125119
}
126120
// Right
127121
else if (keyCode == 39) {
128-
cellRange.translate(0, 1);
129-
this.cellSelectStartRowCol[1] = this.cellSelectStartRowCol[1] + 1;
130-
this.cellSelectEndRowCol[1] = this.cellSelectEndRowCol[1] + 1;
122+
colShift = 1;
131123
}
132124
// Down
133125
else if (keyCode == 40) {
134-
cellRange.translate(1, 0);
135-
this.cellSelectStartRowCol[0] = this.cellSelectStartRowCol[0] + 1;
136-
this.cellSelectEndRowCol[0] = this.cellSelectEndRowCol[0] + 1;
126+
rowShift = 1;
127+
}
128+
129+
// If the shift key is applied, hold the start position fixed
130+
if (!e.shiftKey) {
131+
this.cellSelectStartRowCol[0] = Math.max(0, this.cellSelectStartRowCol[0] + rowShift);
132+
this.cellSelectStartRowCol[1] = Math.max(0, this.cellSelectStartRowCol[1] + colShift);
137133
}
134+
this.cellSelectEndRowCol[0] = Math.max(0, this.cellSelectEndRowCol[0] + rowShift);
135+
this.cellSelectEndRowCol[1] = Math.max(0, this.cellSelectEndRowCol[1] + colShift);
136+
137+
// Get values before merge cells applied
138+
const cellRangeArgs = this.getCellRangeArgsFromSelectStartEnd();
139+
140+
// Account for merge cells
141+
let cellRange = new CellRange(...cellRangeArgs);
138142

139143
// Reapply merge cells after translation
140144
cellRange = this.editor.data.merges.union(cellRange)

0 commit comments

Comments
 (0)