From 61c3b830dcf9a290292c4bfc2565f6d932dbbc28 Mon Sep 17 00:00:00 2001 From: cudr Date: Sun, 6 Oct 2019 00:24:52 +0300 Subject: [PATCH] feat: set data success --- package.json | 3 +- packages/bridge/src/convert/index.ts | 6 ++-- packages/bridge/src/convert/insert.ts | 44 ++++++++++++++++++++++++--- packages/bridge/src/convert/remove.ts | 17 ++++++++++- packages/bridge/src/convert/set.ts | 23 ++++++++++++-- packages/client/src/Connection.ts | 18 +++++++++-- 6 files changed, 97 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index d33d4e9..9ffa8d5 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/packages/bridge/src/convert/index.ts b/packages/bridge/src/convert/index.ts index 35a50cb..e751f58 100644 --- a/packages/bridge/src/convert/index.ts +++ b/packages/bridge/src/convert/index.ts @@ -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 } diff --git a/packages/bridge/src/convert/insert.ts b/packages/bridge/src/convert/insert.ts index 852d4bd..0707a05 100644 --- a/packages/bridge/src/convert/insert.ts +++ b/packages/bridge/src/convert/insert.ts @@ -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, diff --git a/packages/bridge/src/convert/remove.ts b/packages/bridge/src/convert/remove.ts index d6bbba0..6e18490 100644 --- a/packages/bridge/src/convert/remove.ts +++ b/packages/bridge/src/convert/remove.ts @@ -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]) => { diff --git a/packages/bridge/src/convert/set.ts b/packages/bridge/src/convert/set.ts index 3193e2c..a0c9d9b 100644 --- a/packages/bridge/src/convert/set.ts +++ b/packages/bridge/src/convert/set.ts @@ -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) { diff --git a/packages/client/src/Connection.ts b/packages/client/src/Connection.ts index a26c644..5fb9d69 100644 --- a/packages/client/src/Connection.ts +++ b/packages/client/src/Connection.ts @@ -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) => { + 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) }