You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cudr_slate-collaborative/packages/bridge/src/cursor/index.ts

36 lines
749 B

import { Operation, Range } from 'slate'
import { CursorData } from '../model'
export const setCursor = (
id: string,
selection: Range | null,
doc: any,
operations: Operation[],
cursorData: CursorData
) => {
const cursorOps = operations.filter(op => op.type === 'set_selection')
if (!doc.cursors) doc.cursors = {}
const newCursor = cursorOps[cursorOps.length - 1]?.newProperties || {}
if (selection) {
doc.cursors[id] = JSON.stringify(
Object.assign(
(doc.cursors[id] && JSON.parse(doc.cursors[id])) || {},
newCursor,
selection,
{
...cursorData,
isForward: Boolean(newCursor.focus)
}
)
)
} else {
delete doc.cursors[id]
}
return doc
}