@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 4.86 kB
JavaScript
import*as i18n from"../../../../core/i18n/i18n.js";import*as Platform from"../../../../core/platform/platform.js";import*as SDK from"../../../../core/sdk/sdk.js";import*as UI from"../../legacy.js";import*as ObjectUI from"../object_ui/object_ui.js";import jsonViewStyles from"./jsonView.css.legacy.js";const UIStrings={find:"Find"},str_=i18n.i18n.registerUIStrings("ui/legacy/components/source_frame/JSONView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class JSONView extends UI.Widget.VBox{initialized;parsedJSON;startCollapsed;searchableView;treeOutline;currentSearchFocusIndex;currentSearchTreeElements;searchRegex;constructor(e,t){super(),this.initialized=!1,this.registerRequiredCSS(jsonViewStyles),this.parsedJSON=e,this.startCollapsed=Boolean(t),this.element.classList.add("json-view"),this.currentSearchFocusIndex=0,this.currentSearchTreeElements=[],this.searchRegex=null}static async createView(e){const t=await JSONView.parseJSON(e);if(!t||"object"!=typeof t.data)return null;const r=new JSONView(t),s=new UI.SearchableView.SearchableView(r,null);return s.setPlaceholder(i18nString(UIStrings.find)),r.searchableView=s,r.show(s.element),s}static createViewSync(e){const t=new JSONView(new ParsedJSON(e,"","")),r=new UI.SearchableView.SearchableView(t,null);return r.setPlaceholder(i18nString(UIStrings.find)),t.searchableView=r,t.show(r.element),t.element.tabIndex=0,r}static parseJSON(e){let t=null;if(e&&(t=JSONView.extractJSON(e)),!t)return Promise.resolve(null);try{const e=JSON.parse(t.data);if(!e)return Promise.resolve(null);t.data=e}catch(e){t=null}return Promise.resolve(t)}static extractJSON(e){if(e.startsWith("<"))return null;let t=JSONView.findBrackets(e,"{","}");const r=JSONView.findBrackets(e,"[","]");if(t=r.length>t.length?r:t,-1===t.length||e.length-t.length>80)return null;const s=e.substring(0,t.start),i=e.substring(t.end+1);return e=e.substring(t.start,t.end+1),!i.trim().length||i.trim().startsWith(")")&&s.trim().endsWith("(")?new ParsedJSON(e,s,i):null}static findBrackets(e,t,r){const s=e.indexOf(t),i=e.lastIndexOf(r);let n=i-s-1;return(-1===s||-1===i||i<s)&&(n=-1),{start:s,end:i,length:n}}wasShown(){this.initialize()}initialize(){if(this.initialized)return;this.initialized=!0;const e=SDK.RemoteObject.RemoteObject.fromLocalObject(this.parsedJSON.data),t=this.parsedJSON.prefix+e.description+this.parsedJSON.suffix;this.treeOutline=new ObjectUI.ObjectPropertiesSection.ObjectPropertiesSection(e,t,void 0,!0),this.treeOutline.enableContextMenu(),this.treeOutline.setEditable(!1),this.startCollapsed||this.treeOutline.expand(),this.element.appendChild(this.treeOutline.element);const r=this.treeOutline.firstChild();r&&r.select(!0,!1)}jumpToMatch(e){if(!this.searchRegex)return;const t=this.currentSearchTreeElements[this.currentSearchFocusIndex];t&&t.setSearchRegex(this.searchRegex);const r=this.currentSearchTreeElements[e];r?(this.updateSearchIndex(e),r.setSearchRegex(this.searchRegex,UI.UIUtils.highlightedCurrentSearchResultClassName),r.reveal()):this.updateSearchIndex(0)}updateSearchCount(e){this.searchableView&&this.searchableView.updateSearchMatchesCount(e)}updateSearchIndex(e){this.currentSearchFocusIndex=e,this.searchableView&&this.searchableView.updateCurrentMatchIndex(e)}onSearchCanceled(){let e;for(this.searchRegex=null,this.currentSearchTreeElements=[],e=this.treeOutline.rootElement();e;e=e.traverseNextTreeElement(!1))e instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement&&e.revertHighlightChanges();this.updateSearchCount(0),this.updateSearchIndex(0)}performSearch(e,t,r){let s=this.currentSearchFocusIndex;const i=this.currentSearchTreeElements[s];let n;for(this.onSearchCanceled(),this.searchRegex=e.toSearchRegex(!0).regex,n=this.treeOutline.rootElement();n;n=n.traverseNextTreeElement(!1)){if(!(n instanceof ObjectUI.ObjectPropertiesSection.ObjectPropertyTreeElement))continue;const e=n.setSearchRegex(this.searchRegex);if(e&&this.currentSearchTreeElements.push(n),i===n){const t=this.currentSearchTreeElements.length-1;s=e||r?t:t+1}}this.updateSearchCount(this.currentSearchTreeElements.length),this.currentSearchTreeElements.length?(s=Platform.NumberUtilities.mod(s,this.currentSearchTreeElements.length),this.jumpToMatch(s)):this.updateSearchIndex(-1)}jumpToNextSearchResult(){if(!this.currentSearchTreeElements.length)return;const e=Platform.NumberUtilities.mod(this.currentSearchFocusIndex+1,this.currentSearchTreeElements.length);this.jumpToMatch(e)}jumpToPreviousSearchResult(){if(!this.currentSearchTreeElements.length)return;const e=Platform.NumberUtilities.mod(this.currentSearchFocusIndex-1,this.currentSearchTreeElements.length);this.jumpToMatch(e)}supportsCaseSensitiveSearch(){return!0}supportsRegexSearch(){return!0}}export class ParsedJSON{data;prefix;suffix;constructor(e,t,r){this.data=e,this.prefix=t,this.suffix=r}}