This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtreeBuilder.ts
More file actions
148 lines (116 loc) · 3.88 KB
/
treeBuilder.ts
File metadata and controls
148 lines (116 loc) · 3.88 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
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
import {
BaseTreeItem,
Block,
BlockTreeItem,
DocumentTreeItem,
ElementType,
Graphic,
ItemId,
Line,
LineTreeItem,
OcrElement,
Page,
PageTreeItem,
Paragraph,
ParagraphTreeItem,
Word,
} from './types';
import { Tree, TreeItems } from './reducer/types';
import assert from './lib/assert';
import { createUniqueIdentifier } from './utils';
import { getNodeOrThrow } from './treeUtils';
const uniqueId = createUniqueIdentifier();
function assertIsBlock(block: DocumentTreeItem): asserts block is BlockTreeItem {
assert(block.type === ElementType.Block, 'Item is of type %s, expected block.', block.data.type);
}
const createTreeItem = <T extends ElementType, V extends OcrElement<any>, P extends BaseTreeItem<ElementType, any>>(
type: T,
parent: P | null,
data: V,
): BaseTreeItem<T, V> => ({
id: uniqueId().toString(),
type,
parentId: parent?.id.toString() ?? null,
data: data,
parentRelativeOffset:
parent?.type === ElementType.Page
? {
x: data.bbox.x0,
y: data.bbox.y0,
}
: {
x: data.bbox.x0 - (parent?.data.bbox.x0 ?? 0),
y: data.bbox.y0 - (parent?.data.bbox.y0 ?? 0),
},
children: [],
isExpanded: type === ElementType.Block || type === ElementType.Paragraph,
});
const createRootTreeItem = (page: Page): PageTreeItem => ({
id: uniqueId().toString(),
type: ElementType.Page,
parentId: null,
data: page,
parentRelativeOffset: {
x: 0,
y: 0,
},
children: [],
isExpanded: true,
});
const createBlockTreeItem = (parent: PageTreeItem, block: Block) => createTreeItem(ElementType.Block, parent, block);
const createGraphicTreeItem = (parent: PageTreeItem, graphic: Graphic) =>
createTreeItem(ElementType.Graphic, parent, graphic);
const createParagraphTreeItem = (parent: BlockTreeItem, para: Paragraph) =>
createTreeItem(ElementType.Paragraph, parent, para);
const createLineTreeItem = (parent: ParagraphTreeItem, line: Line) => createTreeItem(ElementType.Line, parent, line);
const createWordTreeItem = (parent: LineTreeItem, word: Word) => createTreeItem(ElementType.Word, parent, word);
export function buildTree(page: Page): Tree {
const map: TreeItems = {};
const root = createRootTreeItem(page);
map[root.id] = root;
root.children = page.children.map((block) => {
const treeItem = block.type === 'block' ? createBlockTreeItem(root, block) : createGraphicTreeItem(root, block);
map[treeItem.id] = treeItem;
// if (!canBlockHostChildren(block)) {
// return blockTreeItem.id;
// }
if (block.type === 'graphic') {
return treeItem.id;
}
assertIsBlock(treeItem);
treeItem.children = block.children.map((para) => {
const paragraphTreeItem: ParagraphTreeItem = createParagraphTreeItem(treeItem, para);
paragraphTreeItem.children = para.children.map((line) => {
const lineTreeItem: LineTreeItem = createLineTreeItem(paragraphTreeItem, line);
lineTreeItem.children = line.children.map((word) => {
const wordTreeItem = createWordTreeItem(lineTreeItem, word);
map[wordTreeItem.id] = wordTreeItem;
return wordTreeItem.id;
});
map[lineTreeItem.id] = lineTreeItem;
return lineTreeItem.id;
});
map[paragraphTreeItem.id] = paragraphTreeItem;
return paragraphTreeItem.id;
});
return treeItem.id;
});
return {
rootId: root.id,
items: map,
};
}
export function walkChildren(children: ItemId[], map: TreeItems, action: (item: DocumentTreeItem) => void): void {
walkTree(
children.map((childId) => getNodeOrThrow(map, childId)),
map,
action,
);
}
export function walkTree(tree: DocumentTreeItem[], map: TreeItems, action: (item: DocumentTreeItem) => void): void {
function walk(item: DocumentTreeItem): void {
action(item);
walkChildren(item.children, map, action);
}
tree.forEach((block) => walk(block));
}