UNPKG

jodit-pro

Version:

PRO Version of Jodit Editor

2 lines (1 loc) 4.89 kB
var j=Object.defineProperty;var f=(u,t)=>j(u,"name",{value:t,configurable:!0});var h,l=function(u,t,i,e){var o=arguments.length,s=o<3?t:e===null?e=Object.getOwnPropertyDescriptor(t,i):e,a;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")s=Reflect.decorate(u,t,i,e);else for(var r=u.length-1;r>=0;r--)(a=u[r])&&(s=(o<3?a(s):o>3?a(t,i,s):a(t,i))||s);return o>3&&s&&Object.defineProperty(t,i,s),s},c;import{autobind as y,component as E,debounce as I,hook as L,persistent as F,throttle as w}from"jodit/esm/core/decorators/index.js";import{Dom as d}from"jodit/esm/core/dom/dom.js";import{attr as C,isString as N}from"jodit/esm/core/helpers/index.js";import{UIElement as _,UIInput as b}from"jodit/esm/core/ui/index.js";let n=c=(h=class extends _{className(){return"Emoji"}get defaultList(){return this.recent.concat(this.data?this.data.emoji:[])}reset(){this.input.nativeInput.value="",this.setItems(this.defaultList),this.setActiveCategory(0)}setItems(t){var i,e;this.list.scrollTo(0,0),d.detach(this.list),d.append(this.list,this.generateEmojiList(t,(e=(i=this.data)===null||i===void 0?void 0:i.categories)!==null&&e!==void 0?e:[]))}createContainer(t){const i=this.j.c.div("jodit-emoji");this.input=new b(this.j,{name:"search",icon:"search",autocomplete:!1,clearButton:!0,placeholder:"Search emoji"}),this.input.container.classList.add(this.getFullElName("input")),this.categories=this.j.c.div(this.getFullElName("categories")),this.list=this.j.c.div(this.getFullElName("emojis"));const e=this.j.c.div(this.getFullElName("navigate"));return d.append(e,[this.categories,this.input.container]),d.append(i,[e,this.list]),i}constructor(t){super(t),this.data=null,this.recent=[],this.map={},this.activeIndex=0,this.cache={},this.setMod("loading",!0),(async()=>(this.data=await t.o.emoji.data(),this.setMod("loading",!1),this.data.emoji.forEach(i=>{const e=c.normalizeEmoji(i);this.map[e.emoji]=e}),this.setItems(this.defaultList),d.append(this.categories,this.generateCategoriesList(this.data.categories)),this.setActiveCategory(0)))()}setActiveCategory(t,i=!1){const e=this.getElms("category"),o=e[this.activeIndex];e[t]||(t=e.length-1);const s=e[t],a=this.getFullElName("category","active",!0);o&&o.classList.remove(a),s&&s.classList.add(a),i&&this.getElms("category-title")[t].scrollIntoView(),this.activeIndex=t}generateCategoriesList(t){return t.map((i,e)=>this.j.c.div(this.getFullElName("category"),{"data-index":e,title:i,style:{width:100/t.length+"%"}}))}generateEmojiList(t,i){let e="";return t.reduce((o,s,a)=>{var r,p;s=c.normalizeEmoji(s);const g=s.category===-1?"Recently":i[s.category];if(g!==e){e=g;const v=(r=this.cache[e])!==null&&r!==void 0?r:this.j.c.div(this.getFullElName("category-title"),e?this.j.i18n(e):"");this.cache[e]=v,o.push(v)}const m=(p=this.cache[s.emoji+s.category])!==null&&p!==void 0?p:this.j.c.div(this.getFullElName("emoji"),{"data-id":a,"data-value":s.emoji},s.emoji);return this.cache[s.emoji+s.category]=m,o.push(m),o},[])}static isShortCat(t){return t&&N(t.e)}static normalizeEmoji(t){return c.isShortCat(t)?{emoji:t.e,description:t.d,category:t.c,aliases:t.a,tags:t.t}:t}onInsertCode(t){this.jodit.e.fire(this,"insert",t);const i=this.map[t];if(i){const e=[...this.recent],o=e.findIndex(s=>s.emoji===t);o!==-1&&e.splice(o,1),e.unshift({...i,category:-1}),this.recent=e}}onScrollList(){const t=this.list.scrollTop,i=this.getElms("category-title");let e=0;i.forEach((o,s)=>{const a=o.offsetTop-this.list.offsetTop-20;t>a&&(e=s)}),this.setActiveCategory(e)}onClickCategory(t){this.clearFilter();const i=t.target;i.classList.contains(this.getFullElName("category"))&&this.setActiveCategory(parseInt(C(i,"-index")||"0",10)||0,!0)}onClickItem(t){const i=t.target;if(i.classList.contains(this.getFullElName("emoji"))){const e=C(i,"-value")||"";e&&this.onInsertCode(e)}t.preventDefault()}onInputFilter(){const t=this.input.value.toLowerCase();if(!t.length)return this.setItems(this.defaultList);this.setItems(this.defaultList.filter(i=>{var e,o,s;const a=c.normalizeEmoji(i);return((e=a.description)===null||e===void 0?void 0:e.toLowerCase().includes(t))||((o=a.tags)===null||o===void 0?void 0:o.join(" ").toLowerCase().includes(t))||((s=a.aliases)===null||s===void 0?void 0:s.join(" ").toLowerCase().includes(t))}))}clearFilter(){this.input.nativeInput.value.length&&this.reset()}onReady(){this.j.e.on(this.input.nativeInput,"input",this.onInputFilter).on(this.categories,"click",this.onClickCategory).on(this.list,"scroll",this.onScrollList).on(this.list,"mousedown touchstart",this.onClickItem)}destruct(){return this.input.destruct(),super.destruct()}},f(h,"Emoji"),h);l([F],n.prototype,"recent",void 0),l([w()],n.prototype,"onScrollList",null),l([y],n.prototype,"onClickCategory",null),l([y],n.prototype,"onClickItem",null),l([I()],n.prototype,"onInputFilter",null),l([L("ready")],n.prototype,"onReady",null),n=c=l([E],n);export{n as Emoji};