UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 4.49 kB
import{DeferredDOMNode}from"./DOMModel.js";import{Capability}from"./Target.js";import{SDKModel}from"./SDKModel.js";export var CoreAxPropertyName;!function(e){e.Name="name",e.Description="description",e.Value="value",e.Role="role"}(CoreAxPropertyName||(CoreAxPropertyName={}));export class AccessibilityNode{#e;#t;#n;#r;#d;#o;#s;#i;#a;#l;#h;#c;#I;#u;constructor(e,t){this.#e=e,this.#t=t.nodeId,e.setAXNodeForAXId(this.#t,this),t.backendDOMNodeId?(e.setAXNodeForBackendDOMNodeId(t.backendDOMNodeId,this),this.#n=t.backendDOMNodeId,this.#r=new DeferredDOMNode(e.target(),t.backendDOMNodeId)):(this.#n=null,this.#r=null),this.#d=t.ignored,this.#d&&"ignoredReasons"in t&&(this.#o=t.ignoredReasons),this.#s=t.role||null,this.#i=t.name||null,this.#a=t.description||null,this.#l=t.value||null,this.#h=t.properties||null,this.#u=t.childIds||null,this.#c=t.parentId||null,t.frameId&&!t.parentId?(this.#I=t.frameId,e.setRootAXNodeForFrameId(t.frameId,this)):this.#I=null}id(){return this.#t}accessibilityModel(){return this.#e}ignored(){return this.#d}ignoredReasons(){return this.#o||null}role(){return this.#s||null}coreProperties(){const e=[];return this.#i&&e.push({name:CoreAxPropertyName.Name,value:this.#i}),this.#a&&e.push({name:CoreAxPropertyName.Description,value:this.#a}),this.#l&&e.push({name:CoreAxPropertyName.Value,value:this.#l}),e}name(){return this.#i||null}description(){return this.#a||null}value(){return this.#l||null}properties(){return this.#h||null}parentNode(){return this.#c?this.#e.axNodeForId(this.#c):null}isDOMNode(){return Boolean(this.#n)}backendDOMNodeId(){return this.#n}deferredDOMNode(){return this.#r}highlightDOMNode(){const e=this.deferredDOMNode();e&&e.highlight()}children(){if(!this.#u)return[];const e=[];for(const t of this.#u){const n=this.#e.axNodeForId(t);n&&e.push(n)}return e}numChildren(){return this.#u?this.#u.length:0}hasOnlyUnloadedChildren(){return!(!this.#u||!this.#u.length)&&this.#u.every((e=>null===this.#e.axNodeForId(e)))}hasUnloadedChildren(){return!(!this.#u||!this.#u.length)&&this.#u.some((e=>null===this.#e.axNodeForId(e)))}getFrameId(){return this.#I||this.parentNode()?.getFrameId()||null}}export var Events;!function(e){e.TreeUpdated="TreeUpdated"}(Events||(Events={}));export class AccessibilityModel extends SDKModel{agent;#N;#p;#m;#g;#M;constructor(e){super(e),e.registerAccessibilityDispatcher(this),this.agent=e.accessibilityAgent(),this.resumeModel(),this.#N=new Map,this.#p=new Map,this.#m=new Map,this.#g=new Map,this.#M=null}clear(){this.#M=null,this.#N.clear(),this.#p.clear(),this.#m.clear()}async resumeModel(){await this.agent.invoke_enable()}async suspendModel(){await this.agent.invoke_disable()}async requestPartialAXTree(e){const{nodes:t}=await this.agent.invoke_getPartialAXTree({nodeId:e.id,fetchRelatives:!0});if(!t)return;const n=[];for(const e of t)n.push(new AccessibilityNode(this,e))}loadComplete({root:e}){this.clear(),this.#M=new AccessibilityNode(this,e),this.dispatchEventToListeners(Events.TreeUpdated,{root:this.#M})}nodesUpdated({nodes:e}){this.createNodesFromPayload(e),this.dispatchEventToListeners(Events.TreeUpdated,{})}createNodesFromPayload(e){return e.map((e=>new AccessibilityNode(this,e)))}async requestRootNode(e){if(e&&this.#m.has(e))return this.#m.get(e);if(!e&&this.#M)return this.#M;const{node:t}=await this.agent.invoke_getRootAXNode({frameId:e});return t?this.createNodesFromPayload([t])[0]:void 0}async requestAXChildren(e,t){const n=this.#N.get(e);if(!n)throw Error("Cannot request children before parent");if(!n.hasUnloadedChildren())return n.children();const r=this.#g.get(e);if(r)await r;else{const n=this.agent.invoke_getChildAXNodes({id:e,frameId:t});this.#g.set(e,n);const r=await n;r.getError()||(this.createNodesFromPayload(r.nodes),this.#g.delete(e))}return n.children()}async requestAndLoadSubTreeToNode(e){const t=[];let n=this.axNodeForDOMNode(e);for(;n;){t.push(n);const e=n.parentNode();if(!e)return t;n=e}const{nodes:r}=await this.agent.invoke_getAXNodeAndAncestors({backendNodeId:e.backendNodeId()});if(!r)return null;return this.createNodesFromPayload(r)}axNodeForId(e){return this.#N.get(e)||null}setRootAXNodeForFrameId(e,t){this.#m.set(e,t)}axNodeForFrameId(e){return this.#m.get(e)??null}setAXNodeForAXId(e,t){this.#N.set(e,t)}axNodeForDOMNode(e){return e?this.#p.get(e.backendNodeId())??null:null}setAXNodeForBackendDOMNodeId(e,t){this.#p.set(e,t)}getAgent(){return this.agent}}SDKModel.register(AccessibilityModel,{capabilities:Capability.DOM,autostart:!1});