diff --git a/README.md b/README.md index 7335be1..c791abd 100644 --- a/README.md +++ b/README.md @@ -192,12 +192,7 @@ let quillOptions = { let editor = new Editor("#container", editorOptions, quillOptions); let websocketEndpoint = "ws://localhost:8080"; - -let socket = new ReconnectingWebSocket(websocketEndpoint); -let connection = new ShareDB.Connection(socket); -let doc = connection.get("examples", "test-doc"); - -editor.syncDocument(doc); +editor.syncThroughWebsocket(websocketEndpoint, "examples", "test-doc"); ``` diff --git a/authorship.js b/authorship.js index 126cc7d..d599fe6 100644 --- a/authorship.js +++ b/authorship.js @@ -308,6 +308,10 @@ class AuthorSidebar { current = current.next; } + if(maxLengthAuthor === 0) { + return; + } + // Update author's name inside sidebar item let lineAuthorId = maxLengthAuthor; let self = this; diff --git a/demo/index.js b/demo/index.js index 0a8ca4d..8d2321c 100644 --- a/demo/index.js +++ b/demo/index.js @@ -1,7 +1,5 @@ import Editor from "../editor"; import 'quill/dist/quill.snow.css' -import ReconnectingWebSocket from "reconnecting-websocket"; -import ShareDB from "sharedb/lib/client"; import EditorEvents from "../editor-events"; let authors = [ @@ -110,8 +108,4 @@ editor.on(EditorEvents.imageSkipped, ()=>{ let websocketEndpoint = "ws://127.0.0.1:8080"; -let socket = new ReconnectingWebSocket(websocketEndpoint); -let connection = new ShareDB.Connection(socket); -let doc = connection.get("examples", "test-doc"); - -editor.syncDocument(doc); +editor.syncThroughWebsocket(websocketEndpoint, "examples", "test-doc"); diff --git a/editor.js b/editor.js index e14580b..4b4ab81 100644 --- a/editor.js +++ b/editor.js @@ -16,7 +16,6 @@ import Synchronizer from "./synchronizer"; shareDB.types.register(richText.type); Quill.register('modules/imageDropAndPaste', QuillImageDropAndPaste); -Quill.register("modules/authorship", Authorship); Quill.register(ImagePlaceholder); // For icons of header value 3 @@ -63,8 +62,8 @@ class Editor { }); } - syncDocument(shareDBDocument) { - this.synchronizer.syncShareDBDocument(shareDBDocument); + syncThroughWebsocket(endpoint, collection, docId) { + return this.synchronizer.syncThroughWebsocket(endpoint, collection, docId); } on(event, handler) { @@ -94,6 +93,11 @@ class Editor { }); } } + + close() { + this.eventHandlers = {}; + this.synchronizer.close(); + } } export default Editor; diff --git a/package-lock.json b/package-lock.json index 1c61e9d..49373b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5931,8 +5931,8 @@ }, "reconnecting-websocket": { "version": "4.4.0", - "resolved": "https://registry.npmjs.org/reconnecting-websocket/-/reconnecting-websocket-4.4.0.tgz", - "integrity": "sha512-D2E33ceRPga0NvTDhJmphEgJ7FUYF0v4lr1ki0csq06OdlxKfugGzN0dSkxM/NfqCxYELK4KcaTOUOjTV6Dcng==" + "resolved": "https://registry.npm.taobao.org/reconnecting-websocket/download/reconnecting-websocket-4.4.0.tgz?cache=0&sync_timestamp=1581058644735&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Freconnecting-websocket%2Fdownload%2Freconnecting-websocket-4.4.0.tgz", + "integrity": "sha1-Ow5blu8RnnigMTWGW4uwrxuUh4M=" }, "regenerate": { "version": "1.4.0", diff --git a/synchronizer.js b/synchronizer.js index 04898cf..d30d5b6 100644 --- a/synchronizer.js +++ b/synchronizer.js @@ -1,4 +1,6 @@ import EditorEvents from "./editor-events"; +import ReconnectingWebSocket from "reconnecting-websocket"; +import ShareDB from "sharedb/lib/client"; class Synchronizer { constructor (editor, composition) { @@ -7,16 +9,44 @@ class Synchronizer { this.doc = null; this.debug = false; this.composition = composition; + this.heartbeat = null; + + this.socket = null; } submitDeltaToUpstream(delta) { this.doc.submitOp(delta, {source: 'user'}); } - syncShareDBDocument(shareDBDocument) { + syncThroughWebsocket(endpoint, collection, docId) { this.close(); + this.socket = new ReconnectingWebSocket(endpoint); + + let connection = new ShareDB.Connection(this.socket); + + this.syncShareDBDocument(connection.get(collection, docId)); + + // Send heartbeat message to keep websocket connection alive + + let self = this; + + this.socket.addEventListener("open", () => { + self.heartbeat = setInterval(() => { + self.socket.send('{"a":"hs"}'); + }, 5000); + }); + + this.socket.addEventListener("close", () => { + clearInterval(self.heartbeat); + }); + + return this.socket; + } + + syncShareDBDocument(shareDBDocument) { + this.doc = shareDBDocument; let self = this; @@ -54,6 +84,11 @@ class Synchronizer { this.doc.destroy(); this.doc = null; } + + if(this.socket) { + this.socket.close(); + this.socket = null; + } } log(msg){