From ad0aaf260755d0fcc0943df8291e28e72ada6e9a Mon Sep 17 00:00:00 2001 From: cudr Date: Sun, 10 Nov 2019 21:16:53 +0300 Subject: [PATCH] feat: handle annotations --- packages/bridge/src/apply/annotation.ts | 52 +++++++++++++++++++++---- packages/bridge/src/cursor/index.ts | 3 +- packages/example/src/Client.tsx | 4 +- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/packages/bridge/src/apply/annotation.ts b/packages/bridge/src/apply/annotation.ts index b31b595..359d0ca 100644 --- a/packages/bridge/src/apply/annotation.ts +++ b/packages/bridge/src/apply/annotation.ts @@ -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 } -export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { - // console.log('removeAnnotation!!!', op.toJS()) +export const removeAnnotation = ( + doc: SyncDoc, + op: RemoveAnnotationOperation +): SyncDoc => { + if (doc.annotations) { + delete doc.annotations[op.annotation.key] + } + return doc } -export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { - // console.log('setAnnotation!!!', op.toJS()) +export const setAnnotation = ( + 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 } diff --git a/packages/bridge/src/cursor/index.ts b/packages/bridge/src/cursor/index.ts index 9f048a9..fd191f6 100644 --- a/packages/bridge/src/cursor/index.ts +++ b/packages/bridge/src/cursor/index.ts @@ -1,5 +1,4 @@ -import { Operation, Selection } from 'slate' -import * as Immutable from 'immutable' +import { Selection } from 'slate' import merge from 'lodash/merge' import { toJS } from '../utils' diff --git a/packages/example/src/Client.tsx b/packages/example/src/Client.tsx index 6474476..4530d42 100644 --- a/packages/example/src/Client.tsx +++ b/packages/example/src/Client.tsx @@ -93,9 +93,7 @@ class Client extends Component { ) } - onChange = ({ value }: any) => { - this.setState({ value }) - } + onChange = ({ value }: any) => this.setState({ value }) onConnect = () => this.setState({ isOnline: true })