-
Notifications
You must be signed in to change notification settings - Fork 79
/
getChildren.ts
249 lines (211 loc) · 11.1 KB
/
getChildren.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import _ from 'lodash'
import moize from 'moize'
import ComparatorFunction from '../@types/ComparatorFunction'
import Path from '../@types/Path'
import SimplePath from '../@types/SimplePath'
import State from '../@types/State'
import Thought from '../@types/Thought'
import ThoughtContext from '../@types/ThoughtContext'
import ThoughtId from '../@types/ThoughtId'
import getSortPreference from '../selectors/getSortPreference'
import appendToPath from '../util/appendToPath'
import compareByRank from '../util/compareByRank'
import { compareThought, compareThoughtDescending } from '../util/compareThought'
import head from '../util/head'
import isAbsolute from '../util/isAbsolute'
import isAttribute from '../util/isAttribute'
import isDescendantPath from '../util/isDescendantPath'
import sort from '../util/sort'
import splice from '../util/splice'
import unroot from '../util/unroot'
import childIdsToThoughts from './childIdsToThoughts'
import getThoughtById from './getThoughtById'
// use global instance of empty array so object reference doesn't change
const NO_CHILDREN: Thought[] = []
const NO_THOUGHT_IDS: ThoughtId[] = []
/** A selector that retrieves thoughts from a context and performs other functions like sorting or filtering. */
type GetThoughtsSelector = (state: State, id: ThoughtId) => Thought[]
/** Returns true if the child is not hidden due to being a function or having the =hidden attribute. */
export const isVisible = _.curry((state: State, child: Thought): boolean => {
// temporarily disable =hidden for performance
return !isAttribute(child.value) // && !findDescendant(state, child.id, '=hidden')
})
/** Returns the thoughts for the given thought id. If the children have not changed, returns the same object reference. If given null, returns an empty array. */
export const getAllChildren = (state: State, thoughtId: ThoughtId | null): ThoughtId[] => {
if (!thoughtId) return NO_THOUGHT_IDS
const childrenMap = getThoughtById(state, thoughtId)?.childrenMap
const children = childrenMap ? Object.values(childrenMap) : []
return children?.length > 0 ? children : NO_THOUGHT_IDS
}
/** Returns the subthoughts (as Thoughts) of the given ThoughtId unordered. May return a partial or empty list if any thoughts are missing. */
export const getAllChildrenAsThoughts = (state: State, id: ThoughtId | null): Thought[] => {
const children = childIdsToThoughts(state, getAllChildren(state, id))
return children.length === 0 ? NO_CHILDREN : children
}
/** Makes a function that only returns visible thoughts. */
const getVisibleThoughtsById = _.curry(
(getThoughtsFunction: GetThoughtsSelector, state: State, id: ThoughtId): Thought[] => {
const children = getThoughtsFunction(state, id)
return state.showHiddenThoughts ? children : children.filter(isVisible(state))
},
)
/** Returns true if the context has any visible children. */
export const hasChildren = (state: State, id: ThoughtId): boolean => !!findAnyChild(state, id, isVisible(state))
/** Gets all visible children of an id, unordered. */
export const getChildren = getVisibleThoughtsById(getAllChildrenAsThoughts)
/** Gets all children of a Context sorted by rank or sort preference. */
export const getAllChildrenSorted = (state: State, id: ThoughtId): Thought[] => {
const getThoughtsFunction =
getSortPreference(state, id).type === 'Alphabetical' ? getChildrenSortedAlphabetical : getChildrenRanked
return getThoughtsFunction(state, id)
}
/** Gets all visible children of a thought sorted by rank or sort preference.
* Note: It doesn't check if thought lies within the cursor path or is descendant of meta cursor.
*/
export const getChildrenSorted = (state: State, id: ThoughtId | null): Thought[] => {
return id ? getVisibleThoughtsById(getAllChildrenSorted, state, id) : NO_CHILDREN
}
/** Gets a list of all children of a context sorted by the given comparator function. */
const getChildrenSortedBy = (state: State, id: ThoughtId, compare: ComparatorFunction<Thought>): Thought[] =>
sort(getAllChildrenAsThoughts(state, id), compare)
/** Returns the absolute difference between to child ranks. */
const rankDiff = (a: Thought, b: Thought) => Math.abs(a?.rank - b?.rank)
/** Generates children sorted by their values. Sorts empty thoughts to their point of creation. */
const getChildrenSortedAlphabetical = moize(
(state: State, id: ThoughtId): Thought[] => {
const comparatorFunction =
getSortPreference(state, id).direction === 'Desc' ? compareThoughtDescending : compareThought
const sorted = getChildrenSortedBy(state, id, comparatorFunction)
const emptyIndex = sorted.findIndex(thought => !thought.value)
return emptyIndex === -1 ? sorted : resortEmptyInPlace(sorted)
},
{
maxSize: 50,
profileName: 'getChildrenSortedAlphabetical',
},
)
/** Re-sorts empty thoughts in a sorted array to their point of creation. */
const resortEmptyInPlace = (sorted: Thought[]): Thought[] => {
if (sorted.length === 1) return sorted
let emptyIndex = sorted.findIndex(child => !child.value)
// for each empty thought, find the nearest thought according to rank, determine if it was created before or after, and then splice the empty thought back into the sorted array where it was created
let sortedFinal = sorted
const numEmpties = sorted.filter(child => !child.value).length
let i = 0
// eslint-disable-next-line fp/no-loops
while (emptyIndex !== -1 && i++ < numEmpties) {
// ignore empty thoughts that have an explicit sortOrder
// sortOrder is set when editing a thought to the empty thought in order to preserve their sort order
if (sorted[emptyIndex]?.sortValue) {
// not sure why the linter fails here since emptyIndex and i are declared outside the loop
// See: https://eslint.org/docs/rules/no-loop-func
// eslint-disable-next-line no-loop-func
emptyIndex = sortedFinal.findIndex((child, i) => i < emptyIndex && !child.value)
continue
}
// add an index to each thought
const sortedWithIndex = sortedFinal.map((child, i) => ({ ...child, i }))
// remove the first empty thought
const sortedNoEmpty = splice(sortedWithIndex, emptyIndex, 1)
const empty = sortedWithIndex[emptyIndex]
// find the nearest sibling to the empty thought
// getRankBefore places the new thought closer its next sibling
// getRankAfter places the new thought closer its previous sibling
const nearestSibling = sortedNoEmpty.reduce((accum, child) => {
const diffEmpty = rankDiff(empty, child)
const diffMin = rankDiff(empty, accum)
return diffEmpty < diffMin ? child : accum
}, sortedNoEmpty[0])
// determine whether the empty thought was created before or after
const isEmptyBeforeNearest = empty.rank < nearestSibling.rank
// calculate the new index and splice the empty thought into place
const emptyIndexNew = nearestSibling.i + (isEmptyBeforeNearest ? -1 : 0)
sortedFinal = splice(sortedNoEmpty, emptyIndexNew, 0, empty)
// get the emptyIndex for the next iteration of the loop, ignoring empty thoughts that have already been spliced
emptyIndex = sortedFinal.findIndex((child, i) => i < emptyIndexNew && !child.value)
}
return sortedFinal
}
/** Gets all children of a thought sorted by rank. Returns a new object reference even if the children have not changed. */
export const getChildrenRanked = moize(
(state: State, thoughtId: ThoughtId | null): Thought[] => {
const allChildren = childIdsToThoughts(state, getAllChildren(state, thoughtId))
return sort(allChildren, compareByRank)
},
{
maxSize: 50,
profileName: 'getChildrenRanked',
},
)
/** Returns any child of a thought. Only use on a thought with a single child. Also see: firstVisibleChild. */
export const anyChild = (state: State, id: ThoughtId | undefined | null): Thought | undefined => {
if (!id) return undefined
const children = getAllChildren(state, id)
return children.length > 0 ? getThoughtById(state, children[0]) : undefined
}
/** Finds any child that matches the predicate. If there is more than one child that matches the predicate, which one is returned is non-deterministic. */
export const findAnyChild = (
state: State,
id: ThoughtId,
predicate: (child: Thought) => boolean,
): Thought | undefined => {
const childId = getAllChildren(state, id).find(childId => {
const child = getThoughtById(state, childId)
return child && predicate(child)
})
return childId ? getThoughtById(state, childId) : undefined
}
/** Returns all child that match the predicate (unordered). */
export const filterAllChildren = (state: State, id: ThoughtId, predicate: (child: Thought) => boolean): Thought[] => {
const childIds = getAllChildren(state, id).filter(childId => {
const child = getThoughtById(state, childId)
return child && predicate(child)
})
return childIdsToThoughts(state, childIds)
}
/** Returns the first visible child of a sorted context. */
export const firstVisibleChild = (state: State, id: ThoughtId): Thought | undefined => getChildrenSorted(state, id)[0]
/** Returns the first visible child (with cursor check) of a context. */
export const firstVisibleChildWithCursorCheck = (state: State, path: SimplePath) => {
const children = getAllChildrenSorted(state, head(path))
return (state.showHiddenThoughts ? children : children.filter(isChildVisibleWithCursorCheck(state, path)))[0]
}
/** Checks if a child lies within the cursor path. */
const isChildInCursor = (state: State, path: Path, child: Thought): boolean => {
const childPath = unroot([...path, child.id])
return !!state.cursor && state.cursor[childPath.length - 1] === child.id
}
/** Check if the cursor is a meta attribute && the given Path is the descendant of the cursor. */
const isDescendantOfMetaCursor = (state: State, path: Path): boolean => {
if (!state.cursor) return false
const { value: cursorValue } = getThoughtById(state, head(state.cursor))
return isAttribute(cursorValue) && isDescendantPath(path, state.cursor)
}
/** Checks if the child is visible or if the child lies within the cursor or is descendant of the meta cursor. */
const isChildVisibleWithCursorCheck = _.curry(
(state: State, path: SimplePath, thought: Thought): boolean =>
state.showHiddenThoughts ||
isVisible(state, thought) ||
isChildInCursor(state, path, thought) ||
isDescendantOfMetaCursor(state, appendToPath(path, thought.id)),
3,
)
/** Checks if the child is created after latest absolute context toggle. */
const isCreatedAfterAbsoluteToggle = _.curry((state: State, child: ThoughtId | ThoughtContext): boolean => {
const thought = getThoughtById(state, child)
return !!thought.lastUpdated && !!state.absoluteContextTime && thought.lastUpdated > state.absoluteContextTime
})
/**
* Children filter predicate used for rendering.
*
* 1. Checks if the child is visible.
* 2. Checks if child is within cursor.
* 3. Checks if child is created after latest absolute context toggle if starting context is absolute.
*/
export const childrenFilterPredicate = _.curry((state: State, parentPath: SimplePath, child: Thought): boolean => {
return (
isChildVisibleWithCursorCheck(state, parentPath, child) &&
(!isAbsolute(state.rootContext) || isCreatedAfterAbsoluteToggle(state, child.id))
)
}, 3)
export default getChildren