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