1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
3 import {buildForInput} from "../wysiwyg/config";
5 export class PageComment extends Component {
9 this.commentId = this.$opts.commentId;
10 this.commentLocalId = this.$opts.commentLocalId;
11 this.commentParentId = this.$opts.commentParentId;
12 this.deletedText = this.$opts.deletedText;
13 this.updatedText = this.$opts.updatedText;
15 // Editor reference and text options
16 this.wysiwygEditor = null;
17 this.wysiwygLanguage = this.$opts.wysiwygLanguage;
18 this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
21 this.container = this.$el;
22 this.contentContainer = this.$refs.contentContainer;
23 this.form = this.$refs.form;
24 this.formCancel = this.$refs.formCancel;
25 this.editButton = this.$refs.editButton;
26 this.deleteButton = this.$refs.deleteButton;
27 this.replyButton = this.$refs.replyButton;
28 this.input = this.$refs.input;
30 this.setupListeners();
34 if (this.replyButton) {
35 this.replyButton.addEventListener('click', () => this.$emit('reply', {
36 id: this.commentLocalId,
37 element: this.container,
41 if (this.editButton) {
42 this.editButton.addEventListener('click', this.startEdit.bind(this));
43 this.form.addEventListener('submit', this.update.bind(this));
44 this.formCancel.addEventListener('click', () => this.toggleEditMode(false));
47 if (this.deleteButton) {
48 this.deleteButton.addEventListener('click', this.delete.bind(this));
52 toggleEditMode(show) {
53 this.contentContainer.toggleAttribute('hidden', show);
54 this.form.toggleAttribute('hidden', !show);
58 this.toggleEditMode(true);
60 if (this.wysiwygEditor) {
64 const config = buildForInput({
65 language: this.wysiwygLanguage,
66 containerElement: this.input,
67 darkMode: document.documentElement.classList.contains('dark-mode'),
68 textDirection: this.wysiwygTextDirection,
70 translationMap: window.editor_translations,
73 window.tinymce.init(config).then(editors => {
74 this.wysiwygEditor = editors[0];
79 event.preventDefault();
80 const loading = this.showLoading();
81 this.form.toggleAttribute('hidden', true);
84 text: this.input.value,
85 parent_id: this.parentId || null,
89 const resp = await window.$http.put(`/comment/${this.commentId}`, reqData);
90 const newComment = htmlToDom(resp.data);
91 this.container.replaceWith(newComment);
92 window.$events.success(this.updatedText);
95 window.$events.showValidationErrors(err);
96 this.form.toggleAttribute('hidden', false);
104 await window.$http.delete(`/comment/${this.commentId}`);
105 this.container.closest('.comment-branch').remove();
106 window.$events.success(this.deletedText);
107 this.$emit('delete');
111 const loading = getLoading();
112 loading.classList.add('px-l');
113 this.container.append(loading);