X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=8290b37cd640dd63067a6cadf25269f4ed8756b7;hb=ed31e427bb738c1673b1fbf9465aa4f5324f9a3c;hp=a9d0a3c95d352931672379cbb953bc8fd6b155de;hpb=b3b872ef2a2200cd1e674845f0215341c1e67bfd;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index a9d0a3c..8290b37 100644 --- a/editor.coffee +++ b/editor.coffee @@ -51,7 +51,7 @@ debug_dot_at = (doc, x, y) -> # text nodes don't have getBoundingClientRect(), so use selection api to find # it. -get_el_bounds = (el) -> +get_el_bounds = window.bounds = (el) -> if el.getBoundingClientRect? rect = el.getBoundingClientRect() else @@ -388,13 +388,15 @@ find_prev_cursor_position = (tree, n, i) -> if prev_xyh.x < orig_xyh.x or prev_xyh.y < orig_xyh.y return [n, prev_i] return [n, i - 1] - found_prev = n? + found_prev = null found = null traverse_tree tree, (node) -> if node.type is 'text' + unless n? + found = node + return true if node is n - if found_prev? - found = found_prev + found = found_prev # null if n is the first text node return true found_prev = node return false @@ -621,7 +623,7 @@ class PeachHTML5Editor @cursor_el = null @cursor_visible = false @poll_for_blur_timeout = null - @iframe_offset = null + @wrap2_offset = null @iframe_height = null opt_fragment = @options.fragment ? true @parser_opts = {} @@ -701,12 +703,11 @@ class PeachHTML5Editor if @options.on_init? @options.on_init() overlay_event_to_inner_xy: (e) -> - unless @iframe_offset? - @iframe_offset = get_el_bounds @iframe - x = e.pageX # TODO ?cross-browserify - y = e.pageY + @wrap2.scrollTop # TODO ?cross-browserify - # TODO adjust for scrolling - return x: x - @iframe_offset.x, y: y - @iframe_offset.y + unless @wrap2_offset? + @wrap2_offset = get_el_bounds @wrap2 + x = e.pageX - overlay_padding + y = e.pageY - overlay_padding + @wrap2.scrollTop + return x: x - @wrap2_offset.x, y: y - @wrap2_offset.y onclick: (e) -> xy = @overlay_event_to_inner_xy e new_cursor = find_loc_cursor_position @tree, xy @@ -739,6 +740,47 @@ class PeachHTML5Editor break return false when KEY_UP + if @cursor? + new_cursor = @cursor + old_loc = cursor_to_xyh new_cursor[0], new_cursor[1] + new_loc = x: old_loc.x, y: old_loc.y + while new_loc.y >= old_loc.y + new_cursor = find_prev_cursor_position @tree, new_cursor[0], new_cursor[1] + return false unless new_cursor? + new_loc = cursor_to_xyh new_cursor[0], new_cursor[1] + if new_cursor? + # now we're above + if new_loc.x <= old_loc.x + @move_cursor new_cursor + return false + target_y = new_loc.y + # search leftward, until we find the closest position + while new_loc.x > old_loc.x and new_loc.y is target_y + prev_loc = new_loc + prev_cursor = new_cursor + new_cursor = find_prev_cursor_position @tree, new_cursor[0], new_cursor[1] + break unless new_cursor? + new_loc = cursor_to_xyh new_cursor[0], new_cursor[1] + # move cursor to prev_cursor or new_cursor + if new_cursor? + if new_loc.y is target_y + # both valid, and on the same line, use closest + if (old_loc.x - new_loc.x) < (prev_loc.x - old_loc.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 = find_prev_cursor_position @tree + if new_cursor? + @move_cursor new_cursor + break return false when KEY_RIGHT if @cursor? @@ -746,11 +788,11 @@ class PeachHTML5Editor if new_cursor? @move_cursor new_cursor else - for c in @tree - new_cursor = find_prev_cursor_position @tree, c, -1 - if new_cursor? - @move_cursor new_cursor - break + # move cursor to first position in document + new_cursor = find_prev_cursor_position @tree + if new_cursor? + @move_cursor new_cursor + break return false when KEY_DOWN return false