Skip to content

Commit 143451a

Browse files
committed
fix
1 parent 84c855e commit 143451a

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/cm/commandRegistry.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ const commandMap = new Map();
119119
/** @type {Record<string, any>} */
120120
let resolvedKeyBindings = keyBindings;
121121

122+
/** @type {Record<string, any>} */
123+
let cachedResolvedKeyBindings = {};
124+
125+
let resolvedKeyBindingsVersion = 0;
126+
122127
/** @type {import("@codemirror/view").KeyBinding[]} */
123128
let cachedKeymap = [];
124129

@@ -1336,6 +1341,19 @@ function resolveBindingInfo(name) {
13361341
return baseBinding ? { ...baseBinding, ...override } : override;
13371342
}
13381343

1344+
function buildResolvedKeyBindingsSnapshot() {
1345+
const bindingNames = new Set([
1346+
...Object.keys(keyBindings),
1347+
...Object.keys(resolvedKeyBindings ?? {}),
1348+
]);
1349+
1350+
return Object.fromEntries(
1351+
Array.from(bindingNames, (name) => [name, resolveBindingInfo(name)]).filter(
1352+
([, binding]) => binding,
1353+
),
1354+
);
1355+
}
1356+
13391357
function toCodeMirrorKey(combo) {
13401358
if (!combo) return null;
13411359
const parts = combo
@@ -1377,6 +1395,7 @@ function toCodeMirrorKey(combo) {
13771395

13781396
function rebuildKeymap() {
13791397
const bindings = [];
1398+
cachedResolvedKeyBindings = buildResolvedKeyBindingsSnapshot();
13801399
commandMap.forEach((command, name) => {
13811400
const bindingInfo = resolveBindingInfo(name);
13821401
command.description =
@@ -1398,6 +1417,7 @@ function rebuildKeymap() {
13981417
});
13991418
});
14001419
cachedKeymap = bindings;
1420+
resolvedKeyBindingsVersion += 1;
14011421
return bindings;
14021422
}
14031423

@@ -1436,16 +1456,11 @@ export function getRegisteredCommands() {
14361456
}
14371457

14381458
export function getResolvedKeyBindings() {
1439-
const bindingNames = new Set([
1440-
...Object.keys(keyBindings),
1441-
...Object.keys(resolvedKeyBindings ?? {}),
1442-
]);
1459+
return cachedResolvedKeyBindings;
1460+
}
14431461

1444-
return Object.fromEntries(
1445-
Array.from(bindingNames, (name) => [name, resolveBindingInfo(name)]).filter(
1446-
([, binding]) => binding,
1447-
),
1448-
);
1462+
export function getResolvedKeyBindingsVersion() {
1463+
return resolvedKeyBindingsVersion;
14491464
}
14501465

14511466
export function getCommandKeymapExtension() {

src/components/terminal/terminal.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { Unicode11Addon } from "@xterm/addon-unicode11";
1111
import { WebLinksAddon } from "@xterm/addon-web-links";
1212
import { WebglAddon } from "@xterm/addon-webgl";
1313
import { Terminal as Xterm } from "@xterm/xterm";
14-
import { getResolvedKeyBindings } from "cm/commandRegistry";
14+
import {
15+
getResolvedKeyBindings,
16+
getResolvedKeyBindingsVersion,
17+
} from "cm/commandRegistry";
1518
import toast from "components/toast";
1619
import confirm from "dialogs/confirm";
1720
import fonts from "lib/fonts";
@@ -61,6 +64,8 @@ export default class TerminalComponent {
6164
this.isConnected = false;
6265
this.serverMode = options.serverMode !== false; // Default true
6366
this.touchSelection = null;
67+
this.parsedAppKeybindings = [];
68+
this.parsedAppKeybindingsVersion = -1;
6469

6570
this.init();
6671
}
@@ -335,6 +340,11 @@ export default class TerminalComponent {
335340
* Parse app keybindings into a format usable by the keyboard handler
336341
*/
337342
parseAppKeybindings() {
343+
const version = getResolvedKeyBindingsVersion();
344+
if (this.parsedAppKeybindingsVersion === version) {
345+
return this.parsedAppKeybindings;
346+
}
347+
338348
const parsedBindings = [];
339349

340350
Object.values(getResolvedKeyBindings()).forEach((binding) => {
@@ -378,7 +388,10 @@ export default class TerminalComponent {
378388
});
379389
});
380390

381-
return parsedBindings;
391+
this.parsedAppKeybindings = parsedBindings;
392+
this.parsedAppKeybindingsVersion = version;
393+
394+
return this.parsedAppKeybindings;
382395
}
383396

384397
/**

0 commit comments

Comments
 (0)