2019-02-05 09:20:49 +08:00
|
|
|
<template>
|
|
|
|
<div class="editor">
|
|
|
|
<editor-content class="editor__content" :editor="editor" v-if="editor" />
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2019-02-05 10:07:43 +08:00
|
|
|
import io from 'socket.io-client'
|
2019-02-05 09:20:49 +08:00
|
|
|
import { debounce } from 'lodash-es'
|
|
|
|
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 {
|
2019-02-06 07:03:23 +08:00
|
|
|
history: [],
|
2019-02-05 09:20:49 +08:00
|
|
|
editor: null,
|
2019-02-05 10:07:43 +08:00
|
|
|
socket: null,
|
2019-02-06 07:03:23 +08:00
|
|
|
newState: null,
|
2019-02-05 09:20:49 +08:00
|
|
|
clientID: Math.floor(Math.random() * 0xFFFFFFFF),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
initEditor({ doc, version }) {
|
2019-02-05 10:07:43 +08:00
|
|
|
if (this.editor) {
|
|
|
|
this.editor.destroy()
|
|
|
|
}
|
2019-02-05 09:20:49 +08:00
|
|
|
|
|
|
|
this.editor = new Editor({
|
|
|
|
content: doc,
|
|
|
|
extensions: [
|
|
|
|
new Collab({
|
2019-02-05 10:07:43 +08:00
|
|
|
version,
|
2019-02-05 09:20:49 +08:00
|
|
|
clientID: this.clientID,
|
|
|
|
}),
|
|
|
|
],
|
2019-02-06 07:03:23 +08:00
|
|
|
// onTransaction: transaction => {
|
|
|
|
// console.log('onTransaction')
|
|
|
|
// this.newState = this.editor.state.apply(transaction)
|
|
|
|
// this.editor.view.updateState(this.state.edit)
|
|
|
|
// return false
|
|
|
|
// },
|
2019-02-05 09:20:49 +08:00
|
|
|
onUpdate: ({ state }) => {
|
|
|
|
this.getSendableSteps(state)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getSendableSteps: debounce(function (state) {
|
|
|
|
const sendable = sendableSteps(state)
|
|
|
|
|
|
|
|
if (sendable) {
|
2019-02-05 21:00:30 +08:00
|
|
|
this.socket.emit('update', sendable)
|
2019-02-05 09:20:49 +08:00
|
|
|
|
2019-02-06 07:03:23 +08:00
|
|
|
const { steps } = sendable
|
|
|
|
const clientIDs = this.repeat(sendable.clientID, steps.length)
|
|
|
|
|
|
|
|
this.history.push({
|
|
|
|
state,
|
|
|
|
version: getVersion(state),
|
|
|
|
steps,
|
|
|
|
clientIDs,
|
|
|
|
})
|
|
|
|
|
2019-02-05 09:20:49 +08:00
|
|
|
const transaction = receiveTransaction(
|
2019-02-06 07:03:23 +08:00
|
|
|
state,
|
|
|
|
steps,
|
|
|
|
clientIDs,
|
2019-02-05 09:20:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
this.editor.view.dispatch(transaction)
|
|
|
|
}
|
|
|
|
}, 250),
|
|
|
|
|
|
|
|
receiveData({ steps }) {
|
|
|
|
const { state, view, schema } = this.editor
|
|
|
|
|
|
|
|
const transaction = receiveTransaction(
|
|
|
|
state,
|
|
|
|
steps.map(step => Step.fromJSON(schema, step)),
|
|
|
|
steps.map(step => step.clientID),
|
|
|
|
)
|
|
|
|
|
|
|
|
view.dispatch(transaction)
|
|
|
|
},
|
|
|
|
|
|
|
|
repeat(val, n) {
|
|
|
|
const result = []
|
|
|
|
for (let i = 0; i < n; i++) result.push(val)
|
|
|
|
return result
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted() {
|
2019-02-05 10:07:43 +08:00
|
|
|
this.socket = io('wss://tiptap-sockets-2.glitch.me')
|
|
|
|
.on('connect', () => {
|
|
|
|
console.log('connected')
|
|
|
|
})
|
|
|
|
.on('disconnect', () => {
|
|
|
|
console.log('disconnected')
|
|
|
|
})
|
2019-02-05 21:00:30 +08:00
|
|
|
.on('document', data => {
|
|
|
|
this.initEditor(data)
|
|
|
|
})
|
|
|
|
.on('update', data => {
|
|
|
|
this.receiveData(data)
|
|
|
|
})
|
2019-02-06 07:03:23 +08:00
|
|
|
.on('versionInSync', version => {
|
|
|
|
console.log('version in sync', version)
|
|
|
|
// TODO remove steps older than version
|
|
|
|
})
|
|
|
|
.on('versionMismatch', ({ version, data }) => {
|
|
|
|
console.log('version mismatch', version)
|
|
|
|
// TODO: go back to `version`, apply `steps`, apply unmerged `steps`
|
2019-02-05 21:00:30 +08:00
|
|
|
|
2019-02-06 07:03:23 +08:00
|
|
|
console.log(this.history.length)
|
|
|
|
|
|
|
|
const history = this.history.find(item => {
|
|
|
|
console.log('search', item.version, version)
|
|
|
|
return item.version === version
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log({ history })
|
2019-02-05 21:00:30 +08:00
|
|
|
|
|
|
|
const { state, view, schema } = this.editor
|
|
|
|
|
2019-02-06 07:03:23 +08:00
|
|
|
view.updateState(history.state)
|
|
|
|
|
|
|
|
view.dispatch(receiveTransaction(
|
|
|
|
history.state,
|
2019-02-05 21:00:30 +08:00
|
|
|
data.map(item => Step.fromJSON(schema, item.step)),
|
|
|
|
data.map(item => item.clientID),
|
2019-02-06 07:03:23 +08:00
|
|
|
))
|
|
|
|
|
|
|
|
view.dispatch(receiveTransaction(
|
|
|
|
history.state,
|
|
|
|
history.steps,
|
|
|
|
history.clientIDs,
|
|
|
|
))
|
|
|
|
|
|
|
|
// const transaction = receiveTransaction(
|
|
|
|
// state,
|
|
|
|
// steps,
|
|
|
|
// clientIDs,
|
|
|
|
// )
|
2019-02-05 21:00:30 +08:00
|
|
|
|
2019-02-06 07:03:23 +08:00
|
|
|
// this.editor.view.dispatch(transaction)
|
2019-02-05 10:07:43 +08:00
|
|
|
})
|
2019-02-06 07:03:23 +08:00
|
|
|
// .on('versionMismatch', (version, steps) => {
|
|
|
|
// // set state to the latest synced version?
|
|
|
|
// // this.editor.view.updateState(this.prevState)
|
|
|
|
|
|
|
|
// const currentVersion = getVersion(this.editor.state)
|
|
|
|
// console.log('should poll version', currentVersion)
|
|
|
|
|
|
|
|
// this.socket.emit('getVersionSteps', currentVersion)
|
|
|
|
// })
|
|
|
|
// .on('versionSteps', data => {
|
|
|
|
// console.log('versionSteps', data)
|
|
|
|
// const { state, view, schema } = this.editor
|
|
|
|
|
|
|
|
// const transaction = receiveTransaction(
|
|
|
|
// state,
|
|
|
|
// data.map(item => Step.fromJSON(schema, item.step)),
|
|
|
|
// data.map(item => item.clientID),
|
|
|
|
// )
|
|
|
|
|
|
|
|
// view.dispatch(transaction)
|
|
|
|
// })
|
2019-02-05 09:20:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
beforeDestroy() {
|
|
|
|
this.editor.destroy()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|