import { defineAction } from "@agent-native/core"; import { resolveAccess } from "@agent-native/core/sharing"; import { eq } from "drizzle-orm"; import { z } from "zod"; import { getDb, schema } from "../server/db/index.js"; import { designDataForAccessRole } from "../server/lib/design-data-access.js"; import { injectHiddenLayerExportStyle } from "../server/lib/design-export.js"; import { isBoardFile } from "../shared/board-file.js"; import "../server/db/index.js"; // ensure registerShareableResource runs export default defineAction({ description: "Prepare design data for client-side PDF export. Returns the design data " + "and files needed for the client to render and generate a PDF.", schema: z.object({ id: z.string().describe("Design ID to export"), }), readOnly: true, http: { method: "GET" }, run: async ({ id }) => { const access = await resolveAccess("design", id); if (!access) throw new Error(`Design not found: ${id}`); const row = access.resource; const db = getDb(); // Fetch all design files const files = await db .select() .from(schema.designFiles) .where(eq(schema.designFiles.designId, id)); const exportFiles = files.filter((file) => !isBoardFile(file.filename)); return { id: row.id, title: row.title, description: row.description, projectType: row.projectType, data: designDataForAccessRole(row.data ?? null, access.role), files: exportFiles.map((f) => ({ id: f.id, filename: f.filename, fileType: f.fileType, // Layers toggled hidden in the editor are only suppressed by the live // editor bridge; inject the same display:none rule so the client-side // PDF render (html2canvas over this HTML) doesn't reveal them. content: f.fileType === "html" && f.content ? injectHiddenLayerExportStyle(f.content) : f.content, })), exportInfo: { format: "pdf", note: "The Design editor renders the active artboard as a single-page raster PDF at its fixed authored size. This preserves visual appearance but does not provide selectable/vector text; export separate screens individually.", }, }; }, });