2022-02-10 23:06:24 -06:00
|
|
|
import React from 'react';
|
|
|
|
import { makeStyles } from '@material-ui/styles';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { isMac } from '../keyboard_shortcuts';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import CustomPropTypes from '../custom_prop_types';
|
2022-04-07 07:06:56 -05:00
|
|
|
import gettext from 'sources/gettext';
|
2022-02-10 23:06:24 -06:00
|
|
|
|
|
|
|
const useStyles = makeStyles((theme)=>({
|
2022-04-07 07:06:56 -05:00
|
|
|
shortcutTitle: {
|
|
|
|
width: '100%',
|
|
|
|
textAlign: 'center',
|
|
|
|
},
|
2022-02-10 23:06:24 -06:00
|
|
|
shortcut: {
|
|
|
|
justifyContent: 'center',
|
|
|
|
marginTop: '0.125rem',
|
|
|
|
display: 'flex',
|
|
|
|
},
|
|
|
|
key: {
|
|
|
|
padding: '0 0.25rem',
|
|
|
|
border: `1px solid ${theme.otherVars.borderColor}`,
|
|
|
|
marginRight: '0.125rem',
|
|
|
|
borderRadius: theme.shape.borderRadius,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-04-07 07:06:56 -05:00
|
|
|
export function getBrowserAccesskey() {
|
2023-03-29 03:19:11 -05:00
|
|
|
/* Ref: https://www.w3schools.com/tags/att_accesskey.asp */
|
2022-04-07 07:06:56 -05:00
|
|
|
let ua = window.navigator.userAgent;
|
|
|
|
// macOS
|
|
|
|
if (ua.match(/macintosh/i)) {
|
2023-03-29 03:19:11 -05:00
|
|
|
return ['Ctrl', 'Option'];
|
2022-04-07 07:06:56 -05:00
|
|
|
}
|
|
|
|
|
2023-03-29 03:19:11 -05:00
|
|
|
// Windows / Linux
|
|
|
|
if (ua.match(/windows/i) || ua.match(/linux/i)) {
|
|
|
|
if(ua.match(/firefox/i)) {
|
|
|
|
return ['Alt', 'Shift'];
|
|
|
|
}
|
2022-04-07 07:06:56 -05:00
|
|
|
return ['Alt'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback
|
|
|
|
return [gettext('Accesskey')];
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:06:24 -06:00
|
|
|
export function shortcutToString(shortcut, accesskey=null, asArray=false) {
|
|
|
|
let keys = [];
|
|
|
|
if(accesskey) {
|
2022-04-07 07:06:56 -05:00
|
|
|
keys = getBrowserAccesskey();
|
2022-02-10 23:06:24 -06:00
|
|
|
keys.push(_.capitalize(accesskey?.toUpperCase()));
|
|
|
|
} else if(shortcut) {
|
|
|
|
shortcut.alt && keys.push((isMac() ? 'Option' : 'Alt'));
|
|
|
|
if(isMac() && shortcut.ctrl_is_meta) {
|
|
|
|
shortcut.control && keys.push('Cmd');
|
|
|
|
} else {
|
|
|
|
shortcut.control && keys.push('Ctrl');
|
|
|
|
}
|
|
|
|
shortcut.shift && keys.push('Shift');
|
|
|
|
keys.push(_.capitalize(shortcut.key.char));
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return asArray ? keys : keys.join(' + ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The tooltip content to show shortcut details */
|
2022-04-07 07:06:56 -05:00
|
|
|
export default function ShortcutTitle({title, shortcut, accesskey}) {
|
2022-02-10 23:06:24 -06:00
|
|
|
const classes = useStyles();
|
2022-04-07 07:06:56 -05:00
|
|
|
let keys = shortcutToString(shortcut, accesskey, true);
|
2022-02-10 23:06:24 -06:00
|
|
|
return (
|
|
|
|
<>
|
2022-04-07 07:06:56 -05:00
|
|
|
<div className={classes.shortcutTitle}>{title}</div>
|
2022-02-10 23:06:24 -06:00
|
|
|
<div className={classes.shortcut}>
|
|
|
|
{keys.map((key, i)=>{
|
|
|
|
return <div key={i} className={classes.key}>{key}</div>;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShortcutTitle.propTypes = {
|
|
|
|
title: PropTypes.string,
|
|
|
|
shortcut: CustomPropTypes.shortcut,
|
2022-04-07 07:06:56 -05:00
|
|
|
accesskey: PropTypes.string,
|
2022-02-10 23:06:24 -06:00
|
|
|
};
|