mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
feat: better definitions
This commit is contained in:
parent
fbcbef02af
commit
86e9e77410
@ -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
|
return doc
|
||||||
}
|
}
|
||||||
|
|
||||||
export const removeAnnotation = (doc: any, op: any) => {
|
export const removeAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||||
return doc
|
return doc
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setAnnotation = (doc: any, op: any) => {
|
export const setAnnotation = (doc: SyncDoc, op: Operation): SyncDoc => {
|
||||||
return doc
|
return doc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
import * as Automerge from 'automerge'
|
||||||
|
|
||||||
const createByType = type => (type === 'map' ? {} : type === 'list' ? [] : '')
|
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)
|
map[obj] = createByType(type)
|
||||||
|
|
||||||
return [map, ops]
|
return [map, ops]
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import * as Automerge from 'automerge'
|
||||||
|
|
||||||
import opInsert from './insert'
|
import opInsert from './insert'
|
||||||
import opRemove from './remove'
|
import opRemove from './remove'
|
||||||
import opSet from './set'
|
import opSet from './set'
|
||||||
@ -12,7 +14,7 @@ const byAction = {
|
|||||||
|
|
||||||
const rootKey = '00000000-0000-0000-0000-000000000000'
|
const rootKey = '00000000-0000-0000-0000-000000000000'
|
||||||
|
|
||||||
const toSlateOp = (ops, currentTree) => {
|
const toSlateOp = (ops: Automerge.Diff[], currentTree) => {
|
||||||
const iterate = (acc, op) => {
|
const iterate = (acc, op) => {
|
||||||
const action = byAction[op.action]
|
const action = byAction[op.action]
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import * as Automerge from 'automerge'
|
||||||
import { toSlatePath, toJS } from '../utils/index'
|
import { toSlatePath, toJS } from '../utils/index'
|
||||||
|
|
||||||
const insertTextOp = ({ index, path, value }) => () => ({
|
const insertTextOp = ({ index, path, value }: Automerge.Diff) => () => ({
|
||||||
type: 'insert_text',
|
type: 'insert_text',
|
||||||
path: toSlatePath(path),
|
path: toSlatePath(path),
|
||||||
offset: index,
|
offset: index,
|
||||||
@ -8,10 +9,10 @@ const insertTextOp = ({ index, path, value }) => () => ({
|
|||||||
marks: []
|
marks: []
|
||||||
})
|
})
|
||||||
|
|
||||||
const insertNodeOp = ({ value, obj, index, path }) => map => {
|
const insertNodeOp = ({ value, obj, index, path }: Automerge.Diff) => map => {
|
||||||
const ops = []
|
const ops = []
|
||||||
|
|
||||||
const insertRecoursive = ({ nodes, ...json }: any, path) => {
|
const inserate = ({ nodes, ...json }: any, path) => {
|
||||||
const node = nodes ? { ...json, nodes: [] } : json
|
const node = nodes ? { ...json, nodes: [] } : json
|
||||||
|
|
||||||
if (node.object === 'mark') {
|
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]))
|
const source = map[value] || (map[obj] && toJS(map[obj]))
|
||||||
|
|
||||||
source && insertRecoursive(source, [...toSlatePath(path), index])
|
source && inserate(source, [...toSlatePath(path), index])
|
||||||
|
|
||||||
return ops
|
return ops
|
||||||
}
|
}
|
||||||
@ -43,7 +44,7 @@ const insertByType = {
|
|||||||
list: insertNodeOp
|
list: insertNodeOp
|
||||||
}
|
}
|
||||||
|
|
||||||
const opInsert = (op, [map, ops]) => {
|
const opInsert = (op: Automerge.Diff, [map, ops]) => {
|
||||||
try {
|
try {
|
||||||
const { link, obj, path, index, type, value } = op
|
const { link, obj, path, index, type, value } = op
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import * as Automerge from 'automerge'
|
||||||
import { toSlatePath, toJS } from '../utils/index'
|
import { toSlatePath, toJS } from '../utils/index'
|
||||||
import { getTarget } from '../path'
|
import { getTarget } from '../path'
|
||||||
|
|
||||||
const removeTextOp = ({ index, path }) => () => ({
|
const removeTextOp = ({ index, path }: Automerge.Diff) => () => ({
|
||||||
type: 'remove_text',
|
type: 'remove_text',
|
||||||
path: toSlatePath(path).slice(0, path.length),
|
path: toSlatePath(path).slice(0, path.length),
|
||||||
offset: index,
|
offset: index,
|
||||||
@ -9,7 +10,7 @@ const removeTextOp = ({ index, path }) => () => ({
|
|||||||
marks: []
|
marks: []
|
||||||
})
|
})
|
||||||
|
|
||||||
const removeMarkOp = ({ path, index }) => (map, doc) => {
|
const removeMarkOp = ({ path, index }: Automerge.Diff) => (map, doc) => {
|
||||||
const slatePath = toSlatePath(path)
|
const slatePath = toSlatePath(path)
|
||||||
const target = getTarget(doc, slatePath)
|
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)
|
const slatePath = toSlatePath(path)
|
||||||
if (!map.hasOwnProperty(obj)) {
|
if (!map.hasOwnProperty(obj)) {
|
||||||
const target = getTarget(doc, [...slatePath, index] as any)
|
const target = getTarget(doc, [...slatePath, index] as any)
|
||||||
@ -45,7 +46,7 @@ const removeByType = {
|
|||||||
marks: removeMarkOp
|
marks: removeMarkOp
|
||||||
}
|
}
|
||||||
|
|
||||||
const opRemove = (op, [map, ops]) => {
|
const opRemove = (op: Automerge.Diff, [map, ops]) => {
|
||||||
try {
|
try {
|
||||||
const { index, path, obj } = op
|
const { index, path, obj } = op
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import * as Automerge from 'automerge'
|
||||||
import { toSlatePath, toJS } from '../utils/index'
|
import { toSlatePath, toJS } from '../utils/index'
|
||||||
|
|
||||||
const setDataOp = ({ path, value }) => map => ({
|
const setDataOp = ({ path, value }: Automerge.Diff) => map => ({
|
||||||
type: 'set_node',
|
type: 'set_node',
|
||||||
path: toSlatePath(path),
|
path: toSlatePath(path),
|
||||||
properties: {},
|
properties: {},
|
||||||
@ -13,7 +14,7 @@ const setByType = {
|
|||||||
data: setDataOp
|
data: setDataOp
|
||||||
}
|
}
|
||||||
|
|
||||||
const opSet = (op, [map, ops]) => {
|
const opSet = (op: Automerge.Diff, [map, ops]) => {
|
||||||
const { link, value, path, obj, key } = op
|
const { link, value, path, obj, key } = op
|
||||||
try {
|
try {
|
||||||
const set = setByType[key]
|
const set = setByType[key]
|
||||||
|
15
packages/bridge/src/utils/hexGen.ts
Normal file
15
packages/bridge/src/utils/hexGen.ts
Normal 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
|
@ -1,4 +1,5 @@
|
|||||||
import toSync from './toSync'
|
import toSync from './toSync'
|
||||||
|
import hexGen from './hexGen'
|
||||||
|
|
||||||
export const toJS = node => JSON.parse(JSON.stringify(node))
|
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)) : [])
|
const toSlatePath = path => (path ? path.filter(d => Number.isInteger(d)) : [])
|
||||||
|
|
||||||
export { toSync, toSlatePath }
|
export { toSync, toSlatePath, hexGen }
|
||||||
|
@ -35,8 +35,6 @@ class Connection {
|
|||||||
|
|
||||||
this.docSet = new Automerge.DocSet()
|
this.docSet = new Automerge.DocSet()
|
||||||
|
|
||||||
window['getDoc'] = () => toJS(this.docSet.getDoc(this.docId))
|
|
||||||
|
|
||||||
this.connect()
|
this.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +60,6 @@ class Connection {
|
|||||||
if (operations.length !== 0) {
|
if (operations.length !== 0) {
|
||||||
const slateOps = toSlateOp(operations, currentDoc)
|
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.remote = true
|
||||||
|
|
||||||
this.editor.withoutSaving(() => {
|
this.editor.withoutSaving(() => {
|
||||||
@ -84,8 +76,6 @@ class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
||||||
console.log('change slate ops!!!', operations.map(op => op.toJSON()).toJS())
|
|
||||||
|
|
||||||
const doc = this.docSet.getDoc(this.docId)
|
const doc = this.docSet.getDoc(this.docId)
|
||||||
const message = `change from ${this.socket.id}`
|
const message = `change from ${this.socket.id}`
|
||||||
|
|
||||||
@ -95,8 +85,6 @@ class Connection {
|
|||||||
applySlateOps(d, operations)
|
applySlateOps(d, operations)
|
||||||
)
|
)
|
||||||
|
|
||||||
// console.log('changed!!!', toJS(changed))
|
|
||||||
|
|
||||||
this.docSet.setDoc(this.docId, changed)
|
this.docSet.setDoc(this.docId, changed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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'
|
import Connection from './Connection'
|
||||||
|
|
||||||
@ -14,6 +17,8 @@ class Controller extends Component<ControllerProps> {
|
|||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const { editor, url, connectOpts } = this.props
|
const { editor, url, connectOpts } = this.props
|
||||||
|
|
||||||
|
KeyUtils.setGenerator(() => hexGen())
|
||||||
|
|
||||||
editor.connection = new Connection({
|
editor.connection = new Connection({
|
||||||
editor,
|
editor,
|
||||||
url,
|
url,
|
||||||
|
Loading…
Reference in New Issue
Block a user