UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.41 kB
import*as Platform from"../../../core/platform/platform.js";import*as LitHtml from"../../lit-html/lit-html.js";export function isExpandableNode(e){return"children"in e}class TrackDOMNodeToTreeNode extends LitHtml.Directive.Directive{constructor(e){if(super(e),e.type!==LitHtml.Directive.PartType.ATTRIBUTE)throw new Error("TrackDOMNodeToTreeNode directive must be used as an attribute.")}update(e,[t,r]){const n=e.element;if(!(n instanceof HTMLLIElement))throw new Error("trackTreeNodeToDOMNode must be used on <li> elements.");t.set(n,r)}render(e,t){}}export const trackDOMNodeToTreeNode=LitHtml.Directive.directive(TrackDOMNodeToTreeNode);const findNextParentSibling=e=>{const t=e.parentElement?.parentElement;if(t&&t instanceof HTMLLIElement){const e=t.nextElementSibling;return e&&e instanceof HTMLLIElement?e:findNextParentSibling(t)}return null},getFirstChildOfExpandedTreeNode=e=>{const t=e.querySelector(':scope > [role="group"] > [role="treeitem"]:first-child');if(!t)throw new Error("Could not find child of expanded node.");return t},domNodeIsExpandable=e=>null!==e.getAttribute("aria-expanded"),domNodeIsLeafNode=e=>!domNodeIsExpandable(e),domNodeIsExpanded=e=>domNodeIsExpandable(e)&&"true"===e.getAttribute("aria-expanded"),getDeepLastChildOfExpandedTreeNode=e=>{const t=e.querySelector(':scope > [role="group"] > [role="treeitem"]:last-child');if(!t)throw new Error("Could not find child of expanded node.");return domNodeIsExpanded(t)?getDeepLastChildOfExpandedTreeNode(t):t},getNextSiblingOfCurrentDOMNode=e=>{const t=e.nextElementSibling;return t&&t instanceof HTMLLIElement?t:null},getPreviousSiblingOfCurrentDOMNode=e=>{const t=e.previousElementSibling;return t&&t instanceof HTMLLIElement?t:null},getParentListItemForDOMNode=e=>{let t=e.parentElement;if(!t)return null;for(;t&&"treeitem"!==t.getAttribute("role")&&t instanceof HTMLLIElement==!1;)t=t.parentElement;return t},treeNodeChildrenWeakMap=new WeakMap;export const getNodeChildren=async e=>{if(!e.children)throw new Error("Asked for children of node that does not have any children.");const t=treeNodeChildrenWeakMap.get(e);if(t)return t;const r=await e.children();return treeNodeChildrenWeakMap.set(e,r),r};export const getPathToTreeNode=async(e,t)=>{for(const r of e){const e=await getPathToTreeNodeRecursively(r,t,[r]);if(null!==e)return e}return null};const getPathToTreeNodeRecursively=async(e,t,r)=>{if(e.id===t)return r;if(e.children){const n=await getNodeChildren(e);for(const e of n){const n=await getPathToTreeNodeRecursively(e,t,[...r,e]);if(null!==n)return n}}return null};export const findNextNodeForTreeOutlineKeyboardNavigation=e=>{const{currentDOMNode:t,currentTreeNode:r,direction:n,setNodeExpandedState:o}=e;if(!r)return t;if("ArrowDown"===n){if(domNodeIsExpanded(t))return getFirstChildOfExpandedTreeNode(t);const e=getNextSiblingOfCurrentDOMNode(t);if(e)return e;const r=findNextParentSibling(t);if(r)return r}else{if("ArrowRight"===n)return domNodeIsExpandable(t)?domNodeIsExpanded(t)?getFirstChildOfExpandedTreeNode(t):(o(r,!0),t):t;if("ArrowUp"===n){const e=getPreviousSiblingOfCurrentDOMNode(t);if(e)return domNodeIsExpanded(e)?getDeepLastChildOfExpandedTreeNode(e):e;const r=getParentListItemForDOMNode(t);if(r&&r instanceof HTMLLIElement)return r}else if("ArrowLeft"===n){if(domNodeIsExpanded(t))return o(r,!1),t;const e=getParentListItemForDOMNode(t);if(e&&e instanceof HTMLLIElement)return e}}return t};