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

29 lines
955 B
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import { ySyncPlugin, yCursorPlugin, yUndoPlugin } from 'y-prosemirror'
2022-10-21 10:17:38 +00:00
import type { ProseMirrorExtension } from '../helpers'
import type { YOptions } from '../../store/context'
2022-09-09 11:53:35 +00:00
2022-10-21 10:17:38 +00:00
interface YUser { background: string, foreground: string, name: string }
export const cursorBuilder = (user: YUser): 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),
yCursorPlugin(y.provider.awareness, { cursorBuilder }),
yUndoPlugin()
]
2022-09-09 11:53:35 +00:00
: prev
})