diff --git a/elements/hax-body/hax-body.js b/elements/hax-body/hax-body.js index 789796b39d..7d7466b38b 100755 --- a/elements/hax-body/hax-body.js +++ b/elements/hax-body/hax-body.js @@ -2476,8 +2476,68 @@ class HaxBody extends I18NMixin(UndoManagerBehaviors(SimpleColors)) { // If hax-body isn't the next parent if(currentNode.parentNode && currentNode.parentNode !== bodyNode){ - unwrap(currentNode.parentNode) - return currentNode.parentNode; + const parentNode = currentNode.parentNode; + const grandparentNode = currentNode.parentNode.parentNode; + + if(currentNode.previousElementSibling){ + const prevParent = parentNode.previousElementSibling; + // If the previous parent container is a matching list type, append to it in order + if(prevParent && + prevParent.tagName === parentNode.tagName) { + let prevLI = parentNode.firstChild; + while(prevLI !== currentNode){ + const isPrev = prevLI.nextElementSibling; + prevParent.appendChild(parentNode.firstChild); + prevLI = isPrev; + } + } else { + const beforeNodes = parentNode.cloneNode(false); + let prevLI = currentNode.previousElementSibling; + while(prevLI){ + beforeNodes.prepend(prevLI) + const isPrev = prevLI.previousElementSibling; + prevLI = isPrev; + } + beforeNodes.removeAttribute("data-hax-active"); + grandparentNode.insertBefore(beforeNodes, parentNode); + } + } + + if(currentNode.nextElementSibling){ + const nextParent = parentNode.nextElementSibling; + // If the next parent container is a matching list type, prepend to it in order + if(nextParent && + nextParent.tagName === parentNode.tagName) { + let nextLI = parentNode.lastChild; + while(nextLI !== currentNode){ + const isNext = nextLI.previousElementSibling; + nextParent.prepend(nextLI); + nextLI = isNext; + } + } else { + const afterNodes = parentNode.cloneNode(false); + let nextLI = currentNode.nextElementSibling; + while(nextLI){ + const isNext = nextLI.nextElementSibling; + afterNodes.appendChild(nextLI) + nextLI = isNext; + } + afterNodes.removeAttribute("data-hax-active"); + // If no nextSibling, nextParent is null and insertBefore acts like appendChild + grandparentNode.insertBefore(afterNodes, nextParent); + } + } + // Unwrap original UL/OL + unwrap(parentNode); + + // If we're outdenting into a paragraph, the LI tag shouldn't be preserved + if(grandparentNode.tagName.toLowerCase() !== "ol" || grandparentNode.tagName.toLowerCase() !== "ul" ){ + const strippedLI = globalThis.document.createElement("span"); + strippedLI.innerHTML = currentNode.innerHTML.trim() + "
" + currentNode.replaceWith(strippedLI) + }; + + return grandparentNode; } else { return this.haxChangeTagName(node, "p", true) } @@ -2497,12 +2557,11 @@ class HaxBody extends I18NMixin(UndoManagerBehaviors(SimpleColors)) { ) ) { replacement.innerHTML = - "
  • " + node.innerHTML .trim() - .replace(//g, "
  • \n
  • ") - .replace(/
    /g, "
  • \n
  • ") + - "
  • "; + .replace(//g, "
  • ") + .replace(/
    <\/span>/g, "
  • \n") + .replace(/<\/span>/g, "\n"); } // when converting to list, ensure slot is on the list, not the items if (originalSlot) { @@ -2521,12 +2580,12 @@ class HaxBody extends I18NMixin(UndoManagerBehaviors(SimpleColors)) { node.tagName.toLowerCase() == "ol" ) { // if we're coming from ul or ol strip out the li tags - replacement.innerHTML = replacement.innerHTML - .replace(/