Skip to content

Commit e497d78

Browse files
committed
refactor: before mutable inner treeData
1 parent b53568d commit e497d78

9 files changed

+1538
-42
lines changed

lib/HeTree.tsx

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,26 @@ import type { } from 'type-fest';
77
const forwardRef = React.forwardRef as FixedForwardRef
88

99
// types ==================================
10-
type IDType = string | number
11-
12-
export interface StatBase {
13-
id: IDType,
14-
pid: IDType | null,
15-
childIds: IDType[],
16-
siblingIds: IDType[],
10+
export type Id = string | number
11+
export type Checked = boolean | null
12+
export interface Stat<T> {
13+
id: Id,
14+
pid: Id | null,
15+
childIds: Id[],
16+
siblingIds: Id[],
1717
index: number,
1818
level: number,
19-
open: boolean,
20-
checked: boolean | null,
21-
draggable: boolean,
22-
_isStat?: boolean,
23-
}
24-
export interface Stat<T> extends StatBase {
2519
node: T,
2620
parent: T | null,
2721
parentStat: Stat<T> | null,
2822
children: T[],
2923
childStats: Stat<T>[],
3024
siblings: T[],
3125
siblingStats: Stat<T>[],
26+
_isStat?: boolean,
27+
open: boolean,
28+
checked: Checked,
29+
draggable: boolean,
3230
}
3331

3432
// single instance ==================================
@@ -94,17 +92,17 @@ export function useTree<T extends Record<string, any>>(
9492
// mainCache ==================================
9593
const mainCache = useMemo(
9694
() => {
97-
const stats: Record<IDType, Stat<T>> = {}
98-
const nodes: Record<IDType, T> = {}
99-
const openedIds: IDType[] = []
100-
const checkedIds: IDType[] = []
101-
const semiCheckedIds: IDType[] = []
102-
const rootIds: StatBase['childIds'] = []
95+
const stats: Record<Id, Stat<T>> = {}
96+
const nodes: Record<Id, T> = {}
97+
const openedIds: Id[] = []
98+
const checkedIds: Id[] = []
99+
const semiCheckedIds: Id[] = []
100+
const rootIds: Id[] = []
103101
const rootNodes: T[] = []
104102
const rootNodeStats: Stat<T>[] = []
105103
//
106-
const tasks: Record<IDType, (parentStat: Stat<T>) => void> = {}
107-
const addTask = (pid: IDType, task: (parentStat?: Stat<T>) => void) => {
104+
const tasks: Record<Id, (parentStat: Stat<T>) => void> = {}
105+
const addTask = (pid: Id, task: (parentStat?: Stat<T>) => void) => {
108106
const parentStat = stats[pid]
109107
if (parentStat) {
110108
task(parentStat)
@@ -116,20 +114,20 @@ export function useTree<T extends Record<string, any>>(
116114
}
117115
}
118116
}
119-
const removeTask = (id: IDType) => {
117+
const removeTask = (id: Id) => {
120118
tasks[id]?.(stats[id])
121119
delete tasks[id]
122120
}
123121
//
124122
for (const node of props.data) {
125-
const id = node[ID] as IDType
126-
const pid = node[PID] as IDType
123+
const id = node[ID] as Id
124+
const pid = node[PID] as Id
127125
const parent = nodes[pid] || null
128126
addTask(pid, () => {
129127

130128
})
131129
const parentStat = stats[pid] || null;
132-
const childIds: StatBase['childIds'] = []
130+
const childIds: Id[] = []
133131
const children: T[] = []
134132
const childStats: Stat<T>[] = []
135133
let siblingIds, siblings, siblingStats
@@ -195,8 +193,8 @@ export function useTree<T extends Record<string, any>>(
195193

196194
}
197195
}
198-
const getStat = (nodeOrStatOrId: T | Stat<T> | IDType) => {
199-
let id: IDType
196+
const getStat = (nodeOrStatOrId: T | Stat<T> | Id) => {
197+
let id: Id
200198
if (typeof nodeOrStatOrId === 'object') {
201199
// @ts-ignore
202200
id = nodeOrStatOrId._isStat ? nodeOrStatOrId.id : nodeOrStatOrId[ID]
@@ -210,7 +208,7 @@ export function useTree<T extends Record<string, any>>(
210208
const skip = () => { _skip = true }
211209
yield* walk(node)
212210
function* walk(node?: T | null): Generator<{ node: T, stat: Stat<T>, skip: () => void }> {
213-
let childIds: IDType[]
211+
let childIds: Id[]
214212
if (node) {
215213
const stat = stats[node[ID]]
216214
yield { node, stat, skip }
@@ -238,8 +236,8 @@ export function useTree<T extends Record<string, any>>(
238236
function resolveChecked(node: T, checked: boolean) {
239237
const ckSet = new Set(checkedIds);
240238
const semiSet = new Set(semiCheckedIds);
241-
const t: Record<IDType, StatBase['checked']> = {}
242-
const update = (id: IDType, checked: StatBase['checked']) => {
239+
const t: Record<Id, Checked> = {}
240+
const update = (id: Id, checked: Checked) => {
243241
t[id] = checked
244242
if (checked === null) {
245243
ckSet.delete(id)
@@ -298,7 +296,7 @@ export function useTree<T extends Record<string, any>>(
298296
const isExternal = !draggedStat
299297
const cacheForVisible = useMemo(
300298
() => {
301-
const visibleIds: IDType[] = []
299+
const visibleIds: Id[] = []
302300
const attrsList: (React.HTMLProps<HTMLDivElement> & { 'data-key': string, 'data-level': string, 'data-node-box': boolean, 'data-drag-placeholder'?: boolean })[] = []
303301
for (const { stat, skip } of traverseChildNodesIncludingSelf()) {
304302
const attr = createAttrs(stat)
@@ -587,7 +585,7 @@ export function useTree<T extends Record<string, any>>(
587585
let closest = stat
588586
let index = visibleIds.indexOf(stat.id) // index of closest node
589587
let atTop = false
590-
const isPlaceholderOrDraggedNode = (id: IDType) => id === props.placeholderId || getStat(id) === draggedStat
588+
const isPlaceholderOrDraggedNode = (id: Id) => id === props.placeholderId || getStat(id) === draggedStat
591589
const find = (startIndex: number, step: number) => {
592590
let i = startIndex, cur
593591
do {
@@ -650,7 +648,7 @@ export const HeTree = <T extends Record<string, any>,>(props: ReturnType<typeof
650648
//
651649
return (
652650
<div className="he-tree" onDragOver={props.onDragOverRoot} onDrop={props.onDropToRoot}>
653-
<VirtualList<IDType> ref={props.virtualList} items={props.visibleIds} virtual={false} persistentIndices={persistentIndices}
651+
<VirtualList<Id> ref={props.virtualList} items={props.visibleIds} virtual={false} persistentIndices={persistentIndices}
654652
renderItem={(id, index) => (
655653
<div {...props.attrsList[index]}>
656654
{
@@ -694,4 +692,6 @@ export function* traverseTreeData<T>(
694692

695693
function calculateDistance(x1: number, y1: number, x2: number, y2: number) {
696694
return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
697-
}
695+
}
696+
697+

0 commit comments

Comments
 (0)