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 # The following listeners are installed on every top level Chrome window
40 if passthrough or getPref('disabled')
44 isEditable = utils.isElementEditable(event.originalTarget)
46 keyStr = keyStrFromEvent(event)
48 # We only handle the key if it's recognized by `keyCharFromCode`
49 # and if there is no focused editable element # or if it's the *Esc* key,
50 # which will remove the focus from the currently focused element
51 if keyStr and (not isEditable or keyStr == 'Esc')
52 if window = utils.getCurrentTabWindow(event)
53 if vim = vimBucket.get(window)
54 # No action if blacklisted
58 if vim.handleKeyDown(event, keyStr) and keyStr != 'Esc'
61 console.log(err, 'keydown', err.stack)
65 if passthrough or getPref('disabled')
69 isEditable = utils.isElementEditable(event.originalTarget)
71 # Try to execute keys that were accumulated so far.
72 # Suppress event if there is a matching command.
73 if window = utils.getCurrentTabWindow(event)
74 if vim = vimBucket.get(window)
76 # No action on blacklisted locations
80 # Blur from any active element on Esc. Calling before `handleKeyPress`
81 # because `vim.keys` will be reset afterwards`
82 blur_on_esc = vim.lastKeyStr == 'Esc' and getPref('blur_on_esc')
84 # Process event if there is no editable element in focus
85 # Or last key was Esc key
86 if not isEditable or vim.lastKeyStr == 'Esc'
87 result = vim.handleKeyPress(event)
89 # If there was some processing done then suppress the eveng
90 # unless it's the Esc key
91 if result and vim.lastKeyStr != 'Esc'
94 # Calling after the command has been executed
96 cb = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
97 window.setTimeout(cb, 0)
100 console.log(err, 'keypress', err.stack)
103 if window = utils.getCurrentTabWindow event
104 if vim = vimBucket.get(window)
105 if vim.lastKeyStr and vim.lastKeyStr != 'Esc'
108 vim.lastKeyStr = null
110 popupshown: (event) ->
111 if event.target.tagName in [ 'menupopup', 'panel' ]
115 popuphidden: (event) ->
116 if event.target.tagName in [ 'menupopup', 'panel' ]
119 # When the top level window closes we should release all Vims that were
120 # associated with tabs in this window
121 DOMWindowClose: (event) ->
122 if gBrowser = event.originalTarget.gBrowser
123 for tab in gBrowser.tabs
124 if browser = gBrowser.getBrowserForTab(tab)
125 vimBucket.forget(browser.contentWindow)
128 if gBrowser = utils.getEventTabBrowser(event)
129 if browser = gBrowser.getBrowserForTab(event.originalTarget)
130 vimBucket.forget(browser.contentWindow)
132 # Update the toolbar button icon to reflect the blacklisted state
133 TabSelect: (event) ->
134 if vim = vimBucket.get(event.originalTarget?.linkedBrowser?.contentDocument?.defaultView)
135 if rootWindow = utils.getRootWindow(vim.window)
136 setWindowBlacklisted(rootWindow, vim.blacklisted)
138 # This listener works on individual tabs within Chrome Window
139 # User for: listening for location changes and disabling the extension
140 # on black listed urls
142 onLocationChange: (browser, webProgress, request, location) ->
143 blacklisted = utils.isBlacklisted(location.spec, getPref('black_list'))
144 if vim = vimBucket.get(browser.contentWindow)
145 vim.enterNormalMode()
146 vim.blacklisted = blacklisted
147 if rootWindow = utils.getRootWindow(vim.window)
148 setWindowBlacklisted(rootWindow, vim.blacklisted)
150 addEventListeners = (window) ->
151 for name, listener of windowsListener
152 window.addEventListener(name, listener, true)
154 # Install onLocationChange listener
155 window.gBrowser.addTabsProgressListener(tabsListener)
157 removeEventListeners = ->
158 for name, listener of windowsListener
159 window.removeEventListener(name, listener, true)
162 removeEventListeners(window)
163 window.gBrowser.removeTabsProgressListener(tabsListener)
165 exports.addEventListeners = addEventListeners