]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Fix getting stuck in normal mode in docked DevTools after refocusing
[VimFx.git] / extension / lib / vim.coffee
1 # This file defines the `vim` API, available to all modes and commands. There is
2 # one `Vim` instance for each tab. Most importantly, it provides access to the
3 # owning Firefox window and the current mode, and allows you to change mode.
4 # `vim` objects are exposed by the config file API. Underscored names are
5 # private and should not be used by API consumers.
6
7 messageManager = require('./message-manager')
8 ScrollableElements = require('./scrollable-elements')
9 utils = require('./utils')
10
11 ChromeWindow = Ci.nsIDOMChromeWindow
12
13 class Vim
14 constructor: (browser, @_parent) ->
15 @mode = undefined
16 @focusType = 'none'
17 @_setBrowser(browser, {addListeners: false})
18 @_storage = {}
19
20 @_resetState()
21
22 Object.defineProperty(this, 'options', {
23 get: => @_parent.options
24 enumerable: true
25 })
26
27 _start: ->
28 @_onLocationChange(@browser.currentURI.spec)
29 @_addListeners()
30 focusType = utils.getFocusType(utils.getActiveElement(@window))
31 @_setFocusType(focusType)
32
33 _addListeners: ->
34 # Require the subset of the options needed to be listed explicitly (as
35 # opposed to sending _all_ options) for performance. Each option access
36 # might trigger an optionOverride.
37 @_listen('options', ({prefs}) =>
38 options = {}
39 for pref in prefs
40 options[pref] = @options[pref]
41 return options
42 )
43
44 @_listen('vimMethod', ({method, args = []}, callback = null) =>
45 result = this[method](args...)
46 callback?(result)
47 )
48
49 @_listen('vimMethodSync', ({method, args = []}) =>
50 return this[method](args...)
51 )
52
53 @_listen('locationChange', @_onLocationChange.bind(this))
54
55 @_listen('frameCanReceiveEvents', (value) =>
56 @_state.frameCanReceiveEvents = value
57 )
58
59 @_listen('focusType', (focusType) =>
60 # If the focus moves from a web page element to a browser UI element, the
61 # focus and blur events happen in the expected order, but the message from
62 # the frame script arrives too late. Therefore, check that the currently
63 # active element isn’t a browser UI element first.
64 unless @_isUIElement(utils.getActiveElement(@window))
65 @_setFocusType(focusType)
66 )
67
68 _setBrowser: (@browser, {addListeners = true} = {}) ->
69 @window = @browser.ownerGlobal
70 @_messageManager = @browser.messageManager
71
72 @_addListeners() if addListeners
73
74 _resetState: ->
75 @_state = {
76 frameCanReceiveEvents: false
77 scrollableElements: new ScrollableElements(@window)
78 lastNotification: null
79 persistentNotification: null
80 enteredKeys: []
81 enteredKeysTimeout: null
82 }
83
84 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
85
86 isUIEvent: (event) ->
87 return not @_state.frameCanReceiveEvents or
88 @_isUIElement(event.originalTarget)
89
90 _isUIElement: (element) ->
91 return (element.ownerGlobal instanceof ChromeWindow or
92 utils.isDockedDevtoolsElement(element)) and
93 element != @window.gBrowser.selectedBrowser
94
95 # `args...` is passed to the mode's `onEnter` method.
96 _enterMode: (mode, args...) ->
97 return if @mode == mode
98
99 unless utils.has(@_parent.modes, mode)
100 modes = Object.keys(@_parent.modes).join(', ')
101 throw new Error(
102 "VimFx: Unknown mode. Available modes are: #{modes}. Got: #{mode}"
103 )
104
105 @_call('onLeave') if @mode?
106 @mode = mode
107 @_call('onEnter', null, args...)
108 @_parent.emit('modeChange', {vim: this})
109 @_send('modeChange', {mode})
110
111 _consumeKeyEvent: (event) ->
112 match = @_parent.consumeKeyEvent(event, this)
113 return match if not match or typeof match == 'boolean'
114
115 if @options.notify_entered_keys
116 if match.type in ['none', 'full'] or match.likelyConflict
117 @_clearEnteredKeys()
118 else
119 @_pushEnteredKey(match.keyStr)
120 else
121 @hideNotification()
122
123 return match
124
125 _onInput: (match, event) ->
126 suppress = @_call('onInput', {event, count: match.count}, match)
127 return suppress
128
129 _onLocationChange: (url) ->
130 switch
131 when @_isBlacklisted(url)
132 @_enterMode('ignore', {type: 'blacklist'})
133 when (@mode == 'ignore' and @_storage.ignore.type == 'blacklist') or
134 not @mode
135 @_enterMode('normal')
136 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
137
138 _call: (method, data = {}, extraArgs...) ->
139 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
140 currentMode = @_parent.modes[@mode]
141 return currentMode[method].call(currentMode, args, extraArgs...)
142
143 _run: (name, data = {}, callback = null) ->
144 @_send('runCommand', {name, data}, callback)
145
146 _messageManagerOptions: (options) ->
147 return Object.assign({
148 messageManager: @_messageManager
149 }, options)
150
151 _listen: (name, listener, options = {}) ->
152 messageManager.listen(name, listener, @_messageManagerOptions(options))
153
154 _listenOnce: (name, listener, options = {}) ->
155 messageManager.listenOnce(name, listener, @_messageManagerOptions(options))
156
157 _send: (name, data, callback = null, options = {}) ->
158 messageManager.send(name, data, callback, @_messageManagerOptions(options))
159
160 notify: (message) ->
161 @_state.lastNotification = message
162 @_parent.emit('notification', {vim: this, message})
163 if @options.notifications_enabled
164 @window.StatusPanel?._label = message
165
166 _notifyPersistent: (message) ->
167 @_state.persistentNotification = message
168 @notify(message)
169
170 _refreshPersistentNotification: ->
171 @notify(@_state.persistentNotification) if @_state.persistentNotification
172
173 hideNotification: ->
174 @_parent.emit('hideNotification', {vim: this})
175 @window.StatusPanel?._label = '' # or .update()
176 @_state.lastNotification = null
177 @_state.persistentNotification = null
178
179 _clearEnteredKeys: ->
180 @_clearEnteredKeysTimeout()
181 return unless @_state.enteredKeys.length > 0
182
183 @_state.enteredKeys = []
184 if @_state.persistentNotification
185 @notify(@_state.persistentNotification)
186 else
187 @hideNotification()
188
189 _pushEnteredKey: (keyStr) ->
190 @_state.enteredKeys.push(keyStr)
191 @_clearEnteredKeysTimeout()
192 @notify(@_state.enteredKeys.join(''))
193 clear = @_clearEnteredKeys.bind(this)
194 @_state.enteredKeysTimeout =
195 @window.setTimeout((-> clear()), @options.timeout)
196
197 _clearEnteredKeysTimeout: ->
198 if @_state.enteredKeysTimeout?
199 @window.clearTimeout(@_state.enteredKeysTimeout)
200 @_state.enteredKeysTimeout = null
201
202 _modal: (type, args, callback = null) ->
203 @_run('modal', {type, args}, callback)
204
205 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
206
207 _focusMarkerElement: (elementIndex, options = {}) ->
208 # If you, for example, focus the location bar, unfocus it by pressing
209 # `<esc>` and then try to focus a link or text input in a web page the focus
210 # won’t work unless `@browser` is focused first.
211 @browser.focus()
212 browserOffset = @_getBrowserOffset()
213 @_run('focus_marker_element', {elementIndex, browserOffset, options})
214
215 _setFocusType: (focusType) ->
216 return if focusType == @focusType
217 @focusType = focusType
218 switch
219 when @focusType == 'ignore'
220 @_enterMode('ignore', {type: 'focusType'})
221 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
222 @_enterMode('normal')
223 when @mode == 'normal' and @focusType == 'findbar'
224 @_enterMode('find')
225 when @mode == 'find' and @focusType != 'findbar'
226 @_enterMode('normal')
227 @_parent.emit('focusTypeChange', {vim: this})
228
229 _getBrowserOffset: ->
230 browserRect = @browser.getBoundingClientRect()
231 return {
232 x: @window.screenX + browserRect.left
233 y: @window.screenY + browserRect.top
234 }
235
236 module.exports = Vim
Imprint / Impressum