diff --git a/packages/bridge/src/convert/convert.spec.ts b/packages/bridge/src/convert/convert.spec.ts index a2636d8..5960dd0 100644 --- a/packages/bridge/src/convert/convert.spec.ts +++ b/packages/bridge/src/convert/convert.spec.ts @@ -1,6 +1,6 @@ import * as Automerge from 'automerge' import { toSlateOp } from './index' -import { createDoc, cloneDoc, createParagraphJSON } from '../utils' +import { createDoc, cloneDoc, createBlockJSON, toJS } from '../utils' describe('convert operations to slatejs model', () => { it('convert insert operations', () => { @@ -8,7 +8,7 @@ describe('convert operations to slatejs model', () => { const doc2 = cloneDoc(doc1) const change = Automerge.change(doc1, 'change', (d: any) => { - d.document.nodes.push(createParagraphJSON('hello!')) + d.document.nodes.push(createBlockJSON('paragraph', 'hello!')) d.document.nodes[1].nodes[0].text = 'hello!' }) @@ -31,4 +31,42 @@ describe('convert operations to slatejs model', () => { expect(slateOps).toStrictEqual(expectedOps) }) + + it('convert remove operations', () => { + const doc1 = Automerge.change(createDoc(), 'change', (d: any) => { + d.document.nodes.push(createBlockJSON('paragraph', 'hello!')) + d.document.nodes.push(createBlockJSON('paragraph', 'hello twice!')) + d.document.nodes[1].nodes[0].text = 'hello!' + }) + + const doc2 = cloneDoc(doc1) + + const change = Automerge.change(doc1, 'change', (d: any) => { + delete d.document.nodes[1] + delete d.document.nodes[0].nodes[0] + }) + + const operations = Automerge.diff(doc2, change) + + const slateOps = toSlateOp(operations, change) + + const expectedOps = [ + { + type: 'remove_node', + path: [1], + node: { + object: 'text' + } + }, + { + type: 'remove_node', + path: [0, 0], + node: { + object: 'text' + } + } + ] + + expect(slateOps).toStrictEqual(expectedOps) + }) }) diff --git a/packages/bridge/src/path/index.ts b/packages/bridge/src/path/index.ts index 7ef3fb4..94491a0 100644 --- a/packages/bridge/src/path/index.ts +++ b/packages/bridge/src/path/index.ts @@ -1,7 +1,7 @@ import { SyncDoc, Path } from '../model' import { NodeJSON } from 'slate' -export const isTree = (node: NodeJSON): any => node.object !== 'text' +export const isTree = (node: NodeJSON): any => node && node.object !== 'text' export const getTarget = (doc: SyncDoc, path: Path) => { const iterate = (current: any, idx: number) => { diff --git a/packages/bridge/src/utils/testUtils.ts b/packages/bridge/src/utils/testUtils.ts index 28ca52a..ec522e1 100644 --- a/packages/bridge/src/utils/testUtils.ts +++ b/packages/bridge/src/utils/testUtils.ts @@ -7,15 +7,18 @@ export const createTextJSON = (text: string = ''): TextJSON => ({ text }) -export const createParagraphJSON = (text: string = ''): NodeJSON => ({ +export const createBlockJSON = ( + type: string = 'paragraph', + text: string = '' +): NodeJSON => ({ object: 'block', - type: 'paragraph', + type, nodes: [createTextJSON(text)] }) export const createValueJSON = (): ValueJSON => ({ document: { - nodes: [createParagraphJSON()] + nodes: [createBlockJSON()] } })