|
|
|
@ -1,13 +1,14 @@
|
|
|
|
|
import * as Automerge from 'automerge'
|
|
|
|
|
import { Operation, Node } from 'slate'
|
|
|
|
|
import { Operation, Node, SetNodeOperation, Element } from 'slate'
|
|
|
|
|
import { CollabMap, CollabOperation, SyncDoc } from '../model'
|
|
|
|
|
|
|
|
|
|
import { toSlatePath, toJS } from '../utils'
|
|
|
|
|
import { rootKey } from './constants'
|
|
|
|
|
|
|
|
|
|
const setDataOp = (
|
|
|
|
|
{ key = '', obj, path, value }: Automerge.Diff,
|
|
|
|
|
doc: any
|
|
|
|
|
) => (map: any) => {
|
|
|
|
|
doc: SyncDoc
|
|
|
|
|
) => (map: CollabMap) => {
|
|
|
|
|
return {
|
|
|
|
|
type: 'set_node',
|
|
|
|
|
path: toSlatePath(path),
|
|
|
|
@ -17,10 +18,17 @@ const setDataOp = (
|
|
|
|
|
newProperties: {
|
|
|
|
|
[key]: value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} as SetNodeOperation
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setChildren = (op: Automerge.Diff, doc: any) => (map: any) => {
|
|
|
|
|
/**
|
|
|
|
|
* Convert a root level children update to slate operations.
|
|
|
|
|
*
|
|
|
|
|
* When we receive a root level children array update we need to handle that
|
|
|
|
|
* operation as a special case since slate does not allow for setting the root
|
|
|
|
|
* level node through a simple set_node operation
|
|
|
|
|
*/
|
|
|
|
|
const setChildren = (op: Automerge.Diff) => (map: CollabMap, doc: Element) => {
|
|
|
|
|
const { value } = op
|
|
|
|
|
|
|
|
|
|
const ops: Operation[] = []
|
|
|
|
@ -52,15 +60,20 @@ const setChildren = (op: Automerge.Diff, doc: any) => (map: any) => {
|
|
|
|
|
return ops
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
|
|
|
|
|
const opSet = (
|
|
|
|
|
op: Automerge.Diff,
|
|
|
|
|
[map, ops]: [CollabMap, CollabOperation[]],
|
|
|
|
|
doc: SyncDoc
|
|
|
|
|
) => {
|
|
|
|
|
const { link, value, path, obj, key } = op
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Update our map to include the latest linked values if provided
|
|
|
|
|
if (map[obj]) {
|
|
|
|
|
map[obj][key as any] = link ? map[value] : value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ignore all cursor updates since those do not need to translate into
|
|
|
|
|
// Ignore all cursor updates since those do not need to translate into
|
|
|
|
|
// slate operations
|
|
|
|
|
if (path && path.includes('cursors')) {
|
|
|
|
|
return [map, ops]
|
|
|
|
@ -68,8 +81,10 @@ const opSet = (op: Automerge.Diff, [map, ops]: any, doc: any) => {
|
|
|
|
|
|
|
|
|
|
// Handle updates received for the root children array
|
|
|
|
|
if (obj === rootKey && key === 'children') {
|
|
|
|
|
ops.push(setChildren(op, doc))
|
|
|
|
|
} else if (path && obj !== rootKey && !path.includes('cursors')) {
|
|
|
|
|
ops.push(setChildren(op))
|
|
|
|
|
}
|
|
|
|
|
// Handle other setNode operations
|
|
|
|
|
else if (path && obj !== rootKey && path.length !== 0) {
|
|
|
|
|
ops.push(setDataOp(op, doc))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|