UNPKG

jodit-pro

Version:

PRO Version of Jodit Editor

1 lines 6.66 kB
"use strict";((e,t)=>{if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var n in o)("object"==typeof exports?exports:e)[n]=o[n]}})(self,(function(){return(self.webpackChunkjodit_pro=self.webpackChunkjodit_pro||[]).push([[633],{38096(e,t,o){o.r(t),o.d(t,{autocomplete(){return m}});var n=o(70655),i=o(43464),s=o(733),r=o(98422),l=o(16892),u=o(12815),c=o(17497),a=o(33993);class p extends a.u1{constructor(e,t){var o;super(e),this.item=t;const{container:n}=this;if((0,l.isFunction)(t.itemRenderer)){const e=t.itemRenderer(t);(0,l.isString)(e)?n.innerHTML=e:n.appendChild(e)}else n.innerText=null!==(o=t.title)&&void 0!==o?o:t.value}className(){return"AutocompleteItem"}}let h=class e extends a.qe{constructor(){super(...arguments),this.elements=[],this.currentSelection=-1}className(){return"Autocomplete"}onChangeSelection(e,t){var o;null!=t&&(null===(o=this.elements[t])||void 0===o||o.setMod("active",!1)),this.elements[this.currentSelection].setMod("active",!0),this.elements[this.currentSelection].container.scrollIntoView(!1)}build(e){this.clear(),e.forEach(((e,t)=>{const o=new p(this.jodit,e);this.j.e.on(o.container,"mousedown touchstart",(e=>{e.preventDefault()})).on(o.container,"click",(()=>{this.currentSelection=t,this.select()})),this.append(o)})),this.currentSelection=0,this.onChangeSelection()}selectNext(){this.currentSelection+1>this.elements.length-1?this.currentSelection=0:this.currentSelection+=1}selectPrevious(){0>this.currentSelection-1?this.currentSelection=this.elements.length-1:this.currentSelection-=1}select(){this.j.e.fire("select.autocomplete",this.elements[this.currentSelection].item)}};(0,n.gn)([(0,r.watch)("currentSelection")],h.prototype,"onChangeSelection",null),h=(0,n.gn)([r.component],h);var d=o(49940),f=o(66809);s.D.prototype.autocomplete={sources:[],maxItems:50,isMatchedQuery(e,t){return 0===t.toLowerCase().indexOf(e.toLowerCase())},itemRenderer(e){var t;return null!==(t=e.title)&&void 0!==t?t:e.value},insertValueRenderer(e){return e.value+" "}};class m extends i.S{constructor(){super(...arguments),this.requires=["enter","license"],this.hasStyle=!f.Jodit.fatMode,this.sources=[]}afterInit(e){this.popup=new c.G(e),this.popup.setMod("padding",!1),this.list=new h(e),this.popup.setContent(this.list.container),e.e.on("select.autocomplete",(t=>{let o=t.value;(0,l.isFunction)(t.insertValueRenderer)&&(o=t.insertValueRenderer(t)),u.i.isNode(o)||(o=e.createInside.fromHTML(o)),((e,t)=>{var o,n,i;let{startContainer:s,startOffset:r}=e;if(u.i.isText(s)||(s=s.childNodes[r-1],u.i.isText(s)&&(r=null!==(n=null===(o=s.nodeValue)||void 0===o?void 0:o.length)&&void 0!==n?n:0)),u.i.isText(s)){const o=null!==(i=s.nodeValue)&&void 0!==i?i:"",n=o.substring(0,r),l=o.substring(r),u=n.split(" ");u[u.length-1]="";const c=u.join(" ");s.nodeValue=c+l,e.setStart(s,c.length),e.insertNode(t)}})(e.s.range,o),this.j.s.setCursorAfter(o),this.j.synchronizeValues(),this.popup.close()})).on("keydown.autocomplete",this.onKeyDown).on("keydown.autocomplete",this.onKeyControlDown,{top:!0}).on("beforeEnter.autocomplete",this.onEnter,{top:!0}).on("autocomplete.autocomplete",this.onAutoComplete).on("registerAutocompleteSource.autocomplete",this.registerAutocompleteSource)}registerAutocompleteSource(e){this.sources.push(e)}static isControlKey(e){return e===d.KEY_DOWN||e===d.KEY_UP||e===d.KEY_ENTER||e===d.KEY_TAB}async onKeyDown(e){var t,o;if(e.key===d.KEY_ESC)return void(this.popup.isOpened&&this.popup.close());const{s:n}=this.j;if(m.isControlKey(e.key)||!n.isInsideArea||!n.isCollapsed)return;const i=(null===(t=this.j.s.sel)||void 0===t?void 0:t.rangeCount)?null===(o=this.j.s.sel)||void 0===o?void 0:o.getRangeAt(0):null,s=i&&(e=>{var t,o,n,i;const{startContainer:s,startOffset:r}=e;if(u.i.isText(s))return null!==(o=null===(t=s.nodeValue)||void 0===t?void 0:t.substr(0,r))&&void 0!==o?o:"";const l=s.childNodes[r];if(!l)return"";if(u.i.isText(l))return null!==(n=l.nodeValue)&&void 0!==n?n:"";const c=u.i.findNotEmptyNeighbor(l,!0,s);return u.i.isText(c)&&null!==(i=l.nodeValue)&&void 0!==i?i:""})(i).split(" ").pop();if(s&&(0,l.trim)(s).length){const e=await this.onAutoComplete(s);if(e.length)return this.openPopup(e)}this.popup.isOpened&&this.popup.close()}onKeyControlDown(e){if(this.popup.isOpened&&m.isControlKey(e.key)){switch(e.key){case d.KEY_DOWN:this.list.selectNext();break;case d.KEY_UP:this.list.selectPrevious();break;case d.KEY_TAB:this.list.select()}return this.j.e.stopPropagation(e.type),!1}}onEnter(){if(this.popup.isOpened)return this.list.select(),!1}async onAutoComplete(e){const t=[];return await Promise.all(this.sources.concat(this.j.o.autocomplete.sources).map((async o=>{t.push(...await this.resolveFeed(e,o))}))),t}async resolveFeed(e,t,o){let n;if((0,l.isPromise)(t)&&(t=await t),(0,l.isFunction)(t))n=await t(e);else if((0,l.isArray)(t)){const o=t,{isMatchedQuery:i}=this.j.o.autocomplete;n=o.filter((t=>(0,l.isString)(t)?i(e,t):i(e,t.value))).map((e=>(0,l.isString)(e)?{title:e,value:e}:e))}else n=await this.resolveFeed(e,t.feed,t);if(n&&(0,l.isArray)(n)){const{itemRenderer:e,insertValueRenderer:t,maxItems:i}=this.j.o.autocomplete;return n=n.map((n=>{var i,s;return{itemRenderer:null!==(i=null==o?void 0:o.itemRenderer)&&void 0!==i?i:e,insertValueRenderer:null!==(s=null==o?void 0:o.insertValueRenderer)&&void 0!==s?s:t,...n}})),n.slice(0,i)}return[]}openPopup(e){this.list.build(e),this.popup.open((()=>{const{j:e}=this,t=e.s.range.getBoundingClientRect(),o={left:t.left,top:t.top,height:t.height,width:t.width};if(e.o.iframe&&e.iframe){const t=(0,l.position)(e.iframe,e);o.top+=t.top,o.left+=t.left}return o}))}beforeDestruct(e){e.e.off(this.list).off("keydown.autocomplete",this.onKeyDown).off("autocomplete.autocomplete",this.onAutoComplete).off(".autocomplete"),this.list.destruct(),this.popup.destruct()}}(0,n.gn)([r.autobind],m.prototype,"registerAutocompleteSource",null),(0,n.gn)([(0,r.debounce)()],m.prototype,"onKeyDown",null),(0,n.gn)([r.autobind],m.prototype,"onKeyControlDown",null),(0,n.gn)([r.autobind],m.prototype,"onEnter",null),(0,n.gn)([r.autobind],m.prototype,"onAutoComplete",null),(0,n.gn)([r.autobind],m.prototype,"resolveFeed",null),f.Jodit.plugins.add("autocomplete",m)},70655(e,t,o){function n(e,t,o,n){var i,s=arguments.length,r=3>s?t:null===n?n=Object.getOwnPropertyDescriptor(t,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,o,n);else for(var l=e.length-1;l>=0;l--)(i=e[l])&&(r=(3>s?i(r):s>3?i(t,o,r):i(t,o))||r);return s>3&&r&&Object.defineProperty(t,o,r),r}o.d(t,{gn(){return n}})}},e=>e(e.s=38096)])}));