Beautiful pdf components, built on Takumi and Forme. 100% Free, Zero config, one command setup.
refactor: drop the unused HTML preview from the playground worker
Kane Wangabc098ad0e8b
diff --git a/components/playground/geometry.ts b/components/playground/geometry.tsdeleted file mode 100644index 746fbc0..0000000--- a/components/playground/geometry.ts+++ /dev/null@@ -1,63 +0,0 @@-import type { PageMarginSide } from "takumi-pdf";--const PX_PER_MM = 96 / 25.4;-const PAGE_SIZES = {- a3: { height: 420 * PX_PER_MM, width: 297 * PX_PER_MM },- a4: { height: 297 * PX_PER_MM, width: 210 * PX_PER_MM },- a5: { height: 210 * PX_PER_MM, width: 148 * PX_PER_MM },- b4: { height: 353 * PX_PER_MM, width: 250 * PX_PER_MM },- b5: { height: 250 * PX_PER_MM, width: 176 * PX_PER_MM },- "jis-b4": { height: 364 * PX_PER_MM, width: 257 * PX_PER_MM },- "jis-b5": { height: 257 * PX_PER_MM, width: 182 * PX_PER_MM },- ledger: { height: 17 * 96, width: 11 * 96 },- legal: { height: 14 * 96, width: 8.5 * 96 },- letter: { height: 11 * 96, width: 8.5 * 96 },-};-const DEFAULT_PAGE_MARGIN = 48;--type PdfOptions = NonNullable<PlaygroundOptions["pdf"]>;--export interface OutputGeometry {- width: number;- height?: number;- padding?: string;- label: string;-}--// An auto margin is the height of a band the renderer measures, which this HTML-// preview cannot know, so it stands in the default.-const sidePadding = (side: PageMarginSide): number =>- side === "auto" ? DEFAULT_PAGE_MARGIN : side;--const marginPadding = (margin: PdfOptions["margin"]): string => {- if (margin === undefined || margin === "auto") {- return `${DEFAULT_PAGE_MARGIN}px`;- }- if (typeof margin === "number") {- return `${margin}px`;- }- const { top = 0, right = 0, bottom = 0, left = 0 } = margin;- return `${sidePadding(top)}px ${sidePadding(right)}px ${sidePadding(bottom)}px ${sidePadding(left)}px`;-};--const pdfGeometry = (pdf: PdfOptions): OutputGeometry => {- const size =- typeof pdf.size === "object" ? pdf.size : PAGE_SIZES[pdf.size ?? "a4"];- const preset = typeof pdf.size === "object" ? undefined : (pdf.size ?? "a4");- const width = Math.round(pdf.landscape ? size.height : size.width);- const height = Math.round(pdf.landscape ? size.width : size.height);- const name = preset ? preset.toUpperCase() : `${width} × ${height}`;- return {- label: pdf.landscape ? `${name} landscape` : name,- padding: marginPadding(pdf.margin),- width,- };-};--export const outputGeometry = (options: PlaygroundOptions): OutputGeometry => {- if (options.pdf) {- return pdfGeometry(options.pdf);- }- const { width = 1200, height = 630 } = options;- return { height, label: `${width} × ${height}`, width };-};diff --git a/components/playground/schema.ts b/components/playground/schema.tsindex fde917b..b890f53 100644--- a/components/playground/schema.ts+++ b/components/playground/schema.ts@@ -56,21 +56,10 @@ const readySchema = z.object({type: z.literal("ready"),});-const previewResultSchema = z.object({- cssContents: z.optional(z.array(z.string())),- height: z.optional(z.int().check(z.positive(), z.minimum(1))),- html: z.string(),- id: z.int().check(z.positive(), z.minimum(1)),- padding: z.optional(z.string()),- type: z.literal("preview-result"),- width: z.optional(z.int().check(z.positive(), z.minimum(1))),-});-export const messageSchema = z.discriminatedUnion("type", [renderRequestSchema,renderResultSchema,readySchema,- previewResultSchema,]);export type RenderMessageInput = z.input<typeof messageSchema>;diff --git a/components/playground/use-render-worker.ts b/components/playground/use-render-worker.tsindex 37512f6..69719fe 100644--- a/components/playground/use-render-worker.ts+++ b/components/playground/use-render-worker.ts@@ -44,9 +44,6 @@ export const useRenderWorker = (ranCode: string | undefined) => {case "render-request": {throw new Error("request is not possible for response");}- case "preview-result": {- break;- }case "render-result": {const { result } = message;if (result.id !== currentRequestIdRef.current) {diff --git a/components/playground/worker.ts b/components/playground/worker.tsindex 079feab..fb549d6 100644--- a/components/playground/worker.ts+++ b/components/playground/worker.ts@@ -1,11 +1,9 @@import type * as React from "react";-import { renderToStaticMarkup } from "react-dom/server";import initPdf, { render } from "takumi-pdf/no-init";import type { PageSize } from "takumi-pdf/no-init";import wasmUrl from "takumi-pdf/wasm-url";import { evaluateCodeExports, renderReact } from "./evaluate";-import { outputGeometry } from "./geometry";import { inspectPdf } from "./inspect-pdf";import { messageSchema } from "./schema";import type { RenderMessageInput } from "./schema";@@ -15,6 +13,15 @@ const postMessage = (message: RenderMessageInput, transfer?: Transferable[]) =>const PREVIEW_LOGO_PATH = "/favicon.png";+const pageLabel = (size: PageSize | undefined, landscape: boolean): string => {+ const name =+ typeof size === "object"+ ? `${Math.round(size.width)} × ${Math.round(size.height)}`+ : (size ?? "a4").toUpperCase();++ return landscape ? `${name} landscape` : name;+};+let imagesReady:| Promise<{sources: { data: ArrayBuffer; src: string }[];@@ -57,24 +64,11 @@ const renderRequest = async (id: number, code: string) => {const element = renderReact.createElement(component as React.JSXElementConstructor<unknown>);- const geometry = outputGeometry(options);-- postMessage({- height: geometry.height,- html: renderToStaticMarkup(element),- id,- padding: geometry.padding,- type: "preview-result",- width: geometry.width,- });-const start = performance.now();const pdfOptions = options.pdf ?? ({} as Record<string, unknown>);const pdfBytes = await render(element, {images: await getImages(),- margin: pdfOptions.margin ?? { bottom: 48, left: 48, right: 48, top: 48 },- size: (pdfOptions.size as PageSize | undefined) ?? "a4",...pdfOptions,});@@ -86,7 +80,10 @@ const renderRequest = async (id: number, code: string) => {duration,id,inspection: inspectPdf(pdfBytes),- label: geometry.label,+ label: pageLabel(+ pdfOptions.size as PageSize | undefined,+ pdfOptions.landscape === true+ ),outputBuffer: pdfBytes,outputFormat: "pdf",outputKind: "pdf",@@ -119,8 +116,7 @@ self.onmessage = async (event: MessageEvent) => {break;}case "ready":- case "render-result":- case "preview-result": {+ case "render-result": {throw new Error("Respond message should not be sent from main window.");}default: {