Skip to content

Commit b52dbdd

Browse files
committed
allow export to Png
1 parent db6c356 commit b52dbdd

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"d3-force-3d": "^3.0.5",
6060
"easymde": "^2.19.0",
6161
"glob": "^11.0.0",
62+
"html2canvas": "^1.4.1",
6263
"htmx.org": "^2.0.3",
6364
"jquery": "^3.7.1",
6465
"js-cookie": "^3.0.5",

timetable/static/bundled/timetable/generator-index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import html2canvas from "html2canvas";
2+
13
// see https://regex101.com/r/QHSaPM/2
24
const TIMETABLE_ROW_RE: RegExp =
35
/^(?<ueCode>[A-Z\d]{4}(?:\+[A-Z\d]{4})?)\s+(?<courseType>[A-Z]{2}\d)\s+((?<weekGroup>[AB])\s+)?(?<weekday>(lundi)|(mardi)|(mercredi)|(jeudi)|(vendredi)|(samedi)|(dimanche))\s+(?<startHour>\d{2}:\d{2})\s+(?<endHour>\d{2}:\d{2})\s+[\dA-B]\s+(?:[\wé]*\s+)?(?<room>\w+(?:, \w+)?)$/;
@@ -117,5 +119,16 @@ document.addEventListener("alpine:init", () => {
117119
left: `${this.displayedWeekdays.indexOf(slot.weekday) * SLOT_WIDTH}px`,
118120
};
119121
},
122+
123+
async savePng() {
124+
const elem = document.getElementById("timetable");
125+
const img = (await html2canvas(elem)).toDataURL();
126+
const downloadLink = document.createElement("a");
127+
downloadLink.href = img;
128+
downloadLink.download = "edt.png";
129+
document.body.appendChild(downloadLink);
130+
downloadLink.click();
131+
downloadLink.remove();
132+
},
120133
}));
121134
});

timetable/static/timetable/css/generator.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#timetable {
2+
display: block;
3+
margin: 2em auto ;
24
.header {
35
background-color: white;
46
box-shadow: none;

timetable/templates/timetable/generator.jinja

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<div
2929
id="timetable"
3030
x-show="table.height > 0 && table.width > 0"
31-
:style="{width: `${table.width}px`, height: `${table.height}px`}"
31+
:style="{width: `${table.width}px`, height: `${table.height+40}px`}"
3232
>
3333
<div class="header">
3434
<template x-for="weekday in displayedWeekdays">
@@ -45,5 +45,12 @@
4545
</template>
4646
</div>
4747
</div>
48+
<button
49+
class="margin-bottom btn btn-blue"
50+
@click="savePng"
51+
x-show="table.height > 0 && table.width > 0"
52+
>
53+
{% trans %}Save to PNG{% endtrans %}
54+
</button>
4855
</div>
4956
{% endblock content %}

0 commit comments

Comments
 (0)