diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2023-08-27 19:16:18 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2023-08-27 19:18:32 -0400 |
| commit | 67efc0047a195d790ec2b493eb1d225183582be4 (patch) | |
| tree | 1e506698dff2cdd32f18d655860bb7b06c4a0736 /haunt/static/js/comment-reaction.js | |
| parent | c0b1efb4fec8e677d2e00a949dcf834f94243f6e (diff) | |
[comments] Fix but with "react" button
Previously, clicking the button multiple times would create multiple selector
widgets. Now, it functions as a visibility toggle.
Diffstat (limited to 'haunt/static/js/comment-reaction.js')
| -rw-r--r-- | haunt/static/js/comment-reaction.js | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/haunt/static/js/comment-reaction.js b/haunt/static/js/comment-reaction.js index db980b3..1002fa2 100644 --- a/haunt/static/js/comment-reaction.js +++ b/haunt/static/js/comment-reaction.js @@ -54,9 +54,12 @@ function postReaction(reaction, callback) { xhr.send(encodeAsFormData(reaction)); } +function selectorWidgetName(id) { + return `emoji-picker-${id}`; +} // Simple Emoji picker widget. -function selectorWidget() { +function selectorWidget(id) { function emojiButton(codepoint) { let span = document.createElement("span"); span.innerHTML = String.fromCodePoint(codepoint); @@ -64,6 +67,7 @@ function selectorWidget() { } let selector = document.createElement("div"); + selector.setAttribute("id", selectorWidgetName(id)); selector.setAttribute("class", "emoji-picker"); for (let i = 0x1F600; i < 0x1F64F; i++) { @@ -99,15 +103,25 @@ window.addEventListener("load", () => { // Second pass to add the actual "react" button. replyButtons = document.querySelectorAll(".comment-reply-button"); for (let button of replyButtons) { + let id = button.getAttribute("data-reply-to-id"); let reactButton = document.createElement("a"); reactButton.setAttribute("class", "comment-react-button"); reactButton.setAttribute("href", "#"); - reactButton.setAttribute("data-react-to-id", button.getAttribute("data-reply-to-id")); + reactButton.setAttribute("data-react-to-id", id); reactButton.innerHTML = "react"; button.parentNode.appendChild(reactButton); reactButton.addEventListener('click', (e) => { - let emojiPicker = selectorWidget(); + // If we already made a selector widget for this comment, let's "hide" the + // one that exists. + let existingSelectorWidget = document.getElementById(selectorWidgetName(id)); + if (existingSelectorWidget !== null) { + existingSelectorWidget.remove(); + e.preventDefault(); + return; + } + + let emojiPicker = selectorWidget(id); button.parentNode.parentNode.insertBefore(emojiPicker, button.parentNode); for (let button of emojiPicker.children) { button.addEventListener('click', (e) => { |