UNPKG

jodit-pro

Version:

PRO Version of Jodit Editor

2 lines (1 loc) 1.69 kB
var x=Object.defineProperty;var i=(t,e)=>x(t,"name",{value:e,configurable:!0});import{Dom as c}from"jodit/esm/core/dom/dom.js";function R(t){const e=[],r=t.s.expandSelection().sel;if(!r||r.rangeCount===0||r.isCollapsed)return e;t.s.range.commonAncestorContainer.normalize();const a=t.s.range,{commonAncestorContainer:T,startContainer:l,endContainer:h,startOffset:u,endOffset:d}=a;if(l===h){if(c.isText(l))return[f(l,u,d)];if(d-u===1){const o=l.childNodes[u];return c.isText(o)?[f(o,0,o.nodeValue.length)]:[N(o)]}if(u===0&&l.childNodes.length===d)return[N(l)];for(let o=u;o<d;o++){const s=l.childNodes[o];c.isText(s)?e.push(f(s,0,s.nodeValue.length)):e.push(N(s))}return e}const p=new Set,g=t.ed.createTreeWalker(T,NodeFilter.SHOW_ALL,{acceptNode:i(o=>!a.intersectsNode(o)||c.closest(o,s=>s&&p.has(s),T)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT,"acceptNode")});let n=g.nextNode();for(;n;){if(c.isElement(n)&&m(a,n))p.add(n),e.push(N(n));else if(c.isText(n)&&m(a,n))e.push(f(n,0,n.nodeValue.length));else if(c.isText(n)){let o=0,s=n.nodeValue.length;n===l&&(o=u),n===h&&(s=d),e.push(f(n,o,s))}n=g.nextNode()}return e}i(R,"getSelectionFragments");const m=i((t,e)=>{const r=document.createRange();try{r.selectNodeContents(e)}catch{r.selectNode(e)}return t.compareBoundaryPoints(Range.START_TO_START,r)<=0&&t.compareBoundaryPoints(Range.END_TO_END,r)>=0},"fullyContains");function f(t,e,r){return{text:t.nodeValue.slice(e,r),replace:i(T=>{t.nodeValue=t.nodeValue.slice(0,e)+T+t.nodeValue.slice(r)},"replace")}}i(f,"replaceTextNode");function N(t){const e=t;return{text:e.innerHTML,replace:i(r=>{e.innerHTML=r},"replace")}}i(N,"replaceElementNode");export{R as getSelectionFragments};