@@ -166,20 +166,46 @@ EpComments.prototype.init = async function () {
166166 // Here, it adds a form to edit the comment text
167167 this . container . parent ( ) . on ( 'click' , '.comment-edit' , function ( ) {
168168 const $commentBox = $ ( this ) . closest ( '.comment-container' ) ;
169- $commentBox . addClass ( 'editing' ) ;
170169
170+ const changeAcceptedState = $commentBox . hasClass ( 'change-accepted' ) ;
171+ $commentBox . addClass ( 'editing' ) ;
172+ const changeTo = $commentBox . find ( '.to-value' ) . first ( ) . text ( ) ;
173+ const changeFrom = $commentBox . find ( '.from-value' ) . first ( ) . text ( ) ;
171174 const textBox = self . findCommentText ( $commentBox ) . last ( ) ;
175+ const commentId = $commentBox . data ( 'commentid' ) ;
172176
173177 // if edit form not already there
174178 if ( textBox . siblings ( '.comment-edit-form' ) . length === 0 ) {
175179 // add a form to edit the field
176180 const data = { } ;
181+ data . changeAcceptedState = changeAcceptedState ;
182+ data . commentId = commentId ;
177183 data . text = textBox . text ( ) ;
184+ data . changeFrom = changeFrom ;
185+ data . changeTo = changeTo ;
178186 const content = $ ( '#editCommentTemplate' ) . tmpl ( data ) ;
179187 // localize the comment/reply edit form
180188 commentL10n . localize ( content ) ;
181189 // insert form
182190 textBox . before ( content ) ;
191+ const editForm = textBox . parent ( ) . find ( '.comment-edit-form' ) . first ( ) ;
192+ if ( changeTo ) {
193+ editForm . find ( '.suggestion' ) . show ( ) ;
194+ editForm . find ( '.label-suggestion-checkbox' )
195+ . siblings ( 'input[type="checkbox"]' )
196+ . prop ( 'checked' , true ) ;
197+ }
198+ editForm . on ( 'change' , '.suggestion-checkbox' , function ( ) {
199+ if ( $ ( this ) . is ( ':checked' ) ) {
200+ editForm . find ( '.suggestion' ) . show ( ) ;
201+ } else {
202+ editForm . find ( '.suggestion' ) . hide ( ) ;
203+ }
204+ } ) ;
205+
206+ editForm . find ( '.label-suggestion-checkbox' ) . click ( function ( ) {
207+ $ ( this ) . siblings ( 'input[type="checkbox"]' ) . click ( ) ;
208+ } ) ;
183209 }
184210 } ) ;
185211
@@ -191,14 +217,25 @@ EpComments.prototype.init = async function () {
191217 const $commentForm = $ ( this ) . closest ( '.comment-edit-form' ) ;
192218 const commentId = $commentBox . data ( 'commentid' ) ;
193219 const commentText = $commentForm . find ( '.comment-edit-text' ) . val ( ) ;
220+ const changeFrom = $commentForm . find ( '.from-value' ) . first ( ) . text ( ) ;
221+ const changeTo = $commentForm . find ( '.to-value' ) . val ( ) ;
222+
194223 const data = { } ;
195224 data . commentId = commentId ;
196225 data . padId = clientVars . padId ;
197226 data . commentText = commentText ;
198227 data . authorId = clientVars . userId ;
228+ data . changeFrom = null ;
229+ data . changeTo = null ;
230+ if ( $commentForm . find ( '.label-suggestion-checkbox' )
231+ . siblings ( 'input[type="checkbox"]' )
232+ . is ( ':checked' ) ) {
233+ data . changeFrom = changeFrom ;
234+ data . changeTo = changeTo ;
235+ }
199236
200237 try {
201- await self . _send ( 'updateCommentText ' , data ) ;
238+ await self . _send ( 'updateComment ' , data ) ;
202239 } catch ( err ) {
203240 if ( err . message !== 'unauth' ) throw err ; // Let the uncaught error handler handle it.
204241 $ . gritter . add ( {
@@ -212,7 +249,7 @@ EpComments.prototype.init = async function () {
212249 $commentForm . remove ( ) ;
213250 $commentBox . removeClass ( 'editing' ) ;
214251 self . updateCommentBoxText ( commentId , commentText ) ;
215-
252+ self . updateCommentBoxChangeTo ( commentId , data ) ;
216253 // although the comment or reply was saved on the data base successfully, it needs
217254 // to update the comment or comment reply variable with the new text saved
218255 self . setCommentOrReplyNewText ( commentId , commentText ) ;
@@ -381,6 +418,7 @@ EpComments.prototype.findCommentText = function ($commentBox) {
381418 return $commentBox . find ( '.compact-display-content .comment-text, ' +
382419 '.full-display-content .comment-title-wrapper .comment-text' ) ;
383420} ;
421+
384422// This function is useful to collect new comments on the collaborators
385423EpComments . prototype . collectCommentsAfterSomeIntervalsOfTime = async function ( ) {
386424 await new Promise ( ( resolve ) => window . setTimeout ( resolve , 300 ) ) ;
@@ -1165,6 +1203,33 @@ EpComments.prototype.updateCommentBoxText = function (commentId, commentText) {
11651203 textBox . text ( commentText ) ;
11661204} ;
11671205
1206+ EpComments . prototype . updateCommentBoxChangeTo = function ( commentId , data ) {
1207+ const $comment = this . container . parent ( ) . find ( `[data-commentid='${ commentId } ']` ) ;
1208+ const $suggest = $ ( '#display-suggestion' ) . tmpl ( data ) ;
1209+ if ( data . changeTo ) {
1210+ $ ( $suggest [ 2 ] ) . find ( '.from-label' ) . each ( ( key , item ) => {
1211+ if ( item . dataset ) {
1212+ item . dataset . l10nArgs = JSON . stringify ( {
1213+ changeFrom : data . changeFrom ,
1214+ changeTo : data . changeTo ,
1215+ } ) ;
1216+ }
1217+ } ) ;
1218+
1219+ commentL10n . localize ( $ ( $suggest [ 2 ] ) ) ;
1220+ if ( ! $comment . find ( '.comment-changeTo-form' ) . length ) {
1221+ const textBox = this . findCommentText ( $comment ) ;
1222+ textBox . after ( $suggest ) ;
1223+ }
1224+ $comment . find ( '.comment-changeTo-form' ) . replaceWith ( $suggest ) ;
1225+
1226+
1227+ this . container . parent ( ) . find ( `[data-commentid='${ commentId } ']` ) . replaceWith ( $comment ) ;
1228+ } else {
1229+ $comment . find ( '.comment-changeTo-form' ) . remove ( ) ;
1230+ }
1231+ } ;
1232+
11681233EpComments . prototype . showChangeAsAccepted = function ( commentId ) {
11691234 const self = this ;
11701235
@@ -1197,6 +1262,12 @@ EpComments.prototype.pushComment = function (eventType, callback) {
11971262 this . updateCommentBoxText ( commentId , commentText ) ;
11981263 } ) ;
11991264
1265+ socket . on ( 'commentUpdated' , ( commentId , commentText , changeFrom , changeTo ) => {
1266+ console . log ( 'commentUpdated' , changeFrom , changeTo ) ;
1267+ this . updateCommentBoxText ( commentId , commentText ) ;
1268+ this . updateCommentBoxChangeTo ( commentId , { commentId, commentText, changeFrom, changeTo} ) ;
1269+ } ) ;
1270+
12001271 socket . on ( 'commentDeleted' , ( commentId ) => {
12011272 this . deleteComment ( commentId ) ;
12021273 } ) ;
0 commit comments