From a85e7976ce04ccbceb207ffe60ffbd212eb5a789 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Fri, 29 Jan 2016 18:31:37 -0500 Subject: [PATCH] start on cursor and typing --- editor.coffee | 51 +++++++++++++++++++++++++++++++++++++++++++--- editor_tests.coffee | 10 +++++---- editor_tests_coffee.html | 2 -- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/editor.coffee b/editor.coffee index 4805550..0f68f40 100644 --- a/editor.coffee +++ b/editor.coffee @@ -71,23 +71,68 @@ dom_to_html = (dom) -> ret += ">\n" return ret +domify = (h) -> + for tag, attrs of h + if tag is 'text' + return document.createTextNode attrs + el = document.createElement tag + for k, v of attrs + if k is 'children' + for child in v + el.appendChild child + else + el.setAttribute k, v + return el + +css = '' +css += 'span.peach_editor_cursor {' +css += 'display: inline-block;' +css += 'height: 1em;' +css += 'width: 2px;' +css += 'margin-left: -1px;' +css += 'margin-right: -1px;' +css += 'background: #000;' +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 += '}' +css += '@keyframes "blink" {' +css += 'from, to { background: transparent; }' +css += '50% { background: #000; }' +css += '}' + wysiwyg = (el, options = {}) -> opt_fragment = options.fragment ? true parser_opts = {} if opt_fragment parser_opts.fragment = 'body' editor_instance = { - dom: wheic_parser.parse el.value, parser_opts + dom: [] iframe: document.createElement('iframe') load_html: (html) -> - @dom = wheic_parser.parse el.value, parser_opts + @dom = wheic_parser.parse html, parser_opts + as_html = wheic.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 + idoc.body.onkeypress = (e) -> + char = e.charCode ? e.keyCode ? e.which + el.value += String.fromCharCode char + editor_instance.load_html el.value + return false if options.stylesheet # TODO test this istyle = idoc.createElement 'style' istyle.setAttribute 'src', options.stylesheet idoc.head.appendChild istyle - el.parentNode.appendChild editor_instance.iframe + icss = idoc.createElement 'style' + icss.appendChild idoc.createTextNode css + idoc.head.appendChild icss + editor_instance.load_html el.value return editor_instance window.wheic = { diff --git a/editor_tests.coffee b/editor_tests.coffee index 5777244..06dfd40 100644 --- a/editor_tests.coffee +++ b/editor_tests.coffee @@ -1,13 +1,15 @@ in_el = document.getElementById 'in' -out_el = document.getElementById 'out' button = document.getElementById 'button' editor = null button.onclick = in_el.onchange = in_el.onkeyup = -> + #button.value="Reload" if editor? - editor.load_html in_el + editor.load_html in_el.value else + # first run + button.parentNode.removeChild button editor = wheic.wysiwyg in_el - editor.iframe.contentDocument.body.innerHTML = out_el.value = wheic.dom_to_html editor.dom return false -button.value="Cleanup" +button.value="Load" button.removeAttribute 'disabled' +button.click() diff --git a/editor_tests_coffee.html b/editor_tests_coffee.html index 25bb3fc..a16c0c4 100644 --- a/editor_tests_coffee.html +++ b/editor_tests_coffee.html @@ -17,8 +17,6 @@

In:

-

Cleaned Up:

-
-- 1.7.10.4