]> git.gir.st - VimFx.git/blob - extension/packages/events.coffee
Merge pull request #246 from lydell/issue-213
[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 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
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 # 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')
55
56 return unless window = utils.getEventCurrentTabWindow(event)
57 return unless vim = vimBucket.get(window)
58
59 return if vim.blacklisted
60
61 return unless keyStr = keyStrFromEvent(event)
62 suppress = vim.onInput(keyStr, event)
63
64 suppressEvent(event)
65
66 catch error
67 console.error("#{ error }\n#{ error.stack.replace(/@.+-> /g, '@') }")
68
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
74 # highest priority.
75 keypress: suppressEvent
76 keyup: suppressEvent
77
78 popupshown: checkPassthrough
79 popuphidden: checkPassthrough
80
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)
87
88 TabClose: (event) ->
89 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
90 tab = event.originalTarget
91 removeVimFromTab(tab, gBrowser)
92
93 # Update the toolbar button icon to reflect the blacklisted state
94 TabSelect: (event) ->
95 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
96 return unless vim = vimBucket.get(window)
97 updateButton(vim)
98
99 # This listener works on individual tabs within Chrome Window
100 tabsListener =
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)
104
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
107 # mode in that case.
108 if vim.mode == 'hints'
109 vim.enterMode('normal')
110
111 vim.blacklisted = utils.isBlacklisted(location.spec)
112 updateButton(vim)
113
114 addEventListeners = (window) ->
115 for name, listener of windowsListeners
116 window.addEventListener(name, listener, true)
117
118 window.gBrowser.addTabsProgressListener(tabsListener)
119
120 unload ->
121 for name, listener of windowsListeners
122 window.removeEventListener(name, listener, true)
123
124 window.gBrowser.removeTabsProgressListener(tabsListener)
125
126 exports.addEventListeners = addEventListeners
127 exports.vimBucket = vimBucket
Imprint / Impressum