initial commit

This commit is contained in:
cudr
2019-10-05 11:44:49 +03:00
commit a817eb1ceb
63 changed files with 1769 additions and 0 deletions

View 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

View 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 }

View 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

View 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

View 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