Skip to content

Commit

Permalink
Show innerHTML (with | between text nodes) of editor in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 29, 2015
1 parent 3900924 commit 5e25491
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
42 changes: 36 additions & 6 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,39 @@ var ContentKitDemo = exports.ContentKitDemo = {
$('#rendered-mobiledoc').empty();
$('#rendered-mobiledoc')[0].appendChild(rendered);

var htmlRenderer = new MobiledocHTMLRenderer();
var html = htmlRenderer.render(mobiledoc);
var displayHTML = function(html) {
return html.replace(/&/g,'&amp;').replace(/</g, '&lt;').replace(/>/g,'&gt;');
};

// adds a pipe ("|") between adjacent text nodes for visual debugging
var debugNodeHTML = function(node) {
function convertTextNodes(parentNode, converterFn) {
var iterator = document.createNodeIterator(parentNode, NodeFilter.SHOW_TEXT);
var node = iterator.nextNode();
while (node) {
converterFn(node);
node = iterator.nextNode();
}
}

function addPipeBetweenAdjacentTextNodes(textNode) {
var nextSibling = textNode.nextSibling;
if (nextSibling && nextSibling.nodeType === Node.TEXT_NODE) {
textNode.textContent = textNode.textContent + '|';
}
}

var deep = true;
var cloned = node.cloneNode(deep);
convertTextNodes(cloned, addPipeBetweenAdjacentTextNodes);
return displayHTML(cloned.innerHTML);
};

html = html.replace(/&/g,'&amp;').replace(/</g, '&lt;').replace(/>/g,'&gt;');
var htmlRenderer = new MobiledocHTMLRenderer();
$('#rendered-mobiledoc-html').html(displayHTML(htmlRenderer.render(mobiledoc)));

$('#rendered-mobiledoc-html').html(html);
var editorHTML = debugNodeHTML($('#editor')[0]);
$('#editor-html').html(editorHTML);
},

syntaxHighlight: function(json) {
Expand Down Expand Up @@ -230,9 +257,12 @@ function bootEditor(element, mobiledoc) {
cards: [simpleCard, cardWithEditMode, cardWithInput, selfieCard]
});

editor.on('update', function() {
function sync() {
ContentKitDemo.syncCodePane(editor);
});
}

editor.on('update', sync);
sync();
}

function readMobiledoc(string) {
Expand Down
5 changes: 5 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ <h2>rendered mobiledoc (dom)</h2>
on the serialized mobiledoc.
</p>
<div id="rendered-mobiledoc"></div>

<hr>

<h2>innerHTML of editor surface</h2>
<div id='editor-html'></div>
</div>

<div class="pane">
Expand Down

0 comments on commit 5e25491

Please sign in to comment.