diff options
| author | Stephen Blott | 2016-02-27 15:25:28 +0000 |
|---|---|---|
| committer | Stephen Blott | 2016-03-05 05:37:40 +0000 |
| commit | d388eddd192d925cda43700d7090ce8d52499df0 (patch) | |
| tree | c7ed767e34a9e8ad6dc23101ac37d38f220f01aa | |
| parent | 47de80f2fcb03c8741ab46308ce982209f74f6ac (diff) | |
| download | vimium-d388eddd192d925cda43700d7090ce8d52499df0.tar.bz2 | |
Key bindings; remove legacy code.
| -rw-r--r-- | background_scripts/commands.coffee | 1 | ||||
| -rw-r--r-- | background_scripts/main.coffee | 161 | ||||
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 154 |
3 files changed, 8 insertions, 308 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index a1002929..7e02047c 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -395,7 +395,6 @@ Settings.postUpdateHooks["keyMappings"] = (value) -> Commands.clearKeyMappingsAndSetDefaults() Commands.parseCustomKeyMappings value Commands.generateKeyStateStructure() - refreshCompletionKeysAfterMappingSave() root = exports ? window root.Commands = Commands diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 41b9bf8a..1cb00a82 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -19,17 +19,9 @@ chrome.runtime.onInstalled.addListener ({ reason }) -> func tab.id, { file: file, allFrames: contentScripts.all_frames }, checkLastRuntimeError currentVersion = Utils.getCurrentVersion() -keyQueue = "" # Queue of keys typed -validFirstKeys = {} -singleKeyCommands = [] frameIdsForTab = {} root.urlForTab = {} -# Keys are either literal characters, or "named" - for example <a-b> (alt+b), <left> (left arrow) or <f12> -# This regular expression captures two groups: the first is a named key, the second is the remainder of -# the string. -namedKeyRegex = /^(<(?:[amc]-.|(?:[amc]-)?[a-z0-9]{2,5})>)(.*)$/ - # This is exported for use by "marks.coffee". root.tabLoadedHandlers = {} # tabId -> function() @@ -189,14 +181,6 @@ fetchFileContents = (extensionFileName) -> req.send() req.responseText -# -# Returns the keys that can complete a valid command given the current key queue. -# -getCompletionKeysRequest = (request, keysToCheck = "") -> - name: "refreshCompletionKeys" - completionKeys: generateCompletionKeys(keysToCheck) - validFirstKeys: validFirstKeys - TabOperations = # Opens the url in the current tab. openUrlInCurrentTab: (request, callback = (->)) -> @@ -385,140 +369,6 @@ chrome.tabs.onUpdated.addListener (tabId, changeInfo, tab) -> # End action functions -splitKeyIntoFirstAndSecond = (key) -> - if (key.search(namedKeyRegex) == 0) - { first: RegExp.$1, second: RegExp.$2 } - else - { first: key[0], second: key.slice(1) } - -getActualKeyStrokeLength = (key) -> - if (key.search(namedKeyRegex) == 0) - 1 + getActualKeyStrokeLength(RegExp.$2) - else - key.length - -populateValidFirstKeys = -> - for own key of Commands.keyToCommandRegistry - if (getActualKeyStrokeLength(key) == 2) - validFirstKeys[splitKeyIntoFirstAndSecond(key).first] = true - -populateSingleKeyCommands = -> - for own key of Commands.keyToCommandRegistry - if (getActualKeyStrokeLength(key) == 1) - singleKeyCommands.push(key) - -# Invoked by options.coffee. -root.refreshCompletionKeysAfterMappingSave = -> - validFirstKeys = {} - singleKeyCommands = [] - - populateValidFirstKeys() - populateSingleKeyCommands() - - sendRequestToAllTabs(getCompletionKeysRequest()) - -# Generates a list of keys that can complete a valid command given the current key queue or the one passed in -generateCompletionKeys = (keysToCheck) -> - splitHash = splitKeyQueue(keysToCheck || keyQueue) - command = splitHash.command - count = splitHash.count - - completionKeys = singleKeyCommands.slice(0) - - if (getActualKeyStrokeLength(command) == 1) - for own key of Commands.keyToCommandRegistry - splitKey = splitKeyIntoFirstAndSecond(key) - if (splitKey.first == command) - completionKeys.push(splitKey.second) - - completionKeys - -splitKeyQueue = (queue) -> - match = /([1-9][0-9]*)?(.*)/.exec(queue) - count = parseInt(match[1], 10) - command = match[2] - - { count: count, command: command } - -handleKeyDown = (sender) -> (request, port) -> - key = request.keyChar - if (key == "<ESC>") - logMessage "clearing keyQueue", sender - keyQueue = "" - else - logMessage "checking keyQueue: [#{keyQueue + key}]", sender - keyQueue = checkKeyQueue(keyQueue + key, port.sender.tab.id, request.frameId) - logMessage "new KeyQueue: #{keyQueue}", sender - # Tell the content script whether there are keys in the queue. - # FIXME: There is a race condition here. The behaviour in the content script depends upon whether this message gets - # back there before or after the next keystroke. - # That being said, I suspect there are other similar race conditions here, for example in checkKeyQueue(). - # Steve (23 Aug, 14). - chrome.tabs.sendMessage(port.sender.tab.id, - name: "currentKeyQueue", - keyQueue: keyQueue) - -checkKeyQueue = (keysToCheck, tabId, frameId) -> - refreshedCompletionKeys = false - splitHash = splitKeyQueue(keysToCheck) - command = splitHash.command - count = splitHash.count - - return keysToCheck if command.length == 0 - count = 1 if isNaN(count) - - if (Commands.keyToCommandRegistry[command]) - registryEntry = Commands.keyToCommandRegistry[command] - runCommand = true - count *= registryEntry.options.count ? 1 - - if registryEntry.noRepeat - count = 1 - else if registryEntry.repeatLimit and count > registryEntry.repeatLimit - runCommand = confirm """ - You have asked Vimium to perform #{count} repeats of the command: - #{Commands.availableCommands[registryEntry.command].description} - - Are you sure you want to continue? - """ - - if runCommand - if not registryEntry.isBackgroundCommand - chrome.tabs.sendMessage tabId, - name: "executePageCommand" - command: registryEntry.command - frameId: frameId - count: count - completionKeys: generateCompletionKeys "" - registryEntry: registryEntry - refreshedCompletionKeys = true - else - if registryEntry.passCountToFunction - BackgroundCommands[registryEntry.command](count, frameId) - else if registryEntry.noRepeat - BackgroundCommands[registryEntry.command](frameId) - else - repeatFunction(BackgroundCommands[registryEntry.command], count, 0, frameId) - - newKeyQueue = "" - else if (getActualKeyStrokeLength(command) > 1) - splitKey = splitKeyIntoFirstAndSecond(command) - - # The second key might be a valid command by its self. - if (Commands.keyToCommandRegistry[splitKey.second]) - newKeyQueue = checkKeyQueue(splitKey.second, tabId, frameId) - else - newKeyQueue = (if validFirstKeys[splitKey.second] then splitKey.second else "") - else - newKeyQueue = (if validFirstKeys[command] then count.toString() + command else "") - - # If we haven't sent the completion keys piggybacked on executePageCommand, - # send them by themselves. - unless refreshedCompletionKeys - chrome.tabs.sendMessage(tabId, getCompletionKeysRequest(null, newKeyQueue), null) - - newKeyQueue - runBackgroundCommand = ({frameId, registryEntry, count}) -> if registryEntry.passCountToFunction BackgroundCommands[registryEntry.command] count, frameId @@ -582,12 +432,10 @@ bgLog = (request, sender) -> # Port handler mapping portHandlers = - keyDown: handleKeyDown, completions: handleCompletions sendRequestHandlers = runBackgroundCommand: runBackgroundCommand - getCompletionKeys: getCompletionKeysRequest getCurrentTabUrl: getCurrentTabUrl openUrlInNewTab: TabOperations.openUrlInNewTab openUrlInIncognito: TabOperations.openUrlInIncognito @@ -633,14 +481,7 @@ window.runTests = -> open(chrome.runtime.getURL('tests/dom_tests/dom_tests.html' # # Begin initialization. # -Commands.clearKeyMappingsAndSetDefaults() - -if Settings.has("keyMappings") - Commands.parseCustomKeyMappings(Settings.get("keyMappings")) - -Commands.generateKeyStateStructure() -populateValidFirstKeys() -populateSingleKeyCommands() +Settings.postUpdateHooks["keyMappings"] Settings.get "keyMappings" # Show notification on upgrade. showUpgradeMessage = -> diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 7fe12d62..0cb402b8 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -5,14 +5,9 @@ # "domReady". # -keyPort = null isEnabledForUrl = true isIncognitoMode = chrome.extension.inIncognitoContext passKeys = null -keyQueue = null -# The user's operating system. -currentCompletionKeys = "" -validFirstKeys = "" # We track whther the current window has the focus or not. windowIsFocused = do -> @@ -119,9 +114,13 @@ window.initializeModes = -> commandHandler: (registryEntry, count) -> count *= registryEntry.options.count ? 1 count = 1 if registryEntry.noRepeat - # TODO: Repeat limit. + + if registryEntry.repeatLimit? and registryEntry.repeatLimit < count + return unless confirm """ + You have asked Vimium to perform #{count} repeats of the command: #{registryEntry.description}.\n + Are you sure you want to continue? """ + # TODO: Special handling of Vomnibar. - # TODO: Fix passKeys. if registryEntry.isBackgroundCommand chrome.runtime.sendMessage { handler: "runBackgroundCommand", frameId, registryEntry, count} else @@ -142,30 +141,13 @@ window.initializeModes = -> # initializePreDomReady = -> checkIfEnabledForUrl() - refreshCompletionKeys() - - # Send the key to the key handler in the background page. - keyPort = chrome.runtime.connect({ name: "keyDown" }) - # If the port is closed, the background page has gone away (since we never close it ourselves). Disable all - # our event listeners, and stub out chrome.runtime.sendMessage/connect (to prevent errors). - # TODO(mrmr1993): Do some actual cleanup to free resources, hide UI, etc. - keyPort.onDisconnect.addListener -> - isEnabledForUrl = false - chrome.runtime.sendMessage = -> - chrome.runtime.connect = -> - window.removeEventListener "focus", onFocus requestHandlers = showHUDforDuration: handleShowHUDforDuration toggleHelpDialog: (request) -> if frameId == request.frameId then HelpDialog.toggle request.dialogHtml focusFrame: (request) -> if (frameId == request.frameId) then focusThisFrame request - refreshCompletionKeys: refreshCompletionKeys getScrollPosition: -> scrollX: window.scrollX, scrollY: window.scrollY setScrollPosition: setScrollPosition - executePageCommand: executePageCommand - currentKeyQueue: (request) -> - keyQueue = request.keyQueue - handlerStack.bubbleEvent "registerKeyQueue", { keyQueue: keyQueue } # A frame has received the focus. We don't care here (the Vomnibar/UI-component handles this). frameFocused: -> checkEnabledAfterURLChange: checkEnabledAfterURLChange @@ -179,7 +161,7 @@ initializePreDomReady = -> return if request.handler and not request.name shouldHandleRequest = isEnabledForUrl # We always handle the message if it's one of these listed message types. - shouldHandleRequest ||= request.name in [ "executePageCommand", "checkEnabledAfterURLChange" ] + shouldHandleRequest ||= request.name in [ "checkEnabledAfterURLChange" ] # Requests with a frameId of zero should always and only be handled in the main/top frame (regardless of # whether Vimium is enabled there). if request.frameId == 0 and DomUtils.isTopFrame() @@ -255,28 +237,6 @@ unregisterFrame = -> frameId: frameId tab_is_closing: DomUtils.isTopFrame() -executePageCommand = (request) -> - commandType = request.command.split(".")[0] - # Vomnibar commands are handled in the tab's main/top frame. They are handled even if Vimium is otherwise - # disabled in the frame. - if commandType == "Vomnibar" - if DomUtils.isTopFrame() - # We pass the frameId from request. That's the frame which originated the request, so that's the frame - # which should receive the focus when the vomnibar closes. - Utils.invokeCommandString request.command, [ request.frameId, request.registryEntry ] - refreshCompletionKeys request - return - - # All other commands are handled in their frame (but only if Vimium is enabled). - return unless frameId == request.frameId and isEnabledForUrl - - if request.registryEntry.passCountToFunction - Utils.invokeCommandString(request.command, [request.count]) - else - Utils.invokeCommandString(request.command) for i in [0...request.count] - - refreshCompletionKeys(request) - handleShowHUDforDuration = ({ text, duration }) -> if DomUtils.isTopFrame() DomUtils.documentReady -> HUD.showForDuration text, duration @@ -476,94 +436,6 @@ extend window, targetElement: document.activeElement indicator: false -# Track which keydown events we have handled, so that we can subsequently suppress the corresponding keyup -# event. -KeydownEvents = - handledEvents: {} - - getEventCode: (event) -> event.keyCode - - push: (event) -> - @handledEvents[@getEventCode event] = true - - # Yields truthy or falsy depending upon whether a corresponding keydown event is present (and removes that - # event). - pop: (event) -> - detailString = @getEventCode event - value = @handledEvents[detailString] - delete @handledEvents[detailString] - value - - clear: -> @handledEvents = {} - -handlerStack.push - _name: "KeydownEvents-cleanup" - blur: (event) -> KeydownEvents.clear() if event.target == window; true - -# -# Sends everything except i & ESC to the handler in background_page. i & ESC are special because they control -# insert mode which is local state to the page. The key will be are either a single ascii letter or a -# key-modifier pair, e.g. <c-a> for control a. -# -# Note that some keys will only register keydown events and not keystroke events, e.g. ESC. -# -# @/this, here, is the the normal-mode Mode object. -onKeypress = (event) -> - keyChar = KeyboardUtils.getKeyCharString event - if keyChar - if currentCompletionKeys.indexOf(keyChar) != -1 or isValidFirstKey keyChar - DomUtils.suppressEvent(event) - keyPort.postMessage keyChar:keyChar, frameId:frameId - return @stopBubblingAndTrue - - keyPort.postMessage keyChar:keyChar, frameId:frameId - - return @continueBubbling - -# @/this, here, is the the normal-mode Mode object. -onKeydown = (event) -> - keyChar = KeyboardUtils.getKeyCharString event - - if (HelpDialog.showing && KeyboardUtils.isEscape(event)) - HelpDialog.hide() - DomUtils.suppressEvent event - KeydownEvents.push event - return @stopBubblingAndTrue - - else - if (keyChar) - if (currentCompletionKeys.indexOf(keyChar) != -1 or isValidFirstKey(keyChar)) - DomUtils.suppressEvent event - KeydownEvents.push event - keyPort.postMessage({ keyChar:keyChar, frameId:frameId }) - return @stopBubblingAndTrue - - keyPort.postMessage({ keyChar:keyChar, frameId:frameId }) - - else if (KeyboardUtils.isEscape(event)) - keyPort.postMessage({ keyChar:"<ESC>", frameId:frameId }) - - # Added to prevent propagating this event to other listeners if it's one that'll trigger a Vimium command. - # The goal is to avoid the scenario where Google Instant Search uses every keydown event to dump us - # back into the search box. As a side effect, this should also prevent overriding by other sites. - # - # Subject to internationalization issues since we're using keyIdentifier instead of charCode (in keypress). - # - # TOOD(ilya): Revisit this. Not sure it's the absolute best approach. - if not keyChar && - (currentCompletionKeys.indexOf(KeyboardUtils.getKeyChar(event)) != -1 || - isValidFirstKey(KeyboardUtils.getKeyChar(event))) - DomUtils.suppressPropagation(event) - KeydownEvents.push event - return @stopBubblingAndTrue - - return @continueBubbling - -# @/this, here, is the the normal-mode Mode object. -onKeyup = (event) -> - return @continueBubbling unless KeydownEvents.pop event - DomUtils.suppressPropagation(event) - @stopBubblingAndTrue # Checks if Vimium should be enabled or not in this frame. As a side effect, it also informs the background # page whether this frame has the focus, allowing the background page to track the active frame's URL. @@ -593,18 +465,6 @@ checkIfEnabledForUrl = (frameIsFocused = windowIsFocused()) -> checkEnabledAfterURLChange = -> checkIfEnabledForUrl() if windowIsFocused() -# Exported to window, but only for DOM tests. -window.refreshCompletionKeys = (response) -> - if (response) - currentCompletionKeys = response.completionKeys - - if (response.validFirstKeys) - validFirstKeys = response.validFirstKeys - else - chrome.runtime.sendMessage({ handler: "getCompletionKeys" }, refreshCompletionKeys) - -isValidFirstKey = (keyChar) -> - validFirstKeys[keyChar] || /^[1-9]/.test(keyChar) window.handleEscapeForFindMode = -> document.body.classList.remove("vimiumFindMode") |
