]> git.gir.st - VimFx.git/blob - extension/packages/vim.coffee
Merge branch 'hints-z-index' of git://github.com/lydell/VimFx into lydell-hints-z...
[VimFx.git] / extension / packages / vim.coffee
1 utils = require 'utils'
2 { modes } = require 'modes'
3 { isEscCommandKey
4 , isReturnCommandKey } = require 'commands'
5
6 class Vim
7 constructor: (@window) ->
8 @storage = {}
9 @enterMode('normal')
10
11 enterMode: (mode, args...) ->
12 # `args` is an array of arguments to be passed to the mode's `onEnter` method
13
14 if mode not of modes
15 throw new Error("Not a valid VimFx mode to enter: #{ mode }")
16
17 if @mode != mode
18 if @mode of modes
19 modes[@mode].onLeave(@, @storage[@mode])
20
21 @mode = mode
22
23 modes[@mode].onEnter(@, @storage[@mode] ?= {}, args...)
24
25 onInput: (keyStr, event) ->
26 isEditable = utils.isElementEditable(event.originalTarget)
27
28 if isEditable and not (isEscCommandKey(keyStr) or isReturnCommandKey(keyStr))
29 return false
30
31 oldMode = @mode
32
33 suppress = modes[@mode]?.onInput(@, @storage[@mode], keyStr, event)
34
35 # Esc key is not suppressed, and passed to the browser in `normal` mode.
36 # Here we compare against the mode that was active before the key was
37 # processed because processing the command may change the mode.
38 #
39 # Not suppressing Esc allows for stopping the loading of the page as well as
40 # closing many custom dialogs (and perhaps other things -- Esc is a very
41 # commonly used key). There are two reasons we might suppress it in other
42 # modes. If some custom dialog of a website is open, we should be able to
43 # cancel hint markers on it without closing it. Secondly, otherwise
44 # cancelling hint markers on google causes its search bar to be focused.
45 if oldMode == 'normal' and keyStr == 'Esc'
46 return false
47 else
48 return suppress
49
50 exports.Vim = Vim
Imprint / Impressum