Files
danbooru/app/components/comment_component/comment_component.html.erb
evazion 8ef05ec69b Fix #5021: Downvoting/upvoting a revealed hidden comment will hide it again.
Fix it so that upvoting or downvoting a revealed thresholded comment
doesn't hide it again.

The fix is to explicitly store a `data-show-thresholded` flag on the
comment, instead of manually hiding elements with jQuery, and to morph
the comment HTML instead of replacing it so that the state isn't lost
after voting. Alpine.js is used for this, which isn't strictly necessary,
but is useful to test the library before adopting it on a wider scale.

https://alpinejs.dev/start-here
2022-02-28 20:11:22 -06:00

156 lines
6.4 KiB
Plaintext

<article id="comment_<%= comment.id %>" class="comment message"
x-data="{ showThresholded: false }"
x-bind:data-show-thresholded="String(showThresholded)"
data-id="<%= comment.id %>"
data-post-id="<%= comment.post_id %>"
data-creator-id="<%= comment.creator_id unless redact_deleted? %>"
data-updater-id="<%= comment.updater_id unless redact_deleted? %>"
data-score="<%= comment.score %>"
data-do-not-bump-post="<%= comment.do_not_bump_post? %>"
data-is-deleted="<%= comment.is_deleted? %>"
data-is-sticky="<%= comment.is_sticky? %>"
data-is-dimmed="<%= dimmed? %>"
data-is-thresholded="<%= thresholded? %>"
data-is-reported="<%= reported? %>"
data-is-upvoted="<%= upvoted? %>"
data-is-downvoted="<%= downvoted? %>"
data-show-thresholded="false">
<div class="author">
<div class="author-name">
<% if redact_deleted? %>
[deleted]
<% else %>
<%= link_to_user comment.creator %>
<% if comment.is_deleted? %>
(deleted)
<% end %>
<% end %>
</div>
<%= link_to time_ago_in_words_tagged(comment.created_at), comment, class: "message-timestamp", rel: "nofollow" %>
</div>
<div class="content">
<%= link_to "[hidden]", "javascript:void(0)", class: "unhide-comment-link", "x-on:click": "showThresholded = true" %>
<%= tag.div class: "body prose" do %>
<% if redact_deleted? %>
<p>[deleted]</p>
<% else %>
<%= format_text(comment.body, data: dtext_data) %>
<% end %>
<%= render "application/update_notice", record: comment %>
<% end %>
<menu class="mt-2">
<% if votable? %>
<li class="comment-votes">
<% if current_user.is_anonymous? %>
<%= link_to upvote_icon, login_path(url: request.fullpath), class: "comment-upvote-link inactive-link" %>
<% elsif upvoted? %>
<%= link_to upvote_icon, comment_vote_path(current_vote), class: "comment-upvote-link comment-unvote-link active-link", method: :delete, remote: true %>
<% else %>
<%= link_to upvote_icon, comment_comment_votes_path(comment_id: comment.id, score: "1"), class: "comment-upvote-link inactive-link", method: :post, remote: true %>
<% end %>
<span class="comment-score">
<% if policy(CommentVote).can_see_votes? %>
<%= link_to comment.score, comment_votes_path(search: { comment_id: comment.id }, variant: "compact"), class: "inactive-link" %>
<% else %>
<%= comment.score %>
<% end %>
</span>
<% if current_user.is_anonymous? %>
<%= link_to downvote_icon, login_path(url: request.fullpath), class: "comment-downvote-link inactive-link" %>
<% elsif downvoted? %>
<%= link_to downvote_icon, comment_vote_path(current_vote), class: "comment-downvote-link comment-unvote-link active-link", method: :delete, remote: true %>
<% else %>
<%= link_to downvote_icon, comment_comment_votes_path(comment_id: comment.id, score: "-1"), class: "comment-downvote-link inactive-link", method: :post, remote: true %>
<% end %>
</li>
<% end %>
<% if policy(comment).reply? %>
<li class="comment-reply">
<% if current_user.is_anonymous? %>
<%= link_to "Reply", login_path(url: request.fullpath) %>
<% elsif context == :index_by_comment %>
<%= link_to "Reply", new_comment_path(id: comment, comment: { post_id: comment.post_id }) %>
<% else %>
<%= link_to "Reply", new_comment_path(id: comment, comment: { post_id: comment.post_id }), remote: true %>
<% end %>
</li>
<% end %>
<% if reported? %>
<li class="moderation-report-notice">
Reported (<%= link_to pluralize(comment.pending_moderation_reports.length, "report"), moderation_reports_path(search: { model_type: "Comment", model_id: comment.id, status: "pending" }) %>)
</li>
<% end %>
<%= render PopupMenuComponent.new do |menu| %>
<% if policy(comment).update? %>
<% menu.item do %>
<%= link_to edit_comment_path(comment.id), id: "edit_comment_link_#{comment.id}", class: "edit_comment_link" do %>
<%= edit_icon %> Edit
<% end %>
<% end %>
<% menu.item do %>
<% if comment.is_deleted? %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_deleted]=false", method: :put, remote: true do %>
<%= undelete_icon %> Undelete
<% end %>
<% else %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_deleted]=true", "data-confirm": "Are you sure you want to delete this comment?", method: :put, remote: true do %>
<%= delete_icon %> Delete
<% end %>
<% end %>
<% end %>
<% end %>
<% if policy(comment).can_sticky_comment? %>
<% menu.item do %>
<% if comment.is_sticky? %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_sticky]=false", method: :put, remote: true do %>
<%= unsticky_icon %> Unsticky
<% end %>
<% else %>
<%= link_to comment_path(comment.id), "data-params": "comment[is_sticky]=true", method: :put, remote: true do %>
<%= sticky_icon %> Sticky
<% end %>
<% end %>
<% end %>
<% end %>
<% if policy(comment).reportable? %>
<% menu.item do %>
<%= link_to new_moderation_report_path(moderation_report: { model_type: "Comment", model_id: comment.id }), remote: true do %>
<%= flag_icon %> Report
<% end %>
<% end %>
<% end %>
<% menu.item do %>
<%= link_to comment_path(comment.id), class: "comment-copy-id" do %>
<%= hashtag_icon %> Copy ID
<% end %>
<% end %>
<% menu.item do %>
<%= link_to comment_path(comment.id), class: "comment-copy-link" do %>
<%= link_icon %> Copy Link
<% end %>
<% end %>
<% end %>
</menu>
<% if policy(comment).update? %>
<%= render "comments/form", comment: comment, hidden: true %>
<% end %>
</div>
</article>