Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to stop cell movement with keyboard if unknown modifier key is pressed #416

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cheetah-grid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cheetah-grid",
"version": "1.13.0",
"version": "1.13.1",
"description": "Cheetah Grid is a high performance grid engine that works on canvas",
"keywords": [
"spreadsheet",
Expand Down
66 changes: 38 additions & 28 deletions packages/cheetah-grid/src/js/core/DrawGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,60 +995,67 @@ function _onScroll(grid: DrawGrid, _e: Event): void {
/** @private */
// eslint-disable-next-line complexity
function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
const { shiftKey } = e;
const keyCode = getKeyCode(e);
const focusCell = shiftKey ? this.selection.focus : this.selection.select;
const focusCell = e.shiftKey ? this.selection.focus : this.selection.select;
const ctrlOrMeta = e.ctrlKey || e.metaKey;
if (keyCode === KEY_LEFT) {
if (e.ctrlKey || e.metaKey) {
move(this, null, "W");
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, null, "W", e.shiftKey);
} else {
if (!hMove.call(this, "W")) {
if (!hMove.call(this, "W", e.shiftKey)) {
return;
}
}
cancelEvent(e);
} else if (keyCode === KEY_UP) {
if (e.ctrlKey || e.metaKey) {
move(this, "N", null);
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, "N", null, e.shiftKey);
} else {
if (!vMove.call(this, "N")) {
if (!vMove.call(this, "N", e.shiftKey)) {
return;
}
}
cancelEvent(e);
} else if (keyCode === KEY_RIGHT) {
if (e.ctrlKey || e.metaKey) {
move(this, null, "E");
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, null, "E", e.shiftKey);
} else {
if (!hMove.call(this, "E")) {
if (!hMove.call(this, "E", e.shiftKey)) {
return;
}
}
cancelEvent(e);
} else if (keyCode === KEY_DOWN) {
if (e.ctrlKey || e.metaKey) {
move(this, "S", null);
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, "S", null, e.shiftKey);
} else {
if (!vMove.call(this, "S")) {
if (!vMove.call(this, "S", e.shiftKey)) {
return;
}
}
cancelEvent(e);
} else if (keyCode === KEY_HOME) {
if (e.ctrlKey || e.metaKey) {
move(this, "N", "W");
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, "N", "W", e.shiftKey);
} else {
move(this, null, "W");
move(this, null, "W", e.shiftKey);
}
cancelEvent(e);
} else if (keyCode === KEY_END) {
if (e.ctrlKey || e.metaKey) {
move(this, "S", "E");
if (e.altKey) return; // unknown modifier key
if (ctrlOrMeta) {
move(this, "S", "E", e.shiftKey);
} else {
move(this, null, "E");
move(this, null, "E", e.shiftKey);
}
cancelEvent(e);
} else if (this.keyboardOptions?.moveCellOnTab && keyCode === KEY_TAB) {
if (e.altKey || ctrlOrMeta) return; // unknown modifier key
let newCell: CellAddress | null = null;
if (typeof this.keyboardOptions.moveCellOnTab === "function") {
newCell = this.keyboardOptions.moveCellOnTab({
Expand All @@ -1059,7 +1066,7 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
}
if (newCell) {
_moveFocusCell.call(this, newCell.col, newCell.row, false);
} else if (shiftKey) {
} else if (e.shiftKey) {
if (!hMove.call(this, "W", false)) {
const row = this.getMoveUpRowByKeyDownInternal(focusCell);
if (0 > row) {
Expand All @@ -1078,6 +1085,7 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
}
cancelEvent(e);
} else if (this.keyboardOptions?.moveCellOnEnter && keyCode === KEY_ENTER) {
if (e.altKey || ctrlOrMeta) return; // unknown modifier key
let newCell: CellAddress | null = null;
if (typeof this.keyboardOptions.moveCellOnEnter === "function") {
newCell = this.keyboardOptions.moveCellOnEnter({
Expand All @@ -1088,7 +1096,7 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
}
if (newCell) {
_moveFocusCell.call(this, newCell.col, newCell.row, false);
} else if (shiftKey) {
} else if (e.shiftKey) {
if (!vMove.call(this, "N", false)) {
const col = this.getMoveLeftColByKeyDownInternal(focusCell);
if (0 > col) {
Expand All @@ -1113,9 +1121,10 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
cancelEvent(e);
} else if (
this.keyboardOptions?.selectAllOnCtrlA &&
keyCode === KEY_ALPHA_A &&
(e.ctrlKey || e.metaKey)
keyCode === KEY_ALPHA_A
) {
if (e.altKey || e.shiftKey) return; // unknown modifier key
if (!ctrlOrMeta) return;
this.selection.range = {
start: { col: 0, row: 0 },
end: { col: this.colCount - 1, row: this.rowCount - 1 },
Expand All @@ -1127,19 +1136,20 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
function move(
grid: DrawGrid,
vDir: "N" | "S" | null,
hDir: "W" | "E" | null
hDir: "W" | "E" | null,
shiftKeyFlg: boolean
): void {
const row =
vDir === "S" ? grid.rowCount - 1 : vDir === "N" ? 0 : focusCell.row;
const col =
hDir === "E" ? grid.colCount - 1 : hDir === "W" ? 0 : focusCell.col;
_moveFocusCell.call(grid, col, row, shiftKey);
_moveFocusCell.call(grid, col, row, shiftKeyFlg);
}

function vMove(
this: DrawGrid,
vDir: "N" | "S",
shiftKeyFlg: boolean = shiftKey
shiftKeyFlg: boolean
): boolean {
const { col } = focusCell;
let row: number;
Expand All @@ -1164,7 +1174,7 @@ function _onKeyDownMove(this: DrawGrid, e: KeyboardEvent): void {
function hMove(
this: DrawGrid,
hDir: "W" | "E",
shiftKeyFlg: boolean = shiftKey
shiftKeyFlg: boolean
): boolean {
const { row } = focusCell;
let col: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-cheetah-grid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-cheetah-grid",
"version": "1.13.0",
"version": "1.13.1",
"description": "Cheetah Grid for Vue.js",
"main": "lib/index.js",
"unpkg": "dist/vueCheetahGrid.js",
Expand Down
Loading