-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f077ef
commit 5257210
Showing
7 changed files
with
104 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
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) | ||
} | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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