JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
code cleanup: better checking for tree-top
authorJason Woofenden <jason@jasonwoof.com>
Sat, 26 Mar 2016 20:26:25 +0000 (16:26 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sat, 26 Mar 2016 20:26:25 +0000 (16:26 -0400)
editor.coffee

index 9e21544..f025506 100644 (file)
@@ -665,6 +665,7 @@ class PeachHTML5Editor
                @options = options ? {}
                @in_el = in_el
                @tree = null
+               @tree_parent = null # top-level nodes in @tree should have this .parent
                @matting = []
                @init_1_called = false # when iframes have loaded
                @outer_iframe # iframe to hold editor
@@ -990,12 +991,12 @@ class PeachHTML5Editor
                        return unless cur_block.parent?
                        cur_block = cur_block.parent
                # find array to insert new element into
-               if cur_block.parent?.el?
-                       parent_el = cur_block.parent.el
-                       pc = cur_block.parent.children
-               else
+               if cur_block.parent is @tree_parent # top-level
                        parent_el = @idoc.body
                        pc = @tree
+               else
+                       parent_el = cur_block.parent.el
+                       pc = cur_block.parent.children
                # find index of current block in its parent
                for n, i in pc
                        break if n is cur_block
@@ -1013,6 +1014,7 @@ class PeachHTML5Editor
                new_node.el = domify @idoc, p: style: 'white-space: pre-wrap', children: [new_text.el]
                pc.splice i, 0, new_node
                parent_el.insertBefore new_node.el, before
+               @changed()
                new_cursor = new_cursor_position n: new_text, i: 0
                throw 'bork bork' unless new_cursor?
                @move_cursor new_cursor
@@ -1024,6 +1026,7 @@ class PeachHTML5Editor
                return
        load_html: (html) ->
                @tree = peach_parser.parse html, @parser_opts
+               @tree_parent = @tree[0]?.parent
                @clear_dom()
                instantiate_tree @tree, @idoc.body
                tree_dedup_space @tree
@@ -1104,27 +1107,26 @@ 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
+       # 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) ->
+               return false unless i is 0
+               return false unless n.text is ' '
+               return true 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
        # after calling this, you MUST call changed() and adjust_whitespace_style()
        insert_character: (n, i, char) ->
+               return if @cursor.n.parent is @tree_parent # top-level text not supported atm
                parent = @cursor.n.parent
-               return unless parent
-               return unless parent.el?
                # insert the character
-               if i is 0
-                       # 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
-                       special_case = false
-                       if n.text is ' '
-                               if n.parent?.el?
-                                       if n.parent.children.length is 1
-                                               if n.parent.children[0] is n
-                                                       special_case = true
-                               else
-                                       special_case = true
-                       if special_case
-                               n.text = char
-                       else
-                               n.text = char + n.text
+               if @insert_should_replace n, i
+                       n.text = char
+               else if i is 0
+                       n.text = char + n.text
                else if i is n.text.length
                        # replace the space
                        n.text += char