Skip to content

Commit

Permalink
Updated exports to match docs
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshort committed Dec 28, 2015
1 parent 8f077ef commit 5257210
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 90 deletions.
36 changes: 36 additions & 0 deletions src/dom/createRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import createElement from './createElement'
import {diffNode} from '../diff'
import patch from './patch'
import uid from 'uid'

/**
* Create a DOM renderer using a container element. Everything will be rendered
* inside of that container. Returns a function that accepts new state that can
* replace what is currently rendered.
*/

export default function createDOMRenderer (container, dispatch) {
let oldVnode = null
let node = null
let path = uid()

let update = (newVnode, context) => {
let changes = diffNode(oldVnode, newVnode, path)
node = changes.reduce(patch(dispatch, context), node)
oldVnode = newVnode
return node
}

let create = (vnode, context) => {
node = createElement(vnode, path, dispatch, context)
if (container) container.appendChild(node)
oldVnode = vnode
return node
}

return (vnode, context = {}) => {
return node !== null
? update(vnode, context)
: create(vnode, context)
}
}
37 changes: 5 additions & 32 deletions src/dom/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
import createRenderer from './createRenderer'
import createElement from './createElement'
import {diffNode} from '../diff'
import patch from './patch'
import uid from 'uid'

/**
* Create a DOM renderer using a container element. Everything will be rendered
* inside of that container. Returns a function that accepts new state that can
* replace what is currently rendered.
*/

export default function createDOMRenderer (container, dispatch) {
let oldVnode = null
let node = null
let path = uid()

let update = (newVnode, context) => {
let changes = diffNode(oldVnode, newVnode, path)
node = changes.reduce(patch(dispatch, context), node)
oldVnode = newVnode
return node
}

let create = (vnode, context) => {
node = createElement(vnode, path, dispatch, context)
if (container) container.appendChild(node)
oldVnode = vnode
return node
}

return (vnode, context = {}) => {
return node !== null
? update(vnode, context)
: create(vnode, context)
}
export default {
createRenderer,
createElement,
patch
}
58 changes: 3 additions & 55 deletions src/string/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@
import {isText, isValidAttribute, isThunk} from '../shared/utils'
import renderString from './renderString'

/**
* Turn an object of key/value pairs into a HTML attribute string. This
* function is responsible for what attributes are allowed to be rendered and
* should handle any other special cases specific to deku.
*/

function attributesToString (attributes) {
var str = ''
for (var name in attributes) {
let value = attributes[name]
if (name === 'innerHTML') continue
if (isValidAttribute(value)) str += (' ' + name + '="' + attributes[name] + '"')
}
return str
}

/**
* Render a virtual element to a string. You can pass in an option state context
* object that will be given to all components.
*/

export default function renderString (element, context, path = '0') {
if (isText(element)) {
return element.nodeValue
}

if (isThunk(element)) {
let { props, data, children } = element
let { render } = data
let output = render({
children,
props,
path,
context
})
return renderString(
output,
context,
path
)
}

let {attributes, type, children} = element
let innerHTML = attributes.innerHTML
let str = '<' + type + attributesToString(attributes) + '>'

if (innerHTML) {
str += innerHTML
} else {
str += children.map((child, i) => renderString(child, context, path + '.' + (child.key == null ? i : child.key))).join('')
}

str += '</' + type + '>'
return str
export default {
renderString
}
57 changes: 57 additions & 0 deletions src/string/renderString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {isText, isValidAttribute, isThunk} from '../shared/utils'

/**
* Turn an object of key/value pairs into a HTML attribute string. This
* function is responsible for what attributes are allowed to be rendered and
* should handle any other special cases specific to deku.
*/

function attributesToString (attributes) {
var str = ''
for (var name in attributes) {
let value = attributes[name]
if (name === 'innerHTML') continue
if (isValidAttribute(value)) str += (' ' + name + '="' + attributes[name] + '"')
}
return str
}

/**
* Render a virtual element to a string. You can pass in an option state context
* object that will be given to all components.
*/

export default function renderString (element, context, path = '0') {
if (isText(element)) {
return element.nodeValue
}

if (isThunk(element)) {
let { props, data, children } = element
let { render } = data
let output = render({
children,
props,
path,
context
})
return renderString(
output,
context,
path
)
}

let {attributes, type, children} = element
let innerHTML = attributes.innerHTML
let str = '<' + type + attributesToString(attributes) + '>'

if (innerHTML) {
str += innerHTML
} else {
str += children.map((child, i) => renderString(child, context, path + '.' + (child.key == null ? i : child.key))).join('')
}

str += '</' + type + '>'
return str
}
2 changes: 1 addition & 1 deletion test/createDOMRenderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @jsx h */
import test from 'tape'
import createDOMRenderer from '../src/dom'
import createDOMRenderer from '../src/dom/createRenderer'
import h from '../src/element'

test('rendering elements', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/renderString.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @jsx h */
import test from 'tape'
import render from '../src/string'
import render from '../src/string/renderString'
import h from '../src/element'

test('render to a string', t => {
Expand Down
2 changes: 1 addition & 1 deletion test/thunk.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @jsx h */
import createDOMRenderer from '../src/dom'
import createDOMRenderer from '../src/dom/createRenderer'
import h from '../src/element'
import test from 'tape'
import trigger from 'trigger-event'
Expand Down

0 comments on commit 5257210

Please sign in to comment.