1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
3 import {PageCommentReference} from "./page-comment-reference";
4 import {HttpError} from "../services/http";
5 import {SimpleWysiwygEditorInterface} from "../wysiwyg";
6 import {el} from "../wysiwyg/utils/dom";
8 export interface PageCommentReplyEventData {
9 id: string; // ID of comment being replied to
10 element: HTMLElement; // Container for comment replied to
13 export interface PageCommentArchiveEventData {
14 new_thread_dom: HTMLElement;
17 export class PageComment extends Component {
19 protected commentId!: string;
20 protected commentLocalId!: string;
21 protected deletedText!: string;
22 protected updatedText!: string;
23 protected archiveText!: string;
25 protected wysiwygEditor: SimpleWysiwygEditorInterface|null = null;
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.updatedText = this.$opts.updatedText;
44 this.archiveText = this.$opts.archiveText;
46 // Editor reference and text options
47 this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
50 this.container = this.$el;
51 this.contentContainer = this.$refs.contentContainer;
52 this.form = this.$refs.form as HTMLFormElement;
53 this.formCancel = this.$refs.formCancel;
54 this.editButton = this.$refs.editButton;
55 this.deleteButton = this.$refs.deleteButton;
56 this.replyButton = this.$refs.replyButton;
57 this.archiveButton = this.$refs.archiveButton;
58 this.input = this.$refs.input as HTMLInputElement;
60 this.setupListeners();
63 protected setupListeners(): void {
64 if (this.replyButton) {
65 const data: PageCommentReplyEventData = {
66 id: this.commentLocalId,
67 element: this.container,
69 this.replyButton.addEventListener('click', () => this.$emit('reply', data));
72 if (this.editButton) {
73 this.editButton.addEventListener('click', this.startEdit.bind(this));
74 this.form.addEventListener('submit', this.update.bind(this));
75 this.formCancel.addEventListener('click', () => this.toggleEditMode(false));
78 if (this.deleteButton) {
79 this.deleteButton.addEventListener('click', this.delete.bind(this));
82 if (this.archiveButton) {
83 this.archiveButton.addEventListener('click', this.archive.bind(this));
87 protected toggleEditMode(show: boolean) : void {
88 this.contentContainer.toggleAttribute('hidden', show);
89 this.form.toggleAttribute('hidden', !show);
92 protected async startEdit(): Promise<void> {
93 this.toggleEditMode(true);
95 if (this.wysiwygEditor) {
96 this.wysiwygEditor.focus();
100 type WysiwygModule = typeof import('../wysiwyg');
101 const wysiwygModule = (await window.importVersioned('wysiwyg')) as WysiwygModule;
102 const editorContent = this.input.value;
103 const container = el('div', {class: 'comment-editor-container'});
104 this.input.parentElement?.appendChild(container);
105 this.input.hidden = true;
107 this.wysiwygEditor = wysiwygModule.createBasicEditorInstance(container as HTMLElement, editorContent, {
108 darkMode: document.documentElement.classList.contains('dark-mode'),
109 textDirection: this.$opts.textDirection,
110 translations: (window as unknown as Record<string, Object>).editor_translations,
113 this.wysiwygEditor.focus();
116 protected async update(event: Event): Promise<void> {
117 event.preventDefault();
118 const loading = this.showLoading();
119 this.form.toggleAttribute('hidden', true);
122 html: await this.wysiwygEditor?.getContentAsHtml() || '',
126 const resp = await window.$http.put(`/comment/${this.commentId}`, reqData);
127 const newComment = htmlToDom(resp.data as string);
128 this.container.replaceWith(newComment);
129 window.$events.success(this.updatedText);
132 if (err instanceof HttpError) {
133 window.$events.showValidationErrors(err);
135 this.form.toggleAttribute('hidden', false);
140 protected async delete(): Promise<void> {
143 await window.$http.delete(`/comment/${this.commentId}`);
144 this.$emit('delete');
146 const branch = this.container.closest('.comment-branch');
147 if (branch instanceof HTMLElement) {
148 const refs = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
149 for (const ref of refs) {
155 window.$events.success(this.deletedText);
158 protected async archive(): Promise<void> {
160 const isArchived = this.archiveButton.dataset.isArchived === 'true';
161 const action = isArchived ? 'unarchive' : 'archive';
163 const response = await window.$http.put(`/comment/${this.commentId}/${action}`);
164 window.$events.success(this.archiveText);
165 const eventData: PageCommentArchiveEventData = {new_thread_dom: htmlToDom(response.data as string)};
166 this.$emit(action, eventData);
168 const branch = this.container.closest('.comment-branch') as HTMLElement;
169 const references = window.$components.allWithinElement<PageCommentReference>(branch, 'page-comment-reference');
170 for (const reference of references) {
171 reference.hideMarker();
176 protected showLoading(): HTMLElement {
177 const loading = getLoading();
178 loading.classList.add('px-l');
179 this.container.append(loading);