]> BookStack Code Mirror - bookstack/blob - resources/js/components/page-comment.js
Comments: Added input wysiwyg for creating/updating comments
[bookstack] / resources / js / components / page-comment.js
1 import {Component} from './component';
2 import {getLoading, htmlToDom} from '../services/dom';
3 import {buildForInput} from "../wysiwyg/config";
4
5 export class PageComment extends Component {
6
7     setup() {
8         // Options
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;
14
15         // Editor reference and text options
16         this.wysiwygEditor = null;
17         this.wysiwygLanguage = this.$opts.wysiwygLanguage;
18         this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
19
20         // Element references
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;
29
30         this.setupListeners();
31     }
32
33     setupListeners() {
34         if (this.replyButton) {
35             this.replyButton.addEventListener('click', () => this.$emit('reply', {
36                 id: this.commentLocalId,
37                 element: this.container,
38             }));
39         }
40
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));
45         }
46
47         if (this.deleteButton) {
48             this.deleteButton.addEventListener('click', this.delete.bind(this));
49         }
50     }
51
52     toggleEditMode(show) {
53         this.contentContainer.toggleAttribute('hidden', show);
54         this.form.toggleAttribute('hidden', !show);
55     }
56
57     startEdit() {
58         this.toggleEditMode(true);
59
60         if (this.wysiwygEditor) {
61             return;
62         }
63
64         const config = buildForInput({
65             language: this.wysiwygLanguage,
66             containerElement: this.input,
67             darkMode: document.documentElement.classList.contains('dark-mode'),
68             textDirection: this.wysiwygTextDirection,
69             translations: {},
70             translationMap: window.editor_translations,
71         });
72
73         window.tinymce.init(config).then(editors => {
74             this.wysiwygEditor = editors[0];
75         });
76     }
77
78     async update(event) {
79         event.preventDefault();
80         const loading = this.showLoading();
81         this.form.toggleAttribute('hidden', true);
82
83         const reqData = {
84             text: this.input.value,
85             parent_id: this.parentId || null,
86         };
87
88         try {
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);
93         } catch (err) {
94             console.error(err);
95             window.$events.showValidationErrors(err);
96             this.form.toggleAttribute('hidden', false);
97             loading.remove();
98         }
99     }
100
101     async delete() {
102         this.showLoading();
103
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');
108     }
109
110     showLoading() {
111         const loading = getLoading();
112         loading.classList.add('px-l');
113         this.container.append(loading);
114         return loading;
115     }
116
117 }