@tobyt/expo-pdf-markup 0.5.0 → 0.6.1

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +7 -0
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +83 -3
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
  8. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  9. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +36 -8
  10. package/build/ExpoPdfMarkup.types.d.ts +25 -3
  11. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  12. package/build/ExpoPdfMarkup.types.js.map +1 -1
  13. package/build/ExpoPdfMarkupView.d.ts.map +1 -1
  14. package/build/ExpoPdfMarkupView.js +2 -2
  15. package/build/ExpoPdfMarkupView.js.map +1 -1
  16. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  17. package/build/ExpoPdfMarkupView.web.js +127 -43
  18. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  19. package/build/web/annotationUtils.d.ts +9 -1
  20. package/build/web/annotationUtils.d.ts.map +1 -1
  21. package/build/web/annotationUtils.js +88 -5
  22. package/build/web/annotationUtils.js.map +1 -1
  23. package/ios/AnnotationModels.swift +185 -0
  24. package/ios/AnnotationSerializer.swift +63 -33
  25. package/ios/ExpoPdfMarkupModule.swift +4 -0
  26. package/ios/ExpoPdfMarkupView.swift +279 -86
  27. package/ios/Tests/AnnotationSerializerTests.swift +18 -0
  28. package/package.json +1 -1
  29. package/src/ExpoPdfMarkup.types.ts +33 -3
  30. package/src/ExpoPdfMarkupView.tsx +12 -9
  31. package/src/ExpoPdfMarkupView.web.tsx +165 -41
  32. package/src/web/annotationUtils.ts +121 -4
@@ -19,19 +19,28 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
19
19
  }
20
20
 
21
21
  var annotationLineWidth: CGFloat = 2.0
22
+ var annotationFontFamily: String?
22
23
  var useJsTextDialog: Bool = false
23
24
 
24
25
  private var inkPanGesture: UIPanGestureRecognizer?
25
26
  private var inkShapeLayer: CAShapeLayer?
26
27
  private var inkPoints: [CGPoint] = []
27
28
  private var eraserTapGesture: UITapGestureRecognizer?
28
- private var textTapGesture: UITapGestureRecognizer?
29
+ private var movePanGesture: UIPanGestureRecognizer?
30
+ private var editingOverlayLayer: CAShapeLayer?
31
+ var textTapGesture: UITapGestureRecognizer?
29
32
  private var selectionObserver: NSObjectProtocol?
30
33
  private var selectionDebounceTimer: Timer?
31
34
  private var currentMarkupType: String?
35
+ private var movingPage: PDFPage?
36
+ private var movingOriginalModel: AnnotationModel?
37
+ private var movingPreviewModel: AnnotationModel?
38
+ private var movingPreviewAnnotation: PDFAnnotation?
39
+ private var moveStartPoint: CGPoint?
32
40
 
33
- private var pendingTextPage: PDFPage?
34
- private var pendingTextPoint: CGPoint?
41
+ var pendingTextPage: PDFPage?
42
+ var pendingTextPoint: CGPoint?
43
+ var pendingTextAnnotation: PDFAnnotation?
35
44
 
36
45
  required init(appContext: AppContext? = nil) {
37
46
  super.init(appContext: appContext)
@@ -68,6 +77,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
68
77
  super.layoutSubviews()
69
78
  pdfView.frame = bounds
70
79
  inkShapeLayer?.frame = bounds
80
+ editingOverlayLayer?.frame = pdfView.documentView?.bounds ?? .zero
81
+ updateEditingOverlay()
71
82
  }
72
83
 
73
84
  // MARK: - Props
@@ -94,6 +105,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
94
105
  if let json = currentAnnotationsJSON {
95
106
  currentAnnotationsJSON = nil
96
107
  loadAnnotations(from: json)
108
+ } else {
109
+ updateEditingOverlay()
97
110
  }
98
111
  }
99
112
 
@@ -115,6 +128,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
115
128
  guard let json, !json.isEmpty else {
116
129
  clearModuleManagedAnnotations()
117
130
  currentAnnotationsJSON = nil
131
+ updateEditingOverlay()
118
132
  return
119
133
  }
120
134
 
@@ -136,6 +150,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
136
150
  }
137
151
  page.addAnnotation(pdfAnnotation)
138
152
  }
153
+
154
+ updateEditingOverlay()
139
155
  }
140
156
 
141
157
  func serializeCurrentAnnotations() -> String {
@@ -161,6 +177,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
161
177
  let json = serializeCurrentAnnotations()
162
178
  currentAnnotationsJSON = json
163
179
  onAnnotationsChanged(["annotations": json])
180
+ updateEditingOverlay()
164
181
  }
165
182
 
166
183
  private func clearModuleManagedAnnotations() {
@@ -172,6 +189,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
172
189
  page.removeAnnotation(annotation)
173
190
  }
174
191
  }
192
+ updateEditingOverlay()
175
193
  }
176
194
 
177
195
  // MARK: - Annotation mode
@@ -184,6 +202,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
184
202
  if previousMode != mode {
185
203
  removeInkMode()
186
204
  removeEraserGesture()
205
+ removeMoveMode()
187
206
  removeTextGesture()
188
207
  removeSelectionObserver()
189
208
  }
@@ -193,6 +212,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
193
212
  setupInkMode()
194
213
  case "eraser":
195
214
  setupEraserGesture()
215
+ case "move":
216
+ setupMoveMode()
196
217
  case "highlight", "underline":
197
218
  setupSelectionObserver(markupType: mode)
198
219
  case "text":
@@ -200,6 +221,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
200
221
  default:
201
222
  break
202
223
  }
224
+
225
+ updateEditingOverlay()
203
226
  }
204
227
 
205
228
  // MARK: - Ink drawing
@@ -450,123 +473,293 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
450
473
  }
451
474
  }
452
475
 
453
- // MARK: - Text / Sticky notes
476
+ // MARK: - UIGestureRecognizerDelegate
454
477
 
455
- private func setupTextGesture() {
456
- guard textTapGesture == nil else { return }
457
- let tap = UITapGestureRecognizer(target: self, action: #selector(handleTextTap(_:)))
458
- tap.delegate = self
459
- pdfView.addGestureRecognizer(tap)
460
- textTapGesture = tap
478
+ func gestureRecognizer(
479
+ _: UIGestureRecognizer,
480
+ shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
481
+ ) -> Bool {
482
+ true
483
+ }
484
+
485
+ // MARK: - Page change notification
486
+
487
+ @objc private func handlePageChanged() {
488
+ guard let document = pdfView.document,
489
+ let currentPage = pdfView.currentPage else { return }
490
+
491
+ let pageIndex = document.index(for: currentPage)
492
+ let pageCount = document.pageCount
493
+ let bounds = currentPage.bounds(for: pdfView.displayBox)
494
+ onPageChanged([
495
+ "page": pageIndex,
496
+ "pageCount": pageCount,
497
+ "pageWidth": bounds.width,
498
+ "pageHeight": bounds.height,
499
+ ])
500
+ updateEditingOverlay()
501
+ }
502
+ }
503
+
504
+ extension ExpoPdfMarkupView {
505
+ // MARK: - Move
506
+
507
+ func setupMoveMode() {
508
+ guard movePanGesture == nil else { return }
509
+ pdfScrollView?.isScrollEnabled = false
510
+ let pan = UIPanGestureRecognizer(target: self, action: #selector(handleMovePan(_:)))
511
+ pan.maximumNumberOfTouches = 1
512
+ pan.delegate = self
513
+ pdfView.addGestureRecognizer(pan)
514
+ movePanGesture = pan
461
515
  }
462
516
 
463
- private func removeTextGesture() {
464
- if let gesture = textTapGesture {
517
+ func removeMoveMode() {
518
+ if let gesture = movePanGesture {
465
519
  pdfView.removeGestureRecognizer(gesture)
466
- textTapGesture = nil
520
+ movePanGesture = nil
467
521
  }
522
+ finishMove(commit: false)
523
+ pdfScrollView?.isScrollEnabled = true
468
524
  }
469
525
 
470
- @objc private func handleTextTap(_ gesture: UITapGestureRecognizer) {
471
- let location = gesture.location(in: pdfView)
472
- guard let page = pdfView.page(for: location, nearest: true) else { return }
526
+ @objc func handleMovePan(_ gesture: UIPanGestureRecognizer) {
527
+ switch gesture.state {
528
+ case .began:
529
+ beginMove(at: gesture.location(in: pdfView))
530
+ case .changed:
531
+ updateMove(at: gesture.location(in: pdfView))
532
+ case .ended:
533
+ finishMove(commit: true)
534
+ case .cancelled, .failed:
535
+ finishMove(commit: false)
536
+ default:
537
+ break
538
+ }
539
+ }
540
+
541
+ private func beginMove(at location: CGPoint) {
542
+ guard let document = pdfView.document,
543
+ let page = pdfView.page(for: location, nearest: true) else { return }
544
+
545
+ let pdfPoint = pdfView.convert(location, to: page)
546
+ guard let annotation = hitTestAnnotation(at: pdfPoint, on: page) else { return }
547
+
548
+ let pageIndex = document.index(for: page)
549
+ guard let model = AnnotationSerializer.toModel(from: annotation, page: pageIndex),
550
+ let preview = annotationForModel(model, on: page) else { return }
551
+
552
+ movingPage = page
553
+ movingOriginalModel = model
554
+ movingPreviewModel = model
555
+ moveStartPoint = pdfPoint
556
+
557
+ page.removeAnnotation(annotation)
558
+ page.addAnnotation(preview)
559
+ movingPreviewAnnotation = preview
560
+ updateEditingOverlay()
561
+ }
562
+
563
+ private func updateMove(at location: CGPoint) {
564
+ guard let page = movingPage,
565
+ let originalModel = movingOriginalModel,
566
+ let startPoint = moveStartPoint else { return }
473
567
 
474
568
  let pdfPoint = pdfView.convert(location, to: page)
569
+ let rawDeltaX = pdfPoint.x - startPoint.x
570
+ let rawDeltaY = pdfPoint.y - startPoint.y
571
+ let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
572
+ let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
573
+ guard translatedModel != movingPreviewModel,
574
+ let replacement = annotationForModel(translatedModel, on: page) else { return }
575
+
576
+ if let currentPreview = movingPreviewAnnotation {
577
+ page.removeAnnotation(currentPreview)
578
+ }
579
+ page.addAnnotation(replacement)
580
+ movingPreviewAnnotation = replacement
581
+ movingPreviewModel = translatedModel
582
+ updateEditingOverlay()
583
+ }
475
584
 
476
- if useJsTextDialog {
477
- pendingTextPage = page
478
- pendingTextPoint = pdfPoint
479
- onTextInputRequested([:])
585
+ private func finishMove(commit: Bool) {
586
+ guard let page = movingPage,
587
+ let originalModel = movingOriginalModel else {
588
+ clearMoveState()
589
+ updateEditingOverlay()
480
590
  return
481
591
  }
482
592
 
483
- // Show text input dialog
484
- guard let viewController = findViewController() else { return }
485
- let alert = UIAlertController(title: "Add Text", message: nil, preferredStyle: .alert)
486
- alert.addTextField { textField in
487
- textField.placeholder = "Enter text"
593
+ if let preview = movingPreviewAnnotation {
594
+ page.removeAnnotation(preview)
595
+ }
596
+
597
+ let finalModel = commit ? (movingPreviewModel ?? originalModel) : originalModel
598
+ if let finalAnnotation = annotationForModel(finalModel, on: page) {
599
+ page.addAnnotation(finalAnnotation)
488
600
  }
489
601
 
490
- alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
491
- alert.addAction(UIAlertAction(title: "Add", style: .default) { [weak self] _ in
492
- guard let self, let text = alert.textFields?.first?.text, !text.isEmpty else { return }
493
- addFreeTextAnnotation(text: text, at: pdfPoint, on: page)
494
- })
602
+ let didChange = commit && finalModel != originalModel
603
+ clearMoveState()
604
+ updateEditingOverlay()
495
605
 
496
- viewController.present(alert, animated: true)
606
+ if didChange {
607
+ emitAnnotationsChanged()
608
+ }
497
609
  }
498
610
 
499
- func provideTextInput(text: String?) {
500
- guard let page = pendingTextPage, let point = pendingTextPoint else { return }
501
- pendingTextPage = nil
502
- pendingTextPoint = nil
503
- guard let text, !text.isEmpty else { return }
504
- addFreeTextAnnotation(text: text, at: point, on: page)
611
+ private func clearMoveState() {
612
+ movingPage = nil
613
+ movingOriginalModel = nil
614
+ movingPreviewModel = nil
615
+ movingPreviewAnnotation = nil
616
+ moveStartPoint = nil
505
617
  }
506
618
 
507
- private func addFreeTextAnnotation(text: String, at point: CGPoint, on page: PDFPage) {
508
- let fontSize: CGFloat = 16.0
509
- let font = UIFont.systemFont(ofSize: fontSize)
510
- let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
619
+ // MARK: - Editing overlay
511
620
 
512
- // Measure text size
513
- let attributes: [NSAttributedString.Key: Any] = [.font: font]
514
- let textSize = (text as NSString).size(withAttributes: attributes)
515
- let padding: CGFloat = 4.0
516
- let bounds = CGRect(
517
- x: point.x,
518
- y: point.y - textSize.height - padding,
519
- width: textSize.width + padding * 2,
520
- height: textSize.height + padding * 2
521
- )
621
+ func updateEditingOverlay() {
622
+ guard currentMode == "move" || currentMode == "eraser" else {
623
+ editingOverlayLayer?.removeFromSuperlayer()
624
+ editingOverlayLayer = nil
625
+ return
626
+ }
522
627
 
523
- let annotation = PDFAnnotation(bounds: bounds, forType: .freeText, withProperties: nil)
524
- annotation.font = font
525
- annotation.fontColor = color
526
- annotation.color = .clear
527
- annotation.contents = text
628
+ guard let document = pdfView.document,
629
+ let documentView = pdfView.documentView else { return }
630
+
631
+ let overlay = editingOverlayLayer ?? {
632
+ let shape = CAShapeLayer()
633
+ shape.fillColor = UIColor.clear.cgColor
634
+ shape.strokeColor = UIColor.systemBlue.cgColor
635
+ shape.lineWidth = 1.5
636
+ shape.lineDashPattern = [6, 4]
637
+ documentView.layer.addSublayer(shape)
638
+ editingOverlayLayer = shape
639
+ return shape
640
+ }()
641
+
642
+ if overlay.superlayer !== documentView.layer {
643
+ overlay.removeFromSuperlayer()
644
+ documentView.layer.addSublayer(overlay)
645
+ }
528
646
 
529
- let id = UUID().uuidString
530
- AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
531
- page.addAnnotation(annotation)
647
+ overlay.frame = documentView.bounds
648
+ let path = UIBezierPath()
532
649
 
533
- emitAnnotationsChanged()
650
+ for pageIndex in 0 ..< document.pageCount {
651
+ guard let page = document.page(at: pageIndex) else { continue }
652
+ for annotation in page.annotations where AnnotationSerializer.isModuleManaged(annotation) {
653
+ guard let outlineRect = annotationOutlineRect(for: annotation, on: page, in: documentView) else {
654
+ continue
655
+ }
656
+ path.append(UIBezierPath(rect: outlineRect))
657
+ }
658
+ }
659
+
660
+ overlay.path = path.cgPath
534
661
  }
535
662
 
536
- private func findViewController() -> UIViewController? {
537
- var responder: UIResponder? = self
538
- while let next = responder?.next {
539
- if let vc = next as? UIViewController {
540
- return vc
663
+ private func annotationOutlineRect(
664
+ for annotation: PDFAnnotation,
665
+ on page: PDFPage,
666
+ in documentView: UIView
667
+ ) -> CGRect? {
668
+ let pdfRect: CGRect
669
+ if let paths = annotation.paths, !paths.isEmpty {
670
+ var combinedBounds = CGRect.null
671
+ for path in paths {
672
+ combinedBounds = combinedBounds.union(path.cgPath.boundingBoxOfPath)
541
673
  }
542
- responder = next
674
+ guard !combinedBounds.isNull else { return nil }
675
+ let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
676
+ pdfRect = combinedBounds.insetBy(dx: -padding, dy: -padding)
677
+ } else {
678
+ pdfRect = annotation.bounds
543
679
  }
544
- return nil
680
+
681
+ let pdfViewRect = pdfView.convert(pdfRect, from: page)
682
+ return documentView.convert(pdfViewRect, from: pdfView)
545
683
  }
546
684
 
547
- // MARK: - UIGestureRecognizerDelegate
685
+ private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
686
+ AnnotationSerializer.toPDFAnnotation(model, pageBounds: page.bounds(for: pdfView.displayBox))
687
+ }
548
688
 
549
- func gestureRecognizer(
550
- _: UIGestureRecognizer,
551
- shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
552
- ) -> Bool {
553
- true
689
+ private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
690
+ guard dx != 0 || dy != 0 else { return model }
691
+
692
+ var updated = model
693
+ switch model.type {
694
+ case "ink":
695
+ let translatedPaths = model.decodedPaths.map { stroke in
696
+ stroke.map { point in
697
+ AnnotationPoint(x: point.x + dx, y: point.y + dy)
698
+ }
699
+ }
700
+ updated.paths = updated.encodedPaths(from: translatedPaths)
701
+ case "highlight", "underline", "text", "freeText":
702
+ if let bounds = model.bounds {
703
+ updated.bounds = AnnotationBounds(
704
+ x: bounds.x + dx,
705
+ y: bounds.y + dy,
706
+ width: bounds.width,
707
+ height: bounds.height
708
+ )
709
+ }
710
+ default:
711
+ break
712
+ }
713
+ return updated
554
714
  }
555
715
 
556
- // MARK: - Page change notification
716
+ private func clampedTranslation(
717
+ for model: AnnotationModel,
718
+ dx: CGFloat,
719
+ dy: CGFloat,
720
+ on page: PDFPage
721
+ ) -> CGPoint {
722
+ guard let outlineBounds = annotationOutlineBounds(for: model) else {
723
+ return CGPoint(x: dx, y: dy)
724
+ }
557
725
 
558
- @objc private func handlePageChanged() {
559
- guard let document = pdfView.document,
560
- let currentPage = pdfView.currentPage else { return }
726
+ let pageBounds = page.bounds(for: pdfView.displayBox)
727
+ let minDx = pageBounds.minX - outlineBounds.x
728
+ let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
729
+ let minDy = pageBounds.minY - outlineBounds.y
730
+ let maxDy = pageBounds.maxY - (outlineBounds.y + outlineBounds.height)
561
731
 
562
- let pageIndex = document.index(for: currentPage)
563
- let pageCount = document.pageCount
564
- let bounds = currentPage.bounds(for: pdfView.displayBox)
565
- onPageChanged([
566
- "page": pageIndex,
567
- "pageCount": pageCount,
568
- "pageWidth": bounds.width,
569
- "pageHeight": bounds.height,
570
- ])
732
+ return CGPoint(
733
+ x: min(max(dx, minDx), maxDx),
734
+ y: min(max(dy, minDy), maxDy)
735
+ )
736
+ }
737
+
738
+ private func annotationOutlineBounds(for model: AnnotationModel) -> AnnotationBounds? {
739
+ guard model.type == "ink" else { return model.bounds }
740
+ let paths = model.decodedPaths
741
+ var minX = CGFloat.greatestFiniteMagnitude
742
+ var minY = CGFloat.greatestFiniteMagnitude
743
+ var maxX = -CGFloat.greatestFiniteMagnitude
744
+ var maxY = -CGFloat.greatestFiniteMagnitude
745
+
746
+ for stroke in paths {
747
+ for point in stroke {
748
+ minX = min(minX, point.x)
749
+ minY = min(minY, point.y)
750
+ maxX = max(maxX, point.x)
751
+ maxY = max(maxY, point.y)
752
+ }
753
+ }
754
+
755
+ guard minX.isFinite, minY.isFinite, maxX.isFinite, maxY.isFinite else { return nil }
756
+
757
+ let padding = max(model.lineWidth ?? 2.0, 10.0)
758
+ return AnnotationBounds(
759
+ x: minX - padding,
760
+ y: minY - padding,
761
+ width: maxX - minX + padding * 2,
762
+ height: maxY - minY + padding * 2
763
+ )
571
764
  }
572
765
  }
@@ -73,6 +73,24 @@ final class AnnotationSerializerTests: XCTestCase {
73
73
  XCTAssertEqual(annotation?.color, .clear)
74
74
  }
75
75
 
76
+ func testCreateFreeTextAnnotationExpandsNarrowBoundsToFitText() {
77
+ let originalBounds = AnnotationBounds(x: 100, y: 200, width: 40, height: 24)
78
+ let model = AnnotationModel(
79
+ id: "txt-narrow-1",
80
+ type: "freeText",
81
+ page: 0,
82
+ color: "#00FF00",
83
+ bounds: originalBounds,
84
+ contents: "Hello from Android",
85
+ fontSize: 16.0
86
+ )
87
+
88
+ let annotation = AnnotationSerializer.toPDFAnnotation(model)
89
+ XCTAssertNotNil(annotation)
90
+ XCTAssertGreaterThan(annotation?.bounds.width ?? 0, originalBounds.width)
91
+ XCTAssertEqual(annotation?.bounds.maxY, originalBounds.cgRect.maxY, accuracy: 0.01)
92
+ }
93
+
76
94
  func testUnknownTypeReturnsNil() {
77
95
  let model = AnnotationModel(
78
96
  id: "bad-1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tobyt/expo-pdf-markup",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "An Expo module for displaying and annotating PDFs",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -41,6 +41,13 @@ export type TextAnnotation = {
41
41
  bounds: AnnotationBounds;
42
42
  contents?: string;
43
43
  fontSize?: number;
44
+ /**
45
+ * Font family for the text annotation. Platform-specific:
46
+ * - iOS: PostScript name (`"Georgia"`, `"Courier"`) or `undefined` for system font (San Francisco)
47
+ * - Android: family name (`"serif"`, `"monospace"`) or `undefined` for default sans-serif
48
+ * - Web: CSS font family (`"Georgia, serif"`) or `undefined` for `sans-serif`
49
+ */
50
+ fontFamily?: string;
44
51
  createdAt?: number;
45
52
  };
46
53
 
@@ -51,7 +58,21 @@ export type AnnotationsData = {
51
58
  annotations: Annotation[];
52
59
  };
53
60
 
54
- export type AnnotationMode = 'none' | 'ink' | 'highlight' | 'underline' | 'text' | 'eraser';
61
+ export type AnnotationMode =
62
+ | 'none'
63
+ | 'ink'
64
+ | 'highlight'
65
+ | 'underline'
66
+ | 'text'
67
+ | 'eraser'
68
+ | 'move';
69
+
70
+ export type TextInputRequest = {
71
+ mode: 'create' | 'edit';
72
+ page: number;
73
+ point: AnnotationPoint;
74
+ currentText?: string;
75
+ };
55
76
 
56
77
  export type ExpoPdfMarkupViewProps = {
57
78
  /** Absolute local file path of the PDF to display. */
@@ -68,6 +89,13 @@ export type ExpoPdfMarkupViewProps = {
68
89
  annotationColor?: string;
69
90
  /** Stroke width in points applied to new ink annotations. */
70
91
  annotationLineWidth?: number;
92
+ /**
93
+ * Default font family for new text annotations. Platform-specific:
94
+ * - iOS: PostScript name (`"Georgia"`, `"Courier"`) or `undefined` for system font (San Francisco)
95
+ * - Android: React Native / Expo font family name (`"Montserrat-Regular"`, `"serif"`) or `undefined` for default sans-serif
96
+ * - Web: CSS font family (`"Georgia, serif"`) or `undefined` for `sans-serif`
97
+ */
98
+ annotationFontFamily?: string;
71
99
  /** Fired when the visible page changes. */
72
100
  onPageChanged?: (event: {
73
101
  nativeEvent: { page: number; pageCount: number; pageWidth: number; pageHeight: number };
@@ -81,8 +109,10 @@ export type ExpoPdfMarkupViewProps = {
81
109
  /**
82
110
  * Optional async callback for the text annotation tool. When provided, this is called instead of
83
111
  * the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text
84
- * annotation. Return the text string to create the annotation, or `null` to cancel.
112
+ * annotation. When editing an existing text annotation, `request.currentText` is populated and
113
+ * `request.mode` is `'edit'`. Return the text string to create/update the annotation, or `null`
114
+ * to cancel.
85
115
  */
86
- onTextInputRequested?: () => Promise<string | null>;
116
+ onTextInputRequested?: (request: TextInputRequest) => Promise<string | null>;
87
117
  style?: StyleProp<ViewStyle>;
88
118
  };
@@ -2,11 +2,11 @@ import { requireNativeModule, requireNativeView } from 'expo';
2
2
  import * as React from 'react';
3
3
  import { findNodeHandle } from 'react-native';
4
4
 
5
- import { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';
5
+ import { ExpoPdfMarkupViewProps, TextInputRequest } from './ExpoPdfMarkup.types';
6
6
 
7
7
  type NativeViewProps = Omit<ExpoPdfMarkupViewProps, 'onTextInputRequested'> & {
8
8
  useJsTextDialog?: boolean;
9
- onTextInputRequested?: (event: { nativeEvent: Record<string, never> }) => void;
9
+ onTextInputRequested?: (event: { nativeEvent: TextInputRequest }) => void;
10
10
  };
11
11
 
12
12
  const NativeView = requireNativeView('ExpoPdfMarkup') as React.ForwardRefExoticComponent<
@@ -21,13 +21,16 @@ export default function ExpoPdfMarkupView({
21
21
  }: ExpoPdfMarkupViewProps) {
22
22
  const nativeRef = React.useRef<unknown>(null);
23
23
 
24
- const handleNativeTextInputRequested = React.useCallback(async () => {
25
- if (!onTextInputRequested) return;
26
- const tag = findNodeHandle(nativeRef.current as Parameters<typeof findNodeHandle>[0]);
27
- if (tag == null) return;
28
- const text = await onTextInputRequested();
29
- await NativeModule.provideTextInput(tag, text ?? null);
30
- }, [onTextInputRequested]);
24
+ const handleNativeTextInputRequested = React.useCallback(
25
+ async (event: { nativeEvent: TextInputRequest }) => {
26
+ if (!onTextInputRequested) return;
27
+ const tag = findNodeHandle(nativeRef.current as Parameters<typeof findNodeHandle>[0]);
28
+ if (tag == null) return;
29
+ const text = await onTextInputRequested(event.nativeEvent);
30
+ await NativeModule.provideTextInput(tag, text ?? null);
31
+ },
32
+ [onTextInputRequested]
33
+ );
31
34
 
32
35
  return (
33
36
  <NativeView