1
0
mirror of https://github.com/vbalien/voca.git synced 2025-12-06 19:36:20 +09:00

feat: add dictionary link

This commit is contained in:
2022-02-26 22:15:02 +09:00
parent 8377ee647a
commit 25fe0fb73a
3 changed files with 133 additions and 59 deletions

View File

@@ -1,4 +1,11 @@
export { PDFDocument, PDFPage, rgb } from "https://esm.sh/pdf-lib@^1.11.1";
export {
PDFDocument,
PDFName,
PDFPage,
PDFRef,
PDFString,
rgb,
} from "https://esm.sh/pdf-lib@^1.11.1";
export { default as fontkit } from "https://esm.sh/@pdf-lib/fontkit@^1.0.0";
export { default as React } from "https://esm.sh/react@17";
export { default as ReactDOM } from "https://esm.sh/react-dom@17";

View File

@@ -1,4 +1,13 @@
import { fontkit, PDFDocument, PDFPage, rgb, shuffle } from "./deps.ts";
import {
fontkit,
PDFDocument,
PDFName,
PDFPage,
PDFRef,
PDFString,
rgb,
shuffle,
} from "./deps.ts";
import { JSONType, Voca } from "../types.ts";
function downloadBuffer(buffer: Uint8Array, fileName: string) {
@@ -30,12 +39,18 @@ export async function makePdf(voca_list: Voca[]) {
let page!: PDFPage;
let col = -1;
let yCursor = 1;
let pdfUrlList: PDFRef[] = [];
// 문제 생성
for (let i = 0; i < voca_list.length; ++i) {
const data = voca_list[i];
if (i % perPage === 0) {
if (page) {
page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(pdfUrlList));
}
pdfUrlList = [];
page = pdfDoc.addPage();
yCursor = 1;
col = -1;
}
@@ -43,9 +58,33 @@ export async function makePdf(voca_list: Voca[]) {
col++;
yCursor = 1;
}
page.drawText(`${i + 1}. ${data.word}`, {
x: margin + (page.getWidth() / 2) * col,
y: page.getHeight() - (fontSize + margin) * yCursor,
const text = `${i + 1}. ${data.word}`;
const textWidth = customFont.widthOfTextAtSize(text, fontSize);
const textHeight = customFont.heightAtSize(fontSize);
const textX = margin + (page.getWidth() / 2) * col;
const textY = page.getHeight() - (fontSize + margin) * yCursor;
const pdfUrlDict = pdfDoc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [textX, textY, textX + textWidth, textY + textHeight],
A: {
Type: "Action",
S: "URI",
URI: PDFString.of(
`https://en.dict.naver.com/#/search?range=all&query=${
encodeURIComponent(data.word)
}`,
),
},
});
const pdfUrl = pdfDoc.context.register(pdfUrlDict);
pdfUrlList.push(pdfUrl);
page.drawText(text, {
x: textX,
y: textY,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),
@@ -64,6 +103,10 @@ export async function makePdf(voca_list: Voca[]) {
for (let i = 0; i < voca_list.length; ++i) {
const data = voca_list[i];
if (i % perPage === 0) {
if (page) {
page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(pdfUrlList));
}
pdfUrlList = [];
page = pdfDoc.addPage();
yCursor = 1;
col = -1;
@@ -72,9 +115,33 @@ export async function makePdf(voca_list: Voca[]) {
col++;
yCursor = 1;
}
page.drawText(`${i + 1}. ${data.word}`, {
x: margin + (page.getWidth() / 2) * col,
y: page.getHeight() - (fontSize + margin) * yCursor,
const text = `${i + 1}. ${data.word}`;
const textWidth = customFont.widthOfTextAtSize(text, fontSize);
const textHeight = customFont.heightAtSize(fontSize);
const textX = margin + (page.getWidth() / 2) * col;
const textY = page.getHeight() - (fontSize + margin) * yCursor;
const pdfUrlDict = pdfDoc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [textX, textY, textX + textWidth, textY + textHeight],
A: {
Type: "Action",
S: "URI",
URI: PDFString.of(
`https://en.dict.naver.com/#/search?range=all&query=${
encodeURIComponent(data.word)
}`,
),
},
});
const pdfUrl = pdfDoc.context.register(pdfUrlDict);
pdfUrlList.push(pdfUrl);
page.drawText(text, {
x: textX,
y: textY,
size: fontSize,
font: customFont,
color: rgb(0, 0, 0),