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.
24 popupPassthrough = false
25 checkPassthrough = (event) ->
26 if event.target.nodeName in ['menupopup', 'panel']
27 popupPassthrough = 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 # Suppress popup passthrough mode if there is no passthrough mode on the root document
53 return if popupPassthrough and !!utils.getEventRootWindow(event).document.popupNode
54 return if getPref('disabled')
56 return unless window = utils.getEventCurrentTabWindow(event)
57 return unless vim = vimBucket.get(window)
59 return if vim.blacklisted
61 return unless keyStr = keyStrFromEvent(event)
62 suppress = vim.onInput(keyStr, event)
67 console.error("#{ error }\n#{ error.stack.replace(/@.+-> /g, '@') }")
69 # Note that the below event listeners can suppress the event even in blacklisted sites. That's
70 # intentional. For example, if you press 'x' to close the current tab, it will close before keyup
71 # fires. So keyup (and perhaps keypress) will fire in another tab. Even if that particular tab is
72 # blacklisted, we must suppress the event, so that 'x' isn't sent to the page. The rule is simple:
73 # If the `suppress` flag is `true`, the event should be suppressed, no matter what. It has the
75 keypress: suppressEvent
78 popupshown: checkPassthrough
79 popuphidden: checkPassthrough
81 # When the top level window closes we should release all Vims that were
82 # associated with tabs in this window
83 DOMWindowClose: (event) ->
84 return unless { gBrowser } = event.originalTarget
85 for tab in gBrowser.tabs
86 removeVimFromTab(tab, gBrowser)
89 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
90 tab = event.originalTarget
91 removeVimFromTab(tab, gBrowser)
93 # Update the toolbar button icon to reflect the blacklisted state
95 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
96 return unless vim = vimBucket.get(window)
99 # This listener works on individual tabs within Chrome Window
101 # Listenfor location changes and disable the extension on blacklisted urls
102 onLocationChange: (browser, webProgress, request, location) ->
103 return unless vim = vimBucket.get(browser.contentWindow)
105 # If the location changes when in hints mode (for example because the reload button has been
106 # clicked), we're going to end up in hints mode without any markers. So switch back to normal
108 if vim.mode == 'hints'
109 vim.enterMode('normal')
111 vim.blacklisted = utils.isBlacklisted(location.spec)
114 addEventListeners = (window) ->
115 for name, listener of windowsListeners
116 window.addEventListener(name, listener, true)
118 window.gBrowser.addTabsProgressListener(tabsListener)
121 for name, listener of windowsListeners
122 window.removeEventListener(name, listener, true)
124 window.gBrowser.removeTabsProgressListener(tabsListener)
126 exports.addEventListeners = addEventListeners
127 exports.vimBucket = vimBucket