pgadmin4/web/pgadmin/static/js/components/ShortcutTitle.jsx
Aditya Toshniwal b5b9ee46a1 1) Port query tool to React. Fixes #6131
2) Added status bar to the Query Tool. Fixes #3253
3) Ensure that row numbers should be visible in view when scrolling horizontally. Fixes #3989
4) Allow removing a single query history. Refs #4113
5) Partially fixed Macros usability issues. Ref #6969
6) Fixed an issue where the Query tool opens on minimum size if the user opens multiple query tool Window quickly. Fixes #6725
7) Relocate GIS Viewer Button to the Left Side of the Results Table. Fixes #6830
8) Fixed an issue where the connection bar is not visible. Fixes #7188
9) Fixed an issue where an Empty message popup after running a query. Fixes #7260
10) Ensure that Autocomplete should work after changing the connection. Fixes #7262
11) Fixed an issue where the copy and paste row does not work if the first column contains no data. Fixes #7294
2022-04-07 17:36:56 +05:30

102 lines
2.7 KiB
JavaScript

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';
import gettext from 'sources/gettext';
const useStyles = makeStyles((theme)=>({
shortcutTitle: {
width: '100%',
textAlign: 'center',
},
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,
},
}));
export function getBrowserAccesskey() {
/* Ref: https://github.com/tillsanders/access-key-label-polyfill/ */
let ua = window.navigator.userAgent;
// macOS
if (ua.match(/macintosh/i)) {
// Firefox
if (ua.match(/firefox/i)) {
const firefoxVersion = ua.match(/firefox[\s/](\d+)/i);
// Firefox < v14
if (firefoxVersion[1] && parseInt(firefoxVersion[1], 10) < 14) {
return ['Ctrl'];
}
}
return ['Option', 'Ctrl'];
}
// Internet Explorer / Edge
if (ua.match(/msie|trident/i) || ua.match(/\sedg/i)) {
return ['Alt'];
}
// iOS / iPadOS
if (ua.match(/(ipod|iphone|ipad)/i)) {
// accesskeyLabel is supported > v14, but we're not checking for versions here, since we use
// feature support detection
return ['Option', 'Ctrl'];
}
// Fallback
// Note: Apparently, Chrome for Android is not even supporting accesskey, so be prepared.
return [gettext('Accesskey')];
}
export function shortcutToString(shortcut, accesskey=null, asArray=false) {
let keys = [];
if(accesskey) {
keys = getBrowserAccesskey();
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 */
export default function ShortcutTitle({title, shortcut, accesskey}) {
const classes = useStyles();
let keys = shortcutToString(shortcut, accesskey, true);
return (
<>
<div className={classes.shortcutTitle}>{title}</div>
<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,
accesskey: PropTypes.string,
};