jodit-pro
Version:
PRO Version of Jodit Editor
2 lines (1 loc) • 3.38 kB
JavaScript
var j=Object.defineProperty;var d=(c,e)=>j(c,"name",{value:e,configurable:!0});var g=function(c,e,t,r){var i=arguments.length,o=i<3?e:r===null?r=Object.getOwnPropertyDescriptor(e,t):r,n;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(c,e,t,r);else for(var s=c.length-1;s>=0;s--)(n=c[s])&&(o=(i<3?n(o):i>3?n(e,t,o):n(e,t))||o);return i>3&&o&&Object.defineProperty(e,t,o),o};import"./config.js";import{autobind as k,debounce as b}from"jodit/esm/core/decorators/index.js";import{Dom as a}from"jodit/esm/core/dom/dom.js";import{attr as v}from"jodit/esm/core/helpers/utils/attr.js";import{Plugin as T}from"jodit/esm/core/plugin/plugin.js";import{JoditPro as S}from"../../jodit-pro.js";const h=class h extends T{constructor(){super(...arguments),this.abortController=null}afterInit(e){Object.keys(e.o.highlightSignature.schema).length&&(e.e.on("change afterSetMode",this.walkNodes).on("afterGetValueFromEditor",h.removeUtilWrappers),this.walkNodes())}beforeDestruct(e){e.e.off("change afterSetMode",this.walkNodes).off("afterGetValueFromEditor",h.removeUtilWrappers)}walkNodes(){var e;if(!this.j.isEditorMode())return;this.checkUtilsBoxToSchema();const{j:t}=this,r=t.ed.createNodeIterator(t.editor,NodeFilter.SHOW_TEXT);(e=this.abortController)===null||e===void 0||e.abort(),this.workLoop(r)}runWorker(e){let t;this.j.e.mute();let r=0;const{processInChunkCount:i}=this.j.o.highlightSignature;try{do{if(r+=1,t=e.nextNode(),!t||this.checkNormalizing(t))return;this.checkReplaceSchemas(t)}while(t&&i>r)}finally{this.j.e.unmute()}this.workLoop(e)}workLoop(e){this.abortController=new AbortController,this.j.async.schedulerPostTask(()=>this.runWorker(e),{delay:this.j.o.highlightSignature.processDelay}).catch(()=>null)}checkNormalizing(e){return a.isText(e.nextSibling)?(this.j.editor.normalize(),this.walkNodes(),!0):!1}checkReplaceSchemas(e){if(h.hasUtilWrapper(e))return;const t=e.nodeValue;if(t==null)return;const{j:r}=this,i=r.o.highlightSignature;for(const o in i.schema){const n=RegExp(o);if(n.test(t)){const s=t.match(n);if(!s||s.index===void 0)continue;const u=i.schema[o](this.j,s);if(u){a.markTemporary(u,{dataHighlightSchema:o}),this.replaceMatchedTextToElm(e,t,s,u);return}}}}replaceMatchedTextToElm(e,t,r,i){var o;const{j:n}=this,{range:s}=n.s,u=s.startContainer===e,m=s.startOffset,f=(o=r.index)!==null&&o!==void 0?o:0;e.nodeValue=t.substring(0,f);const p=t.substring(f+r[0].length);if(p.length){const x=n.createInside.text(p);a.after(e,x)}i.innerText=r[0],a.after(e,i),u&&this.restoreCursorPosition(m,e,i.firstChild,i.nextSibling)}static hasUtilWrapper(e){return a.isTemporary(e.parentElement)}static removeUtilWrappers(e){e.value=a.replaceTemporaryFromString(e.value)}restoreCursorPosition(e,...t){for(const r of t)if(r&&r.nodeValue){const i=r.nodeValue;if(i.length>=e){const o=this.j.s.createRange();o.setStart(r,e),this.j.s.selectRange(o,!1);break}e-=i.length}}checkUtilsBoxToSchema(){a.temporaryList(this.j.editor).forEach(e=>{var t;const r=v(e,"dataHighlightSchema");if(!r)return;const i=RegExp(r),o=(t=e.innerText)!==null&&t!==void 0?t:"";(!i.test(o)||o.replace(i,"").length)&&(this.j.s.save(),a.unwrap(e),this.j.s.restore())})}};d(h,"highlightSignature");let l=h;l.requires=["license"],g([b()],l.prototype,"walkNodes",null),g([k],l.prototype,"runWorker",null),S.plugins.add("highlight-signature",l);export{l as highlightSignature};