Merge pull request #6 from cudr/refactoring_types

feat: refactoring types && handle annotation ops
This commit is contained in:
George 2019-11-10 21:27:55 +03:00 committed by GitHub
commit fee0098c3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 27 deletions

View File

@ -1,19 +1,55 @@
import { Operation, SyncDoc } from '../model/index' import { SyncDoc } from '../model/index'
import { toSync } from '../utils'
import {
AddAnnotationOperation,
RemoveAnnotationOperation,
SetAnnotationOperation
} from 'slate'
// TODO: handle annotation ops export const addAnnotation = (
doc: SyncDoc,
op: AddAnnotationOperation
): SyncDoc => {
if (!doc.annotations) {
doc['annotations'] = {}
}
const annotation = op.annotation.toJSON()
doc.annotations[annotation.key] = toSync(annotation)
export const addAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
// console.log('addAnnotation!!!', op.toJS())
return doc return doc
} }
export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { export const removeAnnotation = (
// console.log('removeAnnotation!!!', op.toJS()) doc: SyncDoc,
op: RemoveAnnotationOperation
): SyncDoc => {
if (doc.annotations) {
delete doc.annotations[op.annotation.key]
}
return doc return doc
} }
export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { export const setAnnotation = (
// console.log('setAnnotation!!!', op.toJS()) doc: SyncDoc,
op: SetAnnotationOperation
): SyncDoc => {
/**
* Looks like set_annotation option is broken, temporary disabled
*/
// const { newProperties }: any = op.toJSON()
// if (!doc.annotations || !newProperties) return doc
// if (!doc.annotations[newProperties.key]) {
// return addAnnotation(doc, newProperties)
// } else {
// doc.annotations[newProperties.key] = { ...doc.annotations[newProperties.key], ...newProperties }
// }
return doc return doc
} }

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

@ -1,5 +1,4 @@
import { Operation, Selection } from 'slate' import { Selection } from 'slate'
import * as Immutable from 'immutable'
import merge from 'lodash/merge' import merge from 'lodash/merge'
import { toJS } from '../utils' import { toJS } from '../utils'
@ -51,7 +50,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
} }

View File

@ -93,9 +93,7 @@ class Client extends Component<ClienProps> {
) )
} }
onChange = ({ value }: any) => { onChange = ({ value }: any) => this.setState({ value })
this.setState({ value })
}
onConnect = () => this.setState({ isOnline: true }) onConnect = () => this.setState({ isOnline: true })