From e147c3acd6397a7ca624aabb47b48c3287e59526 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 31 Oct 2020 20:56:45 +0100 Subject: [PATCH] Add renderTree function --- list-editor/index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/list-editor/index.js b/list-editor/index.js index 11be6c5..e993863 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -176,6 +176,38 @@ function editor(root, inputData, options) { return el; } + // TODO: build an actual tree of list items + function renderTree(rootElement, rootData) { + const el = (tag, children = []) => { + let elt = document.createElement(tag) + _.each(children, item => { + elt.appendChild(item) + }) + return elt + } + + /** + * @param {Item[]} items + * @returns {*} + */ + let buildTree = (items) => { + return _.map(items, (item) => { + return el("li", [ + el("div", [document.createTextNode(item.text)]), + el("ul", buildTree(item.children)) + ]) + }) + } + + let tree = rootData.tree(); + $(rootElement).children().remove() + + let $list = buildTree(tree) + let $ul = $('