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 vimBucket = new utils.Bucket(utils.getWindowId, (w) -> new Vim(w))
12 keyStrFromEvent = (event) ->
13 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
16 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
17 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
22 # Passthrough mode is activated when VimFx should temporarily stop processing keyboard input, for
23 # example when a menu is shown.
25 checkPassthrough = (event) ->
26 if event.target.nodeName in ['menupopup', 'panel']
27 passthrough = switch event.type
28 when 'popupshown' then true
29 when 'popuphidden' then false
32 suppressEvent = (event) ->
34 event.preventDefault()
35 event.stopPropagation()
37 removeVimFromTab = (tab, gBrowser) ->
38 return unless browser = gBrowser.getBrowserForTab(tab)
39 vimBucket.forget(browser.contentWindow)
41 updateButton = (vim) ->
42 return unless rootWindow = utils.getRootWindow(vim.window)
43 updateToolbarButton(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 passthrough or getPref('disabled')
54 return unless window = utils.getEventCurrentTabWindow(event)
55 return unless vim = vimBucket.get(window)
57 return if vim.blacklisted
59 return unless keyStr = keyStrFromEvent(event)
60 suppress = vim.onInput(keyStr, event)
65 console.error("#{ error }\n#{ error.stack.replace(/@.+-> /g, '@') }")
67 # Note that the below event listeners can suppress the event even in blacklisted sites. That's
68 # intentional. For example, if you press 'x' to close the current tab, it will close before keyup
69 # fires. So keyup (and perhaps keypress) will fire in another tab. Even if that particular tab is
70 # blacklisted, we must suppress the event, so that 'x' isn't sent to the page. The rule is simple:
71 # If the `suppress` flag is `true`, the event should be suppressed, no matter what. It has the
73 keypress: suppressEvent
76 popupshown: checkPassthrough
77 popuphidden: checkPassthrough
79 # When the top level window closes we should release all Vims that were
80 # associated with tabs in this window
81 DOMWindowClose: (event) ->
82 return unless { gBrowser } = event.originalTarget
83 for tab in gBrowser.tabs
84 removeVimFromTab(tab, gBrowser)
87 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
88 tab = event.originalTarget
89 removeVimFromTab(tab, gBrowser)
91 # Update the toolbar button icon to reflect the blacklisted state
93 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
94 return unless vim = vimBucket.get(window)
97 # This listener works on individual tabs within Chrome Window
99 # Listenfor location changes and disable the extension on blacklisted urls
100 onLocationChange: (browser, webProgress, request, location) ->
101 return unless vim = vimBucket.get(browser.contentWindow)
103 # If the location changes when in hints mode (for example because the reload button has been
104 # clicked), we're going to end up in hints mode without any markers. So switch back to normal
106 if vim.mode == 'hints'
107 vim.enterMode('normal')
109 vim.blacklisted = utils.isBlacklisted(location.spec)
112 addEventListeners = (window) ->
113 for name, listener of windowsListeners
114 window.addEventListener(name, listener, true)
116 window.gBrowser.addTabsProgressListener(tabsListener)
119 for name, listener of windowsListeners
120 window.removeEventListener(name, listener, true)
122 window.gBrowser.removeTabsProgressListener(tabsListener)
124 exports.addEventListeners = addEventListeners