mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
feat: set data success
This commit is contained in:
parent
e17e5f0fc3
commit
61c3b830dc
@ -5,8 +5,9 @@
|
||||
"scripts": {
|
||||
"bootstrap": "lerna bootstrap",
|
||||
"release": "yarn prebuild && yarn build && lerna publish from-package",
|
||||
"dev": "concurrently \"lerna run --parallel watch\" \"lerna run dev --stream\"",
|
||||
"dev": "concurrently \"yarn watch\" \"lerna run dev --stream\"",
|
||||
"build": "lerna run build --stream",
|
||||
"watch": "lerna run --parallel watch",
|
||||
"prebuild": "rm -rf ./packages/**/lib/",
|
||||
"format": "prettier --write"
|
||||
},
|
||||
|
@ -12,7 +12,7 @@ const byAction = {
|
||||
|
||||
const rootKey = '00000000-0000-0000-0000-000000000000'
|
||||
|
||||
const toSlateOp = ops => {
|
||||
const toSlateOp = (ops, currentTree) => {
|
||||
const iterate = (acc, op) => {
|
||||
const action = byAction[op.action]
|
||||
|
||||
@ -21,14 +21,14 @@ const toSlateOp = ops => {
|
||||
return result
|
||||
}
|
||||
|
||||
const [tree, defer] = ops.reduce(iterate, [
|
||||
const [tempTree, defer] = ops.reduce(iterate, [
|
||||
{
|
||||
[rootKey]: {}
|
||||
},
|
||||
[]
|
||||
])
|
||||
|
||||
return defer.map(op => op(tree))
|
||||
return defer.flatMap(op => op(tempTree, currentTree))
|
||||
}
|
||||
|
||||
export { toSlateOp }
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { Block, Text } from 'slate'
|
||||
import { toSlatePath, toJS } from '../utils/index'
|
||||
|
||||
const insertTextOp = ({ index, path, value }) => () => ({
|
||||
@ -8,11 +9,44 @@ const insertTextOp = ({ index, path, value }) => () => ({
|
||||
marks: []
|
||||
})
|
||||
|
||||
const insertNodeOp = ({ value, index, path }) => map => ({
|
||||
type: 'insert_node',
|
||||
path: [...toSlatePath(path), index],
|
||||
node: map[value]
|
||||
})
|
||||
const insertNodeOp = ({ value, index, path }) => map => {
|
||||
const ops = []
|
||||
|
||||
const insertRecoursive = ({ nodes, ...json }: any, path) => {
|
||||
const node = nodes
|
||||
? Block.fromJSON({ ...json, nodes: [] })
|
||||
: Text.fromJSON(json)
|
||||
|
||||
ops.push({
|
||||
type: 'insert_node',
|
||||
path,
|
||||
node
|
||||
})
|
||||
|
||||
nodes && nodes.forEach((n, i) => insertRecoursive(n, [...path, i]))
|
||||
}
|
||||
|
||||
insertRecoursive(map[value], [...toSlatePath(path), index])
|
||||
|
||||
return ops
|
||||
}
|
||||
|
||||
// let count = 4000
|
||||
|
||||
// const insertNodeOp = ({ value, index, path }) => map => {
|
||||
// const node = map[value]
|
||||
|
||||
// if (!node) return null
|
||||
|
||||
// count += 1
|
||||
|
||||
// return {
|
||||
// type: 'insert_node',
|
||||
// path: [...toSlatePath(path), index],
|
||||
// node, //: { ...node, key: count },
|
||||
// data: {}
|
||||
// }
|
||||
// }
|
||||
|
||||
const insertByType = {
|
||||
text: insertTextOp,
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { toSlatePath, toJS } from '../utils/index'
|
||||
import { getTarget } from '../path'
|
||||
|
||||
const removeTextOp = ({ index, path }) => () => ({
|
||||
type: 'remove_text',
|
||||
@ -8,6 +9,19 @@ const removeTextOp = ({ index, path }) => () => ({
|
||||
marks: []
|
||||
})
|
||||
|
||||
const removeMarkOp = ({ path, index }) => (map, doc) => {
|
||||
const slatePath = toSlatePath(path)
|
||||
const target = getTarget(doc, slatePath)
|
||||
|
||||
return {
|
||||
type: 'remove_mark',
|
||||
path: slatePath,
|
||||
mark: {
|
||||
type: target.marks[index].type
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const removeNodesOp = ({ index, path }) => () => {
|
||||
const nPath = toSlatePath(path)
|
||||
return {
|
||||
@ -21,7 +35,8 @@ const removeNodesOp = ({ index, path }) => () => {
|
||||
|
||||
const removeByType = {
|
||||
text: removeTextOp,
|
||||
nodes: removeNodesOp
|
||||
nodes: removeNodesOp,
|
||||
marks: removeMarkOp
|
||||
}
|
||||
|
||||
const opRemove = (op, [map, ops]) => {
|
||||
|
@ -1,9 +1,28 @@
|
||||
import { toJS } from '../utils/index'
|
||||
import { toSlatePath, toJS } from '../utils/index'
|
||||
|
||||
const setData = ({ path, value }) => map => ({
|
||||
type: 'set_node',
|
||||
path: toSlatePath(path),
|
||||
properties: {},
|
||||
newProperties: {
|
||||
data: map[value]
|
||||
}
|
||||
})
|
||||
|
||||
const setByType = {
|
||||
data: setData
|
||||
}
|
||||
|
||||
const opSet = (op, [map, ops]) => {
|
||||
const { link, value, obj, key } = op
|
||||
try {
|
||||
map[obj][key] = link ? map[value] : value
|
||||
const set = setByType[key]
|
||||
|
||||
if (set) {
|
||||
ops.push(set(op))
|
||||
} else {
|
||||
map[obj][key] = link ? map[value] : value
|
||||
}
|
||||
|
||||
return [map, ops]
|
||||
} catch (e) {
|
||||
|
@ -58,12 +58,22 @@ class Connection {
|
||||
const operations = Automerge.diff(currentDoc, docNew)
|
||||
|
||||
if (operations.length !== 0) {
|
||||
const slateOps = toSlateOp(operations, this.connectOpts.query.name)
|
||||
const slateOps = toSlateOp(operations, currentDoc)
|
||||
|
||||
console.log('operations', operations, slateOps)
|
||||
|
||||
console.log('this.editor', this.editor)
|
||||
|
||||
this.editor.remote = true
|
||||
|
||||
this.editor.withoutSaving(() => {
|
||||
slateOps.forEach(o => this.editor.applyOperation(o))
|
||||
slateOps.forEach(o => {
|
||||
this.editor.applyOperation(o)
|
||||
|
||||
if (o.type === 'insert_node') {
|
||||
o.node.regenerateKey()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
setTimeout(() => (this.editor.remote = false), 5)
|
||||
@ -74,6 +84,8 @@ class Connection {
|
||||
}
|
||||
|
||||
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
||||
console.log('change slate ops!!!', operations.toJS())
|
||||
|
||||
const doc = this.docSet.getDoc(this.docId)
|
||||
const message = `change from ${this.socket.id}`
|
||||
|
||||
@ -83,6 +95,8 @@ class Connection {
|
||||
applySlateOps(d, operations)
|
||||
)
|
||||
|
||||
console.log('changed!!!', toJS(changed))
|
||||
|
||||
this.docSet.setDoc(this.docId, changed)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user