Skip to content

Commit

Permalink
Merge pull request #8 from adanski/bugfix
Browse files Browse the repository at this point in the history
Fix for handling deleted comments
  • Loading branch information
adanski authored Feb 3, 2023
2 parents 68d0d7e + 9e069d9 commit 539aae8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ax-comments",
"title": "ax-comments",
"version": "2.1.2",
"version": "2.1.3",
"author": {
"name": "Adrian Z."
},
Expand Down
3 changes: 2 additions & 1 deletion src/default-options-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export function getDefaultOptions(): Required<CommentsOptions> {
putComment: (comment, success, error) => success(comment),
deleteComment: (comment, success, error) => success({
...comment,
content: 'Deleted'
content: 'Deleted',
isDeleted: true
}),
upvoteComment: (comment, success, error) => success(comment),
validateAttachments: (attachments, accept) => accept(attachments),
Expand Down
2 changes: 1 addition & 1 deletion src/subcomponent/button-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class ButtonElement extends HTMLButtonElement implements WebComponent {
static createUpvoteButton(commentModel: CommentModelEnriched): ButtonElement {
const upvoteButton: ButtonElement = document.createElement('button', {is: 'ax-button'}) as ButtonElement;
upvoteButton.classList.add('action', 'upvote');
upvoteButton.classList.toggle("disabled", !!commentModel.createdByCurrentUser);
upvoteButton.classList.toggle("disabled", !!commentModel.createdByCurrentUser || !!commentModel.isDeleted);
const upvoteCount: HTMLSpanElement = document.createElement('span');
upvoteCount.classList.add('upvote-count');

Expand Down
10 changes: 4 additions & 6 deletions src/subcomponent/comment-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class CommentContainerElement extends HTMLElement implements WebComponent
}

// Edit
if (commentModel.createdByCurrentUser || this.#options.currentUserIsAdmin) {
if ((commentModel.createdByCurrentUser || this.#options.currentUserIsAdmin) && !commentModel.isDeleted) {
const editButton: ButtonElement = ButtonElement.createActionButton('edit', this.#options.editText, {
onclick: this.#editButtonClicked
});
Expand Down Expand Up @@ -329,11 +329,9 @@ export class CommentContainerElement extends HTMLElement implements WebComponent
};

#isAllowedToDelete(commentModel: CommentModelEnriched): boolean {
if (!this.#options.enableDeleting || !commentModel.createdByCurrentUser && !this.#options.currentUserIsAdmin) {
return false;
} else {
return this.#options.enableDeletingCommentWithReplies || !commentModel.childIds.length;
}
return this.#options.enableDeleting
&& (this.#options.enableDeletingCommentWithReplies || !commentModel.childIds.length)
&& (this.#options.currentUserIsAdmin || !!commentModel.createdByCurrentUser);
}

#deleteButtonClicked: (e: MouseEvent) => void = e => {
Expand Down
11 changes: 10 additions & 1 deletion src/subcomponent/comment-content-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ export class CommentContentFormatter {
}

getFormattedCommentContent(commentModel: CommentModelEnriched, replaceNewLines?: boolean): HTMLElement {
const content: HTMLSpanElement = document.createElement('span');
let html: string = this.escape(DOMPurify.sanitize(commentModel.content));
if (commentModel.isDeleted) {
const deletedContent: HTMLElement = document.createElement('i');
deletedContent.style.color = 'gray';
deletedContent.innerHTML = html;
content.append(deletedContent);
return content;
}

html = this.linkify(html);
html = this.highlightTags(commentModel, html);
if (replaceNewLines) {
html = html.replace(/(?:\n)/g, '<br>');
}
const content: HTMLSpanElement = document.createElement('span');

content.innerHTML = html;
content.querySelectorAll<HTMLElement>('.hashtag')
.forEach(hashtag => hashtag.onclick = this.#hashtagClicked);
Expand Down

0 comments on commit 539aae8

Please sign in to comment.