JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
create overlay, move cursor to it, break typing
[peach-html5-editor.git] / editor.coffee
1 # Copyright 2015 Jason Woofenden
2 # This file implements an WYSIWYG editor in the browser (no contenteditable)
3 #
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
7 # later version.
8 #
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
12 # details.
13 #
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/>.
16
17 # SETTINGS
18 overlay_padding = 6 # TODO generate css from this
19
20 TYPE_TAG = peach_parser.TYPE_TAG
21 TYPE_TEXT = peach_parser.TYPE_TEXT
22 TYPE_COMMENT = peach_parser.TYPE_COMMENT
23 TYPE_DOCTYPE = peach_parser.TYPE_DOCTYPE
24
25 debug_dot_at = (doc, x, y) ->
26         el = doc.createElement 'div'
27         el.setAttribute 'style', "position: absolute; left: #{x}px; top: #{y}px; width: 1px; height: 3px; background-color: red"
28         doc.body.appendChild el
29         #console.log(new Error().stack)
30
31 # text nodes don't have getBoundingClientRect(), so use selection api to find
32 # it.
33 get_el_bounds = (el) ->
34         if el.getBoundingClientRect?
35                 rect = el.getBoundingClientRect()
36         else
37                 # text nodes don't have getBoundingClientRect(), so use range api
38                 range = el.ownerDocument.createRange()
39                 range.selectNodeContents el
40                 rect = range.getBoundingClientRect()
41         doc = el.ownerDocument.documentElement
42         win = el.ownerDocument.defaultView
43         y_fix = win.pageYOffset - doc.clientTop
44         x_fix = win.pageXOffset - doc.clientLeft
45         return {
46                 x: rect.left + x_fix
47                 y: rect.top + y_fix
48                 w: rect.width ? (rect.right - rect.left)
49                 h: rect.height ? (rect.top - rect.bottom)
50         }
51
52 is_display_block = (el) ->
53         if el.currentStyle?
54                 return el.currentStyle.display is 'block'
55         else
56                 return window.getComputedStyle(el, null).getPropertyValue('display') is 'block'
57
58 # Warning: currently assumes you're asking about a single character
59 # Note: chromium returns multiple bounding rects for a space at a line-break
60 # Note: chromium's getBoundingClientRect() is broken (when zero-area client rects)
61 # Note: sometimes returns null (eg for whitespace that is not visible)
62 text_range_bounds = (el, start, end) ->
63         range = document.createRange()
64         range.setStart el, start
65         range.setEnd el, end
66         rects = range.getClientRects()
67         if rects.length > 0
68                 rect = rects[0]
69         else
70                 return null
71         doc = el.ownerDocument.documentElement
72         win = el.ownerDocument.defaultView
73         y_fix = win.pageYOffset - doc.clientTop
74         x_fix = win.pageXOffset - doc.clientLeft
75         return {
76                 x: rect.left + x_fix
77                 y: rect.top + y_fix
78                 w: rect.width ? (rect.right - rect.left)
79                 h: rect.height ? (rect.top - rect.bottom)
80                 rects: rects
81                 bounding: range.getBoundingClientRect()
82         }
83
84 # figure out the x/y coordinates of where the cursor should be if it's at
85 # position ``i`` within text node ``n``
86 # sometimes returns null (eg for whitespace that is not visible)
87 window.cursor_to_xyh = cursor_to_xyh = (n, i) ->
88         range = document.createRange()
89         if n.text.length is 0
90                 ret = text_range_bounds n.el, 0, 0
91         if i is n.text.length
92                 ret = text_range_bounds n.el, i - 1, i
93                 if ret?
94                         ret.x += ret.w
95         else
96                 ret = text_range_bounds n.el, i, i + 1
97         if ret?
98                 debug_dot_at n.el.ownerDocument, ret.x, ret.y
99         return ret
100
101 # encode text so it can be safely placed inside an html attribute
102 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
103 enc_attr = (txt) ->
104         return txt.replace enc_attr_regex, (match, amp, quote) ->
105                 return '&amp;' if (amp)
106                 return '&quot;' if (quote)
107                 return '&nbsp;'
108 enc_text_regex = new RegExp '(&)|(<)|(\u00A0)', 'g'
109 enc_text = (txt) ->
110         return txt.replace enc_text_regex, (match, amp, lt) ->
111                 return '&amp;' if (amp)
112                 return '&lt;' if (lt)
113                 return '&nbsp;'
114
115 void_elements = {
116         area: true
117         base: true
118         br: true
119         col: true
120         embed: true
121         hr: true
122         img: true
123         input: true
124         keygen: true
125         link: true
126         meta: true
127         param: true
128         source: true
129         track: true
130         wbr: true
131 }
132 dom_to_html = (dom) ->
133         ret = ''
134         for el in dom
135                 switch el.type
136                         when TYPE_TAG
137                                 ret += '<' + el.name
138                                 attr_keys = []
139                                 for k of el.attrs
140                                         attr_keys.unshift k
141                                 #attr_keys.sort()
142                                 for k in attr_keys
143                                         ret += " #{k}"
144                                         if el.attrs[k].length > 0
145                                                 ret += "=\"#{enc_attr el.attrs[k]}\""
146                                 ret += '>'
147                                 unless void_elements[el.name]
148                                         if el.children.length
149                                                 ret += dom_to_html el.children
150                                         ret += "</#{el.name}>"
151                         when TYPE_TEXT
152                                 ret += enc_text el.text
153                         when TYPE_COMMENT
154                                 ret += "<!--#{el.text}-->"
155                         when TYPE_DOCTYPE
156                                 ret += "<!DOCTYPE #{el.name}"
157                                 if el.public_identifier? and el.public_identifier.length > 0
158                                         ret += " \"#{el.public_identifier}\""
159                                 if el.system_identifier? and el.system_identifier.length > 0
160                                         ret += " \"#{el.system_identifier}\""
161                                 ret += ">\n"
162         return ret
163
164 domify = (h) ->
165         for tag, attrs of h
166                 if tag is 'text'
167                         return document.createTextNode attrs
168                 el = document.createElement tag
169                 for k, v of attrs
170                         if k is 'children'
171                                 for child in v
172                                         el.appendChild child
173                         else
174                                 el.setAttribute k, v
175         return el
176
177 css = ''
178 css += 'div#peach_html5_editor_cursor {'
179 css +=     'position: absolute;'
180 css +=     'height: 1em;'
181 css +=     'width: 2px;'
182 css +=     'margin-left: -1px;'
183 css +=     'margin-right: -1px;'
184 css +=     'background: #444;'
185 css +=     '-webkit-animation: blink 1s steps(2, start) infinite;'
186 css +=     'animation: blink 1s steps(2, start) infinite;'
187 css += '}'
188 css += '@-webkit-keyframes blink {'
189 css +=     'to { visibility: hidden; }'
190 css += '}'
191 css += '@keyframes blink {'
192 css +=     'to { visibility: hidden; }'
193 css += '}'
194
195 # key codes:
196 KEY_LEFT = 37
197 KEY_UP = 38
198 KEY_RIGHT = 39
199 KEY_DOWN = 40
200 KEY_BACKSPACE = 8 # <--
201 KEY_DELETE = 46 # -->
202 KEY_END = 35
203 KEY_ENTER = 13
204 KEY_ESCAPE = 27
205 KEY_HOME = 36
206 KEY_INSERT = 45
207 KEY_PAGE_UP = 33
208 KEY_PAGE_DOWN = 34
209 KEY_TAB = 9
210
211 instantiate_tree = (tree, parent) ->
212         for c in tree
213                 switch c.type
214                         when TYPE_TEXT
215                                 c.el = parent.ownerDocument.createTextNode c.text
216                                 parent.appendChild c.el
217                         when TYPE_TAG
218                                 # TODO create in correct namespace
219                                 c.el = parent.ownerDocument.createElement c.name
220                                 for k, v of c.attrs
221                                         # FIXME if attr_whitelist[k]?
222                                         c.el.setAttribute k, v
223                                 parent.appendChild c.el
224                                 if c.children.length
225                                         instantiate_tree c.children, c.el
226
227 traverse_tree = (tree, state, cb) ->
228         for c in tree
229                 cb c, state
230                 break if state.done?
231                 if c.children.length
232                         traverse_tree c.children, state, cb
233                         break if state.done?
234         return state
235 # find the next element in tree (and decendants) that is after n and can contain text
236 # TODO make it so cursor can go places that don't have text but could
237 find_next_cursor_position = (tree, n, i) ->
238         if n? and n.type is TYPE_TEXT and n.text.length > i
239                 orig_xyh = cursor_to_xyh n, i
240                 unless orig_xyh?
241                         console.log "ERROR: couldn't find xy for current cursor location"
242                         return
243                 for next_i in [i+1 .. n.text.length] # inclusive is valid (after last char)
244                         next_xyh = cursor_to_xyh n, next_i
245                         if next_xyh?
246                                 if next_xyh.x > orig_xyh.x or next_xyh.y > orig_xyh.y
247                                         return [n, next_i]
248         found = traverse_tree tree, before: n?, (node, state) ->
249                 if node.type is TYPE_TEXT and state.before is false
250                         state.node = node
251                         state.done = true
252                 if node is n
253                         state.before = false
254         if found.node?
255                 return [found.node, 0]
256         return null
257
258 # TODO make it so cursor can go places that don't have text but could
259 find_prev_cursor_position = (tree, n, i) ->
260         if n? and n.type is TYPE_TEXT and i > 0
261                 orig_xyh = cursor_to_xyh n, i
262                 unless orig_xyh?
263                         console.log "ERROR: couldn't find xy for current cursor location"
264                         return
265                 for prev_i in [i-1 .. 0]
266                         prev_xyh = cursor_to_xyh n, prev_i
267                         if prev_xyh?
268                                 if prev_xyh.x < orig_xyh.x or prev_xyh.y < orig_xyh.y
269                                         return [n, prev_i]
270                 return [n, i - 1]
271         found = traverse_tree tree, before: n?, (node, state) ->
272                 if node.type is TYPE_TEXT
273                         unless n?
274                                 state.node = node
275                                 state.done = true
276                         if node is n
277                                 if state.prev?
278                                         state.node = state.prev
279                                 state.done = true
280                         if node
281                                 state.prev = node
282         if found.node?
283                 return [found.node, found.node.text.length]
284         return null
285
286 find_loc_cursor_position = (tree, loc) ->
287         for c in tree
288                 if c.type is TYPE_TAG or c.type is TYPE_TEXT
289                         bounds = get_el_bounds c.el
290                         continue if loc.x < bounds.x
291                         continue if loc.x > bounds.x + bounds.w
292                         continue if loc.y < bounds.y
293                         continue if loc.y > bounds.y + bounds.h
294                         if c.children.length
295                                 ret = find_loc_cursor_position c.children, loc
296                                 return ret if ret?
297                         if c.type is TYPE_TEXT
298                                 # click is within bounding box that contains all text.
299                                 return [c, 0] if c.text.length is 0
300                                 before_i = 0
301                                 before = cursor_to_xyh c, before_i
302                                 unless before?
303                                         console.log "error: failed to find cursor pixel location for start of", c
304                                         return
305                                 after_i = c.text.length
306                                 after = cursor_to_xyh c, after_i
307                                 unless after?
308                                         console.log "error: failed to find cursor pixel location for end of", c
309                                         return
310                                 if loc.y < before.y + before.h and loc.x < before.x
311                                         # console.log 'before first char on first line'
312                                         continue
313                                 if loc.y > after.y and loc.x > after.x
314                                         # console.log 'after last char on last line'
315                                         continue
316                                 if loc.y < before.y
317                                         console.log "Warning: click in bounding box but above first line"
318                                         continue # above first line (runaround?)
319                                 if loc.y > after.y + after.h
320                                         console.log "Warning: click in bounding box but below last line", loc.y, after.y, after.h
321                                         continue # below last line (shouldn't happen?)
322                                 while after_i - before_i > 1
323                                         cur_i = Math.round((before_i + after_i) / 2)
324                                         cur = cursor_to_xyh c, cur_i
325                                         unless loc?
326                                                 console.log "error: failed to find cursor pixel location for", c, cur_i
327                                                 return
328                                         if loc.y < cur.y or (loc.y <= cur.y + cur.h and loc.x < cur.x)
329                                                 after_i = cur_i
330                                                 after = cur
331                                         else
332                                                 before_i = cur_i
333                                                 before = cur
334                                 # which one is closest?
335                                 if Math.abs(before.x - loc.x) < Math.abs(after.x - loc.x)
336                                         return [c, before_i]
337                                 else
338                                         return [c, after_i]
339         return null
340
341 # browsers collapse these (html5 spec calls these "space characters")
342 is_space_code = (char_code) ->
343         switch char_code
344                 when 9, 10, 12, 13, 32
345                         return true
346         return false
347 is_space = (chr) ->
348         return is_space_code chr.charCodeAt 0
349
350 # pass a array of nodes (from parser library, ie it should have .el and .text)
351 tree_dedup_space = (tree) ->
352         prev = cur = next = null
353         prev_i = cur_i = next_i = 0
354         prev_pos = pos = next_pos = null
355         prev_px = cur_px = next_px = null
356         first = true
357         removed_char = null
358
359         iterate = (tree, cb) ->
360                 for n in tree
361                         if n.type is TYPE_TEXT
362                                 i = 0
363                                 while i < n.text.length # don't foreach, cb might remove chars
364                                         advance = cb n, i
365                                         if advance
366                                                 i += 1
367                         if n.type is TYPE_TAG
368                                 block = is_display_block n.el
369                                 if block
370                                         cb null
371                                 if n.children.length > 0
372                                         iterate n.children, cb
373                                 if block
374                                         cb null
375         # remove cur char
376         remove = ->
377                 removed_char = cur.text.charAt(cur_i)
378                 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + (cur.text.substr cur_i + 1)
379                 if next is cur # in same text node
380                         if next_i is 0
381                                 throw "how is this possible?"
382                         next_i -= 1
383                 return true
384         # undo remove()
385         put_it_back = ->
386                 cur.el.textContent = cur.text = (cur.text.substr 0, cur_i) + removed_char + (cur.text.substr cur_i)
387                 if next is cur # in same text node
388                         next_i += 1
389                 return false
390         # return true if cur was removed from the dom (ie re-use same prev)
391         operate = ->
392                 # cur definitately set
393                 # prev and/or next might be null, indicating the start/end of a display:block
394                 return false unless is_space_code cur.text.charCodeAt cur_i
395                 bounds = text_range_bounds cur.el, cur_i, cur_i + 1
396                 # consistent cases:
397                 # 1. zero rects returned by getClientRects() means collapsed space
398                 if bounds is null
399                         return remove()
400                 # 2. width greater than zero means visible space
401                 if bounds.w > 0
402                         return false
403                 # now the weird edge cases...
404                 #
405                 # firefox and chromium both report zero width for characters at the end
406                 # of a line where the text wraps (automatically, due to word-wrap) to
407                 # the next line. These do not appear to be distinguishable from
408                 # collapsed spaces via the range/bounds api, so...
409                 #
410                 # remove it from the dom, and if prev or next moves, put it back.
411                 if prev? and not prev_px?
412                         prev_px = cursor_to_xyh prev, prev_i
413                 if next? and not next_px?
414                         next_px = cursor_to_xyh next, next_i
415                 #if prev is null and next is null
416                 #       parent_px = cur.parent.el.getBoundingClientRect()
417                 remove()
418                 if prev?
419                         if prev_px?
420                                 new_prev_px = cursor_to_xyh prev, prev_i
421                                 if new_prev_px.x isnt prev_px.x or new_prev_px.y isnt prev_px.y
422                                         return put_it_back()
423                         else
424                                 console.log "this shouldn't happen, we remove spaces that don't locate"
425                 if next?
426                         if next_px?
427                                 new_next_px = cursor_to_xyh next, next_i
428                                 if new_next_px.x isnt next_px.x or new_next_px.y isnt next_px.y
429                                         return put_it_back()
430                         #else
431                         #       console.log "removing space becase space after it is collapsed"
432                 # if there's no prev or next (single space inside a block-level element?) check
433                 # TODO scrapt this, or fix it so it works when there's no parent
434                 # if prev is null and next is null
435                 #       new_parent_px = cur.parent.el.getBoundingClientRect()
436                 #       if new_parent_px.left isnt parent_px.left or new_parent_px.top isnt parent_px.top or new_parent_px.right isnt parent_px.right or new_parent_px.bottom isnt parent_px.bottom
437                 #               console.log "WEIRD: parent moved"
438                 #               return put_it_back()
439                 # we didn't put it back
440                 return true
441         # pass null at start/end of display:block
442         queue = (n, i) ->
443                 next = n
444                 next_i = i
445                 next_px = null
446                 advance = true
447                 if cur?
448                         removed = operate()
449                         # don't advance (to the next character next time) if we removed a
450                         # character from the same text node as ``next``, because doing so
451                         # renumbers the indexes in that string
452                         if removed and cur is next
453                                 advance = false
454                 else
455                         removed = false
456                 unless removed
457                         prev = cur
458                         prev_i = cur_i
459                         prev_px = cur_px
460                 cur = next
461                 cur_i = next_i
462                 cur_px = next_px
463                 return advance
464         queue null
465         iterate tree, queue
466         queue null
467
468 class PeachHTML5Editor
469         constructor: (in_el, options = {}) ->
470                 @in_el = in_el
471                 @tree = []
472                 @outer_el = domify div: class: 'peach_html5_editor', children: [
473                         @iframe = domify iframe: class: 'peach_html5_editor_iframe'
474                         @overlay = domify div: class: 'peach_html5_editor_overlay'
475                 ]
476                 @cursor = null
477                 @cursor_el = null
478                 @cursor_visible = false
479                 opt_fragment = options.fragment ? true
480                 @parser_opts = {}
481                 if opt_fragment
482                         @parser_opts.fragment = 'body'
483
484                 @iframe.onload = =>
485                         @idoc = @iframe.contentDocument
486
487                         ignore_key_codes =
488                                 '18': true # alt
489                                 '20': true # capslock
490                                 '17': true # ctrl
491                                 '144': true # numlock
492                                 '16': true # shift
493                                 '91': true # windows "start" key
494                         control_key_codes = # we react to these, but they aren't typing
495                                 '37': KEY_LEFT
496                                 '38': KEY_UP
497                                 '39': KEY_RIGHT
498                                 '40': KEY_DOWN
499                                 '35': KEY_END
500                                 '8':  KEY_BACKSPACE
501                                 '46': KEY_DELETE
502                                 '13': KEY_ENTER
503                                 '27': KEY_ESCAPE
504                                 '36': KEY_HOME
505                                 '45': KEY_INSERT
506                                 '33': KEY_PAGE_UP
507                                 '34': KEY_PAGE_DOWN
508                                 '9':  KEY_TAB
509
510                         @overlay.onclick = (e) =>
511                                 x = (e.offsetX ? e.layerX) - overlay_padding
512                                 y = (e.offsetY ? e.layerY) - overlay_padding
513                                 new_cursor = find_loc_cursor_position @tree, x: x, y: y
514                                 if new_cursor?
515                                         @move_cursor new_cursor
516                         @idoc.body.onkeyup = (e) =>
517                                 return if e.ctrlKey
518                                 return false if ignore_key_codes[e.keyCode]?
519                                 #return false if control_key_codes[e.keyCode]?
520                         @idoc.body.onkeydown = (e) =>
521                                 return if e.ctrlKey
522                                 return false if ignore_key_codes[e.keyCode]?
523                                 #return false if control_key_codes[e.keyCode]?
524                                 switch e.keyCode
525                                         when KEY_LEFT
526                                                 if @cursor?
527                                                         new_cursor = find_prev_cursor_position @tree, @cursor...
528                                                         if new_cursor?
529                                                                 @move_cursor new_cursor
530                                                 else
531                                                         for c in @tree
532                                                                 new_cursor = find_next_cursor_position @tree, c, -1
533                                                                 if new_cursor?
534                                                                         @move_cursor new_cursor
535                                                                         break
536                                                 return false
537                                         when KEY_UP
538                                                 return false
539                                         when KEY_RIGHT
540                                                 if @cursor?
541                                                         new_cursor = find_next_cursor_position @tree, @cursor...
542                                                         if new_cursor?
543                                                                 @move_cursor new_cursor
544                                                 else
545                                                         for c in @tree
546                                                                 new_cursor = find_prev_cursor_position @tree, c, -1
547                                                                 if new_cursor?
548                                                                         @move_cursor new_cursor
549                                                                         break
550                                                 return false
551                                         when KEY_DOWN
552                                                 return false
553                                         when KEY_END
554                                                 return false
555                                         when KEY_BACKSPACE
556                                                 return false unless @cursor?
557                                                 return false unless @cursor[1] > 0
558                                                 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1] - 1) + @cursor[0].text.substr(@cursor[1])
559                                                 @cursor[0].el.nodeValue = @cursor[0].text
560                                                 @move_cursor [@cursor[0], @cursor[1] - 1]
561                                                 return false
562                                         when KEY_DELETE
563                                                 return false unless @cursor?
564                                                 return false unless @cursor[1] < @cursor[0].text.length
565                                                 @cursor[0].text = @cursor[0].text.substr(0, @cursor[1]) + @cursor[0].text.substr(@cursor[1] + 1)
566                                                 @cursor[0].el.nodeValue = @cursor[0].text
567                                                 @move_cursor [@cursor[0], @cursor[1]]
568                                                 return false
569                                         when KEY_ENTER
570                                                 return false
571                                         when KEY_ESCAPE
572                                                 return false
573                                         when KEY_HOME
574                                                 return false
575                                         when KEY_INSERT
576                                                 return false
577                                         when KEY_PAGE_UP
578                                                 return false
579                                         when KEY_PAGE_DOWN
580                                                 return false
581                                         when KEY_TAB
582                                                 return false
583                         @idoc.body.onkeypress = (e) =>
584                                 return if e.ctrlKey
585                                 return false if ignore_key_codes[e.keyCode]?
586                                 return false if control_key_codes[e.keyCode]? # handled in keydown
587                                 char = e.charCode ? e.keyCode
588                                 if char and @cursor?
589                                         char = String.fromCharCode char
590                                         if @cursor[1] is 0
591                                                 @cursor[0].text = char + @cursor[0].text
592                                         else if @cursor[1] is @cursor[0].text.length - 1
593                                                 @cursor[0].text += char
594                                         else
595                                                 @cursor[0].text =
596                                                         @cursor[0].text.substr(0, @cursor[1]) +
597                                                         char +
598                                                         @cursor[0].text.substr(@cursor[1])
599                                         @cursor[0].el.nodeValue = @cursor[0].text
600                                         @move_cursor [@cursor[0], @cursor[1] + 1]
601                                         @changed()
602                                 return false
603                         if options.stylesheet # TODO test this
604                                 istyle = @idoc.createElement 'style'
605                                 istyle.setAttribute 'src', options.stylesheet
606                                 @idoc.head.appendChild istyle
607                         icss = @idoc.createElement 'style'
608                         icss.appendChild @idoc.createTextNode css
609                         document.head.appendChild icss
610                         @load_html @in_el.value
611
612                 @in_el.parentNode.appendChild @outer_el
613         clear_dom: ->
614                 # FIXME add parent node, so we don't empty body and delete cursor_el
615                 while @idoc.body.childNodes.length
616                         @idoc.body.removeChild @idoc.body.childNodes[0]
617                 @kill_cursor()
618                 @cursor_visible = false
619                 return
620         load_html: (html) ->
621                 @tree = peach_parser.parse html, @parser_opts
622                 @clear_dom()
623                 instantiate_tree @tree, @idoc.body
624                 tree_dedup_space @tree
625                 @changed()
626         changed: ->
627                 # FIXME don't export cursor placeholder (when cursor is between space characters)
628                 @in_el.onchange = null
629                 @in_el.value = dom_to_html @tree
630                 @in_el.onchange = =>
631                         @load_html @in_el.value
632         kill_cursor: -> # remove it, forget where it was
633                 if @cursor_visible
634                         @cursor_el.parentNode.removeChild @cursor_el
635                         @cursor_visible = false
636                 @cursor = null
637         move_cursor: (cursor) ->
638                 loc = cursor_to_xyh cursor[0], cursor[1]
639                 unless loc?
640                         console.log "error: tried to move cursor to position that has no pixel location", cursor[0], cursor[1]
641                         return
642                 @cursor = cursor
643                 # replace cursor, to reset blink animation
644                 if @cursor_visible
645                         @cursor_el.parentNode.removeChild @cursor_el
646                 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
647                 @overlay.appendChild @cursor_el
648                 @cursor_visible = true
649                 # TODO figure out x,y coords for cursor
650                 @cursor_el.style.left = "#{loc.x + overlay_padding}px"
651                 @cursor_el.style.top = "#{loc.y + overlay_padding}px"
652
653 window.peach_html5_editor = (args...) ->
654         return new PeachHTML5Editor args...
655
656 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])