From: Jason Woofenden Date: Tue, 8 Mar 2016 17:48:25 +0000 (-0500) Subject: fix cursor movement past empty text nodes X-Git-Url: https://jasonwoof.com/gitweb/?p=peach-html5-editor.git;a=commitdiff_plain;h=0644f17def6ee0e7b5cc3b6972bfdf279f6d5f15 fix cursor movement past empty text nodes --- diff --git a/editor.coffee b/editor.coffee index 45e5f36..07bf2c7 100644 --- a/editor.coffee +++ b/editor.coffee @@ -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) ->