From 304ed69c871ccb27353b213816617fd8ea328ef7 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Fri, 18 Mar 2016 14:05:37 -0400 Subject: [PATCH] implement down arrow to move cursor --- editor.coffee | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/editor.coffee b/editor.coffee index a932f16..44b5fb6 100644 --- a/editor.coffee +++ b/editor.coffee @@ -806,6 +806,44 @@ class PeachHTML5Editor @move_cursor new_cursor return false when KEY_DOWN + if @cursor? + new_cursor = @cursor + # go next until we move on the y axis + while new_cursor.y <= @cursor.y + new_cursor = find_next_cursor_position @tree, new_cursor + return false unless new_cursor? + # done early if we're already right of old cursor position + if new_cursor.x >= @cursor.x + # this would be strange, but could happen due to runaround + @move_cursor new_cursor + return false + target_y = new_cursor.y + # search rightward, until we find the closest position + # new_cursor is the next-most position we've checked + # prev_cursor is the older value, so it's not as next as new_cursor + while new_cursor.x < @cursor.x and new_cursor.y is target_y + prev_cursor = new_cursor + new_cursor = find_next_cursor_position @tree, new_cursor + break unless new_cursor? + # move cursor to prev_cursor or new_cursor + if new_cursor? + if new_cursor.y is target_y + # both valid, and on the same line, use closest + if (new_cursor.x - @cursor.x) < (@cursor.x - prev_cursor.x) + @move_cursor new_cursor + else + @move_cursor prev_cursor + else + # new_cursor on wrong line, use prev_cursor + @move_cursor prev_cursor + else + # can't go any further prev, use prev_cursor + @move_cursor prev_cursor + else + # move cursor to first position in document + new_cursor = last_cursor_position @tree + if new_cursor? + @move_cursor new_cursor return false when KEY_END return false -- 1.7.10.4