Skip to content

Commit

Permalink
Bring in DOMRenderer from external dep mobiledoc-dom-renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 9, 2015
1 parent 9bf4ab0 commit a78b990
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 90 deletions.
3 changes: 2 additions & 1 deletion Brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var demo = require('./broccoli/demo');
var jquery = require('./broccoli/jquery');

var vendoredModules = [
{name: 'content-kit-utils', options: {libDirName: 'src'}}
{name: 'content-kit-utils', options: {libDirName: 'src'}},
{name: 'mobiledoc-dom-renderer'}
];
var packageName = require('./package.json').name;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"gulp-qunit": "^1.2.1",
"gulp-uglify": "^1.1.0",
"jquery": "^2.1.4",
"mobiledoc-dom-renderer": "^0.1.2",
"testem": "^0.8.4"
}
}
91 changes: 2 additions & 89 deletions src/js/runtime/renderers/mobiledoc-dom.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,3 @@
/**
* runtime DOM renderer
* renders a mobiledoc to DOM
*
* input: mobiledoc
* output: DOM
*/
import DOMRenderer from 'mobiledoc-dom-renderer';

const utils = {
createElement(tagName) {
return document.createElement(tagName);
},
appendChild(target, child) {
target.appendChild(child);
},
createTextNode(text) {
return document.createTextNode(text);
}
};

function createElementFromMarkerType([tagName, attributes]){
let element = utils.createElement(tagName);
attributes = attributes || [];

for (let i=0,l=attributes.length; i<l; i=i+2) {
let [propName, propValue] = attributes[i];
element.setAttribute(propName, propValue);
}
return element;
}

export default class DOMRenderer {
constructor() {
// FIXME Perhaps the render() should specify its target instead of
// creating one here.
this.root = utils.createElement('div');
}

/**
* @return DOMNode
*/
render(mobiledoc) {
const [markerTypes, sections] = mobiledoc;
this.markerTypes = markerTypes;

sections.forEach((section) => this.renderSection(section));

return this.root;
}

renderSection(section) {
const [type] = section;
switch (type) {
case 1:
let rendered = this.renderParagraphSection(section);
utils.appendChild(this.root, rendered);
break;
default:
throw new Error('Unimplement renderer for type ' + type);
}
}

renderParagraphSection([type, tagName, markers]) {
let element = utils.createElement(tagName);
let elements = [element];
let currentElement = element;

for (let i=0, l=markers.length; i<l; i++) {
let marker = markers[i];
let [openTypes, closeTypes, text] = marker;

for (let j=0, m=openTypes.length; j<m; j++) {
let markerType = this.markerTypes[openTypes[j]];
let openedElement = createElementFromMarkerType(markerType);
utils.appendChild(currentElement, openedElement);
elements.push(openedElement);
currentElement = openedElement;
}

utils.appendChild(currentElement, utils.createTextNode(text));

for (let j=0, m=closeTypes; j<m; j++) {
elements.pop();
currentElement = elements[elements.length - 1];
}
}

return element;
}
}
export default DOMRenderer;

0 comments on commit a78b990

Please sign in to comment.