From 2861e35e8cf44df365c288509cd32ca02eb2db7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Tue, 17 Dec 2024 15:21:03 +0100 Subject: [PATCH] Add r shortcut for Desktop commentors --- src/views/story.mjs | 1 + src/web/src/CommentInput.jsx | 37 +++++++++++++++++++++++++++++++++- src/web/src/CommentSection.jsx | 5 ++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/views/story.mjs b/src/views/story.mjs index 678bd14..6f8c8fc 100644 --- a/src/views/story.mjs +++ b/src/views/story.mjs @@ -343,6 +343,7 @@ export default async function (trie, theme, index, value, referral) { >Moderated because: "${comment.reason}"` : html` { button.style.display = "none"; } } + const textareaRef = useRef(null); + useEffect(() => { + const handleKeyPress = (e) => { + if (e.key !== "r") return; + + const selection = window.getSelection(); + const selected = selection.toString().trim(); + if (!selected || !address || (isEligible !== null && !isEligible)) return; + + // Only proceed if selection is within a comment-text span + if ( + !selection.anchorNode?.parentElement?.classList?.contains( + "comment-text", + ) + ) + return; + + const textarea = textareaRef.current; + if (!textarea) return; + + const start = textarea.selectionStart; + const end = textarea.selectionEnd; + const quote = `> ${selected}`; + + setText(text.slice(0, start) + quote + text.slice(end)); + + setTimeout(() => { + textarea.selectionStart = textarea.selectionEnd = start + quote.length; + }, 0); + }; + + document.addEventListener("keydown", handleKeyPress); + return () => document.removeEventListener("keydown", handleKeyPress); + }, [text, address, isEligible]); return (
{ }} >