mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-06 19:38:08 +08:00
add basic collab implementation
This commit is contained in:
parent
8a23c4c945
commit
b23cdcba6b
12
examples/Components/Routes/Collaboration/Collab.js
Normal file
12
examples/Components/Routes/Collaboration/Collab.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Extension } from 'tiptap'
|
||||||
|
import { collab } from 'prosemirror-collab'
|
||||||
|
|
||||||
|
export default class CollabExtension extends Extension {
|
||||||
|
get name() {
|
||||||
|
return 'collab'
|
||||||
|
}
|
||||||
|
|
||||||
|
get plugins() {
|
||||||
|
return [collab()]
|
||||||
|
}
|
||||||
|
}
|
88
examples/Components/Routes/Collaboration/index.vue
Normal file
88
examples/Components/Routes/Collaboration/index.vue
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<template>
|
||||||
|
<div class="editor">
|
||||||
|
<editor-content class="editor__content" :editor="editor" />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import { Editor, EditorContent } from 'tiptap'
|
||||||
|
import { Step } from 'prosemirror-transform'
|
||||||
|
import { receiveTransaction, sendableSteps, getVersion } from 'prosemirror-collab'
|
||||||
|
import Collab from './Collab'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
EditorContent,
|
||||||
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
ws: null,
|
||||||
|
|
||||||
|
authority: {
|
||||||
|
steps: [],
|
||||||
|
stepClientIDs: [],
|
||||||
|
},
|
||||||
|
|
||||||
|
editor: new Editor({
|
||||||
|
content: 'Collaboration!',
|
||||||
|
extensions: [new Collab()],
|
||||||
|
onUpdate: ({ state }) => {
|
||||||
|
const sendable = sendableSteps(state)
|
||||||
|
|
||||||
|
if (sendable) {
|
||||||
|
this.ws.send(JSON.stringify(sendable))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
receiveData({ version, steps, clientID }) {
|
||||||
|
if (version !== this.authority.steps.length) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
steps.forEach(step => {
|
||||||
|
this.authority.steps.push(step)
|
||||||
|
this.authority.stepClientIDs.push(clientID)
|
||||||
|
})
|
||||||
|
|
||||||
|
this.updateDoc()
|
||||||
|
},
|
||||||
|
|
||||||
|
updateDoc() {
|
||||||
|
const { state, view, schema } = this.editor
|
||||||
|
const version = getVersion(state)
|
||||||
|
const data = this.stepsSince(version)
|
||||||
|
const transaction = receiveTransaction(
|
||||||
|
state,
|
||||||
|
data.steps.map(step => Step.fromJSON(schema, step)),
|
||||||
|
data.clientIDs,
|
||||||
|
)
|
||||||
|
|
||||||
|
view.dispatch(transaction)
|
||||||
|
},
|
||||||
|
|
||||||
|
stepsSince(version) {
|
||||||
|
return {
|
||||||
|
steps: this.authority.steps.slice(version),
|
||||||
|
clientIDs: this.authority.stepClientIDs.slice(version),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
mounted() {
|
||||||
|
this.ws = new WebSocket('wss://tiptap-sockets.glitch.me')
|
||||||
|
|
||||||
|
this.ws.onmessage = event => {
|
||||||
|
this.receiveData(JSON.parse(event.data))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
beforeDestroy() {
|
||||||
|
this.editor.destroy()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
@ -48,6 +48,9 @@
|
|||||||
<router-link class="subnavigation__link" to="/export">
|
<router-link class="subnavigation__link" to="/export">
|
||||||
Export HTML or JSON
|
Export HTML or JSON
|
||||||
</router-link>
|
</router-link>
|
||||||
|
<router-link class="subnavigation__link" to="/collaboration">
|
||||||
|
Collaboration
|
||||||
|
</router-link>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -124,6 +124,13 @@ const routes = [
|
|||||||
githubUrl: 'https://github.com/scrumpy/tiptap/tree/master/examples/Components/Routes/Export',
|
githubUrl: 'https://github.com/scrumpy/tiptap/tree/master/examples/Components/Routes/Export',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '/collaboration',
|
||||||
|
component: () => import('Components/Routes/Collaboration'),
|
||||||
|
meta: {
|
||||||
|
githubUrl: 'https://github.com/scrumpy/tiptap/tree/master/examples/Components/Routes/Collaboration',
|
||||||
|
},
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
const router = new VueRouter({
|
const router = new VueRouter({
|
||||||
|
@ -86,5 +86,7 @@
|
|||||||
"webpack-svgstore-plugin": "^4.1.0",
|
"webpack-svgstore-plugin": "^4.1.0",
|
||||||
"zlib": "^1.0.5"
|
"zlib": "^1.0.5"
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"dependencies": {
|
||||||
|
"prosemirror-collab": "^1.1.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9572,6 +9572,13 @@ promzard@^0.3.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
read "1"
|
read "1"
|
||||||
|
|
||||||
|
prosemirror-collab@^1.1.1:
|
||||||
|
version "1.1.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/prosemirror-collab/-/prosemirror-collab-1.1.1.tgz#c8f5d951abaeac8a80818b6bd960f5a392b35b3f"
|
||||||
|
integrity sha512-BpXIB3WBD7UvgxuiasKOxlAZ78TTOdW+SQN4bbJan995tVx/wM/OZXtRJebS+tSWWAbRisHaO3ciFo732vuvdA==
|
||||||
|
dependencies:
|
||||||
|
prosemirror-state "^1.0.0"
|
||||||
|
|
||||||
prosemirror-commands@^1.0.7:
|
prosemirror-commands@^1.0.7:
|
||||||
version "1.0.7"
|
version "1.0.7"
|
||||||
resolved "https://registry.yarnpkg.com/prosemirror-commands/-/prosemirror-commands-1.0.7.tgz#e5a2ba821e29ea7065c88277fe2c3d7f6b0b9d37"
|
resolved "https://registry.yarnpkg.com/prosemirror-commands/-/prosemirror-commands-1.0.7.tgz#e5a2ba821e29ea7065c88277fe2c3d7f6b0b9d37"
|
||||||
|
Loading…
Reference in New Issue
Block a user