1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom.ts';
3 import {buildForInput} from '../wysiwyg-tinymce/config';
5 export interface CommentReplyEvent extends Event {
7 id: string; // ID of comment being replied to
8 element: HTMLElement; // Container for comment replied to
12 export class PageComments extends Component {
14 private elem: HTMLElement;
15 private pageId: number;
16 private container: HTMLElement;
17 private commentCountBar: HTMLElement;
18 private commentsTitle: HTMLElement;
19 private addButtonContainer: HTMLElement;
20 private replyToRow: HTMLElement;
21 private formContainer: HTMLElement;
22 private form: HTMLFormElement;
23 private formInput: HTMLInputElement;
24 private formReplyLink: HTMLAnchorElement;
25 private addCommentButton: HTMLElement;
26 private hideFormButton: HTMLElement;
27 private removeReplyToButton: HTMLElement;
28 private wysiwygLanguage: string;
29 private wysiwygTextDirection: string;
30 private wysiwygEditor: any = null;
31 private createdText: string;
32 private countText: string;
33 private parentId: number | null = null;
34 private contentReference: string = '';
35 private formReplyText: string = '';
39 this.pageId = Number(this.$opts.pageId);
42 this.container = this.$refs.commentContainer;
43 this.commentCountBar = this.$refs.commentCountBar;
44 this.commentsTitle = this.$refs.commentsTitle;
45 this.addButtonContainer = this.$refs.addButtonContainer;
46 this.replyToRow = this.$refs.replyToRow;
47 this.formContainer = this.$refs.formContainer;
48 this.form = this.$refs.form as HTMLFormElement;
49 this.formInput = this.$refs.formInput as HTMLInputElement;
50 this.formReplyLink = this.$refs.formReplyLink as HTMLAnchorElement;
51 this.addCommentButton = this.$refs.addCommentButton;
52 this.hideFormButton = this.$refs.hideFormButton;
53 this.removeReplyToButton = this.$refs.removeReplyToButton;
56 this.wysiwygLanguage = this.$opts.wysiwygLanguage;
57 this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
60 this.createdText = this.$opts.createdText;
61 this.countText = this.$opts.countText;
63 this.formReplyText = this.formReplyLink?.textContent || '';
65 this.setupListeners();
68 protected setupListeners(): void {
69 this.elem.addEventListener('page-comment-delete', () => {
70 setTimeout(() => this.updateCount(), 1);
74 this.elem.addEventListener('page-comment-reply', (event: CommentReplyEvent) => {
75 this.setReply(event.detail.id, event.detail.element);
79 this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
80 this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
81 this.addCommentButton.addEventListener('click', this.showForm.bind(this));
82 this.form.addEventListener('submit', this.saveComment.bind(this));
86 protected saveComment(event): void {
87 event.preventDefault();
88 event.stopPropagation();
90 const loading = getLoading();
91 loading.classList.add('px-l');
92 this.form.after(loading);
93 this.form.toggleAttribute('hidden', true);
96 html: this.wysiwygEditor.getContent(),
97 parent_id: this.parentId || null,
98 content_ref: this.contentReference || '',
101 window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => {
102 const newElem = htmlToDom(resp.data as string);
104 if (reqData.parent_id) {
105 this.formContainer.after(newElem);
107 this.container.append(newElem);
110 window.$events.success(this.createdText);
114 this.form.toggleAttribute('hidden', false);
115 window.$events.showValidationErrors(err);
118 this.form.toggleAttribute('hidden', false);
122 protected updateCount(): void {
123 const count = this.getCommentCount();
124 this.commentsTitle.textContent = window.$trans.choice(this.countText, count);
127 protected resetForm(): void {
129 this.formInput.value = '';
130 this.parentId = null;
131 this.contentReference = '';
132 this.replyToRow.toggleAttribute('hidden', true);
133 this.container.append(this.formContainer);
136 protected showForm(): void {
138 this.formContainer.toggleAttribute('hidden', false);
139 this.addButtonContainer.toggleAttribute('hidden', true);
140 this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
144 protected hideForm(): void {
146 this.formContainer.toggleAttribute('hidden', true);
147 if (this.getCommentCount() > 0) {
148 this.elem.append(this.addButtonContainer);
150 this.commentCountBar.append(this.addButtonContainer);
152 this.addButtonContainer.toggleAttribute('hidden', false);
155 protected loadEditor(): void {
156 if (this.wysiwygEditor) {
157 this.wysiwygEditor.focus();
161 const config = buildForInput({
162 language: this.wysiwygLanguage,
163 containerElement: this.formInput,
164 darkMode: document.documentElement.classList.contains('dark-mode'),
165 textDirection: this.wysiwygTextDirection,
169 translationMap: (window as Record<string, Object>).editor_translations,
172 (window as {tinymce: {init: (Object) => Promise<any>}}).tinymce.init(config).then(editors => {
173 this.wysiwygEditor = editors[0];
174 setTimeout(() => this.wysiwygEditor.focus(), 50);
178 protected removeEditor(): void {
179 if (this.wysiwygEditor) {
180 this.wysiwygEditor.remove();
181 this.wysiwygEditor = null;
185 protected getCommentCount(): number {
186 return this.container.querySelectorAll('[component="page-comment"]').length;
189 protected setReply(commentLocalId, commentElement): void {
190 const targetFormLocation = commentElement.closest('.comment-branch').querySelector('.comment-branch-children');
191 targetFormLocation.append(this.formContainer);
193 this.parentId = commentLocalId;
194 this.replyToRow.toggleAttribute('hidden', false);
195 this.formReplyLink.textContent = this.formReplyText.replace('1234', String(this.parentId));
196 this.formReplyLink.href = `#comment${this.parentId}`;
199 protected removeReplyTo(): void {
200 this.parentId = null;
201 this.replyToRow.toggleAttribute('hidden', true);
202 this.container.append(this.formContainer);
206 public startNewComment(contentReference: string): void {
207 this.removeReplyTo();
208 this.contentReference = contentReference;