mirror of
https://github.com/cudr/slate-collaborative.git
synced 2026-03-02 03:40:18 +00:00
initial commit
This commit is contained in:
9
packages/bridge/src/convert/create.ts
Normal file
9
packages/bridge/src/convert/create.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
const createByType = type => (type === 'map' ? {} : type === 'list' ? [] : '')
|
||||
|
||||
const opCreate = ({ obj, type }, [map, ops]) => {
|
||||
map[obj] = createByType(type)
|
||||
|
||||
return [map, ops]
|
||||
}
|
||||
|
||||
export default opCreate
|
||||
34
packages/bridge/src/convert/index.ts
Normal file
34
packages/bridge/src/convert/index.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import opInsert from './insert'
|
||||
import opRemove from './remove'
|
||||
import opSet from './set'
|
||||
import opCreate from './create'
|
||||
|
||||
const byAction = {
|
||||
create: opCreate,
|
||||
remove: opRemove,
|
||||
set: opSet,
|
||||
insert: opInsert
|
||||
}
|
||||
|
||||
const rootKey = '00000000-0000-0000-0000-000000000000'
|
||||
|
||||
const toSlateOp = ops => {
|
||||
const iterate = (acc, op) => {
|
||||
const action = byAction[op.action]
|
||||
|
||||
const result = action ? action(op, acc) : acc
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
const [tree, defer] = ops.reduce(iterate, [
|
||||
{
|
||||
[rootKey]: {}
|
||||
},
|
||||
[]
|
||||
])
|
||||
|
||||
return defer.map(op => op(tree))
|
||||
}
|
||||
|
||||
export { toSlateOp }
|
||||
51
packages/bridge/src/convert/insert.ts
Normal file
51
packages/bridge/src/convert/insert.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { toSlatePath, toJS } from '../utils/index'
|
||||
|
||||
const insertTextOp = ({ index, path, value }) => () => ({
|
||||
type: 'insert_text',
|
||||
path: toSlatePath(path),
|
||||
offset: index,
|
||||
text: value,
|
||||
marks: []
|
||||
})
|
||||
|
||||
const insertNodeOp = ({ value, index, path }) => map => ({
|
||||
type: 'insert_node',
|
||||
path: [...toSlatePath(path), index],
|
||||
node: map[value]
|
||||
})
|
||||
|
||||
const insertByType = {
|
||||
text: insertTextOp,
|
||||
list: insertNodeOp
|
||||
}
|
||||
|
||||
const opInsert = (op, [map, ops]) => {
|
||||
try {
|
||||
const { link, obj, path, index, type, value } = op
|
||||
|
||||
if (link && map[obj]) {
|
||||
map[obj].splice(index, 0, map[value] || value)
|
||||
} else if (type === 'text' && !path) {
|
||||
map[obj] = map[obj]
|
||||
? map[obj]
|
||||
.slice(0, index)
|
||||
.concat(value)
|
||||
.concat(map[obj].slice(index))
|
||||
: value
|
||||
} else {
|
||||
const insert = insertByType[type]
|
||||
|
||||
const operation = insert && insert(op, map)
|
||||
|
||||
ops.push(operation)
|
||||
}
|
||||
|
||||
return [map, ops]
|
||||
} catch (e) {
|
||||
console.error(e, op, toJS(map))
|
||||
|
||||
return [map, ops]
|
||||
}
|
||||
}
|
||||
|
||||
export default opInsert
|
||||
49
packages/bridge/src/convert/remove.ts
Normal file
49
packages/bridge/src/convert/remove.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { toSlatePath, toJS } from '../utils/index'
|
||||
|
||||
const removeTextOp = ({ index, path }) => () => ({
|
||||
type: 'remove_text',
|
||||
path: toSlatePath(path).slice(0, path.length),
|
||||
offset: index,
|
||||
text: '*',
|
||||
marks: []
|
||||
})
|
||||
|
||||
const removeNodesOp = ({ index, path }) => () => {
|
||||
const nPath = toSlatePath(path)
|
||||
return {
|
||||
type: 'remove_node',
|
||||
path: nPath.length ? nPath.concat(index) : [index],
|
||||
node: {
|
||||
object: 'text'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const removeByType = {
|
||||
text: removeTextOp,
|
||||
nodes: removeNodesOp
|
||||
}
|
||||
|
||||
const opRemove = (op, [map, ops]) => {
|
||||
try {
|
||||
const { index, path, obj } = op
|
||||
|
||||
if (map.hasOwnProperty(obj) && op.type !== 'text') {
|
||||
map[obj].splice(index, 1)
|
||||
|
||||
return [map, ops]
|
||||
}
|
||||
|
||||
if (!path) return [map, ops]
|
||||
|
||||
const fn = removeByType[path[path.length - 1]]
|
||||
|
||||
return [map, [...ops, fn(op)]]
|
||||
} catch (e) {
|
||||
console.error(e, op, toJS(map))
|
||||
|
||||
return [map, ops]
|
||||
}
|
||||
}
|
||||
|
||||
export default opRemove
|
||||
16
packages/bridge/src/convert/set.ts
Normal file
16
packages/bridge/src/convert/set.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { toJS } from '../utils/index'
|
||||
|
||||
const opSet = (op, [map, ops]) => {
|
||||
const { link, value, obj, key } = op
|
||||
try {
|
||||
map[obj][key] = link ? map[value] : value
|
||||
|
||||
return [map, ops]
|
||||
} catch (e) {
|
||||
console.error(e, op, toJS(map))
|
||||
|
||||
return [map, ops]
|
||||
}
|
||||
}
|
||||
|
||||
export default opSet
|
||||
Reference in New Issue
Block a user