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