aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_passkeys.coffee
blob: 82f7596b0d2aab50fd0f19554f261abf16f5ad1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class PassKeysMode extends Mode
  keyQueue: ""
  passKeys: ""

  # Decide whether this keyChar should be passed to the underlying page.  Keystrokes are *never* considered
  # passKeys if the keyQueue is not empty.  So, for example, if 't' is a passKey, then 'gt' and '99t' will
  # neverthless be handled by vimium.
  isPassKey: (keyChar) ->
    not @keyQueue and 0 <= @passKeys.indexOf(keyChar)

  handlePassKeyEvent: (event) ->
    for keyChar in [KeyboardUtils.getKeyChar(event), String.fromCharCode(event.charCode)]
      # A key is passed through to the underlying page by returning handlerStack.passDirectlyToPage.
      return handlerStack.passDirectlyToPage if keyChar and @isPassKey keyChar
    Mode.propagate

  # This is called to set the pass-keys state with various types of request from various sources, so we handle
  # all of these.
  # TODO(smblott) Rationalize this.
  setState: (request) ->
    if request.isEnabledForUrl?
      @passKeys = (request.isEnabledForUrl and request.passKeys) or ""
    if request.enabled?
      @passKeys = (request.enabled and request.passKeys) or ""
    if request.keyQueue?
      @keyQueue = request.keyQueue
    Mode.updateBadge()

  constructor: ->
    super
      name: "passkeys"
      keydown: (event) => @handlePassKeyEvent event
      keypress: (event) => @handlePassKeyEvent event
      keyup: -> Mode.propagate

  # Overriding and re-using updateBadgeForMode() from Mode.updateBadgeForMode().
  updateBadgeForMode: (badge) ->
    @badge = if @passKeys and not @keyQueue then "P" else ""
    super badge

root = exports ? window
root.PassKeysMode = PassKeysMode