@@ -315,16 +315,16 @@ export class Editor extends EventEmitter {
315315 * If we are replacing data and have a valid provider, listen for synced event
316316 * so that we can initialize the data
317317 */
318- initializeCollaborationData ( replacedFile = false ) {
318+ initializeCollaborationData ( ) {
319319 if ( ! this . options . isNewFile || ! this . options . collaborationProvider ) return ;
320320 const { collaborationProvider : provider } = this . options ;
321321
322322 const postSyncInit = ( ) => {
323323 provider . off ( 'synced' , postSyncInit ) ;
324- this . #insertNewFileData( replacedFile ) ;
324+ this . #insertNewFileData( ) ;
325325 } ;
326326
327- if ( provider . synced ) this . #insertNewFileData( replacedFile ) ;
327+ if ( provider . synced ) this . #insertNewFileData( ) ;
328328 // If we are not sync'd yet, wait for the event then insert the data
329329 else provider . on ( 'synced' , postSyncInit ) ;
330330 }
@@ -333,17 +333,17 @@ export class Editor extends EventEmitter {
333333 * Replace the current document with new data. Necessary for initializing a new collaboration file,
334334 * since we need to insert the data only after the provider has synced.
335335 */
336- #insertNewFileData( replacedFile = false ) {
336+ #insertNewFileData( ) {
337337 if ( ! this . options . isNewFile ) return ;
338338 this . options . isNewFile = false ;
339- const doc = this . #generatePmData( ) ;
339+ const doc = this . #generatePmData( true ) ;
340340 const tr = this . state . tr . replaceWith ( 0 , this . state . doc . content . size , doc ) ;
341341 this . view . dispatch ( tr ) ;
342342
343- if ( this . options . collaborationIsReady ) {
343+ setTimeout ( ( ) => {
344344 this . #initPagination( ) ;
345- this . #initComments( replacedFile ) ;
346- }
345+ this . #initComments( ) ;
346+ } , 50 ) ;
347347 }
348348
349349 #registerPluginByNameIfNotExists( name ) {
@@ -522,7 +522,7 @@ export class Editor extends EventEmitter {
522522 /**
523523 * Generate data from file
524524 */
525- #generatePmData( ) {
525+ #generatePmData( includeComments ) {
526526 let doc ;
527527
528528 try {
@@ -532,7 +532,7 @@ export class Editor extends EventEmitter {
532532 doc = createDocument ( this . converter , this . schema , this ) ;
533533
534534 // Perform any additional document processing prior to finalizing the doc here
535- doc = this . #prepareDocumentForImport( doc ) ;
535+ if ( includeComments ) doc = this . #prepareDocumentForImport( doc ) ;
536536
537537 if ( fragment && isHeadless ) {
538538 doc = yXmlFragmentToProseMirrorRootNode ( fragment , this . schema ) ;
@@ -721,20 +721,25 @@ export class Editor extends EventEmitter {
721721
722722 this . options . onCollaborationReady ( { editor, ydoc } ) ;
723723 this . options . collaborationIsReady = true ;
724- this . #initPagination( ) ;
725- this . #initComments( ) ;
724+
725+ if ( ! this . options . isNewFile ) {
726+ this . #initPagination( ) ;
727+ this . #initComments( ) ;
728+ }
726729 } ;
727730
728731 /**
729732 * Initialize comments plugin
730733 */
731- #initComments( replacedFile = false ) {
734+ #initComments( ) {
732735 if ( ! this . options . isCommentsEnabled ) return ;
733736 if ( this . options . isHeadless ) return ;
734737 if ( ! this . options . shouldLoadComments ) return ;
738+ const replacedFile = this . options . replacedFile ;
735739 this . emit ( 'commentsLoaded' , { editor : this , replacedFile, comments : this . converter . comments || [ ] } ) ;
736740
737741 setTimeout ( ( ) => {
742+ this . options . replacedFile = false ;
738743 const { state, dispatch } = this . view ;
739744 const tr = state . tr . setMeta ( CommentsPluginKey , { type : 'force' } ) ;
740745 dispatch ( tr ) ;
@@ -1084,6 +1089,7 @@ export class Editor extends EventEmitter {
10841089 fonts,
10851090 isNewFile : true ,
10861091 shouldLoadComments : true ,
1092+ replacedFile : true ,
10871093 } ) ;
10881094
10891095 this . #createConverter( ) ;
@@ -1094,7 +1100,7 @@ export class Editor extends EventEmitter {
10941100
10951101 if ( ! this . options . ydoc ) {
10961102 this . #initPagination( ) ;
1097- this . #initComments( true ) ;
1103+ this . #initComments( ) ;
10981104 } ;
10991105
11001106 }
0 commit comments