@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 6.99 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 Diff from"../../../../third_party/diff/diff.js";import*as UI from"../../legacy.js";import{FilteredListWidget,Provider,registerProvider}from"./FilteredListWidget.js";import{QuickOpenImpl}from"./QuickOpen.js";const UIStrings={oneOrMoreSettingsHaveChanged:"One or more settings have changed which requires a reload to take effect.",noCommandsFound:"No commands found",run:"Run",command:"Command",deprecated:"— deprecated"},str_=i18n.i18n.registerUIStrings("ui/legacy/components/quick_open/CommandMenu.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let commandMenuInstance,showActionDelegateInstance;export class CommandMenu{commandsInternal;constructor(){this.commandsInternal=[],this.loadCommands()}static instance(e={forceNew:null}){const{forceNew:t}=e;return commandMenuInstance&&!t||(commandMenuInstance=new CommandMenu),commandMenuInstance}static createCommand(e){const{category:t,keys:n,title:o,shortcut:r,executeHandler:a,availableHandler:i,userActionCode:s,deprecationWarning:c,isPanelOrDrawer:m}=e;let d=a;if(s){const e=s;d=()=>{Host.userMetrics.actionTaken(e),a()}}return new Command(t,o,n,r,d,i,c,m)}static createSettingCommand(e,t,n){const o=e.category();if(!o)throw new Error(`Creating '${t}' setting command failed. Setting has no category.`);const r=e.tags()||"",a=Boolean(e.reloadRequired());return CommandMenu.createCommand({category:Common.Settings.getLocalizedSettingsCategory(o),keys:r,title:t,shortcut:"",executeHandler:()=>{!e.deprecation?.disabled||e.deprecation?.experiment&&!e.deprecation.experiment.isEnabled()?(e.set(n),a&&UI.InspectorView.InspectorView.instance().displayReloadRequiredWarning(i18nString(UIStrings.oneOrMoreSettingsHaveChanged))):Common.Revealer.reveal(e)},availableHandler:function(){return e.get()!==n},userActionCode:void 0,deprecationWarning:e.deprecation?.warning})}static createActionCommand(e){const{action:t,userActionCode:n}=e,o=t.category();if(!o)throw new Error(`Creating '${t.title()}' action command failed. Action has no category.`);let r;o===UI.ActionRegistration.ActionCategory.DRAWER&&(r=PanelOrDrawer.DRAWER);const a=UI.ShortcutRegistry.ShortcutRegistry.instance().shortcutTitleForAction(t.id())||"";return CommandMenu.createCommand({category:UI.ActionRegistration.getLocalizedActionCategory(o),keys:t.tags()||"",title:t.title(),shortcut:a,executeHandler:t.execute.bind(t),userActionCode:n,availableHandler:void 0,isPanelOrDrawer:r})}static createRevealViewCommand(e){const{title:t,tags:n,category:o,userActionCode:r,id:a}=e;if(!o)throw new Error(`Creating '${t}' reveal view command failed. Reveal view has no category.`);let i;o===UI.ViewManager.ViewLocationCategory.PANEL?i=PanelOrDrawer.PANEL:o===UI.ViewManager.ViewLocationCategory.DRAWER&&(i=PanelOrDrawer.DRAWER);return CommandMenu.createCommand({category:UI.ViewManager.getLocalizedViewLocationCategory(o),keys:n,title:t,shortcut:"",executeHandler:()=>("issues-pane"===a&&Host.userMetrics.issuesPanelOpenedFrom(Host.UserMetrics.IssueOpener.CommandMenu),UI.ViewManager.ViewManager.instance().showView(a,!0)),userActionCode:r,availableHandler:void 0,isPanelOrDrawer:i})}loadCommands(){const e=new Map;for(const{category:t,name:n}of UI.ViewManager.getRegisteredLocationResolvers())t&&n&&e.set(n,t);const t=UI.ViewManager.getRegisteredViewExtensions();for(const n of t){const t=n.location(),o=t&&e.get(t);if(!o)continue;const r={title:n.commandPrompt(),tags:n.tags()||"",category:o,userActionCode:void 0,id:n.viewId()};this.commandsInternal.push(CommandMenu.createRevealViewCommand(r))}const n=Common.Settings.getRegisteredSettings();for(const e of n){const t=e.options;if(t&&e.category)for(const n of t){const t=Common.Settings.Settings.instance().moduleSetting(e.settingName);this.commandsInternal.push(CommandMenu.createSettingCommand(t,n.title(),n.value))}}}commands(){return this.commandsInternal}}export var PanelOrDrawer;!function(e){e.PANEL="PANEL",e.DRAWER="DRAWER"}(PanelOrDrawer||(PanelOrDrawer={}));export class CommandMenuProvider extends Provider{commands;constructor(e=[]){super(),this.commands=e}attach(){const e=CommandMenu.instance().commands(),t=UI.ActionRegistry.ActionRegistry.instance().availableActions();for(const e of t){if(!e.category())continue;const t={action:e,userActionCode:void 0};this.commands.push(CommandMenu.createActionCommand(t))}for(const t of e)t.available()&&this.commands.push(t);this.commands=this.commands.sort((function(e,t){const n=Platform.StringUtilities.compare(e.category,t.category);return n||Platform.StringUtilities.compare(e.title,t.title)}))}detach(){this.commands=[]}itemCount(){return this.commands.length}itemKeyAt(e){return this.commands[e].key}itemScoreAt(e,t){const n=this.commands[e];let o=Diff.Diff.DiffWrapper.characterScore(t.toLowerCase(),n.title.toLowerCase());return n.isPanelOrDrawer===PanelOrDrawer.PANEL?o+=2:n.isPanelOrDrawer===PanelOrDrawer.DRAWER&&(o+=1),o}renderItem(e,t,n,o){const r=this.commands[e];n.removeChildren(),UI.UIUtils.createTextChild(n,r.title),FilteredListWidget.highlightRanges(n,t,!0),o.textContent=r.shortcut;const a=r.deprecationWarning;if(a){const e=n.parentElement?.createChild("span","deprecated-tag");e&&(e.textContent=i18nString(UIStrings.deprecated),e.title=a)}const i=n.parentElement?.parentElement?.createChild("span","tag");if(!i)return;const s=Platform.StringUtilities.hashCode(r.category)%MaterialPaletteColors.length;i.style.backgroundColor=MaterialPaletteColors[s],i.style.color="var(--color-background)",i.textContent=r.category}selectItem(e,t){null!==e&&(this.commands[e].execute(),Host.userMetrics.actionTaken(Host.UserMetrics.Action.SelectCommandFromCommandMenu))}notFoundText(){return i18nString(UIStrings.noCommandsFound)}}export const MaterialPaletteColors=["#F44336","#E91E63","#9C27B0","#673AB7","#3F51B5","#03A9F4","#00BCD4","#009688","#4CAF50","#8BC34A","#CDDC39","#FFC107","#FF9800","#FF5722","#795548","#9E9E9E","#607D8B"];export class Command{category;title;key;shortcut;deprecationWarning;isPanelOrDrawer;#e;#t;constructor(e,t,n,o,r,a,i,s){this.category=e,this.title=t,this.key=e+"\0"+t+"\0"+n,this.shortcut=o,this.#e=r,this.#t=a,this.deprecationWarning=i,this.isPanelOrDrawer=s}available(){return!this.#t||this.#t()}execute(){return this.#e()}}export class ShowActionDelegate{static instance(e={forceNew:null}){const{forceNew:t}=e;return showActionDelegateInstance&&!t||(showActionDelegateInstance=new ShowActionDelegate),showActionDelegateInstance}handleAction(e,t){return Host.InspectorFrontendHost.InspectorFrontendHostInstance.bringToFront(),QuickOpenImpl.show(">"),!0}}registerProvider({prefix:">",iconName:"chevron-right",iconWidth:"20px",provider:()=>Promise.resolve(new CommandMenuProvider),titlePrefix:()=>i18nString(UIStrings.run),titleSuggestion:()=>i18nString(UIStrings.command)});