-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
149 lines (127 loc) · 4.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import _ from 'lodash/fp.js'
import F from 'futil'
import { encode, Tree } from '../util/tree.js'
import { getTypeProp } from '../types.js'
import wrap from './wrap.js'
import { dedupeWalk } from '../node.js'
let pushOrSpliceOn = (array, item, index) => {
if (index === undefined) array.push(item)
else array.splice(index, 0, item)
return array
}
let arrayDropLast = _.flow(_.toArray, _.dropRight(1))
export default (config) => {
let {
getNode,
flat,
dispatch,
snapshot,
types,
extend,
initNode,
initObject,
onChange,
} = config
let add = (parentPath, node, { index } = {}) => {
let target = getNode(parentPath)
// initialize uniqueString cache for the parent of the node to be added here,
// since it's not visited during the tree walk
let parentDedupe = F.uniqueString(_.map('key', target.children))
node = initObject(node)
dedupeWalk(
(dedupe, parentPath, node) => {
initNode({ extend, types, snapshot }, dedupe, parentPath, node)
flat[encode(node.path)] = node
},
node,
{ target, dedupe: parentDedupe }
)
// consider moving this in the tree walk? it could work for al children too but would be exgra work for chilren
pushOrSpliceOn(target.children, node, index)
onChange(target, { children: target.children })
// Need this nonsense to support the case where push actually mutates, e.g. a mobx observable tree
// flat[encode(path)] = target.children[index]
return dispatch({ type: 'add', path: _.toArray(node.path), node })
}
let remove = (path) => {
let previous = getNode(path)
let parentPath = arrayDropLast(path)
let parent = getNode(parentPath)
F.pullOn(previous, parent.children)
onChange(parent, { children: parent.children })
Tree.walk((node, index, [parent = {}]) => {
let path = [...(parent.path || parentPath), node.key]
delete flat[encode(path)]
})(previous)
return dispatch({ type: 'remove', path, previous })
}
let mutate = _.curry((path, value) => {
let target = getNode(path)
let previous = snapshot(_.omit('children', target))
extend(target, value)
return dispatch({
type: 'mutate',
path,
previous,
value,
node: target,
})
})
let refresh = (path) => dispatch({ type: 'refresh', path })
let triggerUpdate = () =>
dispatch({ type: 'none', path: [], autoUpdate: true })
let clear = (path) =>
mutate(
path,
_.omit(['field'], getTypeProp(types, 'defaults', getNode(path)))
)
let replace = (path, transform) => {
let parentPath = arrayDropLast(path)
let node = getNode(path)
let index = _.findIndex((x) => x === node, getNode(parentPath).children)
let newNode = F.callOrReturn(transform, node)
remove(path)
return add(parentPath, newNode, { index })
}
let { wrapInGroup } = wrap(config, { mutate, replace, add })
let move = (path, { path: targetPath, index: targetIndex } = {}) => {
let parentPath = arrayDropLast(path)
targetPath = targetPath || parentPath
let node = getNode(path)
if (_.isEqual(parentPath, targetPath)) {
// Same group, no dispatch or updating of paths needed - just rearrange children
F.pullOn(node, getNode(parentPath).children)
pushOrSpliceOn(getNode(targetPath).children, node, targetIndex)
onChange(node, { children: node.children })
} else {
return Promise.all([
remove(path),
add(targetPath, node, { index: targetIndex }),
])
}
}
let mutateNested = (path, payload) =>
_.flow(
getNode,
Tree.toArrayBy((node) => mutate(_.toArray(node.path), payload)),
(x) => Promise.all(x)
)(path)
let pauseNested = (path) => mutateNested(path, { paused: true })
let unpauseNested = (path) => mutateNested(path, { paused: false })
let nodeLeaves = _.flow(getNode, Tree.leaves)
let isPausedNested = _.flow(nodeLeaves, _.every('paused'))
return {
add,
remove,
mutate,
refresh,
triggerUpdate,
clear,
replace,
wrapInGroup,
move,
isPausedNested,
pauseNested,
unpauseNested,
}
}