X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/154924cc0c19546599bbe1953744f9ab1162b263..refs/pull/5280/head:/resources/js/components/page-comments.js diff --git a/resources/js/components/page-comments.js b/resources/js/components/page-comments.js index 9dc529963..1d6abfe20 100644 --- a/resources/js/components/page-comments.js +++ b/resources/js/components/page-comments.js @@ -1,5 +1,6 @@ import {Component} from './component'; import {getLoading, htmlToDom} from '../services/dom'; +import {buildForInput} from '../wysiwyg-tinymce/config'; export class PageComments extends Component { @@ -16,35 +17,41 @@ export class PageComments extends Component { this.formContainer = this.$refs.formContainer; this.form = this.$refs.form; this.formInput = this.$refs.formInput; + this.formReplyLink = this.$refs.formReplyLink; this.addCommentButton = this.$refs.addCommentButton; 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.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(); + setTimeout(() => this.updateCount(), 1); this.hideForm(); }); this.elem.addEventListener('page-comment-reply', event => { - this.setReply(event.detail.id); + this.setReply(event.detail.id, event.detail.element); }); 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)); } } @@ -58,69 +65,112 @@ 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, }; window.$http.post(`/comment/${this.pageId}`, reqData).then(resp => { const newElem = htmlToDom(resp.data); - this.container.appendChild(newElem); + + if (reqData.parent_id) { + this.formContainer.after(newElem); + } else { + this.container.append(newElem); + } + window.$events.success(this.createdText); - this.resetForm(); + this.hideForm(); this.updateCount(); }).catch(err => { this.form.toggleAttribute('hidden', false); window.$events.showValidationErrors(err); }); + this.form.toggleAttribute('hidden', false); loading.remove(); } updateCount() { const count = this.getCommentCount(); - this.commentsTitle.textContent = window.trans_plural(this.countText, count, {count}); + console.log('update count', count, this.container); + this.commentsTitle.textContent = window.$trans.choice(this.countText, count, {count}); } resetForm() { + this.removeEditor(); this.formInput.value = ''; - this.hideForm(); - this.removeReplyTo(); + this.parentId = null; + this.replyToRow.toggleAttribute('hidden', true); + this.container.append(this.formContainer); } showForm() { + this.removeEditor(); this.formContainer.toggleAttribute('hidden', false); this.addButtonContainer.toggleAttribute('hidden', true); - this.formInput.focus(); + this.formContainer.scrollIntoView({behavior: 'smooth', block: 'nearest'}); + this.loadEditor(); } hideForm() { + this.resetForm(); this.formContainer.toggleAttribute('hidden', true); if (this.getCommentCount() > 0) { - this.elem.appendChild(this.addButtonContainer); + this.elem.append(this.addButtonContainer); } else { - this.commentCountBar.appendChild(this.addButtonContainer); + this.commentCountBar.append(this.addButtonContainer); } 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('[compontent="page-comment"]').length; + return this.container.querySelectorAll('[component="page-comment"]').length; } - setReply(commentLocalId) { + setReply(commentLocalId, commentElement) { + const targetFormLocation = commentElement.closest('.comment-branch').querySelector('.comment-branch-children'); + targetFormLocation.append(this.formContainer); this.showForm(); this.parentId = commentLocalId; this.replyToRow.toggleAttribute('hidden', false); - const replyLink = this.replyToRow.querySelector('a'); - replyLink.textContent = `#${this.parentId}`; - replyLink.href = `#comment${this.parentId}`; + this.formReplyLink.textContent = this.formReplyText.replace('1234', this.parentId); + this.formReplyLink.href = `#comment${this.parentId}`; } removeReplyTo() { this.parentId = null; this.replyToRow.toggleAttribute('hidden', true); + this.container.append(this.formContainer); + this.showForm(); } }