github / github/hotkey

API alterations to allow injection of hotkey parsing and event processing

オープン
#69 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
3.3k
フォーク
99
平均マージ
14時間 22分
マージ済み PR(30日)
6

説明

I think a useful migration strategy would be to allow serialization/comparison to be overridden as part of the API.

When we talk about what this library does that is "novel" (as in, something you cannot _reasonably achieve_ in a few LOC locally) the library does three things:

- It manages many shortcuts by implementing a Radix Trie.
- It manages event listening & dispatch avoiding footguns: for example delegated events, ensuring hotkeys aren't fired on form fields, etc.
- It provides the above with a simple `install`/`uninstall` API to simplify adding/removing event listeners and state from the Radix Trie.
- It comes up with some lose specification of how to expand/compare hotkey strings to decide when to fire a hotkey combo.

The last one is what causes us a lot of trouble and causes some trashing in this library. `radix-trie.ts` hasn't been touched in 9 months, prior to that 2 years ago. I'd say `radix-trie` is "feature complete". Meanwhile `hotkey.ts` has a regular cadence of alterations every few months as we reach edge cases and scale our use of this library.

The chief problems with the serialization format are:

- It is a psuedo specification. There's no formal set of possible values or a well defined grammar or spec. It is an ad-hoc grammar using RegExps. While this works fine for the most part, it is a source of bugs and confusion, as well as differences of opinion.
- It doesn't properly encode all of the state about what we as developers _intend_ shortcuts to be. We've discussed this quite a lot synchronously; the concept of "logical" (`WSAD`) vs "Semiotic" (`?`) shortcuts.

Effectively the serialization of these shortcuts is, what you might call, unsolved. So I say let's make that apparent by allowing it to be overridden in the API.

The current API is as follows:

```typescript
export install(element: HTMLElement, hotkey?: string): void {}
export uninstall(element: HTMLElement): void {}
```

I propose we expand this to the following:

```typescript
export type ProcessHotkey = (hotkey: string): string[][]
export type ProcessEvent = (event: KeyboardEvent): string

export class HotkeyManager {
constructor(
processHotkey: ProcessHotkey = expandHotkeyToEdges,
processEvent: ProcessEvent = eventToHotkeyString
)

install(element: HTMLelement, hotkey?: string): void {}

uninstall(element: HTMLElement): void {}
}

const defaultManager = new HotkeyManager()
export const install = defaultManager.install
export const uninstall = defaultManager.uninstall
```

By making a class, we can define custom processing for shortcut keys which allows users to define their own shortcut models, but also allows us to make more breaking changes behind experimental APIs, and even feature flag them. By still exposing the `install`/`uninstall` functions per the existing API, we ensure backwards compatibility which minimizes breaking changes, and allows us to "make the hard change easy then make the easy change". A 2.0 change could effectively swap the default hotkey functions out for the newer API, as a one line change.

Thoughts @github/ui-frameworks?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Start by reading hotkey.ts and radix-trie.ts, then compare the current install/uninstall API with the proposed HotkeyManager, ProcessHotkey, and ProcessEvent interfaces. Determine how custom serialization and event processing would integrate without changing the existing default exports; done means the API design and compatibility behavior are agreed and implemented.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
frontend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。