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/>.
17 TYPE_TAG = peach_parser.TYPE_TAG
18 TYPE_TEXT = peach_parser.TYPE_TEXT
19 TYPE_COMMENT = peach_parser.TYPE_COMMENT
20 TYPE_DOCTYPE = peach_parser.TYPE_DOCTYPE
22 get_el_offset = (el) ->
23 rect = el.getBoundingClientRect()
24 doc = el.ownerDocument.documentElement
25 win = el.ownerDocument.defaultView
27 top: rect.top + win.pageYOffset - doc.clientTop
28 left: rect.left + win.pageXOffset - doc.clientLeft
31 # figure out the top/left coordinates of where the cursor should be if it's at
32 # position ``i`` within text node ``n``
34 # implementation: insert a span tag where we want the cursor, and ask the
35 # browser where it put that span
36 cursor_to_loc = (n, i) ->
37 span = domify span: style: "height: 1em"
38 parent = n.el.parentNode
40 # cursor at start of text
41 parent.insertBefore span, n.el
42 ret = get_el_offset span
43 span.parentNode.removeChild span
45 else if i is n.text.length
46 # cursor at end of text
47 parent.insertBefore span, n.el.nextSibling
48 ret = get_el_offset span
49 span.parentNode.removeChild span
51 # cursor in middle of text
53 t0 = n.el.ownerDocument.createTextNode n.text.substr 0, i
54 t1 = n.el.ownerDocument.createTextNode n.text.substr i
55 parent.insertBefore t0, n.el
56 parent.insertBefore span, n.el
57 parent.insertBefore t1, n.el
58 parent.removeChild n.el
59 ret = get_el_offset span
60 parent.insertBefore n.el, t0
62 parent.removeChild span
67 #loc_to_cursor = (n, i) ->
70 # encode text so it can be safely placed inside an html attribute
71 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
73 return txt.replace enc_attr_regex, (match, amp, quote) ->
74 return '&' if (amp)
75 return '"' if (quote)
95 dom_to_html = (dom) ->
107 if el.attrs[k].length > 0
108 ret += "=\"#{enc_attr el.attrs[k]}\""
110 unless void_elements[el.name]
111 if el.children.length
112 ret += dom_to_html el.children
113 ret += "</#{el.name}>"
117 ret += "<!--#{el.text}-->"
119 ret += "<!DOCTYPE #{el.name}"
120 if el.public_identifier? and el.public_identifier.length > 0
121 ret += " \"#{el.public_identifier}\""
122 if el.system_identifier? and el.system_identifier.length > 0
123 ret += " \"#{el.system_identifier}\""
130 return document.createTextNode attrs
131 el = document.createElement tag
141 css += 'div#peach_html5_editor_cursor {'
142 css += 'position: absolute;'
143 css += 'height: 1em;'
145 css += 'margin-left: -1px;'
146 css += 'margin-right: -1px;'
147 css += 'background: #000;'
148 css += '-webkit-animation: 1s blink step-end infinite;'
149 css += 'animation: 1s blink step-end infinite;'
151 css += '@-webkit-keyframes "blink" {'
152 css += 'from, to { background: #000; }'
153 css += '50% { background: transparent; }'
155 css += '@keyframes "blink" {'
156 css += 'from, to { background: #000; }'
157 css += '50% { background: transparent; }'
165 KEY_BACKSPACE = 8 # <--
166 KEY_DELETE = 46 # -->
176 instantiate_tree = (tree, parent) ->
180 c.el = parent.ownerDocument.createTextNode c.text
181 parent.appendChild c.el
183 # TODO create in correct namespace
184 c.el = parent.ownerDocument.createElement c.name
186 # FIXME if attr_whitelist[k]?
187 c.el.setAttribute k, v
188 parent.appendChild c.el
190 instantiate_tree c.children, c.el
192 find_next_cursor_position = (n, i) ->
193 if n.type is TYPE_TEXT
197 if n.type is TYPE_TAG
200 ret = find_next_cursor_position c, -1
204 # FIXME create an empty text node if needed depending on tag name
207 class PeachHTML5Editor
208 constructor: (in_el, options = {}) ->
211 @iframe = document.createElement('iframe')
214 @cursor_visible = false
215 opt_fragment = options.fragment ? true
218 @parser_opts.fragment = 'body'
219 @in_el.parentNode.appendChild @iframe
220 @idoc = @iframe.contentDocument
224 '20': true # capslock
226 '144': true # numlock
228 '91': true # windows "start" key
229 control_key_codes = # we react to these, but they aren't typing
245 @idoc.body.onkeyup = (e) =>
247 return false if ignore_key_codes[e.keyCode]?
248 #return false if control_key_codes[e.keyCode]?
249 @idoc.body.onkeydown = (e) =>
251 return false if ignore_key_codes[e.keyCode]?
252 #return false if control_key_codes[e.keyCode]?
259 window.ce = @cursor_el
261 new_cursor = find_next_cursor_position @cursor...
263 @move_cursor new_cursor
266 for c in @cursor[0].parent.children
268 new_cursor = find_next_cursor_position c, -1
270 @move_cursor new_cursor
276 new_cursor = find_next_cursor_position c, -1
278 @move_cursor new_cursor
303 @idoc.body.onkeypress = (e) =>
305 return false if ignore_key_codes[e.keyCode]?
306 # in firefox, keyCode is only set for non-typing keys
316 new_cursor = find_next_cursor_position @cursor...
318 @move_cursor new_cursor
321 for c in @cursor[0].parent.children
323 new_cursor = find_next_cursor_position c, -1
325 @move_cursor new_cursor
331 new_cursor = find_next_cursor_position c, -1
333 @move_cursor new_cursor
358 char = e.charCode ? e.keyCode
359 @in_el.value += String.fromCharCode char
360 @load_html @in_el.value
362 if options.stylesheet # TODO test this
363 istyle = @idoc.createElement 'style'
364 istyle.setAttribute 'src', options.stylesheet
365 @idoc.head.appendChild istyle
366 icss = @idoc.createElement 'style'
367 icss.appendChild @idoc.createTextNode css
368 @idoc.head.appendChild icss
369 @load_html @in_el.value
371 # FIXME add parent node, so we don't empty body and delete cursor_el
372 while @idoc.body.childNodes.length
373 @idoc.body.removeChild @idoc.body.childNodes[0]
374 @cursor_visible = false
377 @tree = peach_parser.parse html, @parser_opts
378 #as_html = dom_to_html @tree
379 #@iframe.contentDocument.body.innerHTML = as_html
381 instantiate_tree @tree, @idoc.body
382 move_cursor: (cursor) ->
385 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
386 unless @cursor_visible
387 @idoc.body.appendChild @cursor_el
388 @cursor_visible = true
389 # TODO figure out top/left coords for cursor
390 loc = cursor_to_loc cursor[0], cursor[1]
391 @cursor_el.style.top = "#{loc.top}px"
392 @cursor_el.style.left = "#{loc.left}px"
394 window.peach_html5_editor = (args...) ->
395 return new PeachHTML5Editor args...
397 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])