JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix typing (broken by CursorPosition)
[peach-html5-editor.git] / editor.coffee
index 3d3336d..699134a 100644 (file)
@@ -806,23 +806,69 @@ 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
                        when KEY_BACKSPACE
                                return false unless @cursor?
-                               return false unless @cursor[1] > 0
-                               @cursor[0].text = @cursor[0].text.substr(0, @cursor[1] - 1) + @cursor[0].text.substr(@cursor[1])
-                               @cursor[0].el.nodeValue = @cursor[0].text
-                               @move_cursor [@cursor[0], @cursor[1] - 1]
+                               return false unless @cursor.i > 0
+                               @cursor.n.text = @cursor.n.text.substr(0, @cursor.i - 1) + @cursor.n.text.substr(@cursor.i)
+                               @cursor.n.el.nodeValue = @cursor.n.text
+                               new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
+                               if new_cursor?
+                                       @move_cursor new_cursor
+                               else
+                                       @kill_cursor()
                                @changed()
                                return false
                        when KEY_DELETE
                                return false unless @cursor?
-                               return false unless @cursor[1] < @cursor[0].text.length
-                               @cursor[0].text = @cursor[0].text.substr(0, @cursor[1]) + @cursor[0].text.substr(@cursor[1] + 1)
-                               @cursor[0].el.nodeValue = @cursor[0].text
-                               @move_cursor [@cursor[0], @cursor[1]]
+                               return false unless @cursor.i < @cursor.n.text.length
+                               @cursor.n.text = @cursor.n.text.substr(0, @cursor.i) + @cursor.n.text.substr(@cursor.i + 1)
+                               @cursor.n.el.nodeValue = @cursor.n.text
+                               new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
+                               if new_cursor?
+                                       @move_cursor new_cursor
+                               else
+                                       @kill_cursor()
                                @changed()
                                return false
                        when KEY_ENTER
@@ -846,17 +892,27 @@ class PeachHTML5Editor
                char = e.charCode ? e.keyCode
                if char and @cursor?
                        char = String.fromCharCode char
-                       if @cursor[1] is 0
-                               @cursor[0].text = char + @cursor[0].text
-                       else if @cursor[1] is @cursor[0].text.length - 1
-                               @cursor[0].text += char
+                       if @cursor.i is 0
+                               @cursor.n.text = char + @cursor.n.text
+                       else if @cursor.i is @cursor.n.text.length - 1
+                               @cursor.n.text += char
                        else
-                               @cursor[0].text =
-                                       @cursor[0].text.substr(0, @cursor[1]) +
+                               @cursor.n.text =
+                                       @cursor.n.text.substr(0, @cursor.i) +
                                        char +
-                                       @cursor[0].text.substr(@cursor[1])
-                       @cursor[0].el.nodeValue = @cursor[0].text
-                       @move_cursor [@cursor[0], @cursor[1] + 1]
+                                       @cursor.n.text.substr(@cursor.i)
+                       @cursor.n.el.nodeValue = @cursor.n.text
+                       new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i + 1
+                       unless new_cursor
+                               # probably pressed space, and browser isn't displaying it
+                               # FIXME insert &nbsp; instead, rip it out later if possible, etc.
+                               # for now, remove it
+                               @cursor.n.text =
+                                       @cursor.n.text.substr(0, @cursor.i) +
+                                       @cursor.n.text.substr(@cursor.i + 1)
+                               @cursor.n.el.nodeValue = @cursor.n.text
+                               return false
+                       @move_cursor new_cursor
                        @changed()
                return false
        clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
@@ -889,15 +945,13 @@ class PeachHTML5Editor
                        @cursor_el.parentNode.removeChild @cursor_el
                        @cursor_visible = false
                @cursor = null
-               @matt null
+               @annotate null
        move_cursor: (cursor) ->
                @cursor = cursor
-               # replace cursor element, to reset blink animation
-               if @cursor_visible
-                       @cursor_el.parentNode.removeChild @cursor_el
-               @cursor_el = domify @outer_idoc, div: id: 'cursor'
-               @overlay.appendChild @cursor_el
-               @cursor_visible = true
+               unless @cursor_visible
+                       @cursor_el = domify @outer_idoc, div: id: 'cursor'
+                       @overlay.appendChild @cursor_el
+                       @cursor_visible = true
                @cursor_el.style.left = "#{cursor.x + overlay_padding - 1}px"
                if cursor.h < 5
                        height = 12
@@ -905,8 +959,8 @@ class PeachHTML5Editor
                        height = cursor.h
                @cursor_el.style.top = "#{cursor.y + overlay_padding + Math.round(height * .07)}px"
                @cursor_el.style.height = "#{Math.round height * 0.82}px"
-               @matt cursor.n
-       matt: (n) ->
+               @annotate cursor.n
+       annotate: (n) ->
                while @matting.length > 0
                        @overlay.removeChild @matting[0]
                        @matting.shift()
@@ -922,12 +976,12 @@ class PeachHTML5Editor
                        if bounds.x is prev_bounds.x and bounds.y is prev_bounds.y and bounds.w is prev_bounds.w and bounds.h is prev_bounds.h
                                n = n.parent
                                continue
-                       matt = domify @outer_idoc, div: class: 'ann_box', style: "left: #{bounds.x - 1 + overlay_padding}px; top: #{bounds.y - 2 + overlay_padding}px; width: #{bounds.w}px; height: #{bounds.h}px" # outline: 1000px solid rgba(0,153,255,#{alpha});
-                       @overlay.appendChild matt
-                       @matting.push matt
-                       ann = domify @outer_idoc, div: class: 'ann_tag', style: "left: #{bounds.x + 1 + overlay_padding}px; top: #{bounds.y - 7 + overlay_padding}px", children: [domify @outer_idoc, text: " #{n.name} "]
-                       @overlay.appendChild ann
-                       @matting.push ann
+                       ann_box = domify @outer_idoc, div: class: 'ann_box', style: "left: #{bounds.x - 1 + overlay_padding}px; top: #{bounds.y - 2 + overlay_padding}px; width: #{bounds.w}px; height: #{bounds.h}px" # outline: 1000px solid rgba(0,153,255,#{alpha});
+                       @overlay.appendChild ann_box
+                       @matting.push ann_box
+                       ann_tag = domify @outer_idoc, div: class: 'ann_tag', style: "left: #{bounds.x + 1 + overlay_padding}px; top: #{bounds.y - 7 + overlay_padding}px", children: [domify @outer_idoc, text: " #{n.name} "]
+                       @overlay.appendChild ann_tag
+                       @matting.push ann_tag
                        n = n.parent
                        alpha *= 1.5
        pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->