aboutsummaryrefslogtreecommitdiffstats
path: root/content_scripts/mode_key_handler.coffee
diff options
context:
space:
mode:
authorStephen Blott2016-02-27 14:20:12 +0000
committerStephen Blott2016-03-05 05:37:40 +0000
commit34b1fb0f4e2ce1696c17e703d0bc43463355d6ba (patch)
tree0bf5797129ae1f212154b56aff86be0830073a29 /content_scripts/mode_key_handler.coffee
parent7c5fb2c312b9140c2dd091f792535ae8f592ecdb (diff)
downloadvimium-34b1fb0f4e2ce1696c17e703d0bc43463355d6ba.tar.bz2
Key bindings; initial partially-functioning version.
Diffstat (limited to 'content_scripts/mode_key_handler.coffee')
-rw-r--r--content_scripts/mode_key_handler.coffee43
1 files changed, 3 insertions, 40 deletions
diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee
index c79f1991..6e04addb 100644
--- a/content_scripts/mode_key_handler.coffee
+++ b/content_scripts/mode_key_handler.coffee
@@ -1,11 +1,4 @@
-# The important data structure here is the "keyState". The key state is a non-empty list of objects, the keys
-# of which are key names, and the values are other key-mapping objects or commands (strings). Key-mapping
-# objects can be arbitrarily nested; so we support any length of multi-key mapping.
-#
-# Whenever we consume a key, we append a new copy of the global key mapping to the key state (hence, the
-# global mappings are always available, and the key state is always non-empty).
-
class KeyHandlerMode extends Mode
useCount: true
countPrefix: 0
@@ -15,8 +8,6 @@ class KeyHandlerMode extends Mode
constructor: (options) ->
# A function accepting a command name and a count; required.
@commandHandler = options.commandHandler ? (->)
- # A Key mapping structure; required.
- @keyMapping = options.keyMapping ? {}
@useCount = false if options.noCount
@reset()
@@ -31,6 +22,8 @@ class KeyHandlerMode extends Mode
blur: (event) => @alwaysContinueBubbling =>
@keydownEvents = {} if event.target == window
+ setKeyMapping: (@keyMapping) -> @reset()
+
onKeydown: (event) ->
keyChar = KeyboardUtils.getKeyCharString event
@@ -65,7 +58,7 @@ class KeyHandlerMode extends Mode
keyChar = KeyboardUtils.getKeyCharString event
if keyChar and @keyCharIsKeyStatePrefix keyChar
@advanceKeyState keyChar
- commands = @keyState.filter (entry) -> "string" == typeof entry
+ commands = @keyState.filter (entry) -> entry.command
@invokeCommand commands[0] if 0 < commands.length
false # Suppress event.
else if keyChar and @isCountKey keyChar
@@ -124,35 +117,5 @@ class KeyHandlerMode extends Mode
getEventCode: (event) -> event.keyCode
-# Demo/test code.
-# A (very) poor-man's normal mode.
-
-demoKeyMapping =
- j: "scrollDown"
- k: "scrollUp"
- i: "enterInsertMode"
- g:
- g: "scrollToTop"
- a: "scrollToTop"
- z: "scrollToBottom"
- i: "focusInput"
- # A three-key binding.
- a:
- b:
- c: "enterInsertMode"
- # And this should override "j" on its own.
- j: "enterInsertMode"
-
-demoCommandHandler = (command, count) ->
- switch command
- when "scrollDown" then scrollDown()
- when "scrollUp" then scrollUp()
- when "scrollToTop" then scrollToTop count
- when "scrollToBottom" then scrollToBottom()
- when "enterInsertMode" then enterInsertMode()
- when "focusInput" then focusInput count
-
root = exports ? window
root.KeyHandlerMode = KeyHandlerMode
-root.demoKeyMapping = demoKeyMapping
-root.demoCommandHandler = demoCommandHandler