tiptap/packages/extension-collaboration-cursor/index.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-26 17:03:17 +08:00
import { Extension } from '@tiptap/core'
import { yCursorPlugin } from 'y-prosemirror'
export interface CollaborationCursorOptions {
name: string,
color: string,
provider: any,
render (user: { name: string, color: string }): HTMLElement,
2020-09-26 17:03:17 +08:00
}
export default new Extension<CollaborationCursorOptions>()
.name('collaboration_cursor')
.defaults({
provider: null,
name: 'Someone',
color: '#cccccc',
render: user => {
const cursor = document.createElement('span')
cursor.classList.add('collaboration-cursor')
cursor.setAttribute('style', `border-color: ${user.color}`)
const label = document.createElement('div')
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
})(),
// @ts-ignore
{
cursorBuilder: options.render,
}),
2020-09-26 17:03:17 +08:00
])
.create()