fix: garbage cursors on client

This commit is contained in:
cudr 2019-10-27 17:15:16 +03:00
parent 8d7a10be1b
commit eccece2740
3 changed files with 47 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import * as Automerge from 'automerge' import * as Automerge from 'automerge'
import { toSlateOp } from './index' import { toSlateOp } from './index'
import { createDoc, cloneDoc, createParagraphJSON } from '../utils' import { createDoc, cloneDoc, createBlockJSON, toJS } from '../utils'
describe('convert operations to slatejs model', () => { describe('convert operations to slatejs model', () => {
it('convert insert operations', () => { it('convert insert operations', () => {
@ -8,7 +8,7 @@ describe('convert operations to slatejs model', () => {
const doc2 = cloneDoc(doc1) const doc2 = cloneDoc(doc1)
const change = Automerge.change(doc1, 'change', (d: any) => { 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!' d.document.nodes[1].nodes[0].text = 'hello!'
}) })
@ -31,4 +31,42 @@ describe('convert operations to slatejs model', () => {
expect(slateOps).toStrictEqual(expectedOps) 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)
})
}) })

View File

@ -1,7 +1,7 @@
import { SyncDoc, Path } from '../model' import { SyncDoc, Path } from '../model'
import { NodeJSON } from 'slate' 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) => { export const getTarget = (doc: SyncDoc, path: Path) => {
const iterate = (current: any, idx: number) => { const iterate = (current: any, idx: number) => {

View File

@ -7,15 +7,18 @@ export const createTextJSON = (text: string = ''): TextJSON => ({
text text
}) })
export const createParagraphJSON = (text: string = ''): NodeJSON => ({ export const createBlockJSON = (
type: string = 'paragraph',
text: string = ''
): NodeJSON => ({
object: 'block', object: 'block',
type: 'paragraph', type,
nodes: [createTextJSON(text)] nodes: [createTextJSON(text)]
}) })
export const createValueJSON = (): ValueJSON => ({ export const createValueJSON = (): ValueJSON => ({
document: { document: {
nodes: [createParagraphJSON()] nodes: [createBlockJSON()]
} }
}) })