-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcomponents.tsx
83 lines (79 loc) · 2.31 KB
/
components.tsx
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
import type { AnnotationInfo } from './minimap';
import { React, createRoot } from '../global';
/**
* 毫秒级刷新
*/
export default function renderMinimap(container: Element, props: MinimapProps) {
try {
createRoot(container).render(
<Minimap
background={props.background}
pagesHeight={props.pagesHeight}
annotations={props.annotations}
/>,
);
} catch (error) {
addon.log(error);
}
}
/**
* background和pagesHeight的长度必须相同
*/
function Minimap({ background, pagesHeight, annotations }: MinimapProps) {
const totalHeight = pagesHeight.reduce((a, b) => a + b, 0);
return (
<>
{pagesHeight.map((height, i) => (
<MinimapPage
key={Zotero.randomString()}
background={background[i]}
height={height}
totalHeight={totalHeight}
annotations={annotations[i] ?? []}
/>
))}
</>
);
}
function MinimapPage({
background,
height,
totalHeight,
annotations,
}: {
background: number;
height: number;
totalHeight: number;
annotations: AnnotationInfo[];
}) {
// if (__dev__ && annotations.length)
// addon.log('MinimapPage', background, height, JSON.stringify(annotations));
return (
<div
className="chartero-minimap-page"
style={{
backgroundColor: `rgb(${background}, ${background}, ${background})`,
height: `${(height * 100) / totalHeight}%`,
}}
>
{annotations.flatMap(ann =>
[...ann.rects].map(rect => (
<div
key={Zotero.randomString()}
className="chartero-minimap-note"
style={{
backgroundColor: ann.color,
top: `${(1 - rect.top / height) * 100}%`,
bottom: `${(rect.bottom / height) * 100}%`,
}}
></div>
)),
)}
</div>
);
}
interface MinimapProps {
background: number[];
pagesHeight: number[];
annotations: Array<AnnotationInfo[] | null>;
}