diff --git a/app/assets/javascripts/02-editor.js b/app/assets/javascripts/02-editor.js index a6e7254..c367679 100644 --- a/app/assets/javascripts/02-editor.js +++ b/app/assets/javascripts/02-editor.js @@ -224,7 +224,7 @@ function hasContent (element) { function cleanContent (contentEl) { const sel = window.getSelection() - cleanNode(contentEl) + cleanNode(contentEl, contentEl) for (const child of contentEl.childNodes) { if (child.tagName) { @@ -258,6 +258,7 @@ function fixContent (contentEl) { for (const item of child.childNodes) { if (item.tagName !== "LI") notItems.push(item) } + if (notItems.length) { const item = document.createElement("li") item.append(...notItems) @@ -274,7 +275,7 @@ function fixContent (contentEl) { * * Junta nodos de la misma "mark" que están al lado * * Borra elementos sin contenido (ver `hasContent`) y no están seleccionados */ -function cleanNode (node) { +function cleanNode (node, contentEl) { for (const child of node.childNodes) { if (child.nodeType === Node.TEXT_NODE) { if (child.nextSibling && child.nextSibling.nodeType === Node.TEXT_NODE) { @@ -292,8 +293,26 @@ function cleanNode (node) { child.nextSibling.parentNode.removeChild(child.nextSibling) } } + + if (child.tagName === "LI") { + let parentEl = child + while ( + parentEl + && !(parentEl.nodeType == Node.ELEMENT_NODE && elementIsBlock(parentEl)) + && contentEl.contains(parentEl) + ) + parentEl = parentEl.parentElement + + if ( + parentEl + && contentEl.contains(parentEl) + && parentEl.tagName !== "UL" + && parentEl.tagName !== "OL" + ) + moveChildren(child, parentEl, child.nextSibling) + } } - cleanNode(child) + cleanNode(child, contentEl) } } @@ -316,8 +335,6 @@ function setupEditor (editorEl) { if (parentEl == contentEl) parentEl = parentEl.firstChild - console.log(parentEl) - let newEl switch (parentEl.tagName) { case "UL":