From 86e9e774103638819aaf734b616d91b2bebeedb9 Mon Sep 17 00:00:00 2001 From: cudr Date: Sun, 6 Oct 2019 23:56:27 +0300 Subject: [PATCH] feat: better definitions --- packages/bridge/src/apply/annotation.ts | 8 +++++--- packages/bridge/src/convert/create.ts | 4 +++- packages/bridge/src/convert/index.ts | 4 +++- packages/bridge/src/convert/insert.ts | 13 +++++++------ packages/bridge/src/convert/remove.ts | 9 +++++---- packages/bridge/src/convert/set.ts | 5 +++-- packages/bridge/src/utils/hexGen.ts | 15 +++++++++++++++ packages/bridge/src/utils/index.ts | 3 ++- packages/client/src/Connection.ts | 12 ------------ packages/client/src/Controller.tsx | 7 ++++++- 10 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 packages/bridge/src/utils/hexGen.ts diff --git a/packages/bridge/src/apply/annotation.ts b/packages/bridge/src/apply/annotation.ts index fdeea58..083b660 100644 --- a/packages/bridge/src/apply/annotation.ts +++ b/packages/bridge/src/apply/annotation.ts @@ -1,12 +1,14 @@ -export const addAnnotation = (doc: any, op: any) => { +import { Operation, SyncDoc } from '../model' + +export const addAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { return doc } -export const removeAnnotation = (doc: any, op: any) => { +export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { return doc } -export const setAnnotation = (doc: any, op: any) => { +export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => { return doc } diff --git a/packages/bridge/src/convert/create.ts b/packages/bridge/src/convert/create.ts index 7501304..9256fc4 100644 --- a/packages/bridge/src/convert/create.ts +++ b/packages/bridge/src/convert/create.ts @@ -1,6 +1,8 @@ +import * as Automerge from 'automerge' + const createByType = type => (type === 'map' ? {} : type === 'list' ? [] : '') -const opCreate = ({ obj, type }, [map, ops]) => { +const opCreate = ({ obj, type }: Automerge.Diff, [map, ops]) => { map[obj] = createByType(type) return [map, ops] diff --git a/packages/bridge/src/convert/index.ts b/packages/bridge/src/convert/index.ts index e751f58..7dc0321 100644 --- a/packages/bridge/src/convert/index.ts +++ b/packages/bridge/src/convert/index.ts @@ -1,3 +1,5 @@ +import * as Automerge from 'automerge' + import opInsert from './insert' import opRemove from './remove' import opSet from './set' @@ -12,7 +14,7 @@ const byAction = { const rootKey = '00000000-0000-0000-0000-000000000000' -const toSlateOp = (ops, currentTree) => { +const toSlateOp = (ops: Automerge.Diff[], currentTree) => { const iterate = (acc, op) => { const action = byAction[op.action] diff --git a/packages/bridge/src/convert/insert.ts b/packages/bridge/src/convert/insert.ts index b16ffa5..fd2a508 100644 --- a/packages/bridge/src/convert/insert.ts +++ b/packages/bridge/src/convert/insert.ts @@ -1,6 +1,7 @@ +import * as Automerge from 'automerge' import { toSlatePath, toJS } from '../utils/index' -const insertTextOp = ({ index, path, value }) => () => ({ +const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({ type: 'insert_text', path: toSlatePath(path), offset: index, @@ -8,10 +9,10 @@ const insertTextOp = ({ index, path, value }) => () => ({ marks: [] }) -const insertNodeOp = ({ value, obj, index, path }) => map => { +const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => { const ops = [] - const insertRecoursive = ({ nodes, ...json }: any, path) => { + const inserate = ({ nodes, ...json }: any, path) => { const node = nodes ? { ...json, nodes: [] } : json if (node.object === 'mark') { @@ -28,12 +29,12 @@ const insertNodeOp = ({ value, obj, index, path }) => map => { }) } - nodes && nodes.forEach((n, i) => insertRecoursive(n, [...path, i])) + nodes && nodes.forEach((n, i) => inserate(n, [...path, i])) } const source = map[value] || (map[obj] && toJS(map[obj])) - source && insertRecoursive(source, [...toSlatePath(path), index]) + source && inserate(source, [...toSlatePath(path), index]) return ops } @@ -43,7 +44,7 @@ const insertByType = { list: insertNodeOp } -const opInsert = (op, [map, ops]) => { +const opInsert = (op: Automerge.Diff, [map, ops]) => { try { const { link, obj, path, index, type, value } = op diff --git a/packages/bridge/src/convert/remove.ts b/packages/bridge/src/convert/remove.ts index 29b67f4..a768cdf 100644 --- a/packages/bridge/src/convert/remove.ts +++ b/packages/bridge/src/convert/remove.ts @@ -1,7 +1,8 @@ +import * as Automerge from 'automerge' import { toSlatePath, toJS } from '../utils/index' import { getTarget } from '../path' -const removeTextOp = ({ index, path }) => () => ({ +const removeTextOp = ({ index, path }: Automerge.Diff) => () => ({ type: 'remove_text', path: toSlatePath(path).slice(0, path.length), offset: index, @@ -9,7 +10,7 @@ const removeTextOp = ({ index, path }) => () => ({ marks: [] }) -const removeMarkOp = ({ path, index }) => (map, doc) => { +const removeMarkOp = ({ path, index }: Automerge.Diff) => (map, doc) => { const slatePath = toSlatePath(path) const target = getTarget(doc, slatePath) @@ -22,7 +23,7 @@ const removeMarkOp = ({ path, index }) => (map, doc) => { } } -const removeNodesOp = ({ index, obj, path }) => (map, doc) => { +const removeNodesOp = ({ index, obj, path }: Automerge.Diff) => (map, doc) => { const slatePath = toSlatePath(path) if (!map.hasOwnProperty(obj)) { const target = getTarget(doc, [...slatePath, index] as any) @@ -45,7 +46,7 @@ const removeByType = { marks: removeMarkOp } -const opRemove = (op, [map, ops]) => { +const opRemove = (op: Automerge.Diff, [map, ops]) => { try { const { index, path, obj } = op diff --git a/packages/bridge/src/convert/set.ts b/packages/bridge/src/convert/set.ts index e729257..8189597 100644 --- a/packages/bridge/src/convert/set.ts +++ b/packages/bridge/src/convert/set.ts @@ -1,6 +1,7 @@ +import * as Automerge from 'automerge' import { toSlatePath, toJS } from '../utils/index' -const setDataOp = ({ path, value }) => map => ({ +const setDataOp = ({ path, value }: Automerge.Diff) => map => ({ type: 'set_node', path: toSlatePath(path), properties: {}, @@ -13,7 +14,7 @@ const setByType = { data: setDataOp } -const opSet = (op, [map, ops]) => { +const opSet = (op: Automerge.Diff, [map, ops]) => { const { link, value, path, obj, key } = op try { const set = setByType[key] diff --git a/packages/bridge/src/utils/hexGen.ts b/packages/bridge/src/utils/hexGen.ts new file mode 100644 index 0000000..86fa08c --- /dev/null +++ b/packages/bridge/src/utils/hexGen.ts @@ -0,0 +1,15 @@ +export type Hex = string + +const hexGen = (len: number = 12): Hex => { + const maxlen = 8 + const min = Math.pow(16, Math.min(len, maxlen) - 1) + const max = Math.pow(16, Math.min(len, maxlen)) - 1 + const n = Math.floor(Math.random() * (max - min + 1)) + min + let r = n.toString(16) + while (r.length < len) { + r = r + hexGen(len - maxlen) + } + return r +} + +export default hexGen diff --git a/packages/bridge/src/utils/index.ts b/packages/bridge/src/utils/index.ts index 19b47f6..e33660e 100644 --- a/packages/bridge/src/utils/index.ts +++ b/packages/bridge/src/utils/index.ts @@ -1,4 +1,5 @@ import toSync from './toSync' +import hexGen from './hexGen' export const toJS = node => JSON.parse(JSON.stringify(node)) @@ -6,4 +7,4 @@ export const cloneNode = node => toSync(toJS(node)) const toSlatePath = path => (path ? path.filter(d => Number.isInteger(d)) : []) -export { toSync, toSlatePath } +export { toSync, toSlatePath, hexGen } diff --git a/packages/client/src/Connection.ts b/packages/client/src/Connection.ts index f59f523..6971d33 100644 --- a/packages/client/src/Connection.ts +++ b/packages/client/src/Connection.ts @@ -35,8 +35,6 @@ class Connection { this.docSet = new Automerge.DocSet() - window['getDoc'] = () => toJS(this.docSet.getDoc(this.docId)) - this.connect() } @@ -62,12 +60,6 @@ class Connection { if (operations.length !== 0) { const slateOps = toSlateOp(operations, currentDoc) - // console.log('start key', KeyUtils.create()) - - console.log('operations', operations, slateOps) - - // console.log('this.editor', this.editor) - this.editor.remote = true this.editor.withoutSaving(() => { @@ -84,8 +76,6 @@ class Connection { } receiveSlateOps = (operations: Immutable.List) => { - console.log('change slate ops!!!', operations.map(op => op.toJSON()).toJS()) - const doc = this.docSet.getDoc(this.docId) const message = `change from ${this.socket.id}` @@ -95,8 +85,6 @@ class Connection { applySlateOps(d, operations) ) - // console.log('changed!!!', toJS(changed)) - this.docSet.setDoc(this.docId, changed) } diff --git a/packages/client/src/Controller.tsx b/packages/client/src/Controller.tsx index a62a3f3..da41219 100644 --- a/packages/client/src/Controller.tsx +++ b/packages/client/src/Controller.tsx @@ -1,4 +1,7 @@ -import React, { Component, ReactNode } from 'react' +import React, { Component } from 'react' +import { KeyUtils } from 'slate' + +import { hexGen } from '@slate-collaborative/bridge' import Connection from './Connection' @@ -14,6 +17,8 @@ class Controller extends Component { componentDidMount() { const { editor, url, connectOpts } = this.props + KeyUtils.setGenerator(() => hexGen()) + editor.connection = new Connection({ editor, url,