X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=faa2e3f25462bb44211107f980e8a9b88de31dd9;hb=0f0011fe19f527abc6d67c7245720d5d451085f5;hp=0f68f40e4f486b8d9ea72be1f76bfebc95bf4cbf;hpb=a85e7976ce04ccbceb207ffe60ffbd212eb5a789;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index 0f68f40..faa2e3f 100644 --- a/editor.coffee +++ b/editor.coffee @@ -43,7 +43,7 @@ dom_to_html = (dom) -> ret = '' for el in dom switch el.type - when wheic_parser.TYPE_TAG + when peach_parser.TYPE_TAG ret += '<' + el.name attr_keys = [] for k of el.attrs @@ -58,11 +58,11 @@ dom_to_html = (dom) -> if el.children.length ret += dom_to_html el.children ret += "" - when wheic_parser.TYPE_TEXT + when peach_parser.TYPE_TEXT ret += el.text - when wheic_parser.TYPE_COMMENT + when peach_parser.TYPE_COMMENT ret += "" - when wheic_parser.TYPE_DOCTYPE + when peach_parser.TYPE_DOCTYPE ret += " 0 ret += " \"#{el.public_identifier}\"" @@ -85,7 +85,7 @@ domify = (h) -> return el css = '' -css += 'span.peach_editor_cursor {' +css += 'div#peach_editor_cursor {' css += 'display: inline-block;' css += 'height: 1em;' css += 'width: 2px;' @@ -96,14 +96,30 @@ css += '-webkit-animation: 1s blink step-end infinite;' css += 'animation: 1s blink step-end infinite;' css += '}' css += '@-webkit-keyframes "blink" {' -css += 'from, to { background: transparent; }' -css += '50% { background: #000; }' +css += 'from, to { background: #000; }' +css += '50% { background: transparent; }' css += '}' css += '@keyframes "blink" {' -css += 'from, to { background: transparent; }' -css += '50% { background: #000; }' +css += 'from, to { background: #000; }' +css += '50% { background: transparent; }' css += '}' +# key codes: +KEY_LEFT = 37 +KEY_UP = 38 +KEY_RIGHT = 39 +KEY_DOWN = 40 +KEY_BACKSPACE = 8 # <-- +KEY_DELETE = 46 # --> +KEY_END = 35 +KEY_ENTER = 13 +KEY_ESCAPE = 27 +KEY_HOME = 36 +KEY_INSERT = 45 +KEY_PAGE_UP = 33 +KEY_PAGE_DOWN = 34 +KEY_TAB = 9 + wysiwyg = (el, options = {}) -> opt_fragment = options.fragment ? true parser_opts = {} @@ -113,15 +129,49 @@ wysiwyg = (el, options = {}) -> dom: [] iframe: document.createElement('iframe') load_html: (html) -> - @dom = wheic_parser.parse html, parser_opts - as_html = wheic.dom_to_html @dom + @dom = peach_parser.parse html, parser_opts + as_html = peach.dom_to_html @dom as_html = as_html.substr(0, 5) + '' + as_html.substr(5) @iframe.contentDocument.body.innerHTML = as_html } el.parentNode.appendChild editor_instance.iframe idoc = editor_instance.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 + + idoc.body.onkeyup = (e) -> + return false if ignore_key_codes[e.keyCode]? + return false if control_key_codes[e.keyCode]? + idoc.body.onkeydown = (e) -> + return false if ignore_key_codes[e.keyCode]? + return false if control_key_codes[e.keyCode]? idoc.body.onkeypress = (e) -> - char = e.charCode ? e.keyCode ? e.which + return if e.ctrlKey + return false if ignore_key_codes[e.keyCode]? + # in firefox, keyCode is only set for non-typing keys + if e.keyCode isnt KEY_BACKSPACE # so this is fine + return false if control_key_codes[e.keyCode]? + char = e.charCode ? e.keyCode el.value += String.fromCharCode char editor_instance.load_html el.value return false @@ -135,9 +185,9 @@ wysiwyg = (el, options = {}) -> editor_instance.load_html el.value return editor_instance -window.wheic = { +window.peach = { wysiwyg: wysiwyg dom_to_html: dom_to_html } -# test in browser: wheic(document.getElementsByTagName('textarea')[0]) +# test in browser: peach.wysiwyg(document.getElementsByTagName('textarea')[0])