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
89 # encode text so it can be safely placed inside an html attribute
90 enc_attr_regex = new RegExp '(&)|(")|(\u00A0)', 'g'
92 return txt.replace enc_attr_regex, (match, amp, quote) ->
93 return '&' if (amp)
94 return '"' if (quote)
114 dom_to_html = (dom) ->
126 if el.attrs[k].length > 0
127 ret += "=\"#{enc_attr el.attrs[k]}\""
129 unless void_elements[el.name]
130 if el.children.length
131 ret += dom_to_html el.children
132 ret += "</#{el.name}>"
136 ret += "<!--#{el.text}-->"
138 ret += "<!DOCTYPE #{el.name}"
139 if el.public_identifier? and el.public_identifier.length > 0
140 ret += " \"#{el.public_identifier}\""
141 if el.system_identifier? and el.system_identifier.length > 0
142 ret += " \"#{el.system_identifier}\""
149 return document.createTextNode attrs
150 el = document.createElement tag
160 css += 'div#peach_html5_editor_cursor {'
161 css += 'position: absolute;'
162 css += 'height: 1em;'
164 css += 'margin-left: -1px;'
165 css += 'margin-right: -1px;'
166 css += 'background: #000;'
167 css += '-webkit-animation: 1s blink step-end infinite;'
168 css += 'animation: 1s blink step-end infinite;'
170 css += '@-webkit-keyframes "blink" {'
171 css += 'from, to { background: #000; }'
172 css += '50% { background: transparent; }'
174 css += '@keyframes "blink" {'
175 css += 'from, to { background: #000; }'
176 css += '50% { background: transparent; }'
184 KEY_BACKSPACE = 8 # <--
185 KEY_DELETE = 46 # -->
195 instantiate_tree = (tree, parent) ->
199 c.el = parent.ownerDocument.createTextNode c.text
200 parent.appendChild c.el
202 # TODO create in correct namespace
203 c.el = parent.ownerDocument.createElement c.name
205 # FIXME if attr_whitelist[k]?
206 c.el.setAttribute k, v
207 parent.appendChild c.el
209 instantiate_tree c.children, c.el
211 traverse_tree = (tree, state, cb) ->
216 traverse_tree c.children, state, cb
219 # find the next element in top (and decendants) that is after n and can contain text
220 # TODO make it so cursor can go places that don't have text but could
221 find_next_cursor_position = (tree, n, i) ->
222 if n? and n.type is TYPE_TEXT and n.text.length > i
224 found = traverse_tree tree, before: n?, (node, state) ->
225 if node.type is TYPE_TEXT and state.before is false
231 return [found.node, 0]
234 # TODO make it so cursor can go places that don't have text but could
235 find_prev_cursor_position = (tree, n, i) ->
236 if n? and n.type is TYPE_TEXT and i > 0
238 found = traverse_tree tree, before: n?, (node, state) ->
239 if node.type is TYPE_TEXT
245 state.node = state.prev
250 return [found.node, found.node.text.length]
253 find_loc_cursor_position = (tree, loc) ->
254 console.log tree, loc
256 if c.type is TYPE_TAG or c.type is TYPE_TEXT
257 # FIXME get_el_bounds doesn't work on TYPE_TEXT
258 bounds = get_el_bounds c.el
260 continue if loc.left < bounds.x
261 continue if loc.left > bounds.x + bounds.w
262 continue if loc.top < bounds.y
263 continue if loc.top > bounds.y + bounds.h
264 if c.type is TYPE_TEXT
265 # FIXME find position, don't just pass 0
269 ret = find_loc_cursor_position c.children, loc
270 console.log 'found', ret if ret?
274 class PeachHTML5Editor
275 constructor: (in_el, options = {}) ->
278 @iframe = domify iframe: class: 'peach_html5_editor'
281 @cursor_visible = false
282 opt_fragment = options.fragment ? true
285 @parser_opts.fragment = 'body'
288 @idoc = @iframe.contentDocument
292 '20': true # capslock
294 '144': true # numlock
296 '91': true # windows "start" key
297 control_key_codes = # we react to these, but they aren't typing
313 @idoc.body.onclick = (e) =>
314 # idoc.body.offset().left/top
315 new_cursor = find_loc_cursor_position @tree, left: e.pageX, top: e.pageY
317 @move_cursor new_cursor
318 @idoc.body.onkeyup = (e) =>
320 return false if ignore_key_codes[e.keyCode]?
321 #return false if control_key_codes[e.keyCode]?
322 @idoc.body.onkeydown = (e) =>
324 return false if ignore_key_codes[e.keyCode]?
325 #return false if control_key_codes[e.keyCode]?
329 new_cursor = find_prev_cursor_position @tree, @cursor...
331 @move_cursor new_cursor
334 new_cursor = find_next_cursor_position @tree, c, -1
336 @move_cursor new_cursor
343 new_cursor = find_next_cursor_position @tree, @cursor...
345 @move_cursor new_cursor
348 new_cursor = find_prev_cursor_position @tree, c, -1
350 @move_cursor new_cursor
375 @idoc.body.onkeypress = (e) =>
377 return false if ignore_key_codes[e.keyCode]?
378 return false if control_key_codes[e.keyCode]? # handled in keydown
379 char = e.charCode ? e.keyCode
381 char = String.fromCharCode char
383 @cursor[0].text = char + @cursor[0].text
384 else if @cursor[1] is @cursor[0].text.length - 1
385 @cursor[0].text += char
388 @cursor[0].text.substr(0, @cursor[1]) +
390 @cursor[0].text.substr(@cursor[1])
391 @cursor[0].el.nodeValue = @cursor[0].text
392 @move_cursor [@cursor[0], @cursor[1] + 1]
394 if options.stylesheet # TODO test this
395 istyle = @idoc.createElement 'style'
396 istyle.setAttribute 'src', options.stylesheet
397 @idoc.head.appendChild istyle
398 icss = @idoc.createElement 'style'
399 icss.appendChild @idoc.createTextNode css
400 @idoc.head.appendChild icss
401 @load_html @in_el.value
403 @in_el.parentNode.appendChild @iframe
405 # FIXME add parent node, so we don't empty body and delete cursor_el
406 while @idoc.body.childNodes.length
407 @idoc.body.removeChild @idoc.body.childNodes[0]
408 @cursor_visible = false
411 @tree = peach_parser.parse html, @parser_opts
412 #as_html = dom_to_html @tree
413 #@iframe.contentDocument.body.innerHTML = as_html
415 instantiate_tree @tree, @idoc.body
416 move_cursor: (cursor) ->
417 return if @cursor? and cursor? and @cursor[0] is cursor[0] and @cursor[1] is cursor[1]
419 # replace cursor, to reset blink animation
421 @cursor_el.parentNode.removeChild @cursor_el
422 @cursor_el = domify div: id: 'peach_html5_editor_cursor'
423 @idoc.body.appendChild @cursor_el
424 @cursor_visible = true
425 # TODO figure out top/left coords for cursor
426 loc = cursor_to_loc cursor[0], cursor[1]
427 @cursor_el.style.top = "#{loc.top}px"
428 @cursor_el.style.left = "#{loc.left}px"
430 window.peach_html5_editor = (args...) ->
431 return new PeachHTML5Editor args...
433 # test in browser: peach_html5_editor(document.getElementsByTagName('textarea')[0])