This lands the clue collection line on top of origin/dev (v0.32.1, DSH 0.2.2-rc.2). The 14 commits that carried it are squashed into this one because every one of them overlapped upstream's own changes (version, 19 dictionaries, CHANGELOG), and each intermediate commit would have failed to compile against rc.2's menu API. The pre-integration head is kept at backup/clues-uiux-refresh-pre-rebase. What the feature is: - a Clues tab that reads local files, Feishu tasks/docs and GitHub issues/pulls through one source registry, with a scope row, shared search, filters, paging and stable [[dsh-clue:...]] references; - one window host for detail, pack management, source configuration, new pack and sharing (preview and edit are separate paths: a read is never saved over); - .cluepack export/import with per-row resolvability previews; - a source runtime that owns dependency installs and connection authorization (gh, lark-cli device code) and only probes on an explicit read; - the public betterSidebarClues service and the windows capability (features gains clues / windowHost). Merged with upstream rather than over it: - the settings section keeps upstream's start-page takeover group and this branch's clue sources group; navigation is now six groups; - the dictionaries keep both sides' new keys (1211 each, verified equal across all 19), the external-plugin guide and CHANGELOG keep both histories, and service features list both capability sets. Adapted to 0.2.2-rc.2: - menu call sites use anchorPoint instead of getAnchorRect; - the shared SelectMenu mounts the host menu only while open, because rc.2's Menu reads the DOM during render and broke server-rendered settings surfaces; - the submenu flip workaround stays deleted (the host positions submenus now), pinned by an updated layout-css assertion. Version 0.33.0 (three synced spots) with a new 未发布 / Unreleased changelog section; package.json regains the ./clues export, lib/clues.js and the uqr dependency that upstream never had. Gates: typecheck, lint, 5410 unit tests, build, check:consumer-types and the full mount lane (35 passed) against dsh 0.2.2-rc.2 in Chromium.
130 lines
6.0 KiB
TypeScript
130 lines
6.0 KiB
TypeScript
// @vitest-environment jsdom
|
|
/** Native lists retain their semantics and secondary controls while titles share one Tab entry. */
|
|
import { createElement, type KeyboardEvent as ReactKeyboardEvent, type ReactNode } from 'react'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { useListNavigation } from '../src/client/ui/use-list-navigation.ts'
|
|
import { renderRoot, setupReactAct, type RenderedRoot } from './test-utils.ts'
|
|
|
|
setupReactAct()
|
|
|
|
function List({ keys, intercept = false }: { keys: readonly string[]; intercept?: boolean }): ReactNode {
|
|
const navigation = useListNavigation(keys)
|
|
return createElement('ul', {
|
|
ref: navigation.ref,
|
|
onKeyDown: navigation.onKeyDown,
|
|
onFocusCapture: navigation.onFocusCapture,
|
|
onClickCapture: navigation.onClickCapture,
|
|
}, keys.map((key, index) => createElement('li', { key },
|
|
createElement('input', { type: 'checkbox', 'aria-label': 'Select ' + key }),
|
|
createElement('button', {
|
|
'data-list-primary': key,
|
|
tabIndex: navigation.itemTabIndex(index),
|
|
onKeyDown: intercept ? (event: ReactKeyboardEvent<HTMLButtonElement>) => { event.preventDefault() } : undefined,
|
|
}, createElement('span', null, key)),
|
|
createElement('button', { 'data-action': key }, 'Pin'),
|
|
createElement('input', { type: 'text', 'aria-label': 'Rename ' + key }),
|
|
)))
|
|
}
|
|
|
|
let view: RenderedRoot | undefined
|
|
let scroll: ReturnType<typeof vi.fn>
|
|
|
|
beforeEach(() => {
|
|
scroll = vi.fn()
|
|
Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', { configurable: true, value: scroll })
|
|
})
|
|
afterEach(() => { view?.unmount(); view = undefined })
|
|
|
|
function title(key: string): HTMLButtonElement {
|
|
return view!.container.querySelector<HTMLButtonElement>('[data-list-primary="' + key + '"]')!
|
|
}
|
|
function entries(): string[] {
|
|
return [...view!.container.querySelectorAll<HTMLButtonElement>('[data-list-primary]')]
|
|
.filter(button => button.tabIndex === 0).map(button => button.dataset.listPrimary!)
|
|
}
|
|
function keydown(target: HTMLElement, key: string, init: KeyboardEventInit = {}): KeyboardEvent {
|
|
const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, key, ...init })
|
|
act(() => { target.dispatchEvent(event) })
|
|
return event
|
|
}
|
|
|
|
describe('useListNavigation', () => {
|
|
it('keeps native list semantics, one title entry and all secondary controls in Tab order', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b', 'c'] }))
|
|
expect(view.container.querySelector('ul')!.getAttribute('role')).toBeNull()
|
|
expect(entries()).toEqual(['a'])
|
|
expect([...view.container.querySelectorAll<HTMLInputElement>('input')].every(input => input.tabIndex === 0)).toBe(true)
|
|
expect([...view.container.querySelectorAll<HTMLButtonElement>('[data-action]')].every(button => button.tabIndex === 0)).toBe(true)
|
|
expect(keydown(title('a'), 'Tab').defaultPrevented).toBe(false)
|
|
})
|
|
|
|
it('moves titles with arrows/Home/End, scrolls nearest and never wraps', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b', 'c'] }))
|
|
act(() => { title('a').focus() })
|
|
expect(keydown(title('a'), 'ArrowDown').defaultPrevented).toBe(true)
|
|
expect(document.activeElement).toBe(title('b'))
|
|
expect(entries()).toEqual(['b'])
|
|
expect(scroll).toHaveBeenLastCalledWith({ block: 'nearest', inline: 'nearest' })
|
|
keydown(title('b'), 'End')
|
|
expect(document.activeElement).toBe(title('c'))
|
|
keydown(title('c'), 'ArrowDown')
|
|
expect(document.activeElement).toBe(title('c'))
|
|
keydown(title('c'), 'ArrowUp')
|
|
expect(document.activeElement).toBe(title('b'))
|
|
keydown(title('b'), 'Home')
|
|
keydown(title('a'), 'ArrowUp')
|
|
expect(document.activeElement).toBe(title('a'))
|
|
})
|
|
|
|
it('remembers title focus and clicks even when a click does not move focus', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b', 'c'] }))
|
|
act(() => { title('b').focus() })
|
|
expect(entries()).toEqual(['b'])
|
|
act(() => { title('c').querySelector('span')!.click() })
|
|
expect(entries()).toEqual(['c'])
|
|
expect(document.activeElement).toBe(title('b'))
|
|
})
|
|
|
|
it('keeps the same entry after reorder, falls back on removal and handles an empty list', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b', 'c'] }))
|
|
act(() => { title('b').focus() })
|
|
view.rerender(createElement(List, { keys: ['c', 'a', 'b'] }))
|
|
expect(entries()).toEqual(['b'])
|
|
keydown(title('b'), 'ArrowUp')
|
|
expect(document.activeElement).toBe(title('a'))
|
|
view.rerender(createElement(List, { keys: ['c', 'b'] }))
|
|
expect(entries()).toEqual(['c'])
|
|
view.rerender(createElement(List, { keys: [] }))
|
|
expect(entries()).toEqual([])
|
|
view.rerender(createElement(List, { keys: ['d', 'e'] }))
|
|
expect(entries()).toEqual(['d'])
|
|
})
|
|
|
|
it('leaves checkbox, input and inline action keyboard events alone', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b'] }))
|
|
for (const control of view.container.querySelectorAll<HTMLElement>('input, [data-action]')) {
|
|
act(() => { control.focus() })
|
|
for (const key of ['ArrowDown', 'ArrowUp', 'Home', 'End']) {
|
|
expect(keydown(control, key).defaultPrevented).toBe(false)
|
|
expect(document.activeElement).toBe(control)
|
|
}
|
|
}
|
|
expect(scroll).not.toHaveBeenCalled()
|
|
expect(entries()).toEqual(['a'])
|
|
})
|
|
|
|
it('respects composition, repeat, modifiers and keys already handled by a child', () => {
|
|
view = renderRoot(createElement(List, { keys: ['a', 'b'] }))
|
|
act(() => { title('a').focus() })
|
|
for (const init of [{ isComposing: true }, { repeat: true }, { keyCode: 229 }, { ctrlKey: true }, { metaKey: true }, { altKey: true }]) {
|
|
expect(keydown(title('a'), 'ArrowDown', init).defaultPrevented).toBe(false)
|
|
expect(document.activeElement).toBe(title('a'))
|
|
}
|
|
view.rerender(createElement(List, { keys: ['a', 'b'], intercept: true }))
|
|
expect(keydown(title('a'), 'ArrowDown').defaultPrevented).toBe(true)
|
|
expect(document.activeElement).toBe(title('a'))
|
|
expect(scroll).not.toHaveBeenCalled()
|
|
})
|
|
})
|