Skip to content

Commit

Permalink
♿️ (#2735): add aria-keyshortcuts to menu entries
Browse files Browse the repository at this point in the history
Signed-off-by: Vinicius Reis <vinicius.reis@nextcloud.com>
  • Loading branch information
Vinicius Reis authored and vinicius73 committed Aug 1, 2022
1 parent 6b6af3c commit c1e9821
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/components/Menu/ActionSingle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default {
: Button
},
bindState() {
const { keyshortcuts } = this
const state = {
...this.state,
}
Expand All @@ -53,7 +55,11 @@ export default {
'entry-action-item': this.isItem,
}
// item list bejaviour
if (keyshortcuts) {
state['aria-keyshortcuts'] = keyshortcuts
}
// item list behaviour
if (this.isItem) {
state.closeAfterClick = true
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Menu/BaseActionEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip'
import debounce from 'debounce'

import { useEditorMixin, useIsMobileMixin } from '../Editor.provider.js'
import { getActionState, getKeys } from './utils.js'
import { getActionState, getKeys, getKeyshortcuts } from './utils.js'

import './ActionEntry.scss'

Expand All @@ -53,6 +53,9 @@ const BaseActionEntry = {
icon() {
return this.actionEntry.icon
},
keyshortcuts() {
return getKeyshortcuts(this.actionEntry)
},
tooltip() {
return [this.actionEntry.label, getKeys(this.$isMobile, this.actionEntry)].join(' ')
},
Expand Down
22 changes: 12 additions & 10 deletions src/components/Menu/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ import {
import EmojiPickerAction from './EmojiPickerAction.vue'
import ActionImageUpload from './ActionImageUpload.vue'

import { MODIFIERS } from './keys.js'

export default [
{
key: 'undo',
label: t('text', 'Undo'),
keyChar: 'z',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: Undo,
action: (command) => command.undo(),
priority: 5,
Expand All @@ -64,7 +66,7 @@ export default [
key: 'redo',
label: t('text', 'Redo'),
keyChar: 'y',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: Redo,
action: (command) => command.redo(),
priority: 11,
Expand All @@ -73,7 +75,7 @@ export default [
key: 'bold',
label: t('text', 'Bold'),
keyChar: 'b',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: FormatBold,
isActive: 'strong',
action: (command) => {
Expand All @@ -85,7 +87,7 @@ export default [
key: 'italic',
label: t('text', 'Italic'),
keyChar: 'i',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: FormatItalic,
isActive: 'em',
action: (command) => {
Expand All @@ -97,7 +99,7 @@ export default [
key: 'underline',
label: t('text', 'Underline'),
keyChar: 'u',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: FormatUnderline,
isActive: 'underline',
action: (command) => {
Expand All @@ -109,7 +111,7 @@ export default [
key: 'strikethrough',
label: t('text', 'Strikethrough'),
keyChar: 'd',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
icon: FormatStrikethrough,
isActive: 'strike',
action: (command) => {
Expand All @@ -121,7 +123,7 @@ export default [
key: 'headings',
label: t('text', 'Headings'),
keyChar: '1…6',
keyModifiers: ['ctrl', 'shift'],
keyModifiers: [MODIFIERS.Mod, MODIFIERS.Shift],
visible: false,
icon: FormatHeader1,
isActive: 'heading',
Expand Down Expand Up @@ -187,7 +189,7 @@ export default [
key: 'unordered-list',
label: t('text', 'Unordered list'),
keyChar: '8',
keyModifiers: ['ctrl', 'shift'],
keyModifiers: [MODIFIERS.Mod, MODIFIERS.Shift],
isActive: 'bulletList',
icon: FormatListBulleted,
action: (command) => {
Expand All @@ -199,7 +201,7 @@ export default [
key: 'ordered-list',
label: t('text', 'Ordered list'),
keyChar: '9',
keyModifiers: ['ctrl', 'shift'],
keyModifiers: [MODIFIERS.Mod, MODIFIERS.Shift],
isActive: 'orderedList',
icon: FormatListNumbered,
action: (command) => {
Expand All @@ -219,7 +221,7 @@ export default [
key: 'blockquote',
label: t('text', 'Blockquote'),
keyChar: '>',
keyModifiers: ['ctrl'],
keyModifiers: [MODIFIERS.Mod],
isActive: 'blockquote',
icon: FormatQuote,
action: (command) => {
Expand Down
24 changes: 24 additions & 0 deletions src/components/Menu/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const isMac = (navigator.userAgent.includes('Mac'))

const MODIFIERS = {
Mod: isMac ? 'Meta' : 'Control',
Alt: 'Alt', // Option key, on Apple computers.
Control: 'Control',
Shift: 'Shift',

// unused
// AltGraph: 'AltGraph',
// Meta: 'Meta', // Command key on Apple computers
}

const TRANSLATIONS = {
[MODIFIERS.Mod]: t('text', isMac ? 'Command' : 'Control'),
[MODIFIERS.Control]: t('text', 'Ctrl'),
[MODIFIERS.Alt]: t('text', isMac ? 'Option' : 'Command'),
[MODIFIERS.Shift]: t('text', 'Shift'),
}

export {
MODIFIERS,
TRANSLATIONS,
}
22 changes: 14 additions & 8 deletions src/components/Menu/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
*
*/

const translations = {
ctrl: t('text', 'Ctrl'),
alt: t('text', 'Alt'),
shift: t('text', 'Shift'),
}
import {
TRANSLATIONS,
MODIFIERS,
} from './keys.js'

const getEntryClasses = (actionEntry, isActive) => {
return {
Expand All @@ -34,13 +33,19 @@ const getEntryClasses = (actionEntry, isActive) => {
}

const keysString = (keyChar, modifiers = []) => {
return Object.entries(translations)
.filter(([k, v]) => modifiers.includes(k))
.map(([k, v]) => v)
return modifiers
.map(mod => TRANSLATIONS[mod])
.concat(keyChar.toUpperCase())
.join('+')
}

const getKeyshortcuts = ({ keyChar, keyModifiers = [] }) => {
return keyModifiers
.map(mod => MODIFIERS[mod])
.concat(keyChar)
.join('+')
}

const getKeys = (isMobile, { keyChar, keyModifiers }) => {
return (!isMobile && keyChar)
? `(${keysString(keyChar, keyModifiers)})`
Expand Down Expand Up @@ -78,6 +83,7 @@ export {
isDisabled,
getIsActive,
getKeys,
getKeyshortcuts,
getEntryClasses,
getActionState,
}

0 comments on commit c1e9821

Please sign in to comment.