Merge pull request #339 from mdashlw/keyCode

use keyCode instead of key
This commit is contained in:
liamwhite 2024-07-22 12:22:40 -04:00 committed by GitHub
commit 588580ad7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 22 deletions

View file

@ -7,19 +7,19 @@ import { $, $$ } from './utils/dom';
const markdownSyntax = { const markdownSyntax = {
bold: { bold: {
action: wrapSelection, action: wrapSelection,
options: { prefix: '**', shortcutKey: 'b' }, options: { prefix: '**', shortcutKeyCode: 66 },
}, },
italics: { italics: {
action: wrapSelection, action: wrapSelection,
options: { prefix: '*', shortcutKey: 'i' }, options: { prefix: '*', shortcutKeyCode: 73 },
}, },
under: { under: {
action: wrapSelection, action: wrapSelection,
options: { prefix: '__', shortcutKey: 'u' }, options: { prefix: '__', shortcutKeyCode: 85 },
}, },
spoiler: { spoiler: {
action: wrapSelection, action: wrapSelection,
options: { prefix: '||', shortcutKey: 's' }, options: { prefix: '||', shortcutKeyCode: 83 },
}, },
code: { code: {
action: wrapSelectionOrLines, action: wrapSelectionOrLines,
@ -29,7 +29,7 @@ const markdownSyntax = {
prefixMultiline: '```\n', prefixMultiline: '```\n',
suffixMultiline: '\n```', suffixMultiline: '\n```',
singleWrap: true, singleWrap: true,
shortcutKey: 'e', shortcutKeyCode: 69,
}, },
}, },
strike: { strike: {
@ -50,11 +50,11 @@ const markdownSyntax = {
}, },
link: { link: {
action: insertLink, action: insertLink,
options: { shortcutKey: 'l' }, options: { shortcutKeyCode: 76 },
}, },
image: { image: {
action: insertLink, action: insertLink,
options: { image: true, shortcutKey: 'k' }, options: { image: true, shortcutKeyCode: 75 },
}, },
escape: { escape: {
action: escapeSelection, action: escapeSelection,
@ -257,10 +257,10 @@ function shortcutHandler(event) {
} }
const textarea = event.target, const textarea = event.target,
key = event.key.toLowerCase(); keyCode = event.keyCode;
for (const id in markdownSyntax) { for (const id in markdownSyntax) {
if (key === markdownSyntax[id].options.shortcutKey) { if (keyCode === markdownSyntax[id].options.shortcutKeyCode) {
markdownSyntax[id].action(textarea, markdownSyntax[id].options); markdownSyntax[id].action(textarea, markdownSyntax[id].options);
event.preventDefault(); event.preventDefault();
} }

View file

@ -4,7 +4,7 @@
import { $ } from './utils/dom'; import { $ } from './utils/dom';
type ShortcutKeyMap = Record<string, () => void>; type ShortcutKeyMap = Record<number, () => void>;
function getHover(): string | null { function getHover(): string | null {
const thumbBoxHover = $<HTMLDivElement>('.media-box:hover'); const thumbBoxHover = $<HTMLDivElement>('.media-box:hover');
@ -50,19 +50,19 @@ function isOK(event: KeyboardEvent): boolean {
/* eslint-disable prettier/prettier */ /* eslint-disable prettier/prettier */
const keyCodes: ShortcutKeyMap = { const keyCodes: ShortcutKeyMap = {
j() { click('.js-prev'); }, // J - go to previous image 74() { click('.js-prev'); }, // J - go to previous image
i() { click('.js-up'); }, // I - go to index page 73() { click('.js-up'); }, // I - go to index page
k() { click('.js-next'); }, // K - go to next image 75() { click('.js-next'); }, // K - go to next image
r() { click('.js-rand'); }, // R - go to random image 82() { click('.js-rand'); }, // R - go to random image
s() { click('.js-source-link'); }, // S - go to image source 83() { click('.js-source-link'); }, // S - go to image source
l() { click('.js-tag-sauce-toggle'); }, // L - edit tags 76() { click('.js-tag-sauce-toggle'); }, // L - edit tags
o() { openFullView(); }, // O - open original 79() { openFullView(); }, // O - open original
v() { openFullViewNewTab(); }, // V - open original in a new tab 86() { openFullViewNewTab(); }, // V - open original in a new tab
f() { 70() {
// F - favourite image // F - favourite image
click(getHover() ? `a.interaction--fave[data-image-id="${getHover()}"]` : '.block__header a.interaction--fave'); click(getHover() ? `a.interaction--fave[data-image-id="${getHover()}"]` : '.block__header a.interaction--fave');
}, },
u() { 85() {
// U - upvote image // U - upvote image
click(getHover() ? `a.interaction--upvote[data-image-id="${getHover()}"]` : '.block__header a.interaction--upvote'); click(getHover() ? `a.interaction--upvote[data-image-id="${getHover()}"]` : '.block__header a.interaction--upvote');
}, },
@ -72,8 +72,8 @@ const keyCodes: ShortcutKeyMap = {
export function listenForKeys() { export function listenForKeys() {
document.addEventListener('keydown', (event: KeyboardEvent) => { document.addEventListener('keydown', (event: KeyboardEvent) => {
if (isOK(event) && keyCodes[event.key]) { if (isOK(event) && keyCodes[event.keyCode]) {
keyCodes[event.key](); keyCodes[event.keyCode]();
event.preventDefault(); event.preventDefault();
} }
}); });