Mercurial
comparison .cms/lib/codemirror/src/edit/key_events.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 import { signalLater } from "../util/operation_group.js" | |
2 import { restartBlink } from "../display/selection.js" | |
3 import { isModifierKey, keyName, lookupKey } from "../input/keymap.js" | |
4 import { eventInWidget } from "../measurement/widgets.js" | |
5 import { ie, ie_version, mac, presto, gecko } from "../util/browser.js" | |
6 import { activeElt, addClass, rmClass, root } from "../util/dom.js" | |
7 import { e_preventDefault, off, on, signalDOMEvent } from "../util/event.js" | |
8 import { hasCopyEvent } from "../util/feature_detection.js" | |
9 import { Delayed, Pass } from "../util/misc.js" | |
10 | |
11 import { commands } from "./commands.js" | |
12 | |
13 // Run a handler that was bound to a key. | |
14 function doHandleBinding(cm, bound, dropShift) { | |
15 if (typeof bound == "string") { | |
16 bound = commands[bound] | |
17 if (!bound) return false | |
18 } | |
19 // Ensure previous input has been read, so that the handler sees a | |
20 // consistent view of the document | |
21 cm.display.input.ensurePolled() | |
22 let prevShift = cm.display.shift, done = false | |
23 try { | |
24 if (cm.isReadOnly()) cm.state.suppressEdits = true | |
25 if (dropShift) cm.display.shift = false | |
26 done = bound(cm) != Pass | |
27 } finally { | |
28 cm.display.shift = prevShift | |
29 cm.state.suppressEdits = false | |
30 } | |
31 return done | |
32 } | |
33 | |
34 function lookupKeyForEditor(cm, name, handle) { | |
35 for (let i = 0; i < cm.state.keyMaps.length; i++) { | |
36 let result = lookupKey(name, cm.state.keyMaps[i], handle, cm) | |
37 if (result) return result | |
38 } | |
39 return (cm.options.extraKeys && lookupKey(name, cm.options.extraKeys, handle, cm)) | |
40 || lookupKey(name, cm.options.keyMap, handle, cm) | |
41 } | |
42 | |
43 // Note that, despite the name, this function is also used to check | |
44 // for bound mouse clicks. | |
45 | |
46 let stopSeq = new Delayed | |
47 | |
48 export function dispatchKey(cm, name, e, handle) { | |
49 let seq = cm.state.keySeq | |
50 if (seq) { | |
51 if (isModifierKey(name)) return "handled" | |
52 if (/\'$/.test(name)) | |
53 cm.state.keySeq = null | |
54 else | |
55 stopSeq.set(50, () => { | |
56 if (cm.state.keySeq == seq) { | |
57 cm.state.keySeq = null | |
58 cm.display.input.reset() | |
59 } | |
60 }) | |
61 if (dispatchKeyInner(cm, seq + " " + name, e, handle)) return true | |
62 } | |
63 return dispatchKeyInner(cm, name, e, handle) | |
64 } | |
65 | |
66 function dispatchKeyInner(cm, name, e, handle) { | |
67 let result = lookupKeyForEditor(cm, name, handle) | |
68 | |
69 if (result == "multi") | |
70 cm.state.keySeq = name | |
71 if (result == "handled") | |
72 signalLater(cm, "keyHandled", cm, name, e) | |
73 | |
74 if (result == "handled" || result == "multi") { | |
75 e_preventDefault(e) | |
76 restartBlink(cm) | |
77 } | |
78 | |
79 return !!result | |
80 } | |
81 | |
82 // Handle a key from the keydown event. | |
83 function handleKeyBinding(cm, e) { | |
84 let name = keyName(e, true) | |
85 if (!name) return false | |
86 | |
87 if (e.shiftKey && !cm.state.keySeq) { | |
88 // First try to resolve full name (including 'Shift-'). Failing | |
89 // that, see if there is a cursor-motion command (starting with | |
90 // 'go') bound to the keyname without 'Shift-'. | |
91 return dispatchKey(cm, "Shift-" + name, e, b => doHandleBinding(cm, b, true)) | |
92 || dispatchKey(cm, name, e, b => { | |
93 if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion) | |
94 return doHandleBinding(cm, b) | |
95 }) | |
96 } else { | |
97 return dispatchKey(cm, name, e, b => doHandleBinding(cm, b)) | |
98 } | |
99 } | |
100 | |
101 // Handle a key from the keypress event | |
102 function handleCharBinding(cm, e, ch) { | |
103 return dispatchKey(cm, "'" + ch + "'", e, b => doHandleBinding(cm, b, true)) | |
104 } | |
105 | |
106 let lastStoppedKey = null | |
107 export function onKeyDown(e) { | |
108 let cm = this | |
109 if (e.target && e.target != cm.display.input.getField()) return | |
110 cm.curOp.focus = activeElt(root(cm)) | |
111 if (signalDOMEvent(cm, e)) return | |
112 // IE does strange things with escape. | |
113 if (ie && ie_version < 11 && e.keyCode == 27) e.returnValue = false | |
114 let code = e.keyCode | |
115 cm.display.shift = code == 16 || e.shiftKey | |
116 let handled = handleKeyBinding(cm, e) | |
117 if (presto) { | |
118 lastStoppedKey = handled ? code : null | |
119 // Opera has no cut event... we try to at least catch the key combo | |
120 if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey)) | |
121 cm.replaceSelection("", null, "cut") | |
122 } | |
123 if (gecko && !mac && !handled && code == 46 && e.shiftKey && !e.ctrlKey && document.execCommand) | |
124 document.execCommand("cut") | |
125 | |
126 // Turn mouse into crosshair when Alt is held on Mac. | |
127 if (code == 18 && !/\bCodeMirror-crosshair\b/.test(cm.display.lineDiv.className)) | |
128 showCrossHair(cm) | |
129 } | |
130 | |
131 function showCrossHair(cm) { | |
132 let lineDiv = cm.display.lineDiv | |
133 addClass(lineDiv, "CodeMirror-crosshair") | |
134 | |
135 function up(e) { | |
136 if (e.keyCode == 18 || !e.altKey) { | |
137 rmClass(lineDiv, "CodeMirror-crosshair") | |
138 off(document, "keyup", up) | |
139 off(document, "mouseover", up) | |
140 } | |
141 } | |
142 on(document, "keyup", up) | |
143 on(document, "mouseover", up) | |
144 } | |
145 | |
146 export function onKeyUp(e) { | |
147 if (e.keyCode == 16) this.doc.sel.shift = false | |
148 signalDOMEvent(this, e) | |
149 } | |
150 | |
151 export function onKeyPress(e) { | |
152 let cm = this | |
153 if (e.target && e.target != cm.display.input.getField()) return | |
154 if (eventInWidget(cm.display, e) || signalDOMEvent(cm, e) || e.ctrlKey && !e.altKey || mac && e.metaKey) return | |
155 let keyCode = e.keyCode, charCode = e.charCode | |
156 if (presto && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return} | |
157 if ((presto && (!e.which || e.which < 10)) && handleKeyBinding(cm, e)) return | |
158 let ch = String.fromCharCode(charCode == null ? keyCode : charCode) | |
159 // Some browsers fire keypress events for backspace | |
160 if (ch == "\x08") return | |
161 if (handleCharBinding(cm, e, ch)) return | |
162 cm.display.input.onKeyPress(e) | |
163 } |