]> git.gir.st - VimFx.git/blob - extension/lib/vim.coffee
Don't lose state when loading tabs in the background
[VimFx.git] / extension / lib / vim.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013.
3 # Copyright Simon Lydell 2013, 2014, 2015.
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 Public API. Underscored names are private and
25 # should not be used by API consumers.
26
27 messageManager = require('./message-manager')
28 utils = require('./utils')
29
30 ChromeWindow = Ci.nsIDOMChromeWindow
31
32 class Vim
33 constructor: (browser, @_parent) ->
34 @_setBrowser(browser)
35 @_storage = {}
36
37 @_resetState()
38
39 Object.defineProperty(this, 'options', {
40 get: => @_parent.options
41 enumerable: true
42 })
43
44 # Since this is done in the constructor, defer location change handling to
45 # the next tick so that this `vim` instance is saved in `vimfx.vims` first.
46 # This allows 'locationChange' listeners to use `@vimfx.getCurrentVim()`.
47 utils.nextTick(@window, =>
48 @_onLocationChange(@browser.currentURI.spec)
49 )
50
51 # Require the subset of the options needed to be listed explicitly (as
52 # opposed to sending _all_ options) for performance. Each option access
53 # might trigger an optionOverride.
54 @_listen('options', ({ prefs }) =>
55 options = {}
56 for pref in prefs
57 options[pref] = @options[pref]
58 return options
59 )
60
61 @_listen('vimMethod', ({ method, args = [] }, { callback = null }) =>
62 result = @[method](args...)
63 @_send(callback, result) if callback
64 )
65
66 @_listen('vimMethodSync', ({ method, args = [] }) =>
67 return @[method](args...)
68 )
69
70 @_listen('DOMWindowCreated', => @_state.frameCanReceiveEvents = true)
71
72 @_listen('locationChange', @_onLocationChange.bind(this))
73
74 _setBrowser: (@browser) ->
75 @window = @browser.ownerGlobal
76 @_messageManager = @browser.messageManager
77
78 _resetState: ->
79 @_state =
80 frameCanReceiveEvents: false
81
82 _isBlacklisted: (url) -> @options.black_list.some((regex) -> regex.test(url))
83
84 isUIEvent: (event) ->
85 target = event.originalTarget
86 return not @_state.frameCanReceiveEvents or
87 if MULTI_PROCESS_ENABLED
88 (target != @window.gBrowser.selectedBrowser)
89 else
90 (target.ownerGlobal instanceof ChromeWindow)
91
92 # `args...` is passed to the mode's `onEnter` method.
93 enterMode: (mode, args...) ->
94 return if @mode == mode
95
96 unless utils.has(@_parent.modes, mode)
97 modes = Object.keys(@_parent.modes).join(', ')
98 throw new Error("VimFx: Unknown mode. Available modes are: #{ modes }.
99 Got: #{ mode }")
100
101 @_call('onLeave') if @mode?
102 @mode = mode
103 result = @_call('onEnter', null, args...)
104 @_parent.emit('modeChange', this)
105 @_send('modeChange', {mode})
106 return result
107
108 _consumeKeyEvent: (event, focusType) ->
109 return @_parent.consumeKeyEvent(event, this, focusType)
110
111 _onInput: (match, uiEvent = false) ->
112 # In the location bar, `<tab>` is used to cycle between autocomplete
113 # results. In the dev tools, `<tab>` autocompletes what you’re typing. The
114 # only way to preverve this important behavior seems to be special casing.
115 { focus_previous, focus_next } = @_parent.modes.normal.commands
116 if uiEvent and
117 ((match.command == focus_previous and match.keyStr == '<s-tab>') or
118 (match.command == focus_next and match.keyStr == '<tab>'))
119 return false
120
121 suppress = @_call('onInput', {uiEvent, count: match.count}, match)
122 return suppress
123
124 _onLocationChange: (url) ->
125 @enterMode(if @_isBlacklisted(url) then 'ignore' else 'normal')
126 @_parent.emit('locationChange', {vim: this, location: new @window.URL(url)})
127
128 _call: (method, data = {}, extraArgs...) ->
129 args = Object.assign({vim: this, storage: @_storage[@mode] ?= {}}, data)
130 currentMode = @_parent.modes[@mode]
131 return currentMode[method].call(currentMode, args, extraArgs...)
132
133 _run: (name, data = {}, callback = null) ->
134 @_send('runCommand', {name, data}, callback)
135
136 _listen: (name, listener) ->
137 messageManager.listen(name, listener, @_messageManager)
138
139 _listenOnce: (name, listener) ->
140 messageManager.listenOnce(name, listener, @_messageManager)
141
142 _send: (name, data, callback = null) ->
143 messageManager.send(name, data, @_messageManager, callback)
144
145 notify: (title, options = {}) ->
146 return unless @options.notifications_enabled
147 new @window.Notification(title, Object.assign({
148 icon: 'chrome://vimfx/skin/icon128.png'
149 tag: 'VimFx-notification'
150 }, options))
151
152 markPageInteraction: ->
153 @_send('markPageInteraction')
154
155 _focusMarkerElement: (elementIndex, options = {}) ->
156 # If you, for example, focus the location bar, unfocus it by pressing
157 # `<esc>` and then try to focus a link or text input in a web page the focus
158 # won’t work unless `@browser` is focused first.
159 @browser.focus()
160 @_run('focus_marker_element', {elementIndex, options})
161
162 module.exports = Vim
Imprint / Impressum