X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=defecba6f56c8635a98f4da7826a6edc7fbb456e;hb=33e750fec2426e7ab0f7f02d2d1cf76a3bf7091b;hp=5ec9434ae6df91f5b2477f07b2db9307eb142962;hpb=6eab44262a16e431ec8dbcb42820babc5d28fc10;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index 5ec9434..defecba 100644 --- a/editor.coffee +++ b/editor.coffee @@ -34,7 +34,7 @@ get_el_offset = (el) -> # implementation: insert a span tag where we want the cursor, and ask the # browser where it put that span cursor_to_loc = (n, i) -> - span = domify span: style: "height: 1em; width: 0", children: [domify text: "|"] + span = domify span: style: "height: 1em; border-left: 1px solid black; margin-left: -1px" parent = n.el.parentNode if i is 0 # cursor at start of text @@ -235,7 +235,7 @@ class PeachHTML5Editor constructor: (in_el, options = {}) -> @in_el = in_el @tree = [] - @iframe = document.createElement('iframe') + @iframe = domify iframe: class: 'peach_html5_editor' @cursor = null @cursor_el = null @cursor_visible = false @@ -243,116 +243,119 @@ class PeachHTML5Editor @parser_opts = {} if opt_fragment @parser_opts.fragment = 'body' - @in_el.parentNode.appendChild @iframe - @idoc = @iframe.contentDocument - ignore_key_codes = - '18': true # alt - '20': true # capslock - '17': true # ctrl - '144': true # numlock - '16': true # shift - '91': true # windows "start" key - control_key_codes = # we react to these, but they aren't typing - '37': KEY_LEFT - '38': KEY_UP - '39': KEY_RIGHT - '40': KEY_DOWN - '35': KEY_END - '8': KEY_BACKSPACE - '46': KEY_DELETE - '13': KEY_ENTER - '27': KEY_ESCAPE - '36': KEY_HOME - '45': KEY_INSERT - '33': KEY_PAGE_UP - '34': KEY_PAGE_DOWN - '9': KEY_TAB + @iframe.onload = => + @idoc = @iframe.contentDocument - @idoc.body.onkeyup = (e) => - return if e.ctrlKey - return false if ignore_key_codes[e.keyCode]? - #return false if control_key_codes[e.keyCode]? - @idoc.body.onkeydown = (e) => - return if e.ctrlKey - return false if ignore_key_codes[e.keyCode]? - #return false if control_key_codes[e.keyCode]? - switch e.keyCode - when KEY_LEFT - if @cursor? - new_cursor = find_prev_cursor_position @tree, @cursor... - if new_cursor? - @move_cursor new_cursor - else - for c in @tree - new_cursor = find_next_cursor_position @tree, c, -1 + ignore_key_codes = + '18': true # alt + '20': true # capslock + '17': true # ctrl + '144': true # numlock + '16': true # shift + '91': true # windows "start" key + control_key_codes = # we react to these, but they aren't typing + '37': KEY_LEFT + '38': KEY_UP + '39': KEY_RIGHT + '40': KEY_DOWN + '35': KEY_END + '8': KEY_BACKSPACE + '46': KEY_DELETE + '13': KEY_ENTER + '27': KEY_ESCAPE + '36': KEY_HOME + '45': KEY_INSERT + '33': KEY_PAGE_UP + '34': KEY_PAGE_DOWN + '9': KEY_TAB + + @idoc.body.onkeyup = (e) => + return if e.ctrlKey + return false if ignore_key_codes[e.keyCode]? + #return false if control_key_codes[e.keyCode]? + @idoc.body.onkeydown = (e) => + return if e.ctrlKey + return false if ignore_key_codes[e.keyCode]? + #return false if control_key_codes[e.keyCode]? + switch e.keyCode + when KEY_LEFT + if @cursor? + new_cursor = find_prev_cursor_position @tree, @cursor... if new_cursor? @move_cursor new_cursor - break - return false - when KEY_UP - return false - when KEY_RIGHT - if @cursor? - new_cursor = find_next_cursor_position @tree, @cursor... - if new_cursor? - @move_cursor new_cursor - else - for c in @tree - new_cursor = find_prev_cursor_position @tree, c, -1 + else + for c in @tree + new_cursor = find_next_cursor_position @tree, c, -1 + if new_cursor? + @move_cursor new_cursor + break + return false + when KEY_UP + return false + when KEY_RIGHT + if @cursor? + new_cursor = find_next_cursor_position @tree, @cursor... if new_cursor? @move_cursor new_cursor - break - return false - when KEY_DOWN - return false - when KEY_END - return false - when KEY_BACKSPACE - return false - when KEY_DELETE - return false - when KEY_ENTER - return false - when KEY_ESCAPE - return false - when KEY_HOME - return false - when KEY_INSERT - return false - when KEY_PAGE_UP - return false - when KEY_PAGE_DOWN - return false - when KEY_TAB - return false - @idoc.body.onkeypress = (e) => - return if e.ctrlKey - return false if ignore_key_codes[e.keyCode]? - return false if control_key_codes[e.keyCode]? # handled in keydown - char = e.charCode ? e.keyCode - if char and @cursor? - char = String.fromCharCode char - if @cursor[1] is 0 - @cursor[0].text = char + @cursor[0].text - else if @cursor[1] is @cursor[0].text.length - 1 - @cursor[0].text += char - else - @cursor[0].text = - @cursor[0].text.substr(0, @cursor[1]) + - char + - @cursor[0].text.substr(@cursor[1]) - @cursor[0].el.nodeValue = @cursor[0].text - @move_cursor [@cursor[0], @cursor[1] + 1] - return false - if options.stylesheet # TODO test this - istyle = @idoc.createElement 'style' - istyle.setAttribute 'src', options.stylesheet - @idoc.head.appendChild istyle - icss = @idoc.createElement 'style' - icss.appendChild @idoc.createTextNode css - @idoc.head.appendChild icss - @load_html @in_el.value + else + for c in @tree + new_cursor = find_prev_cursor_position @tree, c, -1 + if new_cursor? + @move_cursor new_cursor + break + return false + when KEY_DOWN + return false + when KEY_END + return false + when KEY_BACKSPACE + return false + when KEY_DELETE + return false + when KEY_ENTER + return false + when KEY_ESCAPE + return false + when KEY_HOME + return false + when KEY_INSERT + return false + when KEY_PAGE_UP + return false + when KEY_PAGE_DOWN + return false + when KEY_TAB + return false + @idoc.body.onkeypress = (e) => + return if e.ctrlKey + return false if ignore_key_codes[e.keyCode]? + return false if control_key_codes[e.keyCode]? # handled in keydown + char = e.charCode ? e.keyCode + if char and @cursor? + char = String.fromCharCode char + if @cursor[1] is 0 + @cursor[0].text = char + @cursor[0].text + else if @cursor[1] is @cursor[0].text.length - 1 + @cursor[0].text += char + else + @cursor[0].text = + @cursor[0].text.substr(0, @cursor[1]) + + char + + @cursor[0].text.substr(@cursor[1]) + @cursor[0].el.nodeValue = @cursor[0].text + @move_cursor [@cursor[0], @cursor[1] + 1] + return false + if options.stylesheet # TODO test this + istyle = @idoc.createElement 'style' + istyle.setAttribute 'src', options.stylesheet + @idoc.head.appendChild istyle + icss = @idoc.createElement 'style' + icss.appendChild @idoc.createTextNode css + @idoc.head.appendChild icss + @load_html @in_el.value + + @in_el.parentNode.appendChild @iframe clear_dom: -> # FIXME add parent node, so we don't empty body and delete cursor_el while @idoc.body.childNodes.length