@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 8.91 kB
JavaScript
import*as Host from"../../core/host/host.js";import*as Root from"../../core/root/root.js";import{ActionRegistry}from"./ActionRegistry.js";import{ShortcutRegistry}from"./ShortcutRegistry.js";import{SoftContextMenu}from"./SoftContextMenu.js";import{deepElementFromEvent}from"./UIUtils.js";export class Item{typeInternal;label;disabled;checked;contextMenu;idInternal;customElement;shortcut;#e;constructor(e,t,n,s,o,i){this.typeInternal=t,this.label=n,this.disabled=s,this.checked=o,this.contextMenu=e,this.idInternal=void 0,this.#e=i,"item"!==t&&"checkbox"!==t||(this.idInternal=e?e.nextId():0)}id(){if(void 0===this.idInternal)throw new Error("Tried to access a ContextMenu Item ID but none was set.");return this.idInternal}type(){return this.typeInternal}isEnabled(){return!this.disabled}setEnabled(e){this.disabled=!e}buildDescriptor(){switch(this.typeInternal){case"item":{const e={type:"item",id:this.idInternal,label:this.label,enabled:!this.disabled,checked:void 0,subItems:void 0,tooltip:this.#e};return this.customElement&&(e.element=this.customElement),this.shortcut&&(e.shortcut=this.shortcut),e}case"separator":return{type:"separator",id:void 0,label:void 0,enabled:void 0,checked:void 0,subItems:void 0};case"checkbox":{const e={type:"checkbox",id:this.idInternal,label:this.label,checked:Boolean(this.checked),enabled:!this.disabled,subItems:void 0};return this.customElement&&(e.element=this.customElement),e}}throw new Error("Invalid item type:"+this.typeInternal)}setShortcut(e){this.shortcut=e}}export class Section{contextMenu;items;constructor(e){this.contextMenu=e,this.items=[]}appendItem(e,t,n,s,o){const i=new Item(this.contextMenu,"item",e,n,void 0,o);return s&&(i.customElement=s),this.items.push(i),this.contextMenu&&this.contextMenu.setHandler(i.id(),t),i}appendCustomItem(e){const t=new Item(this.contextMenu,"item");return t.customElement=e,this.items.push(t),t}appendSeparator(){const e=new Item(this.contextMenu,"separator");return this.items.push(e),e}appendAction(e,t,n){const s=ActionRegistry.instance().action(e);if(!s)return void(n||console.error(`Action ${e} was not defined`));t||(t=s.title());const o=this.appendItem(t,s.execute.bind(s)),i=ShortcutRegistry.instance().shortcutTitleForAction(e);i&&o.setShortcut(i)}appendSubMenuItem(e,t){const n=new SubMenu(this.contextMenu,e,t);return n.init(),this.items.push(n),n}appendCheckboxItem(e,t,n,s,o){const i=new Item(this.contextMenu,"checkbox",e,s,n);return this.items.push(i),this.contextMenu&&this.contextMenu.setHandler(i.id(),t),o&&(i.customElement=o),i}}export class SubMenu extends Item{sections;sectionList;constructor(e,t,n){super(e,"subMenu",t,n),this.sections=new Map,this.sectionList=[]}init(){ContextMenu.groupWeights.forEach((e=>this.section(e)))}section(e){let t=e?this.sections.get(e):null;return t||(t=new Section(this.contextMenu),e?(this.sections.set(e,t),this.sectionList.push(t)):this.sectionList.splice(ContextMenu.groupWeights.indexOf("default"),0,t)),t}headerSection(){return this.section("header")}newSection(){return this.section("new")}revealSection(){return this.section("reveal")}clipboardSection(){return this.section("clipboard")}editSection(){return this.section("edit")}debugSection(){return this.section("debug")}viewSection(){return this.section("view")}defaultSection(){return this.section("default")}overrideSection(){return this.section("override")}saveSection(){return this.section("save")}footerSection(){return this.section("footer")}buildDescriptor(){const e={type:"subMenu",label:this.label,enabled:!this.disabled,subItems:[],id:void 0,checked:void 0},t=this.sectionList.filter((e=>Boolean(e.items.length)));for(const n of t){for(const t of n.items)e.subItems||(e.subItems=[]),e.subItems.push(t.buildDescriptor());n!==t[t.length-1]&&(e.subItems||(e.subItems=[]),e.subItems.push({type:"separator",id:void 0,subItems:void 0,checked:void 0,enabled:void 0,label:void 0}))}return e}appendItemsAtLocation(e){const t=getRegisteredItems();t.sort(((e,t)=>(e.order||0)-(t.order||0)));for(const n of t){if(n.experiment&&!Root.Runtime.experiments.isEnabled(n.experiment))continue;const t=n.location,s=n.actionId;if(!t||!t.startsWith(e+"/"))continue;const o=t.substr(e.length+1);o&&!o.includes("/")&&(s&&this.section(o).appendAction(s))}}static uniqueSectionName=0}export class ContextMenu extends SubMenu{contextMenu;defaultSectionInternal;pendingPromises;pendingTargets;event;useSoftMenu;x;y;onSoftMenuClosed;handlers;idInternal;softMenu;contextMenuLabel;constructor(e,t={}){super(null);const n=e;this.contextMenu=this,super.init(),this.defaultSectionInternal=this.defaultSection(),this.pendingPromises=[],this.pendingTargets=[],this.event=n,this.useSoftMenu=Boolean(t.useSoftMenu),this.x=void 0===t.x?n.x:t.x,this.y=void 0===t.y?n.y:t.y,this.onSoftMenuClosed=t.onSoftMenuClosed,this.handlers=new Map,this.idInternal=0;const s=deepElementFromEvent(e);s&&this.appendApplicableItems(s)}static initialize(){Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu,(function(e){ContextMenu.useSoftMenu=e.data}))}static installHandler(e){e.body.addEventListener("contextmenu",(function(e){new ContextMenu(e).show()}),!1)}nextId(){return this.idInternal++}async show(){ContextMenu.pendingMenu=this,this.event.consume(!0);const e=await Promise.all(this.pendingPromises);if(ContextMenu.pendingMenu===this){ContextMenu.pendingMenu=null;for(let t=0;t<e.length;++t){const n=e[t],s=this.pendingTargets[t];for(const e of n)e.appendApplicableItems(this.event,this,s)}this.pendingPromises=[],this.pendingTargets=[],this.innerShow()}}discard(){this.softMenu&&this.softMenu.discard()}innerShow(){const e=this.buildMenuDescriptors(),t=this.event.target;if(!t)return;const n=t.ownerDocument;if(this.useSoftMenu||ContextMenu.useSoftMenu||Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()){this.softMenu=new SoftContextMenu(e,this.itemSelected.bind(this),void 0,this.onSoftMenuClosed);const s="mouse"===this.event.pointerType&&this.event.button>=0;this.softMenu.setFocusOnTheFirstItem(!s),this.softMenu.show(n,new AnchorBox(this.x,this.y,0,0)),this.contextMenuLabel&&this.softMenu.setContextMenuElementLabel(this.contextMenuLabel)}else{function o(){Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.ContextMenuCleared,this.menuCleared,this),Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected,this.onItemSelected,this)}Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint(this.x,this.y,e,n),queueMicrotask(o.bind(this))}}setContextMenuLabel(e){this.contextMenuLabel=e}setX(e){this.x=e}setY(e){this.y=e}setHandler(e,t){t&&this.handlers.set(e,t)}buildMenuDescriptors(){return super.buildDescriptor().subItems}onItemSelected(e){this.itemSelected(e.data)}itemSelected(e){const t=this.handlers.get(e);t&&t.call(this),this.menuCleared()}menuCleared(){Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(Host.InspectorFrontendHostAPI.Events.ContextMenuCleared,this.menuCleared,this),Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected,this.onItemSelected,this)}containsTarget(e){return this.pendingTargets.indexOf(e)>=0}appendApplicableItems(e){this.pendingPromises.push(loadApplicableRegisteredProviders(e)),this.pendingTargets.push(e)}markAsMenuItemCheckBox(){this.softMenu&&this.softMenu.markAsMenuItemCheckBox()}static pendingMenu=null;static useSoftMenu=!1;static groupWeights=["header","new","reveal","edit","clipboard","debug","view","default","override","save","footer"]}const registeredProviders=[];export function registerProvider(e){registeredProviders.push(e)}async function loadApplicableRegisteredProviders(e){return Promise.all(registeredProviders.filter((function(t){if(!Root.Runtime.Runtime.isDescriptorEnabled({experiment:t.experiment,condition:void 0}))return!1;if(!t.contextTypes)return!0;for(const n of t.contextTypes())if(e instanceof n)return!0;return!1})).map((e=>e.loadProvider())))}const registeredItemsProviders=[];export function registerItem(e){registeredItemsProviders.push(e)}export function maybeRemoveItem(e){const t=registeredItemsProviders.findIndex((t=>t.actionId===e.actionId&&t.location===e.location));return!(t<0)&&(registeredItemsProviders.splice(t,1),!0)}function getRegisteredItems(){return registeredItemsProviders}export var ItemLocation;!function(e){e.DEVICE_MODE_MENU_SAVE="deviceModeMenu/save",e.MAIN_MENU="mainMenu",e.MAIN_MENU_DEFAULT="mainMenu/default",e.MAIN_MENU_FOOTER="mainMenu/footer",e.MAIN_MENU_HELP_DEFAULT="mainMenuHelp/default",e.NAVIGATOR_MENU_DEFAULT="navigatorMenu/default",e.TIMELINE_MENU_OPEN="timelineMenu/open"}(ItemLocation||(ItemLocation={}));