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