function t(t,n){const o=document.getSelection();if(!o||0===o.rangeCount)return;const e=o.getRangeAt(0);function c(t){t.style.margin="0"}function r(t){const n=document.getSelection();if(!n)return;const o=document.createRange();o.selectNodeContents(t),o.collapse(!0),n.removeAllRanges(),n.addRange(o)}function i(n){for(;n&&n!==t;){if(n instanceof HTMLElement){const t=getComputedStyle(n).display;if(n.tagName.match(/^(P|DIV|H[1-6]|LI)$/)||"block"===t||"list-item"===t)return n}n=n.parentNode}return null}function u(t,n){const o=document.createElement(n);for(c(o);t.firstChild;)o.appendChild(t.firstChild);t.replaceWith(o)}if(e.collapsed){const o=i(e.startContainer),d=o?.closest("ul,ol");if(d&&d.tagName.toLowerCase()===n)return void function(t){const n=t.closest("li");if(!n)return;const o=n.parentElement,e=document.createElement("p");for(n.style.textAlign&&(e.style.textAlign=n.style.textAlign);n.firstChild;)e.appendChild(n.firstChild);o.parentNode?.insertBefore(e,o.nextSibling),n.remove(),0===o.childNodes.length&&o.remove(),r(e)}(o);if(d&&d.tagName.toLowerCase()!==n)return void u(d,n);if(!o||o===t||0===o.childNodes.length||(f=o,!f.textContent?.trim()&&!f.querySelector("img,br"))){const t=document.createElement(n),o=document.createElement("li");return o.appendChild(document.createElement("br")),t.appendChild(o),c(t),e.insertNode(t),void r(o)}const s=document.createElement(n);c(s);const l=document.createElement("li");for(o.style.textAlign&&(l.style.textAlign=o.style.textAlign);o.firstChild;)l.appendChild(o.firstChild);return s.appendChild(l),o.replaceWith(s),void r(l)}var f;const d=[],s=document.createTreeWalker(t,NodeFilter.SHOW_ELEMENT,{acceptNode:t=>{if(!(t instanceof HTMLElement))return NodeFilter.FILTER_SKIP;if(!/^(P|DIV|LI|H[1-6])$/.test(t.tagName))return NodeFilter.FILTER_SKIP;const n=document.createRange();n.selectNodeContents(t);return e.compareBoundaryPoints(Range.END_TO_START,n)<0&&e.compareBoundaryPoints(Range.START_TO_END,n)>0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT}});let l;for(;l=s.nextNode();)d.push(l);if(0===d.length){const t=i(e.startContainer);t&&d.push(t)}const m=d[0].closest("li");if(m){const t=m.closest("ul,ol");if(!t)return;return void(t.tagName.toLowerCase()===n?function(t){const n=document.createDocumentFragment();Array.from(t.querySelectorAll("li")).forEach((t=>{const o=document.createElement("p");for(t.style.textAlign&&(o.style.textAlign=t.style.textAlign);t.firstChild;)o.appendChild(t.firstChild);n.appendChild(o)})),t.replaceWith(n)}(t):u(t,n))}const a=document.createElement(n);c(a);for(const t of d){const n=document.createElement("li");for(t.style.textAlign&&(n.style.textAlign=t.style.textAlign);t.firstChild;)n.appendChild(t.firstChild);a.appendChild(n)}d[0].replaceWith(a);for(let t=1;t<d.length;t++)d[t].remove()}export{t};
