@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 15.7 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as Extensions from"../../models/extensions/extensions.js";import*as Persistence from"../../models/persistence/persistence.js";import*as TextUtils from"../../models/text_utils/text_utils.js";import*as Workspace from"../../models/workspace/workspace.js";import*as SourceFrame from"../../ui/legacy/components/source_frame/source_frame.js";import*as UI from"../../ui/legacy/legacy.js";import*as IconButton from"../../ui/components/icon_button/icon_button.js";import*as Snippets from"../snippets/snippets.js";import*as Bindings from"../../models/bindings/bindings.js";import{SourcesView}from"./SourcesView.js";import{UISourceCodeFrame}from"./UISourceCodeFrame.js";const UIStrings={areYouSureYouWantToCloseUnsaved:"Are you sure you want to close unsaved file: {PH1}?",unableToLoadThisContent:"Unable to load this content.",changesToThisFileWereNotSavedTo:"Changes to this file were not saved to file system."},str_=i18n.i18n.registerUIStrings("panels/sources/TabbedEditorContainer.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let tabId=0;export class TabbedEditorContainer extends Common.ObjectWrapper.ObjectWrapper{delegate;tabbedPane;tabIds;files;previouslyViewedFilesSetting;history;uriToUISourceCode;idToUISourceCode;currentFileInternal;currentView;scrollTimer;reentrantShow;constructor(e,t,i,s){super(),this.delegate=e,this.tabbedPane=new UI.TabbedPane.TabbedPane,this.tabbedPane.setPlaceholderElement(i,s),this.tabbedPane.setTabDelegate(new EditorContainerTabDelegate(this)),this.tabbedPane.setCloseableTabs(!0),this.tabbedPane.setAllowTabReorder(!0,!0),this.tabbedPane.addEventListener(UI.TabbedPane.Events.TabClosed,this.tabClosed,this),this.tabbedPane.addEventListener(UI.TabbedPane.Events.TabSelected,this.tabSelected,this),Persistence.Persistence.PersistenceImpl.instance().addEventListener(Persistence.Persistence.Events.BindingCreated,this.onBindingCreated,this),Persistence.Persistence.PersistenceImpl.instance().addEventListener(Persistence.Persistence.Events.BindingRemoved,this.onBindingRemoved,this),Persistence.NetworkPersistenceManager.NetworkPersistenceManager.instance().addEventListener(Persistence.NetworkPersistenceManager.Events.RequestsForHeaderOverridesFileChanged,this.#e,this),this.tabIds=new Map,this.files=new Map,this.previouslyViewedFilesSetting=t,this.history=History.fromObject(this.previouslyViewedFilesSetting.get()),this.uriToUISourceCode=new Map,this.idToUISourceCode=new Map,this.reentrantShow=!1}onBindingCreated(e){const t=e.data;this.updateFileTitle(t.fileSystem);const i=this.tabIds.get(t.network);let s=this.tabIds.get(t.fileSystem);const r=this.currentFileInternal===t.network,o=historyItemKey(t.network),n=this.history.selectionRange(o),a=this.history.scrollLineNumber(o);if(this.history.remove(o),i){if(!s){const e=this.tabbedPane.tabView(i),r=this.tabbedPane.tabIndex(i);if(e instanceof UISourceCodeFrame)this.delegate.recycleUISourceCodeFrame(e,t.fileSystem),s=this.appendFileTab(t.fileSystem,!1,r,e);else{s=this.appendFileTab(t.fileSystem,!1,r);const e=this.tabbedPane.tabView(s);this.restoreEditorProperties(e,n,a)}}this.closeTabs([i],!0),r&&this.tabbedPane.selectTab(s,!1),this.updateHistory()}}#e(e){this.updateFileTitle(e.data)}onBindingRemoved(e){const t=e.data;this.updateFileTitle(t.fileSystem)}get view(){return this.tabbedPane}get visibleView(){return this.tabbedPane.visibleView}fileViews(){return this.tabbedPane.tabViews()}leftToolbar(){return this.tabbedPane.leftToolbar()}rightToolbar(){return this.tabbedPane.rightToolbar()}show(e){this.tabbedPane.show(e)}showFile(e){const t=Persistence.Persistence.PersistenceImpl.instance().binding(e);e=t?t.fileSystem:e;const i=UI.Context.Context.instance().flavor(SourcesView);if(i?.currentSourceFrame()?.contentSet&&this.currentFileInternal===e&&i?.currentUISourceCode()===e)Common.EventTarget.fireEvent("source-file-loaded",e.displayName(!0));else{if(e.project().type()===Workspace.Workspace.projectTypes.Debugger){const t=Bindings.DefaultScriptMapping.DefaultScriptMapping.scriptForUISourceCode(e);t&&t.isInlineScript()&&!t.hasSourceURL&&(t.isModule?Host.userMetrics.vmInlineScriptContentShown(0):Host.userMetrics.vmInlineScriptContentShown(1))}this.innerShowFile(e,!0)}}closeFile(e){const t=this.tabIds.get(e);t&&this.closeTabs([t])}closeAllFiles(){this.closeTabs(this.tabbedPane.tabIds())}historyUISourceCodes(){const e=[];for(const{url:t,resourceType:i}of this.history.keys()){const s=this.uriToUISourceCode.get(t);void 0!==s&&s.contentType()===i&&e.push(s)}return e}selectNextTab(){this.tabbedPane.selectNextTab()}selectPrevTab(){this.tabbedPane.selectPrevTab()}addViewListeners(){this.currentView&&this.currentView instanceof SourceFrame.SourceFrame.SourceFrameImpl&&(this.currentView.addEventListener("EditorUpdate",this.onEditorUpdate,this),this.currentView.addEventListener("EditorScroll",this.onScrollChanged,this))}removeViewListeners(){this.currentView&&this.currentView instanceof SourceFrame.SourceFrame.SourceFrameImpl&&(this.currentView.removeEventListener("EditorUpdate",this.onEditorUpdate,this),this.currentView.removeEventListener("EditorScroll",this.onScrollChanged,this))}onScrollChanged(){if(this.currentView instanceof SourceFrame.SourceFrame.SourceFrameImpl&&(this.scrollTimer&&clearTimeout(this.scrollTimer),this.scrollTimer=window.setTimeout((()=>this.previouslyViewedFilesSetting.set(this.history.toObject())),100),this.currentFileInternal)){const{editor:e}=this.currentView.textEditor,t=e.lineBlockAtHeight(e.scrollDOM.getBoundingClientRect().top-e.documentTop),i=e.state.doc.lineAt(t.from).number-1;this.history.updateScrollLineNumber(historyItemKey(this.currentFileInternal),i)}}onEditorUpdate({data:e}){if(e.docChanged||e.selectionSet){const{main:t}=e.state.selection,i=e.state.doc.lineAt(t.from),s=e.state.doc.lineAt(t.to),r=new TextUtils.TextRange.TextRange(i.number-1,t.from-i.from,s.number-1,t.to-s.from);this.currentFileInternal&&this.history.updateSelectionRange(historyItemKey(this.currentFileInternal),r),this.previouslyViewedFilesSetting.set(this.history.toObject()),this.currentFileInternal&&Extensions.ExtensionServer.ExtensionServer.instance().sourceSelectionChanged(this.currentFileInternal.url(),r)}}innerShowFile(e,t){if(this.reentrantShow)return;const i=this.canonicalUISourceCode(e),s=Persistence.Persistence.PersistenceImpl.instance().binding(e);if(e=s?s.fileSystem:e,this.currentFileInternal===e)return;this.removeViewListeners(),this.currentFileInternal=e;try{this.reentrantShow=!0;const e=this.tabIds.get(i)||this.appendFileTab(i,t);this.tabbedPane.selectTab(e,t)}finally{this.reentrantShow=!1}t&&this.editorSelectedByUserAction();const r=this.currentView;this.currentView=this.visibleView,this.addViewListeners(),this.currentView instanceof UISourceCodeFrame&&this.currentView.uiSourceCode()!==e&&(this.delegate.recycleUISourceCodeFrame(this.currentView,e),e.project().type()!==Workspace.Workspace.projectTypes.FileSystem&&e.disableEdit());const o={currentFile:this.currentFileInternal,currentView:this.currentView,previousView:r,userGesture:t};this.dispatchEventToListeners(Events.EditorSelected,o)}titleForFile(e){let t=Platform.StringUtilities.trimMiddle(e.displayName(!0),30);return e.isDirty()&&(t+="*"),t}maybeCloseTab(e,t){const i=this.files.get(e);if(!i)return!1;return!(i.isDirty()&&i.project().canSetFileContent()&&!confirm(i18nString(UIStrings.areYouSureYouWantToCloseUnsaved,{PH1:i.name()})))&&(i.resetWorkingCopy(),t&&this.tabbedPane.selectTab(t,!0),this.tabbedPane.closeTab(e,!0),!0)}closeTabs(e,t){const i=[],s=[];for(let r=0;r<e.length;++r){const o=e[r],n=this.files.get(o);n&&(!t&&n.isDirty()?i.push(o):s.push(o))}i.length&&this.tabbedPane.selectTab(i[0],!0),this.tabbedPane.closeTabs(s,!0);for(let e=0;e<i.length;++e){const t=e+1<i.length?i[e+1]:null;if(!this.maybeCloseTab(i[e],t))break}}onContextMenu(e,t){const i=this.files.get(e);i&&t.appendApplicableItems(i)}canonicalUISourceCode(e){const t=this.idToUISourceCode.get(e.canononicalScriptId());return t||(this.idToUISourceCode.set(e.canononicalScriptId(),e),this.uriToUISourceCode.set(e.url(),e),e)}addUISourceCode(e){const t=this.canonicalUISourceCode(e),i=t!==e,s=Persistence.Persistence.PersistenceImpl.instance().binding(t);if(e=s?s.fileSystem:t,i&&e.project().type()!==Workspace.Workspace.projectTypes.FileSystem&&e.disableEdit(),this.currentFileInternal?.canononicalScriptId()===e.canononicalScriptId())return;const r=this.history.index(historyItemKey(e));if(-1===r)return;if(this.tabIds.has(e)||this.appendFileTab(e,!1),!r)return void this.innerShowFile(e,!1);if(!this.currentFileInternal)return;const o=Snippets.ScriptSnippetFileSystem.isSnippetsUISourceCode(this.currentFileInternal),n=Snippets.ScriptSnippetFileSystem.isSnippetsUISourceCode(e);this.history.index(historyItemKey(this.currentFileInternal))&&o&&!n&&this.innerShowFile(e,!1)}removeUISourceCode(e){this.removeUISourceCodes([e])}removeUISourceCodes(e){const t=[];for(const i of e){const e=this.tabIds.get(i);e&&t.push(e),this.uriToUISourceCode.get(i.url())===i&&this.uriToUISourceCode.delete(i.url()),this.idToUISourceCode.get(i.canononicalScriptId())===i&&this.idToUISourceCode.delete(i.canononicalScriptId())}this.tabbedPane.closeTabs(t)}editorClosedByUserAction(e){this.history.remove(historyItemKey(e)),this.updateHistory()}editorSelectedByUserAction(){this.updateHistory()}updateHistory(){const e=[];for(const t of this.tabbedPane.lastOpenedTabIds(MAX_PREVIOUSLY_VIEWED_FILES_COUNT)){const i=this.files.get(t);void 0!==i&&e.push(historyItemKey(i))}this.history.update(e),this.previouslyViewedFilesSetting.set(this.history.toObject())}tooltipForFile(e){return(e=Persistence.Persistence.PersistenceImpl.instance().network(e)||e).url()}appendFileTab(e,t,i,s){const r=s||this.delegate.viewForFile(e),o=this.titleForFile(e),n=this.tooltipForFile(e),a=this.generateTabId();if(this.tabIds.set(e,a),this.files.set(a,e),!s){const t=this.history.selectionRange(historyItemKey(e)),i=this.history.scrollLineNumber(historyItemKey(e));this.restoreEditorProperties(r,t,i)}return this.tabbedPane.appendTab(a,o,r,n,t,void 0,void 0,i),this.updateFileTitle(e),this.addUISourceCodeListeners(e),e.loadError()?this.addLoadErrorIcon(a):e.contentLoaded()||e.requestContent().then((t=>{e.loadError()&&this.addLoadErrorIcon(a)})),a}addLoadErrorIcon(e){const t=new IconButton.Icon.Icon;t.data={iconName:"cross-circle-filled",color:"var(--icon-error)",width:"14px",height:"14px"},UI.Tooltip.Tooltip.install(t,i18nString(UIStrings.unableToLoadThisContent)),this.tabbedPane.tabView(e)&&this.tabbedPane.setTabIcon(e,t)}restoreEditorProperties(e,t,i){const s=e instanceof SourceFrame.SourceFrame.SourceFrameImpl?e:null;s&&(t&&s.setSelection(t),"number"==typeof i&&s.scrollToLine(i))}tabClosed(e){const{tabId:t,isUserGesture:i}=e.data,s=this.files.get(t);this.currentFileInternal&&this.currentFileInternal.canononicalScriptId()===s?.canononicalScriptId()&&(this.removeViewListeners(),this.currentView=null,this.currentFileInternal=null),s&&this.tabIds.delete(s),this.files.delete(t),s&&(this.removeUISourceCodeListeners(s),this.dispatchEventToListeners(Events.EditorClosed,s),i&&this.editorClosedByUserAction(s))}tabSelected(e){const{tabId:t,isUserGesture:i}=e.data,s=this.files.get(t);s&&this.innerShowFile(s,i)}addUISourceCodeListeners(e){e.addEventListener(Workspace.UISourceCode.Events.TitleChanged,this.uiSourceCodeTitleChanged,this),e.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this.uiSourceCodeWorkingCopyChanged,this),e.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this.uiSourceCodeWorkingCopyCommitted,this)}removeUISourceCodeListeners(e){e.removeEventListener(Workspace.UISourceCode.Events.TitleChanged,this.uiSourceCodeTitleChanged,this),e.removeEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this.uiSourceCodeWorkingCopyChanged,this),e.removeEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this.uiSourceCodeWorkingCopyCommitted,this)}updateFileTitle(e){const t=this.tabIds.get(e);if(t){const i=this.titleForFile(e),s=this.tooltipForFile(e);this.tabbedPane.changeTabTitle(t,i,s);let r=null;e.loadError()?(r=new IconButton.Icon.Icon,r.data={iconName:"cross-circle-filled",color:"var(--icon-error)",width:"14px",height:"14px"},UI.Tooltip.Tooltip.install(r,i18nString(UIStrings.unableToLoadThisContent))):Persistence.Persistence.PersistenceImpl.instance().hasUnsavedCommittedChanges(e)?(r=new IconButton.Icon.Icon,r.data={iconName:"warning-filled",color:"var(--icon-warning)",width:"14px",height:"14px"},UI.Tooltip.Tooltip.install(r,i18nString(UIStrings.changesToThisFileWereNotSavedTo))):r=Persistence.PersistenceUtils.PersistenceUtils.iconForUISourceCode(e),this.tabbedPane.setTabIcon(t,r)}}uiSourceCodeTitleChanged(e){const t=e.data;this.updateFileTitle(t),this.updateHistory();for(const[e,i]of this.uriToUISourceCode)i===t&&e!==i.url()&&this.uriToUISourceCode.delete(e);for(const[e,i]of this.idToUISourceCode)i===t&&e!==i.canononicalScriptId()&&this.idToUISourceCode.delete(e);this.canonicalUISourceCode(t)}uiSourceCodeWorkingCopyChanged(e){const t=e.data;this.updateFileTitle(t)}uiSourceCodeWorkingCopyCommitted(e){const t=e.data.uiSourceCode;this.updateFileTitle(t)}generateTabId(){return"tab_"+tabId++}currentFile(){return this.currentFileInternal||null}}export var Events;!function(e){e.EditorSelected="EditorSelected",e.EditorClosed="EditorClosed"}(Events||(Events={}));const MAX_PREVIOUSLY_VIEWED_FILES_COUNT=30,MAX_SERIALIZABLE_URL_LENGTH=4096;function historyItemKey(e){return{url:e.url(),resourceType:e.contentType()}}export class HistoryItem{url;resourceType;selectionRange;scrollLineNumber;constructor(e,t,i,s){this.url=e,this.resourceType=t,this.selectionRange=i,this.scrollLineNumber=s}static fromObject(e){const t=Common.ResourceType.ResourceType.fromName(e.resourceTypeName);if(null===t)throw new TypeError(`Invalid resource type name "${e.resourceTypeName}"`);const i=e.selectionRange?TextUtils.TextRange.TextRange.fromObject(e.selectionRange):void 0;return new HistoryItem(e.url,t,i,e.scrollLineNumber)}toObject(){return this.url.length>=4096?null:{url:this.url,resourceTypeName:this.resourceType.name(),selectionRange:this.selectionRange,scrollLineNumber:this.scrollLineNumber}}}export class History{items;constructor(e){this.items=e}static fromObject(e){const t=[];for(const i of e)try{t.push(HistoryItem.fromObject(i))}catch{}return new History(t)}index({url:e,resourceType:t}){return this.items.findIndex((i=>i.url===e&&i.resourceType===t))}selectionRange(e){const t=this.index(e);if(-1!==t)return this.items[t].selectionRange}updateSelectionRange(e,t){if(!t)return;const i=this.index(e);-1!==i&&(this.items[i].selectionRange=t)}scrollLineNumber(e){const t=this.index(e);if(-1!==t)return this.items[t].scrollLineNumber}updateScrollLineNumber(e,t){const i=this.index(e);-1!==i&&(this.items[i].scrollLineNumber=t)}update(e){for(let t=e.length-1;t>=0;--t){const i=this.index(e[t]);let s;-1!==i?(s=this.items[i],this.items.splice(i,1)):s=new HistoryItem(e[t].url,e[t].resourceType),this.items.unshift(s)}}remove(e){const t=this.index(e);-1!==t&&this.items.splice(t,1)}toObject(){const e=[];for(const t of this.items){const i=t.toObject();if(i&&e.push(i),e.length===MAX_PREVIOUSLY_VIEWED_FILES_COUNT)break}return e}keys(){return this.items}}export class EditorContainerTabDelegate{editorContainer;constructor(e){this.editorContainer=e}closeTabs(e,t){this.editorContainer.closeTabs(t)}onContextMenu(e,t){this.editorContainer.onContextMenu(e,t)}}