-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFViewSyncController.js
More file actions
137 lines (107 loc) · 4.63 KB
/
PDFViewSyncController.js
File metadata and controls
137 lines (107 loc) · 4.63 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
//cSpell:disable
//provides function to sync pdfViewers (mozilla pdf.js)
class PDFViewSyncController {
constructor(leftPDFviewer,rightPDFviewer) {
this._pdf_viewer_left = leftPDFviewer;
this._pdf_viewer_right = rightPDFviewer;
}
async searchInPdfViewer(text,pdfviewer="left"){
if (pdfviewer == "left")
pdfviewer = this._pdf_viewer_left;
if (pdfviewer == "right")
pdfviewer = this._pdf_viewer_right;
return pdfviewer.findController.executeCommand('find', {
caseSensitive: false,
findPrevious: undefined,
highlightAll: true,
phraseSearch: true,
query: text
});
}
async _getPageFromDest (pdfviewer,dest) {
let num_gen = dest[0];
return pdfviewer.pdfDocument.getPageIndex(num_gen);
}
async _gotoDest (pdfviewer,dest){
pdfviewer.pdfLinkService.navigateTo(dest);
}
async _getPDFOutline (pdfviewer) {
return (pdfviewer.pdfDocument.getOutline());
}
async _getPDFDestinations (pdfviewer) {
return (pdfviewer.pdfDocument.getDestinations());
}
async mapOutlines (){
let left_outline = await this._getPDFOutline(this._pdf_viewer_left);
let right_outline = await this._getPDFOutline(this._pdf_viewer_right);
let ldests = await this._getPDFDestinations(this._pdf_viewer_left);
let rdests = await this._getPDFDestinations(this._pdf_viewer_right);
//recursively visit the nested subsections and make them a linear array
const rec_item_concat = (array)=>{
let output = [];
for (const i of array){
output.push(i);
if(i.items)
output = output.concat(rec_item_concat(i.items));
}
return output;
};
//appply to left pdf and right pdf outline
left_outline = rec_item_concat(left_outline);
right_outline = rec_item_concat(right_outline);
//check size equal
if (left_outline.length !== right_outline.length){
alert("Outlines are different in Size. No matching possible!");
return;
}
//combine both outlines to pair-objects with title,page,...
let combined_outlines = [...Array(left_outline.length).keys()].map(async (i) => {
//left
let {title,dest} = left_outline[i];
let page = await this._getPageFromDest(this._pdf_viewer_left,ldests[dest])+1;
let left_info = {title,dest,page};
({title,dest} = right_outline[i]);
page = await this._getPageFromDest(this._pdf_viewer_right,rdests[dest])+1;
let right_info = {title,dest,page};
return (
{chapter:i,left_info,right_info}
);
});
combined_outlines = await Promise.all(combined_outlines);
alert("Successfully mapped outlines. SearchResults will be mapped to right PDF");
//generate a hashmap: ChapterStartPage (left pdf) -> Destination (right pdf)
let chapterStart2DestinationMap = new Map();
combined_outlines.map(obj =>chapterStart2DestinationMap.set(obj.left_info.page,obj.right_info.dest));
this._lStart2RightDestMap = chapterStart2DestinationMap;
}
//sync pages between PDFviewers
async syncPage_left2right () {
this._pdf_viewer_right.page = this._pdf_viewer_left.page;
}
//sync pages between PDFviewers
async syncPage_right2left () {
this._pdf_viewer_left.page = this._pdf_viewer_right.page;
}
async syncChapter2SearchResults(adj_pdfviewer = "right"){
let pdfviewer = this._pdf_viewer_left;
if (adj_pdfviewer == "left")
pdfviewer = this._pdf_viewer_right;
let page_w_sel = pdfviewer.findController._selected.pageIdx;
if(!page_w_sel)
return;
page_w_sel++;
return this.syncChapterViewFromLeft(page_w_sel);
//TODO:implement left adjust;
}
async syncChapterViewFromLeft(p){
if (!this._lStart2RightDestMap)
throw new Error("mapOutlines function needs to be called first");
const chapter_pages = Array.from(this._lStart2RightDestMap.keys());
const offsets = chapter_pages.map(i=>i-p);
const surr_chapter_p = offsets.map(i=>i<=0).lastIndexOf(true);
const dest_hash = this._lStart2RightDestMap.get(chapter_pages[surr_chapter_p]);
let rdest = await this._pdf_viewer_right.pdfDocument.getDestination(dest_hash);
await this._gotoDest(this._pdf_viewer_right,rdest);
}
}
module.exports = {PDFViewSyncController};