{"version":3,"file":"commands.cjs","names":["RenderToken","SNAPSHOT_TOOL","DocumentsToken"],"sources":["../src/commands.ts"],"sourcesContent":["/**\n * @embedpdf/plugin-render/commands — the snapshot's commands: the snapshot\n * tool, and copy, save and clear for the snapshot it holds. Copy and save\n * reach the browser (the clipboard, a file download), which plugins never\n * touch, so the list is made with the browser's part; each framework's\n * `/render` has it ready as `renderCommands`. The i18n plugin has their\n * labels.\n */\nimport { DocumentsToken, type CommandContext, type CommandDefinition } from '@embedpdf/core';\nimport { toolCommand } from '@embedpdf/plugin-interaction/commands';\n\nimport { RenderToken, SNAPSHOT_TOOL, type PageImage } from './contract';\n\n/** What the snapshot's commands need from the browser. */\nexport interface RenderCommandsPlatform {\n  /**\n   * Copy an image to the clipboard. Called in the press or key itself, before\n   * the image is there: a browser may take the clipboard only then.\n   */\n  copyImage(image: Promise<PageImage>): Promise<unknown>;\n  /** Give the user the bytes as a file download, of the given content type. */\n  saveFile(bytes: Uint8Array | Blob, fileName: string, contentType: string): void;\n}\n\nconst snapshotOf = (context: CommandContext) => context.tryGet(RenderToken)?.getSnapshot() ?? null;\n\n/** The snapshot's commands: `tool:snapshot` and `snapshot:*`, with the browser's part from `platform`. */\nexport function createRenderCommands(\n  platform: RenderCommandsPlatform,\n): readonly CommandDefinition[] {\n  return [\n    // The snapshot tool, for a user who may take snapshots.\n    {\n      ...toolCommand(SNAPSHOT_TOOL),\n      id: 'tool:snapshot',\n      allowed: (context) => context.tryGet(RenderToken)?.canSnapshot() ?? true,\n      notAllowedKey: 'commands.notAllowed.doc-content-copy',\n    },\n\n    // ── the snapshot the snapshot tool holds ──\n    {\n      id: 'snapshot:copy',\n      labelKey: 'commands.snapshot.copy',\n      label: 'Copy',\n      icon: 'copy',\n      shortcut: 'Mod+C',\n      categories: ['snapshot'],\n      // Straight to the clipboard, in the press itself: the picture follows when it's there.\n      run: (context) => {\n        const snapshot = snapshotOf(context);\n        return snapshot ? platform.copyImage(snapshot.image) : undefined;\n      },\n      enabled: (context) => snapshotOf(context) !== null,\n    },\n    {\n      id: 'snapshot:save',\n      labelKey: 'commands.snapshot.save',\n      label: 'Save image',\n      icon: 'download',\n      categories: ['snapshot'],\n      run: async (context) => {\n        const snapshot = snapshotOf(context);\n        if (!snapshot) return;\n        const documents = context.get(DocumentsToken);\n        const id = context.documentId ?? undefined;\n        const blob = await (await snapshot.image).blob();\n        const number =\n          documents\n            .listPages(id)\n            .findIndex((page) => page.ref.objectNumber === snapshot.page.objectNumber) + 1;\n        platform.saveFile(blob, snapshotFileNameOf(documents.get(id)?.name, number), 'image/png');\n      },\n      enabled: (context) => snapshotOf(context) !== null,\n    },\n    {\n      id: 'snapshot:clear',\n      labelKey: 'commands.snapshot.clear',\n      label: 'Clear snapshot',\n      shortcut: 'Escape',\n      categories: ['snapshot'],\n      run: (context) => context.get(RenderToken).clearSnapshot(),\n      enabled: (context) => snapshotOf(context) !== null,\n    },\n  ];\n}\n\n/** A snapshot's file name: the document's name and the page's number, as a PNG (`Report p3.png`). */\nfunction snapshotFileNameOf(name: string | undefined, pageNumber: number): string {\n  const base = (name ?? 'document').replace(/\\.pdf$/i, '');\n  return `${base} p${pageNumber}.png`;\n}\n"],"mappings":";;;;;;;;;;;;;AAwBA,MAAM,cAAc,YAA4B,QAAQ,OAAOA,iBAAAA,WAAW,CAAC,EAAE,YAAY,KAAK;;AAG9F,SAAgB,qBACd,UAC8B;CAC9B,OAAO;EAEL;GACE,IAAA,GAAA,sCAAA,YAAA,CAAeC,iBAAAA,aAAa;GAC5B,IAAI;GACJ,UAAU,YAAY,QAAQ,OAAOD,iBAAAA,WAAW,CAAC,EAAE,YAAY,KAAK;GACpE,eAAe;EACjB;EAGA;GACE,IAAI;GACJ,UAAU;GACV,OAAO;GACP,MAAM;GACN,UAAU;GACV,YAAY,CAAC,UAAU;GAEvB,MAAM,YAAY;IAChB,MAAM,WAAW,WAAW,OAAO;IACnC,OAAO,WAAW,SAAS,UAAU,SAAS,KAAK,IAAI,KAAA;GACzD;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