Files
liceo-ariosto/media/vendor/codemirror/js/codemirror-search.min.js
2025-06-17 11:53:18 +02:00

1 line
24 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{getPanel,EditorView,showPanel,ViewPlugin,Decoration,runScopeHandlers}from"@codemirror/view";import{codePointAt,fromCodePoint,codePointSize,StateEffect,EditorSelection,EditorState,CharCategory,findClusterBreak,Prec,StateField,Facet,combineConfig,RangeSetBuilder}from"@codemirror/state";function crelt(){var e=arguments[0];"string"==typeof e&&(e=document.createElement(e));var t=1,r=arguments[1];if(r&&"object"==typeof r&&null==r.nodeType&&!Array.isArray(r)){for(var i in r)if(Object.prototype.hasOwnProperty.call(r,i)){var n=r[i];"string"==typeof n?e.setAttribute(i,n):null!=n&&(e[i]=n)}t++}for(;t<arguments.length;t++)add(e,arguments[t]);return e}function add(e,t){if("string"==typeof t)e.appendChild(document.createTextNode(t));else if(null==t);else if(null!=t.nodeType)e.appendChild(t);else{if(!Array.isArray(t))throw new RangeError("Unsupported child node: "+t);for(var r=0;r<t.length;r++)add(e,t[r])}}const basicNormalize="function"==typeof String.prototype.normalize?e=>e.normalize("NFKD"):e=>e;class SearchCursor{constructor(e,t,r=0,i=e.length,n,a){this.test=a,this.value={from:0,to:0},this.done=!1,this.matches=[],this.buffer="",this.bufferPos=0,this.iter=e.iterRange(r,i),this.bufferStart=r,this.normalize=n?e=>n(basicNormalize(e)):basicNormalize,this.query=this.normalize(t)}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 codePointAt(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 t=fromCodePoint(e),r=this.bufferStart+this.bufferPos;this.bufferPos+=codePointSize(e);let i=this.normalize(t);for(let e=0,n=r;;e++){let a=i.charCodeAt(e),o=this.match(a,n);if(e==i.length-1){if(o)return this.value=o,this;break}n==r&&e<t.length&&t.charCodeAt(e)==a&&n++}}}match(e,t){let r=null;for(let i=0;i<this.matches.length;i+=2){let n=this.matches[i],a=!1;this.query.charCodeAt(n)==e&&(n==this.query.length-1?r={from:this.matches[i+1],to:t+1}:(this.matches[i]++,a=!0)),a||(this.matches.splice(i,2),i-=2)}return this.query.charCodeAt(0)==e&&(1==this.query.length?r={from:t,to:t+1}:this.matches.push(1,t)),r&&this.test&&!this.test(r.from,r.to,this.buffer,this.bufferPos)&&(r=null),r}}"undefined"!=typeof Symbol&&(SearchCursor.prototype[Symbol.iterator]=function(){return this});const empty={from:-1,to:-1,match:/.*/.exec("")},baseFlags="gm"+(null==/x/.unicode?"":"u");class RegExpCursor{constructor(e,t,r,i=0,n=e.length){if(this.text=e,this.to=n,this.curLine="",this.done=!1,this.value=empty,/\\[sWDnr]|\n|\r|\[\^/.test(t))return new MultilineRegExpCursor(e,t,r,i,n);this.re=new RegExp(t,baseFlags+((null==r?void 0:r.ignoreCase)?"i":"")),this.test=null==r?void 0:r.test,this.iter=e.iter();let a=e.lineAt(i);this.curLineStart=a.from,this.matchPos=toCharEnd(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 t=this.matchPos<=this.to&&this.re.exec(this.curLine);if(t){let r=this.curLineStart+t.index,i=r+t[0].length;if(this.matchPos=toCharEnd(this.text,i+(r==i?1:0)),r==this.curLineStart+this.curLine.length&&this.nextLine(),(r<i||r>this.value.to)&&(!this.test||this.test(r,i,t)))return this.value={from:r,to:i,match:t},this;e=this.matchPos-this.curLineStart}else{if(!(this.curLineStart+this.curLine.length<this.to))return this.done=!0,this;this.nextLine(),e=0}}}}const flattened=new WeakMap;class FlattenedDoc{constructor(e,t){this.from=e,this.text=t}get to(){return this.from+this.text.length}static get(e,t,r){let i=flattened.get(e);if(!i||i.from>=r||i.to<=t){let i=new FlattenedDoc(t,e.sliceString(t,r));return flattened.set(e,i),i}if(i.from==t&&i.to==r)return i;let{text:n,from:a}=i;return a>t&&(n=e.sliceString(t,a)+n,a=t),i.to<r&&(n+=e.sliceString(i.to,r)),flattened.set(e,new FlattenedDoc(a,n)),new FlattenedDoc(t,n.slice(t-a,r-a))}}class MultilineRegExpCursor{constructor(e,t,r,i,n){this.text=e,this.to=n,this.done=!1,this.value=empty,this.matchPos=toCharEnd(e,i),this.re=new RegExp(t,baseFlags+((null==r?void 0:r.ignoreCase)?"i":"")),this.test=null==r?void 0:r.test,this.flat=FlattenedDoc.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,t=this.re.exec(this.flat.text);if(t&&!t[0]&&t.index==e&&(this.re.lastIndex=e+1,t=this.re.exec(this.flat.text)),t){let e=this.flat.from+t.index,r=e+t[0].length;if((this.flat.to>=this.to||t.index+t[0].length<=this.flat.text.length-10)&&(!this.test||this.test(e,r,t)))return this.value={from:e,to:r,match:t},this.matchPos=toCharEnd(this.text,r+(e==r?1:0)),this}if(this.flat.to==this.to)return this.done=!0,this;this.flat=FlattenedDoc.get(this.text,this.flat.from,this.chunkEnd(this.flat.from+2*this.flat.text.length))}}}function validRegExp(e){try{return new RegExp(e,baseFlags),!0}catch(e){return!1}}function toCharEnd(e,t){if(t>=e.length)return t;let r,i=e.lineAt(t);for(;t<i.to&&(r=i.text.charCodeAt(t-i.from))>=56320&&r<57344;)t++;return t}function createLineDialog(e){let t=crelt("input",{class:"cm-textfield",name:"line"});function r(){let r=/^([+-])?(\d+)?(:\d+)?(%)?$/.exec(t.value);if(!r)return;let{state:i}=e,n=i.doc.lineAt(i.selection.main.head),[,a,o,s,c]=r,l=s?+s.slice(1):0,h=o?+o:n.number;if(o&&c){let e=h/100;a&&(e=e*("-"==a?-1:1)+n.number/i.doc.lines),h=Math.round(i.doc.lines*e)}else o&&a&&(h=h*("-"==a?-1:1)+n.number);let u=i.doc.line(Math.max(1,Math.min(i.doc.lines,h))),f=EditorSelection.cursor(u.from+Math.max(0,Math.min(l,u.length)));e.dispatch({effects:[dialogEffect.of(!1),EditorView.scrollIntoView(f.from,{y:"center"})],selection:f}),e.focus()}return{dom:crelt("form",{class:"cm-gotoLine",onkeydown:t=>{27==t.keyCode?(t.preventDefault(),e.dispatch({effects:dialogEffect.of(!1)}),e.focus()):13==t.keyCode&&(t.preventDefault(),r())},onsubmit:e=>{e.preventDefault(),r()}},crelt("label",e.state.phrase("Go to line"),": ",t)," ",crelt("button",{class:"cm-button",type:"submit"},e.state.phrase("go")))}}"undefined"!=typeof Symbol&&(RegExpCursor.prototype[Symbol.iterator]=MultilineRegExpCursor.prototype[Symbol.iterator]=function(){return this});const dialogEffect=StateEffect.define(),dialogField=StateField.define({create:()=>!0,update(e,t){for(let r of t.effects)r.is(dialogEffect)&&(e=r.value);return e},provide:e=>showPanel.from(e,(e=>e?createLineDialog:null))}),gotoLine=e=>{let t=getPanel(e,createLineDialog);if(!t){let r=[dialogEffect.of(!0)];null==e.state.field(dialogField,!1)&&r.push(StateEffect.appendConfig.of([dialogField,baseTheme$1])),e.dispatch({effects:r}),t=getPanel(e,createLineDialog)}return t&&t.dom.querySelector("input").focus(),!0},baseTheme$1=EditorView.baseTheme({".cm-panel.cm-gotoLine":{padding:"2px 6px 4px","& label":{fontSize:"80%"}}}),defaultHighlightOptions={highlightWordAroundCursor:!1,minSelectionLength:1,maxMatches:100,wholeWords:!1},highlightConfig=Facet.define({combine:e=>combineConfig(e,defaultHighlightOptions,{highlightWordAroundCursor:(e,t)=>e||t,minSelectionLength:Math.min,maxMatches:Math.min})});function highlightSelectionMatches(e){let t=[defaultTheme,matchHighlighter];return e&&t.push(highlightConfig.of(e)),t}const matchDeco=Decoration.mark({class:"cm-selectionMatch"}),mainMatchDeco=Decoration.mark({class:"cm-selectionMatch cm-selectionMatch-main"});function insideWordBoundaries(e,t,r,i){return!(0!=r&&e(t.sliceDoc(r-1,r))==CharCategory.Word||i!=t.doc.length&&e(t.sliceDoc(i,i+1))==CharCategory.Word)}function insideWord(e,t,r,i){return e(t.sliceDoc(r,r+1))==CharCategory.Word&&e(t.sliceDoc(i-1,i))==CharCategory.Word}const matchHighlighter=ViewPlugin.fromClass(class{constructor(e){this.decorations=this.getDeco(e)}update(e){(e.selectionSet||e.docChanged||e.viewportChanged)&&(this.decorations=this.getDeco(e.view))}getDeco(e){let t=e.state.facet(highlightConfig),{state:r}=e,i=r.selection;if(i.ranges.length>1)return Decoration.none;let n,a=i.main,o=null;if(a.empty){if(!t.highlightWordAroundCursor)return Decoration.none;let e=r.wordAt(a.head);if(!e)return Decoration.none;o=r.charCategorizer(a.head),n=r.sliceDoc(e.from,e.to)}else{let e=a.to-a.from;if(e<t.minSelectionLength||e>200)return Decoration.none;if(t.wholeWords){if(n=r.sliceDoc(a.from,a.to),o=r.charCategorizer(a.head),!insideWordBoundaries(o,r,a.from,a.to)||!insideWord(o,r,a.from,a.to))return Decoration.none}else if(n=r.sliceDoc(a.from,a.to).trim(),!n)return Decoration.none}let s=[];for(let i of e.visibleRanges){let e=new SearchCursor(r.doc,n,i.from,i.to);for(;!e.next().done;){let{from:i,to:n}=e.value;if((!o||insideWordBoundaries(o,r,i,n))&&(a.empty&&i<=a.from&&n>=a.to?s.push(mainMatchDeco.range(i,n)):(i>=a.to||n<=a.from)&&s.push(matchDeco.range(i,n)),s.length>t.maxMatches))return Decoration.none}}return Decoration.set(s)}},{decorations:e=>e.decorations}),defaultTheme=EditorView.baseTheme({".cm-selectionMatch":{backgroundColor:"#99ff7780"},".cm-searchMatch .cm-selectionMatch":{backgroundColor:"transparent"}}),selectWord=({state:e,dispatch:t})=>{let{selection:r}=e,i=EditorSelection.create(r.ranges.map((t=>e.wordAt(t.head)||EditorSelection.cursor(t.head))),r.mainIndex);return!i.eq(r)&&(t(e.update({selection:i})),!0)};function findNextOccurrence(e,t){let{main:r,ranges:i}=e.selection,n=e.wordAt(r.head),a=n&&n.from==r.from&&n.to==r.to;for(let r=!1,n=new SearchCursor(e.doc,t,i[i.length-1].to);;){if(n.next(),!n.done){if(r&&i.some((e=>e.from==n.value.from)))continue;if(a){let t=e.wordAt(n.value.from);if(!t||t.from!=n.value.from||t.to!=n.value.to)continue}return n.value}if(r)return null;n=new SearchCursor(e.doc,t,0,Math.max(0,i[i.length-1].from-1)),r=!0}}const selectNextOccurrence=({state:e,dispatch:t})=>{let{ranges:r}=e.selection;if(r.some((e=>e.from===e.to)))return selectWord({state:e,dispatch:t});let i=e.sliceDoc(r[0].from,r[0].to);if(e.selection.ranges.some((t=>e.sliceDoc(t.from,t.to)!=i)))return!1;let n=findNextOccurrence(e,i);return!!n&&(t(e.update({selection:e.selection.addRange(EditorSelection.range(n.from,n.to),!1),effects:EditorView.scrollIntoView(n.to)})),!0)},searchConfigFacet=Facet.define({combine:e=>combineConfig(e,{top:!1,caseSensitive:!1,literal:!1,regexp:!1,wholeWord:!1,createPanel:e=>new SearchPanel(e),scrollToMatch:e=>EditorView.scrollIntoView(e)})});function search(e){return e?[searchConfigFacet.of(e),searchExtensions]:searchExtensions}class SearchQuery{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||validRegExp(this.search)),this.unquoted=this.unquote(this.search),this.wholeWord=!!e.wholeWord}unquote(e){return this.literal?e:e.replace(/\\([nrt\\])/g,((e,t)=>"n"==t?"\n":"r"==t?"\r":"t"==t?"\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 RegExpQuery(this):new StringQuery(this)}getCursor(e,t=0,r){let i=e.doc?e:EditorState.create({doc:e});return null==r&&(r=i.doc.length),this.regexp?regexpCursor(this,i,t,r):stringCursor(this,i,t,r)}}class QueryType{constructor(e){this.spec=e}}function stringCursor(e,t,r,i){return new SearchCursor(t.doc,e.unquoted,r,i,e.caseSensitive?void 0:e=>e.toLowerCase(),e.wholeWord?stringWordTest(t.doc,t.charCategorizer(t.selection.main.head)):void 0)}function stringWordTest(e,t){return(r,i,n,a)=>((a>r||a+n.length<i)&&(a=Math.max(0,r-2),n=e.sliceString(a,Math.min(e.length,i+2))),!(t(charBefore(n,r-a))==CharCategory.Word&&t(charAfter(n,r-a))==CharCategory.Word||t(charAfter(n,i-a))==CharCategory.Word&&t(charBefore(n,i-a))==CharCategory.Word))}class StringQuery extends QueryType{constructor(e){super(e)}nextMatch(e,t,r){let i=stringCursor(this.spec,e,r,e.doc.length).nextOverlapping();return i.done&&(i=stringCursor(this.spec,e,0,t).nextOverlapping()),i.done?null:i.value}prevMatchInRange(e,t,r){for(let i=r;;){let r=Math.max(t,i-1e4-this.spec.unquoted.length),n=stringCursor(this.spec,e,r,i),a=null;for(;!n.nextOverlapping().done;)a=n.value;if(a)return a;if(r==t)return null;i-=1e4}}prevMatch(e,t,r){return this.prevMatchInRange(e,0,t)||this.prevMatchInRange(e,r,e.doc.length)}getReplacement(e){return this.spec.unquote(this.spec.replace)}matchAll(e,t){let r=stringCursor(this.spec,e,0,e.doc.length),i=[];for(;!r.next().done;){if(i.length>=t)return null;i.push(r.value)}return i}highlight(e,t,r,i){let n=stringCursor(this.spec,e,Math.max(0,t-this.spec.unquoted.length),Math.min(r+this.spec.unquoted.length,e.doc.length));for(;!n.next().done;)i(n.value.from,n.value.to)}}function regexpCursor(e,t,r,i){return new RegExpCursor(t.doc,e.search,{ignoreCase:!e.caseSensitive,test:e.wholeWord?regexpWordTest(t.charCategorizer(t.selection.main.head)):void 0},r,i)}function charBefore(e,t){return e.slice(findClusterBreak(e,t,!1),t)}function charAfter(e,t){return e.slice(t,findClusterBreak(e,t))}function regexpWordTest(e){return(t,r,i)=>!i[0].length||(e(charBefore(i.input,i.index))!=CharCategory.Word||e(charAfter(i.input,i.index))!=CharCategory.Word)&&(e(charAfter(i.input,i.index+i[0].length))!=CharCategory.Word||e(charBefore(i.input,i.index+i[0].length))!=CharCategory.Word)}class RegExpQuery extends QueryType{nextMatch(e,t,r){let i=regexpCursor(this.spec,e,r,e.doc.length).next();return i.done&&(i=regexpCursor(this.spec,e,0,t).next()),i.done?null:i.value}prevMatchInRange(e,t,r){for(let i=1;;i++){let n=Math.max(t,r-1e4*i),a=regexpCursor(this.spec,e,n,r),o=null;for(;!a.next().done;)o=a.value;if(o&&(n==t||o.from>n+10))return o;if(n==t)return null}}prevMatch(e,t,r){return this.prevMatchInRange(e,0,t)||this.prevMatchInRange(e,r,e.doc.length)}getReplacement(e){return this.spec.unquote(this.spec.replace.replace(/\$([$&\d+])/g,((t,r)=>"$"==r?"$":"&"==r?e.match[0]:"0"!=r&&+r<e.match.length?e.match[r]:t)))}matchAll(e,t){let r=regexpCursor(this.spec,e,0,e.doc.length),i=[];for(;!r.next().done;){if(i.length>=t)return null;i.push(r.value)}return i}highlight(e,t,r,i){let n=regexpCursor(this.spec,e,Math.max(0,t-250),Math.min(r+250,e.doc.length));for(;!n.next().done;)i(n.value.from,n.value.to)}}const setSearchQuery=StateEffect.define(),togglePanel=StateEffect.define(),searchState=StateField.define({create:e=>new SearchState(defaultQuery(e).create(),null),update(e,t){for(let r of t.effects)r.is(setSearchQuery)?e=new SearchState(r.value.create(),e.panel):r.is(togglePanel)&&(e=new SearchState(e.query,r.value?createSearchPanel:null));return e},provide:e=>showPanel.from(e,(e=>e.panel))});function getSearchQuery(e){let t=e.field(searchState,!1);return t?t.query.spec:defaultQuery(e)}function searchPanelOpen(e){var t;return null!=(null===(t=e.field(searchState,!1))||void 0===t?void 0:t.panel)}class SearchState{constructor(e,t){this.query=e,this.panel=t}}const matchMark=Decoration.mark({class:"cm-searchMatch"}),selectedMatchMark=Decoration.mark({class:"cm-searchMatch cm-searchMatch-selected"}),searchHighlighter=ViewPlugin.fromClass(class{constructor(e){this.view=e,this.decorations=this.highlight(e.state.field(searchState))}update(e){let t=e.state.field(searchState);(t!=e.startState.field(searchState)||e.docChanged||e.selectionSet||e.viewportChanged)&&(this.decorations=this.highlight(t))}highlight({query:e,panel:t}){if(!t||!e.spec.valid)return Decoration.none;let{view:r}=this,i=new RangeSetBuilder;for(let t=0,n=r.visibleRanges,a=n.length;t<a;t++){let{from:o,to:s}=n[t];for(;t<a-1&&s>n[t+1].from-500;)s=n[++t].to;e.highlight(r.state,o,s,((e,t)=>{let n=r.state.selection.ranges.some((r=>r.from==e&&r.to==t));i.add(e,t,n?selectedMatchMark:matchMark)}))}return i.finish()}},{decorations:e=>e.decorations});function searchCommand(e){return t=>{let r=t.state.field(searchState,!1);return r&&r.query.spec.valid?e(t,r):openSearchPanel(t)}}const findNext=searchCommand(((e,{query:t})=>{let{to:r}=e.state.selection.main,i=t.nextMatch(e.state,r,r);if(!i)return!1;let n=EditorSelection.single(i.from,i.to),a=e.state.facet(searchConfigFacet);return e.dispatch({selection:n,effects:[announceMatch(e,i),a.scrollToMatch(n.main,e)],userEvent:"select.search"}),selectSearchInput(e),!0})),findPrevious=searchCommand(((e,{query:t})=>{let{state:r}=e,{from:i}=r.selection.main,n=t.prevMatch(r,i,i);if(!n)return!1;let a=EditorSelection.single(n.from,n.to),o=e.state.facet(searchConfigFacet);return e.dispatch({selection:a,effects:[announceMatch(e,n),o.scrollToMatch(a.main,e)],userEvent:"select.search"}),selectSearchInput(e),!0})),selectMatches=searchCommand(((e,{query:t})=>{let r=t.matchAll(e.state,1e3);return!(!r||!r.length)&&(e.dispatch({selection:EditorSelection.create(r.map((e=>EditorSelection.range(e.from,e.to)))),userEvent:"select.search.matches"}),!0)})),selectSelectionMatches=({state:e,dispatch:t})=>{let r=e.selection;if(r.ranges.length>1||r.main.empty)return!1;let{from:i,to:n}=r.main,a=[],o=0;for(let t=new SearchCursor(e.doc,e.sliceDoc(i,n));!t.next().done;){if(a.length>1e3)return!1;t.value.from==i&&(o=a.length),a.push(EditorSelection.range(t.value.from,t.value.to))}return t(e.update({selection:EditorSelection.create(a,o),userEvent:"select.search.matches"})),!0},replaceNext=searchCommand(((e,{query:t})=>{let{state:r}=e,{from:i,to:n}=r.selection.main;if(r.readOnly)return!1;let a=t.nextMatch(r,i,i);if(!a)return!1;let o,s,c=[],l=[];if(a.from==i&&a.to==n&&(s=r.toText(t.getReplacement(a)),c.push({from:a.from,to:a.to,insert:s}),a=t.nextMatch(r,a.from,a.to),l.push(EditorView.announce.of(r.phrase("replaced match on line $",r.doc.lineAt(i).number)+"."))),a){let t=0==c.length||c[0].from>=a.to?0:a.to-a.from-s.length;o=EditorSelection.single(a.from-t,a.to-t),l.push(announceMatch(e,a)),l.push(r.facet(searchConfigFacet).scrollToMatch(o.main,e))}return e.dispatch({changes:c,selection:o,effects:l,userEvent:"input.replace"}),!0})),replaceAll=searchCommand(((e,{query:t})=>{if(e.state.readOnly)return!1;let r=t.matchAll(e.state,1e9).map((e=>{let{from:r,to:i}=e;return{from:r,to:i,insert:t.getReplacement(e)}}));if(!r.length)return!1;let i=e.state.phrase("replaced $ matches",r.length)+".";return e.dispatch({changes:r,effects:EditorView.announce.of(i),userEvent:"input.replace.all"}),!0}));function createSearchPanel(e){return e.state.facet(searchConfigFacet).createPanel(e)}function defaultQuery(e,t){var r,i,n,a,o;let s=e.selection.main,c=s.empty||s.to>s.from+100?"":e.sliceDoc(s.from,s.to);if(t&&!c)return t;let l=e.facet(searchConfigFacet);return new SearchQuery({search:(null!==(r=null==t?void 0:t.literal)&&void 0!==r?r:l.literal)?c:c.replace(/\n/g,"\\n"),caseSensitive:null!==(i=null==t?void 0:t.caseSensitive)&&void 0!==i?i:l.caseSensitive,literal:null!==(n=null==t?void 0:t.literal)&&void 0!==n?n:l.literal,regexp:null!==(a=null==t?void 0:t.regexp)&&void 0!==a?a:l.regexp,wholeWord:null!==(o=null==t?void 0:t.wholeWord)&&void 0!==o?o:l.wholeWord})}function getSearchInput(e){let t=getPanel(e,createSearchPanel);return t&&t.dom.querySelector("[main-field]")}function selectSearchInput(e){let t=getSearchInput(e);t&&t==e.root.activeElement&&t.select()}const openSearchPanel=e=>{let t=e.state.field(searchState,!1);if(t&&t.panel){let r=getSearchInput(e);if(r&&r!=e.root.activeElement){let i=defaultQuery(e.state,t.query.spec);i.valid&&e.dispatch({effects:setSearchQuery.of(i)}),r.focus(),r.select()}}else e.dispatch({effects:[togglePanel.of(!0),t?setSearchQuery.of(defaultQuery(e.state,t.query.spec)):StateEffect.appendConfig.of(searchExtensions)]});return!0},closeSearchPanel=e=>{let t=e.state.field(searchState,!1);if(!t||!t.panel)return!1;let r=getPanel(e,createSearchPanel);return r&&r.dom.contains(e.root.activeElement)&&e.focus(),e.dispatch({effects:togglePanel.of(!1)}),!0},searchKeymap=[{key:"Mod-f",run:openSearchPanel,scope:"editor search-panel"},{key:"F3",run:findNext,shift:findPrevious,scope:"editor search-panel",preventDefault:!0},{key:"Mod-g",run:findNext,shift:findPrevious,scope:"editor search-panel",preventDefault:!0},{key:"Escape",run:closeSearchPanel,scope:"editor search-panel"},{key:"Mod-Shift-l",run:selectSelectionMatches},{key:"Alt-g",run:gotoLine},{key:"Mod-d",run:selectNextOccurrence,preventDefault:!0}];class SearchPanel{constructor(e){this.view=e;let t=this.query=e.state.field(searchState).query.spec;function r(e,t,r){return crelt("button",{class:"cm-button",name:e,onclick:t,type:"button"},r)}this.commit=this.commit.bind(this),this.searchField=crelt("input",{value:t.search,placeholder:phrase(e,"Find"),"aria-label":phrase(e,"Find"),class:"cm-textfield",name:"search",form:"","main-field":"true",onchange:this.commit,onkeyup:this.commit}),this.replaceField=crelt("input",{value:t.replace,placeholder:phrase(e,"Replace"),"aria-label":phrase(e,"Replace"),class:"cm-textfield",name:"replace",form:"",onchange:this.commit,onkeyup:this.commit}),this.caseField=crelt("input",{type:"checkbox",name:"case",form:"",checked:t.caseSensitive,onchange:this.commit}),this.reField=crelt("input",{type:"checkbox",name:"re",form:"",checked:t.regexp,onchange:this.commit}),this.wordField=crelt("input",{type:"checkbox",name:"word",form:"",checked:t.wholeWord,onchange:this.commit}),this.dom=crelt("div",{onkeydown:e=>this.keydown(e),class:"cm-search"},[this.searchField,r("next",(()=>findNext(e)),[phrase(e,"next")]),r("prev",(()=>findPrevious(e)),[phrase(e,"previous")]),r("select",(()=>selectMatches(e)),[phrase(e,"all")]),crelt("label",null,[this.caseField,phrase(e,"match case")]),crelt("label",null,[this.reField,phrase(e,"regexp")]),crelt("label",null,[this.wordField,phrase(e,"by word")]),...e.state.readOnly?[]:[crelt("br"),this.replaceField,r("replace",(()=>replaceNext(e)),[phrase(e,"replace")]),r("replaceAll",(()=>replaceAll(e)),[phrase(e,"replace all")])],crelt("button",{name:"close",onclick:()=>closeSearchPanel(e),"aria-label":phrase(e,"close"),type:"button"},["×"])])}commit(){let e=new SearchQuery({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:setSearchQuery.of(e)}))}keydown(e){runScopeHandlers(this.view,e,"search-panel")?e.preventDefault():13==e.keyCode&&e.target==this.searchField?(e.preventDefault(),(e.shiftKey?findPrevious:findNext)(this.view)):13==e.keyCode&&e.target==this.replaceField&&(e.preventDefault(),replaceNext(this.view))}update(e){for(let t of e.transactions)for(let e of t.effects)e.is(setSearchQuery)&&!e.value.eq(this.query)&&this.setQuery(e.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(searchConfigFacet).top}}function phrase(e,t){return e.state.phrase(t)}const AnnounceMargin=30,Break=/[\s\.,:;?!]/;function announceMatch(e,{from:t,to:r}){let i=e.state.doc.lineAt(t),n=e.state.doc.lineAt(r).to,a=Math.max(i.from,t-AnnounceMargin),o=Math.min(n,r+AnnounceMargin),s=e.state.sliceDoc(a,o);if(a!=i.from)for(let e=0;e<AnnounceMargin;e++)if(!Break.test(s[e+1])&&Break.test(s[e])){s=s.slice(e);break}if(o!=n)for(let e=s.length-1;e>s.length-AnnounceMargin;e--)if(!Break.test(s[e-1])&&Break.test(s[e])){s=s.slice(0,e);break}return EditorView.announce.of(`${e.state.phrase("current match")}. ${s} ${e.state.phrase("on line")} ${i.number}.`)}const baseTheme=EditorView.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"}}),searchExtensions=[searchState,Prec.low(searchHighlighter),baseTheme];export{RegExpCursor,SearchCursor,SearchQuery,closeSearchPanel,findNext,findPrevious,getSearchQuery,gotoLine,highlightSelectionMatches,openSearchPanel,replaceAll,replaceNext,search,searchKeymap,searchPanelOpen,selectMatches,selectNextOccurrence,selectSelectionMatches,setSearchQuery};