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": {
|
"scripts": {
|
||||||
"bootstrap": "lerna bootstrap",
|
"bootstrap": "lerna bootstrap",
|
||||||
"release": "yarn prebuild && yarn build && lerna publish from-package",
|
"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",
|
"build": "lerna run build --stream",
|
||||||
|
"watch": "lerna run --parallel watch",
|
||||||
"prebuild": "rm -rf ./packages/**/lib/",
|
"prebuild": "rm -rf ./packages/**/lib/",
|
||||||
"format": "prettier --write"
|
"format": "prettier --write"
|
||||||
},
|
},
|
||||||
|
@ -12,7 +12,7 @@ const byAction = {
|
|||||||
|
|
||||||
const rootKey = '00000000-0000-0000-0000-000000000000'
|
const rootKey = '00000000-0000-0000-0000-000000000000'
|
||||||
|
|
||||||
const toSlateOp = ops => {
|
const toSlateOp = (ops, currentTree) => {
|
||||||
const iterate = (acc, op) => {
|
const iterate = (acc, op) => {
|
||||||
const action = byAction[op.action]
|
const action = byAction[op.action]
|
||||||
|
|
||||||
@ -21,14 +21,14 @@ const toSlateOp = ops => {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
const [tree, defer] = ops.reduce(iterate, [
|
const [tempTree, defer] = ops.reduce(iterate, [
|
||||||
{
|
{
|
||||||
[rootKey]: {}
|
[rootKey]: {}
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
])
|
])
|
||||||
|
|
||||||
return defer.map(op => op(tree))
|
return defer.flatMap(op => op(tempTree, currentTree))
|
||||||
}
|
}
|
||||||
|
|
||||||
export { toSlateOp }
|
export { toSlateOp }
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { Block, Text } from 'slate'
|
||||||
import { toSlatePath, toJS } from '../utils/index'
|
import { toSlatePath, toJS } from '../utils/index'
|
||||||
|
|
||||||
const insertTextOp = ({ index, path, value }) => () => ({
|
const insertTextOp = ({ index, path, value }) => () => ({
|
||||||
@ -8,11 +9,44 @@ const insertTextOp = ({ index, path, value }) => () => ({
|
|||||||
marks: []
|
marks: []
|
||||||
})
|
})
|
||||||
|
|
||||||
const insertNodeOp = ({ value, index, path }) => map => ({
|
const insertNodeOp = ({ value, index, path }) => map => {
|
||||||
type: 'insert_node',
|
const ops = []
|
||||||
path: [...toSlatePath(path), index],
|
|
||||||
node: map[value]
|
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 = {
|
const insertByType = {
|
||||||
text: insertTextOp,
|
text: insertTextOp,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { toSlatePath, toJS } from '../utils/index'
|
import { toSlatePath, toJS } from '../utils/index'
|
||||||
|
import { getTarget } from '../path'
|
||||||
|
|
||||||
const removeTextOp = ({ index, path }) => () => ({
|
const removeTextOp = ({ index, path }) => () => ({
|
||||||
type: 'remove_text',
|
type: 'remove_text',
|
||||||
@ -8,6 +9,19 @@ const removeTextOp = ({ index, path }) => () => ({
|
|||||||
marks: []
|
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 removeNodesOp = ({ index, path }) => () => {
|
||||||
const nPath = toSlatePath(path)
|
const nPath = toSlatePath(path)
|
||||||
return {
|
return {
|
||||||
@ -21,7 +35,8 @@ const removeNodesOp = ({ index, path }) => () => {
|
|||||||
|
|
||||||
const removeByType = {
|
const removeByType = {
|
||||||
text: removeTextOp,
|
text: removeTextOp,
|
||||||
nodes: removeNodesOp
|
nodes: removeNodesOp,
|
||||||
|
marks: removeMarkOp
|
||||||
}
|
}
|
||||||
|
|
||||||
const opRemove = (op, [map, ops]) => {
|
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 opSet = (op, [map, ops]) => {
|
||||||
const { link, value, obj, key } = op
|
const { link, value, obj, key } = op
|
||||||
try {
|
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]
|
return [map, ops]
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -58,12 +58,22 @@ class Connection {
|
|||||||
const operations = Automerge.diff(currentDoc, docNew)
|
const operations = Automerge.diff(currentDoc, docNew)
|
||||||
|
|
||||||
if (operations.length !== 0) {
|
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.remote = true
|
||||||
|
|
||||||
this.editor.withoutSaving(() => {
|
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)
|
setTimeout(() => (this.editor.remote = false), 5)
|
||||||
@ -74,6 +84,8 @@ class Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
receiveSlateOps = (operations: Immutable.List<Operation>) => {
|
||||||
|
console.log('change slate ops!!!', operations.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}`
|
||||||
|
|
||||||
@ -83,6 +95,8 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user