2020-09-26 23:35:07 +08:00
|
|
|
import { Extension, Command } from '@tiptap/core'
|
2020-09-26 17:03:17 +08:00
|
|
|
import { yCursorPlugin } from 'y-prosemirror'
|
|
|
|
|
|
|
|
export interface CollaborationCursorOptions {
|
|
|
|
name: string,
|
|
|
|
color: string,
|
|
|
|
provider: any,
|
2020-09-26 17:20:19 +08:00
|
|
|
render (user: { name: string, color: string }): HTMLElement,
|
2020-09-26 17:03:17 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 23:35:07 +08:00
|
|
|
export type UserCommand = (attributes: {
|
|
|
|
name: string,
|
|
|
|
color: string,
|
|
|
|
}) => Command
|
|
|
|
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
|
|
interface Commands {
|
|
|
|
user: UserCommand,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 17:03:17 +08:00
|
|
|
export default new Extension<CollaborationCursorOptions>()
|
|
|
|
.name('collaboration_cursor')
|
2020-09-26 23:35:07 +08:00
|
|
|
.commands(({ options }) => ({
|
|
|
|
user: attributes => () => {
|
|
|
|
options.provider.awareness.setLocalStateField('user', attributes)
|
|
|
|
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}))
|
2020-09-26 17:03:17 +08:00
|
|
|
.defaults({
|
|
|
|
provider: null,
|
|
|
|
name: 'Someone',
|
|
|
|
color: '#cccccc',
|
2020-09-26 17:20:19 +08:00
|
|
|
render: user => {
|
|
|
|
const cursor = document.createElement('span')
|
2020-09-27 16:37:48 +08:00
|
|
|
cursor.classList.add('collaboration-cursor__caret')
|
2020-09-26 17:20:19 +08:00
|
|
|
cursor.setAttribute('style', `border-color: ${user.color}`)
|
|
|
|
|
|
|
|
const label = document.createElement('div')
|
2020-09-27 16:37:48 +08:00
|
|
|
label.classList.add('collaboration-cursor__label')
|
2020-09-26 17:20:19 +08:00
|
|
|
label.setAttribute('style', `background-color: ${user.color}`)
|
|
|
|
label.insertBefore(document.createTextNode(user.name), null)
|
|
|
|
cursor.insertBefore(label, null)
|
|
|
|
|
|
|
|
return cursor
|
|
|
|
},
|
2020-09-26 17:03:17 +08:00
|
|
|
})
|
|
|
|
.plugins(({ options }) => [
|
|
|
|
yCursorPlugin((() => {
|
|
|
|
options.provider.awareness.setLocalStateField('user', {
|
|
|
|
name: options.name,
|
|
|
|
color: options.color,
|
|
|
|
})
|
|
|
|
|
|
|
|
return options.provider.awareness
|
2020-09-26 17:20:19 +08:00
|
|
|
})(),
|
|
|
|
// @ts-ignore
|
|
|
|
{
|
|
|
|
cursorBuilder: options.render,
|
|
|
|
}),
|
2020-09-26 17:03:17 +08:00
|
|
|
])
|
|
|
|
.create()
|