diff --git a/public/js/text.js b/public/js/text.js index eafc1c6..8f7b72e 100644 --- a/public/js/text.js +++ b/public/js/text.js @@ -8,16 +8,23 @@ document.addEventListener('DOMContentLoaded', () => { h1.textContent = text.name; document.getElementById('text-detail').appendChild(h1); - return fetch('/api/nodes/' + textId ); - }) - .then(res => res.json()) - .then(nodes => { - const tree = buildTree(nodes); - const ul = renderTree(tree); - document.getElementById('text-detail').appendChild(ul); + return fetchAndRenderNodes(textId); }); }); +function fetchAndRenderNodes(textId) { + return fetch('/api/nodes/' + textId) + .then(res => res.json()) + .then(nodes => { + const existing = document.querySelector('#text-detail > ul'); + if (existing) existing.remove(); + + const tree = buildTree(nodes); + const ul = renderTree(tree, textId); + document.getElementById('text-detail').appendChild(ul); + }); +} + function buildTree(nodes) { const map = {}; nodes.forEach(node => {