@@ -43,48 +43,45 @@ export const CommentsPlugin = Extension.create({
4343 )
4444
4545 dispatch ( tr )
46- return true
46+ return true ;
4747 } ,
4848
4949 removeComment :
5050 ( { commentId, importedId } ) =>
5151 ( { tr, dispatch, state } ) => {
52- tr . setMeta ( CommentsPluginKey , { event : 'deleted' } )
53- removeCommentsById ( { commentId, importedId, state, tr, dispatch } )
52+ tr . setMeta ( CommentsPluginKey , { event : 'deleted' } ) ;
53+ removeCommentsById ( { commentId, importedId, state, tr, dispatch } ) ;
5454 } ,
5555
5656 setActiveComment :
57- ( { commentId, importedId } ) =>
57+ ( { commentId } ) =>
5858 ( { tr, dispatch } ) => {
59- let activeThreadId = importedId
60- if ( importedId === undefined || importedId === null ) {
61- activeThreadId = commentId
62- }
63- tr . setMeta ( CommentsPluginKey , { type : 'setActiveComment' , activeThreadId } )
64- return true
59+ let activeThreadId = commentId ;
60+ tr . setMeta ( CommentsPluginKey , { type : 'setActiveComment' , activeThreadId } ) ;
61+ return true ;
6562 } ,
6663
6764 setCommentInternal :
68- ( { commentId, importedId , isInternal } ) =>
65+ ( { commentId, isInternal } ) =>
6966 ( { tr, dispatch, state } ) => {
70- const { doc } = state
71- let foundStartNode
72- let foundPos
67+ const { doc } = state ;
68+ let foundStartNode ;
69+ let foundPos ;
7370
7471 // Find the commentRangeStart node that matches the comment ID
7572 tr . setMeta ( CommentsPluginKey , { event : 'update' } )
7673 doc . descendants ( ( node , pos ) => {
77- if ( foundStartNode ) return
74+ if ( foundStartNode ) return ;
7875
79- const { marks = [ ] } = node
80- const commentMark = marks . find ( ( mark ) => mark . type . name === CommentMarkName )
76+ const { marks = [ ] } = node ;
77+ const commentMark = marks . find ( ( mark ) => mark . type . name === CommentMarkName ) ;
8178
8279 if ( commentMark ) {
83- const { attrs } = commentMark
84- const wid = attrs . commentId || attrs . importedId
85- if ( wid == commentId || wid == importedId ) {
86- foundStartNode = node
87- foundPos = pos
80+ const { attrs } = commentMark ;
81+ const wid = attrs . commentId ;
82+ if ( wid === commentId ) {
83+ foundStartNode = node ;
84+ foundPos = pos ;
8885 }
8986 }
9087 } )
@@ -108,17 +105,18 @@ export const CommentsPlugin = Extension.create({
108105 } ,
109106
110107 resolveComment :
111- ( { commentId, importedId } ) =>
108+ ( { commentId } ) =>
112109 ( { tr, dispatch, state } ) => {
113110 tr . setMeta ( CommentsPluginKey , { event : 'update' } )
114- removeCommentsById ( { commentId, importedId , state, tr, dispatch } )
111+ removeCommentsById ( { commentId, state, tr, dispatch } )
115112 } ,
116113 }
117114 } ,
118115
119116 addPmPlugins ( ) {
120117 const editor = this . editor
121118 let shouldUpdate ;
119+ let activeThreadId ;
122120
123121 const commentsPlugin = new Plugin ( {
124122 key : CommentsPluginKey ,
@@ -164,26 +162,24 @@ export const CommentsPlugin = Extension.create({
164162 ) ;
165163 } ;
166164
165+ // Check for changes in the actively selected comment
167166 const trChangedActiveComment = meta ?. type === 'setActiveComment' ;
168167 if ( ( ! tr . docChanged && tr . selectionSet ) || trChangedActiveComment ) {
169-
170168 const { selection } = tr ;
171- let activeThreadId = getActiveCommentId ( newEditorState . doc , selection ) ;
169+ const currentActiveThread = getActiveCommentId ( newEditorState . doc , selection ) ;
172170 if ( trChangedActiveComment ) activeThreadId = meta . activeThreadId ;
173171
174- const previousSelectionId = pluginState . activeThreadId ;
175- if ( previousSelectionId !== activeThreadId || trChangedActiveComment ) {
176- pluginState . activeThreadId = activeThreadId ;
172+ const previousSelectionId = activeThreadId ;
173+ if ( previousSelectionId !== currentActiveThread ) {
174+ activeThreadId = currentActiveThread ;
177175 const update = {
178176 type : comments_module_events . SELECTED ,
179177 activeCommentId : activeThreadId ? activeThreadId : null
180178 } ;
181179
180+ shouldUpdate = true ;
182181 editor . emit ( 'commentsUpdate' , update ) ;
183- pluginState . changedActiveThread = true ;
184- } else {
185- pluginState . changedActiveThread = false ;
186- }
182+ } ;
187183 } ;
188184
189185 const { allCommentIds, allCommentPositions } = pluginState ;
@@ -207,11 +203,8 @@ export const CommentsPlugin = Extension.create({
207203 update ( view , prevState ) {
208204 const { state } = view
209205 const { doc, tr } = state
210-
211- const pluginState = CommentsPluginKey . getState ( state )
212- const { activeThreadId} = pluginState ;
213206
214- if ( prevDoc && prevDoc . eq ( doc ) || ! shouldUpdate ) return ;
207+ if ( prevDoc && prevDoc . eq ( doc ) && ! shouldUpdate ) return ;
215208 prevDoc = doc ;
216209
217210 const decorations = [ ]
@@ -237,6 +230,7 @@ export const CommentsPlugin = Extension.create({
237230 } ) ;
238231
239232 const isInternal = attrs . internal ;
233+
240234 const color = getHighlightColor ( { activeThreadId, threadId, isInternal, editor } ) ;
241235 const deco = Decoration . inline ( pos , pos + node . nodeSize , {
242236 style : `background-color: ${ color } ` ,
@@ -263,6 +257,7 @@ export const CommentsPlugin = Extension.create({
263257 const decorationSet = DecorationSet . create ( doc , decorations )
264258
265259 // Compare new decorations with the old state to avoid infinite loop
260+ const pluginState = CommentsPluginKey . getState ( state )
266261 const oldDecorations = pluginState . decorations
267262
268263 // We only dispatch if something actually changed
@@ -280,6 +275,7 @@ export const CommentsPlugin = Extension.create({
280275
281276 // Remember the new decorations for next time
282277 prevDecorations = decorationSet
278+ shouldUpdate = false ;
283279 } ,
284280 }
285281 } ,
0 commit comments