1
0
mirror of https://github.com/vbalien/voca.git synced 2025-12-06 11:26:21 +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

File diff suppressed because one or more lines are too long

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 fontkit } from "https://esm.sh/@pdf-lib/fontkit@^1.0.0";
export { default as React } from "https://esm.sh/react@17"; export { default as React } from "https://esm.sh/react@17";
export { default as ReactDOM } from "https://esm.sh/react-dom@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"; import { JSONType, Voca } from "../types.ts";
function downloadBuffer(buffer: Uint8Array, fileName: string) { function downloadBuffer(buffer: Uint8Array, fileName: string) {
@@ -30,12 +39,18 @@ export async function makePdf(voca_list: Voca[]) {
let page!: PDFPage; let page!: PDFPage;
let col = -1; let col = -1;
let yCursor = 1; let yCursor = 1;
let pdfUrlList: PDFRef[] = [];
// 문제 생성 // 문제 생성
for (let i = 0; i < voca_list.length; ++i) { for (let i = 0; i < voca_list.length; ++i) {
const data = voca_list[i]; const data = voca_list[i];
if (i % perPage === 0) { if (i % perPage === 0) {
if (page) {
page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(pdfUrlList));
}
pdfUrlList = [];
page = pdfDoc.addPage(); page = pdfDoc.addPage();
yCursor = 1; yCursor = 1;
col = -1; col = -1;
} }
@@ -43,9 +58,33 @@ export async function makePdf(voca_list: Voca[]) {
col++; col++;
yCursor = 1; yCursor = 1;
} }
page.drawText(`${i + 1}. ${data.word}`, {
x: margin + (page.getWidth() / 2) * col, const text = `${i + 1}. ${data.word}`;
y: page.getHeight() - (fontSize + margin) * yCursor, 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, size: fontSize,
font: customFont, font: customFont,
color: rgb(0, 0, 0), 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) { for (let i = 0; i < voca_list.length; ++i) {
const data = voca_list[i]; const data = voca_list[i];
if (i % perPage === 0) { if (i % perPage === 0) {
if (page) {
page.node.set(PDFName.of("Annots"), pdfDoc.context.obj(pdfUrlList));
}
pdfUrlList = [];
page = pdfDoc.addPage(); page = pdfDoc.addPage();
yCursor = 1; yCursor = 1;
col = -1; col = -1;
@@ -72,9 +115,33 @@ export async function makePdf(voca_list: Voca[]) {
col++; col++;
yCursor = 1; yCursor = 1;
} }
page.drawText(`${i + 1}. ${data.word}`, {
x: margin + (page.getWidth() / 2) * col, const text = `${i + 1}. ${data.word}`;
y: page.getHeight() - (fontSize + margin) * yCursor, 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, size: fontSize,
font: customFont, font: customFont,
color: rgb(0, 0, 0), color: rgb(0, 0, 0),