aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-01-22 15:10:50 +0000
committerStephen Blott2015-01-22 15:10:50 +0000
commit5493c2f3f878f495a41eb524ab1a8030c7411796 (patch)
tree190a4b0dc9969c1dc6f46982d8d88b892b495951
parentd7b416747e5cf6971737a3f70243618419a1ac4b (diff)
downloadvimium-5493c2f3f878f495a41eb524ab1a8030c7411796.tar.bz2
Visual/edit modes: faster "o" implementation.
-rw-r--r--content_scripts/mode_visual_edit.coffee21
1 files changed, 15 insertions, 6 deletions
diff --git a/content_scripts/mode_visual_edit.coffee b/content_scripts/mode_visual_edit.coffee
index a5d8ff45..0f335a9b 100644
--- a/content_scripts/mode_visual_edit.coffee
+++ b/content_scripts/mode_visual_edit.coffee
@@ -68,6 +68,7 @@ class Movement extends MaintainCount
# Get the direction of the selection, either forward or backward.
# FIXME(smblott). There has to be a better way!
+ # NOTE(smblott). There is. See here: https://dom.spec.whatwg.org/#interface-range.
getDirection: ->
# Try to move the selection forward or backward, then check whether it got bigger or smaller (then restore
# it).
@@ -119,15 +120,23 @@ class Movement extends MaintainCount
"W": -> @moveByWord backward
"o": ->
- # FIXME(smblott). This is super slow if the selection is large.
- length = @selection.toString().length
+ # Swap the anchor and focus.
+ # Note(smblott). I can't find an approach which works for both cases, so we have to implement each case
+ # separately.
+ original = @selection.getRangeAt 0
switch @getDirection()
when forward
- @selection.collapseToEnd()
- @selection.modify "extend", backward, character for [0...length]
+ range = original.cloneRange()
+ range.collapse false
+ @selection.removeAllRanges()
+ @selection.addRange range
+ @selection.extend original.startContainer, original.startOffset
when backward
- @selection.collapseToStart()
- @selection.modify "extend", forward, character for [0...length]
+ range = document.createRange()
+ range.setStart @selection.focusNode, @selection.focusOffset
+ range.setEnd @selection.anchorNode, @selection.anchorOffset
+ @selection.removeAllRanges()
+ @selection.addRange range
# TODO(smblott). What do we do if there is no initial selection? Or multiple ranges?
constructor: (options) ->