@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 6.52 kB
JavaScript
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 SDK from"../../core/sdk/sdk.js";import*as UI from"../../ui/legacy/legacy.js";import consoleContextSelectorStyles from"./consoleContextSelector.css.js";const UIStrings={javascriptContextNotSelected:"JavaScript context: Not selected",extension:"Extension",javascriptContextS:"JavaScript context: {PH1}"},str_=i18n.i18n.registerUIStrings("panels/console/ConsoleContextSelector.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ConsoleContextSelector{items;dropDown;toolbarItemInternal;constructor(){this.items=new UI.ListModel.ListModel,this.dropDown=new UI.SoftDropDown.SoftDropDown(this.items,this),this.dropDown.setRowHeight(36),this.toolbarItemInternal=new UI.Toolbar.ToolbarItem(this.dropDown.element),this.toolbarItemInternal.setEnabled(!1),this.toolbarItemInternal.setTitle(i18nString(UIStrings.javascriptContextNotSelected)),this.items.addEventListener(UI.ListModel.Events.ItemsReplaced,(()=>this.toolbarItemInternal.setEnabled(Boolean(this.items.length)))),this.toolbarItemInternal.element.classList.add("toolbar-has-dropdown"),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextCreated,this.onExecutionContextCreated,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextChanged,this.onExecutionContextChanged,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.RuntimeModel.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextDestroyed,this.onExecutionContextDestroyed,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.ResourceTreeModel.ResourceTreeModel,SDK.ResourceTreeModel.Events.FrameNavigated,this.frameNavigated,this,{scoped:!0}),UI.Context.Context.instance().addFlavorChangeListener(SDK.RuntimeModel.ExecutionContext,this.executionContextChangedExternally,this),UI.Context.Context.instance().addFlavorChangeListener(SDK.DebuggerModel.CallFrame,this.callFrameSelectedInUI,this),SDK.TargetManager.TargetManager.instance().observeModels(SDK.RuntimeModel.RuntimeModel,this,{scoped:!0}),SDK.TargetManager.TargetManager.instance().addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.CallFrameSelected,this.callFrameSelectedInModel,this)}toolbarItem(){return this.toolbarItemInternal}highlightedItemChanged(e,t,o,n){if(SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight(),t&&t.frameId){const e=SDK.FrameManager.FrameManager.instance().getFrame(t.frameId);e&&!e.isOutermostFrame()&&e.highlight()}o&&o.classList.remove("highlighted"),n&&n.classList.add("highlighted")}titleFor(e){const t=e.target(),o=e.label();let n=o?t.decorateLabel(o):"";if(e.frameId){const o=t.model(SDK.ResourceTreeModel.ResourceTreeModel),r=o&&o.frameForId(e.frameId);r&&(n=n||r.displayName())}return n=n||e.origin,n}depthFor(e){let t=e.target(),o=0;if(e.isDefault||o++,e.frameId){let n=SDK.FrameManager.FrameManager.instance().getFrame(e.frameId);for(;n;)n=n.parentFrame(),n&&(o++,t=n.resourceTreeModel().target())}let n=0,r=t.parentTarget();for(;r&&t.type()!==SDK.Target.Type.ServiceWorker;)n++,t=r,r=t.parentTarget();return o+=n,o}executionContextCreated(e){this.items.insertWithComparator(e,e.runtimeModel.executionContextComparator()),e===UI.Context.Context.instance().flavor(SDK.RuntimeModel.ExecutionContext)&&this.dropDown.selectItem(e)}onExecutionContextCreated(e){const t=e.data;this.executionContextCreated(t)}onExecutionContextChanged(e){const t=e.data;-1!==this.items.indexOf(t)&&(this.executionContextDestroyed(t),this.executionContextCreated(t))}executionContextDestroyed(e){const t=this.items.indexOf(e);-1!==t&&this.items.remove(t)}onExecutionContextDestroyed(e){const t=e.data;this.executionContextDestroyed(t)}executionContextChangedExternally({data:e}){e&&!SDK.TargetManager.TargetManager.instance().isInScope(e.target())||this.dropDown.selectItem(e)}isTopContext(e){if(!e||!e.isDefault)return!1;const t=e.target().model(SDK.ResourceTreeModel.ResourceTreeModel),o=e.frameId&&t&&t.frameForId(e.frameId);return!!o&&o.isOutermostFrame()}hasTopContext(){return this.items.some((e=>this.isTopContext(e)))}modelAdded(e){e.executionContexts().forEach(this.executionContextCreated,this)}modelRemoved(e){for(let t=this.items.length-1;t>=0;t--)this.items.at(t).runtimeModel===e&&this.executionContextDestroyed(this.items.at(t))}createElementForItem(e){const t=document.createElement("div"),o=UI.Utils.createShadowRootWithCoreStyles(t,{cssFile:[consoleContextSelectorStyles],delegatesFocus:void 0}),n=o.createChild("div","title");UI.UIUtils.createTextChild(n,Platform.StringUtilities.trimEndWithMaxLength(this.titleFor(e),100));const r=o.createChild("div","subtitle");return UI.UIUtils.createTextChild(r,this.subtitleFor(e)),t.style.paddingLeft=8+15*this.depthFor(e)+"px",t}subtitleFor(e){const t=e.target();let o=null;if(e.frameId){const n=t.model(SDK.ResourceTreeModel.ResourceTreeModel);o=n&&n.frameForId(e.frameId)}if(e.origin.startsWith("chrome-extension://"))return i18nString(UIStrings.extension);const n=o&&o.sameTargetParentFrame();if(!o||!n||n.securityOrigin!==e.origin){const t=Common.ParsedURL.ParsedURL.fromString(e.origin);if(t)return t.domain()}if(o&&o.securityOrigin){const e=new Common.ParsedURL.ParsedURL(o.securityOrigin).domain();if(e)return e}return"IFrame"}isItemSelectable(e){const t=e.debuggerModel.selectedCallFrame(),o=t&&t.script.executionContext();return!o||e===o}itemSelected(e){this.toolbarItemInternal.element.classList.toggle("highlight",!this.isTopContext(e)&&this.hasTopContext());const t=e?i18nString(UIStrings.javascriptContextS,{PH1:this.titleFor(e)}):i18nString(UIStrings.javascriptContextNotSelected);this.toolbarItemInternal.setTitle(t),UI.Context.Context.instance().setFlavor(SDK.RuntimeModel.ExecutionContext,e)}callFrameSelectedInUI(){const e=UI.Context.Context.instance().flavor(SDK.DebuggerModel.CallFrame),t=e&&e.script.executionContext();t&&UI.Context.Context.instance().setFlavor(SDK.RuntimeModel.ExecutionContext,t)}callFrameSelectedInModel(e){const t=e.data;for(const e of this.items)e.debuggerModel===t&&this.dropDown.refreshItem(e)}frameNavigated(e){const t=e.data,o=t.resourceTreeModel().target().model(SDK.RuntimeModel.RuntimeModel);if(o)for(const e of o.executionContexts())t.id===e.frameId&&this.dropDown.refreshItem(e)}}