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; width: 0", children: [domify text: "|"]
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 traverse_tree = (tree, state, cb) ->
197 traverse_tree c.children, state, cb
200 # find the next element in top (and decendants) that is after n and can contain text
201 # TODO make it so cursor can go places that don't have text but could
202 find_next_cursor_position = (top, n, i) ->
203 if n? and n.type is TYPE_TEXT and n.text.length > i
205 found = traverse_tree top, before: n?, (node, state) ->
206 if node.type is TYPE_TEXT and state.before is false
212 return [found.node, 0]
215 # TODO make it so cursor can go places that don't have text but could
216 find_prev_cursor_position = (top, n, i) ->
217 if n? and n.type is TYPE_TEXT and i > 0
219 found = traverse_tree top, before: n?, (node, state) ->
220 if node.type is TYPE_TEXT
226 state.node = state.prev
231 return [found.node, found.node.text.length]
234 class PeachHTML5Editor
235 constructor: (in_el, options = {}) ->
238 @iframe = domify iframe: class: 'peach_html5_editor'
241 @cursor_visible = false
242 opt_fragment = options.fragment ? true
245 @parser_opts.fragment = 'body'
248 @idoc = @iframe.contentDocument
252 '20': true # capslock
254 '144': true # numlock
256 '91': true # windows "start" key
257 control_key_codes = # we react to these, but they aren't typing
273 @idoc.body.onkeyup = (e) =>
275 return false if ignore_key_codes[e.keyCode]?
276 #return false if control_key_codes[e.keyCode]?
277 @idoc.body.onkeydown = (e) =>
279 return false if ignore_key_codes[e.keyCode]?
280 #return false if control_key_codes[e.keyCode]?
284 new_cursor = find_prev_cursor_position @tree, @cursor...
286 @move_cursor new_cursor
289 new_cursor = find_next_cursor_position @tree, c, -1
291 @move_cursor new_cursor
298 new_cursor = find_next_cursor_position @tree, @cursor...
300 @move_cursor new_cursor
303 new_cursor = find_prev_cursor_position @tree, c, -1
305 @move_cursor new_cursor
330 @idoc.body.onkeypress = (e) =>
332 return false if ignore_key_codes[e.keyCode]?
333 return false if control_key_codes[e.keyCode]? # handled in keydown
334 char = e.charCode ? e.keyCode
336 char = String.fromCharCode char
338 @cursor[0].text = char + @cursor[0].text
339 else if @cursor[1] is @cursor[0].text.length - 1
340 @cursor[0].text += char
343 @cursor[0].text.substr(0, @cursor[1]) +
345 @cursor[0].text.substr(@cursor[1])
346 @cursor[0].el.nodeValue = @cursor[0].text
347 @move_cursor [@cursor[0], @cursor[1] + 1]
349 if options.stylesheet # TODO test this
350 istyle = @idoc.createElement 'style'
351 istyle.setAttribute 'src', options.stylesheet
352 @idoc.head.appendChild istyle
353 icss = @idoc.createElement 'style'
354 icss.appendChild @idoc.createTextNode css
355 @idoc.head.appendChild icss
356 @load_html @in_el.value
358 @in_el.parentNode.appendChild @iframe
360 # FIXME add parent node, so we don't empty body and delete cursor_el
361 while @idoc.body.childNodes.length
362 @idoc.body.removeChild @idoc.body.childNodes[0]
363 @cursor_visible = false
366 @tree = peach_parser.parse html, @parser_opts
367 #as_html = dom_to_html @tree
368 #@iframe.contentDocument.body.innerHTML = as_html
370 instantiate_tree @tree, @idoc.body
371 move_cursor: (cursor) ->
372 return if @cursor? and cursor? and @cursor[0] is cursor[0] and @cursor[1] is cursor[1]
374 # replace cursor, to reset blink animation
376 @cursor_el.parentNode.removeChild @cursor_el
377 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
378 @idoc.body.appendChild @cursor_el
379 @cursor_visible = true
380 # TODO figure out top/left coords for cursor
381 loc = cursor_to_loc cursor[0], cursor[1]
382 @cursor_el.style.top = "#{loc.top}px"
383 @cursor_el.style.left = "#{loc.left}px"
385 window.peach_html5_editor = (args...) ->
386 return new PeachHTML5Editor args...
388 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])