feat: better definitions

This commit is contained in:
cudr
2019-10-06 23:56:27 +03:00
parent fbcbef02af
commit 86e9e77410
10 changed files with 49 additions and 31 deletions

View File

@@ -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
}

View File

@@ -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]

View File

@@ -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]

View File

@@ -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

View File

@@ -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

View File

@@ -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]

View File

@@ -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

View File

@@ -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 }