-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathuseAutoIndent.ts
More file actions
66 lines (53 loc) · 1.54 KB
/
useAutoIndent.ts
File metadata and controls
66 lines (53 loc) · 1.54 KB
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
import { RefObject } from "preact";
import { useLayoutEffect, useRef, useState } from "preact/hooks";
import { useResize } from "../utils";
const INITIAL = 14;
const RIGHT_MARGIN = 16;
export function useAutoIndent(
container: RefObject<HTMLElement | null>,
deps: any[],
) {
const indent = useRef(INITIAL);
const [available, setAvailable] = useState(0);
const cacheRef = useRef(new Map<string, number>());
useResize(
() => {
indent.current = INITIAL;
if (container.current) {
setAvailable(container.current.clientWidth);
}
},
[],
true,
);
useLayoutEffect(() => {
if (container.current) {
let space = available;
if (available === 0) {
space = container.current.clientWidth;
}
const cache = cacheRef.current;
const { childNodes } = container.current;
let nextIndent = indent.current;
for (let i = 0; i < childNodes.length; i++) {
const child = childNodes[i] as HTMLElement;
if (!child) continue;
const id = child.getAttribute("data-id");
if (!id) continue;
// Measure the actual first child
const el = child.firstChild as HTMLElement;
if (!el) continue;
let width = cache.get(id);
if (!width) {
width = el.offsetWidth + RIGHT_MARGIN;
cache.set(id, width);
}
const depth = +(child.getAttribute("data-depth") || 0);
nextIndent = Math.min(nextIndent, Math.max(0, (space - width) / depth));
}
container.current.style.setProperty("--indent-depth", `${nextIndent}px`);
indent.current = nextIndent;
}
}, [...deps, available]);
return indent;
}