hazo_pdf 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -410,6 +410,14 @@ See `hazo_pdf_config.ini` in the project root for all available configuration op
410
410
  | `append_timestamp_to_text_edits` | `boolean` | `false` | If `true`, automatically appends a timestamp to all FreeText annotations when created or edited. Format: `[YYYY-MM-DD h:mmam/pm]`. Overrides config file value. |
411
411
  | `annotation_text_suffix_fixed_text` | `string` | `""` | Fixed text string to append before the timestamp (if timestamps are enabled). This text will be enclosed in brackets. Overrides config file value. |
412
412
 
413
+ ##### Sidepanel Metadata
414
+
415
+ | Prop | Type | Default | Description |
416
+ |------|------|---------|-------------|
417
+ | `sidepanel_metadata_enabled` | `boolean` | `false` | If `true`, enables the metadata sidepanel on the right side of the viewer. The panel can be toggled from the toolbar or right edge button. |
418
+ | `metadata_input` | `MetadataInput` | `undefined` | Metadata structure with header, data (accordions), and footer sections. Each section supports different format types (h1-h5, body) and editable fields. See [Sidepanel Metadata](#sidepanel-metadata) section for details. |
419
+ | `on_metadata_change` | `(updatedRow: MetadataDataItem, allData: MetadataInput) => { updatedRow: MetadataDataItem; allData: MetadataInput }` | `undefined` | Callback when a metadata field is edited. Receives the updated row and complete metadata structure. Must return both parameters. Use this to persist metadata changes to your backend. |
420
+
413
421
  ##### Custom Stamps
414
422
 
415
423
  | Prop | Type | Default | Description |
@@ -489,6 +497,278 @@ Type from `pdfjs-dist`. Contains PDF metadata and page proxies.
489
497
 
490
498
  ---
491
499
 
500
+ ## Toolbar Controls
501
+
502
+ The PDF viewer includes a toolbar at the top with the following controls:
503
+
504
+ ### Zoom Controls
505
+
506
+ - **Zoom In (+ button)**: Increases zoom level by 0.25x increments (max 3.0x)
507
+ - **Zoom Out (- button)**: Decreases zoom level by 0.25x increments (min 0.5x)
508
+ - **Reset Zoom button**: Resets zoom to 1.0x (100%)
509
+ - **Zoom level display**: Shows current zoom percentage (e.g., "125%")
510
+
511
+ **Usage:**
512
+ - Click the `+` or `-` buttons to adjust zoom
513
+ - Click "Reset" to return to default zoom
514
+ - Zoom affects the PDF page size but maintains aspect ratio
515
+
516
+ ### Annotation Tools
517
+
518
+ - **Square button**: Activates square/rectangle annotation tool
519
+ - Click and drag on the PDF to create a rectangle
520
+ - Right-click the rectangle to add a comment
521
+ - Button highlights when active
522
+
523
+ ### History Controls
524
+
525
+ - **Undo button**: Reverses the last annotation action
526
+ - Keyboard shortcut: `Ctrl+Z` (Windows/Linux) or `Cmd+Z` (Mac)
527
+ - Disabled when there are no actions to undo
528
+ - Shows history icon
529
+
530
+ - **Redo button**: Reapplies the last undone action
531
+ - Keyboard shortcut: `Ctrl+Y` (Windows/Linux) or `Cmd+Y` (Mac)
532
+ - Disabled when there are no actions to redo
533
+ - Shows redo icon
534
+
535
+ ### Save Control
536
+
537
+ - **Save button**: Saves all annotations directly into the PDF file
538
+ - Disabled when there are no annotations
539
+ - Shows save icon and "Saving..." text during save operation
540
+ - Triggers `on_save` callback with PDF bytes and filename
541
+
542
+ ### Metadata Panel Toggle
543
+
544
+ - **Metadata button**: Toggles the metadata sidepanel (only visible when `sidepanel_metadata_enabled={true}`)
545
+ - Shows panel icon
546
+ - Button highlights when panel is open
547
+ - Opens/closes the right-side metadata panel
548
+
549
+ ---
550
+
551
+ ## Sidepanel Metadata
552
+
553
+ The PDF viewer can display a retractable sidepanel on the right side showing JSON metadata with header, data (accordions), and footer sections.
554
+
555
+ ### Enabling the Sidepanel
556
+
557
+ To enable the metadata sidepanel, set `sidepanel_metadata_enabled={true}` and provide `metadata_input`:
558
+
559
+ ```tsx
560
+ import { PdfViewer } from 'hazo_pdf';
561
+ import type { MetadataInput } from 'hazo_pdf';
562
+
563
+ const metadata: MetadataInput = {
564
+ header: [
565
+ { style: 'h1', label: 'Document Information' },
566
+ { style: 'body', label: 'Last updated: 2025-01-15' }
567
+ ],
568
+ data: [
569
+ {
570
+ label: 'Document Title',
571
+ style: 'h3',
572
+ value: 'Annual Report 2024',
573
+ editable: true
574
+ },
575
+ {
576
+ label: 'Author',
577
+ style: 'h4',
578
+ value: 'John Doe',
579
+ editable: true
580
+ },
581
+ {
582
+ label: 'Status',
583
+ style: 'body',
584
+ value: 'Draft',
585
+ editable: false
586
+ }
587
+ ],
588
+ footer: [
589
+ { style: 'body', label: 'Version 1.0' }
590
+ ]
591
+ };
592
+
593
+ <PdfViewer
594
+ url="/document.pdf"
595
+ sidepanel_metadata_enabled={true}
596
+ metadata_input={metadata}
597
+ on_metadata_change={(updatedRow, allData) => {
598
+ console.log('Updated row:', updatedRow);
599
+ console.log('All data:', allData);
600
+ // Save to server, update state, etc.
601
+ return { updatedRow, allData };
602
+ }}
603
+ />
604
+ ```
605
+
606
+ ### Metadata Structure
607
+
608
+ The `MetadataInput` interface has three sections:
609
+
610
+ #### Header Section
611
+ - Array of `MetadataHeaderItem` objects
612
+ - Each item has `style` (format type) and `label` (text)
613
+ - Rendered at the top of the panel
614
+
615
+ #### Data Section
616
+ - Array of `MetadataDataItem` objects
617
+ - Each item is rendered as a collapsible accordion (starts collapsed)
618
+ - Properties:
619
+ - `label`: Title shown as accordion header (required)
620
+ - `style`: Format type for the label (h1-h5, body) (required)
621
+ - `value`: Value to display (required)
622
+ - `editable`: Whether field can be edited (boolean, required)
623
+
624
+ #### Footer Section
625
+ - Array of `MetadataFooterItem` objects (same structure as header)
626
+ - Rendered at the bottom of the panel
627
+
628
+ ### Format Types
629
+
630
+ The `style` property accepts the following format types:
631
+ - `h1`: Large heading (3xl, bold)
632
+ - `h2`: Heading (2xl, bold)
633
+ - `h3`: Subheading (xl, semibold)
634
+ - `h4`: Smaller subheading (lg, semibold)
635
+ - `h5`: Small heading (base, semibold)
636
+ - `body`: Regular text (base)
637
+
638
+ ### Editable Fields
639
+
640
+ When `editable: true`:
641
+ - A pencil icon appears next to the value
642
+ - Clicking the pencil enters edit mode
643
+ - Edit mode shows:
644
+ - Text input field (single-line)
645
+ - Green circle-check button (save)
646
+ - Red circle-x button (cancel)
647
+ - Pressing `Enter` saves, `Escape` cancels
648
+ - On save, `on_metadata_change` callback is called with:
649
+ - `updatedRow`: The updated `MetadataDataItem`
650
+ - `allData`: The complete `MetadataInput` with all updates
651
+ - Callback must return `{ updatedRow, allData }`
652
+
653
+ ### Panel Controls
654
+
655
+ - **Toggle from toolbar**: Click the "Metadata" button in the toolbar
656
+ - **Toggle from right edge**: Click the chevron button on the right edge (when closed)
657
+ - **Resize**: Drag the left edge of the panel to resize (200px - 800px on desktop)
658
+ - **Close**: Click the chevron button in the panel header or toggle button in toolbar
659
+
660
+ ### Responsive Behavior
661
+
662
+ - **Desktop (> 1024px)**: Panel appears side-by-side with PDF viewer, max width 800px
663
+ - **Tablet (768px - 1024px)**: Panel max width is 50% of screen, can overlay
664
+ - **Mobile (< 768px)**: Panel uses overlay mode, max width 90vw, full height
665
+
666
+ ### Example: Complex Metadata (Test App Example)
667
+
668
+ The test app includes comprehensive test data demonstrating all metadata variations. This example shows all format types (h1-h5, body), editable and non-editable fields:
669
+
670
+ ```tsx
671
+ import type { MetadataInput, MetadataDataItem } from 'hazo_pdf';
672
+
673
+ const test_metadata: MetadataInput = {
674
+ header: [
675
+ { style: 'h1', label: 'Document Information' },
676
+ { style: 'h3', label: 'Test Document Metadata' },
677
+ { style: 'body', label: 'Last updated: 2025-01-15' }
678
+ ],
679
+ data: [
680
+ {
681
+ label: 'Document Title',
682
+ style: 'h2',
683
+ value: 'Annual Report 2024',
684
+ editable: true
685
+ },
686
+ {
687
+ label: 'Author Information',
688
+ style: 'h3',
689
+ value: 'John Doe\nSenior Analyst\nDepartment of Finance',
690
+ editable: true
691
+ },
692
+ {
693
+ label: 'Document Status',
694
+ style: 'h4',
695
+ value: 'Approved',
696
+ editable: false
697
+ },
698
+ {
699
+ label: 'Version',
700
+ style: 'h5',
701
+ value: '1.0.0',
702
+ editable: true
703
+ },
704
+ {
705
+ label: 'Category',
706
+ style: 'body',
707
+ value: 'Financial Report',
708
+ editable: false
709
+ },
710
+ {
711
+ label: 'Keywords',
712
+ style: 'body',
713
+ value: 'financial, annual, report, 2024',
714
+ editable: true
715
+ },
716
+ {
717
+ label: 'Document ID',
718
+ style: 'h5',
719
+ value: 'DOC-2024-001',
720
+ editable: false
721
+ },
722
+ {
723
+ label: 'Notes',
724
+ style: 'body',
725
+ value: 'This document contains comprehensive financial data for the fiscal year 2024. Please review all sections carefully before finalizing.',
726
+ editable: true
727
+ },
728
+ {
729
+ label: 'Confidentiality Level',
730
+ style: 'h4',
731
+ value: 'Internal Use Only',
732
+ editable: false
733
+ },
734
+ {
735
+ label: 'Approval Date',
736
+ style: 'body',
737
+ value: '2025-01-10',
738
+ editable: false
739
+ }
740
+ ],
741
+ footer: [
742
+ { style: 'body', label: 'This is a test metadata example' },
743
+ { style: 'h5', label: 'Version 1.0 - Test App' }
744
+ ]
745
+ };
746
+
747
+ <PdfViewer
748
+ url="/document.pdf"
749
+ sidepanel_metadata_enabled={true}
750
+ metadata_input={test_metadata}
751
+ on_metadata_change={(updatedRow, allData) => {
752
+ console.log('Updated:', updatedRow);
753
+ console.log('All data:', allData);
754
+ // Update state, save to backend, etc.
755
+ return { updatedRow, allData };
756
+ }}
757
+ />
758
+ ```
759
+
760
+ **This example demonstrates:**
761
+ - **Header section**: Multiple format types (h1, h3, body)
762
+ - **Data section**: All format types (h2, h3, h4, h5, body) with both editable and non-editable fields
763
+ - **Footer section**: Multiple format types (body, h5)
764
+ - **Multi-line values**: Author Information includes newlines
765
+ - **Long text values**: Notes field contains a longer description
766
+ - **Mixed editability**: Some fields editable, others read-only
767
+
768
+ **Note:** This test data is used in the test app (`app/viewer/[filename]/page.tsx`) to demonstrate all sidepanel metadata features.
769
+
770
+ ---
771
+
492
772
  ## Usage Tips
493
773
 
494
774
  ### Styling
@@ -561,6 +841,102 @@ The position and bracket style can be configured via the config file:
561
841
  - `suffix_enclosing_brackets`: Two-character string like `"[]"`, `"()"`, `"{}"` (default: `"[]"`)
562
842
  - `add_enclosing_brackets_to_suffixes`: `true` or `false` (default: `true`)
563
843
 
844
+ ### Responsive Design
845
+
846
+ The PDF viewer is fully responsive and adapts to different screen sizes:
847
+
848
+ **Desktop (> 1024px):**
849
+ - Full feature set available
850
+ - Sidepanel appears side-by-side with PDF (when enabled)
851
+ - All toolbar buttons visible
852
+ - Optimal viewing experience
853
+
854
+ **Tablet (768px - 1024px):**
855
+ - Sidepanel max width is 50% of screen (when enabled)
856
+ - Toolbar buttons may wrap to multiple rows
857
+ - Touch-friendly button sizes
858
+ - Horizontal scrolling available for zoomed PDFs
859
+
860
+ **Mobile (< 768px):**
861
+ - Sidepanel uses overlay mode (when enabled), max 90vw width
862
+ - Toolbar buttons wrap and use touch-friendly sizes (min 44px height)
863
+ - PDF viewer maintains full functionality
864
+ - Pan tool works with touch gestures
865
+ - All annotation tools remain accessible
866
+
867
+ **Breakpoints:**
868
+ - Mobile: `< 768px`
869
+ - Tablet: `768px - 1024px`
870
+ - Desktop: `> 1024px`
871
+
872
+ The viewer uses CSS media queries to adjust layout and component sizes automatically. All interactive elements are touch-friendly on mobile devices.
873
+
874
+ ---
875
+
876
+ ### Annotation Coordinate System
877
+
878
+ - Annotations use PDF coordinate space (points), not screen pixels.
879
+ - PDF coordinates start at the bottom-left (Y increases upward).
880
+ - The component handles coordinate conversion automatically.
881
+
882
+ ### Pan Tool
883
+
884
+ - Pan is the default tool (`current_tool = null`).
885
+ - Users can drag to scroll/pan the document.
886
+ - The cursor changes to a hand icon when panning.
887
+
888
+ ### Square Annotations
889
+
890
+ 1. Click the "Square" button in the toolbar.
891
+ 2. Click and drag on the PDF to create a rectangle.
892
+ 3. Right-click the rectangle to add a comment.
893
+
894
+ ### FreeText Annotations
895
+
896
+ 1. Right-click anywhere on the PDF.
897
+ 2. Select "Annotate" from the context menu.
898
+ 3. Enter text in the dialog.
899
+ 4. Click the checkmark to save.
900
+ 5. Left-click an existing FreeText annotation to edit or delete it.
901
+
902
+ ### Custom Stamps
903
+
904
+ 1. Configure stamps via `right_click_custom_stamps` prop or config file.
905
+ 2. Right-click on the PDF.
906
+ 3. Select a stamp from the bottom of the menu.
907
+ 4. The stamp text is added at the click position with optional timestamp/fixed text.
908
+
909
+ ### Saving Annotations
910
+
911
+ - Click the "Save" button in the toolbar.
912
+ - The `on_save` callback receives PDF bytes with annotations embedded using `pdf-lib`.
913
+ - You can download directly or upload to a server.
914
+
915
+ **Example download:**
916
+
917
+ ```tsx
918
+ const handleSave = (pdfBytes: Uint8Array, filename: string) => {
919
+ const blob = new Blob([pdfBytes], { type: 'application/pdf' });
920
+ const url = URL.createObjectURL(blob);
921
+ const a = document.createElement('a');
922
+ a.href = url;
923
+ a.download = filename || 'document.pdf';
924
+ a.click();
925
+ URL.revokeObjectURL(url);
926
+ };
927
+ ```
928
+
929
+ ### Timestamp Formatting
930
+
931
+ When `append_timestamp_to_text_edits` is enabled, timestamps are formatted as:
932
+ - Format: `YYYY-MM-DD h:mmam/pm`
933
+ - Example: `2025-11-17 2:24pm`
934
+
935
+ The position and bracket style can be configured via the config file:
936
+ - `suffix_text_position`: `"adjacent"`, `"below_single_line"`, or `"below_multi_line"` (default)
937
+ - `suffix_enclosing_brackets`: Two-character string like `"[]"`, `"()"`, `"{}"` (default: `"[]"`)
938
+ - `add_enclosing_brackets_to_suffixes`: `true` or `false` (default: `true`)
939
+
564
940
  ---
565
941
 
566
942
  ## Development
package/dist/index.d.ts CHANGED
@@ -171,6 +171,16 @@ interface PdfViewerProps {
171
171
  /** Each stamp has: name, text, order, time_stamp_suffix_enabled (default false), fixed_text_suffix_enabled (default false) */
172
172
  /** Example: '[{"name":"Verified","text":"XXX","order":1,"time_stamp_suffix_enabled":true,"fixed_text_suffix_enabled":true}]' */
173
173
  right_click_custom_stamps?: string;
174
+ /** Whether to enable the sidepanel metadata display (default: false) */
175
+ sidepanel_metadata_enabled?: boolean;
176
+ /** Metadata input structure for sidepanel display */
177
+ metadata_input?: MetadataInput;
178
+ /** Callback when metadata is changed via editing */
179
+ /** Returns both the updated row and the complete metadata structure */
180
+ on_metadata_change?: (updatedRow: MetadataDataItem, allData: MetadataInput) => {
181
+ updatedRow: MetadataDataItem;
182
+ allData: MetadataInput;
183
+ };
174
184
  }
175
185
  /**
176
186
  * PDF Annotation interface matching PDF standard
@@ -267,6 +277,52 @@ type PdfDocument = PDFDocumentProxy;
267
277
  * PDF Page Proxy type
268
278
  */
269
279
  type PdfPage = PDFPageProxy;
280
+ /**
281
+ * Metadata format types for sidepanel display
282
+ */
283
+ type MetadataFormatType = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'body';
284
+ /**
285
+ * Metadata header item
286
+ */
287
+ interface MetadataHeaderItem {
288
+ /** Format style for the header text */
289
+ style: MetadataFormatType;
290
+ /** Label text to display */
291
+ label: string;
292
+ }
293
+ /**
294
+ * Metadata data item
295
+ */
296
+ interface MetadataDataItem {
297
+ /** Label/title for the data item (used as accordion title) */
298
+ label: string;
299
+ /** Format style for the label text */
300
+ style: MetadataFormatType;
301
+ /** Value to display */
302
+ value: string;
303
+ /** Whether this field is editable */
304
+ editable: boolean;
305
+ }
306
+ /**
307
+ * Metadata footer item
308
+ */
309
+ interface MetadataFooterItem {
310
+ /** Format style for the footer text */
311
+ style: MetadataFormatType;
312
+ /** Label text to display */
313
+ label: string;
314
+ }
315
+ /**
316
+ * Metadata input structure for sidepanel
317
+ */
318
+ interface MetadataInput {
319
+ /** Array of header items */
320
+ header: MetadataHeaderItem[];
321
+ /** Array of data items (rendered as accordions) */
322
+ data: MetadataDataItem[];
323
+ /** Array of footer items */
324
+ footer: MetadataFooterItem[];
325
+ }
270
326
 
271
327
  /**
272
328
  * PDF Viewer Component
@@ -590,4 +646,4 @@ declare function load_pdf_config(config_file?: string): PdfViewerConfig;
590
646
  */
591
647
  declare const default_config: PdfViewerConfig;
592
648
 
593
- export { AnnotationOverlay, type CoordinateMapper, type CustomStamp, type PageDimensions, type PdfAnnotation, type PdfBookmark, type PdfDocument, type PdfPage, PdfPageRenderer, PdfViewer, type PdfViewerConfig, PdfViewerLayout, type PdfViewerProps, build_config_from_ini, calculate_rectangle_coords, create_coordinate_mapper, default_config, download_pdf, download_xfdf, export_annotations_to_xfdf, generate_xfdf, get_viewport_dimensions, is_rectangle_too_small, load_pdf_config, load_pdf_config_async, load_pdf_document, parse_color, parse_number, parse_opacity, parse_string, pdf_rect_to_rectangle, rectangle_to_pdf_rect, save_and_download_pdf, save_annotations_to_pdf };
649
+ export { AnnotationOverlay, type CoordinateMapper, type CustomStamp, type MetadataDataItem, type MetadataFooterItem, type MetadataFormatType, type MetadataHeaderItem, type MetadataInput, type PageDimensions, type PdfAnnotation, type PdfBookmark, type PdfDocument, type PdfPage, PdfPageRenderer, PdfViewer, type PdfViewerConfig, PdfViewerLayout, type PdfViewerProps, build_config_from_ini, calculate_rectangle_coords, create_coordinate_mapper, default_config, download_pdf, download_xfdf, export_annotations_to_xfdf, generate_xfdf, get_viewport_dimensions, is_rectangle_too_small, load_pdf_config, load_pdf_config_async, load_pdf_document, parse_color, parse_number, parse_opacity, parse_string, pdf_rect_to_rectangle, rectangle_to_pdf_rect, save_and_download_pdf, save_annotations_to_pdf };