Skip to content

Commit

Permalink
implement hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinfuchs committed Mar 26, 2022
1 parent 24aa3b7 commit 40c6fae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frontend/size-plugin.json

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions frontend/src/components/PasteMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {route} from "preact-router";
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'
import {faFileArrowUp, faFileCirclePlus, faFileLines, faFilePen} from "@fortawesome/free-solid-svg-icons";
import {faGithub} from "@fortawesome/free-brands-svg-icons";
import {useEffect} from "preact/hooks";

export default function PasteMenu({pasteId, content, setContent, language}) {
const canSave = !pasteId && content.length !== 0
Expand Down Expand Up @@ -34,6 +35,7 @@ export default function PasteMenu({pasteId, content, setContent, language}) {
}

function onDuplicate() {
if (!pasteId) return
route("/")
}

Expand All @@ -42,6 +44,38 @@ export default function PasteMenu({pasteId, content, setContent, language}) {
window.location.href = `/api/pastes/${pasteId}/raw`;
}

useEffect(() => {
function onKeyDown(e) {
if (!e.ctrlKey) return;
console.log(e.key)
switch (e.key) {
case "s":
e.preventDefault();
onSave();
break;
case "n":
e.preventDefault();
onCreateNew();
break;
case "d":
e.preventDefault();
onDuplicate()
break;
case "r":
e.preventDefault();
if (e.shiftKey) onOpenRaw();
break;
case "R":
e.preventDefault();
if (e.shiftKey) onOpenRaw();
break;
}
}

document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown)
}, [pasteId, content, setContent, language])

return (
<div className={style.menu}>
<div className={style.button}>
Expand Down

0 comments on commit 40c6fae

Please sign in to comment.