-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring in DOMRenderer from external dep mobiledoc-dom-renderer
- Loading branch information
Showing
3 changed files
with
5 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |