-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenhancedPDF.js
797 lines (716 loc) · 29.9 KB
/
enhancedPDF.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// ==UserScript==
// @name Enhanced-PDF Extension for Roam Research
// @author hungriesthippo <https://github.com/hungriesthippo> & CCC <https://github.com/c3founder>
// @require -
// @version 1.0
// @match https://*.roamresearch.com
// @description Handle PDF Highlights.
// MAIN OUTPUT MODES:
// 1) cousin
// Highlights are added as the cousin block.
// The breadCrumbAttribute & citeKeyAttribute are searched for in the PDF grand parent subtree
// - PDF grand parent block
// - PDF parent block
// - PDF block, e.g., {{pdf: https://firebasestorage.googleapis.com/v0/b/exampleurl}}
// - Highlight
// - Cousin of the PDF block
// 2) child
// Highlights are added as the child block:
// The breadCrumbAttribute & citeKeyAttribute are searched for in the PDF parent subtree
// - PDF parent block
// - PDF block
// - Child of the PDF block
/*******************************************************/
/*******************Parameter BEGIN*********************/
const pdfParams = window.pdfParams;
/*******************Parameter END***********************/
/*******************************************************/
/* Begin Importing Utility Functions */
if (typeof ccc !== 'undefined' && typeof ccc.util !== 'undefined') {
//Somebody has already loaded the utility
startC3PdfExtension();
} else {
let s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://c3founder.github.io/Roam-Enhancement/enhancedUtility.js"
s.id = 'c3util4pdf'
s.onload = () => { startC3PdfExtension() }
try { document.getElementById('c3util4pdf').remove() } catch (e) { };
document.getElementsByTagName('head')[0].appendChild(s);
}
/* End Importing Utility Functions */
function startC3PdfExtension() {
var ccc = window.ccc || {};
var c3u = ccc.util;
/*******************************************************/
/**********************Main BEGIN***********************/
// const serverPerfix = 'http://localhost:3000/?url=';
const serverPerfix = 'https://roampdf.web.app/?url=';
const pdfChar = ' ';
function initPdf() {
Array.from(document.getElementsByTagName('iframe')).forEach(iframe => {
if (!iframe.classList.contains('pdf-activated')) {
try {
if (new URL(iframe.src).pathname.endsWith('.pdf')) {
const originalPdfUrl = iframe.src; //the permanent pdfId
iframe.id = "pdf-" + iframe.closest('.roam-block').id; //window level pdfId
const pdfBlockUid = c3u.getUidOfContainingBlock(iframe); //for click purpose
allPdfIframes.push(iframe); //save for interaction
renderPdf(iframe); //render pdf through the server
sendHighlights(iframe, originalPdfUrl, pdfBlockUid);
}
} catch { } // some iframes have invalid src
}
if (iframe.src.startsWith(serverPerfix)) {
adjustPdfIframe(iframe);
}
})
activateButtons();
}
///////////////Responsive PDF Iframe
function adjustPdfIframe(iframe) {
const reactParent = iframe.closest('.react-resizable')
const reactHandle = reactParent.querySelector(".react-resizable-handle")
const hoverParent = iframe.closest('.hoverparent')
reactHandle.style.display = 'none';
reactParent.style.width = '100%';
reactParent.style.height = '100%';
hoverParent.style.width = '100%';
hoverParent.style.height = '100%';
}
/************************Main END***********************/
/*******************************************************/
/*******************************************************/
/*************Look for Highlight Delete BEGIN***********/
let hlDeletionObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.removedNodes.forEach(node => {
if (typeof (node.classList) !== 'undefined') {
if (node.classList.contains("roam-block-container")) { //if a block is deleted
handleHighlightDelete(node)
handlePdfDelete(node)
}
}
});
});
});
function handleHighlightDelete(node) {
Array.from(node.getElementsByTagName('button')) //if it had a button
.filter(isHighlightBtn)
.forEach(async function (btn) {
const match = btn.id.match(/main-hlBtn-(.........)/)
if (match) {
if (c3u.existBlockUid(match[1])) {//If the data page was deleted ignore
await c3u.sleep(3000) //Maybe someone is moving blocks or undo
if (!c3u.existBlockUid(c3u.blockString(match[1]))) {
//Delete the date row
const hlDataRowUid = match[1];
const hlDataRow = c3u.queryAllTxtInChildren(hlDataRowUid);
const toDeleteHighlight = getHighlight(hlDataRow[0][0]);
const dataTableUid = c3u.parentBlockUid(hlDataRowUid);
c3u.deleteBlock(hlDataRowUid)
//Delete the hl on pdf (send message to render)
const dataPageUid = c3u.parentBlockUid(dataTableUid);
const pdfUrl = encodePdfUrl(c3u.blockString(c3u.getNthChildUid(dataPageUid, 0)));
Array.from(document.getElementsByTagName('iframe'))
.filter(x => x.src === pdfUrl)
.forEach(iframe => {
iframe.contentWindow.postMessage({ deleted: toDeleteHighlight }, '*');
});
}
}
}
});
}
function handlePdfDelete(node) {
Array.from(node.getElementsByTagName('iframe'))
.filter(x => { return x.src.indexOf(serverPerfix) !== -1; })
.forEach(async function (iframe) {
await c3u.sleep(1000) //Maybe someone is moving blocks or undo
const pdfUid = iframe.id.slice(-9)
if (!c3u.existBlockUid(pdfUid)) { //pdf block deleted
const pdfUrl = decodePdfUrl(iframe.src)
const pdfDataPageTitle = getDataPageTitle(pdfUrl);
const pdfDataPageUid = c3u.getPageUid(pdfDataPageTitle);
if (pdfDataPageUid) { //If the data page exists
const tableUid = c3u.getNthChildUid(pdfDataPageUid, 2);
const res = c3u.allChildrenInfo(tableUid)[0][0];
c3u.deletePage(pdfDataPageUid)
res.children.map(async function (child) {
//You can check their existence but seems redundent.
c3u.deleteBlock(child.string);
});
}
}
});
}
///////////////Wait for roam to fully load then observe
let roamArticle;
let roamArticleReady = setInterval(() => {
if (!document.querySelector('.roam-app')) return;
roamArticle = document.querySelector('.roam-app')
hlDeletionObserver.observe(roamArticle, {
childList: true,
subtree: true
});
clearInterval(roamArticleReady);
}, 1000);
/*************Look for Highlight Delete END***********/
/*****************************************************/
/*******************************************************/
/**************Button Activation BEGIN******************/
/*****Fixing Highlight Btns Appearance and Functions****/
function activateButtons() {
Array.from(document.getElementsByTagName('button'))
.filter(isUnObservedHighlightBtn)
.forEach(btn => {
if (!btn.closest('.rm-zoom-item'))
hlBtnAppearsObserver.observe(btn);
btn.classList.add('btn-observed');
})
activateSortButtons();
}
function activateSortButtons() {
Array.from(document.getElementsByTagName('button'))
.filter(isInactiveSortBtn)
.forEach(btn => {
const sortBtnBlockUid = c3u.getUidOfContainingBlock(btn);
btn.classList.add('btn-sort-highlight', 'btn-pdf-activated');
const pdfUid = c3u.parentBlockUid(sortBtnBlockUid)
const match = c3u.blockString(pdfUid).match(/\{{\[?\[?pdf\]?\]?:\s(.*)}}/);
if (match[1]) {
const pdfUrl = match[1];
let highlights = getAllHighlights(pdfUrl, pdfUid)
highlights.sort(function (a, b) {
if (a.position.pageNumber < b.position.pageNumber)
return -1
if (a.position.pageNumber > b.position.pageNumber)
return +1
if (a.position.boundingRect.x2 < b.position.boundingRect.x1)
return -1
if (b.position.boundingRect.x2 < a.position.boundingRect.x1)
return +1
if (a.position.boundingRect.y1 < b.position.boundingRect.y1)
return -1
return +1
});
let cnt = 0
btn.onclick = () =>
highlights.map(function (item) { c3u.createChildBlock(sortBtnBlockUid, cnt++, "((" + item.id + "))", c3u.createUid()); })
}
})
}
let options = {
root: document.querySelector('.roam-app'),
rootMargin: "0px 0px 500px 0px",
threshold: 1.0
}
function activateSingleBtn(entries) {
entries.forEach((entry) => {
const btn = entry.target;
if (isInactiveHighlightBtn(btn) && entry.intersectionRatio > .25) {
const hlInfo = getHlInfoFromBtn(btn);
const highlight = getSingleHighlight(hlInfo.uid)
let pdfInfo = getPdfInfoFromHighlight(hlInfo.uid);
if (pdfInfo) {
const btnBlock = btn.closest(".rm-block__input");
const page = btn.innerText;
addBreadcrumb(btnBlock, page, pdfInfo.uid);
pdfInfo.url = encodePdfUrl(pdfInfo.url);
handleBtn(btn, pdfInfo, hlInfo, highlight);
}
}
});
}
hlBtnAppearsObserver = new IntersectionObserver(activateSingleBtn, options);
/////////////////////////////////////////////////////////
///////////////Portal to the Data Page //////////////////
/////////////////////////////////////////////////////////
///////////////From highlight => Data page => Retrieve PDF url and uid.
function getPdfInfoFromHighlight(hlBlockUid) {
let match = c3u.blockString(hlBlockUid).match(/\[..?]\(\(\((.........)\)\)\)/);
if (!match[1]) return null;
const pdfUid = match[1];
match = c3u.blockString(pdfUid).match(/\{{\[?\[?pdf\]?\]?:\s(.*)}}/);
if (!match[1]) return null;
const pdfUrl = match[1];
return { url: pdfUrl, uid: pdfUid };
}
///////////////From highlight => Row of the data table => Highlight coordinates
function getSingleHighlight(hlBlockUid) {
const hlDataRowUid = getHighlightDataBlockUid(hlBlockUid);
const hlDataRow = c3u.queryAllTxtInChildren(hlDataRowUid);
if (hlDataRow.length === 0) return null;
return getHighlight(hlDataRow[0][0]);
}
///////////////From button's text jump to the corresponding data table row
function getHighlightDataBlockUid(hlBlockUid) {
const match = c3u.blockString(hlBlockUid).match(/{{\d+:\s*(.........)}}/);
if (!match) return null;
return match[1]
}
///////////////Get the Original Highlight
///////////////Where am I? Main Hilight or Reference?
function getHlInfoFromBtn(btn) {
let hlType, hlUid;
const blockRefSpan = btn.closest('.rm-block-ref')
if (!blockRefSpan) {
hlType = 'main';
hlUid = c3u.getUidOfContainingBlock(btn);
} else {
hlType = 'ref';
hlUid = blockRefSpan.dataset.uid
}
return { type: hlType, uid: hlUid };
}
function getHighlightsFromTable(uid) {
const hls = c3u.queryAllTxtInChildren(uid)[0][0].children;
return hls.map(function (x) { return getHighlight(x); }).filter(hl => hl != null);
}
function getHighlight(hl) { //the column order is: (hlUid, hlInfo(pos, color), hlTxt)
//Extracting Text
const hlText = hl.children[0].children[0].string;
//Extracting Info = (position, color)
const hlInfo = JSON.parse(hl.children[0].string);
let position, color;
if (typeof (hlInfo.position) === 'undefined') {//if older version highlight
position = JSON.parse(hl.children[0].string);
color = 0;
} else {
position = hlInfo.position;
color = hlInfo.color;
}
//Extracting Id
const id = hl.string
return { id, content: { text: hlText }, position, color };
}
////////////////////////////////////////////////////////////
////////Activate all of the Highlight Buttons////////
////////////////////////////////////////////////////////////
////////Open the PDF and send the HLs to server
async function handleHighlightClick(e, pdfInfo, highlight) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
let iframe = getOpenIframeElementWithSrc(pdfInfo.url);
if (!iframe) { //Iframe is closed
c3u.openBlockInSidebar('block', pdfInfo.uid)
await c3u.sleep(3000);
iframe = getOpenIframeElementWithSrc(pdfInfo.url);
}
iframe.contentWindow.postMessage({ scrollTo: highlight }, '*');
}
function getOpenIframeElementWithSrc(iframeSrc) {
return Array.from(document.getElementsByTagName('iframe'))
.find(iframe => iframe.src === iframeSrc);
}
function handleBtn(btn, pdfInfo, hlInfo, highlight) {
const blockUid = c3u.getUidOfContainingBlock(btn);
//Shared for main and reference jump btns
const extraClass = 'btn-' + hlInfo.type + '-annotation'
btn.classList.add(extraClass, 'btn', 'btn-default', 'btn-pdf-activated');
const btnId = getHighlightDataBlockUid(hlInfo.uid);
btn.id = hlInfo.type + '-hlBtn-' + btnId;
btn.addEventListener("click", (e) => { handleHighlightClick(e, pdfInfo, highlight) });
if (hlInfo.type === 'ref') {
//Fix highlight btn
btn.classList.add('popup');
const wrapperSpan = btn.closest('.bp3-popover-wrapper')
const closestSpan = btn.closest('span')
closestSpan.classList.add('displacedBtns')
closestSpan.parentElement.closest('span').querySelector('.rm-block__ref-count-footnote')?.closest('.bp3-popover-wrapper').remove();
c3u.insertAfter(closestSpan, wrapperSpan)
//Fix footnote btn if exists
const footnote = wrapperSpan.closest('span').querySelector('.rm-block__ref-count-footnote')
footnote?.classList.add('popup');
//Add replace with text and alias btns
let btnRepText = null, btnRepAlias = null;
if (pdfParams.textChar !== '') {
btnRepText = createCtrlBtn(1, blockUid, hlInfo.uid);
btnRepText.addEventListener("click", function (e) {
replaceHl(blockUid, hlInfo.uid, 1, btn)
});
c3u.insertAfter(btnRepText, btn)
}
if (pdfParams.aliasChar !== '') {
btnRepAlias = createCtrlBtn(0, blockUid, hlInfo.uid);
btnRepAlias.addEventListener("click", function (e) {
replaceHl(blockUid, hlInfo.uid, 0, btn)
});
c3u.insertAfter(btnRepAlias, btn)
}
}
}
function createCtrlBtn(asText, btnBlockUid, hlBlockUid) {
const trail = asText ? 'text' : 'alias';
const btnText = asText ? pdfParams.textChar : pdfParams.aliasChar;
const cssClass = 'btn-rep-' + trail;
const newBtn = document.createElement('button');
newBtn.classList.add(cssClass, 'btn', 'btn-default', 'btn-pdf-activated', 'popup');
newBtn.innerText = btnText;
newBtn.title = 'Replace with ' + trail;
newBtn.id = btnBlockUid + hlBlockUid + asText
return newBtn;
}
////////////////////Breadcrumb Addition////////////////////
////////////////////Breadcrumb Placement
let pdf2attr = {}
function addBreadcrumb(btnBlock, pageNumber, pdfUid) {
if (!pdf2attr[pdfUid]) pdf2attr[pdfUid] = findPDFAttribute(pdfUid, pdfParams.breadCrumbAttribute)
btnBlock.firstChild.setAttribute("title", pdf2attr[pdfUid] + "/Pg" + pageNumber);
btnBlock.firstChild.classList.add("breadCrumb");
}
////////////////////Search the sub-tree of HL/PDF's
////////////////////shared parents for the meta info
function findPDFAttribute(pdfUid, attribute) {
let gParentRef;
if (pdfParams.outputHighlighAt === 'cousin') {
gParentRef = c3u.parentBlockUid(c3u.parentBlockUid(pdfUid));
if (!gParentRef) gParentRef = pdfUid;
}
else //child mode
gParentRef = pdfUid; //parentBlockUid(pdfUid);
let ancestorrule = `[
[ (ancestor ?b ?a)
[?a :block/children ?b] ]
[ (ancestor ?b ?a)
[?parent :block/children ?b ]
(ancestor ?parent ?a) ] ] ]`;
const res = window.roamAlphaAPI.q(
`[:find (pull ?block [:block/string])
:in $ %
:where
[?block :block/string ?attr]
[(clojure.string/starts-with? ?attr \"${attribute}:\")]
(ancestor ?block ?gblock)
[?gblock :block/uid \"${gParentRef}\"]]`, ancestorrule)
if (!res.length) return ' ';
// match attribute: or attribute::
const attrMatch = new RegExp(`^${attribute}::?\\s*(.*)$`);
return res[0][0].string.match(attrMatch)[1];
}
///////////////////Main Button Replacement//////////////////
///////////////////Main Button Onclick//////////////////
///////////////HL Reference Replacement: As Text or Alias
function replaceHl(btnBlockUid, hlBlockUid, asText, btn) {
//Prepare the substitute string
const hl = c3u.blockString(hlBlockUid);
const match = hl.match(/\{\{\d+:\s*.........\}\}\s*\[..?\]\(\(\(.........\)\)\)/)
const hlText = hl.substring(0, match.index);
const hlAlias = hlText + "[*](((" + hlBlockUid + ")))";
//Search for what to replace
const blockTxt = c3u.blockString(btnBlockUid);
const re = new RegExp("\\(\\(" + hlBlockUid + "\\)\\)", "g");
let newBlockTxt;
if (asText)
newBlockTxt = blockTxt.replace(re, hlText)
else
newBlockTxt = blockTxt.replace(re, hlAlias)
c3u.updateBlockString(btnBlockUid, newBlockTxt)
const toRemoveSpans = btn.closest('.rm-block__input').querySelectorAll('.displacedBtns')
toRemoveSpans.forEach(item => item.remove())
}
/***************Button Activation END*******************/
/*******************************************************/
/*******************************************************/
/************Handle New HL Received BEGIN***************/
window.addEventListener('message', handleRecievedMessage, false);
///////////Recieve Highlight Data, Output Highlight Text, Store HL Data
function handleRecievedMessage(event) {
switch (event.data.actionType) {
case 'added':
handleNewHighlight(event)
break;
case 'updated':
handleUpdatedHighlight(event)
break;
case 'deleted':
handleDeletedHighlight(event)
break;
case 'openHlBlock':
handleOpenHighlight(event)
break;
case 'copyRef':
handleCopyHighlightRef(event)
break;
}
}
function handleCopyHighlightRef(event) {
const toOpenHlTextUid = event.data.highlight.id;
const toOpenHlDataRowUid = getHighlightDataBlockUid(toOpenHlTextUid);
const hlBlockUid = c3u.blockString(toOpenHlDataRowUid);
navigator.clipboard.writeText("((" + hlBlockUid + "))");
}
function handleOpenHighlight(event) {
const toOpenHlTextUid = event.data.highlight.id;
const toOpenHlDataRowUid = getHighlightDataBlockUid(toOpenHlTextUid);
const hlBlockUid = c3u.blockString(toOpenHlDataRowUid);
c3u.openBlockInSidebar('block', hlBlockUid)
c3u.sleep(30) //prevent multiple block opening.
}
function handleUpdatedHighlight(event) {
const newColorNum = parseInt(event.data.highlight.color);
if (pdfParams.addColoredHighlight) {
const hlId = event.data.highlight.id;
updateHighlightText(newColorNum, hlId);
updateHighlightData(newColorNum, hlId);
}
}
function updateHighlightText(newColorNum, hlId) {
console.log(hlId)
console.log(newColorNum)
let newColorString = '';
switch (newColorNum) {
case 0: newColorString = ""; break;
case 1: newColorString = "yellow"; break;
case 2: newColorString = "red"; break;
case 3: newColorString = "green"; break;
case 4: newColorString = "blue"; break;
case 5: newColorString = "purple"; break;
case 6: newColorString = "grey"; break;
}
// if (newColorString !== '') {
let hlText = c3u.blockString(hlId);
//Separate Perfix and Main Text
const hasPerfix1 = hlText.match(/>(.*)/);
const hasPerfix2 = hlText.match(/\[\[>\]\](.*)/);
let perfix, restTxt;
if (hasPerfix1) {
perfix = '>';
restTxt = hasPerfix1[1];
} else if (hasPerfix2) {
perfix = '[[>]]';
restTxt = hasPerfix2[1];
} else {
perfix = '';
restTxt = hlText;
}
const content = restTxt.match(/(.*)({{\d+:\s*.........}}\s*\[..?\]\(\(\(.........\)\)\))/);
const isImage = restTxt.match(/.*(\!\[.*\]\(.*\))\s*{{\d+:\s*.........}}\s*\[..?\]\(\(\(.........\)\)\)/)
const isHlTxt = restTxt.match(/.*(\^{2}(.*)\^{2}).*/)
let mainContent;
if (isImage)
mainContent = ` ${isImage[1]}`;
else if (isHlTxt) {
mainHl = isHlTxt[1];
mainTxt = isHlTxt[2];
}
else {
mainHl = `^^${content[1]}^^`;
mainTxt = content[1];
}
const trail = content[2];
let colorPerfix = `#h:${newColorString}`;
mainContent = mainHl;
if (newColorString == '') { //Reset color
colorPerfix = '';
mainContent = mainTxt;
}
c3u.updateBlockString(hlId, `${perfix} ${colorPerfix}${mainContent} ${trail}`);
// }
}
function updateHighlightData(newColorNum, toUpdateHlTextUid) {
const toUpdateHlDataRowUid = getHighlightDataBlockUid(toUpdateHlTextUid)
const toUpdateHlInfoUid = c3u.getNthChildUid(toUpdateHlDataRowUid, 0);
const toUpdateHlInfoString = c3u.blockString(toUpdateHlInfoUid);
let toUpdateHlInfo = JSON.parse(toUpdateHlInfoString)
let hlPosition;
if (typeof (toUpdateHlInfo.position) === 'undefined') //if older version highlight
hlPosition = toUpdateHlInfo;
else
hlPosition = toUpdateHlInfo.position;
c3u.updateBlockString(toUpdateHlInfoUid, JSON.stringify({ position: hlPosition, color: newColorNum }));
}
function handleNewHighlight(event) {
if (event.data.highlight.position.rects.length == 0) {
event.data.highlight.position.rects[0] = event.data.highlight.position.boundingRect;
}
const page = event.data.highlight.position.pageNumber;
const hlInfo = JSON.stringify({
position: event.data.highlight.position, color: event.data.highlight.color
});
const iframe = document.getElementById(activePdfIframeId);
const pdfBlockUid = c3u.getUidOfContainingBlock(iframe);
let hlContent;
const pdfAlias = `[${pdfChar}](((${pdfBlockUid})))`;
const hlDataUid = c3u.createUid();
const hlTextUid = event.data.highlight.id;
const hlBtn = `{{${page}: ${hlDataUid}}}`;
if (event.data.highlight.imageUrl) {
hlContent = ``;
} else {
hlContent = `${event.data.highlight.content.text}`;
}
writeHighlightText(pdfBlockUid, hlTextUid, hlBtn, hlContent, pdfAlias, page);
saveHighlightData(pdfBlockUid, decodePdfUrl(iframe.src), hlDataUid, hlTextUid, hlInfo, hlContent);
}
function handleDeletedHighlight(event) {
const toDeleteHlTextUid = event.data.highlight.id;
const toDeleteHlDataRowUid = getHighlightDataBlockUid(toDeleteHlTextUid)
c3u.deleteBlock(toDeleteHlTextUid)
c3u.deleteBlock(toDeleteHlDataRowUid)
}
///////////For the Cousin Output Mode: Find the Uncle of the PDF Block.
function getUncleBlock(pdfBlockUid) {
const pdfParentBlockUid = c3u.parentBlockUid(pdfBlockUid);
const gParentBlockUid = c3u.parentBlockUid(pdfParentBlockUid);
let dictUid2Ord = {};
let dictOrd2Uid = {};
if (!gParentBlockUid) return null;
const mainBlocksUid = c3u.allChildrenInfo(gParentBlockUid);
mainBlocksUid[0][0].children.map(child => {
dictUid2Ord[child.uid] = child.order;
dictOrd2Uid[child.order] = child.uid;
});
//Single assumption: PDF & Highlights are assumed to be siblings.
let hlParentBlockUid = dictOrd2Uid[dictUid2Ord[pdfParentBlockUid] + 1];
if (!hlParentBlockUid) {
hlParentBlockUid = c3u.createUid()
c3u.createChildBlock(gParentBlockUid, dictUid2Ord[pdfParentBlockUid] + 1,
pdfParams.highlightHeading, hlParentBlockUid);
}
return hlParentBlockUid;
}
////////////Write the Highlight Text Using the Given Format
let pdf2citeKey = {}
let pdf2pgOffset = {}
async function writeHighlightText(pdfBlockUid, hlTextUid, hlBtn, hlContent, pdfAlias, page) {
let hlParentBlockUid;
//Find where to write
if (pdfParams.outputHighlighAt === 'cousin') {
hlParentBlockUid = getUncleBlock(pdfBlockUid);
await c3u.sleep(100);
if (!hlParentBlockUid) hlParentBlockUid = pdfBlockUid; //there is no gparent, write hl as a child
} else { //outputHighlighAt ==='child'
hlParentBlockUid = pdfBlockUid
}
//Make the citation
const perfix = (pdfParams.blockQPerfix === '') ? '' : pdfParams.blockQPerfix + ' ';
let Citekey = '';
if (pdfParams.citationFormat !== '') {
if (!pdf2citeKey[pdfBlockUid]) {
pdf2citeKey[pdfBlockUid] = findPDFAttribute(pdfBlockUid, "Citekey")
}
if (!pdf2pgOffset[pdfBlockUid]) {
const tempOffset = parseInt(findPDFAttribute(pdfBlockUid, "Page Offset"));
pdf2pgOffset[pdfBlockUid] = isNaN(tempOffset) ? 0 : tempOffset;
}
Citekey = pdf2citeKey[pdfBlockUid];
page = page - pdf2pgOffset[pdfBlockUid];
}
const citation = eval('`' + pdfParams.citationFormat + '`').replace(/\s+/g, '');
const hlText = `${perfix}${hlContent}${citation} ${hlBtn} ${pdfAlias}`;
let ord = (pdfParams.appendHighlight) ? 'last' : 0;
//Finally: writing
c3u.createChildBlock(hlParentBlockUid, ord, hlText, hlTextUid);
//What to put in clipboard
if (pdfParams.copyBlockRef)
navigator.clipboard.writeText("((" + hlTextUid + "))");
else
navigator.clipboard.writeText(hlContent);
}
///////////Save Annotations in the PDF Data Page in a Table
function saveHighlightData(pdfUid, pdfUrl, hlDataUid, hlTextUid, hlInfo, hlContent) {
const dataTableUid = getDataTableUid(pdfUrl, pdfUid);
c3u.createChildBlock(dataTableUid, 0, hlTextUid, hlDataUid);
const posUid = c3u.createUid();
c3u.createChildBlock(hlDataUid, 0, hlInfo, posUid);
c3u.createChildBlock(posUid, 0, hlContent, c3u.createUid());
}
/************Handle New HL Received END****************/
/*******************************************************/
/*******************************************************/
/**********Render PDF and Highlights BEGIN**************/
/////////////////////Find pdf iframe being highlighted
let allPdfIframes = []; //History of opened pdf on page
let activePdfIframeId = null; //Last active pdf iframe.id
window.addEventListener('blur', function () {
setTimeout(function () {
activePdfIframe = allPdfIframes.find(x => x === document.activeElement);
activePdfIframeId = activePdfIframe?.id;
}, 500)
});
/////////////////////Show the PDF through the Server
function renderPdf(iframe) {
iframe.classList.add('pdf-activated');
iframe.src = encodePdfUrl(iframe.src);
iframe.style.minWidth = `${pdfParams.pdfMinWidth}px`;
iframe.style.minHeight = `${pdfParams.pdfMinHeight}px`;
}
/////////////////////Send Old Saved Highlights to Server to Render
function sendHighlights(iframe, originalPdfUrl, pdfBlockUid) {
const highlights = getAllHighlights(originalPdfUrl, pdfBlockUid);
window.setTimeout( // give it 5 seconds to load
() => iframe.contentWindow.postMessage({ highlights }, '*'), 2000);
}
/////////////////////From PDF URL => Data Page => Retrieve Data
function getAllHighlights(pdfUrl, pdfUid) {
const dataTableUid = getDataTableUid(pdfUrl, pdfUid);
return getHighlightsFromTable(dataTableUid);
}
function getDataTableUid(pdfUrl, pdfUid) {
const pdfDataPageTitle = getDataPageTitle(pdfUrl);
let pdfDataPageUid = c3u.getPageUid(pdfDataPageTitle);
if (!pdfDataPageUid) //If this is the first time uploading the pdf
pdfDataPageUid = createDataPage(pdfDataPageTitle, pdfUrl, pdfUid);
return c3u.getNthChildUid(pdfDataPageUid, 2);
}
function getDataPageTitle(pdfUrl) {
return 'roam/js/pdf/data/' + c3u.hashCode(pdfUrl);
}
/////////////////////Initialize a Data Page. Format is:
/////////////////////pdfPageTitle
/////////////////////////pdfUrl
/////////////////////////pdfUid
/////////////////////////{{table}}
function createDataPage(pdfPageTitle, pdfUrl, pdfUid) {
const pdfDataPageUid = c3u.createPage(pdfPageTitle);
c3u.createChildBlock(pdfDataPageUid, 0, pdfUrl, c3u.createUid());
c3u.createChildBlock(pdfDataPageUid, 1, pdfUid, c3u.createUid());
c3u.createChildBlock(pdfDataPageUid, 2, "{{table}}", c3u.createUid());
return pdfDataPageUid;
}
/***********Render PDF and Highlights END***************/
/*******************************************************/
/*******************************************************/
/*********Helper API Functions BEGIN************/
/////////////////////////////////////////////////////////
//////////////Gather Buttons Information ////////////////
/////////////////////////////////////////////////////////
///////////////Are these highlight buttons?
function isRoamBtn(btn) {
return btn.classList.contains('block-ref-count-button')
|| btn.classList.contains('bp3-minimal')
}
function isInactive(btn) {
return !btn.classList.contains('btn-pdf-activated');
}
function isUnObserved(btn) {
return !btn.classList.contains('btn-observed');
}
function isHighlightBtn(btn) {
return !isRoamBtn(btn)
&& btn.innerText.match(/^\d+$/)
}
function isSortBtn(btn) {
return !isRoamBtn(btn)
&& btn.innerText.match(new RegExp(pdfParams.sortBtnText))
}
function isInactiveSortBtn(btn) {
return isSortBtn(btn) && isInactive(btn)
}
function isInactiveHighlightBtn(btn) {
return isHighlightBtn(btn) && isInactive(btn)
}
function isUnObservedHighlightBtn(btn) {
return isHighlightBtn(btn) && isUnObserved(btn)
}
function encodePdfUrl(url) {
return serverPerfix + encodeURI(url);
}
function decodePdfUrl(url) {
return decodeURI(url).substring(serverPerfix.length);
}
/*********Helper Functions END************/
window.setInterval(initPdf, 1000);
}