UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 3.85 kB
import*as Common from"../../../core/common/common.js";import*as Platform from"../../../core/platform/platform.js";import{assertNotNullOrUndefined}from"../../../core/platform/platform.js";const SUMMARY_ELEMENT_SELECTOR="summary",domNodeIsTree=e=>"tree"===e.getAttribute("role"),domNodeIsBreakpointItemNode=e=>"treeitem"===e.getAttribute("role"),domNodeIsPauseOnExceptionsNode=e=>null!==e.getAttribute("data-first-pause")||null!==e.getAttribute("data-last-pause"),domNodeIsSummaryNode=e=>!domNodeIsBreakpointItemNode(e),groupIsExpanded=e=>null!==e.getAttribute("open"),getFirstBreakpointItemInGroup=e=>e.querySelector("[data-first-breakpoint]"),getLastBreakpointItemInGroup=e=>e.querySelector("[data-last-breakpoint]"),getNextGroupsSummaryNode=e=>{const t=getNextDetailsElement(e);return t&&t instanceof HTMLDetailsElement?t?.querySelector("summary"):null},getCurrentSummaryNode=e=>e.querySelector("summary"),getNextDetailsElement=e=>{const t=e.nextElementSibling;return t&&t instanceof HTMLDetailsElement?t:null},getPreviousDetailsElement=e=>{const t=e.previousElementSibling;return t&&t instanceof HTMLDetailsElement?t:null};function findNextNodeForPauseOnExceptions(e,t){console.assert(domNodeIsPauseOnExceptionsNode(e));let n=null;switch(t){case"ArrowUp":{const t=e.previousElementSibling;t instanceof HTMLElement&&(n=t,console.assert(domNodeIsPauseOnExceptionsNode(n)));break}case"ArrowDown":{const t=e.nextElementSibling;if(t instanceof HTMLElement)if(domNodeIsTree(t)){const e=t.querySelector("[data-first-group]");e&&(n=getCurrentSummaryNode(e))}else n=t,console.assert(domNodeIsPauseOnExceptionsNode(n));break}}return n}export async function findNextNodeForKeyboardNavigation(e,t,n){if(domNodeIsPauseOnExceptionsNode(e))return findNextNodeForPauseOnExceptions(e,t);const o=e.parentElement;if(!(o&&o instanceof HTMLDetailsElement))throw new Error("The selected nodes should be direct children of an HTMLDetails element.");let r=null;switch(t){case"ArrowLeft":if(!domNodeIsSummaryNode(e))return getCurrentSummaryNode(o);groupIsExpanded(o)&&await n(o,!1);break;case"ArrowRight":if(domNodeIsSummaryNode(e)){if(groupIsExpanded(o))return getFirstBreakpointItemInGroup(o);await n(o,!0)}break;case"ArrowDown":if(domNodeIsSummaryNode(e))r=groupIsExpanded(o)?getFirstBreakpointItemInGroup(o):getNextGroupsSummaryNode(o);else{const t=e.nextElementSibling;r=t&&t instanceof HTMLDivElement?t:getNextGroupsSummaryNode(o)}break;case"ArrowUp":if(domNodeIsSummaryNode(e)){const e=getPreviousDetailsElement(o);if(e)r=groupIsExpanded(e)?getLastBreakpointItemInGroup(e):getCurrentSummaryNode(e);else{const e=o.parentElement?.previousElementSibling;e instanceof HTMLElement&&(r=e)}}else{const t=e.previousElementSibling;t instanceof HTMLElement&&(r=t)}}return r}function findFirstDifferingSegmentIndex(e){const t=e[0];let n=-1;for(let o=0;o<t.length&&-1===n;++o){const r=t[o];for(let t=1;t<e.length;++t){const s=e[t];if(s.length<=o||s[o]!==r){n=o;break}}}return-1===n?t.length:n}function findDifferentiatingPath(e,t,n){const o=[];let r=t.filter((t=>t!==e));for(let t=n;t<e.length;++t){const n=e[t];if(o.push(n),r=r.filter((e=>e.length>t&&e[t]===n)),0===r.length)break}return o}function populateDifferentiatingPathMap(e,t){const n=e.map((e=>{const t=Common.ParsedURL.ParsedURL.fromString(e)?.folderPathComponents.slice(1);return assertNotNullOrUndefined(t),t.split("/").reverse()})),o=findFirstDifferingSegmentIndex(n);for(let r=0;r<n.length;++r){const s=findDifferentiatingPath(n[r],n,o).reverse().join("/");0===o?t.set(e[r],s+"/"):t.set(e[r],s+"/…/")}console.assert(new Set(t.values()).size===e.length,"Differentiators should be unique.")}export function getDifferentiatingPathMap(e){const t=new Map,n=new Map;for(const{name:n,url:o}of e)t.has(n)||t.set(n,[]),t.get(n)?.push(o);for(const e of t.values())e.length>1&&populateDifferentiatingPathMap(e,n);return n}