1 # Copyright 2015 Jason Woofenden
2 # This file implements an WYSIWYG editor in the browser (no contenteditable)
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 breathing_room = 30 # minimum pixels above/below cursor (scrolling)
21 timeout = (ms, cb) -> return setTimeout cb, ms
23 if (window.requestAnimationFrame?)
24 window.requestAnimationFrame cb
28 this_url_sans_path = ->
29 ret = "#{window.location.href}"
30 clip = ret.lastIndexOf '#'
32 ret = ret.substr 0, clip
33 clip = ret.lastIndexOf '?'
35 ret = ret.substr 0, clip
36 clip = ret.lastIndexOf '/'
38 ret = ret.substr 0, clip + 1
41 # table too look up the properties of various values for css's white-space
44 space: false # spaces are not preserved/rendered
45 newline: false # newlines are not preserved/rendered
46 wrap: true # text is word-wrapped
47 to_preserve: 'pre-wrap' # to preservespaces, change white-space to this
57 to_preserve: 'pre-wrap'
69 # xml 1.0 spec, chromium and firefox accept these, plus lots of unicode chars
70 valid_attr_regex = new RegExp '^[a-zA-Z_:][-a-zA-Z0-9_:.]*$'
71 # html5 spec is much more lax, but chromium won't let me make at attribute with the name "4"
72 js_attr_regex = new RegExp '^[oO][nN].'
73 # html5 spec says that only these characters are collapsable
74 multi_sp_regex = new RegExp '[\u0020\u0009\u000a\u000c\u000d][\u0020\u0009\u000a\u000c\u000d]'
76 str_has_ws_run = (str) ->
77 return multi_sp_regex.test str
79 debug_dot_at = (doc, x, y) ->
81 el = doc.createElement 'div'
82 el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
83 doc.body.appendChild el
84 #console.log(new Error().stack)
86 # text nodes don't have getBoundingClientRect(), so use selection api to find
88 get_el_bounds = window.bounds = (el) ->
89 if el.getBoundingClientRect?
90 rect = el.getBoundingClientRect()
92 # text nodes don't have getBoundingClientRect(), so use range api
93 range = el.ownerDocument.createRange()
94 range.selectNodeContents el
95 rect = range.getBoundingClientRect()
96 doc = el.ownerDocument.documentElement
97 win = el.ownerDocument.defaultView
98 y_fix = win.pageYOffset - doc.clientTop
99 x_fix = win.pageXOffset - doc.clientLeft
103 w: rect.width ? (rect.right - rect.left)
104 h: rect.height ? (rect.top - rect.bottom)
107 is_display_block = (el) ->
109 return el.currentStyle.display is 'block'
111 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
113 # Pass return value from dom event handlers to this.
114 # If they return false, this will addinionally stop propagation and default.
115 event_return = (e, bool) ->
117 if e.stopPropagation?
122 # Warning: currently assumes you're asking about a single character
123 # Note: chromium returns multiple bounding rects for a space at a line-break
124 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
125 # Note: sometimes returns null (eg for whitespace that is not visible)
126 text_range_bounds = (el, start, end) ->
127 range = document.createRange()
128 range.setStart el, start
130 rects = range.getClientRects()
133 if rects[1].width > rects[0].width
141 doc = el.ownerDocument.documentElement
142 win = el.ownerDocument.defaultView
143 y_fix = win.pageYOffset - doc.clientTop
144 x_fix = win.pageXOffset - doc.clientLeft
148 w: rect.width ? (rect.right - rect.left)
149 h: rect.height ? (rect.top - rect.bottom)
151 bounding: range.getBoundingClientRect()
155 constructor: (args) ->
166 range = document.createRange()
167 if @n.text.length is 0
168 ret = text_range_bounds @n.el, 0, 0
169 else if @i is @n.text.length
170 ret = text_range_bounds @n.el, @i - 1, @i
174 ret = text_range_bounds @n.el, @i, @i + 1
185 new_cursor_position = (args) ->
186 ret = new CursorPosition args
191 # encode text so it can be safely placed inside an html attribute
192 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
194 return txt.replace enc_attr_regex, (match, amp, quote) ->
195 return '&' if (amp)
196 return '"' if (quote)
198 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
200 return txt.replace enc_text_regex, (match, amp, lt) ->
201 return '&' if (amp)
202 return '<' if (lt)
222 # TODO make these always pretty-print (on the inside) like blocks
223 no_text_elements = { # these elements never contain text
233 domify = (doc, hash) ->
234 for tag, attrs of hash
236 return document.createTextNode attrs
237 el = document.createElement tag
250 '20': true # capslock
252 '144': true # numlock
254 '91': true # windows "start" key
255 # key codes: (valid on keydown, not keypress)
260 KEY_BACKSPACE = 8 # <--
261 KEY_DELETE = 46 # -->
270 control_key_codes = # we react to these, but they aren't typing
286 instantiate_tree = (tree, parent) ->
291 c.el = parent.ownerDocument.createTextNode c.text
292 parent.appendChild c.el
294 if c.name in ['script', 'object', 'iframe', 'link']
295 # TODO put placeholders instead
298 # TODO create in correct namespace
299 c.el = parent.ownerDocument.createElement c.name
301 # FIXME if attr_whitelist[k]?
302 if valid_attr_regex.test k
303 unless js_attr_regex.test k
304 c.el.setAttribute k, v
305 parent.appendChild c.el
307 instantiate_tree c.children, c.el
311 traverse_tree = (tree, cb) ->
317 done = traverse_tree c.children, cb
321 first_cursor_position = (tree) ->
323 traverse_tree tree, (node, state) ->
324 if node.type is 'text'
325 cursor = new_cursor_position n: node, i: 0
328 return true # done traversing
329 return false # not done traversing
330 return found # maybe null
332 # this will fail when text has non-locatable cursor positions
333 find_next_cursor_position = (tree, cursor) ->
334 if cursor.n.type is 'text' and cursor.n.text.length > cursor.i
335 new_cursor = new_cursor_position n: cursor.n, i: cursor.i + 1
340 traverse_tree tree, (node, state) ->
341 if node.type is 'text' and state_before is false
342 new_cursor = new_cursor_position n: node, i: 0
345 return true # done traversing
348 return false # not done traversing
353 last_cursor_position = (tree) ->
355 traverse_tree tree, (node) ->
356 if node.type is 'text'
357 cursor = new_cursor_position n: node, i: node.text.length
360 return false # not done traversing
361 return found # maybe null
363 # this will fail when text has non-locatable cursor positions
364 find_prev_cursor_position = (tree, cursor) ->
365 if cursor.n.type is 'text' and cursor.i > 0
366 new_cursor = new_cursor_position n: cursor.n, i: cursor.i - 1
371 traverse_tree tree, (node) ->
373 found = found_prev # maybe null
374 return true # done traversing
375 if node.type is 'text'
376 new_cursor = new_cursor_position n: node, i: node.text.length
378 found_prev = new_cursor
379 return false # not done traversing
380 return found # maybe null
382 find_up_cursor_position = (tree, cursor, ideal_x) ->
384 # go prev until we're higher on y axis
385 while new_cursor.y >= cursor.y
386 new_cursor = find_prev_cursor_position tree, new_cursor
387 return null unless new_cursor?
388 # done early if we're already left of old cursor position
389 if new_cursor.x <= ideal_x
391 target_y = new_cursor.y
392 # search leftward, until we find the closest position
393 # new_cursor is the prev-most position we've checked
394 # prev_cursor is the older value, so it's not as prev as new_cursor
395 while new_cursor.x > ideal_x and new_cursor.y is target_y
396 prev_cursor = new_cursor
397 new_cursor = find_prev_cursor_position tree, new_cursor
398 break unless new_cursor?
399 # move cursor to prev_cursor or new_cursor
401 if new_cursor.y is target_y
402 # both valid, and on the same line, use closest
403 if (ideal_x - new_cursor.x) < (prev_cursor.x - ideal_x)
408 # new_cursor on wrong line, use prev_cursor
411 # can't go any further prev, use prev_cursor
414 find_down_cursor_position = (tree, cursor, ideal_x) ->
416 # go next until we move on the y axis
417 while new_cursor.y <= cursor.y
418 new_cursor = find_next_cursor_position tree, new_cursor
419 return null unless new_cursor?
420 # done early if we're already right of old cursor position
421 if new_cursor.x >= ideal_x
422 # this would be strange, but could happen due to runaround
424 target_y = new_cursor.y
425 # search rightward, until we find the closest position
426 # new_cursor is the next-most position we've checked
427 # prev_cursor is the older value, so it's not as next as new_cursor
428 while new_cursor.x < ideal_x and new_cursor.y is target_y
429 prev_cursor = new_cursor
430 new_cursor = find_next_cursor_position tree, new_cursor
431 break unless new_cursor?
432 # move cursor to prev_cursor or new_cursor
434 if new_cursor.y is target_y
435 # both valid, and on the same line, use closest
436 if (new_cursor.x - ideal_x) < (ideal_x - prev_cursor.x)
441 # new_cursor on wrong line, use prev_cursor
444 # can't go any further prev, use prev_cursor
447 xy_to_cursor = (tree, xy) ->
449 if n.type is 'tag' or n.type is 'text'
450 bounds = get_el_bounds n.el
451 continue if xy.x < bounds.x
452 continue if xy.x > bounds.x + bounds.w
453 continue if xy.y < bounds.y
454 continue if xy.y > bounds.y + bounds.h
456 ret = xy_to_cursor n.children, xy
459 # click is within bounding box that contains all text.
460 if n.text.length is 0
461 ret = new_cursor_position n: n, i: 0
464 before = new_cursor_position n: n, i: 0
465 continue unless before?
466 after = new_cursor_position n: n, i: n.text.length
467 continue unless after?
468 if xy.y < before.y + before.h and xy.x < before.x
469 # console.log 'before first char on first line'
471 if xy.y > after.y and xy.x > after.x
472 # console.log 'after last char on last line'
475 console.log "Warning: click in text bounding box but above first line"
476 continue # above first line (runaround?)
477 if xy.y > after.y + after.h
478 console.log "Warning: click in text bounding box but below last line", xy.y, after.y, after.h
479 continue # below last line (shouldn't happen?)
480 while after.i - before.i > 1
481 guess_i = Math.round((before.i + after.i) / 2)
482 cur = new_cursor_position n: n, i: guess_i
484 console.log "error: failed to find cursor pixel location for", n, guess_i
487 if xy.y < cur.y or (xy.y <= cur.y + cur.h and xy.x < cur.x)
491 continue unless before? # signals failure to find a cursor position
492 # which one is closest?
493 if Math.abs(before.x - xy.x) < Math.abs(after.x - xy.x)
499 # browsers collapse these (html5 spec calls these "space characters")
500 is_space_code = (char_code) ->
502 when 9, 10, 12, 13, 32
506 return is_space_code chr.charCodeAt 0
508 tree_remove_empty_text_nodes = (tree) ->
510 traverse_tree tree, (n) ->
512 if n.text.length is 0
514 return false # not done traversing
516 # don't completely empty the tree
518 if tree[0].type is 'text'
519 console.log "oop, leaving a blank node because it's the only thing"
521 n.el.parentNode.removeChild n.el
522 for c, i in n.parent.children
524 n.parent.children.splice i, 1
527 # pass a array of nodes (from parser library, ie it should have .el and .text)
528 tree_dedup_space = (tree) ->
529 prev = cur = next = null
530 prev_i = cur_i = next_i = 0
531 prev_pos = pos = next_pos = null
532 prev_px = cur_px = next_px = null
536 tree_remove_empty_text_nodes(tree)
538 iterate = (tree, cb) ->
542 while i < n.text.length # don't foreach, cb might remove chars
547 block = is_display_block n.el
550 if n.children.length > 0
551 iterate n.children, cb
557 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
558 if next is cur # in same text node
562 removed_char = cur.text.charAt(cur_i)
563 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
564 if next is cur # in same text node
566 throw "how is this possible?"
569 replace_with_space = (undo) ->
571 cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i + 1)
572 cur.el.textContent = cur.text
574 removed_char = cur.text.charAt(cur_i)
575 if removed_char isnt ' '
576 cur.text = (cur.text.substr 0, cur_i) + ' ' + (cur.text.substr cur_i + 1)
577 cur.el.textContent = cur.text
579 # return true if cur was removed from the dom (ie re-use same prev)
581 # cur definitately set
582 # prev and/or next might be null, indicating the start/end of a display:block
583 return false unless is_space_code cur.text.charCodeAt cur_i
584 fixers = [remove, replace_with_space]
585 # check for common case: single whitespace surrounded by non-whitespace chars
587 unless (is_space_code prev.text.charCodeAt prev_i) or (is_space_code next.text.charCodeAt next_i)
588 dbg = cur.text.charCodeAt cur_i
589 if cur.text.charAt(cur_i) is ' ' # perens required
590 # single space can't collapse, doesn't need fixin'
593 # tab, newline, etc, can't collapse, but maybe should be replaced
594 fixers = [replace_with_space]
595 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
597 # 1. zero rects returned by getClientRects() means collapsed space
600 # 2. width greater than zero means visible space
602 # has bounds, don't try removing
603 fixers = [replace_with_space]
604 # now the weird edge cases...
606 # firefox and chromium both report zero width for characters at the end
607 # of a line where the text wraps (automatically, due to word-wrap) to
608 # the next line. These do not appear to be distinguishable from
609 # collapsed spaces via the range/bounds api, so...
611 # remove it from the dom, and if prev or next moves, put it back.
613 # this block (try changing it, put it back if something moves) is also
614 # used on collapsable whitespace characters besides space. In this case
615 # the character is replaced with a normal space character instead of
617 if prev? and not prev_px?
618 prev_px = new_cursor_position n: prev, i: prev_i
619 if next? and not next_px?
620 next_px = new_cursor_position n: next, i: next_i
621 #if prev is null and next is null
622 # parent_px = cur.parent.el.getBoundingClientRect()
623 undo_arg = true # just for readabality
631 new_prev_px = new_cursor_position n: prev, i: prev_i
633 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
638 console.log "this shouldn't happen, we remove spaces that don't locate"
639 if next? and not need_undo
641 new_next_px = new_cursor_position n: next, i: next_i
643 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
648 # console.log "removing space becase space after it is collapsed"
650 removed += fixer undo_arg
655 # pass null at start/end of display:block
663 # don't advance (to the next character next time) if we removed a
664 # character from the same text node as ``next``, because doing so
665 # renumbers the indexes in that string
666 if removed and cur is next
682 tree_remove_empty_text_nodes(tree)
684 class PeachHTML5Editor
685 # Options: (all optional)
686 # editor_id: "id" attribute for outer-most element created by/for editor
687 # css_file: filename of a css file to style editable content
688 # on_init: callback for when the editable content is in place
689 constructor: (in_el, options) ->
690 @options = options ? {}
692 @tree = null # array of Nodes, all editable content
693 @tree_parent = null # @tree is this.children. .el might === @idoc.body
695 @init_1_called = false # when iframes have loaded
696 @outer_iframe # iframe to hold editor
697 @outer_idoc # "document" object for @outer_iframe
698 @wrap2 = null # scrollbar is on this
700 @wrap2_height = null # including padding
701 @iframe = null # iframe to hold editable content
702 @idoc = null # "document" object for @iframe
705 @cursor_visible = false
706 @cursor_ideal_x = null
707 @poll_for_blur_timeout = null
708 opt_fragment = @options.fragment ? true
711 @parser_opts.fragment = 'body'
713 @outer_iframe = domify document, iframe: {}
714 outer_iframe_style = 'border: none !important; margin: 0 !important; padding: 0 !important; height: 100% !important; width: 100% !important;'
715 if @options.editor_id?
716 @outer_iframe.setAttribute 'id', @options.editor_id
717 @outer_iframe.onload = =>
718 @outer_idoc = @outer_iframe.contentDocument
719 icss = domify @outer_idoc, style: children: [
720 domify @outer_idoc, text: css
722 @outer_idoc.head.appendChild icss
723 @iframe = domify @outer_idoc, iframe: sandbox: 'allow-same-origin allow-scripts'
726 timeout 200, => # firefox never fires this onload
727 @init_1() unless @init_1_called
728 @outer_idoc.body.appendChild(
729 domify @outer_idoc, div: id: 'wrap1', children: [
730 domify @outer_idoc, div: style: "position: absolute; top: 0; left: 1px; font-size: 10px", children: [ domify @outer_idoc, text: "Peach HTML5 Editor" ]
731 @wrap2 = domify @outer_idoc, div: id: 'wrap2', children: [
732 domify @outer_idoc, div: id: 'wrap3', children: [
734 @overlay = domify @outer_idoc, div: id: 'overlay'
739 outer_wrap = domify document, div: class: 'peach_html5_editor'
740 @in_el.parentNode.appendChild outer_wrap
741 outer_bounds = get_el_bounds outer_wrap
742 if outer_bounds.w < 300
744 if outer_bounds.h < 300
746 outer_iframe_style += "width: #{outer_bounds.w}px; height: #{outer_bounds.h}px;"
747 @outer_iframe.setAttribute 'style', outer_iframe_style
748 css = @generate_outer_css w: outer_bounds.w, h: outer_bounds.h
749 outer_wrap.appendChild @outer_iframe
750 init_1: -> # @iframe has loaded (but not it's css)
751 @idoc = @iframe.contentDocument
752 @init_1_called = true
753 # chromium doesn't resolve relative urls as though they were at the same domain
754 # so add a <base> tag
755 @idoc.head.appendChild domify @idoc, base: href: this_url_sans_path()
756 # don't let @iframe have scrollbars
757 @idoc.head.appendChild domify @idoc, style: children: [domify @idoc, text: "body { overflow: hidden; }"]
760 istyle = domify @idoc, link: rel: 'stylesheet', href: @options.css_file
763 @idoc.head.appendChild istyle
766 init_2: -> # @iframe and it's css file(s) are ready
767 @overlay.onclick = (e) =>
769 return event_return e, @onclick e
770 @overlay.ondoubleclick = (e) =>
772 return event_return e, @ondoubleclick e
773 @outer_idoc.body.onkeyup = (e) =>
775 return event_return e, @onkeyup e
776 @outer_idoc.body.onkeydown = (e) =>
778 return event_return e, @onkeydown e
779 @outer_idoc.body.onkeypress = (e) =>
781 return event_return e, @onkeypress e
782 @load_html @in_el.value
785 generate_outer_css: (args) ->
788 inner_padding = args.inner_padding ? overlay_padding
789 frame_width = args.frame_width ? inner_padding
790 occupy = (left, top = left, right = left, bottom = top) ->
793 return Math.max(left, top, right, bottom)
798 ret += 'color: black;'
799 ret += 'background: white;'
802 ret += "border: #{occupy 1}px solid black;"
803 ret += "padding: #{occupy frame_width}px;"
806 ret += "border: #{occupy 1}px solid black;"
807 @wrap2_height = h # including padding because padding scrolls
808 ret += "padding: #{occupy inner_padding}px;"
809 ret += "padding-right: #{inner_padding + occupy 0, 0, 15, 0}px;" # for scroll bar
810 ret += "width: #{w}px;"
811 ret += "height: #{h}px;"
812 ret += 'overflow-x: hidden;'
813 ret += 'overflow-y: scroll;'
816 ret += 'position: relative;'
817 ret += "width: #{w}px;"
818 ret += "min-height: #{h}px;"
821 ret += 'box-sizing: border-box;'
823 ret += 'border: none;'
825 ret += "width: #{w}px;"
826 #ret += "height: #{h}px;" # height auto-set when content set/changed
827 ret += '-ms-user-select: none;'
828 ret += '-webkit-user-select: none;'
829 ret += '-moz-user-select: none;'
830 ret += 'user-select: none;'
833 ret += 'position: absolute;'
834 ret += "left: -#{inner_padding}px;"
835 ret += "top: -#{inner_padding}px;"
836 ret += "right: -#{inner_padding}px;"
837 ret += "bottom: -#{inner_padding}px;"
838 ret += 'overflow: hidden;'
841 ret += 'position: absolute;'
842 ret += 'background: rgba(100,100,100,0.2);'
845 ret += 'position: absolute;'
847 ret += 'background: linear-gradient(0deg, rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1), rgba(255,255,255,1), rgba(0,0,0,1));'
848 ret += 'background-size: 200% 200%;'
849 ret += '-webkit-animation: blink 1s linear normal infinite;'
850 ret += 'animation: blink 1s linear normal infinite;'
852 ret += '@-webkit-keyframes blink {'
853 ret += '0%{background-position:0% 0%}'
854 ret += '100%{background-position:0% -100%}'
856 ret += '@keyframes blink { '
857 ret += '0%{background-position:0% 0%}'
858 ret += '100%{background-position:0% -100%}'
862 ret += 'position: absolute;'
863 ret += 'border: 1px solid rgba(0,0,0,0.1);'
864 ret += 'outline: 1px solid rgba(255,255,255,0.1);' # in case there's a black background
867 ret += 'z-index: 10;'
868 ret += 'position: absolute;'
869 ret += 'font-size: 8px;'
870 ret += 'white-space: pre;'
871 ret += 'background: rgba(255,255,255,0.4);'
872 ret += '-ms-user-select: none;'
873 ret += '-webkit-user-select: none;'
874 ret += '-moz-user-select: none;'
875 ret += 'user-select: none;'
878 overlay_event_to_inner_xy: (e) ->
879 unless @wrap2_offset?
880 @wrap2_offset = get_el_bounds @wrap2
881 x = e.pageX - overlay_padding
882 y = e.pageY - overlay_padding + @wrap2.scrollTop
883 return x: x - @wrap2_offset.x, y: y - @wrap2_offset.y
885 xy = @overlay_event_to_inner_xy e
886 new_cursor = xy_to_cursor @tree, xy
888 @move_cursor new_cursor
892 ondoubleclick: (e) ->
896 return false if ignore_key_codes[e.keyCode]?
897 #return false if control_key_codes[e.keyCode]?
900 return false if ignore_key_codes[e.keyCode]?
901 #return false if control_key_codes[e.keyCode]?
905 new_cursor = find_prev_cursor_position @tree, @cursor
907 new_cursor = first_cursor_position @tree
909 @move_cursor new_cursor
913 new_cursor = find_next_cursor_position @tree, @cursor
915 new_cursor = last_cursor_position @tree
917 @move_cursor new_cursor
921 new_cursor = find_up_cursor_position @tree, @cursor, @cursor_ideal_x
923 saved_ideal_x = @cursor_ideal_x
924 @move_cursor new_cursor
925 @cursor_ideal_x = saved_ideal_x
927 # move cursor to first position in document
928 new_cursor = first_cursor_position @tree
930 @move_cursor new_cursor
934 new_cursor = find_down_cursor_position @tree, @cursor, @cursor_ideal_x
936 saved_ideal_x = @cursor_ideal_x
937 @move_cursor new_cursor
938 @cursor_ideal_x = saved_ideal_x
940 # move cursor to first position in document
941 new_cursor = last_cursor_position @tree
943 @move_cursor new_cursor
946 new_cursor = last_cursor_position @tree
948 @move_cursor new_cursor
954 return false unless @cursor?
955 return false unless @cursor.i < @cursor.n.text.length
956 @remove_character @cursor.n, @cursor.i
957 @text_cleanup @cursor.n
959 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
961 @move_cursor new_cursor
972 new_cursor = first_cursor_position @tree
974 @move_cursor new_cursor
988 return false if ignore_key_codes[e.keyCode]?
989 char = e.charCode ? e.keyCode
991 char = String.fromCharCode char
992 @insert_character @cursor.n, @cursor.i, char
993 @text_cleanup @cursor.n
995 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i + 1
997 @move_cursor new_cursor
999 console.log "ERROR: couldn't find cursor position after insert"
1002 on_key_enter: (e) -> # enter key pressed
1003 return unless @cursor_visible
1004 cur_block = @cursor.n
1006 if cur_block.type is 'tag'
1007 if is_display_block cur_block.el
1009 return unless cur_block.parent?
1010 cur_block = cur_block.parent
1011 # find array to insert new element into
1012 if cur_block.parent is @tree_parent # top-level
1013 parent_el = @idoc.body
1016 parent_el = cur_block.parent.el
1017 pc = cur_block.parent.children
1018 # find index of current block in its parent
1020 break if n is cur_block
1021 i += 1 # we want to be after it
1026 # TODO if content after cursor
1027 # TODO new block is empty
1028 new_text = new peach_parser.Node 'text', text: ' '
1029 new_node = new peach_parser.Node 'tag', name: 'p', parent: cur_block.parent, attrs: {style: 'white-space: pre-wrap'}, children: [new_text]
1030 new_text.parent = new_node
1031 new_text.el = domify @idoc, text: ' '
1032 new_node.el = domify @idoc, p: style: 'white-space: pre-wrap', children: [new_text.el]
1033 pc.splice i, 0, new_node
1034 parent_el.insertBefore new_node.el, before
1036 new_cursor = new_cursor_position n: new_text, i: 0
1037 throw 'bork bork' unless new_cursor?
1038 @move_cursor new_cursor
1039 # TODO move content past cursor into this new block
1040 # unlike the global function, this takes a Node, not an element
1041 is_display_block: (n) ->
1042 # TODO stop calling global function, merge it into here, use iframe's window object
1043 return false unless n.type is 'tag'
1044 return is_display_block n.el
1045 find_block_parent: (n) ->
1048 return null unless n?
1049 return n if @is_display_block n
1050 return n if n is @tree_parent
1052 # return a flat array of nodes (text, <br>, and later also inline-block)
1053 # that are flowing/wrapping together. n can be the containing block, or any
1054 # element inside it.
1055 get_text_run: (n) ->
1057 if @is_display_block n
1060 block = @find_block_parent n
1061 return ret unless block?
1062 traverse_tree block.children, (n) =>
1065 else if n.type is 'tag'
1069 disp = @computed_style n
1070 if disp is 'inline-block'
1072 return false # not done traversing
1074 _merge_left: (state) -> # helper for on_key_backspace
1075 # the node prev to n was not prev to it a moment ago, merge with it if reasonable
1076 pi = state.n.parent.children.indexOf(state.n)
1078 prev = state.n.parent.children[pi - 1]
1079 if prev.type is 'text'
1080 state.i = prev.text.length
1081 prev.text = prev.el.textContent = prev.text + state.n.text
1082 @remove_node state.n
1084 state.changed = true
1085 # else # TODO merge possible consecutive matching inline tags at @cursor
1087 _remove_node_and_inline_parents: (n) -> # helper for on_key_backspace
1088 block = @find_block_parent n
1091 # delete any inline parents
1093 while n? and n isnt block
1094 while n.children.length > 0
1095 @move_node n.children[0], n.parent, n
1099 on_key_backspace: (e) ->
1100 return unless @cursor?
1104 if @cursor.i is 0 # cursor is at start of text node
1105 run ?= @get_text_run @cursor.n
1106 run_i = run.indexOf(@cursor.n)
1107 if run_i is 0 # if at start of text run
1108 block = @find_block_parent @cursor.n
1109 prev_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
1110 if prev_cursor is null # if in first text run of document
1111 # do nothing (there's nothing text-like to the left of the cursor)
1113 # else merge with prev/outer text run
1114 pcb = @find_block_parent prev_cursor.n
1115 while block.children.length > 0
1116 @move_node block.children[0], pcb
1118 # merge possible consecutive text nodes at @cursor
1119 merge_state = n: @cursor.n
1120 @_merge_left merge_state
1121 @text_cleanup merge_state.n
1122 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
1123 else # at start of text node, but not start of text run
1124 prev = run[run_i - 1]
1125 if prev.type is 'text' # if previous in text run is text
1126 if prev.text.length is 1 # if emptying prev (in text run)
1127 @_remove_node_and_inline_parents prev
1128 merge_state = n: @cursor.n, i: @cursor.i
1129 @_merge_left merge_state
1130 @text_cleanup merge_state.n
1131 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
1132 else # prev in run is text with muliple chars
1133 # delete last character in prev
1134 prev.text = prev.text.substr(0, prev.text.length - 1)
1135 prev.el.textContent = prev.text
1136 @text_cleanup @cursor.n
1137 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
1138 else if prev.name is 'br' or prev.name is 'hr'
1139 @_remove_node_and_inline_parents prev
1140 merge_state = n: @cursor.n, i: @cursor.i
1141 @_merge_left merge_state
1142 @text_cleanup merge_state.n
1143 new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
1145 # else # if prev (in run) is inline-block
1146 # if that inline-block has text in it
1147 # delete last char in prev inlineblock
1148 # if that empties it
1152 # move cursor inside
1154 # delete prev (inline) block
1156 # auto-delete this @cursor.parent(s) if this empties them
1157 else # cursor is not at start of text node
1158 run ?= @get_text_run @cursor.n
1159 if @cursor.n.text.length is 1 # if emptying text node
1160 if run.length is 1 # if emptying text run (of text/br/hr/inline-block)
1161 # remove inline-parents of @cursor.n
1162 block = @find_block_parent @cursor.n
1164 n = @cursor.n.parent
1165 while n and n isnt block
1167 while n.children.length > 0
1168 @move_node n.children[0], n.parent, n
1171 # replace @cursor.n with a single (preserved) space
1172 if @cursor.n.text != ' '
1174 @cursor.n.text = @cursor.n.el.textContent = ' '
1176 @text_cleanup @cursor.n
1177 # place the cursor to the left of that space
1178 new_cursor = new_cursor_position n: @cursor.n, i: 0
1179 else # emptying a text node (but not a whole text run)
1180 # figure out where cursor should land
1181 block = @find_block_parent @cursor.n
1182 new_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
1183 ncb = @find_block_parent new_cursor.n
1185 new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: 1
1187 @remove_node @cursor.n
1188 # delete any inline parents
1189 n = @cursor.n.parent
1190 while n? and n isnt block
1191 # FIXME break if anything else in the text run is in here too
1192 while n.children.length > 0
1193 @move_node n.children[0], n.parent, n
1196 # update cursor dest in case things moved around
1198 new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i
1199 else # there's a char left of cursor that we can delete without emptying anything
1201 need_text_cleanup = true
1202 if @cursor.i > 1 and @cursor.i < @cursor.n.text.length
1203 pre = @cursor.n.text.substr(@cursor.i - 2, 3)
1204 post = pre.charAt(0) + pre.charAt(2)
1205 if str_has_ws_run(pre) is str_has_ws_run(post)
1206 need_text_cleanup = false
1207 @remove_character(@cursor.n, @cursor.i - 1)
1208 # call text_cleanup if whe created/removed a whitespace run
1209 if need_text_cleanup
1210 @text_cleanup @cursor.n
1211 new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
1212 # mark document changed and move the cursor
1216 @move_cursor new_cursor
1220 on_page_up_key: (e) ->
1221 if @wrap2.scrollTop is 0
1222 return unless @cursor?
1223 new_cursor = first_cursor_position @tree
1225 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1226 @move_cursor new_cursor
1229 screen_y = @cursor.y - @wrap2.scrollTop
1230 scroll_amount = @wrap2_height - breathing_room
1231 @wrap2.scrollTop = Math.max 0, @wrap2.scrollTop - scroll_amount
1233 @move_cursor_into_view screen_y + @wrap2.scrollTop
1234 on_page_down_key: (e) ->
1235 lowest_scrollpos = @wrap2.scrollHeight - @wrap2_height
1236 if @wrap2.scrollTop is lowest_scrollpos
1237 return unless @cursor?
1238 new_cursor = last_cursor_position @tree
1240 if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
1241 @move_cursor new_cursor
1244 screen_y = @cursor.y - @wrap2.scrollTop
1245 scroll_amount = @wrap2_height - breathing_room
1246 @wrap2.scrollTop = Math.min lowest_scrollpos, @wrap2.scrollTop + scroll_amount
1248 @move_cursor_into_view screen_y + @wrap2.scrollTop
1250 move_cursor_into_view: (y_target) ->
1251 return if y_target is @cursor.y
1253 y_min = @wrap2.scrollTop
1254 unless @wrap2.scrollTop is 0
1255 y_min += breathing_room
1256 y_max = @wrap2.scrollTop + @wrap2_height
1257 unless @wrap2.scrollTop is @wrap2.scrollHeight - @wrap2_height # downmost
1258 y_max -= breathing_room
1259 y_target = Math.min y_target, y_max
1260 y_target = Math.max y_target, y_min
1261 if y_target < @cursor.y
1262 finder = find_up_cursor_position
1263 far_enough = (cur, target_y) ->
1264 return cur.y + cur.h <= target_y
1266 finder = find_down_cursor_position
1267 far_enough = (cur, y_target) ->
1268 return cur.y >= y_target
1270 cur = finder @tree, was, @cursor_ideal_x
1272 break if far_enough cur, y_target
1277 if was.y + was.h > y_max
1279 else if was.y < y_min
1282 if cur.y + cur.h > y_max
1284 else if cur.y < y_min
1287 # both valid, pick best
1290 else if was.y + was.h > y_max
1292 else if cur.y - y_target < y_target - was.y
1297 new_cursor = was ? cur
1299 saved_ideal_x = @cursor_ideal_x
1300 @move_cursor new_cursor
1301 @cursor_ideal_x = saved_ideal_x
1303 clear_dom: -> # remove all the editable content (and cursor, overlays, etc)
1304 while @idoc.body.childNodes.length
1305 @idoc.body.removeChild @idoc.body.childNodes[0]
1308 load_html: (html) ->
1309 @tree = peach_parser.parse html, @parser_opts
1310 if !@tree[0]?.parent
1311 @tree = peach_parser.parse '<p style="white-space: pre-wrap"> </p>', @parser_opts
1312 @tree_parent = @tree[0]?.parent
1313 @tree_parent.el = @idoc.body
1315 instantiate_tree @tree, @tree_parent.el
1316 tree_dedup_space @tree
1319 @in_el.onchange = null
1320 @in_el.value = @pretty_html @tree
1321 @in_el.onchange = =>
1322 @load_html @in_el.value
1323 @adjust_iframe_height()
1324 adjust_iframe_height: ->
1325 s = @wrap2.scrollTop
1326 # when the content gets shorter, the idoc's body tag will continue to
1327 # report the old (too big) height in Chrome. The workaround is to
1328 # shrink the iframe before the content height:
1329 @iframe.style.height = "10px"
1330 h = parseInt(@idoc.body.scrollHeight, 10)
1331 @iframe.style.height = "#{h}px"
1332 @wrap2.scrollTop = s
1333 # does this node have whitespace that would be collapsed by white-space: normal?
1334 # note: this checks direct text children, and does _not_ recurse into child tags
1335 # tag is a node with type:"tag"
1336 # FIXME use new textrun api
1337 has_collapsable_space: (tag) ->
1338 for n in tag.children
1340 for i in [0...n.text.length]
1341 code = n.text.charCodeAt i
1342 if code isnt 32 and is_space_code code
1345 # check for double spaces that don't surround insert location
1347 if n.text.substr(i - 1, 2) is ' '
1349 if n.text.length > 0
1350 if is_space_code n.text.charCodeAt 0
1352 if is_space_code n.text.charCodeAt n.text.length - 1
1355 # true if n is text node with only one caracter, and the only child of a tag
1356 is_only_char_in_tag: (n, i) ->
1357 return false unless n.type is 'text'
1358 return false unless n.text.length is 1
1359 return false if n.parent is @tree_parent
1360 return false unless n.parent.children.length is 1
1362 # true if n is text node with just a space in it, and the only child of a tag
1363 is_lone_space: (n, i) ->
1364 return false unless n.type is 'text'
1365 return false unless n.text is ' '
1366 return false if n.parent is @tree_parent
1367 return false unless n.parent.children.length is 1
1369 # detect special case: typing before a space that's the only thing in a block/doc
1370 # reason: enter key creates blocks with just a space in them
1371 insert_should_replace: (n, i) ->
1372 return false unless i is 0
1373 return false unless n.text is ' '
1374 return true if n.parent is @tree_parent
1375 if n.parent.children.length is 1
1376 if n.parent.children[0] is n
1380 # after calling this, you MUST call changed() and text_cleanup()
1381 insert_character: (n, i, char) ->
1382 return if n.parent is @tree_parent # FIXME implement text nodes at top level
1383 # insert the character
1384 if @insert_should_replace n, i
1387 n.text = char + n.text
1388 else if i is n.text.length
1393 n.text.substr(0, i) +
1396 n.el.nodeValue = n.text
1397 # WARNING: after calling this, you MUST call changed() and text_cleanup()
1398 remove_character: (n, i) ->
1399 n.text = n.text.substr(0, i) + n.text.substr(i + 1)
1400 n.el.nodeValue = n.text
1401 computed_style: (n, prop) ->
1404 style = @iframe.contentWindow.getComputedStyle n.el, null
1405 return style.getPropertyValue prop
1406 # returns the new white-space value that will preserve spaces for node n
1407 preserve_space: (n, ideal_target) ->
1412 while target isnt ideal_target and not target.el.style.whiteSpace
1414 console.log "bug #967123"
1416 target = target.parent
1417 ws = ws_props[target.el.style.whiteSpace]?.to_preserve
1419 target.el.style.whiteSpace = ws
1420 @update_style_from_el target
1422 update_style_from_el: (n) ->
1423 style = n.el.getAttribute 'style'
1425 n.attrs.style = style
1428 delete n.attrs.style
1429 # call this after you insert or remove inline nodes. It will:
1430 # merge consecutive text nodes
1431 # remove empty text nodes
1432 # adjust white-space property
1433 text_cleanup: (n) ->
1434 if @is_display_block n
1437 block = @find_block_parent n
1438 return unless block?
1439 run = @get_text_run block
1441 # merge consecutive text nodes
1445 while i < run.length
1447 if prev.type is 'text' and n.type is 'text'
1448 if prev.parent is n.parent
1449 prev_i = n.parent.children.indexOf prev
1450 n_i = n.parent.children.indexOf n
1451 if n_i is prev_i + 1
1452 prev.text = prev.text + n.text
1453 prev.el.textContent = prev.text
1456 continue # don't increment i or change prev
1459 # remove empty text nodes
1461 while i < run.length
1466 # FIXME maybe remove parents recursively if this makes them empty
1468 continue # don't increment i
1470 # note: inline tags can have white-space:pre-line/etc
1471 # note: inline-blocks have their whitespace collapsed independantly of outer run
1472 # note: inline-blocks are treated like non-whitespace char even if empty
1473 if block.el.style.whiteSpace?
1474 ws = block.el.style.whiteSpace
1476 if ws_props[ws].space
1477 if ws_props[ws].to_collapse is 'normal'
1478 block.el.style.whiteSpace = null
1480 block.el.style.whiteSpace = ws_props[ws].to_collapse
1481 @update_style_from_el block
1482 # note: space after <br> colapses, but not space before
1483 # check for spaces that would collapse without help
1484 eats_start_sp = true # if the next node starts with space it collapses (unless pre)
1489 eats_start_sp = true
1491 eats_start_sp = false
1493 need_preserve = false
1494 if n.type isnt 'text'
1495 console.log "bug #232308"
1498 if is_space_code n.text.charCodeAt 0
1499 need_preserve = true
1500 unless need_preserve
1501 need_preserve = multi_sp_regex.test n.text
1503 # do we have it already?
1504 ws = @computed_style n, 'white-space' # FIXME implement this
1505 unless ws_props[ws]?.space
1506 # 2nd arg is ideal target for css rule
1507 ws = @preserve_space n, block
1508 eats_start_sp = false
1510 if is_space_code n.text.charCodeAt(n.text.length - 1)
1511 ws = @computed_style n, 'white-space' # FIXME implement this
1512 if ws_props[ws]?.space
1513 eats_start_sp = false
1515 eats_start_sp = true
1517 eats_start_sp = false
1518 # check if text ends with a collapsable space
1520 last = run[run.length - 1]
1521 if last.type is 'text'
1523 @preserve_space last, block
1525 css_clear: (n, prop) ->
1526 return unless n.attrs.style?
1527 return if n.attrs.style is ''
1528 css_delimiter_regex = new RegExp('\s*;\s*', 'g') # FIXME make this global
1529 styles = n.attrs.style.trim().split css_delimiter
1530 return unless styles.length > 0
1531 if styles[styles.length - 1] is ''
1533 return unless styles.length > 0
1535 while i < styles.length
1536 if styles[i].substr(0, 12) is 'white-space:'
1541 # WARNING: after calling this one or more times, you MUST:
1542 # if it's inline: call @text_cleanup
1545 i = n.parent.children.indexOf n
1547 throw "BUG #9187112313"
1548 n.el.parentNode.removeChild n.el
1549 n.parent.children.splice i, 1
1551 # remove a node from the tree/dom, insert into new_parent before insert_before?end
1552 # WARNING: after calling this one or more times, you MUST:
1553 # if it's inline: call @text_cleanup
1555 move_node: (n, new_parent, insert_before = null) ->
1556 i = n.parent.children.indexOf n
1558 throw "Error: tried to remove node, but it's not in it's parents list of children"
1561 before_i = new_parent.children.indexOf insert_before
1563 throw "Error: tried to move a node to be before a non-existent node"
1564 insert_before = insert_before.el
1567 new_parent.el.insertBefore n.el, insert_before
1568 new_parent.children.splice before_i, 0, n
1570 new_parent.el.appendChild n.el, insert_before
1571 new_parent.children.push n
1572 n.parent = new_parent
1574 kill_cursor: -> # remove it, forget where it was
1576 @cursor_el.parentNode.removeChild @cursor_el
1577 @cursor_visible = false
1581 move_cursor: (cursor) ->
1582 @cursor_ideal_x = cursor.x
1584 unless @cursor_visible
1585 @cursor_el = domify @outer_idoc, div: id: 'cursor'
1586 @overlay.appendChild @cursor_el
1587 @cursor_visible = true
1588 @cursor_el.style.left = "#{cursor.x + overlay_padding - 1}px"
1593 @cursor_el.style.top = "#{cursor.y + overlay_padding + Math.round(height * .07)}px"
1594 @cursor_el.style.height = "#{Math.round height * 0.82}px"
1596 @scroll_into_view cursor.y, height
1598 scroll_into_view: (y, h = 0) ->
1599 y += overlay_padding # convert units from @idoc to @wrap2
1600 # very top of document
1601 if y <= breathing_room
1602 @wrap2.scrollTop = 0
1604 # very bottom of document
1605 if y + h >= @wrap2.scrollHeight - breathing_room
1606 @wrap2.scrollTop = @wrap2.scrollHeight - @wrap2_height
1608 # The most scrolled up (lowest value for scrollTop) that would be OK
1609 upmost = y + h + breathing_room - @wrap2_height
1610 upmost = Math.max(upmost, 0)
1611 # the most scrolled down (highest value for scrollTop) that would be OK
1612 downmost = y - breathing_room
1613 downmost = Math.min(downmost, @wrap2.scrollHeight - @wrap2_height)
1614 if upmost > downmost # means h is too big to fit
1615 # scroll so top is visible
1616 @wrap2.scrollTop = downmost
1618 if @wrap2.scrollTop < upmost
1619 @wrap2.scrollTop = upmost
1621 if @wrap2.scrollTop > downmost
1622 @wrap2.scrollTop = downmost
1626 while @matting.length > 0
1627 @overlay.removeChild @matting[0]
1630 prev_bounds = x: 0, y: 0, w: 0, h: 0
1632 while n?.el? and n isnt @tree_parent
1636 bounds = get_el_bounds n.el
1637 return unless bounds?
1638 if bounds.x is prev_bounds.x and bounds.y is prev_bounds.y and bounds.w is prev_bounds.w and bounds.h is prev_bounds.h
1641 ann_box = domify @outer_idoc, div: class: 'ann_box', style: "left: #{bounds.x - 1 + overlay_padding}px; top: #{bounds.y - 2 + overlay_padding}px; width: #{bounds.w}px; height: #{bounds.h}px" # outline: 1000px solid rgba(0,153,255,#{alpha});
1642 @overlay.appendChild ann_box
1643 @matting.push ann_box
1644 ann_tag = domify @outer_idoc, div: class: 'ann_tag', style: "left: #{bounds.x + 1 + overlay_padding}px; top: #{bounds.y - 7 + overlay_padding}px", children: [domify @outer_idoc, text: " #{n.name} "]
1645 @overlay.appendChild ann_tag
1646 @matting.push ann_tag
1649 pretty_html: (tree, indent = '', parent_flags = pre_ish: false, block: true, want_nl: false) ->
1651 want_nl = parent_flags.want_nl
1652 prev_in_flow_is_text = false
1653 prev_in_flow_is_block = false
1656 inner_flags = want_nl: true
1663 if n.el.currentStyle?
1664 cs = n.el.currentStyle
1665 whitespace = cs['white-space']
1666 display = cs['display']
1667 position = cs['position']
1669 visibility = cs['visibility']
1671 cs = @iframe.contentWindow.getComputedStyle(n.el, null)
1672 whitespace = cs.getPropertyValue 'white-space'
1673 display = cs.getPropertyValue 'display'
1674 position = cs.getPropertyValue 'position'
1675 float = cs.getPropertyValue 'float'
1676 visibility = cs.getPropertyValue 'visibility'
1677 if n.name is 'textarea'
1678 inner_flags.pre_ish = true
1680 inner_flags.pre_ish = whitespace.substr(0, 3) is 'pre'
1682 when 'left', 'right'
1686 when 'absolute', 'fixed'
1689 if 'display' is 'none'
1693 when 'hidden', 'collapse'
1698 when 'inline', 'none'
1699 inner_flags.block = false
1700 is_block = in_flow_block = false
1702 inner_flags.block = true
1703 is_block = in_flow_block = false
1704 else # block, table, etc
1705 inner_flags.block = true
1707 in_flow_block = in_flow
1712 in_flow_block = false
1713 else # 'comment', 'doctype'
1717 in_flow_block = false
1718 # print whitespace if we can
1719 unless parent_flags.pre_ish
1720 unless prev_in_flow_is_text and is_br
1721 if (i is 0 and parent_flags.block) or in_flow_block or prev_in_flow_is_block
1734 if n.attrs[k].length > 0
1735 ret += "=\"#{enc_attr n.attrs[k]}\""
1737 unless void_elements[n.name]?
1738 if inner_flags.block
1739 next_indent = indent + ' '
1741 next_indent = indent
1742 if n.children.length
1743 ret += @pretty_html n.children, next_indent, inner_flags
1744 ret += "</#{n.name}>"
1746 ret += enc_text n.text
1748 ret += "<!--#{n.text}-->" # TODO encode?
1750 ret += "<!DOCTYPE #{n.name}"
1751 if n.public_identifier? and n.public_identifier.length > 0
1752 ret += " \"#{n.public_identifier}\""
1753 if n.system_identifier? and n.system_identifier.length > 0
1754 ret += " \"#{n.system_identifier}\""
1758 prev_in_flow_is_text = is_text
1759 prev_in_flow_is_block = is_block or (in_flow and is_br)
1761 # output final newline if allowed
1762 unless parent_flags.pre_ish
1763 if prev_in_flow_is_block or parent_flags.block
1764 ret += "\n#{indent.substr 4}"
1769 @editor_is_focused = true
1772 return if @poll_for_blur_timeout? # already polling
1773 @poll_for_blur_timeout = timeout 150, =>
1774 next_frame => # pause polling when browser knows we're not active/visible/etc.
1775 @poll_for_blur_timeout = null
1776 if document.activeElement is @outer_iframe
1779 @editor_is_focused = false
1782 window.peach_html5_editor = (args...) ->
1783 return new PeachHTML5Editor args...
1785 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])