1 utils = require 'utils'
2 keyUtils = require 'key-utils'
3 { getCommand } = require 'commands'
4 { Vim } = require 'vim'
5 { getPref } = require 'prefs'
6 { setWindowBlacklisted } = require 'button'
7 { unload } = require 'unload'
8 { console } = require 'console'
10 { interfaces: Ci } = Components
12 vimBucket = new utils.Bucket(utils.getWindowId, (obj) -> new Vim(obj))
14 suppressEvent = (event) ->
15 event.preventDefault()
16 event.stopPropagation()
18 # *************************
19 # NB! TODO! All this shit needs to be redone!!
20 # *************************
22 keyStrFromEvent = (event) ->
24 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
27 if keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
28 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
32 # Passthrough mode is activated when VimFx should temporarily stop processking
33 # keyboard input. For example when a context menu is whown
36 logError = (err, eventName) ->
37 console.log("#{ err } (in #{ eventName })\n#{ err.stack.replace(/@(?:.+->)+ /g, '@') }")
39 # The following listeners are installed on every top level Chrome window
43 if passthrough or getPref('disabled')
47 isEditable = utils.isElementEditable(event.originalTarget)
49 keyStr = keyStrFromEvent(event)
51 # We only handle the key if it's recognized by `keyCharFromCode`
52 # and if there is no focused editable element # or if it's the *Esc* key,
53 # which will remove the focus from the currently focused element
54 if keyStr and (not isEditable or keyStr == 'Esc')
55 if window = utils.getCurrentTabWindow(event)
56 if vim = vimBucket.get(window)
57 # No action if blacklisted
61 if vim.handleKeyDown(event, keyStr) and keyStr != 'Esc'
64 logError(err, 'keydown')
68 if passthrough or getPref('disabled')
72 isEditable = utils.isElementEditable(event.originalTarget)
74 # Try to execute keys that were accumulated so far.
75 # Suppress event if there is a matching command.
76 if window = utils.getCurrentTabWindow(event)
77 if vim = vimBucket.get(window)
79 # No action on blacklisted locations
83 # Blur from any active element on Esc. Calling before `handleKeyPress`
84 # because `vim.keys` will be reset afterwards`
85 blur_on_esc = vim.lastKeyStr == 'Esc' and getPref('blur_on_esc')
87 # Process event if there is no editable element in focus
88 # Or last key was Esc key
89 if not isEditable or vim.lastKeyStr == 'Esc'
90 result = vim.handleKeyPress(event)
92 # If there was some processing done then suppress the eveng
93 # unless it's the Esc key
94 if result and vim.lastKeyStr != 'Esc'
97 # Calling after the command has been executed
99 cb = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
100 window.setTimeout(cb, 0)
103 logError(err, 'keypress')
106 if window = utils.getCurrentTabWindow event
107 if vim = vimBucket.get(window)
108 if vim.lastKeyStr and vim.lastKeyStr != 'Esc'
111 vim.lastKeyStr = null
113 popupshown: (event) ->
114 if event.target.tagName in [ 'menupopup', 'panel' ]
118 popuphidden: (event) ->
119 if event.target.tagName in [ 'menupopup', 'panel' ]
122 # When the top level window closes we should release all Vims that were
123 # associated with tabs in this window
124 DOMWindowClose: (event) ->
125 if gBrowser = event.originalTarget.gBrowser
126 for tab in gBrowser.tabs
127 if browser = gBrowser.getBrowserForTab(tab)
128 vimBucket.forget(browser.contentWindow)
131 if gBrowser = utils.getEventTabBrowser(event)
132 if browser = gBrowser.getBrowserForTab(event.originalTarget)
133 vimBucket.forget(browser.contentWindow)
135 # Update the toolbar button icon to reflect the blacklisted state
136 TabSelect: (event) ->
137 if vim = vimBucket.get(event.originalTarget?.linkedBrowser?.contentDocument?.defaultView)
138 if rootWindow = utils.getRootWindow(vim.window)
139 setWindowBlacklisted(rootWindow, vim.blacklisted)
141 # This listener works on individual tabs within Chrome Window
142 # User for: listening for location changes and disabling the extension
143 # on black listed urls
145 onLocationChange: (browser, webProgress, request, location) ->
146 blacklisted = utils.isBlacklisted(location.spec, getPref('black_list'))
147 if vim = vimBucket.get(browser.contentWindow)
148 vim.enterNormalMode()
149 vim.blacklisted = blacklisted
150 if rootWindow = utils.getRootWindow(vim.window)
151 setWindowBlacklisted(rootWindow, vim.blacklisted)
153 addEventListeners = (window) ->
154 for name, listener of windowsListener
155 window.addEventListener(name, listener, true)
157 # Install onLocationChange listener
158 window.gBrowser.addTabsProgressListener(tabsListener)
160 removeEventListeners = ->
161 for name, listener of windowsListener
162 window.removeEventListener(name, listener, true)
165 removeEventListeners(window)
166 window.gBrowser.removeTabsProgressListener(tabsListener)
168 exports.addEventListeners = addEventListeners