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_bounds = (el) ->
23 rect = el.getBoundingClientRect()
24 doc = el.ownerDocument.documentElement
25 win = el.ownerDocument.defaultView
26 y_fix = win.pageYOffset - doc.clientTop
27 x_fix = win.pageXOffset - doc.clientLeft
31 w: rect.width ? (rect.right - rect.left)
32 h: rect.height ? (rect.top - rect.bottom)
35 # figure out the top/left coordinates of where the cursor should be if it's at
36 # position ``i`` within text node ``n``
38 # implementation: insert a span tag where we want the cursor, and ask the
39 # browser where it put that span
40 cursor_to_loc = (n, i) ->
41 span = domify span: style: "height: 1em; border-left: 1px solid black; margin-left: -1px"
42 parent = n.el.parentNode
47 bounds = get_el_offset n.el
51 if i is 0 # cursor at start of text
53 txts.push n.text.substr 0, 1
54 txts.push n.text.substr 1
55 else if i is n.text.length # cursor at end of text
58 txts.push n.text.substr 0, n.text.length - 1
59 txts.push n.text.substr n.text.length - 1
62 txts.push n.text.substr 0, i
63 txts.push n.text.substr i, 1
64 txts.push n.text.substr i + 1
65 for txt, txt_i in txts
66 el = n.el.ownerDocument.createTextNode txt
68 span = n.el.ownerDocument.createElement 'span'
72 parent.insertBefore el, n.el
73 parent.removeChild n.el
74 bounds = get_el_bounds els[check_i]
75 parent.insertBefore n.el, els[0]
78 ret = top: bounds.y, left: bounds.x
81 # fudge case where bounds are BS because we're on non-significant whitespace
83 first = cursor_to_loc n, 0
84 if ret.left <= first.left
85 # no need for a loop here, because recursion
86 ret = cursor_to_loc n, i - 1
90 #loc_to_cursor = (n, i) ->
93 # encode text so it can be safely placed inside an html attribute
94 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
96 return txt.replace enc_attr_regex, (match, amp, quote) ->
97 return '&' if (amp)
98 return '"' if (quote)
118 dom_to_html = (dom) ->
130 if el.attrs[k].length > 0
131 ret += "=\"#{enc_attr el.attrs[k]}\""
133 unless void_elements[el.name]
134 if el.children.length
135 ret += dom_to_html el.children
136 ret += "</#{el.name}>"
140 ret += "<!--#{el.text}-->"
142 ret += "<!DOCTYPE #{el.name}"
143 if el.public_identifier? and el.public_identifier.length > 0
144 ret += " \"#{el.public_identifier}\""
145 if el.system_identifier? and el.system_identifier.length > 0
146 ret += " \"#{el.system_identifier}\""
153 return document.createTextNode attrs
154 el = document.createElement tag
164 css += 'div#peach_html5_editor_cursor {'
165 css += 'position: absolute;'
166 css += 'height: 1em;'
168 css += 'margin-left: -1px;'
169 css += 'margin-right: -1px;'
170 css += 'background: #000;'
171 css += '-webkit-animation: 1s blink step-end infinite;'
172 css += 'animation: 1s blink step-end infinite;'
174 css += '@-webkit-keyframes "blink" {'
175 css += 'from, to { background: #000; }'
176 css += '50% { background: transparent; }'
178 css += '@keyframes "blink" {'
179 css += 'from, to { background: #000; }'
180 css += '50% { background: transparent; }'
188 KEY_BACKSPACE = 8 # <--
189 KEY_DELETE = 46 # -->
199 instantiate_tree = (tree, parent) ->
203 c.el = parent.ownerDocument.createTextNode c.text
204 parent.appendChild c.el
206 # TODO create in correct namespace
207 c.el = parent.ownerDocument.createElement c.name
209 # FIXME if attr_whitelist[k]?
210 c.el.setAttribute k, v
211 parent.appendChild c.el
213 instantiate_tree c.children, c.el
215 traverse_tree = (tree, state, cb) ->
220 traverse_tree c.children, state, cb
223 # find the next element in top (and decendants) that is after n and can contain text
224 # TODO make it so cursor can go places that don't have text but could
225 find_next_cursor_position = (top, n, i) ->
226 if n? and n.type is TYPE_TEXT and n.text.length > i
228 found = traverse_tree top, before: n?, (node, state) ->
229 if node.type is TYPE_TEXT and state.before is false
235 return [found.node, 0]
238 # TODO make it so cursor can go places that don't have text but could
239 find_prev_cursor_position = (top, n, i) ->
240 if n? and n.type is TYPE_TEXT and i > 0
242 found = traverse_tree top, before: n?, (node, state) ->
243 if node.type is TYPE_TEXT
249 state.node = state.prev
254 return [found.node, found.node.text.length]
257 class PeachHTML5Editor
258 constructor: (in_el, options = {}) ->
261 @iframe = domify iframe: class: 'peach_html5_editor'
264 @cursor_visible = false
265 opt_fragment = options.fragment ? true
268 @parser_opts.fragment = 'body'
271 @idoc = @iframe.contentDocument
275 '20': true # capslock
277 '144': true # numlock
279 '91': true # windows "start" key
280 control_key_codes = # we react to these, but they aren't typing
296 @idoc.body.onkeyup = (e) =>
298 return false if ignore_key_codes[e.keyCode]?
299 #return false if control_key_codes[e.keyCode]?
300 @idoc.body.onkeydown = (e) =>
302 return false if ignore_key_codes[e.keyCode]?
303 #return false if control_key_codes[e.keyCode]?
307 new_cursor = find_prev_cursor_position @tree, @cursor...
309 @move_cursor new_cursor
312 new_cursor = find_next_cursor_position @tree, c, -1
314 @move_cursor new_cursor
321 new_cursor = find_next_cursor_position @tree, @cursor...
323 @move_cursor new_cursor
326 new_cursor = find_prev_cursor_position @tree, c, -1
328 @move_cursor new_cursor
353 @idoc.body.onkeypress = (e) =>
355 return false if ignore_key_codes[e.keyCode]?
356 return false if control_key_codes[e.keyCode]? # handled in keydown
357 char = e.charCode ? e.keyCode
359 char = String.fromCharCode char
361 @cursor[0].text = char + @cursor[0].text
362 else if @cursor[1] is @cursor[0].text.length - 1
363 @cursor[0].text += char
366 @cursor[0].text.substr(0, @cursor[1]) +
368 @cursor[0].text.substr(@cursor[1])
369 @cursor[0].el.nodeValue = @cursor[0].text
370 @move_cursor [@cursor[0], @cursor[1] + 1]
372 if options.stylesheet # TODO test this
373 istyle = @idoc.createElement 'style'
374 istyle.setAttribute 'src', options.stylesheet
375 @idoc.head.appendChild istyle
376 icss = @idoc.createElement 'style'
377 icss.appendChild @idoc.createTextNode css
378 @idoc.head.appendChild icss
379 @load_html @in_el.value
381 @in_el.parentNode.appendChild @iframe
383 # FIXME add parent node, so we don't empty body and delete cursor_el
384 while @idoc.body.childNodes.length
385 @idoc.body.removeChild @idoc.body.childNodes[0]
386 @cursor_visible = false
389 @tree = peach_parser.parse html, @parser_opts
390 #as_html = dom_to_html @tree
391 #@iframe.contentDocument.body.innerHTML = as_html
393 instantiate_tree @tree, @idoc.body
394 move_cursor: (cursor) ->
395 return if @cursor? and cursor? and @cursor[0] is cursor[0] and @cursor[1] is cursor[1]
397 # replace cursor, to reset blink animation
399 @cursor_el.parentNode.removeChild @cursor_el
400 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
401 @idoc.body.appendChild @cursor_el
402 @cursor_visible = true
403 # TODO figure out top/left coords for cursor
404 loc = cursor_to_loc cursor[0], cursor[1]
405 @cursor_el.style.top = "#{loc.top}px"
406 @cursor_el.style.left = "#{loc.left}px"
408 window.peach_html5_editor = (args...) ->
409 return new PeachHTML5Editor args...
411 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])