From 0ec38c1050b00845abaa2faa9f56d04c9d2b8992 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 21 Jan 2015 11:34:56 +0000 Subject: Edit mode: initial framework. --- content_scripts/mode.coffee | 2 +- content_scripts/mode_edit.coffee | 33 +++++++++++++++++++++++++++++++++ content_scripts/vimium_frontend.coffee | 18 ++++++++++++++---- 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 content_scripts/mode_edit.coffee (limited to 'content_scripts') diff --git a/content_scripts/mode.coffee b/content_scripts/mode.coffee index acc3978e..a74acfed 100644 --- a/content_scripts/mode.coffee +++ b/content_scripts/mode.coffee @@ -34,7 +34,7 @@ count = 0 class Mode # If Mode.debug is true, then we generate a trace of modes being activated and deactivated on the console. - debug: false + debug: true @modes: [] # Constants; short, readable names for the return values expected by handlerStack.bubbleEvent. diff --git a/content_scripts/mode_edit.coffee b/content_scripts/mode_edit.coffee new file mode 100644 index 00000000..32f2e796 --- /dev/null +++ b/content_scripts/mode_edit.coffee @@ -0,0 +1,33 @@ + +class EditMode extends Mode + @activeElements = [] + + constructor: (options = {}) -> + defaults = + name: "edit" + exitOnEscape: true + keydown: (event) => if @isActive() then @handleKeydown event else @continueBubbling + keypress: (event) => if @isActive() then @handleKeypress event else @continueBubbling + keyup: (event) => if @isActive() then @handleKeyup event else @continueBubbling + + @element = document.activeElement + if @element and DomUtils.isEditable @element + super extend defaults, options + + handleKeydown: (event) -> @suppressEvent + handleKeypress: (event) -> @suppressEvent + handleKeyup: (event) -> @suppressEvent + + isActive: -> + document.activeElement and DomUtils.isDOMDescendant @element, document.activeElement + + exit: (event, target) -> + super() + @element.blur() if target? and DomUtils.isDOMDescendant @element, target + EditMode.activeElements = EditMode.activeElements.filter (element) => element != @element + + updateBadge: (badge) -> + badge.badge = "E" if @isActive() + +root = exports ? window +root.EditMode = EditMode diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 725d8a53..79302930 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -328,9 +328,12 @@ extend window, enterInsertMode: -> new InsertMode global: true - enterVisualMode: => + enterVisualMode: -> new VisualMode() + enterEditMode: -> + @focusInput 1, EditMode + focusInput: do -> # Track the most recently focused input element. recentlyFocusedElement = null @@ -340,10 +343,11 @@ extend window, recentlyFocusedElement = event.target if DomUtils.isEditable event.target true - (count) -> + (count, mode = InsertMode) -> # Focus the first input element on the page, and create overlays to highlight all the input elements, with # the currently-focused element highlighted specially. Tabbing will shift focus to the next input element. # Pressing any other key will remove the overlays and the special tab behavior. + # If mode is provided, then enter that mode on exit. Otherwise, just let insert mode take over. resultSet = DomUtils.evaluateXPath textInputXPath, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE visibleInputs = for i in [0...resultSet.snapshotLength] by 1 @@ -395,8 +399,7 @@ extend window, @exit() @continueBubbling - @onExit -> DomUtils.removeElement hintContainingDiv - hintContainingDiv = DomUtils.addElementList hints, + @hintContainingDiv = DomUtils.addElementList hints, id: "vimiumInputMarkerContainer" className: "vimiumReset" @@ -406,6 +409,13 @@ extend window, else hints[selectedInputIndex].classList.add 'internalVimiumSelectedInputHint' + exit: -> + super() + DomUtils.removeElement @hintContainingDiv + if mode and DomUtils.isEditable document.activeElement + new mode + singleton: document.activeElement + # Decide whether this keyChar should be passed to the underlying page. # Keystrokes are *never* considered passKeys if the keyQueue is not empty. So, for example, if 't' is a # passKey, then 'gt' and '99t' will neverthless be handled by vimium. -- cgit v1.2.3