Refactored search keyboard handlers

This commit is contained in:
squidfunk 2020-02-14 11:33:59 +01:00
parent 8c92565b7b
commit f150a6c9b5
6 changed files with 108 additions and 108 deletions

View File

@ -25,6 +25,7 @@ import { map, shareReplay, switchMap } from "rxjs/operators"
import { SearchResult } from "modules" import { SearchResult } from "modules"
import { import {
Key,
SearchQuery, SearchQuery,
Viewport, Viewport,
WorkerHandler, WorkerHandler,
@ -57,6 +58,7 @@ export interface Search {
*/ */
interface MountOptions { interface MountOptions {
viewport$: Observable<Viewport> /* Viewport observable */ viewport$: Observable<Viewport> /* Viewport observable */
keyboard$: Observable<Key> /* Keyboard observable */
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
@ -72,7 +74,7 @@ interface MountOptions {
* @return Search observable * @return Search observable
*/ */
export function mountSearch( export function mountSearch(
handler: WorkerHandler<SearchMessage>, { viewport$ }: MountOptions handler: WorkerHandler<SearchMessage>, { viewport$, keyboard$ }: MountOptions
): OperatorFunction<HTMLElement, Search> { ): OperatorFunction<HTMLElement, Search> {
return pipe( return pipe(
switchMap(() => { switchMap(() => {
@ -93,7 +95,7 @@ export function mountSearch(
/* Mount search result */ /* Mount search result */
const result$ = useComponent("search-result") const result$ = useComponent("search-result")
.pipe( .pipe(
mountSearchResult(handler, { viewport$, query$ }) mountSearchResult(handler, { query$, viewport$, keyboard$ })
) )
/* Combine into a single hot observable */ /* Combine into a single hot observable */

View File

@ -29,7 +29,7 @@ import {
tap tap
} from "rxjs/operators" } from "rxjs/operators"
import { watchSearchReset } from "observables" import { setElementFocus, watchSearchReset } from "observables"
import { useComponent } from "../../_" import { useComponent } from "../../_"
@ -47,7 +47,7 @@ export function mountSearchReset(): OperatorFunction<HTMLElement, void> {
return pipe( return pipe(
switchMap(watchSearchReset), switchMap(watchSearchReset),
switchMapTo(query$), switchMapTo(query$),
tap(el => el.focus()), tap(setElementFocus),
mapTo(undefined), mapTo(undefined),
startWith(undefined) startWith(undefined)
) )

View File

@ -28,22 +28,33 @@ import {
map, map,
pluck, pluck,
shareReplay, shareReplay,
switchMap switchMap,
withLatestFrom
} from "rxjs/operators" } from "rxjs/operators"
import { setToggle } from "actions"
import { SearchResult } from "modules" import { SearchResult } from "modules"
import { import {
Key,
SearchQuery, SearchQuery,
Viewport, Viewport,
WorkerHandler, WorkerHandler,
getActiveElement,
getElements,
paintSearchResult, paintSearchResult,
watchElementOffset setElementFocus,
useToggle,
watchElementOffset,
watchToggle
} from "observables" } from "observables"
import { takeIf } from "utilities"
import { import {
SearchMessage, SearchMessage,
isSearchResultMessage isSearchResultMessage
} from "workers" } from "workers"
import { useComponent } from "../../_"
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* Helper types * Helper types
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
@ -54,6 +65,7 @@ import {
interface MountOptions { interface MountOptions {
query$: Observable<SearchQuery> /* Search query observable */ query$: Observable<SearchQuery> /* Search query observable */
viewport$: Observable<Viewport> /* Viewport observable */ viewport$: Observable<Viewport> /* Viewport observable */
keyboard$: Observable<Key> /* Keyboard observable */
} }
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
@ -70,8 +82,9 @@ interface MountOptions {
*/ */
export function mountSearchResult( export function mountSearchResult(
{ rx$ }: WorkerHandler<SearchMessage>, { rx$ }: WorkerHandler<SearchMessage>,
{ query$, viewport$ }: MountOptions { query$, viewport$, keyboard$ }: MountOptions
): OperatorFunction<HTMLElement, SearchResult[]> { ): OperatorFunction<HTMLElement, SearchResult[]> {
const toggle$ = useToggle("search")
return pipe( return pipe(
switchMap(el => { switchMap(el => {
const container = el.parentElement! const container = el.parentElement!
@ -86,6 +99,55 @@ export function mountSearchResult(
filter(identity) filter(identity)
) )
/* Setup keyboard navigation in search mode */
keyboard$
.pipe(
takeIf(toggle$.pipe(switchMap(watchToggle))),
withLatestFrom(toggle$, useComponent("search-query"))
)
.subscribe(([key, toggle, query]) => {
const active = getActiveElement()
switch (key.type) {
/* Enter: prevent form submission */
case "Enter":
if (active === query)
key.claim()
break
/* Escape or Tab: close search */
case "Escape":
case "Tab":
setToggle(toggle, false)
setElementFocus(query, false)
break
/* Vertical arrows: select previous or next search result */
case "ArrowUp":
case "ArrowDown":
if (typeof active === "undefined") {
setElementFocus(query)
} else {
const els = [query, ...getElements("[href]", el)]
const i = Math.max(0, (
Math.max(0, els.indexOf(active)) + els.length + (
key.type === "ArrowUp" ? -1 : +1
)
) % els.length)
setElementFocus(els[i])
}
/* Prevent scrolling of page */
key.claim()
break
/* All other keys: hand to search query */
default:
if (query !== getActiveElement())
setElementFocus(query)
}
})
/* Paint search results */ /* Paint search results */
return rx$ return rx$
.pipe( .pipe(

View File

@ -27,7 +27,7 @@ import "../stylesheets/app.scss"
import "../stylesheets/app-palette.scss" import "../stylesheets/app-palette.scss"
import * as Clipboard from "clipboard" import * as Clipboard from "clipboard"
import { identity, not, values } from "ramda" import { identity, values } from "ramda"
import { import {
EMPTY, EMPTY,
merge, merge,
@ -39,9 +39,7 @@ import {
filter, filter,
map, map,
switchMap, switchMap,
tap, tap
withLatestFrom,
switchMapTo
} from "rxjs/operators" } from "rxjs/operators"
import { import {
@ -59,14 +57,10 @@ import {
watchViewport, watchViewport,
watchKeyboard, watchKeyboard,
watchToggleMap, watchToggleMap,
useToggle, useToggle
getActiveElement,
mayReceiveKeyboardEvents,
watchMain
} from "./observables" } from "./observables"
import { setupSearchWorker } from "./workers" import { setupSearchWorker } from "./workers"
import { renderSource } from "templates" import { renderSource } from "templates"
import { takeIf } from "utilities"
import { renderClipboard } from "templates/clipboard" import { renderClipboard } from "templates/clipboard"
import { fetchGitHubStats } from "modules/source/github" import { fetchGitHubStats } from "modules/source/github"
import { renderTable } from "templates/table" import { renderTable } from "templates/table"
@ -247,7 +241,7 @@ export function initialize(config: unknown) {
const search$ = useComponent("search") const search$ = useComponent("search")
.pipe( .pipe(
mountSearch(sw, { viewport$ }), mountSearch(sw, { viewport$, keyboard$ }),
) )
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
@ -274,95 +268,20 @@ export function initialize(config: unknown) {
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
function openSearchOnHotKey() { // search$
const toggle$ = useToggle("search") // .pipe(
const search$ = toggle$ // filter(not),
.pipe( // switchMapTo(keyboard$),
switchMap(watchToggle) // filter(key => ["s", "f"].includes(key.type)),
) // switchMapTo(toggle$)
// )
const query$ = useComponent<HTMLInputElement>("search-query") // .subscribe(toggle => {
// const el = getActiveElement()
search$ // if (!(el && mayReceiveKeyboardEvents(el)))
.pipe( // setToggle(toggle, true)
filter(not), // })
switchMapTo(keyboard$),
filter(key => ["KeyS", "KeyF"].includes(key.code)),
switchMapTo(toggle$)
)
.subscribe(toggle => {
const el = getActiveElement()
if (!(el && mayReceiveKeyboardEvents(el)))
setToggle(toggle, true)
})
search$
.pipe(
filter(identity),
switchMapTo(keyboard$),
filter(key => ["Escape", "Tab"].includes(key.code)),
switchMapTo(toggle$),
withLatestFrom(query$)
)
.subscribe(([toggle, el]) => {
setToggle(toggle, false)
el.blur()
})
} // TODO: handle ALL cases in one switch case statement!
const search = getElement<HTMLInputElement>("[data-md-toggle=search]")! const search = getElement<HTMLInputElement>("[data-md-toggle=search]")!
const searchActive$ = useToggle("search").pipe(
switchMap(el => watchToggle(el)),
delay(400)
)
openSearchOnHotKey()
// note that all links have tabindex=-1
keyboard$
.pipe(
takeIf(searchActive$),
/* Abort if meta key (macOS) or ctrl key (Windows) is pressed */
tap(key => {
console.log("jo", key)
if (key.code === "Enter") {
if (document.activeElement === getElement("[data-md-component=search-query]")) {
key.claim()
// intercept hash change after search closed
} else {
setToggle(search, false)
}
}
if (key.code === "ArrowUp" || key.code === "ArrowDown") {
const active = getElements("[data-md-component=search-query], [data-md-component=search-result] [href]")
const i = Math.max(0, active.findIndex(el => el === document.activeElement))
const x = Math.max(0, (i + active.length + (key.code === "ArrowUp" ? -1 : +1)) % active.length)
active[x].focus()
// pass keyboard to search result!?
/* Prevent scrolling of page */
key.claim()
// } else if (key.code === "Escape" || key.code === "Tab") {
// setToggle(search, false)
// getElement("[data-md-component=search-query]")!.blur()
} else {
if (search.checked && document.activeElement !== getElement("[data-md-component=search-query]")) {
getElement("[data-md-component=search-query]")!.focus()
}
}
})
)
.subscribe()
// TODO: close search on hashchange
// anchor jump -> always close drawer + search
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
@ -470,8 +389,6 @@ export function initialize(config: unknown) {
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */
const state = { const state = {
search$, search$,
main$, main$,

View File

@ -29,6 +29,25 @@ import { getActiveElement } from "../_"
* Functions * Functions
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
/**
* Set element focus
*
* @param el - Element
* @param value - Whether the element should be focused
*
* @return Element offset
*/
export function setElementFocus(
el: HTMLElement, value: boolean = true
): void {
if (value)
el.focus()
else
el.blur()
}
/* ------------------------------------------------------------------------- */
/** /**
* Watch element focus * Watch element focus
* *

View File

@ -31,7 +31,7 @@ import { filter, map, share } from "rxjs/operators"
* Key * Key
*/ */
export interface Key { export interface Key {
code: string /* Key code */ type: string /* Key type */
claim(): void /* Key claim */ claim(): void /* Key claim */
} }
@ -82,7 +82,7 @@ export function watchKeyboard(): Observable<Key> {
.pipe( .pipe(
filter(ev => !(ev.shiftKey || ev.metaKey || ev.ctrlKey)), filter(ev => !(ev.shiftKey || ev.metaKey || ev.ctrlKey)),
map(ev => ({ map(ev => ({
code: ev.code, type: ev.key,
claim() { claim() {
ev.preventDefault() ev.preventDefault()
ev.stopPropagation() ev.stopPropagation()