-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
306 lines (229 loc) · 8.5 KB
/
Copy pathrenderer.js
File metadata and controls
306 lines (229 loc) · 8.5 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
// cSpell:disable
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
//refs to the pdfviewer applications in the iframes, default false;
let left_pdfViewer_ready = false;
let right_pdfViewer_ready = false;
let cur_assignment = null;
let syncChapters_enabled =true;
let cur_assigned_id=0;
function getNextId(){
cur_assigned_id++;
return cur_assigned_id;
}
//PDFViewSyncController
let pdfViewSyncCon = null;// will be init. after both pdfs are loaded
const page_renderedEvent = new Event("page_rendered");
// eslint-disable-next-line no-undef
let data = new DataController();
//gets currently selected text in element
function getSelectionText (ele) {
var text = "";
if (ele.getSelection) {
text = ele.getSelection().toString();
}
return text;
}
//shortcut document.getElementById
function byId (id) {
return document.getElementById(id);
}
//shortcut document.getElementById
function byClass (classname) {
return document.getElementsByClassName(classname);
}
//helper function to get the refs to the pdfviewer applications
function getPdfViewer (outer_iframe) {
return byId(outer_iframe).contentWindow.PDFViewerApplication;
}
function preventDeselection(ele,window){
ele = ele || window.event;
ele.preventDefault();
}
function handleMarkSourceText(){
let clipboard_win = byId("html_clipboard").contentWindow;
if (cur_assignment){
data.remove(cur_assignment);
cur_assignment = null;
//remove mark bg
clipboard_win.removeMark();
} else {
try {
let selText = clipboard_win.getTextFromSel();
// eslint-disable-next-line no-undef
selText = clearWhitespaces(selText);
if(selText.length === 0){
alert("Selected Text only contains whitespaces.Aborting...");
return;
}
// eslint-disable-next-line no-undef
const ass = new Assignment(selText,null);
//update reference
cur_assignment = ass;
//add to data managment
data.add(ass);
//mark text bg red
clipboard_win.addMark();
} catch(e){
alert(e.message);
}
}
}
function handleMarkTargetText(id=null){
//remove
if(id){
const span = byId("html_clipboard").contentDocument.getElementById(id);
const text = span.childNodes[0].nodeValue;
const ass = data.get(text);
data.remove(ass);
byId("html_clipboard").contentWindow.removeAssignedMark(id);
return;
}
if(!id)
id = "assigned_"+getNextId();
let r_pdf_doc = byId("pdf_view_fl").contentDocument;
let selText = getSelectionText(r_pdf_doc);
if(selText.length===0){
alert("No text in right PDF selected. Aborting...");
return;
}
if(!cur_assignment){
alert("No source text marked. Aborting...");
return;
}
cur_assignment.targetText = selText;
cur_assignment = null;
byId("html_clipboard").contentWindow.markAssigned(id);
}
//communicate with the MenuBar/hotkey
// eslint-disable-next-line no-undef
ipcRenderer.on('syncPage', () => {
if (pdfViewSyncCon)
pdfViewSyncCon.syncPage_left2right();
});
// eslint-disable-next-line no-undef
ipcRenderer.on('markSourceText', handleMarkSourceText);
// eslint-disable-next-line no-undef
ipcRenderer.on('markTargetText',()=> handleMarkTargetText());
//paste html clipboard into left iframe
async function renderHTMLClipboard(){
// eslint-disable-next-line no-undef
let text = clipboard.readHTML();
if(!text)
return;
// fill the iframe with the clipboard contents
const iframe =document.getElementById("html_clipboard");
iframe.srcdoc = text;
return;
}
//wait until pdf.js search has found -> syncChapters
function setupPDFViewerFoundSearchResListener(iframe_sel){
byId(iframe_sel).contentDocument
.addEventListener("updatefindmatchescount",async()=>{
if(!syncChapters_enabled)
return;
// eslint-disable-next-line no-undef
await sleep(500); //hack wait for page switch
await pdfViewSyncCon.syncChapter2SearchResults();
});
}
//wait unitl pdf.js finished rendering page
function setupPDFviewerRenderFinishListener (iframe_sel,left_or_right_rdy){
byId(iframe_sel).contentDocument.addEventListener("pagerendered",()=>{
if( left_or_right_rdy === "left")
left_pdfViewer_ready = true;
if( left_or_right_rdy === "right")
right_pdfViewer_ready = true;
document.dispatchEvent(page_renderedEvent); //pass the render event to outter document
});
}
async function searchSelectionHandler(){
let window = byId("html_clipboard").contentWindow;
//do not search if currently in assignment mode
if(cur_assignment){
return;
}
let selText = getSelectionText(window.document);
//clear starting spaces and trailing spaces /newlines
// eslint-disable-next-line no-undef
selText = clearWhitespaces(selText);
//check if pdfSynController exists
if(!pdfViewSyncCon)
return;
//execute search with selection in left viewer
pdfViewSyncCon.searchInPdfViewer(selText);
}
// eslint-disable-next-line no-unused-vars
async function viewAssignedInPDFViewers(sourceText){
// eslint-disable-next-line no-undef
sourceText = clearWhitespaces(sourceText);
const ass = data.get(sourceText);
if (!pdfViewSyncCon){
return;
}
syncChapters_enabled = false;
//TODO:suche durch page speicheren ersetzten
pdfViewSyncCon.searchInPdfViewer(ass.sourceText);
// eslint-disable-next-line no-undef
pdfViewSyncCon.searchInPdfViewer(ass.targetText,"right");
await sleep(500);
syncChapters_enabled = true;
}
//setup button functions
//function for right pdf button
byId("load_de_pdf_b").addEventListener('click',async ()=>{
// eslint-disable-next-line no-undef
await loadPDFfile("pdf_view_de");
pdfViewSyncCon=null;
});
//function for right pdf button
byId("load_fl_pdf_b").addEventListener('click',async ()=> {
// eslint-disable-next-line no-undef
await loadPDFfile("pdf_view_fl");
pdfViewSyncCon=null;
});
//function for clipboard button
byId("load_clist_b").addEventListener('click', async () => await renderHTMLClipboard());
//wait for iframes to load, setup scripts
//clipboard html
byId("html_clipboard").addEventListener("load",function(){
let window = byId("html_clipboard").contentWindow;
window.addEventListener("mouseup",searchSelectionHandler);
//append marking logic
let markingLogic = document.createElement("script");
markingLogic.src = "./markingLogic.js";
byId("html_clipboard").contentDocument.body.appendChild(markingLogic);
//append css for tooltips
let cssLink = document.createElement("link");
cssLink.href = "./left_iframe.css";
cssLink .rel = "stylesheet";
cssLink .type = "text/css";
byId("html_clipboard").contentDocument.body.appendChild(cssLink);
});
//left pdfviewer
byId("pdf_view_de").addEventListener("load",()=> {
setupPDFviewerRenderFinishListener("pdf_view_de","left");
setupPDFViewerFoundSearchResListener("pdf_view_de",left_pdfViewer_ready);
//prevent deselection
let win = byId("pdf_view_de").contentWindow;
let ele = byId("pdf_view_de").contentDocument;
ele.addEventListener('onmousedown',preventDeselection,ele,win);
});
//right pdfviewer
byId("pdf_view_fl").addEventListener("load",()=> setupPDFviewerRenderFinishListener("pdf_view_fl","right"));
//if both viewers have finished rendering setup the syncController
document.addEventListener("page_rendered",async ()=>{
if(left_pdfViewer_ready && right_pdfViewer_ready && pdfViewSyncCon === null){
let left_pdfViewer = getPdfViewer("pdf_view_de");
let right_pdfViewer = getPdfViewer("pdf_view_fl");
// eslint-disable-next-line no-undef
pdfViewSyncCon = new PDFViewSyncController(left_pdfViewer,right_pdfViewer);
await pdfViewSyncCon.mapOutlines();
}
});
//document.addEventListener("readystatechange ",document.getElementsByClassName("window-close")[0].addEventListener("close",()=>remote.BrowserWindow.getFocusedWindow().close()));