mirror of https://github.com/artf/grapesjs.git
3 changed files with 73 additions and 15 deletions
@ -0,0 +1,30 @@ |
|||||
|
// DOM helpers
|
||||
|
import { each } from 'underscore'; |
||||
|
|
||||
|
const KEY_TAG = 'tag'; |
||||
|
const KEY_ATTR = 'attributes'; |
||||
|
const KEY_CHILD = 'children'; |
||||
|
|
||||
|
export const empty = node => { |
||||
|
while (node.firstChild) node.removeChild(node.firstChild); |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Append an array of vNodes to an element |
||||
|
* @param {HTMLElement} node HTML element |
||||
|
* @param {Array} vNodes Array of node objects |
||||
|
*/ |
||||
|
export const appendVNodes = (node, vNodes = []) => { |
||||
|
const vNodesArr = Array.isArray(vNodes) ? vNodes : [vNodes]; |
||||
|
vNodesArr.forEach(vnode => { |
||||
|
const tag = vnode[KEY_TAG] || 'div'; |
||||
|
const attr = vnode[KEY_ATTR] || {}; |
||||
|
const el = document.createElement(tag); |
||||
|
|
||||
|
each(attr, (value, key) => { |
||||
|
el.setAttribute(key, value); |
||||
|
}); |
||||
|
|
||||
|
node.appendChild(el); |
||||
|
}); |
||||
|
}; |
||||
Loading…
Reference in new issue