@@ -108,6 +108,7 @@ const commandKeymapCompartment = new Compartment();
108108 * run: (view?: EditorView | null) => boolean | void;
109109 * requiresView?: boolean;
110110 * defaultDescription?: string;
111+ * defaultKey?: string | null;
111112 * key?: string | null;
112113 * }} CommandEntry
113114 */
@@ -1177,6 +1178,7 @@ function addCommand(entry) {
11771178 const command = {
11781179 ...entry ,
11791180 defaultDescription : entry . description || entry . name ,
1181+ defaultKey : entry . key ?? null ,
11801182 key : entry . key ?? null ,
11811183 } ;
11821184 commandMap . set ( entry . name , command ) ;
@@ -1314,6 +1316,26 @@ function parseKeyString(keyString) {
13141316 . filter ( Boolean ) ;
13151317}
13161318
1319+ function hasOwnBindingOverride ( name ) {
1320+ return Object . prototype . hasOwnProperty . call ( resolvedKeyBindings ?? { } , name ) ;
1321+ }
1322+
1323+ function resolveBindingInfo ( name ) {
1324+ const baseBinding = keyBindings [ name ] ?? null ;
1325+ if ( ! hasOwnBindingOverride ( name ) ) return baseBinding ;
1326+
1327+ const override = resolvedKeyBindings ?. [ name ] ;
1328+ if ( override === null ) {
1329+ return baseBinding ? { ...baseBinding , key : null } : { key : null } ;
1330+ }
1331+
1332+ if ( ! override || typeof override !== "object" ) {
1333+ return baseBinding ;
1334+ }
1335+
1336+ return baseBinding ? { ...baseBinding , ...override } : override ;
1337+ }
1338+
13171339function toCodeMirrorKey ( combo ) {
13181340 if ( ! combo ) return null ;
13191341 const parts = combo
@@ -1356,10 +1378,13 @@ function toCodeMirrorKey(combo) {
13561378function rebuildKeymap ( ) {
13571379 const bindings = [ ] ;
13581380 commandMap . forEach ( ( command , name ) => {
1359- const bindingInfo = resolvedKeyBindings ?. [ name ] ;
1381+ const bindingInfo = resolveBindingInfo ( name ) ;
13601382 command . description =
13611383 bindingInfo ?. description || command . defaultDescription ;
1362- const keySource = bindingInfo ?. key ?? command . key ?? null ;
1384+ const keySource =
1385+ bindingInfo && Object . prototype . hasOwnProperty . call ( bindingInfo , "key" )
1386+ ? bindingInfo . key
1387+ : ( command . defaultKey ?? null ) ;
13631388 command . key = keySource ;
13641389 const combos = parseKeyString ( keySource ) ;
13651390 combos . forEach ( ( combo ) => {
@@ -1410,6 +1435,19 @@ export function getRegisteredCommands() {
14101435 } ) ) ;
14111436}
14121437
1438+ export function getResolvedKeyBindings ( ) {
1439+ const bindingNames = new Set ( [
1440+ ...Object . keys ( keyBindings ) ,
1441+ ...Object . keys ( resolvedKeyBindings ?? { } ) ,
1442+ ] ) ;
1443+
1444+ return Object . fromEntries (
1445+ Array . from ( bindingNames , ( name ) => [ name , resolveBindingInfo ( name ) ] ) . filter (
1446+ ( [ , binding ] ) => binding ,
1447+ ) ,
1448+ ) ;
1449+ }
1450+
14131451export function getCommandKeymapExtension ( ) {
14141452 return commandKeymapCompartment . of ( keymap . of ( cachedKeymap ) ) ;
14151453}
0 commit comments