JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix backspace of last character in a block
authorJason Woofenden <jason@jasonwoof.com>
Sat, 26 Mar 2016 21:45:43 +0000 (17:45 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sat, 26 Mar 2016 21:50:22 +0000 (17:50 -0400)
editor.coffee

index 636bf19..8367d09 100644 (file)
@@ -1050,13 +1050,21 @@ class PeachHTML5Editor
                                        @kill_cursor
                                return
                else if @cursor.i is 0
-                       console.log 'not implemented yet'
+                       console.log 'unimplemented: backspace at start of non-empty tag'
                        # TODO if block, merge parent into prev
                        # TODO if inline, delete char from prev text node
                        return false
                else
                        # TODO handle case of removing last char
-                       @remove_character @cursor.n, @cursor.i - 1
+                       # CONTINUE
+                       if @is_only_char_in_tag @cursor.n
+                               if is_display_block @cursor.n.parent.el
+                                       @cursor.n.el.textContent = @cursor.n.text = ' '
+                               else
+                                       console.log "unimplemented: delete last char in inline" # FIXME
+                                       return
+                       else
+                               @remove_character @cursor.n, @cursor.i - 1
                        @adjust_whitespace_style @cursor.n
                        @changed()
                        new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
@@ -1064,6 +1072,7 @@ class PeachHTML5Editor
                                @move_cursor new_cursor
                        else
                                @kill_cursor()
+               return
        clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
                while @idoc.body.childNodes.length
                        @idoc.body.removeChild @idoc.body.childNodes[0]
@@ -1155,16 +1164,20 @@ class PeachHTML5Editor
                                if needle is n.attrs.style.substr n.attrs.style.length - needle
                                        n.attrs.style = n.attrs.style.substr 0, n.attrs.style.length - needle
                                        n.el.setAttribute n.attrs.style
+       # true if n is text node with only one caracter, and the only child of a tag
+       is_only_char_in_tag: (n, i) ->
+               return false unless n.type is 'text'
+               return false unless n.text.length is 1
+               return false if n.parent is @tree_parent
+               return false unless n.parent.children.length is 1
+               return true
        # true if n is text node with just a space in it, and the only child of a tag
        is_lone_space: (n, i) ->
                return false unless n.type is 'text'
                return false unless n.text is ' '
                return false if n.parent is @tree_parent
-               if n.parent.children.length is 1
-                       if n.parent.children[0] is n
-                               # n is only child
-                               return true
-               return false
+               return false unless n.parent.children.length is 1
+               return true
        # detect special case: typing before a space that's the only thing in a block/doc
        # reason: enter key creates blocks with just a space in them
        insert_should_replace: (n, i) ->