mirror of
				https://github.com/cudr/slate-collaborative.git
				synced 2025-06-13 12:54:04 +00:00 
			
		
		
		
	Merge pull request #6 from cudr/refactoring_types
feat: refactoring types && handle annotation ops
This commit is contained in:
		
						commit
						fee0098c3d
					
				| @ -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 | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -8,7 +8,7 @@ import annotation from './annotation' | ||||
| const setSelection = doc => doc | ||||
| const setValue = doc => doc | ||||
| 
 | ||||
| const opType: any = { | ||||
| const opType = { | ||||
|   ...text, | ||||
|   ...annotation, | ||||
|   ...node, | ||||
|  | ||||
| @ -1,13 +1,13 @@ | ||||
| import * as Automerge from 'automerge' | ||||
| import { toSlateOp } from './index' | ||||
| import { createDoc, cloneDoc, createBlockJSON, toJS } from '../utils' | ||||
| import { createDoc, cloneDoc, createBlockJSON } 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) => { | ||||
|     const change = Automerge.change(doc1, 'change', d => { | ||||
|       d.document.nodes.push(createBlockJSON('paragraph', 'hello!')) | ||||
|       d.document.nodes[1].nodes[0].text = 'hello!' | ||||
|     }) | ||||
| @ -33,7 +33,7 @@ describe('convert operations to slatejs model', () => { | ||||
|   }) | ||||
| 
 | ||||
|   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 twice!')) | ||||
|       d.document.nodes[1].nodes[0].text = 'hello!' | ||||
| @ -41,7 +41,7 @@ describe('convert operations to slatejs model', () => { | ||||
| 
 | ||||
|     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[0].nodes[0] | ||||
|     }) | ||||
|  | ||||
| @ -12,7 +12,7 @@ const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({ | ||||
| const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => { | ||||
|   const ops = [] | ||||
| 
 | ||||
|   const inserate = ({ nodes, ...json }: any, path) => { | ||||
|   const iterate = ({ nodes, ...json }, path) => { | ||||
|     const node = nodes ? { ...json, nodes: [] } : json | ||||
| 
 | ||||
|     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])) | ||||
| 
 | ||||
|   source && inserate(source, [...toSlatePath(path), index]) | ||||
|   source && iterate(source, [...toSlatePath(path), index]) | ||||
| 
 | ||||
|   return ops | ||||
| } | ||||
|  | ||||
| @ -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' | ||||
| @ -51,7 +50,7 @@ export const removeCursor = (doc: SyncDoc, key: CursorKey) => { | ||||
|   return doc | ||||
| } | ||||
| 
 | ||||
| export const cursorOpFilter = (ops: any, type: string): any => | ||||
| export const cursorOpFilter = (ops, type: string) => | ||||
|   ops.filter(op => { | ||||
|     if (op.type === 'set_annotation') { | ||||
|       return !( | ||||
|  | ||||
| @ -1,3 +1,5 @@ | ||||
| import { ValueJSON } from 'slate' | ||||
| 
 | ||||
| export type CursorKey = string | ||||
| 
 | ||||
| export type SyncDoc = any | ||||
| export interface SyncDoc extends ValueJSON {} | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import * as Automerge from 'automerge' | ||||
| import { ValueJSON, TextJSON, NodeJSON } from 'slate' | ||||
| import { TextJSON } from 'slate' | ||||
| 
 | ||||
| export const createTextJSON = (text: string = ''): TextJSON => ({ | ||||
|   object: 'text', | ||||
| @ -10,13 +10,13 @@ export const createTextJSON = (text: string = ''): TextJSON => ({ | ||||
| export const createBlockJSON = ( | ||||
|   type: string = 'paragraph', | ||||
|   text: string = '' | ||||
| ): NodeJSON => ({ | ||||
| ) => ({ | ||||
|   object: 'block', | ||||
|   type, | ||||
|   nodes: [createTextJSON(text)] | ||||
| }) | ||||
| 
 | ||||
| export const createValueJSON = (): ValueJSON => ({ | ||||
| export const createValueJSON = () => ({ | ||||
|   document: { | ||||
|     nodes: [createBlockJSON()] | ||||
|   } | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import * as Automerge from 'automerge' | ||||
| 
 | ||||
| const toSync = (node: any): any => { | ||||
| const toSync = node => { | ||||
|   if (!node) { | ||||
|     return | ||||
|   } | ||||
|  | ||||
| @ -93,9 +93,7 @@ class Client extends Component<ClienProps> { | ||||
|     ) | ||||
|   } | ||||
| 
 | ||||
|   onChange = ({ value }: any) => { | ||||
|     this.setState({ value }) | ||||
|   } | ||||
|   onChange = ({ value }: any) => this.setState({ value }) | ||||
| 
 | ||||
|   onConnect = () => this.setState({ isOnline: true }) | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user