-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
181 lines (157 loc) · 6.16 KB
/
index.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
class PdfHistory {
constructor() {
this.locations = [];
this.current_idx = -1;
}
add(pre, post) {
if (this.locations.length == 0) {
this.locations = [pre, post];
this.current_idx = 1;
return;
}
this.locations[this.current_idx] = pre;
if (this.locations.length === ++this.current_idx) { // there are no forwards
this.locations.push(post);
} else {
this.locations[this.current_idx] = post;
this.locations.splice(this.current_idx + 1)
}
}
back() {
console.log("Back");
if (this.canGoBack()) this.locations[--this.current_idx].scrollIntoView();
// console.log(this.locations, this.current_idx);
}
forward() {
console.log("Forward", this.locations);
if (this.canGoForward()) this.locations[++this.current_idx].scrollIntoView();
// console.log(this.locations, this.current_idx);
}
canGoBack() {
return this.current_idx > 0;
}
canGoForward() {
return this.current_idx < this.locations.length - 1;
}
}
class pdfLocation {
constructor(pageNumber, left, top) {
this.page = pageNumber;
this.left = left;
this.top = top;
}
scrollIntoView() {
const viewer = top?.window.lsActivePdfViewer || top?.window.lsPdfViewer;
viewer.scrollPageIntoView({
pageNumber: this.page,
destArray: [null, { name: "XYZ" },
this.left, this.top]
});
}
}
function waitForElement(selector) {
return new Promise(resolve => {
if (top?.document.querySelector(selector)) {
return resolve(top?.document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (top?.document.querySelector(selector)) {
resolve(top?.document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(top?.document.querySelector("div#app-single-container"), {
childList: true,
subtree: true
});
});
}
function initialize(viewer) {
if (viewer?._pluginPdfHistroy) return
const ele = viewer.container
const pdfHistory = viewer._pluginPdfHistroy = new PdfHistory();
function callback(e) {
const ele = e.srcElement;
if (ele.tagName !== 'A' || (ele.className !== "internalLink" && !ele.closest('[data-internal-link]')))
return;
/* An example of viewer._location
{
"pageNumber": 235,
"scale": "auto",
"top": 784,
"left": 0,
"rotation": 0,
"pdfOpenParams": "#page=235&zoom=auto,0,784"
} */
const pre = new pdfLocation(viewer._location.pageNumber, viewer._location.left, viewer._location.top);
setTimeout(() => {
// console.log(viewer.scroll)
const post = new pdfLocation(viewer._location.pageNumber, viewer._location.left, viewer._location.top);
pdfHistory.add(pre, post);
}, 100);
// console.log({ pdfHistory });
}
setTimeout(() => {
const toolbar = top?.document.querySelector(".extensions__pdf-toolbar > div > div.buttons");
let bck_btn = top?.document.createElement("a");
bck_btn.innerHTML = left_arr;
let fwd_btn = top?.document.createElement("a");
fwd_btn.innerHTML = right_arr;
bck_btn.onclick = pdfHistory.back.bind(pdfHistory); // fix `this` issue
fwd_btn.onclick = pdfHistory.forward.bind(pdfHistory);
fwd_btn.style.margin = "auto"; // center the svg vertically
bck_btn.style.margin = "auto";
bck_btn.style.marginLeft = "4px";
fwd_btn.style.padding = "4px";
bck_btn.style.padding = "4px";
toolbar.insertBefore(fwd_btn, toolbar.children[0]);
toolbar.insertBefore(bck_btn, toolbar.children[0]);
ele.addEventListener('click', callback, false);
}, 64);
}
async function main() {
const pdfOwner = top?.document.body
const getActivePdfViewer = () => top?.lsActivePdfViewer || top?.lsPdfViewer;
const pdfOwnerObserver = new MutationObserver(async () => {
if (pdfOwner.classList.contains('is-pdf-active')) {
await waitForElement('.extensions__pdf-toolbar')
const viewer = getActivePdfViewer();
if (!viewer) return
initialize(viewer);
}
})
pdfOwnerObserver.observe(pdfOwner, {attributes: true})
// shortcuts
logseq.App.registerCommandPalette({
key: 'pdf-nav-backward',
label: 'Navigate PDF viewer: backward',
keybinding: { binding: 'alt+z'}
}, () => getActivePdfViewer()?._pluginPdfHistroy.back())
logseq.App.registerCommandPalette({
key: 'pdf-nav-forward',
label: 'Navigate PDF viewer: forward',
keybinding: { binding: 'shift+alt+z'}
}, () => getActivePdfViewer()?._pluginPdfHistroy.forward())
logseq.beforeunload(() => {
pdfOwnerObserver.disconnect()
})
}
const left_arr = `
<svg stroke="currentColor" fill="none" width="16" class="icon" stroke-width="2" stroke-linejoin="round" viewBox="0 0 24 24" stroke-linecap="round" height="16">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<line y1="12" x1="5" y2="12" x2="19"></line>
<line y1="16" x1="9" y2="12" x2="5"></line>
<line y1="8" x1="9" y2="12" x2="5"></line>
</svg>`;
const right_arr = `
<svg stroke="currentColor" fill="none" width="16" class="icon" stroke-width="2" stroke-linejoin="round" viewBox="0 0 24 24" stroke-linecap="round" height="16">
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
<line y1="12" x1="5" y2="12" x2="19"></line>
<line y1="16" x1="15" y2="12" x2="19"></line>
<line y1="8" x1="15" y2="12" x2="19"></line>
</svg>`;
const isDebug = false;
logseq.ready(() => {
isDebug && logseq.App.showMsg("❤️ logseq-pdf-nav has been loaded!");
main();
}).catch(console.error);