From 81ac2225a44291bf245a2472cffd4563edd88e64 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Dec 2019 11:42:01 -0300 Subject: [PATCH 1/7] wysiwyg-editor interface. --- js/jquery-comments.js | 80 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 47c7ed7..ff6e556 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -173,6 +173,24 @@ textareaMaxRows: 5, maxRepliesVisible: 2, + // wysiwyg-editor interface + wysiwyg_editor: { + opts: { + // Whether an external editor is being used. + enable: false, + // Indicates whether comments will be displayed with plain text or rendered as html. + is_html: true + }, + // Initializes a new instance of the html editor. + init: function (textarea, html) {}, + // jquery object within which the editor will be rendered. + get_container: function(textarea) {}, + // Object that returns the html content of the edit (html text). + get_contents: function(editor) {}, + // Call of events. + on_post_comment: function(editor, evt) {}, + on_put_comment: function(editor, evt) {} + }, fieldMappings: { id: 'id', parent: 'parent', @@ -223,7 +241,7 @@ if($.browser.mobile) this.$el.addClass('mobile'); // Init options - this.options = $.extend(true, {}, this.getDefaultOptions(), options);; + this.options = $.extend(true, {}, this.getDefaultOptions(), options); // Read-only mode if(this.options.readOnly) this.$el.addClass('read-only'); @@ -816,6 +834,7 @@ textareaContentChanged: function(ev) { var textarea = $(ev.currentTarget); + var wysiwyg_editor = textarea.data('wysiwyg_editor'); var saveButton = textarea.siblings('.control-row').find('.save'); // Update parent id if reply-to tag was removed @@ -847,7 +866,12 @@ // Check if content or parent has changed if editing var contentOrParentChangedIfEditing = true; - var content = this.getTextareaContent(textarea, true); + var content; + if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { + content = this.options.wysiwyg_editor.get_contents(wysiwyg_editor); + } else { + content = this.getTextareaContent(textarea, true); + } if(commentModel = this.commentsById[textarea.attr('data-comment')]) { var contentChanged = content != commentModel.content; var parentFromModel; @@ -885,6 +909,7 @@ var sendButton = $(ev.currentTarget); var commentingField = sendButton.parents('.commenting-field').first(); var textarea = commentingField.find('.textarea'); + var wysiwyg_editor = textarea.data('wysiwyg_editor'); // Disable send button while request is pending sendButton.removeClass('enabled'); @@ -905,6 +930,10 @@ }; this.options.postComment(commentJSON, success, error); + // Notify event to external editor + if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { + this.options.wysiwyg_editor.on_post_comment(wysiwyg_editor, ev); + } }, createComment: function(commentJSON) { @@ -918,6 +947,7 @@ var saveButton = $(ev.currentTarget); var commentingField = saveButton.parents('.commenting-field').first(); var textarea = commentingField.find('.textarea'); + var wysiwyg_editor = textarea.data('wysiwyg_editor'); // Disable send button while request is pending saveButton.removeClass('enabled'); @@ -956,6 +986,10 @@ }; this.options.putComment(commentJSON, success, error); + // Notify event to external editor + if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { + this.options.wysiwyg_editor.on_put_comment(wysiwyg_editor, ev); + } }, deleteComment: function(ev) { @@ -1066,7 +1100,13 @@ // Move cursor to end var textarea = replyField.find('.textarea'); - this.moveCursorToEnd(textarea) + + // starts new editor instance (before cursor change). + // textarea saves the reference to the editor that will be used later. + if (this.options.wysiwyg_editor.opts.enable) { + textarea.data('wysiwyg_editor', this.options.wysiwyg_editor.init(textarea)); + } + this.moveCursorToEnd(textarea); // Make sure the reply field will be displayed var scrollTop = this.options.scrollContainer.scrollTop(); @@ -1096,6 +1136,14 @@ // Escaping HTML textarea.append(this.getFormattedCommentContent(commentModel, true)); + // starts new editor instance. + // textarea saves the reference to the editor that will be used later. + // In this case the editor starts with the content of the model (raw text). + if (this.options.wysiwyg_editor.opts.enable) { + textarea.data('wysiwyg_editor', this.options.wysiwyg_editor.init( + textarea, commentModel.content + )); + } // Move cursor to end this.moveCursorToEnd(textarea); }, @@ -1173,12 +1221,18 @@ // Commenting field var mainCommentingField = this.createMainCommentingFieldElement(); this.$el.append(mainCommentingField); - // Hide control row and close button var mainControlRow = mainCommentingField.find('.control-row'); mainControlRow.hide(); mainCommentingField.find('.close').hide(); + // Starts a new instance of the editor. + // Connects the textarea with the corresponding editor. + if (this.options.wysiwyg_editor.opts.enable) { + var textarea = mainCommentingField.find('.textarea'); + textarea.data('wysiwyg_editor', this.options.wysiwyg_editor.init(textarea)); + } + // Navigation bar if (this.options.enableNavigation) { this.$el.append(this.createNavigationElement()); @@ -1395,9 +1449,14 @@ // Populate the element controlRow.prepend(saveButton); textareaWrapper.append(closeButton).append(textarea).append(controlRow); + // controle over external html editor + if (this.options.wysiwyg_editor.opts.enable) { + // Who assumes here is the external wysiwyg editor but the default api should keep working. + textarea.hide(); + textareaWrapper.append(this.options.wysiwyg_editor.get_container(textarea)).append(controlRow); + } commentingField.append(profilePicture).append(textareaWrapper); - if(parentId) { // Set the parent id to the field if necessary @@ -1818,7 +1877,11 @@ // Case: regular comment } else { - content.html(this.getFormattedCommentContent(commentModel)); + if (this.options.wysiwyg_editor.opts.is_html) { + content.html(commentModel.content); + } else { + content.html(this.getFormattedCommentContent(commentModel)); + } } // Edited timestamp @@ -2125,6 +2188,11 @@ }, getTextareaContent: function(textarea, humanReadable) { + var wysiwyg_editor = textarea.data('wysiwyg_editor'); + if (wysiwyg_editor && this.options.wysiwyg_editor) { + return this.options.wysiwyg_editor.get_contents(wysiwyg_editor); + } + var parentId = textarea.data('parent'); var textareaClone = textarea.clone(); // Remove reply-to tag From 36c7f5a08a24e149e0ced11ab4f0ac44e8d536e0 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Dec 2019 12:00:13 -0300 Subject: [PATCH 2/7] Fix: check editor is enable. --- js/jquery-comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index ff6e556..8f9991e 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -2189,7 +2189,7 @@ getTextareaContent: function(textarea, humanReadable) { var wysiwyg_editor = textarea.data('wysiwyg_editor'); - if (wysiwyg_editor && this.options.wysiwyg_editor) { + if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { return this.options.wysiwyg_editor.get_contents(wysiwyg_editor); } var parentId = textarea.data('parent'); From 6d4187b153bd041c5633a3e5c49f12ecae51681f Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Dec 2019 12:15:27 -0300 Subject: [PATCH 3/7] Interface callback to close button. --- js/jquery-comments.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 8f9991e..9991cb3 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -189,7 +189,8 @@ get_contents: function(editor) {}, // Call of events. on_post_comment: function(editor, evt) {}, - on_put_comment: function(editor, evt) {} + on_put_comment: function(editor, evt) {}, + on_close_button: function (editor, evt) {} }, fieldMappings: { id: 'id', @@ -817,7 +818,11 @@ var closeButton = $(ev.currentTarget); var mainTextarea = this.$el.find('.commenting-field.main .textarea'); var mainControlRow = this.$el.find('.commenting-field.main .control-row'); + var wysiwyg_editor = mainTextarea.data('wysiwyg_editor'); + if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { + this.options.wysiwyg_editor.on_close_button(wysiwyg_editor, ev); + } this.clearTextarea(mainTextarea); this.adjustTextareaHeight(mainTextarea, false); From 6084335c0938b1038917a8e50def0afa6769fa81 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Dec 2019 13:47:17 -0300 Subject: [PATCH 4/7] Adding the state of events in editor calls. --- js/jquery-comments.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 9991cb3..8dc9c6c 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -188,8 +188,8 @@ // Object that returns the html content of the edit (html text). get_contents: function(editor) {}, // Call of events. - on_post_comment: function(editor, evt) {}, - on_put_comment: function(editor, evt) {}, + on_post_comment: function(editor, evt, status) {}, + on_put_comment: function(editor, evt, status) {}, on_close_button: function (editor, evt) {} }, fieldMappings: { @@ -925,20 +925,27 @@ // Reverse mapping commentJSON = this.applyExternalMappings(commentJSON); + // Notify event to external editor + var editor_event_post_comment = function (status) { + if (wysiwyg_editor && self.options.wysiwyg_editor.opts.enable) { + self.options.wysiwyg_editor.on_post_comment(wysiwyg_editor, ev, status); + } + }; + var success = function(commentJSON) { + editor_event_post_comment('success'); self.createComment(commentJSON); commentingField.find('.close').trigger('click'); }; var error = function() { + editor_event_post_comment('error'); sendButton.addClass('enabled'); }; + this.options.postComment(commentJSON, success, error); - // Notify event to external editor - if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { - this.options.wysiwyg_editor.on_post_comment(wysiwyg_editor, ev); - } + editor_event_post_comment(); }, createComment: function(commentJSON) { @@ -969,7 +976,15 @@ // Reverse mapping commentJSON = this.applyExternalMappings(commentJSON); + // Notify event to external editor + var editor_event_put_comment = function (status) { + if (wysiwyg_editor && self.options.wysiwyg_editor.opts.enable) { + self.options.wysiwyg_editor.on_put_comment(wysiwyg_editor, ev, status); + } + }; var success = function(commentJSON) { + editor_event_put_comment('success'); + // The outermost parent can not be changed by editing the comment so the childs array // of parent does not require an update @@ -987,14 +1002,13 @@ }; var error = function() { + editor_event_put_comment('error'); saveButton.addClass('enabled'); }; this.options.putComment(commentJSON, success, error); // Notify event to external editor - if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { - this.options.wysiwyg_editor.on_put_comment(wysiwyg_editor, ev); - } + editor_event_put_comment(); }, deleteComment: function(ev) { From f4d076acfd76fa51d7796adea9b4cca9c28c860a Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 26 Jan 2021 18:30:20 -0300 Subject: [PATCH 5/7] merge fix. --- js/jquery-comments.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 94726d3..1ca2e60 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -874,7 +874,6 @@ textareaContentChanged: function(ev) { var textarea = $(ev.currentTarget); - var wysiwyg_editor = textarea.data('wysiwyg_editor'); // Update parent id if reply-to tag was removed if(!textarea.find('.reply-to.tag').length) { @@ -910,6 +909,7 @@ toggleSaveButton: function(commentingField) { var textarea = commentingField.find('.textarea'); var saveButton = textarea.siblings('.control-row').find('.save'); + var wysiwyg_editor = textarea.data('wysiwyg_editor'); var content; if (wysiwyg_editor && this.options.wysiwyg_editor.opts.enable) { content = this.options.wysiwyg_editor.get_contents(wysiwyg_editor); @@ -1931,8 +1931,11 @@ var content = $('
', { 'class': 'content' }); - content.html(this.getFormattedCommentContent(commentModel)); - + if (this.options.wysiwyg_editor.opts.is_html) { + content.html(commentModel.content); + } else { + content.html(this.getFormattedCommentContent(commentModel)); + } // Edited timestamp if(commentModel.modified && commentModel.modified != commentModel.created) { var editedTime = this.options.timeFormatter(commentModel.modified); From 0b25637b6d8ec84075a7152163b8d5b4f90163ce Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 24 Feb 2021 13:19:42 -0300 Subject: [PATCH 6/7] Fix textarea missing ref. --- js/jquery-comments.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 1ca2e60..05b4ca3 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -968,6 +968,7 @@ var self = this; var sendButton = $(ev.currentTarget); var commentingField = sendButton.parents('.commenting-field').first(); + var textarea = commentingField.find('.textarea'); var wysiwyg_editor = textarea.data('wysiwyg_editor'); // Set button state to loading From 376af40d05bd0a7fe1202f57a29572593bda541b Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 24 Feb 2021 17:31:19 -0300 Subject: [PATCH 7/7] Set button type. --- js/jquery-comments.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/jquery-comments.js b/js/jquery-comments.js index 05b4ca3..da2e663 100644 --- a/js/jquery-comments.js +++ b/js/jquery-comments.js @@ -2054,7 +2054,8 @@ if(commentModel.createdByCurrentUser || this.options.currentUserIsAdmin) { var editButton = $('