JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix cursor movement past empty text nodes
authorJason Woofenden <jason@jasonwoof.com>
Tue, 8 Mar 2016 17:48:25 +0000 (12:48 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Tue, 8 Mar 2016 17:48:25 +0000 (12:48 -0500)
editor.coffee

index 45e5f36..07bf2c7 100644 (file)
@@ -326,8 +326,7 @@ traverse_tree = (tree, state, cb) ->
                        traverse_tree c.children, state, cb
                        break if state.done?
        return state
-# find the next element in tree (and decendants) that is after n and can contain text
-# TODO make it so cursor can go places that don't have text but could
+
 find_next_cursor_position = (tree, n, i) ->
        if n? and n.type is TYPE_TEXT and n.text.length > i
                orig_xyh = cursor_to_xyh n, i
@@ -341,15 +340,15 @@ find_next_cursor_position = (tree, n, i) ->
                                        return [n, next_i]
        found = traverse_tree tree, before: n?, (node, state) ->
                if node.type is TYPE_TEXT and state.before is false
-                       state.node = node
-                       state.done = true
+                       if cursor_to_xyh(node, 0)?
+                               state.node = node
+                               state.done = true
                if node is n
                        state.before = false
        if found.node?
                return [found.node, 0]
        return null
 
-# TODO make it so cursor can go places that don't have text but could
 find_prev_cursor_position = (tree, n, i) ->
        if n? and n.type is TYPE_TEXT and i > 0
                orig_xyh = cursor_to_xyh n, i
@@ -374,7 +373,12 @@ find_prev_cursor_position = (tree, n, i) ->
                        if node
                                state.prev = node
        if found.node?
-               return [found.node, found.node.text.length]
+               ret = [found.node, found.node.text.length]
+               # check for unusual case: text not visible
+               loc = cursor_to_xyh ret[0], ret[1]
+               if loc?
+                       return ret
+               return find_prev_cursor_position tree, ret[0], 0
        return null
 
 find_loc_cursor_position = (tree, loc) ->