JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
replase odd whitespace characters with spaces where allowed
authorJason Woofenden <jason@jasonwoof.com>
Sun, 20 Mar 2016 02:12:05 +0000 (22:12 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sun, 20 Mar 2016 02:12:05 +0000 (22:12 -0400)
editor.coffee
editor_tests_compiled.html

index b6f28f0..dfcf6d2 100644 (file)
@@ -572,25 +572,35 @@ tree_dedup_space = (tree) ->
                                if block
                                        cb null
        # remove cur char
-       remove = ->
-               removed_char = cur.text.charAt(cur_i)
-               cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
-               if next is cur # in same text node
-                       if next_i is 0
-                               throw "how is this possible?"
-                       next_i -= 1
-               return true
-       # undo remove()
-       put_it_back = ->
-               cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
-               if next is cur # in same text node
-                       next_i += 1
-               return false
+       remove = (undo) ->
+               if undo
+                       cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
+                       if next is cur # in same text node
+                               next_i += 1
+                       return -1
+               else
+                       removed_char = cur.text.charAt(cur_i)
+                       cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
+                       if next is cur # in same text node
+                               if next_i is 0
+                                       throw "how is this possible?"
+                               next_i -= 1
+                       return 1
+       whitespace_to_space = (undo) ->
+               if undo
+                       cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i + 1)
+                       cur.el.textContent = cur.text
+               else
+                       removed_char = cur.text.charAt(cur_i)
+                       cur.text = (cur.text.substr 0, cur_i) + ' ' + (cur.text.substr cur_i + 1)
+                       cur.el.textContent = cur.text
+               return 0
        # return true if cur was removed from the dom (ie re-use same prev)
        operate = ->
                # cur definitately set
                # prev and/or next might be null, indicating the start/end of a display:block
                return false unless is_space_code cur.text.charCodeAt cur_i
+               fixers = [remove, whitespace_to_space]
                bounds = text_range_bounds cur.el, cur_i, cur_i + 1
                # consistent cases:
                # 1. zero rects returned by getClientRects() means collapsed space
@@ -598,7 +608,7 @@ tree_dedup_space = (tree) ->
                        return remove()
                # 2. width greater than zero means visible space
                if bounds.w > 0
-                       return false
+                       fixers.shift() # don't try removing
                # now the weird edge cases...
                #
                # firefox and chromium both report zero width for characters at the end
@@ -613,22 +623,38 @@ tree_dedup_space = (tree) ->
                        next_px = new_cursor_position n: next, i: next_i
                #if prev is null and next is null
                #       parent_px = cur.parent.el.getBoundingClientRect()
-               remove()
-               if prev?
-                       if prev_px?
-                               new_prev_px = new_cursor_position n: prev, i: prev_i
-                               if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
-                                       return put_it_back()
-                       else
-                               console.log "this shouldn't happen, we remove spaces that don't locate"
-               if next?
-                       if next_px?
-                               new_next_px = new_cursor_position n: next, i: next_i
-                               if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
-                                       return put_it_back()
-                       #else
-                       #       console.log "removing space becase space after it is collapsed"
-               return true
+               undo_arg = true # just for readabality
+               removed = 0
+               for fixer in fixers
+                       break if removed > 0
+                       removed += fixer()
+                       need_undo = false
+                       if prev?
+                               if prev_px?
+                                       new_prev_px = new_cursor_position n: prev, i: prev_i
+                                       if new_prev_px?
+                                               if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
+                                                       need_undo = true
+                                       else
+                                               need_undo = true
+                               else
+                                       console.log "this shouldn't happen, we remove spaces that don't locate"
+                       if next? and not need_undo
+                               if next_px?
+                                       new_next_px = new_cursor_position n: next, i: next_i
+                                       if new_next_px?
+                                               if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
+                                                       need_undo = true
+                                       else
+                                               need_undo = true
+                               #else
+                               #       console.log "removing space becase space after it is collapsed"
+                       if need_undo
+                               removed += fixer undo_arg
+               if removed > 0
+                       return true
+               else
+                       return false
        # pass null at start/end of display:block
        queue = (n, i) ->
                next = n
index 23517cb..7260bd1 100644 (file)
@@ -23,7 +23,8 @@
        <h1>Peach HTML5 Editor test page (compiled version)</h1>
        <p>This color scheme is just temporary, for testing the cursor and annotations on a variaty of background colors</p>
        <form action="#" method="get">
-       <p>HTML view. Changes here propagate when you remove your cursor (press tab or click outside)<br><textarea name="in" id="in">&lt;H1>Headline!&lt;/h1>&lt;p&gt;  normal text that is hopefully long enough that it will wrap around and spill onto a second line.&lt;/p>    &lt;p  >Text   with lots of extra whitespace
+       <p>HTML view. Changes here propagate when you remove your cursor (press tab or click outside)<br><textarea name="in" id="in">&lt;H1>Headline!&lt;/h1>&lt;p&gt;  normal text that is hopefully long enough\fthat it will wrap     around\rand
+spill onto a second line.&lt;/p>    &lt;p  >Text   with lots of extra whitespace