From c2bd2f306a7dc6d47d9ced2e64cb15439ca58eeb Mon Sep 17 00:00:00 2001 From: Stephane Zermatten Date: Sun, 13 Jan 2019 19:03:29 +0100 Subject: [PATCH] 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. --- scss/themes/_base.scss | 2 + scss/themes/_dark.scss | 1 + src/routes/_components/DynamicPageBanner.html | 9 +- src/routes/_components/Nav.html | 17 +- src/routes/_components/NavItem.html | 4 +- src/routes/_components/NavShortcuts.html | 27 +++ src/routes/_components/dialog/asyncDialogs.js | 6 +- .../dialog/components/ModalDialog.html | 10 + .../dialog/components/ShortcutHelpDialog.html | 67 ++++++ .../dialog/creators/showShortcutHelpDialog.js | 14 ++ src/routes/_components/list/List.html | 7 +- src/routes/_components/list/ListItem.html | 9 + src/routes/_components/list/listStore.js | 1 + .../shortcut/ScrollListShortcuts.html | 115 ++++++++++ src/routes/_components/shortcut/Shortcut.html | 25 ++ .../_components/status/Notification.html | 9 +- src/routes/_components/status/Status.html | 27 ++- .../status/StatusMediaAttachments.html | 11 +- .../_components/status/StatusSpoiler.html | 11 +- .../_components/status/StatusToolbar.html | 43 ++-- .../timeline/NotificationVirtualListItem.html | 3 +- .../timeline/StatusVirtualListItem.html | 4 +- .../_components/virtualList/VirtualList.html | 9 +- .../virtualList/VirtualListItem.html | 9 +- .../virtualList/virtualListStore.js | 1 + src/routes/_utils/asyncModules.js | 4 + src/routes/_utils/scrollIntoView.js | 64 ++++++ src/routes/_utils/shortcuts.js | 181 +++++++++++++++ .../{smoothScrollToTop.js => smoothScroll.js} | 8 +- tests/spec/024-shortcuts-navigation.js | 95 ++++++++ tests/spec/025-shortcuts-status.js | 128 +++++++++++ tests/unit/test-shortcuts.js | 214 ++++++++++++++++++ 32 files changed, 1083 insertions(+), 52 deletions(-) create mode 100644 src/routes/_components/NavShortcuts.html create mode 100644 src/routes/_components/dialog/components/ShortcutHelpDialog.html create mode 100644 src/routes/_components/dialog/creators/showShortcutHelpDialog.js create mode 100644 src/routes/_components/shortcut/ScrollListShortcuts.html create mode 100644 src/routes/_components/shortcut/Shortcut.html create mode 100644 src/routes/_utils/scrollIntoView.js create mode 100644 src/routes/_utils/shortcuts.js rename src/routes/_utils/{smoothScrollToTop.js => smoothScroll.js} (88%) create mode 100644 tests/spec/024-shortcuts-navigation.js create mode 100644 tests/spec/025-shortcuts-status.js create mode 100644 tests/unit/test-shortcuts.js diff --git a/scss/themes/_base.scss b/scss/themes/_base.scss index e5e5102..9558314 100644 --- a/scss/themes/_base.scss +++ b/scss/themes/_base.scss @@ -80,10 +80,12 @@ --very-deemphasized-text-color: #{rgba(#666, 0.6)}; --status-direct-background: #{darken($body-bg-color, 5%)}; + --status-active-background: #{lighten($body-bg-color, 2%)}; --main-theme-color: #{$main-theme-color}; --warning-color: #{#e01f19}; --alt-input-bg: #{rgba($main-bg-color, 0.7)}; + --muted-modal-text: #{$secondary-text-color}; --muted-modal-bg: #{transparent}; --muted-modal-focus: #{#999}; --muted-modal-hover: #{rgba(255, 255, 255, 0.2)}; diff --git a/scss/themes/_dark.scss b/scss/themes/_dark.scss index ad554cf..4aa55e3 100644 --- a/scss/themes/_dark.scss +++ b/scss/themes/_dark.scss @@ -16,6 +16,7 @@ --very-deemphasized-text-color: #{lighten($main-bg-color, 32%)}; --status-direct-background: #{darken($body-bg-color, 5%)}; + --status-active-background: #{lighten($body-bg-color, 10%)}; --main-theme-color: #{$main-theme-color}; --warning-color: #{#c7423d}; --alt-input-bg: #{rgba($main-bg-color, 0.7)}; diff --git a/src/routes/_components/DynamicPageBanner.html b/src/routes/_components/DynamicPageBanner.html index 84df219..4ab961b 100644 --- a/src/routes/_components/DynamicPageBanner.html +++ b/src/routes/_components/DynamicPageBanner.html @@ -10,8 +10,9 @@ + on:click|preventDefault="onGoBack()">Back + \ No newline at end of file + diff --git a/src/routes/_components/NavItem.html b/src/routes/_components/NavItem.html index e7e8db4..ab2ada6 100644 --- a/src/routes/_components/NavItem.html +++ b/src/routes/_components/NavItem.html @@ -156,7 +156,7 @@ diff --git a/src/routes/_components/dialog/asyncDialogs.js b/src/routes/_components/dialog/asyncDialogs.js index be9b1dc..5d66574 100644 --- a/src/routes/_components/dialog/asyncDialogs.js +++ b/src/routes/_components/dialog/asyncDialogs.js @@ -32,4 +32,8 @@ export const importShowVideoDialog = () => import( export const importShowCopyDialog = () => import( /* webpackChunkName: 'showCopyDialog' */ './creators/showCopyDialog' - ).then(mod => mod.default) \ No newline at end of file + ).then(mod => mod.default) + +export const importShowShortcutHelpDialog = () => import( + /* webpackChunkName: 'showShortcutHelpDialog' */ './creators/showShortcutHelpDialog' + ).then(mod => mod.default) diff --git a/src/routes/_components/dialog/components/ModalDialog.html b/src/routes/_components/dialog/components/ModalDialog.html index 69978cc..a8e79ce 100644 --- a/src/routes/_components/dialog/components/ModalDialog.html +++ b/src/routes/_components/dialog/components/ModalDialog.html @@ -24,6 +24,7 @@ + diff --git a/src/routes/_components/dialog/creators/showShortcutHelpDialog.js b/src/routes/_components/dialog/creators/showShortcutHelpDialog.js new file mode 100644 index 0000000..1b7effe --- /dev/null +++ b/src/routes/_components/dialog/creators/showShortcutHelpDialog.js @@ -0,0 +1,14 @@ +import ShortcutHelpDialog from '../components/ShortcutHelpDialog.html' +import { createDialogElement } from '../helpers/createDialogElement' +import { createDialogId } from '../helpers/createDialogId' + +export default function showShortcutHelpDialog (options) { + let dialog = new ShortcutHelpDialog({ + target: createDialogElement(), + data: Object.assign({ + id: createDialogId(), + label: 'shortcut help dialog' + }, options) + }) + dialog.show() +} diff --git a/src/routes/_components/list/List.html b/src/routes/_components/list/List.html index 67872ff..7b814d4 100644 --- a/src/routes/_components/list/List.html +++ b/src/routes/_components/list/List.html @@ -10,6 +10,7 @@ /> {/each} + \ No newline at end of file + diff --git a/src/routes/_components/list/ListItem.html b/src/routes/_components/list/ListItem.html index d67b63c..bea7064 100644 --- a/src/routes/_components/list/ListItem.html +++ b/src/routes/_components/list/ListItem.html @@ -4,5 +4,14 @@ virtualProps={props} virtualIndex={index} virtualLength={length} + virtualKey={key} + active={active} /> + diff --git a/src/routes/_components/list/listStore.js b/src/routes/_components/list/listStore.js index 0668c8d..2ee464a 100644 --- a/src/routes/_components/list/listStore.js +++ b/src/routes/_components/list/listStore.js @@ -8,6 +8,7 @@ class ListStore extends RealmStore { const listStore = new ListStore() +listStore.computeForRealm('activeItem', null) listStore.computeForRealm('intersectionStates', {}) if (process.browser && process.env.NODE_ENV !== 'production') { diff --git a/src/routes/_components/shortcut/ScrollListShortcuts.html b/src/routes/_components/shortcut/ScrollListShortcuts.html new file mode 100644 index 0000000..2ded318 --- /dev/null +++ b/src/routes/_components/shortcut/ScrollListShortcuts.html @@ -0,0 +1,115 @@ + diff --git a/src/routes/_components/shortcut/Shortcut.html b/src/routes/_components/shortcut/Shortcut.html new file mode 100644 index 0000000..790b6cd --- /dev/null +++ b/src/routes/_components/shortcut/Shortcut.html @@ -0,0 +1,25 @@ + diff --git a/src/routes/_components/status/Notification.html b/src/routes/_components/status/Notification.html index 6a26137..0864c66 100644 --- a/src/routes/_components/status/Notification.html +++ b/src/routes/_components/status/Notification.html @@ -1,9 +1,9 @@ {#if status} {:else} -
\ No newline at end of file + diff --git a/src/routes/_components/status/Status.html b/src/routes/_components/status/Status.html index 6d26c80..f4a6769 100644 --- a/src/routes/_components/status/Status.html +++ b/src/routes/_components/status/Status.html @@ -35,6 +35,9 @@ {/if}
+{#if shortcutScope} + +{/if} \ No newline at end of file + diff --git a/src/routes/_components/timeline/NotificationVirtualListItem.html b/src/routes/_components/timeline/NotificationVirtualListItem.html index eb96ddb..eaf1129 100644 --- a/src/routes/_components/timeline/NotificationVirtualListItem.html +++ b/src/routes/_components/timeline/NotificationVirtualListItem.html @@ -5,6 +5,7 @@ focusSelector={virtualProps.focusSelector} index={virtualIndex} length={virtualLength} + {active} on:recalculateHeight /> \ No newline at end of file + diff --git a/src/routes/_components/timeline/StatusVirtualListItem.html b/src/routes/_components/timeline/StatusVirtualListItem.html index eede316..228b47c 100644 --- a/src/routes/_components/timeline/StatusVirtualListItem.html +++ b/src/routes/_components/timeline/StatusVirtualListItem.html @@ -2,8 +2,10 @@ timelineType={virtualProps.timelineType} timelineValue={virtualProps.timelineValue} focusSelector={virtualProps.focusSelector} + shortcutScope={virtualKey} index={virtualIndex} length={virtualLength} + active={active} on:recalculateHeight /> \ No newline at end of file + diff --git a/src/routes/_components/virtualList/VirtualList.html b/src/routes/_components/virtualList/VirtualList.html index f8f759f..f82b3d0 100644 --- a/src/routes/_components/virtualList/VirtualList.html +++ b/src/routes/_components/virtualList/VirtualList.html @@ -18,6 +18,8 @@ {/if} + visibleItem.key} />