diff --git a/app/components/comment_component.rb b/app/components/comment_component.rb new file mode 100644 index 000000000..562a9b5c5 --- /dev/null +++ b/app/components/comment_component.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class CommentComponent < ApplicationComponent + attr_reader :comment, :context, :dtext_data, :moderation_reports, :show_deleted, :current_user + delegate :link_to_user, :time_ago_in_words_tagged, :format_text, :policy, to: :helpers + + def initialize(comment:, context: nil, dtext_data: nil, moderation_reports: [], show_deleted: false, current_user: User.anonymous) + @comment = comment + @context = context + @dtext_data = dtext_data + @moderation_reports = moderation_reports + @show_deleted = show_deleted + @current_user = current_user + end + + def render? + !comment.is_deleted? || show_deleted || current_user.is_moderator? + end +end diff --git a/app/components/comment_component/comment_component.html.erb b/app/components/comment_component/comment_component.html.erb new file mode 100644 index 000000000..ebf3b7995 --- /dev/null +++ b/app/components/comment_component/comment_component.html.erb @@ -0,0 +1,65 @@ + + diff --git a/app/components/comment_component/comment_component.scss b/app/components/comment_component/comment_component.scss new file mode 100644 index 000000000..a1ecd01d3 --- /dev/null +++ b/app/components/comment_component/comment_component.scss @@ -0,0 +1,31 @@ +article.comment { + flex: 1; + + &[data-is-sticky="true"] { + background: var(--comment-sticky-background-color); + } + + &[data-is-reported="true"] { + border: var(--moderation-report-border); + } + + &[data-is-voted="true"] { + .comment-vote-up-link, .comment-vote-down-link { + display: none; + } + } + + &[data-is-voted="false"] { + .comment-unvote-link { + display: none; + } + } + + &[data-below-threshold="true"][data-is-sticky="false"] { + opacity: 0.3; + + &:hover { + opacity: 1; + } + } +} diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 000000000..3fb85f989 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,10 @@ +module CommentsHelper + def render_comment(comment, **options) + render CommentComponent.new(comment: comment, **options) + end + + def render_comment_list(comments, **options) + dtext_data = DText.preprocess(comments.map(&:body)) + render CommentComponent.with_collection(comments, dtext_data: dtext_data, **options) + end +end diff --git a/app/javascript/src/styles/specific/comments.scss b/app/javascript/src/styles/specific/comments.scss index 5bf5d95f9..47f01878f 100644 --- a/app/javascript/src/styles/specific/comments.scss +++ b/app/javascript/src/styles/specific/comments.scss @@ -10,40 +10,6 @@ div.comments-for-post { div.hidden-comments-notice { margin: 1em 0; } - - div.list-of-comments { - article.comment { - flex: 1; - - &[data-is-sticky="true"] { - background: var(--comment-sticky-background-color); - } - - &[data-is-reported="true"] { - border: var(--moderation-report-border); - } - - &[data-is-voted="true"] { - .comment-vote-up-link, .comment-vote-down-link { - display: none; - } - } - - &[data-is-voted="false"] { - .comment-unvote-link { - display: none; - } - } - - &[data-below-threshold="true"][data-is-sticky="false"] { - opacity: 0.3; - - &:hover { - opacity: 1; - } - } - } - } } div#c-comments { diff --git a/app/views/comments/_index_by_comment.html.erb b/app/views/comments/_index_by_comment.html.erb index 3a2f5cc2d..e44411e56 100644 --- a/app/views/comments/_index_by_comment.html.erb +++ b/app/views/comments/_index_by_comment.html.erb @@ -1,5 +1,6 @@