Skip to content

Commit 0f70ad4

Browse files
committed
add keyboard shortcuts for bulk-printing
1 parent c6ed439 commit 0f70ad4

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ If you'd like to host your own files, you'll have to fork this repository.
4545
* Font-size
4646
* Line-spacing
4747
* Light-Mode / Dark-Mode
48+
### Shortcuts
49+
In view-mode, when visiting your web-page containing the rendered Markdown files, you may use the following keyboard-shortcuts:
50+
51+
| Keys | Explanation |
52+
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
53+
| CTRL + ALT + d | Opens the `print-the-document-as-pdf` view in the same window you are currently in, without opening it in a new tab. |
54+
| CTRL + ALT + p | Opens the `print-the-document-as-presentation` view in the same window you are currently in, without opening it in a new tab. |
4855
## Technical Explanation
4956
The purpose of this repository is to provide a platform for all lecture-notes and interactive material for the teacher and students.
5057
It hides behind a configurable Keycloak Authorization Server (version >16 as far as I can tell, most current version is recommended) and thus your material is not publicly available, circumventing various license's restrictions.

obsidian-page.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ window.addEventListener("scroll", function () {
1010
markTopAnchor();
1111
});
1212

13+
/**
14+
* Opens print-document-as-pdf or print-presentation-as-pdf page in new tab.
15+
*/
16+
window.addEventListener(
17+
"keydown",
18+
(event) => {
19+
if (event.defaultPrevented) {
20+
return; // Do nothing if the event was already processed
21+
}
22+
if (event.ctrlKey && event.altKey) {
23+
if (event.key === "p" || event.key === "P") {
24+
console.log("Opening print-presentation page");
25+
openAsPresentation(true, true);
26+
event.preventDefault();
27+
}
28+
if (event.key === "d" || event.key === "d") {
29+
console.log("Opening print-document page");
30+
openAsDocument(true);
31+
event.preventDefault();
32+
}
33+
return;
34+
}
35+
},
36+
true
37+
);
38+
1339
function markTopAnchor() {
1440
const mainAnchors = document.querySelectorAll(".docanchor");
1541
const sidebarAnchors = document.querySelectorAll(".sidebar-anchors a");
@@ -165,19 +191,27 @@ function toggleTopdownMenu() {
165191
}
166192
}
167193

168-
function openAsPresentation(print) {
194+
function openAsPresentation(print, sameWindow = false) {
169195
let url = new URL(window.location.href);
170196
if (print) {
171-
url.searchParams.set("print-pdf", "true")
197+
url.searchParams.set("print-pdf", "true");
172198
}
173199
url.searchParams.set("reveal", "true");
174-
window.open(url, "_blank");
200+
if (sameWindow) {
201+
window.location.href = url;
202+
} else {
203+
window.open(url, "_blank");
204+
}
175205
}
176206

177-
function openAsDocument() {
207+
function openAsDocument(sameWindow = false) {
178208
const url = new URL(window.location.href);
179209
url.searchParams.set("document", "true");
180-
window.open(url, "_blank");
210+
if (sameWindow) {
211+
window.location.href = url;
212+
} else {
213+
window.open(url, "_blank");
214+
}
181215
}
182216

183217
let mainFontsArray = [];
@@ -386,27 +420,36 @@ function toggleViewExam() {
386420
}
387421

388422
/*
389-
* This script is used to resize the right panel by dragging the border.
390-
*/
423+
* This script is used to resize the right panel by dragging the border.
424+
*/
391425
const BORDER_SIZE = 4;
392426
const panel = document.getElementById("sidebar");
393427

394428
let m_pos;
395-
function resize(e){
429+
function resize(e) {
396430
const dx = m_pos - e.x;
397431
m_pos = e.x;
398-
panel.style.width = (parseInt(getComputedStyle(panel, '').width) - dx) + "px";
432+
panel.style.width = parseInt(getComputedStyle(panel, "").width) - dx + "px";
399433
}
400434

401-
panel.addEventListener("mousedown", function(e){
402-
const cs = getComputedStyle(panel, '')
403-
const w = parseInt(cs.width) + parseInt(cs.paddingLeft) + parseInt(cs.paddingRight)
404-
if (e.offsetX >= w - BORDER_SIZE) {
405-
m_pos = e.x;
406-
document.addEventListener("mousemove", resize, false);
407-
}
408-
}, false);
435+
panel.addEventListener(
436+
"mousedown",
437+
function (e) {
438+
const cs = getComputedStyle(panel, "");
439+
const w =
440+
parseInt(cs.width) + parseInt(cs.paddingLeft) + parseInt(cs.paddingRight);
441+
if (e.offsetX >= w - BORDER_SIZE) {
442+
m_pos = e.x;
443+
document.addEventListener("mousemove", resize, false);
444+
}
445+
},
446+
false
447+
);
409448

410-
document.addEventListener("mouseup", function(){
411-
document.removeEventListener("mousemove", resize, false);
412-
}, false);
449+
document.addEventListener(
450+
"mouseup",
451+
function () {
452+
document.removeEventListener("mousemove", resize, false);
453+
},
454+
false
455+
);

obsidian.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,12 +1228,12 @@ async function getTopdownMenu(req) {
12281228
: ""
12291229
}
12301230
<div style="padding: 0px; margin: 0px; margin-left: 0px; margin-top: 25px; margin-bottom: -10px; text-align: left; display: flex;">
1231-
<button class="sl-button" style="height: 32px; margin: 0px;" onclick="openAsPresentation(true)">${lucideIcon(
1231+
<button class="sl-button" style="height: 32px; margin: 0px;" onclick="openAsPresentation(true, false)">${lucideIcon(
12321232
"Printer"
12331233
)}
12341234
${lucideIcon("Presentation")}
12351235
</button>
1236-
<button class="sl-button" style="height: 32px; margin: 0px; margin-left: 6px" onclick="openAsDocument(true)">${lucideIcon(
1236+
<button class="sl-button" style="height: 32px; margin: 0px; margin-left: 6px" onclick="openAsDocument(false)">${lucideIcon(
12371237
"Printer"
12381238
)}
12391239
${lucideIcon("ReceiptText")}

0 commit comments

Comments
 (0)