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

33 lines
990 B
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import { ySyncPlugin, yCursorPlugin, yUndoPlugin } from 'y-prosemirror'
2022-10-18 18:50:02 +00:00
import type { YOptions } from '../../store/context'
2022-10-09 00:00:13 +00:00
import type { ProseMirrorExtension } from '../helpers'
2022-09-09 11:53:35 +00:00
2022-10-09 08:33:28 +00:00
export interface EditingProps {
name: string
foreground: string
background: string
}
export const cursorBuilder = (user: EditingProps): 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
? [
...prev,
2022-10-09 00:00:13 +00:00
ySyncPlugin(y.type),
2022-09-09 11:53:35 +00:00
yCursorPlugin(y.provider.awareness, { cursorBuilder }),
yUndoPlugin()
]
: prev
})