/* esm.sh - prosemirror-gapcursor@1.4.1 */
import{keydownHandler as v}from"../../../prosemirror-keymap@^1.0.0?deps=prosemirror-model@1.25.12&target=es2022";import{Selection as c,NodeSelection as m,TextSelection as C,Plugin as w}from"../../../prosemirror-state@^1.0.0?deps=prosemirror-model@1.25.12&target=es2022";import{Slice as x,Fragment as h}from"../../../prosemirror-model@1.25.12/es2022/prosemirror-model.mjs";import{DecorationSet as S,Decoration as A}from"../../../prosemirror-view@^1.0.0?deps=prosemirror-model@1.25.12&target=es2022";var l=class t extends c{constructor(e){super(e,e)}map(e,r){let o=e.resolve(r.map(this.head));return t.valid(o)?new t(o):c.near(o)}content(){return x.empty}eq(e){return e instanceof t&&e.head==this.head}toJSON(){return{type:"gapcursor",pos:this.head}}static fromJSON(e,r){if(typeof r.pos!="number")throw new RangeError("Invalid input for GapCursor.fromJSON");return new t(e.resolve(r.pos))}getBookmark(){return new d(this.anchor)}static valid(e){let r=e.parent;if(r.inlineContent||!y(e)||!b(e))return!1;let o=r.type.spec.allowGapCursor;if(o!=null)return o;let n=r.contentMatchAt(e.index()).defaultType;return n&&n.isTextblock}static findGapCursorFrom(e,r,o=!1){e:for(;;){if(!o&&t.valid(e))return e;let n=e.pos,s=null;for(let i=e.depth;;i--){let a=e.node(i);if(r>0?e.indexAfter(i)<a.childCount:e.index(i)>0){s=a.child(r>0?e.indexAfter(i):e.index(i)-1);break}else if(i==0)return null;n+=r;let f=e.doc.resolve(n);if(t.valid(f))return f}for(;;){let i=r>0?s.firstChild:s.lastChild;if(!i){if(s.isAtom&&!s.isText&&!m.isSelectable(s)){e=e.doc.resolve(n+s.nodeSize*r),o=!1;continue e}break}s=i,n+=r;let a=e.doc.resolve(n);if(t.valid(a))return a}return null}}};l.prototype.visible=!1;l.findFrom=l.findGapCursorFrom;c.jsonID("gapcursor",l);var d=class t{constructor(e){this.pos=e}map(e){return new t(e.map(this.pos))}resolve(e){let r=e.resolve(this.pos);return l.valid(r)?new l(r):c.near(r)}};function g(t){return t.isAtom||t.spec.isolating||t.spec.createGapCursor}function y(t){for(let e=t.depth;e>=0;e--){let r=t.index(e),o=t.node(e);if(r==0){if(o.type.spec.isolating)return!0;continue}for(let n=o.child(r-1);;n=n.lastChild){if(n.childCount==0&&!n.inlineContent||g(n.type))return!0;if(n.inlineContent)return!1}}return!0}function b(t){for(let e=t.depth;e>=0;e--){let r=t.indexAfter(e),o=t.node(e);if(r==o.childCount){if(o.type.spec.isolating)return!0;continue}for(let n=o.child(r);;n=n.firstChild){if(n.childCount==0&&!n.inlineContent||g(n.type))return!0;if(n.inlineContent)return!1}}return!0}function z(){return new w({props:{decorations:F,createSelectionBetween(t,e,r){return e.pos==r.pos&&l.valid(r)?new l(r):null},handleClick:T,handleKeyDown:k,handleDOMEvents:{beforeinput:D}}})}var k=v({ArrowLeft:u("horiz",-1),ArrowRight:u("horiz",1),ArrowUp:u("vert",-1),ArrowDown:u("vert",1)});function u(t,e){let r=t=="vert"?e>0?"down":"up":e>0?"right":"left";return function(o,n,s){let i=o.selection,a=e>0?i.$to:i.$from,f=i.empty;if(i instanceof C){if(!s.endOfTextblock(r)||a.depth==0)return!1;f=!1,a=o.doc.resolve(e>0?a.after():a.before())}let p=l.findGapCursorFrom(a,e,f);return p?(n&&n(o.tr.setSelection(new l(p))),!0):!1}}function T(t,e,r){if(!t||!t.editable)return!1;let o=t.state.doc.resolve(e);if(!l.valid(o))return!1;let n=t.posAtCoords({left:r.clientX,top:r.clientY});return n&&n.inside>-1&&m.isSelectable(t.state.doc.nodeAt(n.inside))?!1:(t.dispatch(t.state.tr.setSelection(new l(o))),!0)}function D(t,e){if(e.inputType!="insertCompositionText"||!(t.state.selection instanceof l))return!1;let{$from:r}=t.state.selection,o=r.parent.contentMatchAt(r.index()).findWrapping(t.state.schema.nodes.text);if(!o)return!1;let n=h.empty;for(let i=o.length-1;i>=0;i--)n=h.from(o[i].createAndFill(null,n));let s=t.state.tr.replace(r.pos,r.pos,new x(n,0,0));return s.setSelection(C.near(s.doc.resolve(r.pos+1))),t.dispatch(s),!1}function F(t){if(!(t.selection instanceof l))return null;let e=document.createElement("div");return e.className="ProseMirror-gapcursor",S.create(t.doc,[A.widget(t.selection.head,e,{key:"gapcursor"})])}export{l as GapCursor,z as gapCursor};
//# sourceMappingURL=prosemirror-gapcursor.mjs.map