improve collab demo
This commit is contained in:
@@ -6,7 +6,19 @@ export default class CollabExtension extends Extension {
|
|||||||
return 'collab'
|
return 'collab'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get defaultOptions() {
|
||||||
|
return {
|
||||||
|
version: 0,
|
||||||
|
clientID: Math.floor(Math.random() * 0xFFFFFFFF),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get plugins() {
|
get plugins() {
|
||||||
return [collab()]
|
return [
|
||||||
|
collab({
|
||||||
|
version: this.options.version,
|
||||||
|
clientID: this.options.clientID,
|
||||||
|
}),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="editor">
|
<div class="editor">
|
||||||
<editor-content class="editor__content" :editor="editor" />
|
<editor-content class="editor__content" :editor="editor" v-if="editor" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -17,16 +17,29 @@ export default {
|
|||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
editor: null,
|
||||||
ws: null,
|
ws: null,
|
||||||
|
clientID: Math.floor(Math.random() * 0xFFFFFFFF),
|
||||||
authority: {
|
collabStartVersion: 0,
|
||||||
|
collabHistory: {
|
||||||
steps: [],
|
steps: [],
|
||||||
stepClientIDs: [],
|
clientIDs: [],
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
editor: new Editor({
|
methods: {
|
||||||
content: 'Collaboration!',
|
initEditor({ doc, version }) {
|
||||||
extensions: [new Collab()],
|
this.collabStartVersion = version + 1
|
||||||
|
|
||||||
|
this.editor = new Editor({
|
||||||
|
content: doc,
|
||||||
|
extensions: [
|
||||||
|
new Collab({
|
||||||
|
version: this.collabStartVersion,
|
||||||
|
clientID: this.clientID,
|
||||||
|
}),
|
||||||
|
],
|
||||||
onUpdate: ({ state }) => {
|
onUpdate: ({ state }) => {
|
||||||
const sendable = sendableSteps(state)
|
const sendable = sendableSteps(state)
|
||||||
|
|
||||||
@@ -34,19 +47,17 @@ export default {
|
|||||||
this.ws.send(JSON.stringify(sendable))
|
this.ws.send(JSON.stringify(sendable))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
})
|
||||||
}
|
},
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
receiveData({ version, steps, clientID }) {
|
receiveData({ version, steps, clientID }) {
|
||||||
if (version !== this.authority.steps.length) {
|
if (version !== this.collabHistory.steps.length + this.collabStartVersion) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
steps.forEach(step => {
|
steps.forEach(step => {
|
||||||
this.authority.steps.push(step)
|
this.collabHistory.steps.push(step)
|
||||||
this.authority.stepClientIDs.push(clientID)
|
this.collabHistory.clientIDs.push(clientID)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.updateDoc()
|
this.updateDoc()
|
||||||
@@ -66,9 +77,10 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
stepsSince(version) {
|
stepsSince(version) {
|
||||||
|
const count = version - this.collabStartVersion
|
||||||
return {
|
return {
|
||||||
steps: this.authority.steps.slice(version),
|
steps: this.collabHistory.steps.slice(count),
|
||||||
clientIDs: this.authority.stepClientIDs.slice(version),
|
clientIDs: this.collabHistory.clientIDs.slice(count),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -77,7 +89,13 @@ export default {
|
|||||||
this.ws = new WebSocket('wss://tiptap-sockets.glitch.me')
|
this.ws = new WebSocket('wss://tiptap-sockets.glitch.me')
|
||||||
|
|
||||||
this.ws.onmessage = event => {
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user