openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

key_events.js (5237B)


      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 } from "../util/browser.js"
      6 import { activeElt, addClass, rmClass } 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   cm.curOp.focus = activeElt()
    110   if (signalDOMEvent(cm, e)) return
    111   // IE does strange things with escape.
    112   if (ie && ie_version < 11 && e.keyCode == 27) e.returnValue = false
    113   let code = e.keyCode
    114   cm.display.shift = code == 16 || e.shiftKey
    115   let handled = handleKeyBinding(cm, e)
    116   if (presto) {
    117     lastStoppedKey = handled ? code : null
    118     // Opera has no cut event... we try to at least catch the key combo
    119     if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey))
    120       cm.replaceSelection("", null, "cut")
    121   }
    122 
    123   // Turn mouse into crosshair when Alt is held on Mac.
    124   if (code == 18 && !/\bCodeMirror-crosshair\b/.test(cm.display.lineDiv.className))
    125     showCrossHair(cm)
    126 }
    127 
    128 function showCrossHair(cm) {
    129   let lineDiv = cm.display.lineDiv
    130   addClass(lineDiv, "CodeMirror-crosshair")
    131 
    132   function up(e) {
    133     if (e.keyCode == 18 || !e.altKey) {
    134       rmClass(lineDiv, "CodeMirror-crosshair")
    135       off(document, "keyup", up)
    136       off(document, "mouseover", up)
    137     }
    138   }
    139   on(document, "keyup", up)
    140   on(document, "mouseover", up)
    141 }
    142 
    143 export function onKeyUp(e) {
    144   if (e.keyCode == 16) this.doc.sel.shift = false
    145   signalDOMEvent(this, e)
    146 }
    147 
    148 export function onKeyPress(e) {
    149   let cm = this
    150   if (eventInWidget(cm.display, e) || signalDOMEvent(cm, e) || e.ctrlKey && !e.altKey || mac && e.metaKey) return
    151   let keyCode = e.keyCode, charCode = e.charCode
    152   if (presto && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return}
    153   if ((presto && (!e.which || e.which < 10)) && handleKeyBinding(cm, e)) return
    154   let ch = String.fromCharCode(charCode == null ? keyCode : charCode)
    155   // Some browsers fire keypress events for backspace
    156   if (ch == "\x08") return
    157   if (handleCharBinding(cm, e, ch)) return
    158   cm.display.input.onKeyPress(e)
    159 }