]> BookStack Code Mirror - bookstack/blobdiff - resources/js/components/page-comments.js
Turned off autocomplete for TOTP codes
[bookstack] / resources / js / components / page-comments.js
index a46a5c3b3ef8e1de311bde9d2be816738d8f794a..cfb0634a904c3500a58ce6bc017f355d57f3d75a 100644 (file)
@@ -1,5 +1,6 @@
 import {Component} from './component';
 import {getLoading, htmlToDom} from '../services/dom';
+import {buildForInput} from '../wysiwyg/config';
 
 export class PageComments extends Component {
 
@@ -21,22 +22,23 @@ export class PageComments extends Component {
         this.hideFormButton = this.$refs.hideFormButton;
         this.removeReplyToButton = this.$refs.removeReplyToButton;
 
+        // WYSIWYG options
+        this.wysiwygLanguage = this.$opts.wysiwygLanguage;
+        this.wysiwygTextDirection = this.$opts.wysiwygTextDirection;
+        this.wysiwygEditor = null;
+
         // Translations
         this.createdText = this.$opts.createdText;
         this.countText = this.$opts.countText;
 
         // Internal State
         this.parentId = null;
-        this.formReplyText = this.formReplyLink.textContent;
+        this.formReplyText = this.formReplyLink?.textContent || '';
 
         this.setupListeners();
     }
 
     setupListeners() {
-        this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
-        this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
-        this.addCommentButton.addEventListener('click', this.showForm.bind(this));
-
         this.elem.addEventListener('page-comment-delete', () => {
             this.updateCount();
             this.hideForm();
@@ -47,6 +49,9 @@ export class PageComments extends Component {
         });
 
         if (this.form) {
+            this.removeReplyToButton.addEventListener('click', this.removeReplyTo.bind(this));
+            this.hideFormButton.addEventListener('click', this.hideForm.bind(this));
+            this.addCommentButton.addEventListener('click', this.showForm.bind(this));
             this.form.addEventListener('submit', this.saveComment.bind(this));
         }
     }
@@ -60,9 +65,8 @@ export class PageComments extends Component {
         this.form.after(loading);
         this.form.toggleAttribute('hidden', true);
 
-        const text = this.formInput.value;
         const reqData = {
-            text,
+            html: this.wysiwygEditor.getContent(),
             parent_id: this.parentId || null,
         };
 
@@ -87,6 +91,7 @@ export class PageComments extends Component {
     }
 
     resetForm() {
+        this.removeEditor();
         this.formInput.value = '';
         this.parentId = null;
         this.replyToRow.toggleAttribute('hidden', true);
@@ -94,12 +99,11 @@ export class PageComments extends Component {
     }
 
     showForm() {
+        this.removeEditor();
         this.formContainer.toggleAttribute('hidden', false);
         this.addButtonContainer.toggleAttribute('hidden', true);
         this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'});
-        setTimeout(() => {
-            this.formInput.focus();
-        }, 100);
+        this.loadEditor();
     }
 
     hideForm() {
@@ -113,6 +117,34 @@ export class PageComments extends Component {
         this.addButtonContainer.toggleAttribute('hidden', false);
     }
 
+    loadEditor() {
+        if (this.wysiwygEditor) {
+            this.wysiwygEditor.focus();
+            return;
+        }
+
+        const config = buildForInput({
+            language: this.wysiwygLanguage,
+            containerElement: this.formInput,
+            darkMode: document.documentElement.classList.contains('dark-mode'),
+            textDirection: this.wysiwygTextDirection,
+            translations: {},
+            translationMap: window.editor_translations,
+        });
+
+        window.tinymce.init(config).then(editors => {
+            this.wysiwygEditor = editors[0];
+            setTimeout(() => this.wysiwygEditor.focus(), 50);
+        });
+    }
+
+    removeEditor() {
+        if (this.wysiwygEditor) {
+            this.wysiwygEditor.remove();
+            this.wysiwygEditor = null;
+        }
+    }
+
     getCommentCount() {
         return this.container.querySelectorAll('[component="page-comment"]').length;
     }
@@ -123,9 +155,8 @@ export class PageComments extends Component {
         this.showForm();
         this.parentId = commentLocalId;
         this.replyToRow.toggleAttribute('hidden', false);
-        const replyLink = this.replyToRow.querySelector('a');
-        replyLink.textContent = this.formReplyText.replace('1234', this.parentId);
-        replyLink.href = `#comment${this.parentId}`;
+        this.formReplyLink.textContent = this.formReplyText.replace('1234', this.parentId);
+        this.formReplyLink.href = `#comment${this.parentId}`;
     }
 
     removeReplyTo() {