mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
feat: add ci and op convert insert test
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
import { Operation, SyncDoc } from '../model/index'
|
||||
|
||||
// TODO: handle annotation ops
|
||||
|
||||
export const addAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||
console.log('addAnnotation!!!', op.toJS())
|
||||
// console.log('addAnnotation!!!', op.toJS())
|
||||
return doc
|
||||
}
|
||||
|
||||
export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||
console.log('removeAnnotation!!!', op.toJS())
|
||||
// console.log('removeAnnotation!!!', op.toJS())
|
||||
return doc
|
||||
}
|
||||
|
||||
export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||
console.log('setAnnotation!!!', op.toJS())
|
||||
// console.log('setAnnotation!!!', op.toJS())
|
||||
return doc
|
||||
}
|
||||
|
||||
|
||||
34
packages/bridge/src/convert/convert.spec.ts
Normal file
34
packages/bridge/src/convert/convert.spec.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import * as Automerge from 'automerge'
|
||||
import { toSlateOp } from './index'
|
||||
import { createDoc, cloneDoc, createParagraphJSON } from '../utils'
|
||||
|
||||
describe('convert operations to slatejs model', () => {
|
||||
it('convert insert operations', () => {
|
||||
const doc1 = createDoc()
|
||||
const doc2 = cloneDoc(doc1)
|
||||
|
||||
const change = Automerge.change(doc1, 'change', (d: any) => {
|
||||
d.document.nodes.push(createParagraphJSON('hello!'))
|
||||
d.document.nodes[1].nodes[0].text = 'hello!'
|
||||
})
|
||||
|
||||
const operations = Automerge.diff(doc2, change)
|
||||
|
||||
const slateOps = toSlateOp(operations, change)
|
||||
|
||||
const expectedOps = [
|
||||
{
|
||||
type: 'insert_node',
|
||||
path: [1],
|
||||
node: { object: 'block', type: 'paragraph', nodes: [] }
|
||||
},
|
||||
{
|
||||
type: 'insert_node',
|
||||
path: [1, 0],
|
||||
node: { object: 'text', marks: [], text: 'hello!' }
|
||||
}
|
||||
]
|
||||
|
||||
expect(slateOps).toStrictEqual(expectedOps)
|
||||
})
|
||||
})
|
||||
@@ -20,6 +20,7 @@ const AnnotationSetOp = ({ key, value }: Automerge.Diff) => (map, doc) => {
|
||||
/**
|
||||
* Looks like set_annotation option is broken, temporary disabled
|
||||
*/
|
||||
|
||||
// if (!doc.annotations[key]) {
|
||||
op = {
|
||||
type: 'add_annotation',
|
||||
|
||||
@@ -51,7 +51,7 @@ export const removeCursor = (doc: SyncDoc, key: CursorKey) => {
|
||||
return doc
|
||||
}
|
||||
|
||||
export const cursorOpFilter = (ops: Immutable.List<Operation>, type: string) =>
|
||||
export const cursorOpFilter = (ops: any, type: string): any =>
|
||||
ops.filter(op => {
|
||||
if (op.type === 'set_annotation') {
|
||||
return !(
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import toSync from './toSync'
|
||||
import hexGen from './hexGen'
|
||||
|
||||
export * from './testUtils'
|
||||
|
||||
const toJS = node => {
|
||||
try {
|
||||
return JSON.parse(JSON.stringify(node))
|
||||
|
||||
24
packages/bridge/src/utils/testUtils.ts
Normal file
24
packages/bridge/src/utils/testUtils.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import * as Automerge from 'automerge'
|
||||
import { ValueJSON, TextJSON, NodeJSON } from 'slate'
|
||||
|
||||
export const createTextJSON = (text: string = ''): TextJSON => ({
|
||||
object: 'text',
|
||||
marks: [],
|
||||
text
|
||||
})
|
||||
|
||||
export const createParagraphJSON = (text: string = ''): NodeJSON => ({
|
||||
object: 'block',
|
||||
type: 'paragraph',
|
||||
nodes: [createTextJSON(text)]
|
||||
})
|
||||
|
||||
export const createValueJSON = (): ValueJSON => ({
|
||||
document: {
|
||||
nodes: [createParagraphJSON()]
|
||||
}
|
||||
})
|
||||
|
||||
export const createDoc = () => Automerge.from(createValueJSON())
|
||||
|
||||
export const cloneDoc = doc => Automerge.change(doc, '', d => d)
|
||||
Reference in New Issue
Block a user