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