pixelplanet/src/controls/keypress.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

/*
* keypress actions
*/
import { t } from 'ttag';
import copy from '../utils/clipboard';
import {
toggleGrid,
2020-06-23 16:49:56 +00:00
toggleHistoricalView,
toggleHiddenCanvases,
togglePixelNotify,
toggleMute,
selectCanvas,
2022-07-11 12:42:09 +00:00
} from '../store/actions';
import {
notify,
} from '../store/actions/thunks';
const usedKeys = ['g', 'h', 'x', 'm', 'r', 'p'];
2022-07-11 13:33:29 +00:00
function createKeyPressHandler(store) {
return (event) => {
// ignore key presses if modal is open or chat is used
if (event.target.nodeName === 'INPUT'
|| event.target.nodeName === 'TEXTAREA'
) {
return;
}
2022-07-11 13:33:29 +00:00
let { key } = event;
2022-07-11 13:33:29 +00:00
const num = Number(key);
if (!Number.isNaN(num) && num > 0) {
// switch to canvas on num keys
const { canvases, canvasId: curCanvasId } = store.getState().canvas;
const canvasIds = Object.keys(canvases).filter((id) => !canvases[id].hid);
if (num <= canvasIds.length) {
const canvasId = canvasIds[num - 1];
if (canvasId !== curCanvasId) {
2022-07-11 13:33:29 +00:00
store.dispatch(selectCanvas(canvasId));
const canvasName = canvases[canvasId].title;
store.dispatch(notify(t`Switched to ${canvasName}`));
}
2021-07-16 10:06:20 +00:00
}
2022-07-11 13:33:29 +00:00
return;
}
2022-07-11 13:33:29 +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
*/
if (!usedKeys.includes(key)) {
key = event.code;
if (!key.startsWith('Key')) {
return;
}
key = key.slice(-1).toLowerCase();
2021-01-29 21:46:58 +00:00
}
2022-07-11 13:33:29 +00:00
switch (key) {
case 'g':
store.dispatch(toggleGrid());
store.dispatch(notify((store.getState().gui.showGrid)
? t`Grid ON`
: t`Grid OFF`));
return;
case 'h':
if (window.ssv && window.ssv.backupurl) {
store.dispatch(toggleHistoricalView());
}
return;
case 'x':
store.dispatch(togglePixelNotify());
store.dispatch(notify((store.getState().gui.showPixelNotify)
? t`Pixel Notify ON`
: t`Pixel Notify OFF`));
return;
case 'm':
store.dispatch(toggleMute());
2022-09-05 08:59:06 +00:00
store.dispatch(notify((store.getState().gui.mute)
2022-07-11 13:33:29 +00:00
? t`Muted Sound`
: t`Unmuted Sound`));
return;
case 'r': {
2022-08-17 19:48:50 +00:00
const { hover } = store.getState().canvas;
2022-07-11 13:33:29 +00:00
const text = hover.join('_');
copy(text);
store.dispatch(notify(t`Copied!`));
return;
}
2022-07-11 13:33:29 +00:00
case 'p':
store.dispatch(toggleHiddenCanvases());
store.dispatch(notify((store.getState().canvas.showHiddenCanvases)
? t`Show Hidden Canvases`
: t`Hide Hidden Canvases`));
break;
default:
}
2022-07-11 13:33:29 +00:00
};
}
2022-07-11 13:33:29 +00:00
export default createKeyPressHandler;