|
| 1 | +<script lang="ts"> |
| 2 | + import { Viewport } from '@embedpdf/plugin-viewport/svelte'; |
| 3 | + import { Scroller, type RenderPageProps } from '@embedpdf/plugin-scroll/svelte'; |
| 4 | + import { RenderLayer } from '@embedpdf/plugin-render/svelte'; |
| 5 | + import { useExport } from '@embedpdf/plugin-export/svelte'; |
| 6 | + import { SelectionLayer } from '@embedpdf/plugin-selection/svelte'; |
| 7 | + import { AnnotationLayer } from '@embedpdf/plugin-annotation/svelte'; |
| 8 | + import { PagePointerProvider } from '@embedpdf/plugin-interaction-manager/svelte'; |
| 9 | + import { useZoom } from '@embedpdf/plugin-zoom/svelte'; |
| 10 | + import { useFormCapability } from '@embedpdf/plugin-form/svelte'; |
| 11 | + import { Download, ZoomIn, ZoomOut } from 'lucide-svelte'; |
| 12 | +
|
| 13 | + interface Props { |
| 14 | + documentId: string; |
| 15 | + } |
| 16 | +
|
| 17 | + let { documentId }: Props = $props(); |
| 18 | +
|
| 19 | + const zoom = useZoom(() => documentId); |
| 20 | + const exportApi = useExport(() => documentId); |
| 21 | + const formCapability = useFormCapability(); |
| 22 | + let formValues = $state<Record<string, string>>({}); |
| 23 | +
|
| 24 | + $effect(() => { |
| 25 | + if (!formCapability.provides || !documentId) return; |
| 26 | +
|
| 27 | + const scope = formCapability.provides.forDocument(documentId); |
| 28 | + formValues = scope.getFormValues(); |
| 29 | +
|
| 30 | + const unsubReady = scope.onFormReady(() => { |
| 31 | + formValues = scope.getFormValues(); |
| 32 | + }); |
| 33 | + const unsubChange = scope.onFieldValueChange(() => { |
| 34 | + formValues = scope.getFormValues(); |
| 35 | + }); |
| 36 | +
|
| 37 | + return () => { |
| 38 | + unsubReady(); |
| 39 | + unsubChange(); |
| 40 | + }; |
| 41 | + }); |
| 42 | +</script> |
| 43 | + |
| 44 | +<div |
| 45 | + class="overflow-hidden rounded-lg border border-gray-300 bg-white shadow-sm dark:border-gray-700 dark:bg-gray-900" |
| 46 | + style="user-select: none" |
| 47 | +> |
| 48 | + <div |
| 49 | + class="flex flex-wrap items-center justify-between gap-3 border-b border-gray-300 bg-gray-100 px-3 py-2 dark:border-gray-700 dark:bg-gray-800" |
| 50 | + > |
| 51 | + <div class="flex items-center gap-1.5"> |
| 52 | + <button |
| 53 | + type="button" |
| 54 | + onclick={() => zoom.provides?.zoomOut()} |
| 55 | + class="inline-flex h-8 w-8 items-center justify-center rounded-md bg-white text-gray-600 shadow-sm ring-1 ring-gray-300 transition-all hover:bg-gray-50 hover:text-gray-900 dark:bg-gray-700 dark:text-gray-300 dark:ring-gray-600 dark:hover:bg-gray-600 dark:hover:text-gray-100" |
| 56 | + title="Zoom Out" |
| 57 | + > |
| 58 | + <ZoomOut size={16} /> |
| 59 | + </button> |
| 60 | + |
| 61 | + <button |
| 62 | + type="button" |
| 63 | + onclick={() => zoom.provides?.zoomIn()} |
| 64 | + class="inline-flex h-8 w-8 items-center justify-center rounded-md bg-white text-gray-600 shadow-sm ring-1 ring-gray-300 transition-all hover:bg-gray-50 hover:text-gray-900 dark:bg-gray-700 dark:text-gray-300 dark:ring-gray-600 dark:hover:bg-gray-600 dark:hover:text-gray-100" |
| 65 | + title="Zoom In" |
| 66 | + > |
| 67 | + <ZoomIn size={16} /> |
| 68 | + </button> |
| 69 | + </div> |
| 70 | + |
| 71 | + <button |
| 72 | + type="button" |
| 73 | + onclick={() => exportApi.provides?.download()} |
| 74 | + disabled={!exportApi.provides} |
| 75 | + class="inline-flex items-center justify-center gap-1.5 rounded-md bg-blue-600 px-3 py-1.5 text-sm font-medium text-white transition-colors hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50" |
| 76 | + > |
| 77 | + <Download size={16} /> |
| 78 | + Download PDF |
| 79 | + </button> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div class="flex flex-col lg:h-[550px] lg:flex-row"> |
| 83 | + <div class="relative h-[420px] sm:h-[550px] lg:h-auto lg:flex-1"> |
| 84 | + {#snippet renderPage(page: RenderPageProps)} |
| 85 | + <PagePointerProvider {documentId} pageIndex={page.pageIndex}> |
| 86 | + <RenderLayer {documentId} pageIndex={page.pageIndex} style="pointer-events: none;" /> |
| 87 | + <SelectionLayer {documentId} pageIndex={page.pageIndex} /> |
| 88 | + <AnnotationLayer {documentId} pageIndex={page.pageIndex} /> |
| 89 | + </PagePointerProvider> |
| 90 | + {/snippet} |
| 91 | + |
| 92 | + <Viewport {documentId} class="absolute inset-0 bg-gray-200 dark:bg-gray-800"> |
| 93 | + <Scroller {documentId} {renderPage} /> |
| 94 | + </Viewport> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div |
| 98 | + class="flex h-full min-h-[240px] flex-col overflow-hidden border-t border-gray-200 bg-white dark:border-gray-800 dark:bg-gray-900 lg:w-1/3 lg:min-w-[250px] lg:max-w-[400px] lg:border-l lg:border-t-0" |
| 99 | + > |
| 100 | + <div |
| 101 | + class="border-b border-gray-200 bg-gray-50 px-4 py-3 dark:border-gray-800 dark:bg-gray-800" |
| 102 | + > |
| 103 | + <h3 class="text-sm font-semibold text-gray-700 dark:text-gray-200">Form State (JSON)</h3> |
| 104 | + <p class="mt-1 text-xs text-gray-500 dark:text-gray-400"> |
| 105 | + Fill the form on the left to see the state update here. |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div class="flex-1 overflow-auto p-4"> |
| 110 | + {#if Object.keys(formValues).length > 0} |
| 111 | + <pre class="text-xs text-gray-800 dark:text-gray-300">{JSON.stringify( |
| 112 | + formValues, |
| 113 | + null, |
| 114 | + 2, |
| 115 | + )}</pre> |
| 116 | + {:else} |
| 117 | + <div class="flex h-full items-center justify-center text-sm text-gray-400"> |
| 118 | + Loading form data... |
| 119 | + </div> |
| 120 | + {/if} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | +</div> |
0 commit comments