vendor: 1,202 bytes, lines 1-39
1import { keyName, base } from 'w3c-keyname'; 2import { Plugin } from 'prosemirror-state'; 3 4const mac = typeof navigator != "undefined" ? /Mac|iP(hone|[oa]d)/.test(navigator.platform) : false; 5function normalizeKeyName(name) { 6 let parts = name.split(/-(?!$)/), result = parts[parts.length - 1]; 7 if (result == "Space") 8 result = " "; 9 let alt, ctrl, shift, meta; 10 for (let i = 0; i < parts.length - 1; i++) { 11 let mod = parts[i]; 12 if (/^(cmd|meta|m)$/i.test(mod)) 13 meta = true; 14 else if (/^a(lt)?$/i.test(mod)) 15 alt = true; 16 else if (/^(c|ctrl|control)$/i.test(mod)) 17 ctrl = true; 18 else if (/^s(hift)?$/i.test(mod)) 19 shift = true; 20 else if (/^mod$/i.test(mod)) { 21 if (mac) 22 meta = true; 23 else 24 ctrl = true; 25 } 26 else 27 throw new Error("Unrecognized modifier name: " + mod); 28 } 29 if (alt) 30 result = "Alt-" + result; 31 if (ctrl) 32 result = "Ctrl-" + result; 33 if (meta) 34 result = "Meta-" + result; 35 if (shift) 36 result = "Shift-" + result; 37 return result; 38} 39function normalize(map) {
vendor: 3,528 bytes, lines 40-125
40 let copy = Object.create(null); 41 for (let prop in map) 42 copy[normalizeKeyName(prop)] = map[prop]; 43 return copy; 44} 45function modifiers(name, event, shift = true) { 46 if (event.altKey) 47 name = "Alt-" + name; 48 if (event.ctrlKey) 49 name = "Ctrl-" + name; 50 if (event.metaKey) 51 name = "Meta-" + name; 52 if (shift && event.shiftKey) 53 name = "Shift-" + name; 54 return name; 55} 56/** 57Create a keymap plugin for the given set of bindings. 58 59Bindings should map key names to [command](https://prosemirror.net/docs/ref/#commands)-style 60functions, which will be called with `(EditorState, dispatch, 61EditorView)` arguments, and should return true when they've handled 62the key. Note that the view argument isn't part of the command 63protocol, but can be used as an escape hatch if a binding needs to 64directly interact with the UI. 65 66Key names may be strings like `"Shift-Ctrl-Enter"`âa key 67identifier prefixed with zero or more modifiers. Key identifiers 68are based on the strings that can appear in 69[`KeyEvent.key`](https:developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key). 70Use lowercase letters to refer to letter keys (or uppercase letters 71if you want shift to be held). You may use `"Space"` as an alias 72for the `" "` name. 73 74Modifiers can be given in any order. `Shift-` (or `s-`), `Alt-` (or 75`a-`), `Ctrl-` (or `c-` or `Control-`) and `Cmd-` (or `m-` or 76`Meta-`) are recognized. For characters that are created by holding 77shift, the `Shift-` prefix is implied, and should not be added 78explicitly. 79 80You can use `Mod-` as a shorthand for `Cmd-` on Mac and `Ctrl-` on 81other platforms. 82 83You can add multiple keymap plugins to an editor. The order in 84which they appear determines their precedence (the ones early in 85the array get to dispatch first). 86*/ 87function keymap(bindings) { 88 return new Plugin({ props: { handleKeyDown: keydownHandler(bindings) } }); 89} 90/** 91Given a set of bindings (using the same format as 92[`keymap`](https://prosemirror.net/docs/ref/#keymap.keymap)), return a [keydown 93handler](https://prosemirror.net/docs/ref/#view.EditorProps.handleKeyDown) that handles them. 94*/ 95function keydownHandler(bindings) { 96 let map = normalize(bindings); 97 return function (view, event) { 98 let name = keyName(event), baseName, direct = map[modifiers(name, event)]; 99 if (direct && direct(view.state, view.dispatch, view)) 100 return true; 101 // A character key 102 if (name.length == 1 && name != " ") { 103 if (event.shiftKey) { 104 // In case the name was already modified by shift, try looking 105 // it up without its shift modifier 106 let noShift = map[modifiers(name, event, false)]; 107 if (noShift && noShift(view.state, view.dispatch, view)) 108 return true; 109 } 110 if ((event.shiftKey || event.altKey || event.metaKey || name.charCodeAt(0) > 127) && 111 (baseName = base[event.keyCode]) && baseName != name) { 112 // Try falling back to the keyCode when there's a modifier 113 // active or the character produced isn't ASCII, and our table 114 // produces a different name from the the keyCode. See #668, 115 // #1060 116 let fromCode = map[modifiers(baseName, event)]; 117 if (fromCode && fromCode(view.state, view.dispatch, view)) 118 return true; 119 } 120 } 121 return false; 122 }; 123} 124 125export { keydownHandler, keymap };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.