Skip to content

Commit

Permalink
Don't pass the keyboard event to commands from keymap
Browse files Browse the repository at this point in the history
FIX: Fix an issue where commands with an optional second argument would get
the keyboard event in that argument when called from a keymap.

Closes codemirror/dev#1368
  • Loading branch information
marijnh committed Apr 13, 2024
1 parent 6555b95 commit 70ebd2a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ function modifiers(name: string, event: KeyboardEvent, shift: boolean) {
type Binding = {
preventDefault: boolean,
stopPropagation: boolean,
run: ((view: EditorView, event: KeyboardEvent) => boolean)[]
run: ((view: EditorView) => boolean)[]
}

// In each scope, the `_all` property is used for bindings that apply
// In each scope, the `_any` property is used for bindings that apply
// to all keys.
type Keymap = {[scope: string]: {[key: string]: Binding}}

Expand Down Expand Up @@ -199,7 +199,8 @@ function buildKeymap(bindings: readonly KeyBinding[], platform = currentPlatform
if (b.any) for (let scope of scopes) {
let scopeObj = bound[scope] || (bound[scope] = Object.create(null))
if (!scopeObj._any) scopeObj._any = {preventDefault: false, stopPropagation: false, run: []}
for (let key in scopeObj) scopeObj[key].run.push(b.any)
let {any} = b
for (let key in scopeObj) scopeObj[key].run.push(view => any(view, currentKeyEvent!))
}
let name = b[platform] || b.key
if (!name) continue
Expand All @@ -211,7 +212,10 @@ function buildKeymap(bindings: readonly KeyBinding[], platform = currentPlatform
return bound
}

let currentKeyEvent: KeyboardEvent | null = null

function runHandlers(map: Keymap, event: KeyboardEvent, view: EditorView, scope: string): boolean {
currentKeyEvent = event
let name = keyName(event)
let charCode = codePointAt(name, 0), isChar = codePointSize(charCode) == name.length && name != " "
let prefix = "", handled = false, prevented = false, stopPropagation = false
Expand All @@ -228,7 +232,7 @@ function runHandlers(map: Keymap, event: KeyboardEvent, view: EditorView, scope:
if (binding) {
for (let cmd of binding.run) if (!ran.has(cmd)) {
ran.add(cmd)
if (cmd(view, event)) {
if (cmd(view)) {
if (binding.stopPropagation) stopPropagation = true
return true
}
Expand Down Expand Up @@ -264,5 +268,6 @@ function runHandlers(map: Keymap, event: KeyboardEvent, view: EditorView, scope:

if (prevented) handled = true
if (handled && stopPropagation) event.stopPropagation()
currentKeyEvent = null
return handled
}

0 comments on commit 70ebd2a

Please sign in to comment.