refactor the collaboration extensions
This commit is contained in:
@@ -87,7 +87,7 @@
|
|||||||
class="collaboration-users__item"
|
class="collaboration-users__item"
|
||||||
:style="`background-color: ${user.color}`"
|
:style="`background-color: ${user.color}`"
|
||||||
v-for="user in users"
|
v-for="user in users"
|
||||||
:key="user.id"
|
:key="user.clientId"
|
||||||
>
|
>
|
||||||
{{ user.name }}
|
{{ user.name }}
|
||||||
</div>
|
</div>
|
||||||
@@ -112,12 +112,9 @@ export default {
|
|||||||
|
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
documentName: 'tiptap-collaboration-example',
|
|
||||||
name: this.getRandomName(),
|
name: this.getRandomName(),
|
||||||
color: this.getRandomColor(),
|
color: this.getRandomColor(),
|
||||||
ydoc: null,
|
|
||||||
provider: null,
|
provider: null,
|
||||||
type: null,
|
|
||||||
indexdb: null,
|
indexdb: null,
|
||||||
editor: null,
|
editor: null,
|
||||||
users: [],
|
users: [],
|
||||||
@@ -125,28 +122,28 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.ydoc = new Y.Doc()
|
const ydoc = new Y.Doc()
|
||||||
this.type = this.ydoc.getXmlFragment('prosemirror')
|
this.provider = new WebrtcProvider('tiptap-collaboration-example', ydoc)
|
||||||
this.indexdb = new IndexeddbPersistence(this.documentName, this.ydoc)
|
this.indexdb = new IndexeddbPersistence('tiptap-collaboration-example', ydoc)
|
||||||
|
|
||||||
this.provider = new WebrtcProvider(this.documentName, this.ydoc)
|
|
||||||
this.provider.awareness.on('change', this.updateState)
|
|
||||||
|
|
||||||
this.editor = new Editor({
|
this.editor = new Editor({
|
||||||
extensions: [
|
extensions: [
|
||||||
...defaultExtensions(),
|
...defaultExtensions(),
|
||||||
Collaboration.configure({
|
Collaboration.configure({
|
||||||
type: this.type,
|
provider: this.provider,
|
||||||
}),
|
}),
|
||||||
CollaborationCursor.configure({
|
CollaborationCursor.configure({
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
name: this.name,
|
user: {
|
||||||
color: this.color,
|
name: this.name,
|
||||||
|
color: this.color,
|
||||||
|
},
|
||||||
|
onUpdate: users => {
|
||||||
|
this.users = users
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
this.updateState()
|
|
||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
@@ -175,7 +172,7 @@ export default {
|
|||||||
color: this.color,
|
color: this.color,
|
||||||
}).run()
|
}).run()
|
||||||
|
|
||||||
this.updateState()
|
// this.updateState()
|
||||||
},
|
},
|
||||||
|
|
||||||
getRandomColor() {
|
getRandomColor() {
|
||||||
@@ -200,17 +197,6 @@ export default {
|
|||||||
getRandomElement(list) {
|
getRandomElement(list) {
|
||||||
return list[Math.floor(Math.random() * list.length)]
|
return list[Math.floor(Math.random() * list.length)]
|
||||||
},
|
},
|
||||||
|
|
||||||
updateState() {
|
|
||||||
const { states } = this.provider.awareness
|
|
||||||
|
|
||||||
this.users = Array.from(states.entries()).map(state => {
|
|
||||||
return {
|
|
||||||
id: state[0],
|
|
||||||
...state[1].user,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
beforeDestroy() {
|
beforeDestroy() {
|
||||||
|
|||||||
@@ -2,17 +2,28 @@ import { Extension, Command } from '@tiptap/core'
|
|||||||
import { yCursorPlugin } from 'y-prosemirror'
|
import { yCursorPlugin } from 'y-prosemirror'
|
||||||
|
|
||||||
export interface CollaborationCursorOptions {
|
export interface CollaborationCursorOptions {
|
||||||
name: string,
|
|
||||||
color: string,
|
|
||||||
provider: any,
|
provider: any,
|
||||||
render (user: { name: string, color: string }): HTMLElement,
|
user: { [key: string]: any },
|
||||||
|
render (user: { [key: string]: any }): HTMLElement,
|
||||||
|
onUpdate: (users: { clientId: string, [key: string]: any }[]) => null,
|
||||||
|
}
|
||||||
|
|
||||||
|
const awarenessStatesToArray = (states: Map<number, { [key: string]: any }>) => {
|
||||||
|
return Array.from(states.entries()).map(([key, value]) => {
|
||||||
|
return {
|
||||||
|
clientId: key,
|
||||||
|
...value.user,
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const CollaborationCursor = Extension.create({
|
const CollaborationCursor = Extension.create({
|
||||||
defaultOptions: <CollaborationCursorOptions>{
|
defaultOptions: <CollaborationCursorOptions>{
|
||||||
provider: null,
|
provider: null,
|
||||||
name: 'Someone',
|
user: {
|
||||||
color: '#cccccc',
|
name: null,
|
||||||
|
color: null,
|
||||||
|
},
|
||||||
render: user => {
|
render: user => {
|
||||||
const cursor = document.createElement('span')
|
const cursor = document.createElement('span')
|
||||||
cursor.classList.add('collaboration-cursor__caret')
|
cursor.classList.add('collaboration-cursor__caret')
|
||||||
@@ -26,6 +37,7 @@ const CollaborationCursor = Extension.create({
|
|||||||
|
|
||||||
return cursor
|
return cursor
|
||||||
},
|
},
|
||||||
|
onUpdate: () => null,
|
||||||
},
|
},
|
||||||
|
|
||||||
addCommands() {
|
addCommands() {
|
||||||
@@ -33,11 +45,9 @@ const CollaborationCursor = Extension.create({
|
|||||||
/**
|
/**
|
||||||
* Update details of the current user
|
* Update details of the current user
|
||||||
*/
|
*/
|
||||||
user: (attributes: {
|
user: (attributes: { [key: string]: any }): Command => () => {
|
||||||
name: string,
|
|
||||||
color: string,
|
|
||||||
}): Command => () => {
|
|
||||||
this.options.provider.awareness.setLocalStateField('user', attributes)
|
this.options.provider.awareness.setLocalStateField('user', attributes)
|
||||||
|
this.options.onUpdate(awarenessStatesToArray(this.options.provider.awareness.states))
|
||||||
|
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
@@ -47,11 +57,14 @@ const CollaborationCursor = Extension.create({
|
|||||||
addProseMirrorPlugins() {
|
addProseMirrorPlugins() {
|
||||||
return [
|
return [
|
||||||
yCursorPlugin((() => {
|
yCursorPlugin((() => {
|
||||||
this.options.provider.awareness.setLocalStateField('user', {
|
this.options.provider.awareness.setLocalStateField('user', this.options.user)
|
||||||
name: this.options.name,
|
|
||||||
color: this.options.color,
|
this.options.provider.awareness.on('change', () => {
|
||||||
|
this.options.onUpdate(awarenessStatesToArray(this.options.provider.awareness.states))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.options.onUpdate(awarenessStatesToArray(this.options.provider.awareness.states))
|
||||||
|
|
||||||
return this.options.provider.awareness
|
return this.options.provider.awareness
|
||||||
})(),
|
})(),
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|||||||
@@ -7,17 +7,19 @@ import {
|
|||||||
} from 'y-prosemirror'
|
} from 'y-prosemirror'
|
||||||
|
|
||||||
export interface CollaborationOptions {
|
export interface CollaborationOptions {
|
||||||
type: any,
|
provider: any,
|
||||||
}
|
}
|
||||||
|
|
||||||
const Collaboration = Extension.create({
|
const Collaboration = Extension.create({
|
||||||
defaultOptions: <CollaborationOptions>{
|
defaultOptions: <CollaborationOptions>{
|
||||||
type: null,
|
provider: null,
|
||||||
},
|
},
|
||||||
|
|
||||||
addProseMirrorPlugins() {
|
addProseMirrorPlugins() {
|
||||||
return [
|
return [
|
||||||
ySyncPlugin(this.options.type),
|
ySyncPlugin(
|
||||||
|
this.options.provider.doc.getXmlFragment('prosemirror'),
|
||||||
|
),
|
||||||
yUndoPlugin(),
|
yUndoPlugin(),
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user