-
Notifications
You must be signed in to change notification settings - Fork 3
/
TransitionalViewContext.ts
86 lines (71 loc) · 2.85 KB
/
TransitionalViewContext.ts
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
import { Messenger } from "../Messenger";
import { AnnotationMaskCoordinates } from "../pdf/PDFPageRenderer";
export interface PdfPageDetail {
pageNumber: number;
width: number;
height: number;
scale: number;
}
export class TransitionalViewContext {
readonly messenger: Messenger;
readonly initialAnnotationMaskCoordinates: AnnotationMaskCoordinates;
readonly initialPdfPageDetail: PdfPageDetail;
// Some useful data for fetching updated mask coordinates and PDF page details
private pdfPageNumber: number;
private codeMappingId: number;
constructor(
codeMappingId: number,
messenger: Messenger,
annotationMaskCoordinates: AnnotationMaskCoordinates,
pdfPageDetail: PdfPageDetail,
) {
this.messenger = messenger;
this.initialAnnotationMaskCoordinates = annotationMaskCoordinates;
this.initialPdfPageDetail = pdfPageDetail;
this.pdfPageNumber = pdfPageDetail.pageNumber;
this.codeMappingId = codeMappingId;
}
private get pdfPageNode(): HTMLElement | null {
const node = document.body
.querySelector(`#pdf-container .pdf-page-container .pdf-page[data-page-number="${this.pdfPageNumber}"]`);
if (!node) {
console.warn(`The node of PDF page ${this.pdfPageNumber} could not be found.`);
return null;
}
return node as HTMLElement;
}
private get annotationMaskNode(): HTMLElement | null {
const node = document.body
.querySelector(`#pdf-container .pdf-annotation-mask-container .annotation-mask[data-code-mapping-id="${this.codeMappingId}"]`);
if (!node) {
console.warn(`The node of the annotation mask with code mapping ID "${this.pdfPageNumber}" could not be found.`);
return null;
}
return node as HTMLElement;
}
get annotationMaskCoordinates(): AnnotationMaskCoordinates | null {
const annotationMaskNode = this.annotationMaskNode;
if (!annotationMaskNode) {
return null;
}
const annotationMaskBox = annotationMaskNode.getBoundingClientRect();
return [
window.scrollX + annotationMaskBox.left,
window.scrollY + annotationMaskBox.top,
window.scrollX + annotationMaskBox.right,
window.scrollY + annotationMaskBox.bottom,
];
}
get pdfPageDetail(): PdfPageDetail | null {
const pdfPageNode = this.pdfPageNode;
if (!pdfPageNode) {
return null;
}
return {
pageNumber: this.pdfPageNumber,
width: parseFloat(pdfPageNode.getAttribute("data-viewport-width")!),
height: parseFloat(pdfPageNode.getAttribute("data-viewport-height")!),
scale: parseFloat(pdfPageNode.getAttribute("data-viewport-scale")!)
};
}
}