1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
3 import {buildForInput} from '../wysiwyg-tinymce/config';
4 import {PageCommentReference} from "./page-comment-reference";
5 import {HttpError} from "../services/http";
7 export interface PageCommentReplyEventData {
8 id: string; // ID of comment being replied to
9 element: HTMLElement; // Container for comment replied to
12 export interface PageCommentArchiveEventData {
13 new_thread_dom: HTMLElement;
16 export class PageComment extends Component {
18 protected commentId!: string;
19 protected commentLocalId!: string;
20 protected deletedText!: string;
21 protected updatedText!: string;
22 protected archiveText!: string;
24 protected wysiwygEditor: any = null;
25 protected wysiwygLanguage!: string;
26 protected wysiwygTextDirection!: string;
28 protected container!: HTMLElement;
29 protected contentContainer!: HTMLElement;
30 protected form!: HTMLFormElement;
31 protected formCancel!: HTMLElement;
32 protected editButton!: HTMLElement;
33 protected deleteButton!: HTMLElement;
34 protected replyButton!: HTMLElement;
35 protected archiveButton!: HTMLElement;
36 protected input!: HTMLInputElement;
40 this.commentId = this.$opts.commentId;
41 this.commentLocalId = this.$opts.commentLocalId;
42 this.deletedText = this.$opts.deletedText;
43 this.deletedText = this.$opts.deletedText;
44 this.archiveText = this.$opts.archiveText;
46 // Editor reference and text options
47 this.wysiwygLanguage = this.$opts.wysiwygLanguage;
48 this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
51 this.container = this.$el;
52 this.contentContainer = this.$refs.contentContainer;
53 this.form = this.$refs.form as HTMLFormElement;
54 this.formCancel = this.$refs.formCancel;
55 this.editButton = this.$refs.editButton;
56 this.deleteButton = this.$refs.deleteButton;
57 this.replyButton = this.$refs.replyButton;
58 this.archiveButton = this.$refs.archiveButton;
59 this.input = this.$refs.input as HTMLInputElement;
61 this.setupListeners();
64 protected setupListeners(): void {
65 if (this.replyButton) {
66 const data: PageCommentReplyEventData = {
67 id: this.commentLocalId,
68 element: this.container,
70 this.replyButton.addEventListener('click', () => this.$emit('reply', data));
73 if (this.editButton) {
74 this.editButton.addEventListener('click', this.startEdit.bind(this));
75 this.form.addEventListener('submit', this.update.bind(this));
76 this.formCancel.addEventListener('click', () => this.toggleEditMode(false));
79 if (this.deleteButton) {
80 this.deleteButton.addEventListener('click', this.delete.bind(this));
83 if (this.archiveButton) {
84 this.archiveButton.addEventListener('click', this.archive.bind(this));
88 protected toggleEditMode(show: boolean) : void {
89 this.contentContainer.toggleAttribute('hidden', show);
90 this.form.toggleAttribute('hidden', !show);
93 protected startEdit() : void {
94 this.toggleEditMode(true);
96 if (this.wysiwygEditor) {
97 this.wysiwygEditor.focus();
101 const config = buildForInput({
102 language: this.wysiwygLanguage,
103 containerElement: this.input,
104 darkMode: document.documentElement.classList.contains('dark-mode'),
105 textDirection: this.wysiwygTextDirection,
109 translationMap: (window as unknown as Record<string, Object>).editor_translations,
112 (window as unknown as {tinymce: {init: (arg0: Object) => Promise<any>}}).tinymce.init(config).then(editors => {
113 this.wysiwygEditor = editors[0];
114 setTimeout(() => this.wysiwygEditor.focus(), 50);
118 protected async update(event: Event): Promise<void> {
119 event.preventDefault();
120 const loading = this.showLoading();
121 this.form.toggleAttribute('hidden', true);
124 html: this.wysiwygEditor.getContent(),
128 const resp = await window.$http.put(`/comment/${this.commentId}`, reqData);
129 const newComment = htmlToDom(resp.data as string);
130 this.container.replaceWith(newComment);
131 window.$events.success(this.updatedText);
134 if (err instanceof HttpError) {
135 window.$events.showValidationErrors(err);
137 this.form.toggleAttribute('hidden', false);
142 protected async delete(): Promise<void> {
145 await window.$http.delete(`/comment/${this.commentId}`);
146 this.$emit('delete');
148 const branch = this.container.closest('.comment-branch');
149 if (branch instanceof HTMLElement) {
150 const refs = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
151 for (const ref of refs) {
157 window.$events.success(this.deletedText);
160 protected async archive(): Promise<void> {
162 const isArchived = this.archiveButton.dataset.isArchived === 'true';
163 const action = isArchived ? 'unarchive' : 'archive';
165 const response = await window.$http.put(`/comment/${this.commentId}/${action}`);
166 window.$events.success(this.archiveText);
167 const eventData: PageCommentArchiveEventData = {new_thread_dom: htmlToDom(response.data as string)};
168 this.$emit(action, eventData);
170 const branch = this.container.closest('.comment-branch') as HTMLElement;
171 const references = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
172 for (const reference of references) {
173 reference.hideMarker();
178 protected showLoading(): HTMLElement {
179 const loading = getLoading();
180 loading.classList.add('px-l');
181 this.container.append(loading);