Skip to content

Commit eeee735

Browse files
committed
fix: only initialize editor once
When we recover a session the editor already exists. Start the syncing again as the polling backend is created from scratch. Do not attach further events to the editor and do not reset the content. Signed-off-by: Max <[email protected]>
1 parent 7576cca commit eeee735

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

cypress/e2e/sync.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ describe('Sync', () => {
8080
req.alias = 'deadSync'
8181
}
8282
})
83-
cy.wait('@push', { timeout: 15000 + Cypress.config().defaultCommandTimeout })
84-
cy.wait('@push', { timeout: 15000 + Cypress.config().defaultCommandTimeout })
85-
cy.wait('@push', { timeout: 15000 + Cypress.config().defaultCommandTimeout })
86-
cy.wait('@push', { timeout: 15000 + Cypress.config().defaultCommandTimeout })
83+
cy.get('#editor-container .document-status')
84+
.should('contain', 'File could not be loaded')
85+
cy.get('#editor-container .document-status', { timeout: 30000 })
86+
.should('not.contain', 'File could not be loaded')
8787
cy.getContent().type('* more content added after the lost connection{enter}')
8888
cy.closeFile()
8989
cy.testName()

src/components/Editor.vue

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ export default {
476476
this.currentSession = session
477477
this.document = document
478478
this.readOnly = document.readOnly
479+
if (this.$editor) {
480+
this.$editor.setOptions({ editable: !this.readOnly })
481+
}
479482
this.lock = this.$syncService.lock
480483
localStorage.setItem('nick', this.currentSession.guestName)
481484
this.setCurrentSession(this.currentSession)
@@ -498,47 +501,52 @@ export default {
498501
(this.isRichEditor ? Promise.resolve() : loadSyntaxHighlight(language))
499502
.then(() => {
500503
const session = this.currentSession
501-
this.$editor ||= createEditor({
502-
language,
503-
relativePath: this.relativePath,
504-
session,
505-
onCreate: ({ editor }) => {
506-
this.$syncService.startSync()
507-
},
508-
onUpdate: ({ editor }) => {
509-
// this.debugContent(editor)
510-
const proseMirrorMarkdown = this.$syncService.serialize(editor.state.doc)
511-
this.$parent.$emit('update:content', {
512-
markdown: proseMirrorMarkdown,
513-
})
514-
},
515-
extensions: [
516-
Collaboration.configure({
517-
document: this.$ydoc,
518-
}),
519-
CollaborationCursor.configure({
520-
provider: this.$providers[0],
521-
user: {
522-
name: session?.userId
523-
? session.displayName
524-
: (session?.guestName || t('text', 'Guest')),
525-
color: session?.color,
526-
},
527-
}),
528-
Keymap.configure({
529-
'Mod-s': () => {
530-
this.$syncService.save()
531-
return true
532-
},
533-
}),
534-
],
535-
enableRichEditing: this.isRichEditor,
536-
})
537-
this.hasEditor = true
538-
if (!documentState && documentSource) {
539-
this.setContent(documentSource, { addToHistory: false })
504+
if (!this.$editor) {
505+
this.$editor = createEditor({
506+
language,
507+
relativePath: this.relativePath,
508+
session,
509+
onCreate: ({ editor }) => {
510+
this.$syncService.startSync()
511+
},
512+
onUpdate: ({ editor }) => {
513+
// this.debugContent(editor)
514+
const proseMirrorMarkdown = this.$syncService.serialize(editor.state.doc)
515+
this.$parent.$emit('update:content', {
516+
markdown: proseMirrorMarkdown,
517+
})
518+
},
519+
extensions: [
520+
Collaboration.configure({
521+
document: this.$ydoc,
522+
}),
523+
CollaborationCursor.configure({
524+
provider: this.$providers[0],
525+
user: {
526+
name: session?.userId
527+
? session.displayName
528+
: (session?.guestName || t('text', 'Guest')),
529+
color: session?.color,
530+
},
531+
}),
532+
Keymap.configure({
533+
'Mod-s': () => {
534+
this.$syncService.save()
535+
return true
536+
},
537+
}),
538+
],
539+
enableRichEditing: this.isRichEditor,
540+
})
541+
this.hasEditor = true
542+
if (!documentState && documentSource) {
543+
this.setContent(documentSource, { addToHistory: false })
544+
}
545+
this.listenEditorEvents()
546+
} else {
547+
// $editor already existed. So this is a reconnect.
548+
this.$syncService.startSync()
540549
}
541-
this.listenEditorEvents()
542550
543551
})
544552

0 commit comments

Comments
 (0)