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'
8 { interfaces: Ci } = Components
10 HTMLDocument = Ci.nsIDOMHTMLDocument
12 vimBucket = new utils.Bucket(utils.getWindowId, (w) -> new Vim(w))
14 keyStrFromEvent = (event) ->
15 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
18 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
19 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
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
34 suppressEvent = (event) ->
35 event.preventDefault()
36 event.stopPropagation()
38 removeVimFromTab = (tab, gBrowser) ->
39 return unless browser = gBrowser.getBrowserForTab(tab)
40 vimBucket.forget(browser.contentWindow)
42 updateButton = (vim) ->
43 updateToolbarButton(vim.rootWindow, {blacklisted: vim.blacklisted, insertMode: vim.mode == 'insert'})
45 # The following listeners are installed on every top level Chrome window
49 # No matter what, always reset the `suppress` flag, so we don't suppress more than intended.
52 return if getPref('disabled')
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')
62 return if popup.state == 'open'
63 popupPassthrough = false # No popup was actually open: Reset the flag.
65 return unless window = utils.getEventCurrentTabWindow(event)
66 return unless vim = vimBucket.get(window)
68 return if vim.blacklisted
70 return unless keyStr = keyStrFromEvent(event)
71 suppress = vim.onInput(keyStr, event)
73 suppressEvent(event) if suppress
76 console.error("#{ error }\n#{ error.stack?.replace(/@.+-> /g, '@') }")
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
84 keypress: (event) -> suppressEvent(event) if suppress
85 keyup: (event) -> suppressEvent(event) if suppress
87 popupshown: checkPassthrough
88 popuphidden: checkPassthrough
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
99 if (new Date().getTime() - lastLoad.getTime()) < 1000
101 vim.storage.lastLoad = undefined
102 window.setTimeout((-> target.blur()), 0)
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
112 if vim.storage.lastLoad && activeElement = window.document.activeElement
114 vim.storage.lastLoad = undefined
115 window.setTimeout((-> activeElement.blur()), 0)
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)
126 { gBrowser } = utils.getEventRootWindow(event) ? {}
127 return unless gBrowser
128 tab = event.originalTarget
129 removeVimFromTab(tab, gBrowser)
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)
138 # This listener works on individual tabs within Chrome Window
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)
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
147 if vim.mode == 'hints'
148 vim.enterMode('normal')
150 vim.blacklisted = utils.isBlacklisted(location.spec)
153 addEventListeners = (window) ->
154 for name, listener of windowsListeners
155 window.addEventListener(name, listener, true)
157 window.gBrowser.addTabsProgressListener(tabsListener)
160 for name, listener of windowsListeners
161 window.removeEventListener(name, listener, true)
163 window.gBrowser.removeTabsProgressListener(tabsListener)
165 exports.addEventListeners = addEventListeners
166 exports.vimBucket = vimBucket