/* esm.sh - @milkdown/plugin-listener@7.22.3 */
import{EditorViewReady as f,InitReady as L,SerializerReady as m,prosePluginsCtx as w,serializerCtx as y}from"../../../core@7.22.3/X-ZEBtaWxrZG93bi9jdHhANy4yMi4zLEBtaWxrZG93bi9wcm9zZUA3LjIyLjMscHJvc2VtaXJyb3ItbW9kZWxAMS4yNS4xMg/es2022/core.mjs";import{createSlice as b}from"../../../ctx@7.22.3/es2022/ctx.mjs";import{Plugin as g,PluginKey as k}from"../../../prose@7.22.3/X-ZHByb3NlbWlycm9yLW1vZGVsQDEuMjUuMTI/es2022/state.mjs";import{debounce as U}from"../../../../lodash-es@^4.17.21?target=es2022";var p=class{constructor(){this.beforeMountedListeners=[],this.mountedListeners=[],this.updatedListeners=[],this.selectionUpdatedListeners=[],this.markdownUpdatedListeners=[],this.blurListeners=[],this.focusListeners=[],this.destroyListeners=[],this.beforeMount=e=>(this.beforeMountedListeners.push(e),this),this.mounted=e=>(this.mountedListeners.push(e),this),this.updated=e=>(this.updatedListeners.push(e),this)}get listeners(){return{beforeMount:this.beforeMountedListeners,mounted:this.mountedListeners,updated:this.updatedListeners,markdownUpdated:this.markdownUpdatedListeners,blur:this.blurListeners,focus:this.focusListeners,destroy:this.destroyListeners,selectionUpdated:this.selectionUpdatedListeners}}markdownUpdated(e){return this.markdownUpdatedListeners.push(e),this}blur(e){return this.blurListeners.push(e),this}focus(e){return this.focusListeners.push(e),this}destroy(e){return this.destroyListeners.push(e),this}selectionUpdated(e){return this.selectionUpdatedListeners.push(e),this}},h=b(new p,"listener"),E=new k("MILKDOWN_LISTENER"),M=e=>(e.inject(h,new p),async()=>{await e.wait(L);let{listeners:r}=e.get(h);r.beforeMount.forEach(t=>t(e)),await e.wait(m);let u=e.get(y),n=null,d=null,i=null,o=null,l=U(()=>{if(!o)return;let{doc:t}=o;if(r.updated.length>0&&n&&!n.eq(t)&&r.updated.forEach(s=>{s(e,t,n)}),r.markdownUpdated.length>0&&n&&!n.eq(t)){let s=u(t);r.markdownUpdated.forEach(a=>{a(e,s,d)}),d=s}n=t,o=null},200),c=new g({key:E,view:()=>({destroy:()=>{l.cancel(),r.destroy.forEach(t=>t(e))}}),props:{handleDOMEvents:{focus:()=>(r.focus.forEach(t=>t(e)),!1),blur:()=>(r.blur.forEach(t=>t(e)),!1)}},state:{init:(t,s)=>{n=s.doc,d=u(s.doc)},apply:t=>{let s=t.selection;(!i&&s||i&&!s.eq(i))&&(r.selectionUpdated.forEach(a=>{a(e,s,i)}),i=s),!(!(t.docChanged||t.storedMarksSet)||t.getMeta("addToHistory")===!1)&&(o=t,l())}}});e.update(w,t=>t.concat(c)),await e.wait(f),r.mounted.forEach(t=>t(e))});M.meta={package:"@milkdown/plugin-listener",displayName:"Listener"};export{p as ListenerManager,E as key,M as listener,h as listenerCtx};
//# sourceMappingURL=plugin-listener.mjs.map