UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 5.75 kB
import*as Common from"../../core/common/common.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as TextUtils from"../../models/text_utils/text_utils.js";import*as Components from"../../ui/legacy/components/utils/utils.js";import*as UI from"../../ui/legacy/legacy.js";import searchResultsPaneStyles from"./searchResultsPane.css.js";const UIStrings={matchesCountS:"Matches Count {PH1}",lineS:"Line {PH1}",showDMore:"Show {PH1} more"},str_=i18n.i18n.registerUIStrings("panels/search/SearchResultsPane.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class SearchResultsPane extends UI.Widget.VBox{searchConfig;searchResults;treeElements;treeOutline;matchesExpandedCount;constructor(e){super(!0),this.searchConfig=e,this.searchResults=[],this.treeElements=[],this.treeOutline=new UI.TreeOutline.TreeOutlineInShadow,this.treeOutline.hideOverflow(),this.contentElement.appendChild(this.treeOutline.element),this.matchesExpandedCount=0}addSearchResult(e){this.searchResults.push(e),this.addTreeElement(e)}showAllMatches(){this.treeElements.forEach((e=>{e.expand(),e.showAllMatches()}))}collapseAllResults(){this.treeElements.forEach((e=>{e.collapse()}))}addTreeElement(e){const t=new SearchResultsTreeElement(this.searchConfig,e);this.treeOutline.appendChild(t),this.treeOutline.selectedTreeElement||t.select(!0,!0),this.matchesExpandedCount<matchesExpandedByDefault&&t.expand(),this.matchesExpandedCount+=e.matchesCount(),this.treeElements.push(t)}wasShown(){super.wasShown(),this.treeOutline.registerCSSFiles([searchResultsPaneStyles])}}export const matchesExpandedByDefault=200;export const matchesShownAtOnce=20;export class SearchResultsTreeElement extends UI.TreeOutline.TreeElement{searchConfig;searchResult;initialized;toggleOnClick;constructor(e,t){super("",!0),this.searchConfig=e,this.searchResult=t,this.initialized=!1,this.toggleOnClick=!0}onexpand(){this.initialized||(this.updateMatchesUI(),this.initialized=!0)}showAllMatches(){this.removeChildren(),this.appendSearchMatches(0,this.searchResult.matchesCount())}updateMatchesUI(){this.removeChildren();const e=Math.min(this.searchResult.matchesCount(),20);e<this.searchResult.matchesCount()?(this.appendSearchMatches(0,e-1),this.appendShowMoreMatchesElement(e-1)):this.appendSearchMatches(0,e)}onattach(){this.updateSearchMatches()}updateSearchMatches(){this.listItemElement.classList.add("search-result");const e=s(this.searchResult.label(),"search-result-file-name");e.appendChild(s("—","search-result-dash")),e.appendChild(s(this.searchResult.description(),"search-result-qualifier")),this.tooltip=this.searchResult.description(),this.listItemElement.appendChild(e);const t=document.createElement("span");function s(e,t){const s=document.createElement("span");return s.className=t,s.textContent=e,s}t.className="search-result-matches-count",t.textContent=`${this.searchResult.matchesCount()}`,UI.ARIAUtils.setLabel(t,i18nString(UIStrings.matchesCountS,{PH1:this.searchResult.matchesCount()})),this.listItemElement.appendChild(t),this.expanded&&this.updateMatchesUI()}appendSearchMatches(e,t){const s=this.searchResult,n=this.searchConfig.queries(),a=[];for(let e=0;e<n.length;++e)a.push(Platform.StringUtilities.createSearchRegex(n[e],!this.searchConfig.ignoreCase(),this.searchConfig.isRegex()));for(let n=e;n<t;++n){let e=s.matchLineContent(n).trim(),t=[];for(let s=0;s<a.length;++s)t=t.concat(this.regexMatchRanges(e,a[s]));({lineSegment:e,matchRanges:t}=lineSegmentForMultipleMatches(e,t));const i=Components.Linkifier.Linkifier.linkifyRevealable(s.matchRevealable(n),"");i.classList.add("search-match-link");const h=document.createElement("span");h.classList.add("search-match-line-number");const l=s.matchLabel(n);h.textContent=l,"number"!=typeof l||isNaN(l)?UI.ARIAUtils.setLabel(h,l):UI.ARIAUtils.setLabel(h,i18nString(UIStrings.lineS,{PH1:l})),i.appendChild(h);const r=this.createContentSpan(e,t);i.appendChild(r);const c=new UI.TreeOutline.TreeElement;this.appendChild(c),c.listItemElement.className="search-match",c.listItemElement.appendChild(i),c.listItemElement.addEventListener("keydown",(e=>{"Enter"===e.key&&(e.consume(!0),Common.Revealer.reveal(s.matchRevealable(n)))})),c.tooltip=e}}appendShowMoreMatchesElement(e){const t=this.searchResult.matchesCount()-e,s=i18nString(UIStrings.showDMore,{PH1:t}),n=new UI.TreeOutline.TreeElement(s);this.appendChild(n),n.listItemElement.classList.add("show-more-matches"),n.onselect=this.showMoreMatchesElementSelected.bind(this,n,e)}createContentSpan(e,t){const s=document.createElement("span");return s.className="search-match-content",s.textContent=e,UI.ARIAUtils.setLabel(s,`${e} line`),UI.UIUtils.highlightRangesWithStyleClass(s,t,"highlighted-search-result"),s}regexMatchRanges(e,t){let s;t.lastIndex=0;const n=[];for(;t.lastIndex<e.length&&(s=t.exec(e));)n.push(new TextUtils.TextRange.SourceRange(s.index,s[0].length));return n}showMoreMatchesElementSelected(e,t){return this.removeChild(e),this.appendSearchMatches(t,this.searchResult.matchesCount()),!1}}const DEFAULT_OPTS={prefixLength:25,maxLength:1e3};export function lineSegmentForMatch(e,t,s=DEFAULT_OPTS){const n={...DEFAULT_OPTS,...s},a=e.trimStart(),i=e.length-a.length,h=Math.min(t.offset,i),l=Math.max(h,t.offset-n.prefixLength),r=Math.min(e.length,l+n.maxLength),c=l>h?"…":"",o=c+e.substring(l,r),m=t.offset-l+c.length,u=Math.min(t.length,o.length-m);return{lineSegment:o,matchRange:new TextUtils.TextRange.SourceRange(m,u)}}function lineSegmentForMultipleMatches(e,t){let s=0,n=t;n.length>0&&n[0].offset>20&&(s=15);let a=e.substring(s,1e3+s);return s&&(n=n.map((e=>new TextUtils.TextRange.SourceRange(e.offset-s+1,e.length))),a="…"+a),{lineSegment:a,matchRanges:n}}