UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 11.5 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"../text_utils/text_utils.js";import{Events as WorkspaceImplEvents}from"./WorkspaceImpl.js";const UIStrings={index:"(index)",thisFileWasChangedExternally:"This file was changed externally. Would you like to reload it?"},str_=i18n.i18n.registerUIStrings("models/workspace/UISourceCode.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class UISourceCode extends Common.ObjectWrapper.ObjectWrapper{projectInternal;urlInternal;originInternal;parentURLInternal;nameInternal;contentTypeInternal;requestContentPromise;decorations=new Map;hasCommitsInternal;messagesInternal;contentLoadedInternal;contentInternal;forceLoadOnCheckContentInternal;checkingContent;lastAcceptedContent;workingCopyInternal;workingCopyGetter;disableEditInternal;contentEncodedInternal;isKnownThirdPartyInternal;isUnconditionallyIgnoreListedInternal;constructor(t,e,n){super(),this.projectInternal=t,this.urlInternal=e;const r=Common.ParsedURL.ParsedURL.fromString(e);r?(this.originInternal=r.securityOrigin(),this.parentURLInternal=Common.ParsedURL.ParsedURL.concatenate(this.originInternal,r.folderPathComponents),!r.queryParams||r.lastPathComponent&&n.isFromSourceMap()?this.nameInternal=decodeURIComponent(r.lastPathComponent):this.nameInternal=r.lastPathComponent+"?"+r.queryParams):(this.originInternal=Platform.DevToolsPath.EmptyUrlString,this.parentURLInternal=Platform.DevToolsPath.EmptyUrlString,this.nameInternal=e),this.contentTypeInternal=n,this.requestContentPromise=null,this.hasCommitsInternal=!1,this.messagesInternal=null,this.contentLoadedInternal=!1,this.contentInternal=null,this.forceLoadOnCheckContentInternal=!1,this.checkingContent=!1,this.lastAcceptedContent=null,this.workingCopyInternal=null,this.workingCopyGetter=null,this.disableEditInternal=!1,this.isKnownThirdPartyInternal=!1,this.isUnconditionallyIgnoreListedInternal=!1}requestMetadata(){return this.projectInternal.requestMetadata(this)}name(){return this.nameInternal}mimeType(){return this.projectInternal.mimeType(this)}url(){return this.urlInternal}canononicalScriptId(){return`${this.contentTypeInternal.name()},${this.urlInternal}`}parentURL(){return this.parentURLInternal}origin(){return this.originInternal}fullDisplayName(){return this.projectInternal.fullDisplayName(this)}displayName(t){if(!this.nameInternal)return i18nString(UIStrings.index);const e=this.nameInternal;return t?e:Platform.StringUtilities.trimEndWithMaxLength(e,100)}canRename(){return this.projectInternal.canRename()}rename(t){let e;const n=new Promise((t=>{e=t}));return this.projectInternal.rename(this,t,function(t,n,r,i){t&&this.updateName(n,r,i);e(t)}.bind(this)),n}remove(){this.projectInternal.deleteFile(this)}updateName(t,e,n){const r=this.urlInternal;this.nameInternal=t,this.urlInternal=e||Common.ParsedURL.ParsedURL.relativePathToUrlString(t,r),n&&(this.contentTypeInternal=n),this.dispatchEventToListeners(Events.TitleChanged,this),this.project().workspace().dispatchEventToListeners(WorkspaceImplEvents.UISourceCodeRenamed,{oldURL:r,uiSourceCode:this})}contentURL(){return this.url()}contentType(){return this.contentTypeInternal}project(){return this.projectInternal}requestContent({cachedWasmOnly:t}={}){return this.requestContentPromise?this.requestContentPromise:this.contentLoadedInternal?Promise.resolve(this.contentInternal):t&&"application/wasm"===this.mimeType()?Promise.resolve({content:"",isEncoded:!1,wasmDisassemblyInfo:new Common.WasmDisassembly.WasmDisassembly([],[],[])}):(this.requestContentPromise=this.requestContentImpl(),this.requestContentPromise)}async requestContentImpl(){try{const t=await this.projectInternal.requestFileContent(this);this.contentLoadedInternal||(this.contentLoadedInternal=!0,this.contentInternal=t,this.contentEncodedInternal=t.isEncoded)}catch(t){this.contentLoadedInternal=!0,this.contentInternal={content:null,error:t?String(t):"",isEncoded:!1}}return this.contentInternal}#t(t){return t?t.isEncoded&&t.content?window.atob(t.content):t.content:null}async checkContentUpdated(){if(!this.contentLoadedInternal&&!this.forceLoadOnCheckContentInternal)return;if(!this.projectInternal.canSetFileContent()||this.checkingContent)return;this.checkingContent=!0;const t=await this.projectInternal.requestFileContent(this);if("error"in t)return;if(this.checkingContent=!1,null===t.content){const t=this.workingCopy();return this.contentCommitted("",!1),void this.setWorkingCopy(t)}if(this.lastAcceptedContent===t.content)return;if(this.#t(this.contentInternal)===this.#t(t))return void(this.lastAcceptedContent=null);if(!this.isDirty()||this.workingCopyInternal===t.content)return void this.contentCommitted(t.content,!1);await Common.Revealer.reveal(this),await new Promise((t=>window.setTimeout(t,0)));window.confirm(i18nString(UIStrings.thisFileWasChangedExternally))?this.contentCommitted(t.content,!1):this.lastAcceptedContent=t.content}forceLoadOnCheckContent(){this.forceLoadOnCheckContentInternal=!0}commitContent(t){this.projectInternal.canSetFileContent()&&this.projectInternal.setFileContent(this,t,!1),this.contentCommitted(t,!0)}contentCommitted(t,e){this.lastAcceptedContent=null,this.contentInternal={content:t,isEncoded:!1},this.contentLoadedInternal=!0,this.requestContentPromise=null,this.hasCommitsInternal=!0,this.innerResetWorkingCopy();const n={uiSourceCode:this,content:t,encoded:this.contentEncodedInternal};this.dispatchEventToListeners(Events.WorkingCopyCommitted,n),this.projectInternal.workspace().dispatchEventToListeners(WorkspaceImplEvents.WorkingCopyCommitted,n),e&&this.projectInternal.workspace().dispatchEventToListeners(WorkspaceImplEvents.WorkingCopyCommittedByUser,n)}addRevision(t){this.commitContent(t)}hasCommits(){return this.hasCommitsInternal}workingCopy(){return this.workingCopyContent().content||""}workingCopyContent(){return this.workingCopyGetter&&(this.workingCopyInternal=this.workingCopyGetter(),this.workingCopyGetter=null),this.isDirty()?{content:this.workingCopyInternal,isEncoded:!1}:this.contentInternal?this.contentInternal:{content:"",isEncoded:!1}}resetWorkingCopy(){this.innerResetWorkingCopy(),this.workingCopyChanged()}innerResetWorkingCopy(){this.workingCopyInternal=null,this.workingCopyGetter=null}setWorkingCopy(t){this.workingCopyInternal=t,this.workingCopyGetter=null,this.workingCopyChanged()}setContent(t,e){this.contentEncodedInternal=e,this.projectInternal.canSetFileContent()&&this.projectInternal.setFileContent(this,t,e),this.contentCommitted(t,!0)}setWorkingCopyGetter(t){this.workingCopyGetter=t,this.workingCopyChanged()}workingCopyChanged(){this.removeAllMessages(),this.dispatchEventToListeners(Events.WorkingCopyChanged,this),this.projectInternal.workspace().dispatchEventToListeners(WorkspaceImplEvents.WorkingCopyChanged,{uiSourceCode:this})}removeWorkingCopyGetter(){this.workingCopyGetter&&(this.workingCopyInternal=this.workingCopyGetter(),this.workingCopyGetter=null)}commitWorkingCopy(){this.isDirty()&&this.commitContent(this.workingCopy())}isDirty(){return null!==this.workingCopyInternal||null!==this.workingCopyGetter}isKnownThirdParty(){return this.isKnownThirdPartyInternal}markKnownThirdParty(){this.isKnownThirdPartyInternal=!0}isUnconditionallyIgnoreListed(){return this.isUnconditionallyIgnoreListedInternal}isFetchXHR(){return[Common.ResourceType.resourceTypes.XHR,Common.ResourceType.resourceTypes.Fetch].includes(this.contentType())}markAsUnconditionallyIgnoreListed(){this.isUnconditionallyIgnoreListedInternal=!0}extension(){return Common.ParsedURL.ParsedURL.extractExtension(this.nameInternal)}content(){return this.contentInternal?.content||""}loadError(){return this.contentInternal&&"error"in this.contentInternal&&this.contentInternal.error||null}searchInContent(t,e,n){const r=this.content();return r?Promise.resolve(TextUtils.TextUtils.performSearchInContent(r,t,e,n)):this.projectInternal.searchInFileContent(this,t,e,n)}contentLoaded(){return this.contentLoadedInternal}uiLocation(t,e){return new UILocation(this,t,e)}messages(){return this.messagesInternal?new Set(this.messagesInternal):new Set}addLineMessage(t,e,n,r,i){const o=TextUtils.TextRange.TextRange.createFromLocation(n,r||0),s=new Message(t,e,i,o);return this.addMessage(s),s}addMessage(t){this.messagesInternal||(this.messagesInternal=new Set),this.messagesInternal.add(t),this.dispatchEventToListeners(Events.MessageAdded,t)}removeMessage(t){this.messagesInternal?.delete(t)&&this.dispatchEventToListeners(Events.MessageRemoved,t)}removeAllMessages(){if(this.messagesInternal){for(const t of this.messagesInternal)this.dispatchEventToListeners(Events.MessageRemoved,t);this.messagesInternal=null}}setDecorationData(t,e){e!==this.decorations.get(t)&&(this.decorations.set(t,e),this.dispatchEventToListeners(Events.DecorationChanged,t))}getDecorationData(t){return this.decorations.get(t)}disableEdit(){this.disableEditInternal=!0}editDisabled(){return this.disableEditInternal}}export var Events;!function(t){t.WorkingCopyChanged="WorkingCopyChanged",t.WorkingCopyCommitted="WorkingCopyCommitted",t.TitleChanged="TitleChanged",t.MessageAdded="MessageAdded",t.MessageRemoved="MessageRemoved",t.DecorationChanged="DecorationChanged"}(Events||(Events={}));export class UILocation{uiSourceCode;lineNumber;columnNumber;constructor(t,e,n){this.uiSourceCode=t,this.lineNumber=e,this.columnNumber=n}linkText(t=!1,e=!1){const n=this.uiSourceCode.displayName(t),r=this.lineAndColumnText(e);return r?n+":"+r:n}lineAndColumnText(t=!1){let e;return"application/wasm"===this.uiSourceCode.mimeType()?"number"==typeof this.columnNumber&&(e=`0x${this.columnNumber.toString(16)}`):(e=`${this.lineNumber+1}`,t&&"number"==typeof this.columnNumber&&(e+=":"+(this.columnNumber+1))),e}id(){return"number"==typeof this.columnNumber?this.uiSourceCode.project().id()+":"+this.uiSourceCode.url()+":"+this.lineNumber+":"+this.columnNumber:this.lineId()}lineId(){return this.uiSourceCode.project().id()+":"+this.uiSourceCode.url()+":"+this.lineNumber}toUIString(){return this.uiSourceCode.url()+":"+(this.lineNumber+1)}static comparator(t,e){return t.compareTo(e)}compareTo(t){return this.uiSourceCode.url()!==t.uiSourceCode.url()?this.uiSourceCode.url()>t.uiSourceCode.url()?1:-1:this.lineNumber!==t.lineNumber?this.lineNumber-t.lineNumber:this.columnNumber===t.columnNumber?0:"number"!=typeof this.columnNumber?-1:"number"!=typeof t.columnNumber?1:this.columnNumber-t.columnNumber}}export class Message{levelInternal;textInternal;range;clickHandlerInternal;constructor(t,e,n,r){this.levelInternal=t,this.textInternal=e,this.range=r??new TextUtils.TextRange.TextRange(0,0,0,0),this.clickHandlerInternal=n}level(){return this.levelInternal}text(){return this.textInternal}clickHandler(){return this.clickHandlerInternal}lineNumber(){return this.range.startLine}columnNumber(){return this.range.startColumn}isEqual(t){return this.text()===t.text()&&this.level()===t.level()&&this.range.equal(t.range)}}!function(t){let e;!function(t){t.Error="Error",t.Issue="Issue",t.Warning="Warning"}(e=t.Level||(t.Level={}))}(Message||(Message={}));export class LineMarker{rangeInternal;typeInternal;dataInternal;constructor(t,e,n){this.rangeInternal=t,this.typeInternal=e,this.dataInternal=n}range(){return this.rangeInternal}type(){return this.typeInternal}data(){return this.dataInternal}}export class UISourceCodeMetadata{modificationTime;contentSize;constructor(t,e){this.modificationTime=t,this.contentSize=e}}