3 lines
21 KiB
JavaScript
3 lines
21 KiB
JavaScript
import{getPanel as R,EditorView as y,showPanel as te,ViewPlugin as re,Decoration as f,runScopeHandlers as be}from"@codemirror/view";import{codePointAt as Ce,fromCodePoint as ke,codePointSize as Le,StateEffect as W,EditorSelection as p,EditorState as We,CharCategory as m,findClusterBreak as ne,Prec as Ae,StateField as ie,Facet as se,combineConfig as le,RangeSetBuilder as Ee}from"@codemirror/state";function h(){var t=arguments[0];typeof t=="string"&&(t=document.createElement(t));var e=1,r=arguments[1];if(r&&typeof r=="object"&&r.nodeType==null&&!Array.isArray(r)){for(var n in r)if(Object.prototype.hasOwnProperty.call(r,n)){var i=r[n];typeof i=="string"?t.setAttribute(n,i):i!=null&&(t[n]=i)}e++}for(;e<arguments.length;e++)oe(t,arguments[e]);return t}function oe(t,e){if(typeof e=="string")t.appendChild(document.createTextNode(e));else if(e!=null)if(e.nodeType!=null)t.appendChild(e);else if(Array.isArray(e))for(var r=0;r<e.length;r++)oe(t,e[r]);else throw new RangeError("Unsupported child node: "+e)}const ce=typeof String.prototype.normalize=="function"?t=>t.normalize("NFKD"):t=>t;class S{constructor(e,r,n=0,i=e.length,s,o){this.test=o,this.value={from:0,to:0},this.done=!1,this.matches=[],this.buffer="",this.bufferPos=0,this.iter=e.iterRange(n,i),this.bufferStart=n,this.normalize=s?l=>s(ce(l)):ce,this.query=this.normalize(r)}peek(){if(this.bufferPos==this.buffer.length){if(this.bufferStart+=this.buffer.length,this.iter.next(),this.iter.done)return-1;this.bufferPos=0,this.buffer=this.iter.value}return Ce(this.buffer,this.bufferPos)}next(){for(;this.matches.length;)this.matches.pop();return this.nextOverlapping()}nextOverlapping(){for(;;){let e=this.peek();if(e<0)return this.done=!0,this;let r=ke(e),n=this.bufferStart+this.bufferPos;this.bufferPos+=Le(e);let i=this.normalize(r);for(let s=0,o=n;;s++){let l=i.charCodeAt(s),c=this.match(l,o,this.bufferPos+this.bufferStart);if(s==i.length-1){if(c)return this.value=c,this;break}o==n&&s<r.length&&r.charCodeAt(s)==l&&o++}}}match(e,r,n){let i=null;for(let s=0;s<this.matches.length;s+=2){let o=this.matches[s],l=!1;this.query.charCodeAt(o)==e&&(o==this.query.length-1?i={from:this.matches[s+1],to:n}:(this.matches[s]++,l=!0)),l||(this.matches.splice(s,2),s-=2)}return this.query.charCodeAt(0)==e&&(this.query.length==1?i={from:r,to:n}:this.matches.push(1,r)),i&&this.test&&!this.test(i.from,i.to,this.buffer,this.bufferStart)&&(i=null),i}}typeof Symbol<"u"&&(S.prototype[Symbol.iterator]=function(){return this});const ae={from:-1,to:-1,match:/.*/.exec("")},_="gm"+(/x/.unicode==null?"":"u");class z{constructor(e,r,n,i=0,s=e.length){if(this.text=e,this.to=s,this.curLine="",this.done=!1,this.value=ae,/\\[sWDnr]|\n|\r|\[\^/.test(r))return new he(e,r,n,i,s);this.re=new RegExp(r,_+(n?.ignoreCase?"i":"")),this.test=n?.test,this.iter=e.iter();let o=e.lineAt(i);this.curLineStart=o.from,this.matchPos=P(e,i),this.getLine(this.curLineStart)}getLine(e){this.iter.next(e),this.iter.lineBreak?this.curLine="":(this.curLine=this.iter.value,this.curLineStart+this.curLine.length>this.to&&(this.curLine=this.curLine.slice(0,this.to-this.curLineStart)),this.iter.next())}nextLine(){this.curLineStart=this.curLineStart+this.curLine.length+1,this.curLineStart>this.to?this.curLine="":this.getLine(0)}next(){for(let e=this.matchPos-this.curLineStart;;){this.re.lastIndex=e;let r=this.matchPos<=this.to&&this.re.exec(this.curLine);if(r){let n=this.curLineStart+r.index,i=n+r[0].length;if(this.matchPos=P(this.text,i+(n==i?1:0)),n==this.curLineStart+this.curLine.length&&this.nextLine(),(n<i||n>this.value.to)&&(!this.test||this.test(n,i,r)))return this.value={from:n,to:i,match:r},this;e=this.matchPos-this.curLineStart}else if(this.curLineStart+this.curLine.length<this.to)this.nextLine(),e=0;else return this.done=!0,this}}}const N=new WeakMap;class k{constructor(e,r){this.from=e,this.text=r}get to(){return this.from+this.text.length}static get(e,r,n){let i=N.get(e);if(!i||i.from>=n||i.to<=r){let l=new k(r,e.sliceString(r,n));return N.set(e,l),l}if(i.from==r&&i.to==n)return i;let{text:s,from:o}=i;return o>r&&(s=e.sliceString(r,o)+s,o=r),i.to<n&&(s+=e.sliceString(i.to,n)),N.set(e,new k(o,s)),new k(r,s.slice(r-o,n-o))}}class he{constructor(e,r,n,i,s){this.text=e,this.to=s,this.done=!1,this.value=ae,this.matchPos=P(e,i),this.re=new RegExp(r,_+(n?.ignoreCase?"i":"")),this.test=n?.test,this.flat=k.get(e,i,this.chunkEnd(i+5e3))}chunkEnd(e){return e>=this.to?this.to:this.text.lineAt(e).to}next(){for(;;){let e=this.re.lastIndex=this.matchPos-this.flat.from,r=this.re.exec(this.flat.text);if(r&&!r[0]&&r.index==e&&(this.re.lastIndex=e+1,r=this.re.exec(this.flat.text)),r){let n=this.flat.from+r.index,i=n+r[0].length;if((this.flat.to>=this.to||r.index+r[0].length<=this.flat.text.length-10)&&(!this.test||this.test(n,i,r)))return this.value={from:n,to:i,match:r},this.matchPos=P(this.text,i+(n==i?1:0)),this}if(this.flat.to==this.to)return this.done=!0,this;this.flat=k.get(this.text,this.flat.from,this.chunkEnd(this.flat.from+this.flat.text.length*2))}}}typeof Symbol<"u"&&(z.prototype[Symbol.iterator]=he.prototype[Symbol.iterator]=function(){return this});function Fe(t){try{return new RegExp(t,_),!0}catch{return!1}}function P(t,e){if(e>=t.length)return e;let r=t.lineAt(e),n;for(;e<r.to&&(n=r.text.charCodeAt(e-r.from))>=56320&&n<57344;)e++;return e}function B(t){let e=String(t.state.doc.lineAt(t.state.selection.main.head).number),r=h("input",{class:"cm-textfield",name:"line",value:e}),n=h("form",{class:"cm-gotoLine",onkeydown:s=>{s.keyCode==27?(s.preventDefault(),t.dispatch({effects:q.of(!1)}),t.focus()):s.keyCode==13&&(s.preventDefault(),i())},onsubmit:s=>{s.preventDefault(),i()}},h("label",t.state.phrase("Go to line"),": ",r)," ",h("button",{class:"cm-button",type:"submit"},t.state.phrase("go")));function i(){let s=/^([+-])?(\d+)?(:\d+)?(%)?$/.exec(r.value);if(!s)return;let{state:o}=t,l=o.doc.lineAt(o.selection.main.head),[,c,a,u,x]=s,D=u?+u.slice(1):0,L=a?+a:l.number;if(a&&x){let Q=L/100;c&&(Q=Q*(c=="-"?-1:1)+l.number/o.doc.lines),L=Math.round(o.doc.lines*Q)}else a&&c&&(L=L*(c=="-"?-1:1)+l.number);let Z=o.doc.line(Math.max(1,Math.min(o.doc.lines,L))),ee=p.cursor(Z.from+Math.max(0,Math.min(D,Z.length)));t.dispatch({effects:[q.of(!1),y.scrollIntoView(ee.from,{y:"center"})],selection:ee}),t.focus()}return{dom:n}}const q=W.define(),ue=ie.define({create(){return!0},update(t,e){for(let r of e.effects)r.is(q)&&(t=r.value);return t},provide:t=>te.from(t,e=>e?B:null)}),fe=t=>{let e=R(t,B);if(!e){let r=[q.of(!0)];t.state.field(ue,!1)==null&&r.push(W.appendConfig.of([ue,De])),t.dispatch({effects:r}),e=R(t,B)}return e&&e.dom.querySelector("input").select(),!0},De=y.baseTheme({".cm-panel.cm-gotoLine":{padding:"2px 6px 4px","& label":{fontSize:"80%"}}}),Re={highlightWordAroundCursor:!1,minSelectionLength:1,maxMatches:100,wholeWords:!1},de=se.define({combine(t){return le(t,Re,{highlightWordAroundCursor:(e,r)=>e||r,minSelectionLength:Math.min,maxMatches:Math.min})}});function Pe(t){let e=[Oe,Ie];return t&&e.push(de.of(t)),e}const qe=f.mark({class:"cm-selectionMatch"}),Te=f.mark({class:"cm-selectionMatch cm-selectionMatch-main"});function me(t,e,r,n){return(r==0||t(e.sliceDoc(r-1,r))!=m.Word)&&(n==e.doc.length||t(e.sliceDoc(n,n+1))!=m.Word)}function we(t,e,r,n){return t(e.sliceDoc(r,r+1))==m.Word&&t(e.sliceDoc(n-1,n))==m.Word}const Ie=re.fromClass(class{constructor(t){this.decorations=this.getDeco(t)}update(t){(t.selectionSet||t.docChanged||t.viewportChanged)&&(this.decorations=this.getDeco(t.view))}getDeco(t){let e=t.state.facet(de),{state:r}=t,n=r.selection;if(n.ranges.length>1)return f.none;let i=n.main,s,o=null;if(i.empty){if(!e.highlightWordAroundCursor)return f.none;let c=r.wordAt(i.head);if(!c)return f.none;o=r.charCategorizer(i.head),s=r.sliceDoc(c.from,c.to)}else{let c=i.to-i.from;if(c<e.minSelectionLength||c>200)return f.none;if(e.wholeWords){if(s=r.sliceDoc(i.from,i.to),o=r.charCategorizer(i.head),!(me(o,r,i.from,i.to)&&we(o,r,i.from,i.to)))return f.none}else if(s=r.sliceDoc(i.from,i.to),!s)return f.none}let l=[];for(let c of t.visibleRanges){let a=new S(r.doc,s,c.from,c.to);for(;!a.next().done;){let{from:u,to:x}=a.value;if((!o||me(o,r,u,x))&&(i.empty&&u<=i.from&&x>=i.to?l.push(Te.range(u,x)):(u>=i.to||x<=i.from)&&l.push(qe.range(u,x)),l.length>e.maxMatches))return f.none}}return f.set(l)}},{decorations:t=>t.decorations}),Oe=y.baseTheme({".cm-selectionMatch":{backgroundColor:"#99ff7780"},".cm-searchMatch .cm-selectionMatch":{backgroundColor:"transparent"}}),$e=({state:t,dispatch:e})=>{let{selection:r}=t,n=p.create(r.ranges.map(i=>t.wordAt(i.head)||p.cursor(i.head)),r.mainIndex);return n.eq(r)?!1:(e(t.update({selection:n})),!0)};function Qe(t,e){let{main:r,ranges:n}=t.selection,i=t.wordAt(r.head),s=i&&i.from==r.from&&i.to==r.to;for(let o=!1,l=new S(t.doc,e,n[n.length-1].to);;)if(l.next(),l.done){if(o)return null;l=new S(t.doc,e,0,Math.max(0,n[n.length-1].from-1)),o=!0}else{if(o&&n.some(c=>c.from==l.value.from))continue;if(s){let c=t.wordAt(l.value.from);if(!c||c.from!=l.value.from||c.to!=l.value.to)continue}return l.value}}const pe=({state:t,dispatch:e})=>{let{ranges:r}=t.selection;if(r.some(s=>s.from===s.to))return $e({state:t,dispatch:e});let n=t.sliceDoc(r[0].from,r[0].to);if(t.selection.ranges.some(s=>t.sliceDoc(s.from,s.to)!=n))return!1;let i=Qe(t,n);return i?(e(t.update({selection:t.selection.addRange(p.range(i.from,i.to),!1),effects:y.scrollIntoView(i.to)})),!0):!1},M=se.define({combine(t){return le(t,{top:!1,caseSensitive:!1,literal:!1,regexp:!1,wholeWord:!1,createPanel:e=>new Xe(e),scrollToMatch:e=>y.scrollIntoView(e)})}});function _e(t){return t?[M.of(t),Y]:Y}class V{constructor(e){this.search=e.search,this.caseSensitive=!!e.caseSensitive,this.literal=!!e.literal,this.regexp=!!e.regexp,this.replace=e.replace||"",this.valid=!!this.search&&(!this.regexp||Fe(this.search)),this.unquoted=this.unquote(this.search),this.wholeWord=!!e.wholeWord}unquote(e){return this.literal?e:e.replace(/\\([nrt\\])/g,(r,n)=>n=="n"?`
|
|
`:n=="r"?"\r":n=="t"?" ":"\\")}eq(e){return this.search==e.search&&this.replace==e.replace&&this.caseSensitive==e.caseSensitive&&this.regexp==e.regexp&&this.wholeWord==e.wholeWord}create(){return this.regexp?new Ve(this):new Ne(this)}getCursor(e,r=0,n){let i=e.doc?e:We.create({doc:e});return n==null&&(n=i.doc.length),this.regexp?b(this,i,r,n):v(this,i,r,n)}}class ge{constructor(e){this.spec=e}}function v(t,e,r,n){return new S(e.doc,t.unquoted,r,n,t.caseSensitive?void 0:i=>i.toLowerCase(),t.wholeWord?ze(e.doc,e.charCategorizer(e.selection.main.head)):void 0)}function ze(t,e){return(r,n,i,s)=>((s>r||s+i.length<n)&&(s=Math.max(0,r-2),i=t.sliceString(s,Math.min(t.length,n+2))),(e(T(i,r-s))!=m.Word||e(w(i,r-s))!=m.Word)&&(e(w(i,n-s))!=m.Word||e(T(i,n-s))!=m.Word))}class Ne extends ge{constructor(e){super(e)}nextMatch(e,r,n){let i=v(this.spec,e,n,e.doc.length).nextOverlapping();return i.done&&(i=v(this.spec,e,0,r).nextOverlapping()),i.done?null:i.value}prevMatchInRange(e,r,n){for(let i=n;;){let s=Math.max(r,i-1e4-this.spec.unquoted.length),o=v(this.spec,e,s,i),l=null;for(;!o.nextOverlapping().done;)l=o.value;if(l)return l;if(s==r)return null;i-=1e4}}prevMatch(e,r,n){return this.prevMatchInRange(e,0,r)||this.prevMatchInRange(e,n,e.doc.length)}getReplacement(e){return this.spec.unquote(this.spec.replace)}matchAll(e,r){let n=v(this.spec,e,0,e.doc.length),i=[];for(;!n.next().done;){if(i.length>=r)return null;i.push(n.value)}return i}highlight(e,r,n,i){let s=v(this.spec,e,Math.max(0,r-this.spec.unquoted.length),Math.min(n+this.spec.unquoted.length,e.doc.length));for(;!s.next().done;)i(s.value.from,s.value.to)}}function b(t,e,r,n){return new z(e.doc,t.search,{ignoreCase:!t.caseSensitive,test:t.wholeWord?Be(e.charCategorizer(e.selection.main.head)):void 0},r,n)}function T(t,e){return t.slice(ne(t,e,!1),e)}function w(t,e){return t.slice(e,ne(t,e))}function Be(t){return(e,r,n)=>!n[0].length||(t(T(n.input,n.index))!=m.Word||t(w(n.input,n.index))!=m.Word)&&(t(w(n.input,n.index+n[0].length))!=m.Word||t(T(n.input,n.index+n[0].length))!=m.Word)}class Ve extends ge{nextMatch(e,r,n){let i=b(this.spec,e,n,e.doc.length).next();return i.done&&(i=b(this.spec,e,0,r).next()),i.done?null:i.value}prevMatchInRange(e,r,n){for(let i=1;;i++){let s=Math.max(r,n-i*1e4),o=b(this.spec,e,s,n),l=null;for(;!o.next().done;)l=o.value;if(l&&(s==r||l.from>s+10))return l;if(s==r)return null}}prevMatch(e,r,n){return this.prevMatchInRange(e,0,r)||this.prevMatchInRange(e,n,e.doc.length)}getReplacement(e){return this.spec.unquote(this.spec.replace).replace(/\$([$&\d+])/g,(r,n)=>n=="$"?"$":n=="&"?e.match[0]:n!="0"&&+n<e.match.length?e.match[n]:r)}matchAll(e,r){let n=b(this.spec,e,0,e.doc.length),i=[];for(;!n.next().done;){if(i.length>=r)return null;i.push(n.value)}return i}highlight(e,r,n,i){let s=b(this.spec,e,Math.max(0,r-250),Math.min(n+250,e.doc.length));for(;!s.next().done;)i(s.value.from,s.value.to)}}const C=W.define(),H=W.define(),g=ie.define({create(t){return new K(I(t).create(),null)},update(t,e){for(let r of e.effects)r.is(C)?t=new K(r.value.create(),t.panel):r.is(H)&&(t=new K(t.query,r.value?G:null));return t},provide:t=>te.from(t,e=>e.panel)});function He(t){let e=t.field(g,!1);return e?e.query.spec:I(t)}function Ke(t){var e;return((e=t.field(g,!1))===null||e===void 0?void 0:e.panel)!=null}class K{constructor(e,r){this.query=e,this.panel=r}}const je=f.mark({class:"cm-searchMatch"}),Ge=f.mark({class:"cm-searchMatch cm-searchMatch-selected"}),Ue=re.fromClass(class{constructor(t){this.view=t,this.decorations=this.highlight(t.state.field(g))}update(t){let e=t.state.field(g);(e!=t.startState.field(g)||t.docChanged||t.selectionSet||t.viewportChanged)&&(this.decorations=this.highlight(e))}highlight({query:t,panel:e}){if(!e||!t.spec.valid)return f.none;let{view:r}=this,n=new Ee;for(let i=0,s=r.visibleRanges,o=s.length;i<o;i++){let{from:l,to:c}=s[i];for(;i<o-1&&c>s[i+1].from-2*250;)c=s[++i].to;t.highlight(r.state,l,c,(a,u)=>{let x=r.state.selection.ranges.some(D=>D.from==a&&D.to==u);n.add(a,u,x?Ge:je)})}return n.finish()}},{decorations:t=>t.decorations});function A(t){return e=>{let r=e.state.field(g,!1);return r&&r.query.spec.valid?t(e,r):U(e)}}const E=A((t,{query:e})=>{let{to:r}=t.state.selection.main,n=e.nextMatch(t.state,r,r);if(!n)return!1;let i=p.single(n.from,n.to),s=t.state.facet(M);return t.dispatch({selection:i,effects:[X(t,n),s.scrollToMatch(i.main,t)],userEvent:"select.search"}),ve(t),!0}),F=A((t,{query:e})=>{let{state:r}=t,{from:n}=r.selection.main,i=e.prevMatch(r,n,n);if(!i)return!1;let s=p.single(i.from,i.to),o=t.state.facet(M);return t.dispatch({selection:s,effects:[X(t,i),o.scrollToMatch(s.main,t)],userEvent:"select.search"}),ve(t),!0}),xe=A((t,{query:e})=>{let r=e.matchAll(t.state,1e3);return!r||!r.length?!1:(t.dispatch({selection:p.create(r.map(n=>p.range(n.from,n.to))),userEvent:"select.search.matches"}),!0)}),ye=({state:t,dispatch:e})=>{let r=t.selection;if(r.ranges.length>1||r.main.empty)return!1;let{from:n,to:i}=r.main,s=[],o=0;for(let l=new S(t.doc,t.sliceDoc(n,i));!l.next().done;){if(s.length>1e3)return!1;l.value.from==n&&(o=s.length),s.push(p.range(l.value.from,l.value.to))}return e(t.update({selection:p.create(s,o),userEvent:"select.search.matches"})),!0},j=A((t,{query:e})=>{let{state:r}=t,{from:n,to:i}=r.selection.main;if(r.readOnly)return!1;let s=e.nextMatch(r,n,n);if(!s)return!1;let o=[],l,c,a=[];if(s.from==n&&s.to==i&&(c=r.toText(e.getReplacement(s)),o.push({from:s.from,to:s.to,insert:c}),s=e.nextMatch(r,s.from,s.to),a.push(y.announce.of(r.phrase("replaced match on line $",r.doc.lineAt(n).number)+"."))),s){let u=o.length==0||o[0].from>=s.to?0:s.to-s.from-c.length;l=p.single(s.from-u,s.to-u),a.push(X(t,s)),a.push(r.facet(M).scrollToMatch(l.main,t))}return t.dispatch({changes:o,selection:l,effects:a,userEvent:"input.replace"}),!0}),Se=A((t,{query:e})=>{if(t.state.readOnly)return!1;let r=e.matchAll(t.state,1e9).map(i=>{let{from:s,to:o}=i;return{from:s,to:o,insert:e.getReplacement(i)}});if(!r.length)return!1;let n=t.state.phrase("replaced $ matches",r.length)+".";return t.dispatch({changes:r,effects:y.announce.of(n),userEvent:"input.replace.all"}),!0});function G(t){return t.state.facet(M).createPanel(t)}function I(t,e){var r,n,i,s,o;let l=t.selection.main,c=l.empty||l.to>l.from+100?"":t.sliceDoc(l.from,l.to);if(e&&!c)return e;let a=t.facet(M);return new V({search:((r=e?.literal)!==null&&r!==void 0?r:a.literal)?c:c.replace(/\n/g,"\\n"),caseSensitive:(n=e?.caseSensitive)!==null&&n!==void 0?n:a.caseSensitive,literal:(i=e?.literal)!==null&&i!==void 0?i:a.literal,regexp:(s=e?.regexp)!==null&&s!==void 0?s:a.regexp,wholeWord:(o=e?.wholeWord)!==null&&o!==void 0?o:a.wholeWord})}function Me(t){let e=R(t,G);return e&&e.dom.querySelector("[main-field]")}function ve(t){let e=Me(t);e&&e==t.root.activeElement&&e.select()}const U=t=>{let e=t.state.field(g,!1);if(e&&e.panel){let r=Me(t);if(r&&r!=t.root.activeElement){let n=I(t.state,e.query.spec);n.valid&&t.dispatch({effects:C.of(n)}),r.focus(),r.select()}}else t.dispatch({effects:[H.of(!0),e?C.of(I(t.state,e.query.spec)):W.appendConfig.of(Y)]});return!0},J=t=>{let e=t.state.field(g,!1);if(!e||!e.panel)return!1;let r=R(t,G);return r&&r.dom.contains(t.root.activeElement)&&t.focus(),t.dispatch({effects:H.of(!1)}),!0},Je=[{key:"Mod-f",run:U,scope:"editor search-panel"},{key:"F3",run:E,shift:F,scope:"editor search-panel",preventDefault:!0},{key:"Mod-g",run:E,shift:F,scope:"editor search-panel",preventDefault:!0},{key:"Escape",run:J,scope:"editor search-panel"},{key:"Mod-Shift-l",run:ye},{key:"Mod-Alt-g",run:fe},{key:"Mod-d",run:pe,preventDefault:!0}];class Xe{constructor(e){this.view=e;let r=this.query=e.state.field(g).query.spec;this.commit=this.commit.bind(this),this.searchField=h("input",{value:r.search,placeholder:d(e,"Find"),"aria-label":d(e,"Find"),class:"cm-textfield",name:"search",form:"","main-field":"true",onchange:this.commit,onkeyup:this.commit}),this.replaceField=h("input",{value:r.replace,placeholder:d(e,"Replace"),"aria-label":d(e,"Replace"),class:"cm-textfield",name:"replace",form:"",onchange:this.commit,onkeyup:this.commit}),this.caseField=h("input",{type:"checkbox",name:"case",form:"",checked:r.caseSensitive,onchange:this.commit}),this.reField=h("input",{type:"checkbox",name:"re",form:"",checked:r.regexp,onchange:this.commit}),this.wordField=h("input",{type:"checkbox",name:"word",form:"",checked:r.wholeWord,onchange:this.commit});function n(i,s,o){return h("button",{class:"cm-button",name:i,onclick:s,type:"button"},o)}this.dom=h("div",{onkeydown:i=>this.keydown(i),class:"cm-search"},[this.searchField,n("next",()=>E(e),[d(e,"next")]),n("prev",()=>F(e),[d(e,"previous")]),n("select",()=>xe(e),[d(e,"all")]),h("label",null,[this.caseField,d(e,"match case")]),h("label",null,[this.reField,d(e,"regexp")]),h("label",null,[this.wordField,d(e,"by word")]),...e.state.readOnly?[]:[h("br"),this.replaceField,n("replace",()=>j(e),[d(e,"replace")]),n("replaceAll",()=>Se(e),[d(e,"replace all")])],h("button",{name:"close",onclick:()=>J(e),"aria-label":d(e,"close"),type:"button"},["\xD7"])])}commit(){let e=new V({search:this.searchField.value,caseSensitive:this.caseField.checked,regexp:this.reField.checked,wholeWord:this.wordField.checked,replace:this.replaceField.value});e.eq(this.query)||(this.query=e,this.view.dispatch({effects:C.of(e)}))}keydown(e){be(this.view,e,"search-panel")?e.preventDefault():e.keyCode==13&&e.target==this.searchField?(e.preventDefault(),(e.shiftKey?F:E)(this.view)):e.keyCode==13&&e.target==this.replaceField&&(e.preventDefault(),j(this.view))}update(e){for(let r of e.transactions)for(let n of r.effects)n.is(C)&&!n.value.eq(this.query)&&this.setQuery(n.value)}setQuery(e){this.query=e,this.searchField.value=e.search,this.replaceField.value=e.replace,this.caseField.checked=e.caseSensitive,this.reField.checked=e.regexp,this.wordField.checked=e.wholeWord}mount(){this.searchField.select()}get pos(){return 80}get top(){return this.view.state.facet(M).top}}function d(t,e){return t.state.phrase(e)}const O=30,$=/[\s\.,:;?!]/;function X(t,{from:e,to:r}){let n=t.state.doc.lineAt(e),i=t.state.doc.lineAt(r).to,s=Math.max(n.from,e-O),o=Math.min(i,r+O),l=t.state.sliceDoc(s,o);if(s!=n.from){for(let c=0;c<O;c++)if(!$.test(l[c+1])&&$.test(l[c])){l=l.slice(c);break}}if(o!=i){for(let c=l.length-1;c>l.length-O;c--)if(!$.test(l[c-1])&&$.test(l[c])){l=l.slice(0,c);break}}return y.announce.of(`${t.state.phrase("current match")}. ${l} ${t.state.phrase("on line")} ${n.number}.`)}const Ye=y.baseTheme({".cm-panel.cm-search":{padding:"2px 6px 4px",position:"relative","& [name=close]":{position:"absolute",top:"0",right:"4px",backgroundColor:"inherit",border:"none",font:"inherit",padding:0,margin:0},"& input, & button, & label":{margin:".2em .6em .2em 0"},"& input[type=checkbox]":{marginRight:".2em"},"& label":{fontSize:"80%",whiteSpace:"pre"}},"&light .cm-searchMatch":{backgroundColor:"#ffff0054"},"&dark .cm-searchMatch":{backgroundColor:"#00ffff8a"},"&light .cm-searchMatch-selected":{backgroundColor:"#ff6a0054"},"&dark .cm-searchMatch-selected":{backgroundColor:"#ff00ff8a"}}),Y=[g,Ae.low(Ue),Ye];export{z as RegExpCursor,S as SearchCursor,V as SearchQuery,J as closeSearchPanel,E as findNext,F as findPrevious,He as getSearchQuery,fe as gotoLine,Pe as highlightSelectionMatches,U as openSearchPanel,Se as replaceAll,j as replaceNext,_e as search,Je as searchKeymap,Ke as searchPanelOpen,xe as selectMatches,pe as selectNextOccurrence,ye as selectSelectionMatches,C as setSearchQuery};
|