-
-
Notifications
You must be signed in to change notification settings - Fork 844
/
StructTree.tsx
108 lines (82 loc) · 2.68 KB
/
StructTree.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
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
import { useEffect } from 'react';
import makeCancellable from 'make-cancellable-promise';
import invariant from 'tiny-invariant';
import warning from 'warning';
import StructTreeItem from './StructTreeItem.js';
import usePageContext from './shared/hooks/usePageContext.js';
import useResolver from './shared/hooks/useResolver.js';
import { cancelRunningTask } from './shared/utils.js';
import type { StructTreeNodeWithExtraAttributes } from './shared/types.js';
export default function StructTree() {
const pageContext = usePageContext();
invariant(pageContext, 'Unable to find Page context.');
const {
onGetStructTreeError: onGetStructTreeErrorProps,
onGetStructTreeSuccess: onGetStructTreeSuccessProps,
} = pageContext;
const [structTreeState, structTreeDispatch] = useResolver<StructTreeNodeWithExtraAttributes>();
const { value: structTree, error: structTreeError } = structTreeState;
const { customTextRenderer, page } = pageContext;
function onLoadSuccess() {
if (!structTree) {
// Impossible, but TypeScript doesn't know that
return;
}
if (onGetStructTreeSuccessProps) {
onGetStructTreeSuccessProps(structTree);
}
}
function onLoadError() {
if (!structTreeError) {
// Impossible, but TypeScript doesn't know that
return;
}
warning(false, structTreeError.toString());
if (onGetStructTreeErrorProps) {
onGetStructTreeErrorProps(structTreeError);
}
}
function resetAnnotations() {
structTreeDispatch({ type: 'RESET' });
}
useEffect(resetAnnotations, [structTreeDispatch, page]);
function loadStructTree() {
if (customTextRenderer) {
// TODO: Document why this is necessary
return;
}
if (!page) {
return;
}
const cancellable = makeCancellable(page.getStructTree());
const runningTask = cancellable;
cancellable.promise
.then((nextStructTree) => {
structTreeDispatch({ type: 'RESOLVE', value: nextStructTree });
})
.catch((error) => {
structTreeDispatch({ type: 'REJECT', error });
});
return () => cancelRunningTask(runningTask);
}
useEffect(loadStructTree, [customTextRenderer, page, structTreeDispatch]);
useEffect(
() => {
if (structTree === undefined) {
return;
}
if (structTree === false) {
onLoadError();
return;
}
onLoadSuccess();
},
// Ommitted callbacks so they are not called every time they change
// eslint-disable-next-line react-hooks/exhaustive-deps
[structTree],
);
if (!structTree) {
return null;
}
return <StructTreeItem className="react-pdf__Page__structTree structTree" node={structTree} />;
}