-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #17
- Loading branch information
Showing
8 changed files
with
471 additions
and
209 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
import { | ||
createElement, | ||
appendChild | ||
} from '../utils/dom'; | ||
import { createElement } from '../utils/dom'; | ||
import RENDER_TYPE from '../utils/render-type'; | ||
|
||
const ImageCard = { | ||
export default { | ||
name: 'image', | ||
display: { | ||
setup(element, options, env, payload) { | ||
if (payload.src) { | ||
let img = createElement('img'); | ||
img.src = payload.src; | ||
appendChild(element, img); | ||
} | ||
} | ||
type: RENDER_TYPE, | ||
render({payload}) { | ||
let img = createElement('img'); | ||
img.src = payload.src; | ||
return img; | ||
} | ||
}; | ||
|
||
export default ImageCard; |
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,7 +1,10 @@ | ||
import Renderer from './renderer'; | ||
import RendererFactory from './renderer-factory'; | ||
import RENDER_TYPE from './utils/render-type'; | ||
|
||
export { RENDER_TYPE }; | ||
|
||
export function registerGlobal(window) { | ||
window.MobiledocDOMRenderer = Renderer; | ||
window.MobiledocDOMRenderer = RendererFactory; | ||
} | ||
|
||
export default Renderer; | ||
export default RendererFactory; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Renderer from './renderer'; | ||
import RENDER_TYPE from './utils/render-type'; | ||
|
||
/** | ||
* runtime DOM renderer | ||
* renders a mobiledoc to DOM | ||
* | ||
* input: mobiledoc | ||
* output: DOM | ||
*/ | ||
|
||
function validateCards(cards) { | ||
if (!Array.isArray(cards)) { | ||
throw new Error('`cards` must be passed as an array, not an object.'); | ||
} | ||
|
||
for (let i=0; i < cards.length; i++) { | ||
let card = cards[i]; | ||
if (card.type !== RENDER_TYPE) { | ||
throw new Error(`Card "${card.name}" must be of type "${RENDER_TYPE}", is type "${card.type}"`); | ||
} | ||
if (!card.render) { | ||
throw new Error(`Card "${card.name}" must define \`render\``); | ||
} | ||
} | ||
} | ||
|
||
export default class RendererFactory { | ||
constructor({cards, atoms, cardOptions, unknownCardHandler}={}) { | ||
cards = cards || []; | ||
validateCards(cards); | ||
atoms = atoms || []; | ||
cardOptions = cardOptions || {}; | ||
|
||
this.state = { cards, atoms, cardOptions, unknownCardHandler }; | ||
} | ||
|
||
render(mobiledoc) { | ||
return new Renderer(mobiledoc, this.state).render(); | ||
} | ||
} |
Oops, something went wrong.