(() => { const bar = document.querySelector('[data-command-bar]'), paths = window.PagesWikiPaths; if (!bar || !paths) return; document.body.append(bar); // Escape transformed/clipped headers. const input = bar.querySelector('input'), list = bar.querySelector('[role=listbox]'), status = bar.querySelector('[role=status]'); const browser = bar.dataset.browser === 'true', current = bar.dataset.commandPath || '/'; let previous, request, generation = 0, options = [], index = -1, seed = '', completing = false, composing = false; const interactive = target => target?.closest?.('input,textarea,select,button,a,summary,[contenteditable]:not([contenteditable=false]),[role=textbox],[role=button],.cm-editor'); const reset = () => { generation++; request?.abort(); completing = false; options = []; index = -1; list.replaceChildren(); list.hidden = true; status.textContent = ''; input.setAttribute('aria-expanded', 'false'); input.removeAttribute('aria-activedescendant'); }; const open = text => { previous = document.activeElement; bar.hidden = false; reset(); input.value = text; input.focus(); input.setSelectionRange(input.value.length, input.value.length); }; const close = () => { reset(); input.value = ''; if (!browser) bar.hidden = true; input.blur(); if (previous?.isConnected && !bar.contains(previous)) previous.focus(); }; const choose = i => { index = (i + options.length) % options.length; input.value = options[index].value; input.setSelectionRange(input.value.length, input.value.length); [...list.children].forEach((el,n) => el.setAttribute('aria-selected', String(n === index))); input.setAttribute('aria-activedescendant', list.children[index].id); list.children[index].scrollIntoView({block:'nearest'}); }; async function complete(backwards) { if (options.length && !(options.length === 1 && input.value.endsWith('/')) && (input.value === seed || input.value === options[index]?.value)) { choose(index + (backwards ? -1 : 1)); return; } if (completing) return; reset(); seed = input.value; const match = /^(cd|vi|view)\s+(.*)$/s.exec(seed); if (!match || input.selectionStart !== input.value.length || input.selectionEnd !== input.value.length) return; const raw = match[2], slash = raw.lastIndexOf('/'); const directory = slash < 0 ? '' : raw.slice(0, slash + 1), prefix = raw.slice(slash + 1); let parent; try { parent = paths.resolvePath(directory || '.', current); } catch { status.textContent = bar.dataset.invalid; return; } const ticket = generation; request = new AbortController(); completing = true; try { const url = '/_/api/v1/path-picker?' + new URLSearchParams({path:parent, mode:'children', prefix}); const response = await fetch(url, {signal:request.signal, headers:{Accept:'application/json'}, cache:'no-store'}); if (!response.ok || !response.headers.get('content-type')?.includes('application/json')) throw Error('unavailable'); const data = await response.json(); if (ticket !== generation || input.value !== seed) return; options = (data.entries || []).filter(e => typeof e.name === 'string' && e.name.toLowerCase().startsWith(prefix.toLowerCase())).slice(0,100).map(e => ({value:match[1] + ' ' + directory + e.name + (e.kind === 'folder' ? '/' : ''), label:e.name + (e.kind === 'folder' ? '/' : '')})); status.textContent = !options.length ? bar.dataset.empty : data.truncated || data.entries.length > 100 ? bar.dataset.more : ''; for (const [i,item] of options.entries()) { const option = document.createElement('div'); option.role = 'option'; option.id = 'wiki-command-option-' + i; option.textContent = item.label; option.addEventListener('mousedown', event => event.preventDefault()); option.addEventListener('click', () => { choose(i); input.focus(); }); list.append(option); } list.hidden = !options.length; input.setAttribute('aria-expanded', String(!!options.length)); if (options.length) choose(backwards ? options.length - 1 : 0); } catch (error) { if (ticket === generation && error.name !== 'AbortError') status.textContent = bar.dataset.unavailable; } finally { if (ticket === generation) completing = false; } } bar.addEventListener('submit', event => { event.preventDefault(); if (composing || !input.value.trim()) return; try { if (!/^(cd|vi|view)(?:\s|$)/.test(input.value.trim())) throw Error('invalid'); location.assign(paths.destination(input.value, current).href); } catch { status.textContent = bar.dataset.invalid; } }); input.addEventListener('input', reset); input.addEventListener('compositionstart', () => { composing = true; reset(); }); input.addEventListener('compositionend', () => { composing = false; }); bar.addEventListener('focusout', event => { if (!bar.contains(event.relatedTarget)) reset(); }); input.addEventListener('keydown', event => { if (event.isComposing || event.keyCode === 229 || event.ctrlKey || event.metaKey || event.altKey) return; if (event.key === 'Escape') { event.preventDefault(); close(); } if (event.key === 'Tab' && /^(cd|vi|view)\s/.test(input.value) && input.selectionStart === input.value.length && input.selectionEnd === input.value.length) { event.preventDefault(); complete(event.shiftKey); } if (options.length && ['ArrowDown','ArrowUp'].includes(event.key)) { event.preventDefault(); choose(index + (event.key === 'ArrowDown' ? 1 : -1)); } }); bar.querySelector('[data-command-close]').addEventListener('click', close); document.addEventListener('keydown', event => { if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || event.repeat || interactive(event.target) || document.querySelector('dialog[open],.auth-menu details[open],.theme-picker[open]') || String(window.getSelection() || '')) return; if (browser && (event.key === 'Process' || event.keyCode === 229)) { open(''); return; } if (event.isComposing) return; if (!browser && event.key === 'g') { event.preventDefault(); open(''); } else if (browser && event.key.length === 1) { event.preventDefault(); open(event.key); } }, true); })();