webapp/src/components/Editor/prosemirror/extension/collab.ts

28 lines
891 B
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import { ySyncPlugin, yCursorPlugin, yUndoPlugin } from 'y-prosemirror'
2022-10-19 17:26:07 +00:00
import { ProseMirrorExtension } from '../helpers'
import { YOptions } from '../../store/context'
2022-09-09 11:53:35 +00:00
2022-10-19 17:26:07 +00:00
export const cursorBuilder = (user: any): HTMLElement => {
2022-09-09 11:53:35 +00:00
const cursor = document.createElement('span')
cursor.classList.add('ProseMirror-yjs-cursor')
cursor.setAttribute('style', `border-color: ${user.background}`)
2022-10-09 00:00:13 +00:00
const userDiv = document.createElement('span')
2022-09-09 11:53:35 +00:00
userDiv.setAttribute('style', `background-color: ${user.background}; color: ${user.foreground}`)
userDiv.textContent = user.name
cursor.append(userDiv)
return cursor
}
2022-10-09 00:00:13 +00:00
export default (y: YOptions): ProseMirrorExtension => ({
2022-09-09 11:53:35 +00:00
plugins: (prev) =>
y
? [
2022-10-19 17:26:07 +00:00
...prev,
ySyncPlugin(y.type),
// @ts-ignore
yCursorPlugin(y.provider.awareness, { cursorBuilder }),
yUndoPlugin()
]
2022-09-09 11:53:35 +00:00
: prev
})