UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 15.6 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 Root from"../../core/root/root.js";import*as Persistence from"../../models/persistence/persistence.js";import*as Workspace from"../../models/workspace/workspace.js";import*as QuickOpen from"../../ui/legacy/components/quick_open/quick_open.js";import*as SourceFrame from"../../ui/legacy/components/source_frame/source_frame.js";import*as UI from"../../ui/legacy/legacy.js";import*as Components from"./components/components.js";import{EditingLocationHistoryManager}from"./EditingLocationHistoryManager.js";import sourcesViewStyles from"./sourcesView.css.js";import{Events as TabbedEditorContainerEvents,TabbedEditorContainer}from"./TabbedEditorContainer.js";import{Events as UISourceCodeFrameEvents,UISourceCodeFrame}from"./UISourceCodeFrame.js";const UIStrings={openFile:"Open file",runCommand:"Run command",workspaceDropInAFolderToSyncSources:"To sync edits to the workspace, drop a folder with your sources here",sourceViewActions:"Source View Actions",LearnMore:"Learn more"},str_=i18n.i18n.registerUIStrings("panels/sources/SourcesView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class SourcesView extends(Common.ObjectWrapper.eventMixin(UI.Widget.VBox)){placeholderOptionArray;selectedIndex;searchableViewInternal;sourceViewByUISourceCode;editorContainer;historyManager;toolbarContainerElementInternal;scriptViewToolbar;bottomToolbarInternal;toolbarChangedListener;shortcuts;focusedPlaceholderElement;searchView;searchConfig;constructor(){super(),this.element.id="sources-panel-sources-view",this.setMinimumAndPreferredSizes(88,52,150,100),this.placeholderOptionArray=[],this.selectedIndex=0;const e=Workspace.Workspace.WorkspaceImpl.instance();this.searchableViewInternal=new UI.SearchableView.SearchableView(this,this,"sourcesViewSearchConfig"),this.searchableViewInternal.setMinimalSearchQuerySize(0),this.searchableViewInternal.show(this.element),this.sourceViewByUISourceCode=new Map,this.editorContainer=new TabbedEditorContainer(this,Common.Settings.Settings.instance().createLocalSetting("previouslyViewedFiles",[]),this.placeholderElement(),this.focusedPlaceholderElement),this.editorContainer.show(this.searchableViewInternal.element),this.editorContainer.addEventListener(TabbedEditorContainerEvents.EditorSelected,this.editorSelected,this),this.editorContainer.addEventListener(TabbedEditorContainerEvents.EditorClosed,this.editorClosed,this),this.historyManager=new EditingLocationHistoryManager(this),this.toolbarContainerElementInternal=this.element.createChild("div","sources-toolbar"),this.scriptViewToolbar=new UI.Toolbar.Toolbar("",this.toolbarContainerElementInternal),this.scriptViewToolbar.element.style.flex="auto",this.bottomToolbarInternal=new UI.Toolbar.Toolbar("",this.toolbarContainerElementInternal),this.toolbarChangedListener=null,UI.UIUtils.startBatchUpdate(),e.uiSourceCodes().forEach(this.addUISourceCode.bind(this)),UI.UIUtils.endBatchUpdate(),e.addEventListener(Workspace.Workspace.Events.UISourceCodeAdded,this.uiSourceCodeAdded,this),e.addEventListener(Workspace.Workspace.Events.UISourceCodeRemoved,this.uiSourceCodeRemoved,this),e.addEventListener(Workspace.Workspace.Events.ProjectRemoved,this.projectRemoved.bind(this),this),window.opener||window.addEventListener("beforeunload",(function(e){if(e.returnValue)return;const t=[],o=Workspace.Workspace.WorkspaceImpl.instance().projectsForType(Workspace.Workspace.projectTypes.FileSystem);for(const e of o)for(const o of e.uiSourceCodes())o.isDirty()&&t.push(o);if(t.length){e.returnValue=!0,UI.ViewManager.ViewManager.instance().showView("sources");for(const e of t)Common.Revealer.reveal(e)}}),!0),this.shortcuts=new Map,this.element.addEventListener("keydown",this.handleKeyDown.bind(this),!1)}placeholderElement(){this.placeholderOptionArray=[];const e=[{actionId:"quickOpen.show",description:i18nString(UIStrings.openFile)},{actionId:"commandMenu.show",description:i18nString(UIStrings.runCommand)},{actionId:"sources.add-folder-to-workspace",description:i18nString(UIStrings.workspaceDropInAFolderToSyncSources)}],t=document.createElement("div");t.addEventListener("keydown",this.placeholderOnKeyDown.bind(this),!1),UI.ARIAUtils.markAsList(t),UI.ARIAUtils.setLabel(t,i18nString(UIStrings.sourceViewActions));for(const o of e){const e=UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction(o.actionId),r=t.createChild("div","tabbed-pane-placeholder-row");UI.ARIAUtils.markAsListitem(r),e?(r.createChild("span").textContent=e,r.createChild("span").textContent=o.description):r.createChild("span").textContent=o.description;const i=UI.ActionRegistry.ActionRegistry.instance().action(o.actionId);i&&this.placeholderOptionArray.push({element:r,handler(){i.execute()}})}return t.appendChild(UI.XLink.XLink.create("https://goo.gle/devtools-workspace",i18nString(UIStrings.LearnMore))),t}placeholderOnKeyDown(e){const t=e;if(Platform.KeyboardUtilities.isEnterOrSpaceKey(t))return void this.placeholderOptionArray[this.selectedIndex].handler();let o=0;"ArrowDown"===t.key?o=1:"ArrowUp"===t.key&&(o=-1);const r=Math.max(Math.min(this.placeholderOptionArray.length-1,this.selectedIndex+o),0),i=this.placeholderOptionArray[r].element,s=this.placeholderOptionArray[this.selectedIndex].element;i!==s&&(s.tabIndex=-1,i.tabIndex=0,UI.ARIAUtils.setSelected(s,!1),UI.ARIAUtils.setSelected(i,!0),this.selectedIndex=r,i.focus())}static defaultUISourceCodeScores(){const e=new Map,t=UI.Context.Context.instance().flavor(SourcesView);if(t){const o=t.editorContainer.historyUISourceCodes();for(let t=1;t<o.length;++t)e.set(o[t],o.length-t)}return e}leftToolbar(){return this.editorContainer.leftToolbar()}rightToolbar(){return this.editorContainer.rightToolbar()}bottomToolbar(){return this.bottomToolbarInternal}registerShortcuts(e,t){for(let o=0;o<e.length;++o)this.shortcuts.set(e[o].key,t)}handleKeyDown(e){const t=UI.KeyboardShortcut.KeyboardShortcut.makeKeyFromEvent(e),o=this.shortcuts.get(t);o&&o()&&e.consume(!0)}wasShown(){super.wasShown(),this.registerCSSFiles([sourcesViewStyles]),UI.Context.Context.instance().setFlavor(SourcesView,this)}willHide(){UI.Context.Context.instance().setFlavor(SourcesView,null),super.willHide()}toolbarContainerElement(){return this.toolbarContainerElementInternal}searchableView(){return this.searchableViewInternal}visibleView(){return this.editorContainer.visibleView}currentSourceFrame(){const e=this.visibleView();return e instanceof UISourceCodeFrame?e:null}currentUISourceCode(){return this.editorContainer.currentFile()}onCloseEditorTab(){const e=this.editorContainer.currentFile();return!!e&&(this.editorContainer.closeFile(e),!0)}onJumpToPreviousLocation(){this.historyManager.rollback()}onJumpToNextLocation(){this.historyManager.rollover()}uiSourceCodeAdded(e){const t=e.data;this.addUISourceCode(t)}addUISourceCode(e){e.project().isServiceProject()||e.project().type()===Workspace.Workspace.projectTypes.FileSystem&&"overrides"===Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.fileSystemType(e.project())||this.editorContainer.addUISourceCode(e)}uiSourceCodeRemoved(e){const t=e.data;this.removeUISourceCodes([t])}removeUISourceCodes(e){this.editorContainer.removeUISourceCodes(e);for(let t=0;t<e.length;++t)this.removeSourceFrame(e[t]),this.historyManager.removeHistoryForSourceCode(e[t])}projectRemoved(e){const t=e.data.uiSourceCodes();this.removeUISourceCodes([...t])}updateScriptViewToolbarItems(){const e=this.visibleView();e instanceof UI.View.SimpleView&&e.toolbarItems().then((e=>{this.scriptViewToolbar.removeToolbarItems();for(const e of getRegisteredEditorActions())this.scriptViewToolbar.appendToolbarItem(e.getOrCreateButton(this));e.map((e=>this.scriptViewToolbar.appendToolbarItem(e)))}))}showSourceLocation(e,t,o,r){const i=this.currentSourceFrame();i&&this.historyManager.updateCurrentState(i.uiSourceCode(),i.textEditor.state.selection.main.head),this.editorContainer.showFile(e);const s=this.currentSourceFrame();s&&t&&s.revealPosition(t,!r);const n=this.visibleView();!o&&n&&n.focus()}createSourceView(e){let t,o;const r=e.contentType();r===Common.ResourceType.resourceTypes.Image?o=new SourceFrame.ImageView.ImageView(e.mimeType(),e):r===Common.ResourceType.resourceTypes.Font?o=new SourceFrame.FontView.FontView(e.mimeType(),e):e.name()===HEADER_OVERRIDES_FILENAME&&Root.Runtime.experiments.isEnabled(Root.Runtime.ExperimentName.HEADER_OVERRIDES)?o=new Components.HeadersView.HeadersView(e):t=new UISourceCodeFrame(e),t&&this.historyManager.trackSourceFrameCursorJumps(t),e.addEventListener(Workspace.UISourceCode.Events.TitleChanged,this.#e,this);const i=t||o;return this.sourceViewByUISourceCode.set(e,i),i}#t(e){return e instanceof SourceFrame.ImageView.ImageView?SourceViewType.ImageView:e instanceof SourceFrame.FontView.FontView?SourceViewType.FontView:e instanceof Components.HeadersView.HeadersView?SourceViewType.HeadersView:SourceViewType.SourceView}#o(e){if(e.name()===HEADER_OVERRIDES_FILENAME&&Root.Runtime.experiments.isEnabled(Root.Runtime.ExperimentName.HEADER_OVERRIDES))return SourceViewType.HeadersView;switch(e.contentType()){case Common.ResourceType.resourceTypes.Image:return SourceViewType.ImageView;case Common.ResourceType.resourceTypes.Font:return SourceViewType.FontView;default:return SourceViewType.SourceView}}#e(e){const t=e.data,o=this.sourceViewByUISourceCode.get(t);o&&this.#t(o)!==this.#o(t)&&(this.removeUISourceCodes([t]),this.showSourceLocation(t))}getSourceView(e){return this.sourceViewByUISourceCode.get(e)}getOrCreateSourceView(e){return this.sourceViewByUISourceCode.get(e)||this.createSourceView(e)}recycleUISourceCodeFrame(e,t){e.uiSourceCode().removeEventListener(Workspace.UISourceCode.Events.TitleChanged,this.#e,this),this.sourceViewByUISourceCode.delete(e.uiSourceCode()),e.setUISourceCode(t),this.sourceViewByUISourceCode.set(t,e),t.addEventListener(Workspace.UISourceCode.Events.TitleChanged,this.#e,this)}viewForFile(e){return this.getOrCreateSourceView(e)}removeSourceFrame(e){const t=this.sourceViewByUISourceCode.get(e);this.sourceViewByUISourceCode.delete(e),t&&t instanceof UISourceCodeFrame&&t.dispose(),e.removeEventListener(Workspace.UISourceCode.Events.TitleChanged,this.#e,this)}editorClosed(e){const t=e.data;this.historyManager.removeHistoryForSourceCode(t);let o=!1;this.editorContainer.currentFile()||(o=!0),this.removeToolbarChangedListener(),this.updateScriptViewToolbarItems(),this.searchableViewInternal.resetSearch();const r={uiSourceCode:t,wasSelected:o};this.dispatchEventToListeners(Events.EditorClosed,r)}editorSelected(e){const t=e.data.previousView instanceof UISourceCodeFrame?e.data.previousView:null;t&&t.setSearchableView(null);const o=e.data.currentView instanceof UISourceCodeFrame?e.data.currentView:null;o&&o.setSearchableView(this.searchableViewInternal),this.searchableViewInternal.setReplaceable(Boolean(o?.canEditSource())),this.searchableViewInternal.refreshSearch(),this.updateToolbarChangedListener(),this.updateScriptViewToolbarItems();const r=this.editorContainer.currentFile();r&&this.dispatchEventToListeners(Events.EditorSelected,r)}removeToolbarChangedListener(){this.toolbarChangedListener&&Common.EventTarget.removeEventListeners([this.toolbarChangedListener]),this.toolbarChangedListener=null}updateToolbarChangedListener(){this.removeToolbarChangedListener();const e=this.currentSourceFrame();e&&(this.toolbarChangedListener=e.addEventListener(UISourceCodeFrameEvents.ToolbarItemsChanged,this.updateScriptViewToolbarItems,this))}onSearchCanceled(){this.searchView&&this.searchView.onSearchCanceled(),delete this.searchView,delete this.searchConfig}performSearch(e,t,o){const r=this.currentSourceFrame();r&&(this.searchView=r,this.searchConfig=e,this.searchView.performSearch(this.searchConfig,t,o))}jumpToNextSearchResult(){this.searchView&&(this.searchConfig&&this.searchView!==this.currentSourceFrame()?this.performSearch(this.searchConfig,!0):this.searchView.jumpToNextSearchResult())}jumpToPreviousSearchResult(){if(this.searchView)return this.searchConfig&&this.searchView!==this.currentSourceFrame()?(this.performSearch(this.searchConfig,!0),void(this.searchView&&this.searchView.jumpToLastSearchResult())):void this.searchView.jumpToPreviousSearchResult()}supportsCaseSensitiveSearch(){return!0}supportsRegexSearch(){return!0}replaceSelectionWith(e,t){const o=this.currentSourceFrame();o?o.replaceSelectionWith(e,t):console.assert(Boolean(o))}replaceAllWith(e,t){const o=this.currentSourceFrame();o?o.replaceAllWith(e,t):console.assert(Boolean(o))}showOutlineQuickOpen(){QuickOpen.QuickOpen.QuickOpenImpl.show("@")}showGoToLineQuickOpen(){this.editorContainer.currentFile()&&QuickOpen.QuickOpen.QuickOpenImpl.show(":")}save(){this.saveSourceFrame(this.currentSourceFrame())}saveAll(){this.editorContainer.fileViews().forEach(this.saveSourceFrame.bind(this))}saveSourceFrame(e){if(!(e instanceof UISourceCodeFrame))return;e.commitEditing()}toggleBreakpointsActiveState(e){this.editorContainer.view.element.classList.toggle("breakpoints-deactivated",!e)}}export var Events;!function(e){e.EditorClosed="EditorClosed",e.EditorSelected="EditorSelected"}(Events||(Events={}));const registeredEditorActions=[];export function registerEditorAction(e){registeredEditorActions.push(e)}export function getRegisteredEditorActions(){return registeredEditorActions.map((e=>e()))}let switchFileActionDelegateInstance,actionDelegateInstance;export class SwitchFileActionDelegate{static instance(e={forceNew:null}){const{forceNew:t}=e;return switchFileActionDelegateInstance&&!t||(switchFileActionDelegateInstance=new SwitchFileActionDelegate),switchFileActionDelegateInstance}static nextFile(e){function t(e){const t=e.lastIndexOf(".");return e.substr(0,-1!==t?t:e.length).toLowerCase()}const o=[],r=e.parentURL(),i=e.name(),s=t(i);for(const i of e.project().uiSourceCodes())r===i.parentURL()&&t(i.name())===s&&o.push(i.name());o.sort(Platform.StringUtilities.naturalOrderComparator);const n=Platform.NumberUtilities.mod(o.indexOf(i)+1,o.length),a=Common.ParsedURL.ParsedURL.concatenate(r?Common.ParsedURL.ParsedURL.concatenate(r,"/"):"",o[n]),c=e.project().uiSourceCodeForURL(a);return c!==e?c:null}handleAction(e,t){const o=UI.Context.Context.instance().flavor(SourcesView);if(!o)return!1;const r=o.currentUISourceCode();if(!r)return!1;const i=SwitchFileActionDelegate.nextFile(r);return!!i&&(o.showSourceLocation(i),!0)}}export class ActionDelegate{static instance(e={forceNew:null}){const{forceNew:t}=e;return actionDelegateInstance&&!t||(actionDelegateInstance=new ActionDelegate),actionDelegateInstance}handleAction(e,t){const o=UI.Context.Context.instance().flavor(SourcesView);if(!o)return!1;switch(t){case"sources.close-all":return o.editorContainer.closeAllFiles(),!0;case"sources.jump-to-previous-location":return o.onJumpToPreviousLocation(),!0;case"sources.jump-to-next-location":return o.onJumpToNextLocation(),!0;case"sources.next-editor-tab":return o.editorContainer.selectNextTab(),!0;case"sources.previous-editor-tab":return o.editorContainer.selectPrevTab(),!0;case"sources.close-editor-tab":return o.onCloseEditorTab();case"sources.go-to-line":return o.showGoToLineQuickOpen(),!0;case"sources.go-to-member":return o.showOutlineQuickOpen(),!0;case"sources.save":return o.save(),!0;case"sources.save-all":return o.saveAll(),!0}return!1}}const HEADER_OVERRIDES_FILENAME=".headers";var SourceViewType;!function(e){e.ImageView="ImageView",e.FontView="FontView",e.HeadersView="HeadersView",e.SourceView="SourceView"}(SourceViewType||(SourceViewType={}));