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')
66 if passthrough or getPref('disabled')
70 isEditable = utils.isElementEditable(event.originalTarget)
72 # Try to execute keys that were accumulated so far.
73 # Suppress event if there is a matching command.
74 if window = utils.getCurrentTabWindow(event)
75 if vim = vimBucket.get(window)
77 # No action on blacklisted locations
81 # Blur from any active element on Esc. Calling before `handleKeyPress`
82 # because `vim.keys` will be reset afterwards`
83 blur_on_esc = vim.lastKeyStr == 'Esc' and getPref('blur_on_esc')
85 # Process event if there is no editable element in focus
86 # Or last key was Esc key
87 if not isEditable or vim.lastKeyStr == 'Esc'
88 result = vim.handleKeyPress(event)
90 # If there was some processing done then suppress the eveng
91 # unless it's the Esc key
92 if result and vim.lastKeyStr != 'Esc'
95 # Calling after the command has been executed
97 cb = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
98 window.setTimeout(cb, 0)
101 console.log(err, 'keypress')
105 if window = utils.getCurrentTabWindow event
106 if vim = vimBucket.get(window)
107 if vim.lastKeyStr and vim.lastKeyStr != 'Esc'
110 vim.lastKeyStr = null
112 popupshown: (event) ->
113 if event.target.tagName in [ 'menupopup', 'panel' ]
117 popuphidden: (event) ->
118 if event.target.tagName in [ 'menupopup', 'panel' ]
121 # When the top level window closes we should release all Vims that were
122 # associated with tabs in this window
123 DOMWindowClose: (event) ->
124 if gBrowser = event.originalTarget.gBrowser
125 for tab in gBrowser.tabs
126 if browser = gBrowser.getBrowserForTab(tab)
127 vimBucket.forget(browser.contentWindow)
130 if gBrowser = utils.getEventTabBrowser(event)
131 if browser = gBrowser.getBrowserForTab(event.originalTarget)
132 vimBucket.forget(browser.contentWindow)
134 # Update the toolbar button icon to reflect the blacklisted state
135 TabSelect: (event) ->
136 if vim = vimBucket.get(event.originalTarget?.linkedBrowser?.contentDocument?.defaultView)
137 if rootWindow = utils.getRootWindow(vim.window)
138 setWindowBlacklisted(rootWindow, vim.blacklisted)
140 # This listener works on individual tabs within Chrome Window
141 # User for: listening for location changes and disabling the extension
142 # on black listed urls
144 onLocationChange: (browser, webProgress, request, location) ->
145 blacklisted = utils.isBlacklisted(location.spec, getPref('black_list'))
146 if vim = vimBucket.get(browser.contentWindow)
147 vim.enterNormalMode()
148 vim.blacklisted = blacklisted
149 if rootWindow = utils.getRootWindow(vim.window)
150 setWindowBlacklisted(rootWindow, vim.blacklisted)
152 addEventListeners = (window) ->
153 for name, listener of windowsListener
154 window.addEventListener(name, listener, true)
156 # Install onLocationChange listener
157 window.gBrowser.addTabsProgressListener(tabsListener)
159 removeEventListeners = ->
160 for name, listener of windowsListener
161 window.removeEventListener(name, listener, true)
164 removeEventListeners(window)
165 window.gBrowser.removeTabsProgressListener(tabsListener)
167 exports.addEventListeners = addEventListeners