]> git.gir.st - VimFx.git/blob - extension/packages/events.coffee
Fix #213: Suppress logic bug
[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 vimBucket = new utils.Bucket(utils.getWindowId, (w) -> new Vim(w))
11
12 keyStrFromEvent = (event) ->
13 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
14
15 if !meta and !alt
16 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
17 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
18 return keyStr
19
20 return null
21
22 # Passthrough mode is activated when VimFx should temporarily stop processing keyboard input, for
23 # example when a menu is shown.
24 passthrough = false
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
30
31 suppress = false
32 suppressEvent = (event) ->
33 if suppress
34 event.preventDefault()
35 event.stopPropagation()
36
37 removeVimFromTab = (tab, gBrowser) ->
38 return unless browser = gBrowser.getBrowserForTab(tab)
39 vimBucket.forget(browser.contentWindow)
40
41 updateButton = (vim) ->
42 return unless rootWindow = utils.getRootWindow(vim.window)
43 updateToolbarButton(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 passthrough or getPref('disabled')
53
54 return unless window = utils.getEventCurrentTabWindow(event)
55 return unless vim = vimBucket.get(window)
56
57 return if vim.blacklisted
58
59 return unless keyStr = keyStrFromEvent(event)
60 suppress = vim.onInput(keyStr, event)
61
62 suppressEvent(event)
63
64 catch error
65 console.error("#{ error }\n#{ error.stack.replace(/@.+-> /g, '@') }")
66
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
72 # highest priority.
73 keypress: suppressEvent
74 keyup: suppressEvent
75
76 popupshown: checkPassthrough
77 popuphidden: checkPassthrough
78
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)
85
86 TabClose: (event) ->
87 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
88 tab = event.originalTarget
89 removeVimFromTab(tab, gBrowser)
90
91 # Update the toolbar button icon to reflect the blacklisted state
92 TabSelect: (event) ->
93 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
94 return unless vim = vimBucket.get(window)
95 updateButton(vim)
96
97 # This listener works on individual tabs within Chrome Window
98 tabsListener =
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)
102
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
105 # mode in that case.
106 if vim.mode == 'hints'
107 vim.enterMode('normal')
108
109 vim.blacklisted = utils.isBlacklisted(location.spec)
110 updateButton(vim)
111
112 addEventListeners = (window) ->
113 for name, listener of windowsListeners
114 window.addEventListener(name, listener, true)
115
116 window.gBrowser.addTabsProgressListener(tabsListener)
117
118 unload ->
119 for name, listener of windowsListeners
120 window.removeEventListener(name, listener, true)
121
122 window.gBrowser.removeTabsProgressListener(tabsListener)
123
124 exports.addEventListeners = addEventListeners
Imprint / Impressum