feat: strart refactoring types

This commit is contained in:
cudr 2019-11-03 11:32:52 +03:00
parent a805d2476a
commit 0f98915909
7 changed files with 16 additions and 14 deletions

View File

@ -8,7 +8,7 @@ import annotation from './annotation'
const setSelection = doc => doc const setSelection = doc => doc
const setValue = doc => doc const setValue = doc => doc
const opType: any = { const opType = {
...text, ...text,
...annotation, ...annotation,
...node, ...node,

View File

@ -1,13 +1,13 @@
import * as Automerge from 'automerge' import * as Automerge from 'automerge'
import { toSlateOp } from './index' import { toSlateOp } from './index'
import { createDoc, cloneDoc, createBlockJSON, toJS } from '../utils' import { createDoc, cloneDoc, createBlockJSON } from '../utils'
describe('convert operations to slatejs model', () => { describe('convert operations to slatejs model', () => {
it('convert insert operations', () => { it('convert insert operations', () => {
const doc1 = createDoc() const doc1 = createDoc()
const doc2 = cloneDoc(doc1) const doc2 = cloneDoc(doc1)
const change = Automerge.change(doc1, 'change', (d: any) => { const change = Automerge.change(doc1, 'change', d => {
d.document.nodes.push(createBlockJSON('paragraph', 'hello!')) d.document.nodes.push(createBlockJSON('paragraph', 'hello!'))
d.document.nodes[1].nodes[0].text = 'hello!' d.document.nodes[1].nodes[0].text = 'hello!'
}) })
@ -33,7 +33,7 @@ describe('convert operations to slatejs model', () => {
}) })
it('convert remove operations', () => { it('convert remove operations', () => {
const doc1 = Automerge.change(createDoc(), 'change', (d: any) => { const doc1 = Automerge.change(createDoc(), 'change', d => {
d.document.nodes.push(createBlockJSON('paragraph', 'hello!')) d.document.nodes.push(createBlockJSON('paragraph', 'hello!'))
d.document.nodes.push(createBlockJSON('paragraph', 'hello twice!')) d.document.nodes.push(createBlockJSON('paragraph', 'hello twice!'))
d.document.nodes[1].nodes[0].text = 'hello!' d.document.nodes[1].nodes[0].text = 'hello!'
@ -41,7 +41,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 => {
delete d.document.nodes[1] delete d.document.nodes[1]
delete d.document.nodes[0].nodes[0] delete d.document.nodes[0].nodes[0]
}) })

View File

@ -12,7 +12,7 @@ const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({
const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => { const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => {
const ops = [] const ops = []
const inserate = ({ nodes, ...json }: any, path) => { const iterate = ({ nodes, ...json }, path) => {
const node = nodes ? { ...json, nodes: [] } : json const node = nodes ? { ...json, nodes: [] } : json
if (node.object) { if (node.object) {
@ -31,12 +31,12 @@ const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => {
} }
} }
nodes && nodes.forEach((n, i) => inserate(n, [...path, i])) nodes && nodes.forEach((n, i) => iterate(n, [...path, i]))
} }
const source = map[value] || (map[obj] && toJS(map[obj])) const source = map[value] || (map[obj] && toJS(map[obj]))
source && inserate(source, [...toSlatePath(path), index]) source && iterate(source, [...toSlatePath(path), index])
return ops return ops
} }

View File

@ -51,7 +51,7 @@ export const removeCursor = (doc: SyncDoc, key: CursorKey) => {
return doc return doc
} }
export const cursorOpFilter = (ops: any, type: string): any => export const cursorOpFilter = (ops, type: string) =>
ops.filter(op => { ops.filter(op => {
if (op.type === 'set_annotation') { if (op.type === 'set_annotation') {
return !( return !(

View File

@ -1,3 +1,5 @@
import { ValueJSON } from 'slate'
export type CursorKey = string export type CursorKey = string
export type SyncDoc = any export interface SyncDoc extends ValueJSON {}

View File

@ -1,5 +1,5 @@
import * as Automerge from 'automerge' import * as Automerge from 'automerge'
import { ValueJSON, TextJSON, NodeJSON } from 'slate' import { TextJSON } from 'slate'
export const createTextJSON = (text: string = ''): TextJSON => ({ export const createTextJSON = (text: string = ''): TextJSON => ({
object: 'text', object: 'text',
@ -10,13 +10,13 @@ export const createTextJSON = (text: string = ''): TextJSON => ({
export const createBlockJSON = ( export const createBlockJSON = (
type: string = 'paragraph', type: string = 'paragraph',
text: string = '' text: string = ''
): NodeJSON => ({ ) => ({
object: 'block', object: 'block',
type, type,
nodes: [createTextJSON(text)] nodes: [createTextJSON(text)]
}) })
export const createValueJSON = (): ValueJSON => ({ export const createValueJSON = () => ({
document: { document: {
nodes: [createBlockJSON()] nodes: [createBlockJSON()]
} }

View File

@ -1,6 +1,6 @@
import * as Automerge from 'automerge' import * as Automerge from 'automerge'
const toSync = (node: any): any => { const toSync = node => {
if (!node) { if (!node) {
return return
} }