1 utils = require 'utils'
2 keyUtils = require 'key-utils'
3 { Vim } = require 'vim'
4 { getPref } = require 'prefs'
5 { setWindowBlacklisted } = require 'button'
6 { unload } = require 'unload'
8 { interfaces: Ci } = Components
10 vimBucket = new utils.Bucket(utils.getWindowId, (obj) -> new Vim(obj))
12 suppressEvent = (event) ->
13 event.preventDefault()
14 event.stopPropagation()
16 # *************************
17 # NB! TODO! All this shit needs to be redone!!
18 # *************************
20 keyStrFromEvent = (event) ->
22 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
25 if keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
26 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
30 # Passthrough mode is activated when VimFx should temporarily stop processking
31 # keyboard input. For example when a context menu is whown
34 logError = (err, eventName) ->
35 console.log("#{ err } (in #{ eventName })\n#{ err.stack.replace(/@.+-> /g, '@') }")
37 # The following listeners are installed on every top level Chrome window
41 if passthrough or getPref('disabled')
45 isEditable = utils.isElementEditable(event.originalTarget)
47 keyStr = keyStrFromEvent(event)
49 # We only handle the key if it's recognized by `keyCharFromCode`
50 # and if there is no focused editable element # or if it's the *Esc* key,
51 # which will remove the focus from the currently focused element
52 if keyStr and (not isEditable or keyStr == 'Esc')
53 if window = utils.getCurrentTabWindow(event)
54 if vim = vimBucket.get(window)
55 # No action if blacklisted
59 if vim.handleKeyDown(event, keyStr) and keyStr != 'Esc'
63 logError(err, 'keydown')
67 if passthrough or getPref('disabled')
71 isEditable = utils.isElementEditable(event.originalTarget)
73 # Try to execute keys that were accumulated so far.
74 # Suppress event if there is a matching command.
75 if window = utils.getCurrentTabWindow(event)
76 if vim = vimBucket.get(window)
78 # No action on blacklisted locations
82 # Blur from any active element on Esc. Calling before `handleKeyPress`
83 # because `vim.keys` will be reset afterwards`
84 blur_on_esc = vim.lastKeyStr == 'Esc' and getPref('blur_on_esc')
86 # Process event if there is no editable element in focus
87 # Or last key was Esc key
88 if not isEditable or vim.lastKeyStr == 'Esc'
89 result = vim.handleKeyPress(event)
91 # If there was some processing done then suppress the eveng
92 # unless it's the Esc key
93 if result and vim.lastKeyStr != 'Esc'
96 # Calling after the command has been executed
98 cb = -> event.originalTarget?.ownerDocument?.activeElement?.blur()
99 window.setTimeout(cb, 0)
102 logError(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