]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Simplify and improve Find mode auto-enter and -exit
[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 @focusType = 'none'
37 @_setBrowser(browser, {addListeners: false})
38 @_storage = {}
39
40 @_resetState()
41
42 Object.defineProperty(this, 'options', {
43 get: => @_parent.options
44 enumerable: true
45 })
46
47 _start: ->
48 @_onLocationChange(@browser.currentURI.spec)
49 @_addListeners()
50 focusType = utils.getFocusType(utils.getActiveElement(@window))
51 @_setFocusType(focusType)
52
53 _addListeners: ->
54 @_listen('vimMethod', ({method, args = []}, callback = null) =>
55 result = @[method](args...)
56 callback?(result)
57 )
58
59 @_listen('vimMethodSync', ({method, args = []}) =>
60 return @[method](args...)
61 )
62
63 @_listen('locationChange', @_onLocationChange.bind(this))
64
65 @_listen('frameCanReceiveEvents', (value) =>
66 @_state.frameCanReceiveEvents = value
67 )
68
69 @_listen('focusType', (focusType) =>
70 # If the focus moves from a web page element to a browser UI element, the
71 # focus and blur events happen in the expected order, but the message from
72 # the frame script arrives too late. Therefore, check that the currently
73 # active element isn’t a browser UI element first.
74 unless @_isUIElement(utils.getActiveElement(@window))
75 @_setFocusType(focusType)
76 )
77
78 _setBrowser: (@browser, {addListeners = true} = {}) ->
79 @window = @browser.ownerGlobal
80 @_messageManager = @browser.messageManager
81
82 @_statusPanel?.remove()
83 @_statusPanel = statusPanel.injectStatusPanel(@browser)
84 @_statusPanel.onclick = @hideNotification.bind(this)
85
86 @_addListeners() if addListeners
87
88 _resetState: ->
89 @_state = {
90 frameCanReceiveEvents: false
91 scrollableElements: new ScrollableElements(@window)
92 }
93
94 _isBlacklisted: (url) -> @options.blacklist.some((regex) -> regex.test(url))
95
96 isUIEvent: (event) ->
97 return not @_state.frameCanReceiveEvents or
98 @_isUIElement(event.originalTarget)
99
100 _isUIElement: (element) ->
101 # TODO: The `element.ownerGlobal` check will be redundant when
102 # non-multi-process is removed from Firefox.
103 return element.ownerGlobal instanceof ChromeWindow and
104 element != @window.gBrowser.selectedBrowser
105
106 # `args...` is passed to the mode's `onEnter` method.
107 enterMode: (mode, args...) ->
108 return if @mode == mode
109
110 unless utils.has(@_parent.modes, mode)
111 modes = Object.keys(@_parent.modes).join(', ')
112 throw new Error("VimFx: Unknown mode. Available modes are: #{modes}.
113 Got: #{mode}")
114
115 @_call('onLeave') if @mode?
116 @mode = mode
117 result = @_call('onEnter', null, args...)
118 @_parent.emit('modeChange', {vim: this})
119 @_send('modeChange', {mode})
120 return result
121
122 _consumeKeyEvent: (event) ->
123 return @_parent.consumeKeyEvent(event, this)
124
125 _onInput: (match, event) ->
126 uiEvent = if @isUIEvent(event) then event else false
127 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
128 return suppress
129
130 _onLocationChange: (url) ->
131 unless @mode == 'ignore' and @_storage.ignore.type == 'explicit'
132 if @_isBlacklisted(url)
133 @enterMode('ignore', {type: 'blacklist'})
134 else
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 @_parent.emit('notification', {vim: this, message})
162 if @options.notifications_enabled
163 @_statusPanel.setAttribute('label', message)
164 @_statusPanel.removeAttribute('inactive')
165
166 hideNotification: ->
167 @_parent.emit('hideNotification', {vim: this})
168 @_statusPanel.setAttribute('inactive', 'true')
169
170 markPageInteraction: (value = null) -> @_send('markPageInteraction', value)
171
172 _focusMarkerElement: (elementIndex, options = {}) ->
173 # If you, for example, focus the location bar, unfocus it by pressing
174 # `<esc>` and then try to focus a link or text input in a web page the focus
175 # won’t work unless `@browser` is focused first.
176 @browser.focus()
177 @_run('focus_marker_element', {elementIndex, options})
178
179 _setFocusType: (@focusType) ->
180 switch
181 when @focusType == 'ignore'
182 @enterMode('ignore', {type: 'focusType'})
183 when @mode == 'ignore' and @_storage.ignore.type == 'focusType'
184 @enterMode('normal')
185 when @mode == 'normal' and @focusType == 'findbar'
186 @enterMode('find')
187 when @mode == 'find' and @focusType != 'findbar'
188 @enterMode('normal')
189 @_parent.emit('focusTypeChange', {vim: this})
190
191 module.exports = Vim
Imprint / Impressum