@tobyt/expo-pdf-markup 0.4.0 → 0.6.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/CHANGELOG.md +22 -0
- package/README.md +12 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +49 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +20 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +52 -6
- package/build/ExpoPdfMarkup.types.d.ts +29 -1
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.d.ts +1 -1
- package/build/ExpoPdfMarkupView.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.js +15 -3
- package/build/ExpoPdfMarkupView.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +131 -43
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/pdfjs.d.ts.map +1 -1
- package/build/pdfjs.js +0 -1
- package/build/pdfjs.js.map +1 -1
- package/build/web/annotationUtils.d.ts +9 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +88 -5
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +185 -0
- package/ios/AnnotationSerializer.swift +39 -31
- package/ios/ExpoPdfMarkupModule.swift +16 -1
- package/ios/ExpoPdfMarkupView.swift +287 -74
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +37 -1
- package/src/ExpoPdfMarkupView.tsx +38 -5
- package/src/ExpoPdfMarkupView.web.tsx +170 -41
- package/src/pdfjs.ts +0 -1
- package/src/web/annotationUtils.ts +121 -4
|
@@ -7,6 +7,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
7
7
|
let onLoadComplete = EventDispatcher()
|
|
8
8
|
let onError = EventDispatcher()
|
|
9
9
|
let onAnnotationsChanged = EventDispatcher()
|
|
10
|
+
let onTextInputRequested = EventDispatcher()
|
|
10
11
|
|
|
11
12
|
private var currentSource: String?
|
|
12
13
|
private var currentAnnotationsJSON: String?
|
|
@@ -18,15 +19,28 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
var annotationLineWidth: CGFloat = 2.0
|
|
22
|
+
var annotationFontFamily: String?
|
|
23
|
+
var useJsTextDialog: Bool = false
|
|
21
24
|
|
|
22
25
|
private var inkPanGesture: UIPanGestureRecognizer?
|
|
23
26
|
private var inkShapeLayer: CAShapeLayer?
|
|
24
27
|
private var inkPoints: [CGPoint] = []
|
|
25
28
|
private var eraserTapGesture: UITapGestureRecognizer?
|
|
26
|
-
private var
|
|
29
|
+
private var movePanGesture: UIPanGestureRecognizer?
|
|
30
|
+
private var editingOverlayLayer: CAShapeLayer?
|
|
31
|
+
var textTapGesture: UITapGestureRecognizer?
|
|
27
32
|
private var selectionObserver: NSObjectProtocol?
|
|
28
33
|
private var selectionDebounceTimer: Timer?
|
|
29
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?
|
|
40
|
+
|
|
41
|
+
var pendingTextPage: PDFPage?
|
|
42
|
+
var pendingTextPoint: CGPoint?
|
|
43
|
+
var pendingTextAnnotation: PDFAnnotation?
|
|
30
44
|
|
|
31
45
|
required init(appContext: AppContext? = nil) {
|
|
32
46
|
super.init(appContext: appContext)
|
|
@@ -63,6 +77,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
63
77
|
super.layoutSubviews()
|
|
64
78
|
pdfView.frame = bounds
|
|
65
79
|
inkShapeLayer?.frame = bounds
|
|
80
|
+
editingOverlayLayer?.frame = pdfView.documentView?.bounds ?? .zero
|
|
81
|
+
updateEditingOverlay()
|
|
66
82
|
}
|
|
67
83
|
|
|
68
84
|
// MARK: - Props
|
|
@@ -89,6 +105,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
89
105
|
if let json = currentAnnotationsJSON {
|
|
90
106
|
currentAnnotationsJSON = nil
|
|
91
107
|
loadAnnotations(from: json)
|
|
108
|
+
} else {
|
|
109
|
+
updateEditingOverlay()
|
|
92
110
|
}
|
|
93
111
|
}
|
|
94
112
|
|
|
@@ -110,6 +128,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
110
128
|
guard let json, !json.isEmpty else {
|
|
111
129
|
clearModuleManagedAnnotations()
|
|
112
130
|
currentAnnotationsJSON = nil
|
|
131
|
+
updateEditingOverlay()
|
|
113
132
|
return
|
|
114
133
|
}
|
|
115
134
|
|
|
@@ -131,6 +150,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
131
150
|
}
|
|
132
151
|
page.addAnnotation(pdfAnnotation)
|
|
133
152
|
}
|
|
153
|
+
|
|
154
|
+
updateEditingOverlay()
|
|
134
155
|
}
|
|
135
156
|
|
|
136
157
|
func serializeCurrentAnnotations() -> String {
|
|
@@ -156,6 +177,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
156
177
|
let json = serializeCurrentAnnotations()
|
|
157
178
|
currentAnnotationsJSON = json
|
|
158
179
|
onAnnotationsChanged(["annotations": json])
|
|
180
|
+
updateEditingOverlay()
|
|
159
181
|
}
|
|
160
182
|
|
|
161
183
|
private func clearModuleManagedAnnotations() {
|
|
@@ -167,6 +189,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
167
189
|
page.removeAnnotation(annotation)
|
|
168
190
|
}
|
|
169
191
|
}
|
|
192
|
+
updateEditingOverlay()
|
|
170
193
|
}
|
|
171
194
|
|
|
172
195
|
// MARK: - Annotation mode
|
|
@@ -179,6 +202,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
179
202
|
if previousMode != mode {
|
|
180
203
|
removeInkMode()
|
|
181
204
|
removeEraserGesture()
|
|
205
|
+
removeMoveMode()
|
|
182
206
|
removeTextGesture()
|
|
183
207
|
removeSelectionObserver()
|
|
184
208
|
}
|
|
@@ -188,6 +212,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
188
212
|
setupInkMode()
|
|
189
213
|
case "eraser":
|
|
190
214
|
setupEraserGesture()
|
|
215
|
+
case "move":
|
|
216
|
+
setupMoveMode()
|
|
191
217
|
case "highlight", "underline":
|
|
192
218
|
setupSelectionObserver(markupType: mode)
|
|
193
219
|
case "text":
|
|
@@ -195,6 +221,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
195
221
|
default:
|
|
196
222
|
break
|
|
197
223
|
}
|
|
224
|
+
|
|
225
|
+
updateEditingOverlay()
|
|
198
226
|
}
|
|
199
227
|
|
|
200
228
|
// MARK: - Ink drawing
|
|
@@ -445,108 +473,293 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
445
473
|
}
|
|
446
474
|
}
|
|
447
475
|
|
|
448
|
-
// MARK: -
|
|
476
|
+
// MARK: - UIGestureRecognizerDelegate
|
|
449
477
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
textTapGesture = tap
|
|
478
|
+
func gestureRecognizer(
|
|
479
|
+
_: UIGestureRecognizer,
|
|
480
|
+
shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
|
|
481
|
+
) -> Bool {
|
|
482
|
+
true
|
|
456
483
|
}
|
|
457
484
|
|
|
458
|
-
|
|
459
|
-
|
|
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
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
func removeMoveMode() {
|
|
518
|
+
if let gesture = movePanGesture {
|
|
460
519
|
pdfView.removeGestureRecognizer(gesture)
|
|
461
|
-
|
|
520
|
+
movePanGesture = nil
|
|
462
521
|
}
|
|
522
|
+
finishMove(commit: false)
|
|
523
|
+
pdfScrollView?.isScrollEnabled = true
|
|
463
524
|
}
|
|
464
525
|
|
|
465
|
-
@objc
|
|
466
|
-
|
|
467
|
-
|
|
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 }
|
|
468
544
|
|
|
469
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 }
|
|
567
|
+
|
|
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
|
+
}
|
|
584
|
+
|
|
585
|
+
private func finishMove(commit: Bool) {
|
|
586
|
+
guard let page = movingPage,
|
|
587
|
+
let originalModel = movingOriginalModel else {
|
|
588
|
+
clearMoveState()
|
|
589
|
+
updateEditingOverlay()
|
|
590
|
+
return
|
|
591
|
+
}
|
|
470
592
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
let alert = UIAlertController(title: "Add Text", message: nil, preferredStyle: .alert)
|
|
474
|
-
alert.addTextField { textField in
|
|
475
|
-
textField.placeholder = "Enter text"
|
|
593
|
+
if let preview = movingPreviewAnnotation {
|
|
594
|
+
page.removeAnnotation(preview)
|
|
476
595
|
}
|
|
477
596
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
597
|
+
let finalModel = commit ? (movingPreviewModel ?? originalModel) : originalModel
|
|
598
|
+
if let finalAnnotation = annotationForModel(finalModel, on: page) {
|
|
599
|
+
page.addAnnotation(finalAnnotation)
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
let didChange = commit && finalModel != originalModel
|
|
603
|
+
clearMoveState()
|
|
604
|
+
updateEditingOverlay()
|
|
605
|
+
|
|
606
|
+
if didChange {
|
|
607
|
+
emitAnnotationsChanged()
|
|
608
|
+
}
|
|
609
|
+
}
|
|
483
610
|
|
|
484
|
-
|
|
611
|
+
private func clearMoveState() {
|
|
612
|
+
movingPage = nil
|
|
613
|
+
movingOriginalModel = nil
|
|
614
|
+
movingPreviewModel = nil
|
|
615
|
+
movingPreviewAnnotation = nil
|
|
616
|
+
moveStartPoint = nil
|
|
485
617
|
}
|
|
486
618
|
|
|
487
|
-
|
|
488
|
-
let fontSize: CGFloat = 16.0
|
|
489
|
-
let font = UIFont.systemFont(ofSize: fontSize)
|
|
490
|
-
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
619
|
+
// MARK: - Editing overlay
|
|
491
620
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
y: point.y - textSize.height - padding,
|
|
499
|
-
width: textSize.width + padding * 2,
|
|
500
|
-
height: textSize.height + padding * 2
|
|
501
|
-
)
|
|
621
|
+
func updateEditingOverlay() {
|
|
622
|
+
guard currentMode == "move" || currentMode == "eraser" else {
|
|
623
|
+
editingOverlayLayer?.removeFromSuperlayer()
|
|
624
|
+
editingOverlayLayer = nil
|
|
625
|
+
return
|
|
626
|
+
}
|
|
502
627
|
|
|
503
|
-
let
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
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
|
+
}
|
|
508
646
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
page.addAnnotation(annotation)
|
|
647
|
+
overlay.frame = documentView.bounds
|
|
648
|
+
let path = UIBezierPath()
|
|
512
649
|
|
|
513
|
-
|
|
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
|
|
514
661
|
}
|
|
515
662
|
|
|
516
|
-
private func
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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)
|
|
521
673
|
}
|
|
522
|
-
|
|
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
|
|
523
679
|
}
|
|
524
|
-
|
|
680
|
+
|
|
681
|
+
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
682
|
+
return documentView.convert(pdfViewRect, from: pdfView)
|
|
525
683
|
}
|
|
526
684
|
|
|
527
|
-
|
|
685
|
+
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
686
|
+
AnnotationSerializer.toPDFAnnotation(model, pageBounds: page.bounds(for: pdfView.displayBox))
|
|
687
|
+
}
|
|
528
688
|
|
|
529
|
-
func
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
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
|
|
534
714
|
}
|
|
535
715
|
|
|
536
|
-
|
|
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
|
+
}
|
|
537
725
|
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
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)
|
|
541
731
|
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
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
|
+
)
|
|
551
764
|
}
|
|
552
765
|
}
|
package/package.json
CHANGED
|
@@ -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 =
|
|
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: family name (`"serif"`, `"monospace"`) 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 };
|
|
@@ -78,5 +106,13 @@ export type ExpoPdfMarkupViewProps = {
|
|
|
78
106
|
onError?: (event: { nativeEvent: { message: string } }) => void;
|
|
79
107
|
/** Fired after annotations are created or edited. `annotations` is a serialised {@link AnnotationsData} JSON string. */
|
|
80
108
|
onAnnotationsChanged?: (event: { nativeEvent: { annotations: string } }) => void;
|
|
109
|
+
/**
|
|
110
|
+
* Optional async callback for the text annotation tool. When provided, this is called instead of
|
|
111
|
+
* the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text
|
|
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.
|
|
115
|
+
*/
|
|
116
|
+
onTextInputRequested?: (request: TextInputRequest) => Promise<string | null>;
|
|
81
117
|
style?: StyleProp<ViewStyle>;
|
|
82
118
|
};
|
|
@@ -1,10 +1,43 @@
|
|
|
1
|
-
import { requireNativeView } from 'expo';
|
|
1
|
+
import { requireNativeModule, requireNativeView } from 'expo';
|
|
2
2
|
import * as React from 'react';
|
|
3
|
+
import { findNodeHandle } from 'react-native';
|
|
3
4
|
|
|
4
|
-
import { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';
|
|
5
|
+
import { ExpoPdfMarkupViewProps, TextInputRequest } from './ExpoPdfMarkup.types';
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
type NativeViewProps = Omit<ExpoPdfMarkupViewProps, 'onTextInputRequested'> & {
|
|
8
|
+
useJsTextDialog?: boolean;
|
|
9
|
+
onTextInputRequested?: (event: { nativeEvent: TextInputRequest }) => void;
|
|
10
|
+
};
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
const NativeView = requireNativeView('ExpoPdfMarkup') as React.ForwardRefExoticComponent<
|
|
13
|
+
NativeViewProps & React.RefAttributes<unknown>
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
const NativeModule = requireNativeModule('ExpoPdfMarkup');
|
|
17
|
+
|
|
18
|
+
export default function ExpoPdfMarkupView({
|
|
19
|
+
onTextInputRequested,
|
|
20
|
+
...props
|
|
21
|
+
}: ExpoPdfMarkupViewProps) {
|
|
22
|
+
const nativeRef = React.useRef<unknown>(null);
|
|
23
|
+
|
|
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
|
+
);
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<NativeView
|
|
37
|
+
ref={nativeRef}
|
|
38
|
+
{...props}
|
|
39
|
+
useJsTextDialog={!!onTextInputRequested}
|
|
40
|
+
onTextInputRequested={onTextInputRequested ? handleNativeTextInputRequested : undefined}
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
10
43
|
}
|