JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fix typing (broken by CursorPosition)
authorJason Woofenden <jason@jasonwoof.com>
Fri, 18 Mar 2016 18:15:07 +0000 (14:15 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Fri, 18 Mar 2016 18:15:07 +0000 (14:15 -0400)
editor.coffee

index 44b5fb6..699134a 100644 (file)
@@ -849,18 +849,26 @@ class PeachHTML5Editor
                                return false
                        when KEY_BACKSPACE
                                return false unless @cursor?
                                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?
                                @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
                                @changed()
                                return false
                        when KEY_ENTER
@@ -884,17 +892,27 @@ class PeachHTML5Editor
                char = e.charCode ? e.keyCode
                if char and @cursor?
                        char = String.fromCharCode char
                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
                        else
-                               @cursor[0].text =
-                                       @cursor[0].text.substr(0, @cursor[1]) +
+                               @cursor.n.text =
+                                       @cursor.n.text.substr(0, @cursor.i) +
                                        char +
                                        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)
                        @changed()
                return false
        clear_dom: -> # remove all the editable content (and cursor, overlays, etc)