UNPKG

reactjs-tiptap-editor

Version:

A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React

2 lines (1 loc) 3.62 kB
"use strict";const q=require("./index-CSvnpArE.cjs");function U(B){var c;const{char:_,allowSpaces:C,allowToIncludeChar:E,allowedPrefixes:f,startOfLine:D,$position:d}=B,I=C&&!E,g=q.escapeForRegEx(_),M=new RegExp(`\\s${g}$`),v=D?"^":"",R=E?"":g,k=I?new RegExp(`${v}${g}.*?(?=\\s${R}|$)`,"gm"):new RegExp(`${v}(?:^)?${g}[^\\s${R}]*`,"gm"),r=((c=d.nodeBefore)===null||c===void 0?void 0:c.isText)&&d.nodeBefore.text;if(!r)return null;const e=d.pos-r.length,o=Array.from(r.matchAll(k)).pop();if(!o||o.input===void 0||o.index===void 0)return null;const n=o.input.slice(Math.max(0,o.index-1),o.index),i=new RegExp(`^[${f==null?void 0:f.join("")}\0]?$`).test(n);if(f!==null&&!i)return null;const a=e+o.index;let l=a+o[0].length;return I&&M.test(r.slice(l-1,l+1))&&(o[0]+=" ",l+=1),a<d.pos&&l>=d.pos?{range:{from:a,to:l},query:o[0].slice(_.length),text:o[0]}:null}const j=new q.PluginKey("suggestion");function L({pluginKey:B=j,editor:c,char:_="@",allowSpaces:C=!1,allowToIncludeChar:E=!1,allowedPrefixes:f=[" "],startOfLine:D=!1,decorationTag:d="span",decorationClass:I="suggestion",command:g=()=>null,items:M=()=>[],render:v=()=>({}),allow:R=()=>!0,findSuggestionMatch:k=U}){let r;const e=v==null?void 0:v(),o=new q.Plugin({key:B,view(){return{update:async(n,i)=>{var a,l,u,p,m,y,x;const t=(a=this.key)===null||a===void 0?void 0:a.getState(i),s=(l=this.key)===null||l===void 0?void 0:l.getState(n.state),S=t.active&&s.active&&t.range.from!==s.range.from,b=!t.active&&s.active,F=t.active&&!s.active,A=!b&&!F&&t.query!==s.query,$=b||S&&A,P=A||S,K=F||S&&A;if(!$&&!P&&!K)return;const h=K&&!$?t:s,O=n.dom.querySelector(`[data-decoration-id="${h.decorationId}"]`);r={editor:c,range:h.range,query:h.query,text:h.text,items:[],command:w=>g({editor:c,range:h.range,props:w}),decorationNode:O,clientRect:O?()=>{var w;const{decorationId:T}=(w=this.key)===null||w===void 0?void 0:w.getState(c.state),N=n.dom.querySelector(`[data-decoration-id="${T}"]`);return(N==null?void 0:N.getBoundingClientRect())||null}:null},$&&((u=e==null?void 0:e.onBeforeStart)===null||u===void 0||u.call(e,r)),P&&((p=e==null?void 0:e.onBeforeUpdate)===null||p===void 0||p.call(e,r)),(P||$)&&(r.items=await M({editor:c,query:h.query})),K&&((m=e==null?void 0:e.onExit)===null||m===void 0||m.call(e,r)),P&&((y=e==null?void 0:e.onUpdate)===null||y===void 0||y.call(e,r)),$&&((x=e==null?void 0:e.onStart)===null||x===void 0||x.call(e,r))},destroy:()=>{var n;r&&((n=e==null?void 0:e.onExit)===null||n===void 0||n.call(e,r))}}},state:{init(){return{active:!1,range:{from:0,to:0},query:null,text:null,composing:!1}},apply(n,i,a,l){const{isEditable:u}=c,{composing:p}=c.view,{selection:m}=n,{empty:y,from:x}=m,t={...i};if(t.composing=p,u&&(y||c.view.composing)){(x<i.range.from||x>i.range.to)&&!p&&!i.composing&&(t.active=!1);const s=k({char:_,allowSpaces:C,allowToIncludeChar:E,allowedPrefixes:f,startOfLine:D,$position:m.$from}),S=`id_${Math.floor(Math.random()*4294967295)}`;s&&R({editor:c,state:l,range:s.range,isActive:i.active})?(t.active=!0,t.decorationId=i.decorationId?i.decorationId:S,t.range=s.range,t.query=s.query,t.text=s.text):t.active=!1}else t.active=!1;return t.active||(t.decorationId=null,t.range={from:0,to:0},t.query=null,t.text=null),t}},props:{handleKeyDown(n,i){var a;const{active:l,range:u}=o.getState(n.state);return l&&((a=e==null?void 0:e.onKeyDown)===null||a===void 0?void 0:a.call(e,{view:n,event:i,range:u}))||!1},decorations(n){const{active:i,range:a,decorationId:l}=o.getState(n);return i?q.DecorationSet.create(n.doc,[q.Decoration.inline(a.from,a.to,{nodeName:d,class:I,"data-decoration-id":l})]):null}}});return o}exports.Suggestion=L;