From bf21ebd719c942a4a766360d343f0e787d8d36d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sun, 3 Feb 2019 21:18:03 +0100 Subject: [PATCH] improve collab demo --- .../Components/Routes/Collaboration/Collab.js | 14 ++++- .../Components/Routes/Collaboration/index.vue | 52 +++++++++++++------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/examples/Components/Routes/Collaboration/Collab.js b/examples/Components/Routes/Collaboration/Collab.js index 7607d18a..21dc7255 100644 --- a/examples/Components/Routes/Collaboration/Collab.js +++ b/examples/Components/Routes/Collaboration/Collab.js @@ -6,7 +6,19 @@ export default class CollabExtension extends Extension { return 'collab' } + get defaultOptions() { + return { + version: 0, + clientID: Math.floor(Math.random() * 0xFFFFFFFF), + } + } + get plugins() { - return [collab()] + return [ + collab({ + version: this.options.version, + clientID: this.options.clientID, + }), + ] } } diff --git a/examples/Components/Routes/Collaboration/index.vue b/examples/Components/Routes/Collaboration/index.vue index bd89ace0..0d7620eb 100644 --- a/examples/Components/Routes/Collaboration/index.vue +++ b/examples/Components/Routes/Collaboration/index.vue @@ -1,6 +1,6 @@ @@ -17,16 +17,29 @@ export default { data() { return { + editor: null, ws: null, - - authority: { + clientID: Math.floor(Math.random() * 0xFFFFFFFF), + collabStartVersion: 0, + collabHistory: { steps: [], - stepClientIDs: [], + clientIDs: [], }, + } + }, - editor: new Editor({ - content: 'Collaboration!', - extensions: [new Collab()], + methods: { + initEditor({ doc, version }) { + this.collabStartVersion = version + 1 + + this.editor = new Editor({ + content: doc, + extensions: [ + new Collab({ + version: this.collabStartVersion, + clientID: this.clientID, + }), + ], onUpdate: ({ state }) => { const sendable = sendableSteps(state) @@ -34,19 +47,17 @@ export default { this.ws.send(JSON.stringify(sendable)) } }, - }), - } - }, + }) + }, - methods: { receiveData({ version, steps, clientID }) { - if (version !== this.authority.steps.length) { + if (version !== this.collabHistory.steps.length + this.collabStartVersion) { return } steps.forEach(step => { - this.authority.steps.push(step) - this.authority.stepClientIDs.push(clientID) + this.collabHistory.steps.push(step) + this.collabHistory.clientIDs.push(clientID) }) this.updateDoc() @@ -66,9 +77,10 @@ export default { }, stepsSince(version) { + const count = version - this.collabStartVersion return { - steps: this.authority.steps.slice(version), - clientIDs: this.authority.stepClientIDs.slice(version), + steps: this.collabHistory.steps.slice(count), + clientIDs: this.collabHistory.clientIDs.slice(count), } }, }, @@ -77,7 +89,13 @@ export default { this.ws = new WebSocket('wss://tiptap-sockets.glitch.me') this.ws.onmessage = event => { - this.receiveData(JSON.parse(event.data)) + const payload = JSON.parse(event.data) + + if (payload.doc) { + this.initEditor(payload) + } else { + this.receiveData(payload) + } } },