diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 20f0653..0125511 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -981,6 +981,327 @@ if (c.form.requestSubmit) { c.form.requestSubmit(); } else { c.form.submit(); } }); + // ---- searchable select ---------------------------------------------------------------------- + // A long single-choice select (the audit action filter and the like) opens a search field over + // its options. The select stays the source of truth: the picker only sets selectedIndex and fires + // input and change, so forms, the no-JS path and data-autosubmit behave as before. Document-level + // delegation means a live swap needs no setup. + + var SELECT_SEARCH_MIN = 8; // fewer options than this keep the browser's own picker + var SELECT_MIN_WIDTH = 220; // the picker is never narrower than this, in px + var SELECT_ROOM = 260; // less room below the select than this, and more above: the picker goes above + var SELECT_SLOP = 10; // a touch that moves further than this is a scroll, not a tap + var SELECT_LIST_ID = 'select-search-list'; + + var ssPicker = null; // the picker element, made on first use and reused + var ssInput = null; // its search field + var ssList = null; // its listbox + var ssNone = null; // the no-match line + var ssSelect = null; // the select the picker is open for + var ssShowing = false; // whether the picker is open + var ssRows = []; // one entry per option shown: { li, text, norm, group } + var ssGroups = []; // one entry per optgroup: { li, shown } + var ssActiveLi = null; // the highlighted option + var ssTouch = null; // where the current touch started on a select + + // The select an event landed on when it can take the search picker, or null. + function selectTarget(e) { + var el = e.target && e.target.closest ? e.target.closest('select') : null; + if (!el || el.multiple || el.size > 1 || el.disabled || el.options.length < SELECT_SEARCH_MIN) { return null; } + return el; + } + + // The option element an event landed on, or null. + function ssOptionOf(node) { + return node && node.closest ? node.closest('[role="option"]') : null; + } + + // Lower case with the separators removed, so `repo.delete` matches `repo delete` and `user_login`. + function ssNorm(s) { + return s.replace(/[_.\- ]+/g, ''); + } + + // Makes the picker once: a search field over a listbox, appended to the body. + function ssBuild() { + ssPicker = document.createElement('div'); + ssPicker.className = 'select-search'; + ssPicker.setAttribute('popover', 'manual'); + + ssInput = document.createElement('input'); + ssInput.type = 'search'; + ssInput.className = 'select-search-input'; + ssInput.setAttribute('autocomplete', 'off'); + ssInput.setAttribute('spellcheck', 'false'); + ssInput.setAttribute('role', 'combobox'); + ssInput.setAttribute('aria-controls', SELECT_LIST_ID); + ssInput.setAttribute('aria-autocomplete', 'list'); + ssInput.setAttribute('aria-expanded', 'false'); + ssInput.placeholder = document.body.getAttribute('data-select-filter') || ''; + + ssList = document.createElement('ul'); + ssList.className = 'select-search-list'; + ssList.id = SELECT_LIST_ID; + ssList.setAttribute('role', 'listbox'); + + ssNone = document.createElement('li'); + ssNone.className = 'select-search-none'; + ssNone.setAttribute('role', 'presentation'); + ssNone.textContent = document.body.getAttribute('data-select-none') || ''; + ssNone.hidden = true; + + ssPicker.append(ssInput, ssList); + document.body.appendChild(ssPicker); + + ssInput.addEventListener('input', ssFilter, false); + ssInput.addEventListener('keydown', ssKey, false); + ssList.addEventListener('click', function (e) { + var li = ssOptionOf(e.target); + if (li) { ssPick(li); } + }, false); + ssList.addEventListener('mouseover', function (e) { + var li = ssOptionOf(e.target); + if (li && li !== ssActiveLi && li.getAttribute('aria-disabled') !== 'true' && !li.hidden) { ssActivate(li, false); } + }, false); + } + + // Fills the list from the select: an optgroup gets a heading, hidden options are left out. + function ssRender() { + ssList.textContent = ''; + ssRows = []; + ssGroups = []; + ssActiveLi = null; + + function addOption(opt, group) { + if (opt.hidden) { return; } + var li = document.createElement('li'); + li.id = 'select-search-opt-' + opt.index; + li.setAttribute('role', 'option'); + li.setAttribute('data-index', String(opt.index)); + li.setAttribute('aria-selected', opt.index === ssSelect.selectedIndex ? 'true' : 'false'); + if (opt.disabled) { li.setAttribute('aria-disabled', 'true'); } + var text = opt.textContent.trim(); + li.textContent = text; + ssList.appendChild(li); + var lower = text.toLocaleLowerCase(); + ssRows.push({ li: li, text: lower, norm: ssNorm(lower), group: group }); + } + + Array.prototype.forEach.call(ssSelect.children, function (node) { + if (node.tagName === 'OPTGROUP') { + var head = document.createElement('li'); + head.className = 'select-search-group'; + head.setAttribute('role', 'presentation'); + head.textContent = node.label; + ssList.appendChild(head); + var group = { li: head, shown: 0 }; + ssGroups.push(group); + Array.prototype.forEach.call(node.children, function (opt) { + if (opt.tagName === 'OPTION') { addOption(opt, group); } + }); + } else if (node.tagName === 'OPTION') { + addOption(node, null); + } + }); + ssList.appendChild(ssNone); + } + + // Moves the highlight to `li`, scrolling it into view when the keys moved it. + function ssActivate(li, scroll) { + if (ssActiveLi) { ssActiveLi.classList.remove('is-active'); } + ssActiveLi = li; + if (!li) { ssInput.removeAttribute('aria-activedescendant'); return; } + li.classList.add('is-active'); + ssInput.setAttribute('aria-activedescendant', li.id); + if (scroll) { li.scrollIntoView({ block: 'nearest' }); } + } + + // Applies the search: a case-insensitive substring match, also matched without the separators. + // The first enabled option shown becomes the highlight. + function ssFilter() { + var q = ssInput.value.trim().toLocaleLowerCase(); + var nq = ssNorm(q); + var first = null; + var any = false; + ssGroups.forEach(function (g) { g.shown = 0; }); + ssRows.forEach(function (row) { + var show = !q || row.text.indexOf(q) >= 0 || (nq !== '' && row.norm.indexOf(nq) >= 0); + row.li.hidden = !show; + if (!show) { return; } + any = true; + if (row.group) { row.group.shown += 1; } + if (!first && row.li.getAttribute('aria-disabled') !== 'true') { first = row.li; } + }); + ssGroups.forEach(function (g) { g.li.hidden = g.shown === 0; }); + ssNone.hidden = any; + ssActivate(first, true); + } + + // The enabled options on screen, in order: what the arrow keys move through. + function ssOffered() { + return ssRows.filter(function (row) { + return !row.li.hidden && row.li.getAttribute('aria-disabled') !== 'true'; + }).map(function (row) { return row.li; }); + } + + function ssStep(dir) { + var list = ssOffered(); + if (!list.length) { return; } + var at = list.indexOf(ssActiveLi); + var next = at < 0 ? (dir > 0 ? 0 : list.length - 1) : Math.max(0, Math.min(list.length - 1, at + dir)); + ssActivate(list[next], true); + } + + // Keys in the search field. Home and End stay with the text. + function ssKey(e) { + if (e.key === 'ArrowDown') { e.preventDefault(); ssStep(1); } + else if (e.key === 'ArrowUp') { e.preventDefault(); ssStep(-1); } + else if (e.key === 'Enter') { e.preventDefault(); if (ssActiveLi) { ssPick(ssActiveLi); } } + else if (e.key === 'Escape') { + e.preventDefault(); + e.stopPropagation(); + var back = ssSelect; + ssClose(); + if (back) { back.focus({ preventScroll: true }); } + } + else if (e.key === 'Tab') { ssClose(); } + } + + // Places the picker under the select, or above it when there is less room below. + function ssPosition() { + if (!ssSelect || !ssSelect.isConnected) { ssClose(); return; } + var r = ssSelect.getBoundingClientRect(); + var below = window.innerHeight - r.bottom; + var width = Math.max(r.width, SELECT_MIN_WIDTH); + var left = Math.max(8, Math.min(r.left, window.innerWidth - width - 8)); + ssPicker.style.left = left + 'px'; + ssPicker.style.width = width + 'px'; + if (below < SELECT_ROOM && r.top > below) { + ssPicker.style.top = 'auto'; + ssPicker.style.bottom = (window.innerHeight - r.top + 4) + 'px'; + } else { + ssPicker.style.bottom = 'auto'; + ssPicker.style.top = (r.bottom + 4) + 'px'; + } + } + + // Opens the picker for `sel`, with `prefill` as the search (a typed character). + function ssOpen(sel, prefill) { + if (!ssPicker) { ssBuild(); } + if (ssShowing && ssSelect === sel) { ssInput.focus({ preventScroll: true }); return; } + ssSelect = sel; + ssRender(); + ssInput.value = prefill || ''; + ssFilter(); + ssPosition(); + if (!ssShowing) { + ssShowing = true; + if (ssPicker.showPopover) { ssPicker.showPopover(); } else { ssPicker.classList.add('is-open'); } + } + ssInput.setAttribute('aria-expanded', 'true'); + ssInput.focus({ preventScroll: true }); + var end = ssInput.value.length; + ssInput.setSelectionRange(end, end); + } + + // Closes the picker and forgets its select. The select keeps its value and focus. + function ssClose() { + if (!ssPicker) { return; } + if (ssShowing) { + ssShowing = false; + if (ssPicker.hidePopover) { ssPicker.hidePopover(); } else { ssPicker.classList.remove('is-open'); } + } + ssList.textContent = ''; + ssRows = []; + ssGroups = []; + ssActiveLi = null; + ssInput.value = ''; + ssInput.setAttribute('aria-expanded', 'false'); + ssInput.removeAttribute('aria-activedescendant'); + ssSelect = null; + } + + // Picks an option: the select takes it and fires input and change, as a native choice does. + function ssPick(li) { + if (li.getAttribute('aria-disabled') === 'true' || !ssSelect) { return; } + var sel = ssSelect; + var index = parseInt(li.getAttribute('data-index'), 10); + if (index !== sel.selectedIndex) { + sel.selectedIndex = index; + sel.dispatchEvent(new Event('input', { bubbles: true })); + sel.dispatchEvent(new Event('change', { bubbles: true })); + } + ssClose(); + sel.focus({ preventScroll: true }); + } + + // A mouse press on a select opens the picker (the press is kept from the native one). A press on + // an option keeps focus in the field. A press anywhere else closes the picker. + document.addEventListener('mousedown', function (e) { + var node = e.target && e.target.nodeType === 1 ? e.target : null; + if (ssShowing && node && ssPicker.contains(node)) { + if (ssOptionOf(node)) { e.preventDefault(); } + return; + } + var sel = e.button === 0 ? selectTarget(e) : null; + if (sel) { + e.preventDefault(); + sel.focus({ preventScroll: true }); + // A second press on the select shuts the picker, as it does the native one. + if (ssShowing && ssSelect === sel) { ssClose(); } else { ssOpen(sel, ''); } + return; + } + if (ssShowing) { ssClose(); } + }, false); + + // Keys on a focused select: Enter, Space, F4 and Alt with an arrow open the picker; a printable + // character opens it with that character as the search. Other keys keep the browser's own behaviour. + document.addEventListener('keydown', function (e) { + var sel = selectTarget(e); + if (!sel || e.ctrlKey || e.metaKey) { return; } + var key = e.key; + if (key === 'Enter' || key === ' ' || key === 'Spacebar' || key === 'F4' || ((key === 'ArrowDown' || key === 'ArrowUp') && e.altKey)) { + e.preventDefault(); + ssOpen(sel, ''); + } else if (typeof key === 'string' && key.length === 1 && !e.altKey) { + e.preventDefault(); + ssOpen(sel, key); + } + }, false); + + // Touch: iOS ignores a prevented mousedown, so a tap on a select (a touch that does not move) opens + // the picker from touchend, which also keeps the native picker from opening. + document.addEventListener('touchstart', function (e) { + var sel = selectTarget(e); + var t = sel && e.touches && e.touches.length === 1 ? e.touches[0] : null; + ssTouch = t ? { sel: sel, x: t.clientX, y: t.clientY } : null; + }, { passive: true }); + + document.addEventListener('touchend', function (e) { + var start = ssTouch; + ssTouch = null; + if (!start || !e.changedTouches || !e.changedTouches.length) { return; } + if (selectTarget(e) !== start.sel) { return; } + var t = e.changedTouches[0]; + if (Math.abs(t.clientX - start.x) >= SELECT_SLOP || Math.abs(t.clientY - start.y) >= SELECT_SLOP) { return; } + e.preventDefault(); + start.sel.focus({ preventScroll: true }); + ssOpen(start.sel, ''); + }, { passive: false }); + + // The picker follows its select when the page scrolls or resizes (a phone scrolls the field into + // view when its keyboard opens). A scroll inside the picker (its list) does not move it. + function ssFollow(e) { + if (!ssShowing) { return; } + var t = e && e.target; + if (t && t.nodeType === 1 && ssPicker.contains(t)) { return; } + ssPosition(); + } + window.addEventListener('resize', ssFollow, false); + document.addEventListener('scroll', ssFollow, true); + + // A re-rendered page (a live swap) closes the picker: its select may be gone. + document.addEventListener('forge:swapped', function () { ssClose(); }); + // Appearance page: a colour or theme choice shows on at once, before the form's request returns. // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light. var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' }; diff --git a/crates/forge-web/src/i18n/link.rs b/crates/forge-web/src/i18n/link.rs index d8580d1..c3d6b1d 100644 --- a/crates/forge-web/src/i18n/link.rs +++ b/crates/forge-web/src/i18n/link.rs @@ -15,6 +15,10 @@ pub enum K { DraftRestored, /// The button that drops a restored draft. DraftDiscard, + /// The placeholder of the search field over a long select dropdown. + SelectFilter, + /// The line a select search shows when no option matches. + SelectNone, } /// The phrase for `key`. @@ -33,6 +37,10 @@ pub fn t(lang: Lang, key: K) -> &'static str { (DraftRestored, Lang::Tr) => "Taslak geri yüklendi", (DraftDiscard, Lang::En) => "Discard", (DraftDiscard, Lang::Tr) => "Vazgeç", + (SelectFilter, Lang::En) => "Search\u{2026}", + (SelectFilter, Lang::Tr) => "Ara\u{2026}", + (SelectNone, Lang::En) => "No matches", + (SelectNone, Lang::Tr) => "Eşleşme yok", } } @@ -43,7 +51,7 @@ mod tests { #[test] fn every_string_has_no_em_dash() { for lang in [Lang::En, Lang::Tr] { - for key in [K::Restarting, K::BackSubmit, K::Saved, K::Done, K::DraftRestored, K::DraftDiscard] { + for key in [K::Restarting, K::BackSubmit, K::Saved, K::Done, K::DraftRestored, K::DraftDiscard, K::SelectFilter, K::SelectNone] { assert!(!t(lang, key).contains('\u{2014}')); } } diff --git a/crates/forge-web/src/views/mod.rs b/crates/forge-web/src/views/mod.rs index 4be2587..47f306e 100644 --- a/crates/forge-web/src/views/mod.rs +++ b/crates/forge-web/src/views/mod.rs @@ -664,7 +664,7 @@ pub async fn shell( } -
+