Skip to content

Commit f4f1505

Browse files
committed
Replace PDF previews with hash-based gradient art
1 parent 0c0b3cc commit f4f1505

2 files changed

Lines changed: 12 additions & 26 deletions

File tree

src/components/BatchCard.astro

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ interface Props {
55
id: number;
66
pdf: string;
77
posts: CollectionEntry<"blog">[];
8+
hash: string;
89
}
910
10-
const { id, pdf, posts } = Astro.props as Props;
11+
const { id, pdf, posts, hash } = Astro.props as Props;
12+
13+
const color1 = `#${hash.slice(0, 6)}`;
14+
const color2 = `#${(parseInt(hash.slice(0, 6), 16) ^ 0xffffff)
15+
.toString(16)
16+
.padStart(6, "0")}`;
1117
---
1218
<a href={pdf} class="group block overflow-hidden rounded-lg border border-black/15 dark:border-white/20 hover:bg-black/5 dark:hover:bg-white/5 transition-colors duration-300 ease-in-out">
13-
<canvas id={`preview-${id}`} class="w-full h-48 object-cover bg-black/5 dark:bg-white/10" />
19+
<div class="w-full h-48" style={`background: linear-gradient(135deg, ${color1}, ${color2});`}></div>
1420
<div class="p-4 space-y-2">
1521
<div class="font-semibold">Batch #{id}</div>
1622
<ul class="text-sm list-disc list-inside space-y-0.5">
@@ -20,25 +26,3 @@ const { id, pdf, posts } = Astro.props as Props;
2026
</ul>
2127
</div>
2228
</a>
23-
24-
<script type="module" is:inline>
25-
const pdfUrl = "${pdf}";
26-
const canvasId = "preview-${id}";
27-
import("https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/+esm").then((pdfjsLib) => {
28-
pdfjsLib.GlobalWorkerOptions.workerSrc =
29-
"https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.worker.min.js";
30-
const canvas = document.getElementById(canvasId);
31-
if (!canvas) return;
32-
pdfjsLib.getDocument(pdfUrl).promise.then((pdfDoc) => {
33-
pdfDoc.getPage(1).then((page) => {
34-
const viewport = page.getViewport({ scale: 1 });
35-
const scale = canvas.clientWidth / viewport.width;
36-
const scaledViewport = page.getViewport({ scale });
37-
canvas.width = scaledViewport.width;
38-
canvas.height = scaledViewport.height;
39-
const context = canvas.getContext("2d");
40-
page.render({ canvasContext: context, viewport: scaledViewport });
41-
});
42-
});
43-
});
44-
</script>

src/pages/batch/index.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const blog = (await getCollection("blog"))
1616
1717
const batches = pdfFiles
1818
.map((file) => {
19-
const [, idStr] = file.split("_");
19+
const [, idStr, hashWithExt] = file.split("_");
2020
const id = parseInt(idStr, 10);
21+
const hash = hashWithExt.replace(/\.pdf$/, "");
2122
const posts = blog.slice(id * 5, id * 5 + 5);
2223
return {
2324
id,
2425
pdf: `/batch/${file}`,
2526
posts,
27+
hash,
2628
};
2729
})
2830
.sort((a, b) => b.id - a.id);
@@ -43,7 +45,7 @@ const batches = pdfFiles
4345
</section>
4446
<div class="grid gap-6 md:grid-cols-2">
4547
{batches.map((batch) => (
46-
<BatchCard id={batch.id} pdf={batch.pdf} posts={batch.posts} />
48+
<BatchCard id={batch.id} pdf={batch.pdf} posts={batch.posts} hash={batch.hash} />
4749
))}
4850
</div>
4951
</div>

0 commit comments

Comments
 (0)