2020-01-22 14:34:46 +00:00
|
|
|
/*
|
|
|
|
* keypress actions
|
|
|
|
* @flow
|
|
|
|
*/
|
2021-01-30 07:02:31 +00:00
|
|
|
import { t } from 'ttag';
|
2020-01-22 14:34:46 +00:00
|
|
|
import store from '../ui/store';
|
2020-04-28 05:16:47 +00:00
|
|
|
import copy from '../utils/clipboard';
|
2020-01-22 14:34:46 +00:00
|
|
|
import {
|
|
|
|
toggleGrid,
|
2020-06-23 16:49:56 +00:00
|
|
|
toggleHistoricalView,
|
2020-07-05 09:22:40 +00:00
|
|
|
toggleHiddenCanvases,
|
2020-01-22 14:34:46 +00:00
|
|
|
togglePixelNotify,
|
|
|
|
toggleMute,
|
2020-04-28 05:16:47 +00:00
|
|
|
notify,
|
2021-06-18 11:35:53 +00:00
|
|
|
selectCanvas,
|
2020-01-22 14:34:46 +00:00
|
|
|
} from '../actions';
|
|
|
|
|
2021-01-27 21:10:06 +00:00
|
|
|
const usedKeys = ['g', 'h', 'x', 'm', 'r', 'p'];
|
2020-01-22 14:34:46 +00:00
|
|
|
|
|
|
|
function onKeyPress(event: KeyboardEvent) {
|
|
|
|
// ignore key presses if modal is open or chat is used
|
|
|
|
if (event.target.nodeName === 'INPUT'
|
|
|
|
|| event.target.nodeName === 'TEXTAREA'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:35:53 +00:00
|
|
|
let { key } = event;
|
|
|
|
|
|
|
|
const num = Number(key);
|
|
|
|
if (!Number.isNaN(num) && num > 0) {
|
|
|
|
// switch to canvas on num keys
|
|
|
|
const { canvases } = store.getState().canvas;
|
|
|
|
const canvasIds = Object.keys(canvases).filter((id) => !canvases[id].hid);
|
|
|
|
if (num <= canvasIds.length) {
|
|
|
|
const canvasId = canvasIds[num - 1];
|
|
|
|
store.dispatch(selectCanvas(canvasId));
|
|
|
|
const canvasName = canvases[canvasId].title;
|
|
|
|
store.dispatch(notify(t`Switched to ${canvasName}`));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:10:06 +00:00
|
|
|
/*
|
|
|
|
* if char of key isn't used by a keybind,
|
|
|
|
* we check if the key location is where a
|
|
|
|
* key that is used would be on QWERTY
|
|
|
|
*/
|
2021-01-29 21:46:58 +00:00
|
|
|
if (!usedKeys.includes(key)) {
|
|
|
|
key = event.code;
|
|
|
|
if (!key.startsWith('Key')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
key = key.substr(-1).toLowerCase();
|
|
|
|
}
|
2021-01-27 21:10:06 +00:00
|
|
|
|
|
|
|
switch (key) {
|
2020-01-22 14:34:46 +00:00
|
|
|
case 'g':
|
|
|
|
store.dispatch(toggleGrid());
|
2021-06-18 11:35:53 +00:00
|
|
|
store.dispatch(notify((store.getState().gui.showGrid)
|
|
|
|
? t`Grid ON`
|
|
|
|
: t`Grid OFF`));
|
2021-01-27 21:10:06 +00:00
|
|
|
return;
|
2020-06-23 16:49:56 +00:00
|
|
|
case 'h':
|
|
|
|
store.dispatch(toggleHistoricalView());
|
2021-01-27 21:10:06 +00:00
|
|
|
return;
|
2020-04-28 05:16:47 +00:00
|
|
|
case 'x':
|
2020-01-22 14:34:46 +00:00
|
|
|
store.dispatch(togglePixelNotify());
|
2021-06-18 11:35:53 +00:00
|
|
|
store.dispatch(notify((store.getState().gui.showPixelNotify)
|
|
|
|
? t`Pixel Notify ON`
|
|
|
|
: t`Pixel Notify OFF`));
|
2021-01-27 21:10:06 +00:00
|
|
|
return;
|
2020-01-22 14:34:46 +00:00
|
|
|
case 'm':
|
|
|
|
store.dispatch(toggleMute());
|
2021-06-18 11:35:53 +00:00
|
|
|
store.dispatch(notify((store.getState().audio.mute)
|
|
|
|
? t`Muted Sound`
|
|
|
|
: t`Unmuted Sound`));
|
2021-01-27 21:10:06 +00:00
|
|
|
return;
|
2020-04-29 02:38:36 +00:00
|
|
|
case 'r': {
|
2021-06-18 11:35:53 +00:00
|
|
|
const { hover } = store.getState().gui;
|
2020-04-28 05:16:47 +00:00
|
|
|
const text = hover.join('_');
|
|
|
|
copy(text);
|
2021-01-30 07:02:31 +00:00
|
|
|
store.dispatch(notify(t`Copied!`));
|
2021-01-27 21:10:06 +00:00
|
|
|
return;
|
2020-04-28 05:16:47 +00:00
|
|
|
}
|
2021-01-27 21:10:06 +00:00
|
|
|
case 'p':
|
2020-07-05 09:22:40 +00:00
|
|
|
store.dispatch(toggleHiddenCanvases());
|
2021-06-18 11:35:53 +00:00
|
|
|
store.dispatch(notify((store.getState().canvas.showHiddenCanvases)
|
|
|
|
? t`Show Hidden Canvases`
|
|
|
|
: t`Hide Hidden Canvases`));
|
2020-07-05 09:22:40 +00:00
|
|
|
break;
|
2020-01-22 14:34:46 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default onKeyPress;
|