]> git.gir.st - VimFx.git/blob - extension/packages/events.coffee
Merge branch 'develop'
[VimFx.git] / extension / packages / events.coffee
1 utils = require 'utils'
2 keyUtils = require 'key-utils'
3 { Vim } = require 'vim'
4 { getPref } = require 'prefs'
5 { updateToolbarButton } = require 'button'
6 { unload } = require 'unload'
7
8 { interfaces: Ci } = Components
9
10 HTMLDocument = Ci.nsIDOMHTMLDocument
11
12 vimBucket = new utils.Bucket(utils.getWindowId, (w) -> new Vim(w))
13
14 keyStrFromEvent = (event) ->
15 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
16
17 if !meta and !alt
18 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
19 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
20 return keyStr
21
22 return null
23
24 # When a menu or panel is shown VimFx should temporarily stop processing keyboard input, allowing
25 # accesskeys to be used.
26 popupPassthrough = false
27 checkPassthrough = (event) ->
28 if event.target.nodeName in ['menupopup', 'panel']
29 popupPassthrough = switch event.type
30 when 'popupshown' then true
31 when 'popuphidden' then false
32
33 suppress = false
34 suppressEvent = (event) ->
35 event.preventDefault()
36 event.stopPropagation()
37
38 removeVimFromTab = (tab, gBrowser) ->
39 return unless browser = gBrowser.getBrowserForTab(tab)
40 vimBucket.forget(browser.contentWindow)
41
42 updateButton = (vim) ->
43 updateToolbarButton(vim.rootWindow, {blacklisted: vim.blacklisted, insertMode: vim.mode == 'insert'})
44
45 # The following listeners are installed on every top level Chrome window
46 windowsListeners =
47 keydown: (event) ->
48 try
49 # No matter what, always reset the `suppress` flag, so we don't suppress more than intended.
50 suppress = false
51
52 return if getPref('disabled')
53
54 if popupPassthrough
55 # The `popupPassthrough` flag is set a bit unreliably. Sometimes it can be stuck as `true`
56 # even though no popup is shown, effectively disabling the extension. Therefore we check
57 # if there actually _are_ any open popups before stopping processing keyboard input. This is
58 # only done when popups (might) be open (not on every keystroke) of performance reasons.
59 return unless rootWindow = utils.getEventRootWindow(event)
60 popups = rootWindow.document.querySelectorAll('menupopup, panel')
61 for popup in popups
62 return if popup.state == 'open'
63 popupPassthrough = false # No popup was actually open: Reset the flag.
64
65 return unless window = utils.getEventCurrentTabWindow(event)
66 return unless vim = vimBucket.get(window)
67
68 return if vim.blacklisted
69
70 return unless keyStr = keyStrFromEvent(event)
71 suppress = vim.onInput(keyStr, event)
72
73 suppressEvent(event) if suppress
74
75 catch error
76 console.error("#{ error }\n#{ error.stack?.replace(/@.+-> /g, '@') }")
77
78 # Note that the below event listeners can suppress the event even in blacklisted sites. That's
79 # intentional. For example, if you press 'x' to close the current tab, it will close before keyup
80 # fires. So keyup (and perhaps keypress) will fire in another tab. Even if that particular tab is
81 # blacklisted, we must suppress the event, so that 'x' isn't sent to the page. The rule is simple:
82 # If the `suppress` flag is `true`, the event should be suppressed, no matter what. It has the
83 # highest priority.
84 keypress: (event) -> suppressEvent(event) if suppress
85 keyup: (event) -> suppressEvent(event) if suppress
86
87 popupshown: checkPassthrough
88 popuphidden: checkPassthrough
89
90 focus: (event) ->
91 return if getPref('disabled') or getPref('prevent_autofocus')
92 return unless target = event.originalTarget
93 return unless target.ownerDocument instanceof HTMLDocument
94 return unless window = utils.getEventCurrentTabWindow(event)
95 return unless vim = vimBucket.get(window)
96 return unless isEditable = utils.isElementEditable(target)
97 return unless lastLoad = vim.storage.lastLoad
98
99 if (new Date().getTime() - lastLoad.getTime()) < 1000
100 console.log("blur")
101 vim.storage.lastLoad = undefined
102 window.setTimeout((-> target.blur()), 0)
103
104 DOMContentLoaded: (event) ->
105 return if getPref('disabled') or getPref('prevent_autofocus')
106 return unless window = utils.getEventCurrentTabWindow(event)
107 return unless vim = vimBucket.get(window)
108 if vim.storage.location != window.location.href
109 vim.storage.location = window.location.href
110 vim.storage.lastLoad = new Date
111
112 if vim.storage.lastLoad && activeElement = window.document.activeElement
113 console.log("blur")
114 vim.storage.lastLoad = undefined
115 window.setTimeout((-> activeElement.blur()), 0)
116
117 # When the top level window closes we should release all Vims that were
118 # associated with tabs in this window
119 DOMWindowClose: (event) ->
120 { gBrowser } = event.originalTarget
121 return unless gBrowser
122 for tab in gBrowser.tabs
123 removeVimFromTab(tab, gBrowser)
124
125 TabClose: (event) ->
126 { gBrowser } = utils.getEventRootWindow(event) ? {}
127 return unless gBrowser
128 tab = event.originalTarget
129 removeVimFromTab(tab, gBrowser)
130
131 # Update the toolbar button icon to reflect the blacklisted state
132 TabSelect: (event) ->
133 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
134 return unless vim = vimBucket.get(window)
135 updateButton(vim)
136
137
138 # This listener works on individual tabs within Chrome Window
139 tabsListener =
140 # Listenfor location changes and disable the extension on blacklisted urls
141 onLocationChange: (browser, webProgress, request, location) ->
142 return unless vim = vimBucket.get(browser.contentWindow)
143
144 # If the location changes when in hints mode (for example because the reload button has been
145 # clicked), we're going to end up in hints mode without any markers. So switch back to normal
146 # mode in that case.
147 if vim.mode == 'hints'
148 vim.enterMode('normal')
149
150 vim.blacklisted = utils.isBlacklisted(location.spec)
151 updateButton(vim)
152
153 addEventListeners = (window) ->
154 for name, listener of windowsListeners
155 window.addEventListener(name, listener, true)
156
157 window.gBrowser.addTabsProgressListener(tabsListener)
158
159 unload ->
160 for name, listener of windowsListeners
161 window.removeEventListener(name, listener, true)
162
163 window.gBrowser.removeTabsProgressListener(tabsListener)
164
165 exports.addEventListeners = addEventListeners
166 exports.vimBucket = vimBucket
Imprint / Impressum