import{Command as q,Plugin as A}from"@ckeditor/ckeditor5-core";import{CKEditorError as R,toMap as W,uid as K,Rect as v,keyCodes as p,Collection as j,logWarning as H,env as z}from"@ckeditor/ckeditor5-utils";import{ListView as G,View as J,ListItemView as Q,ContextualBalloon as P,clickOutsideHandler as X,ButtonView as Y}from"@ckeditor/ckeditor5-ui";import{TextWatcher as Z}from"@ckeditor/ckeditor5-typing";/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/const E={"(":")","[":"]","{":"}"};class T extends q{constructor(e){super(e),this._isEnabledBasedOnSelection=!1}refresh(){const e=this.editor.model,t=e.document;this.isEnabled=e.schema.checkAttributeInSelection(t.selection,"mention")}execute(e){const t=this.editor.model,n=t.document.selection,s=typeof e.mention=="string"?{id:e.mention}:e.mention,r=s.id,c=e.range||n.getFirstRange();if(!t.canEditAt(c))return;const a=e.text||r,d=y({_text:a,id:r},s);if(!r.startsWith(e.marker))throw new R("mentioncommand-incorrect-id",this);t.change(l=>{const u=W(n.getAttributes()),m=new Map(u.entries());m.set("mention",d);const f=t.insertContent(l.createText(a,m),c),w=f.start.nodeBefore,h=f.end.nodeAfter,x=h&&h.is("$text")&&h.data.startsWith(" ");let g=!1;if(w&&h&&w.is("$text")&&h.is("$text")){const _=w.data.slice(-1),V=_ in E,O=V&&h.data.startsWith(E[_]);g=V&&O}!g&&!x&&t.insertContent(l.createText(" ",u),c.start.getShiftedBy(a.length))})}}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class F extends A{static get pluginName(){return"MentionEditing"}static get isOfficialPlugin(){return!0}init(){const e=this.editor,t=e.model,i=t.document;t.schema.extend("$text",{allowAttributes:"mention"}),e.conversion.for("upcast").elementToAttribute({view:{name:"span",attributes:"data-mention",classes:"mention"},model:{key:"mention",value:n=>M(n)}}),e.conversion.for("downcast").attributeToElement({model:"mention",view:te}),e.conversion.for("downcast").add(ee),i.registerPostFixer(n=>oe(n,i,t.schema)),i.registerPostFixer(n=>se(n,i)),i.registerPostFixer(n=>ne(n,i)),e.commands.add("mention",new T(e))}}function y(o,e){return Object.assign({uid:K()},o,e||{})}function M(o,e){const t=o.getAttribute("data-mention"),i=o.getChild(0);if(!i)return;const n={id:t,_text:i.data};return y(n,e)}function ee(o){o.on("attribute:mention",(e,t,i)=>{const n=t.attributeNewValue;if(!t.item.is("$textProxy")||!n)return;const s=t.range.start;(s.textNode||s.nodeAfter).data!=n._text&&i.consumable.consume(t.item,e.name)},{priority:"highest"})}function te(o,{writer:e}){if(!o)return;const t={class:"mention","data-mention":o.id},i={id:o.uid,priority:20};return e.createAttributeElement("span",t,i)}function ne(o,e){const t=e.selection,i=t.focus;return t.isCollapsed&&t.hasAttribute("mention")&&ie(i)?(o.removeSelectionAttribute("mention"),!0):!1}function ie(o){const e=o.isAtStart;return o.nodeBefore&&o.nodeBefore.is("$text")||e}function oe(o,e,t){const i=e.differ.getChanges();let n=!1;for(const s of i){if(s.type=="attribute")continue;const r=s.position;if(s.name=="$text"){const c=r.textNode&&r.textNode.nextSibling;n=b(r.textNode,o)||n,n=b(c,o)||n,n=b(r.nodeBefore,o)||n,n=b(r.nodeAfter,o)||n}if(s.name!="$text"&&s.type=="insert"){const c=r.nodeAfter;for(const a of o.createRangeIn(c).getItems())n=b(a,o)||n}if(s.type=="insert"&&t.isInline(s.name)){const c=r.nodeAfter&&r.nodeAfter.nextSibling;n=b(r.nodeBefore,o)||n,n=b(c,o)||n}}return n}function se(o,e){const t=e.differ.getChanges();let i=!1;for(const n of t)if(n.type==="attribute"&&n.attributeKey!="mention"){const s=n.range.start.nodeBefore,r=n.range.end.nodeAfter;for(const c of[s,r])B(c)&&c.getAttribute(n.attributeKey)!=n.attributeNewValue&&(o.setAttribute(n.attributeKey,n.attributeNewValue,c),i=!0)}return i}function B(o){if(!o||!(o.is("$text")||o.is("$textProxy"))||!o.hasAttribute("mention"))return!1;const e=o.data,i=o.getAttribute("mention")._text;return e!=i}function b(o,e){return B(o)?(e.removeAttribute("mention",o),!0):!1}function re(o,e,{signal:t,edges:i}={}){let n,s=null;const r=i!=null&&i.includes("leading"),c=i==null||i.includes("trailing"),a=()=>{s!==null&&(o.apply(n,s),n=void 0,s=null)},d=()=>{c&&a(),f()};let l=null;const u=()=>{l!=null&&clearTimeout(l),l=setTimeout(()=>{l=null,d()},e)},m=()=>{l!==null&&(clearTimeout(l),l=null)},f=()=>{m(),n=void 0,s=null},w=()=>{m(),a()},h=function(...x){if(t?.aborted)return;n=this,s=x;const g=l==null;u(),r&&g&&a()};return h.schedule=u,h.cancel=f,h.flush=w,t?.addEventListener("abort",f,{once:!0}),h}function ce(o,e=0,t={}){typeof t!="object"&&(t={});const{leading:i=!1,trailing:n=!0,maxWait:s}=t,r=Array(2);i&&(r[0]="leading"),n&&(r[1]="trailing");let c,a=null;const d=re(function(...m){c=o.apply(this,m),a=null},e,{edges:r}),l=function(...m){return s!=null&&(a===null&&(a=Date.now()),Date.now()-a>=s)?(c=o.apply(this,m),a=Date.now(),d.cancel(),d.schedule(),c):(d.apply(this,m),c)},u=()=>(d.flush(),c);return l.cancel=d.cancel,l.flush=u,l}function N(o,{insertAt:e}={}){if(!o||typeof document>"u")return;const t=document.head||document.getElementsByTagName("head")[0],i=document.createElement("style");i.type="text/css",window.litNonce&&i.setAttribute("nonce",window.litNonce),e==="top"&&t.firstChild?t.insertBefore(i,t.firstChild):t.appendChild(i),i.styleSheet?i.styleSheet.cssText=o:i.appendChild(document.createTextNode(o))}N(":root{--ck-mention-list-max-height:300px}.ck.ck-mentions{max-height:var(--ck-mention-list-max-height);overflow-x:hidden;overflow-y:auto;overscroll-behavior:contain}.ck.ck-mentions>.ck-list__item{flex-shrink:0;overflow:hidden}div.ck.ck-balloon-panel.ck-mention-balloon{z-index:calc(var( --ck-z-dialog ) + 1)}");/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class D extends G{selected;position;constructor(e){super(e),this.extendTemplate({attributes:{class:["ck-mentions"],tabindex:"-1"}})}selectFirst(){this.select(0)}selectNext(){const e=this.selected,t=this.items.getIndex(e);this.select(t+1)}selectPrevious(){const e=this.selected,t=this.items.getIndex(e);this.select(t-1)}select(e){let t=0;e>0&&e<this.items.length?t=e:e<0&&(t=this.items.length-1);const i=this.items.get(t);this.selected!==i&&(this.selected&&this.selected.removeHighlight(),i.highlight(),this.selected=i,this._isItemVisibleInScrolledArea(i)||(this.element.scrollTop=i.element.offsetTop))}executeSelected(){this.selected.fire("execute")}_isItemVisibleInScrolledArea(e){return new v(this.element).contains(new v(e.element))}}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class S extends J{domElement;constructor(e,t){super(e),this.template=void 0,this.domElement=t,this.domElement.classList.add("ck-button"),this.set("isOn",!1),this.on("change:isOn",(i,n,s)=>{s?(this.domElement.classList.add("ck-on"),this.domElement.classList.remove("ck-off")):(this.domElement.classList.add("ck-off"),this.domElement.classList.remove("ck-on"))}),this.listenTo(this.domElement,"click",()=>{this.fire("execute")})}render(){super.render(),this.element=this.domElement}focus(){this.domElement.focus()}}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class L extends Q{item;marker;highlight(){const e=this.children.first;e.isOn=!0}removeHighlight(){const e=this.children.first;e.isOn=!1}}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/const k=3,ae=[p.arrowup,p.arrowdown,p.esc],le=[p.enter,p.tab];class $ extends A{_mentionsView;_mentionsConfigurations;_balloon;_items=new j;_lastRequested;_requestFeedDebounced;static get pluginName(){return"MentionUI"}static get isOfficialPlugin(){return!0}static get requires(){return[P]}constructor(e){super(e),this._mentionsView=this._createMentionView(),this._mentionsConfigurations=new Map,this._requestFeedDebounced=ce(this._requestFeed,100),e.config.define("mention",{feeds:[]})}init(){const e=this.editor,t=e.config.get("mention.commitKeys")||le,i=ae.concat(t);this._balloon=e.plugins.get(P),e.editing.view.document.on("keydown",(r,c)=>{s(c.keyCode)&&this._isUIVisible&&(c.preventDefault(),r.stop(),c.keyCode==p.arrowdown&&this._mentionsView.selectNext(),c.keyCode==p.arrowup&&this._mentionsView.selectPrevious(),t.includes(c.keyCode)&&this._mentionsView.executeSelected(),c.keyCode==p.esc&&this._hideUIAndRemoveMarker())},{priority:"highest"}),X({emitter:this._mentionsView,activator:()=>this._isUIVisible,contextElements:()=>[this._balloon.view.element],callback:()=>this._hideUIAndRemoveMarker()});const n=e.config.get("mention.feeds");for(const r of n){const{feed:c,marker:a,dropdownLimit:d}=r;if(!pe(a))throw new R("mentionconfig-incorrect-marker",null,{marker:a});const l=typeof c=="function"?c.bind(this.editor):he(c),u=r.itemRenderer,m={marker:a,feedCallback:l,itemRenderer:u,dropdownLimit:d};this._mentionsConfigurations.set(a,m)}this._setupTextWatcher(n),this.listenTo(e,"change:isReadOnly",()=>{this._hideUIAndRemoveMarker()}),this.on("requestFeed:response",(r,c)=>this._handleFeedResponse(c)),this.on("requestFeed:error",()=>this._hideUIAndRemoveMarker());function s(r){return i.includes(r)}}destroy(){super.destroy(),this._mentionsView.destroy()}get _isUIVisible(){return this._balloon.visibleView===this._mentionsView}_createMentionView(){const e=this.editor.locale,t=new D(e);return t.items.bindTo(this._items).using(i=>{const{item:n,marker:s}=i,{dropdownLimit:r}=this._mentionsConfigurations.get(s),c=r||this.editor.config.get("mention.dropdownLimit")||10;if(t.items.length>=c)return null;const a=new L(e),d=this._renderItem(n,s);return d.delegate("execute").to(a),a.children.add(d),a.item=n,a.marker=s,a.on("execute",()=>{t.fire("execute",{item:n,marker:s})}),a}),t.on("execute",(i,n)=>{const s=this.editor,r=s.model,c=n.item,a=n.marker,d=s.model.markers.get("mention"),l=r.createPositionAt(r.document.selection.focus),u=r.createPositionAt(d.getStart()),m=r.createRange(u,l);this._hideUIAndRemoveMarker(),s.execute("mention",{mention:c,text:c.text,marker:a,range:m}),s.editing.view.focus()}),t}_getItemRenderer(e){const{itemRenderer:t}=this._mentionsConfigurations.get(e);return t}_requestFeed(e,t){this._lastRequested=t;const{feedCallback:i}=this._mentionsConfigurations.get(e),n=i(t);if(!(n instanceof Promise)){this.fire("requestFeed:response",{feed:n,marker:e,feedText:t});return}n.then(r=>{this._lastRequested==t?this.fire("requestFeed:response",{feed:r,marker:e,feedText:t}):this.fire("requestFeed:discarded",{feed:r,marker:e,feedText:t})}).catch(r=>{this.fire("requestFeed:error",{error:r}),H("mention-feed-callback-error",{marker:e})})}_setupTextWatcher(e){const t=this.editor,i=e.map(r=>({...r,pattern:C(r.marker,r.minimumCharacters||0)})),n=new Z(t.model,me(i));n.on("matched",(r,c)=>{const a=U(i,c.text),l=t.model.document.selection.focus,u=t.model.createPositionAt(l.parent,a.position);if(fe(l)||ge(u)){this._hideUIAndRemoveMarker();return}const m=ue(a,c.text),f=a.marker.length+m.length,w=l.getShiftedBy(-f),h=l.getShiftedBy(-m.length),x=t.model.createRange(w,h);if(I(t)){const g=t.model.markers.get("mention");t.model.change(_=>{_.updateMarker(g,{range:x})})}else t.model.change(g=>{g.addMarker("mention",{range:x,usingOperation:!1,affectsData:!1})});this._requestFeedDebounced(a.marker,m)}),n.on("unmatched",()=>{this._hideUIAndRemoveMarker()});const s=t.commands.get("mention");return n.bind("isEnabled").to(s),n}_handleFeedResponse(e){const{feed:t,marker:i}=e;if(!I(this.editor))return;this._items.clear();for(const s of t){const r=typeof s!="object"?{id:s,text:s}:s;this._items.add({item:r,marker:i})}const n=this.editor.model.markers.get("mention");this._items.length?this._showOrUpdateUI(n):this._hideUIAndRemoveMarker()}_showOrUpdateUI(e){this._isUIVisible?this._balloon.updatePosition(this._getBalloonPanelPositionData(e,this._mentionsView.position)):this._balloon.add({view:this._mentionsView,position:this._getBalloonPanelPositionData(e,this._mentionsView.position),singleViewMode:!0,balloonClassName:"ck-mention-balloon"}),this._mentionsView.position=this._balloon.view.position,this._mentionsView.selectFirst()}_hideUIAndRemoveMarker(){this._balloon.hasView(this._mentionsView)&&this._balloon.remove(this._mentionsView),I(this.editor)&&this.editor.model.change(e=>e.removeMarker("mention")),this._mentionsView.position=void 0}_renderItem(e,t){const i=this.editor;let n,s=e.id;const r=this._getItemRenderer(t);if(r){const c=r(e);typeof c!="string"?n=new S(i.locale,c):s=c}if(!n){const c=new Y(i.locale);c.label=s,c.withText=!0,n=c}return n}_getBalloonPanelPositionData(e,t){const i=this.editor,n=i.editing,s=n.view.domConverter,r=n.mapper,c=i.locale.uiLanguageDirection;return{target:()=>{let a=e.getRange();a.start.root.rootName=="$graveyard"&&(a=i.model.document.selection.getFirstRange());const d=r.toViewRange(a);return v.getDomRangeRects(s.viewRangeToDom(d)).pop()},limiter:()=>{const a=this.editor.editing.view,l=a.document.selection.editableElement;return l?a.domConverter.mapViewToDom(l.root):null},positions:de(t,c)}}}function de(o,e){const t={caret_se:i=>({top:i.bottom+k,left:i.right,name:"caret_se",config:{withArrow:!1}}),caret_ne:(i,n)=>({top:i.top-n.height-k,left:i.right,name:"caret_ne",config:{withArrow:!1}}),caret_sw:(i,n)=>({top:i.bottom+k,left:i.right-n.width,name:"caret_sw",config:{withArrow:!1}}),caret_nw:(i,n)=>({top:i.top-n.height-k,left:i.right-n.width,name:"caret_nw",config:{withArrow:!1}})};return Object.prototype.hasOwnProperty.call(t,o)?[t[o]]:e!=="rtl"?[t.caret_se,t.caret_sw,t.caret_ne,t.caret_nw]:[t.caret_sw,t.caret_se,t.caret_nw,t.caret_ne]}function U(o,e){let t;for(const i of o){const n=e.lastIndexOf(i.marker);n>0&&!e.substring(n-1).match(i.pattern)||(!t||n>=t.position)&&(t={marker:i.marker,position:n,minimumCharacters:i.minimumCharacters,pattern:i.pattern})}return t}function C(o,e){const t=e==0?"*":`{${e},}`,i=z.features.isRegExpUnicodePropertySupported?`\\p{Ps}\\p{Pi}"'`:`\\(\\[{"'`,n=".";o=o.replace(/[.*+?^${}()\-|[\]\\]/g,"\\$&");const s=`(?:^|[ ${i}])(${o})(${n}${t})$`;return new RegExp(s,"u")}function me(o){return t=>{const i=U(o,t);if(!i)return!1;let n=0;i.position!==0&&(n=i.position-1);const s=t.substring(n);return i.pattern.test(s)}}function ue(o,e){let t=0;o.position!==0&&(t=o.position-1);const i=C(o.marker,0);return e.substring(t).match(i)[2]}function he(o){return e=>o.filter(i=>(typeof i=="string"?i:String(i.id)).toLowerCase().includes(e.toLowerCase()))}function fe(o){const e=o.textNode&&o.textNode.hasAttribute("mention"),t=o.nodeBefore;return e||t&&t.is("$text")&&t.hasAttribute("mention")}function ge(o){const e=o.nodeAfter;return e&&e.is("$text")&&e.hasAttribute("mention")}function pe(o){return!!o}function I(o){return o.model.markers.has("mention")}N(":root{--ck-content-color-mention-background:rgba(153,0,48,.1);--ck-content-color-mention-text:#990030}.ck-content .mention{background:var(--ck-content-color-mention-background);color:var(--ck-content-color-mention-text)}");/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class we extends A{toMentionAttribute(e,t){return M(e,t)}static get pluginName(){return"Mention"}static get isOfficialPlugin(){return!0}static get requires(){return[F,$]}}export{we as Mention,T as MentionCommand,S as MentionDomWrapperView,F as MentionEditing,L as MentionListItemView,$ as MentionUI,D as MentionsView,y as _addMentionAttributes,C as _createMentionMarkerRegExp,M as _toMentionAttribute};
