JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
remove obsolete debugging code
[peach-html5-editor.git] / editor.coffee
index 36c6660..2bb3d99 100644 (file)
@@ -76,13 +76,6 @@ multi_sp_regex = new RegExp '[\u0020\u0009\u000a\u000c\u000d][\u0020\u0009\u000a
 str_has_ws_run = (str) ->
        return multi_sp_regex.test str
 
-debug_dot_at = (doc, x, y) ->
-       return # disabled
-       el = doc.createElement 'div'
-       el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
-       doc.body.appendChild el
-       #console.log(new Error().stack)
-
 # text nodes don't have getBoundingClientRect(), so use selection api to find
 # it.
 get_el_bounds = window.bounds = (el) ->
@@ -524,8 +517,10 @@ tree_remove_empty_text_nodes = (tree) ->
                                n.parent.children.splice i, 1
                                break
 
-# pass a array of nodes (from parser library, ie it should have .el and .text)
-tree_dedup_space = (tree) ->
+# remove whitespace that would be trimmed
+# replace whitespace that would collapse with a single space
+# FIXME delete this, use @collapse_whitespace instead
+collapse_whitespace = (tree) ->
        prev = cur = next = null
        prev_i = cur_i = next_i = 0
        prev_pos = pos = next_pos = null
@@ -952,15 +947,22 @@ class PeachHTML5Editor
                                return false
                        when KEY_DELETE
                                return false unless @cursor?
-                               return false unless @cursor.i < @cursor.n.text.length
-                               @remove_character @cursor.n, @cursor.i
-                               @text_cleanup @cursor.n
-                               @changed()
-                               new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
+                               new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: @cursor.i
+                               # try moving cursor right and then running backspace code
+                               # TODO replace this hack with a real implementation
                                if new_cursor?
-                                       @move_cursor new_cursor
-                               else
-                                       @kill_cursor()
+                                       # try to detect common case where cursor goes inside an block,
+                                       # but doesn't pass a character (and advance one more in that case)
+                                       if new_cursor.n isnt @cursor.n and new_cursor.i is 0
+                                               if new_cursor.n.type is 'text' and new_cursor.n.text.length > 0
+                                                       if new_cursor.n.parent?
+                                                               unless @is_display_block new_cursor.n.parent
+                                                                       # FIXME should test run sibling
+                                                                       new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i + 1
+                               if new_cursor?
+                                       if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
+                                               @move_cursor new_cursor
+                                               @on_key_backspace e
                                return false
                        when KEY_ENTER
                                @on_key_enter e
@@ -1159,7 +1161,7 @@ class PeachHTML5Editor
                                        @_merge_left merge_state
                                        @text_cleanup merge_state.n
                                        new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
-                               # FIXME CONTINUE
+                               # FIXME implement this:
                                # else # if prev (in run) is inline-block
                                        # if that inline-block has text in it
                                                # delete last char in prev inlineblock
@@ -1341,7 +1343,7 @@ class PeachHTML5Editor
                @tree_parent.el = @idoc.body
                @clear_dom()
                instantiate_tree @tree, @tree_parent.el
-               tree_dedup_space @tree
+               @collapse_whitespace @tree
                @changed()
        changed: ->
                @in_el.onchange = null
@@ -1454,10 +1456,18 @@ class PeachHTML5Editor
                else
                        if n.attrs.style?
                                delete n.attrs.style
+       # remove whitespace that would be trimmed
+       # replace whitespace that would collapse with a single space
+       collapse_whitespace: (tree = @tree) ->
+               return collapse_whitespace tree # FIXME CONTINUE
+               return
        # call this after you insert or remove inline nodes. It will:
        #    merge consecutive text nodes
        #    remove empty text nodes
        #    adjust white-space property
+       # note: this assumes that all whitespace in text nodes should be displayed
+       # (ie not collapse or be trimmed) and will change the white-space property
+       # as needed to achieve this.
        text_cleanup: (n) ->
                if @is_display_block n
                        block = n