UNPKG

jodit-pro

Version:

PRO Version of Jodit Editor

2 lines (1 loc) 1.64 kB
var w=Object.defineProperty;var m=(n,s)=>w(n,"name",{value:s,configurable:!0});import{throwIfAborted as h}from"../../core/abort-utils.js";import{requireString as p}from"../../core/arg-utils.js";function d(n,s,o,c,u,a){const l=n.editor.querySelectorAll(u);if(l.length===0)return{replacements:0,message:`No elements found matching selector "${u}"`};let e=0;return l.forEach(t=>{h(a);let r=t.innerHTML;const f=r;if(c)try{const i=new RegExp(s,"g");r=r.replace(i,()=>(e++,o))}catch(i){throw new Error(`Invalid regex pattern: ${i?.message||"Unknown error"}`)}else{const i=s.length;let g=0;for(;(g=r.indexOf(s,g))!==-1;)h(a),r=r.substring(0,g)+o+r.substring(g+i),g+=o.length,e++}r!==f&&(t.innerHTML=r)}),e>0&&(n.synchronizeValues(),n.s.restore()),{replacements:e}}m(d,"replaceInSelectedElements");function v(n,s,o,c,u,a){let l=0,e=s;if(u)try{const t=new RegExp(o,"g");e=e.replace(t,()=>(l++,c))}catch(t){throw new Error(`Invalid regex pattern: ${t?.message||"Unknown error"}`)}else{const t=o.length;let r=0;for(;(r=e.indexOf(o,r))!==-1;)h(a),e=e.substring(0,r)+c+e.substring(r+t),r+=c.length,l++}return l>0&&(n.value=e,n.s.restore(),n.synchronizeValues()),l}m(v,"replaceInDocument");async function b(n,s,o){const c=p(s,"search"),u=p(s,"replace",{allowEmpty:!0}),{useRegex:a,useSelector:l}=s;h(o);try{n.s.focus(),n.s.save();let e=0,t;if(l){const r=d(n,c,u,a,l,o);e=r.replacements,t=r.message}else e=v(n,n.value,c,u,a,o);return{replacements:e,message:t||(e>0?`Replaced ${e} occurrence(s)`:"No matches found")}}catch(e){throw n.s.restore(),new Error(`Failed to replace in document: ${e?.message||"Unknown error"}`)}}m(b,"execute");export{b as execute};