mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
feat: try to use inner annotations model to cursors
This commit is contained in:
@@ -30,7 +30,11 @@ const toSlateOp = (ops: Automerge.Diff[], currentTree) => {
|
||||
[]
|
||||
])
|
||||
|
||||
return defer.flatMap(op => op(tempTree, currentTree))
|
||||
const res = defer.flatMap(op => op(tempTree, currentTree))
|
||||
|
||||
console.log('toSlate@@@', ops, res)
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
export { toSlateOp }
|
||||
|
||||
@@ -52,7 +52,7 @@ const opInsert = (op: Automerge.Diff, [map, ops]) => {
|
||||
|
||||
if (link && map[obj]) {
|
||||
map[obj].splice(index, 0, map[value] || value)
|
||||
} else if (type === 'text' && !path) {
|
||||
} else if ((type === 'text' || type === 'list') && !path) {
|
||||
map[obj] = map[obj]
|
||||
? map[obj]
|
||||
.slice(0, index)
|
||||
|
||||
@@ -40,10 +40,18 @@ const removeNodesOp = ({ index, obj, path }: Automerge.Diff) => (map, doc) => {
|
||||
}
|
||||
}
|
||||
|
||||
const removeAnnotationOp = ({ key }: Automerge.Diff) => (map, doc) => {
|
||||
return {
|
||||
type: 'remove_annotation',
|
||||
annotation: toJS(doc.annotations[key])
|
||||
}
|
||||
}
|
||||
|
||||
const removeByType = {
|
||||
text: removeTextOp,
|
||||
nodes: removeNodesOp,
|
||||
marks: removeMarkOp
|
||||
marks: removeMarkOp,
|
||||
annotations: removeAnnotationOp
|
||||
}
|
||||
|
||||
const opRemove = (op: Automerge.Diff, [map, ops]) => {
|
||||
|
||||
@@ -10,6 +10,31 @@ const setDataOp = ({ path, value }: Automerge.Diff) => map => ({
|
||||
}
|
||||
})
|
||||
|
||||
const AnnotationSetOp = ({ key, value }: Automerge.Diff) => (map, doc) => {
|
||||
if (!doc.annotations) {
|
||||
doc.annotations = {}
|
||||
}
|
||||
|
||||
let op
|
||||
|
||||
if (!doc.annotations[key]) {
|
||||
op = {
|
||||
type: 'add_annotation',
|
||||
annotation: map[value]
|
||||
}
|
||||
} else {
|
||||
op = {
|
||||
type: 'set_annotation',
|
||||
properties: doc.annotations[key],
|
||||
newProperties: map[value]
|
||||
}
|
||||
}
|
||||
|
||||
console.log('opSET!!', key, map[value], op)
|
||||
|
||||
return op
|
||||
}
|
||||
|
||||
const setByType = {
|
||||
data: setDataOp
|
||||
}
|
||||
@@ -25,6 +50,13 @@ const opSet = (op: Automerge.Diff, [map, ops]) => {
|
||||
map[obj][key] = link ? map[value] : value
|
||||
}
|
||||
|
||||
/**
|
||||
* Annotation
|
||||
*/
|
||||
if (path && path.length === 1 && path[0] === 'annotations') {
|
||||
ops.push(AnnotationSetOp(op))
|
||||
}
|
||||
|
||||
return [map, ops]
|
||||
} catch (e) {
|
||||
console.error(e, op, toJS(map))
|
||||
|
||||
Reference in New Issue
Block a user