2019-01-13 14:02:15 -08:00
|
|
|
import { store } from '../_store/store'
|
|
|
|
|
feat: Add support for keyboard shortcuts (#870)
* Add support for keyboard shortcuts.
This change introduces a Shortcut component for defining global
keyboard shortcuts from whichever component makes more sense.
This change also adds an initial set of navigation shortcuts:
- Backspace to leave a modal dialog or to go back
- g t to go to the federated timeline
- g f to go to the favorite page
- g h to go to the home page
- g n to go to the notification page
- g c to go to the community page
- s to go to the search page
These shortcuts are loaded asynchronously from _layout.html
In modal dialogs, shortcuts are also modal, to avoid strange or
overly complex behavior. This is implemented by grouping
shortcuts into scopes, and activating a separate 'modal' scope
when entering a modal dialog, so a separate set of shortcuts can
be enabled in modal dialog. Modal dialogs can be exited by
pressing 'Backspace'.
* Navigate up/down lists using keyboard shortcuts.
This change introduces keyboard shortcuts for navigating in lists and
virtual lists. j or arrow up selects the next element, k or arrow down,
the previous element. Selecting an element scrolls the list up and down,
as necessary.
This change also allows directing keyboard shortcuts to the active
element and defines the following shortcuts, for the active status:
- f to favorite or unfavorite it
- b to boost or unboost it
- r to reply to it
- o to open its thread
- x to toggle the display of a CW
- y to toggle the display of sensitive medias
This works by defining a keyboard shortcut scope for each list element.
A new component, ScrollListShortcuts, keeps track of the active element,
based on list or virtual list elements and redirects shortcuts to the
active element's scope. ScrollListShortcuts keeps the active element in
the current realm of the store, so the active element is restored when
going back to the list.
* Typing h or ? displays the list of available keyboard shortcuts.
This change introduces a new modal dialog that documents the list of
available shortcuts.
2019-01-13 19:03:29 +01:00
|
|
|
// A map of scopeKey to KeyMap
|
|
|
|
let scopeKeyMaps
|
|
|
|
|
|
|
|
// Current scope, starting with 'global'
|
|
|
|
let currentScopeKey
|
|
|
|
|
|
|
|
// Previous current scopes
|
|
|
|
let scopeStack
|
|
|
|
|
|
|
|
// Currently active prefix map
|
|
|
|
let prefixMap
|
|
|
|
|
|
|
|
// Scope in which prefixMap is valid
|
|
|
|
let prefixMapScope
|
|
|
|
|
|
|
|
// A map of key to components or other KeyMaps
|
|
|
|
function KeyMap () {}
|
|
|
|
|
|
|
|
export function initShortcuts () {
|
|
|
|
currentScopeKey = 'global'
|
|
|
|
scopeStack = []
|
|
|
|
scopeKeyMaps = null
|
|
|
|
prefixMap = null
|
|
|
|
prefixMapScope = null
|
|
|
|
}
|
|
|
|
initShortcuts()
|
|
|
|
|
|
|
|
// Sets scopeKey as current scope.
|
|
|
|
export function pushShortcutScope (scopeKey) {
|
|
|
|
scopeStack.push(currentScopeKey)
|
|
|
|
currentScopeKey = scopeKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go back to previous current scope.
|
|
|
|
export function popShortcutScope (scopeKey) {
|
|
|
|
if (scopeKey !== currentScopeKey) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentScopeKey = scopeStack.pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call component.onKeyDown(event) when a key in keys is pressed
|
|
|
|
// in the given scope.
|
|
|
|
export function addToShortcutScope (scopeKey, keys, component) {
|
|
|
|
if (scopeKeyMaps == null) {
|
|
|
|
window.addEventListener('keydown', onKeyDown)
|
|
|
|
scopeKeyMaps = {}
|
|
|
|
}
|
|
|
|
let keyMap = scopeKeyMaps[scopeKey]
|
|
|
|
if (!keyMap) {
|
|
|
|
keyMap = new KeyMap()
|
|
|
|
scopeKeyMaps[scopeKey] = keyMap
|
|
|
|
}
|
|
|
|
mapKeys(keyMap, keys, component)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeFromShortcutScope (scopeKey, keys, component) {
|
|
|
|
let keyMap = scopeKeyMaps[scopeKey]
|
|
|
|
if (!keyMap) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
unmapKeys(keyMap, keys, component)
|
|
|
|
if (Object.keys(keyMap).length === 0) {
|
|
|
|
delete scopeKeyMaps[scopeKey]
|
|
|
|
}
|
|
|
|
if (Object.keys(scopeKeyMaps).length === 0) {
|
|
|
|
scopeKeyMaps = null
|
|
|
|
window.removeEventListener('keydown', onKeyDown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FALLBACK_KEY = '__fallback__'
|
|
|
|
|
|
|
|
// Call component.onKeyDown(event) if no other shortcuts handled
|
|
|
|
// the current key.
|
|
|
|
export function addShortcutFallback (scopeKey, component) {
|
|
|
|
addToShortcutScope(scopeKey, FALLBACK_KEY, component)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeShortcutFallback (scopeKey, component) {
|
|
|
|
removeFromShortcutScope(scopeKey, FALLBACK_KEY, component)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Direct the given event to the appropriate component in the given
|
|
|
|
// scope for the event's key.
|
|
|
|
export function onKeyDownInShortcutScope (scopeKey, event) {
|
|
|
|
if (prefixMap) {
|
|
|
|
let handled = false
|
|
|
|
if (prefixMap && prefixMapScope === scopeKey) {
|
|
|
|
handled = handleEvent(scopeKey, prefixMap, event.key, event)
|
|
|
|
}
|
|
|
|
prefixMap = null
|
|
|
|
prefixMapScope = null
|
|
|
|
if (handled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let keyMap = scopeKeyMaps[scopeKey]
|
|
|
|
if (!keyMap) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!handleEvent(scopeKey, keyMap, event.key, event)) {
|
|
|
|
handleEvent(scopeKey, keyMap, FALLBACK_KEY, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleEvent (scopeKey, keyMap, key, event) {
|
|
|
|
let value = keyMap[key]
|
|
|
|
if (!value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (KeyMap.prototype.isPrototypeOf(value)) {
|
|
|
|
prefixMap = value
|
|
|
|
prefixMapScope = scopeKey
|
|
|
|
} else {
|
|
|
|
value.onKeyDown(event)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
function onKeyDown (event) {
|
2019-01-13 14:02:15 -08:00
|
|
|
if (store.get().disableHotkeys) {
|
|
|
|
return
|
|
|
|
}
|
feat: Add support for keyboard shortcuts (#870)
* Add support for keyboard shortcuts.
This change introduces a Shortcut component for defining global
keyboard shortcuts from whichever component makes more sense.
This change also adds an initial set of navigation shortcuts:
- Backspace to leave a modal dialog or to go back
- g t to go to the federated timeline
- g f to go to the favorite page
- g h to go to the home page
- g n to go to the notification page
- g c to go to the community page
- s to go to the search page
These shortcuts are loaded asynchronously from _layout.html
In modal dialogs, shortcuts are also modal, to avoid strange or
overly complex behavior. This is implemented by grouping
shortcuts into scopes, and activating a separate 'modal' scope
when entering a modal dialog, so a separate set of shortcuts can
be enabled in modal dialog. Modal dialogs can be exited by
pressing 'Backspace'.
* Navigate up/down lists using keyboard shortcuts.
This change introduces keyboard shortcuts for navigating in lists and
virtual lists. j or arrow up selects the next element, k or arrow down,
the previous element. Selecting an element scrolls the list up and down,
as necessary.
This change also allows directing keyboard shortcuts to the active
element and defines the following shortcuts, for the active status:
- f to favorite or unfavorite it
- b to boost or unboost it
- r to reply to it
- o to open its thread
- x to toggle the display of a CW
- y to toggle the display of sensitive medias
This works by defining a keyboard shortcut scope for each list element.
A new component, ScrollListShortcuts, keeps track of the active element,
based on list or virtual list elements and redirects shortcuts to the
active element's scope. ScrollListShortcuts keeps the active element in
the current realm of the store, so the active element is restored when
going back to the list.
* Typing h or ? displays the list of available keyboard shortcuts.
This change introduces a new modal dialog that documents the list of
available shortcuts.
2019-01-13 19:03:29 +01:00
|
|
|
if (!acceptShortcutEvent(event)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
onKeyDownInShortcutScope(currentScopeKey, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapKeys (keyMap, keys, component) {
|
|
|
|
keys.split('|').forEach(
|
|
|
|
(seq) => {
|
|
|
|
let seqArray = seq.split(' ')
|
|
|
|
let prefixLen = seqArray.length - 1
|
|
|
|
let currentMap = keyMap
|
|
|
|
let i = -1
|
|
|
|
while (++i < prefixLen) {
|
|
|
|
let prefixMap = currentMap[seqArray[i]]
|
|
|
|
if (!prefixMap) {
|
|
|
|
prefixMap = new KeyMap()
|
|
|
|
currentMap[seqArray[i]] = prefixMap
|
|
|
|
}
|
|
|
|
currentMap = prefixMap
|
|
|
|
}
|
|
|
|
currentMap[seqArray[prefixLen]] = component
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function unmapKeys (keyMap, keys, component) {
|
|
|
|
keys.split('|').forEach(
|
|
|
|
(seq) => {
|
|
|
|
let seqArray = seq.split(' ')
|
|
|
|
let prefixLen = seqArray.length - 1
|
|
|
|
let currentMap = keyMap
|
|
|
|
let i = -1
|
|
|
|
while (++i < prefixLen) {
|
|
|
|
let prefixMap = currentMap[seqArray[i]]
|
|
|
|
if (!prefixMap) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentMap = prefixMap
|
|
|
|
}
|
|
|
|
let lastKey = seqArray[prefixLen]
|
|
|
|
if (currentMap[lastKey] === component) {
|
|
|
|
delete currentMap[lastKey]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function acceptShortcutEvent (event) {
|
2019-01-13 14:02:01 -08:00
|
|
|
let { target } = event
|
|
|
|
return !(
|
|
|
|
event.metaKey ||
|
|
|
|
event.ctrlKey ||
|
|
|
|
(event.shiftKey && event.key !== '?') || // '?' is a special case - it is allowed
|
|
|
|
(target && (
|
|
|
|
target.isContentEditable ||
|
|
|
|
['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName)
|
|
|
|
))
|
|
|
|
)
|
feat: Add support for keyboard shortcuts (#870)
* Add support for keyboard shortcuts.
This change introduces a Shortcut component for defining global
keyboard shortcuts from whichever component makes more sense.
This change also adds an initial set of navigation shortcuts:
- Backspace to leave a modal dialog or to go back
- g t to go to the federated timeline
- g f to go to the favorite page
- g h to go to the home page
- g n to go to the notification page
- g c to go to the community page
- s to go to the search page
These shortcuts are loaded asynchronously from _layout.html
In modal dialogs, shortcuts are also modal, to avoid strange or
overly complex behavior. This is implemented by grouping
shortcuts into scopes, and activating a separate 'modal' scope
when entering a modal dialog, so a separate set of shortcuts can
be enabled in modal dialog. Modal dialogs can be exited by
pressing 'Backspace'.
* Navigate up/down lists using keyboard shortcuts.
This change introduces keyboard shortcuts for navigating in lists and
virtual lists. j or arrow up selects the next element, k or arrow down,
the previous element. Selecting an element scrolls the list up and down,
as necessary.
This change also allows directing keyboard shortcuts to the active
element and defines the following shortcuts, for the active status:
- f to favorite or unfavorite it
- b to boost or unboost it
- r to reply to it
- o to open its thread
- x to toggle the display of a CW
- y to toggle the display of sensitive medias
This works by defining a keyboard shortcut scope for each list element.
A new component, ScrollListShortcuts, keeps track of the active element,
based on list or virtual list elements and redirects shortcuts to the
active element's scope. ScrollListShortcuts keeps the active element in
the current realm of the store, so the active element is restored when
going back to the list.
* Typing h or ? displays the list of available keyboard shortcuts.
This change introduces a new modal dialog that documents the list of
available shortcuts.
2019-01-13 19:03:29 +01:00
|
|
|
}
|