mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
Merge pull request #339 from mdashlw/keyCode
use keyCode instead of key
This commit is contained in:
commit
588580ad7c
2 changed files with 22 additions and 22 deletions
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue