@tobyt/expo-pdf-markup 1.1.4 → 2.0.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 +18 -0
- package/android/build.gradle +3 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookPager.kt +66 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookTurnPolicy.kt +117 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +370 -57
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +96 -5
- package/build/ExpoPdfMarkup.types.d.ts +19 -1
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +396 -65
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/web/bookMode.d.ts +77 -0
- package/build/web/bookMode.d.ts.map +1 -0
- package/build/web/bookMode.js +169 -0
- package/build/web/bookMode.js.map +1 -0
- package/build/web/layout.d.ts +8 -0
- package/build/web/layout.d.ts.map +1 -0
- package/build/web/layout.js +22 -0
- package/build/web/layout.js.map +1 -0
- package/build/web/types.d.ts +2 -0
- package/build/web/types.d.ts.map +1 -1
- package/build/web/types.js.map +1 -1
- package/docs/agents/domain.md +51 -0
- package/docs/agents/issue-tracker.md +46 -0
- package/docs/agents/triage-labels.md +15 -0
- package/docs/annotation-coordinates.md +45 -0
- package/docs/annotation-validation.md +117 -0
- package/fixtures/annotations-v1.json +130 -0
- package/ios/AnnotationModels.swift +29 -30
- package/ios/AnnotationSerializer.swift +99 -12
- package/ios/ExpoPdfMarkupModule.swift +4 -0
- package/ios/ExpoPdfMarkupView+Book.swift +455 -0
- package/ios/ExpoPdfMarkupView+Ink.swift +13 -28
- package/ios/ExpoPdfMarkupView+Markup.swift +151 -0
- package/ios/ExpoPdfMarkupView+Move.swift +17 -6
- package/ios/ExpoPdfMarkupView.swift +76 -84
- package/ios/StampPDFAnnotation.swift +21 -3
- package/ios/Tests/AnnotationCoordinateTests.swift +337 -0
- package/ios/Tests/AnnotationSerializerTests.swift +43 -1
- package/ios/Tests/AreaMarkupTests.swift +279 -0
- package/ios/Tests/BookModeTests.swift +534 -0
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +29 -0
- package/ios/Tests/RotatedAnnotationRenderingTests.swift +411 -0
- package/ios/Tests/book-sample.pdf +207 -0
- package/ios/Tests/single-page.pdf +52 -0
- package/package.json +3 -2
- package/src/ExpoPdfMarkup.types.ts +20 -1
- package/src/ExpoPdfMarkupView.web.tsx +471 -70
- package/src/web/bookMode.ts +186 -0
- package/src/web/layout.ts +30 -0
- package/src/web/types.ts +3 -0
- package/tsconfig.test.json +10 -0
|
@@ -84,35 +84,20 @@ extension ExpoPdfMarkupView {
|
|
|
84
84
|
let pdfViewMidPoint = pdfView.convert(midPoint, from: self)
|
|
85
85
|
guard let page = pdfView.page(for: pdfViewMidPoint, nearest: true) else { return }
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
let
|
|
89
|
-
let
|
|
90
|
-
|
|
87
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
88
|
+
let points = inkPoints.map { displayPoint -> [String: CGFloat] in
|
|
89
|
+
let location = pdfView.convert(displayPoint, from: self)
|
|
90
|
+
let point = pdfView.convert(location, to: page).applying(coordinates.nativeToVisible)
|
|
91
|
+
return ["x": point.x, "y": point.y]
|
|
91
92
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
let
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
let border = PDFBorder()
|
|
101
|
-
border.lineWidth = annotationLineWidth
|
|
102
|
-
annotation.border = border
|
|
103
|
-
|
|
104
|
-
let bezierPath = UIBezierPath()
|
|
105
|
-
for (index, point) in pdfPoints.enumerated() {
|
|
106
|
-
if index == 0 {
|
|
107
|
-
bezierPath.move(to: point)
|
|
108
|
-
} else {
|
|
109
|
-
bezierPath.addLine(to: point)
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
annotation.add(bezierPath)
|
|
113
|
-
|
|
114
|
-
let id = UUID().uuidString
|
|
115
|
-
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
93
|
+
let model = AnnotationModel(id: UUID().uuidString, type: "ink", page: 0,
|
|
94
|
+
color: annotationColor, lineWidth: annotationLineWidth, paths: [points],
|
|
95
|
+
createdAt: Date().timeIntervalSince1970)
|
|
96
|
+
guard let annotation = AnnotationSerializer.toPDFAnnotation(
|
|
97
|
+
model,
|
|
98
|
+
pageBounds: page.bounds(for: .cropBox),
|
|
99
|
+
pageRotation: page.rotation
|
|
100
|
+
) else { return }
|
|
116
101
|
page.addAnnotation(annotation)
|
|
117
102
|
|
|
118
103
|
emitAnnotationsChanged()
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import PDFKit
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
extension ExpoPdfMarkupView {
|
|
5
|
+
/// Area input works on scanned and selectable pages alike. Navigation is explicit:
|
|
6
|
+
/// leave the tool for touch scrolling/zoom, or use the page prop/book hardware keys.
|
|
7
|
+
func setupAreaMarkupMode() {
|
|
8
|
+
guard markupOverlayView == nil else { return }
|
|
9
|
+
markupScrollWasEnabled = pdfScrollView?.isScrollEnabled
|
|
10
|
+
pdfScrollView?.isScrollEnabled = false
|
|
11
|
+
markupDisabledGestures = pdfView.gestureRecognizers?.filter(\.isEnabled) ?? []
|
|
12
|
+
markupDisabledGestures.forEach { $0.isEnabled = false }
|
|
13
|
+
pdfView.clearSelection()
|
|
14
|
+
|
|
15
|
+
let overlay = InkOverlayView(frame: bounds)
|
|
16
|
+
overlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
17
|
+
let preview = CAShapeLayer()
|
|
18
|
+
overlay.layer.addSublayer(preview)
|
|
19
|
+
markupPreviewLayer = preview
|
|
20
|
+
overlay.onBegan = { [weak self] in self?.beginAreaMarkup(at: $0) }
|
|
21
|
+
overlay.onMoved = { [weak self] in self?.updateAreaMarkup(at: $0) }
|
|
22
|
+
overlay.onEnded = { [weak self] in self?.finishAreaMarkup() }
|
|
23
|
+
overlay.onCancelled = { [weak self] in self?.cancelAreaMarkup() }
|
|
24
|
+
addSubview(overlay)
|
|
25
|
+
markupOverlayView = overlay
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func removeAreaMarkupMode() {
|
|
29
|
+
cancelAreaMarkup()
|
|
30
|
+
markupOverlayView?.removeFromSuperview()
|
|
31
|
+
markupOverlayView = nil
|
|
32
|
+
markupPreviewLayer = nil
|
|
33
|
+
if let wasEnabled = markupScrollWasEnabled { pdfScrollView?.isScrollEnabled = wasEnabled }
|
|
34
|
+
markupScrollWasEnabled = nil
|
|
35
|
+
markupDisabledGestures.forEach { $0.isEnabled = true }
|
|
36
|
+
markupDisabledGestures = []
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
func cancelAreaMarkup() {
|
|
40
|
+
markupPage = nil
|
|
41
|
+
markupStartPoint = nil
|
|
42
|
+
markupEndPoint = nil
|
|
43
|
+
markupPreviewLayer?.path = nil
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private func beginAreaMarkup(at point: CGPoint) {
|
|
47
|
+
cancelAreaMarkup()
|
|
48
|
+
let location = pdfView.convert(point, from: self)
|
|
49
|
+
guard let page = pdfView.page(for: location, nearest: false) else { return }
|
|
50
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
51
|
+
let visible = pdfView.convert(location, to: page).applying(coordinates.nativeToVisible)
|
|
52
|
+
guard coordinates.visibleBounds.contains(visible) else { return }
|
|
53
|
+
markupPage = page
|
|
54
|
+
markupStartPoint = visible
|
|
55
|
+
markupEndPoint = visible
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private func updateAreaMarkup(at point: CGPoint) {
|
|
59
|
+
guard let page = markupPage, page.document === pdfView.document else { return }
|
|
60
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
61
|
+
let visible = pdfView.convert(pdfView.convert(point, from: self), to: page)
|
|
62
|
+
.applying(coordinates.nativeToVisible)
|
|
63
|
+
let bounds = coordinates.visibleBounds
|
|
64
|
+
markupEndPoint = CGPoint(x: min(max(visible.x, bounds.minX), bounds.maxX),
|
|
65
|
+
y: min(max(visible.y, bounds.minY), bounds.maxY))
|
|
66
|
+
guard let area = areaMarkupBounds else { return }
|
|
67
|
+
let native = area.applying(coordinates.visibleToNative)
|
|
68
|
+
let display = convert(pdfView.convert(native, from: page), from: pdfView)
|
|
69
|
+
let path = UIBezierPath()
|
|
70
|
+
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
71
|
+
if currentMode == "highlight" {
|
|
72
|
+
path.append(UIBezierPath(rect: display))
|
|
73
|
+
markupPreviewLayer?.fillColor = color.withAlphaComponent(0.5).cgColor
|
|
74
|
+
markupPreviewLayer?.strokeColor = nil
|
|
75
|
+
} else {
|
|
76
|
+
path.move(to: CGPoint(x: display.minX, y: display.maxY))
|
|
77
|
+
path.addLine(to: CGPoint(x: display.maxX, y: display.maxY))
|
|
78
|
+
markupPreviewLayer?.fillColor = nil
|
|
79
|
+
markupPreviewLayer?.strokeColor = color.cgColor
|
|
80
|
+
markupPreviewLayer?.lineWidth = AreaUnderlinePDFAnnotation.strokeWidth * pdfView.scaleFactor
|
|
81
|
+
}
|
|
82
|
+
markupPreviewLayer?.path = path.cgPath
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private var areaMarkupBounds: CGRect? {
|
|
86
|
+
guard let start = markupStartPoint, let end = markupEndPoint else { return nil }
|
|
87
|
+
return CGRect(x: min(start.x, end.x), y: min(start.y, end.y),
|
|
88
|
+
width: abs(end.x - start.x), height: abs(end.y - start.y))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private func finishAreaMarkup() {
|
|
92
|
+
defer { cancelAreaMarkup() }
|
|
93
|
+
guard currentMode == "highlight" || currentMode == "underline",
|
|
94
|
+
let page = markupPage, page.document === pdfView.document,
|
|
95
|
+
let bounds = areaMarkupBounds, bounds.width > 1, bounds.height > 1 else { return }
|
|
96
|
+
let model = AnnotationModel(id: UUID().uuidString, type: currentMode,
|
|
97
|
+
page: pdfView.document?.index(for: page) ?? 0, color: annotationColor,
|
|
98
|
+
alpha: currentMode == "highlight" ? 0.5 : nil,
|
|
99
|
+
bounds: AnnotationBounds(bounds), createdAt: Date().timeIntervalSince1970)
|
|
100
|
+
guard let annotation = AnnotationSerializer.toPDFAnnotation(
|
|
101
|
+
model, pageBounds: page.bounds(for: .cropBox), pageRotation: page.rotation
|
|
102
|
+
) else { return }
|
|
103
|
+
page.addAnnotation(annotation)
|
|
104
|
+
emitAnnotationsChanged()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// PDFKit's built-in underline offsets its stroke toward native page-up. On rotated
|
|
109
|
+
/// pages that can select the wrong edge or put the line outside the crop box.
|
|
110
|
+
/// Drawing the visible rectangle's lower edge also keeps edit and hit geometry aligned.
|
|
111
|
+
final class AreaUnderlinePDFAnnotation: PDFAnnotation {
|
|
112
|
+
static let strokeWidth: CGFloat = 2
|
|
113
|
+
|
|
114
|
+
required init?(coder: NSCoder) {
|
|
115
|
+
super.init(coder: coder)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
override init(bounds: CGRect, forType type: PDFAnnotationSubtype, withProperties properties: [AnyHashable: Any]?) {
|
|
119
|
+
super.init(
|
|
120
|
+
bounds: bounds.insetBy(
|
|
121
|
+
dx: -AnnotationCoordinates.appearancePadding,
|
|
122
|
+
dy: -AnnotationCoordinates.appearancePadding
|
|
123
|
+
),
|
|
124
|
+
forType: type,
|
|
125
|
+
withProperties: properties
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
var contentBounds: CGRect {
|
|
130
|
+
bounds.insetBy(dx: AnnotationCoordinates.appearancePadding, dy: AnnotationCoordinates.appearancePadding)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var renderedBounds: CGRect {
|
|
134
|
+
contentBounds.insetBy(dx: -Self.strokeWidth / 2, dy: -Self.strokeWidth / 2)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
override func draw(with box: PDFDisplayBox, in context: CGContext) {
|
|
138
|
+
let coordinates = page.map {
|
|
139
|
+
AnnotationCoordinates(bounds: $0.bounds(for: box), rotation: $0.rotation)
|
|
140
|
+
} ?? AnnotationCoordinates(bounds: .zero)
|
|
141
|
+
let visible = contentBounds.applying(coordinates.nativeToVisible)
|
|
142
|
+
context.saveGState()
|
|
143
|
+
coordinates.prepareForDrawing(in: context, annotationBounds: bounds)
|
|
144
|
+
context.setStrokeColor(color.cgColor)
|
|
145
|
+
context.setLineWidth(Self.strokeWidth)
|
|
146
|
+
context.move(to: CGPoint(x: visible.minX, y: visible.minY))
|
|
147
|
+
context.addLine(to: CGPoint(x: visible.maxX, y: visible.minY))
|
|
148
|
+
context.strokePath()
|
|
149
|
+
context.restoreGState()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -66,8 +66,11 @@ extension ExpoPdfMarkupView {
|
|
|
66
66
|
let startPoint = moveStartPoint else { return }
|
|
67
67
|
|
|
68
68
|
let pdfPoint = pdfView.convert(location, to: page)
|
|
69
|
-
let
|
|
70
|
-
let
|
|
69
|
+
let coordinates = AnnotationCoordinates(page: page)
|
|
70
|
+
let current = pdfPoint.applying(coordinates.nativeToVisible)
|
|
71
|
+
let start = startPoint.applying(coordinates.nativeToVisible)
|
|
72
|
+
let rawDeltaX = current.x - start.x
|
|
73
|
+
let rawDeltaY = current.y - start.y
|
|
71
74
|
let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
|
|
72
75
|
let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
|
|
73
76
|
guard translatedModel != movingPreviewModel,
|
|
@@ -173,9 +176,13 @@ extension ExpoPdfMarkupView {
|
|
|
173
176
|
}
|
|
174
177
|
guard !combinedBounds.isNull else { return nil }
|
|
175
178
|
let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
176
|
-
pdfRect = combinedBounds.
|
|
179
|
+
pdfRect = combinedBounds.offsetBy(dx: annotation.bounds.minX, dy: annotation.bounds.minY).insetBy(
|
|
180
|
+
dx: -padding,
|
|
181
|
+
dy: -padding
|
|
182
|
+
)
|
|
177
183
|
} else {
|
|
178
|
-
pdfRect = annotation
|
|
184
|
+
pdfRect = (annotation as? AreaUnderlinePDFAnnotation)?.renderedBounds ?? (annotation as? StampPDFAnnotation)?
|
|
185
|
+
.contentBounds ?? annotation.bounds
|
|
179
186
|
}
|
|
180
187
|
|
|
181
188
|
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
@@ -183,7 +190,11 @@ extension ExpoPdfMarkupView {
|
|
|
183
190
|
}
|
|
184
191
|
|
|
185
192
|
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
186
|
-
AnnotationSerializer.toPDFAnnotation(
|
|
193
|
+
AnnotationSerializer.toPDFAnnotation(
|
|
194
|
+
model,
|
|
195
|
+
pageBounds: page.bounds(for: pdfView.displayBox),
|
|
196
|
+
pageRotation: page.rotation
|
|
197
|
+
)
|
|
187
198
|
}
|
|
188
199
|
|
|
189
200
|
private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
|
|
@@ -223,7 +234,7 @@ extension ExpoPdfMarkupView {
|
|
|
223
234
|
return CGPoint(x: dx, y: dy)
|
|
224
235
|
}
|
|
225
236
|
|
|
226
|
-
let pageBounds = page
|
|
237
|
+
let pageBounds = AnnotationCoordinates(page: page).visibleBounds
|
|
227
238
|
let minDx = pageBounds.minX - outlineBounds.x
|
|
228
239
|
let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
|
|
229
240
|
let minDy = pageBounds.minY - outlineBounds.y
|
|
@@ -33,9 +33,20 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
33
33
|
var editingOverlayLayer: CAShapeLayer?
|
|
34
34
|
var textTapGesture: UITapGestureRecognizer?
|
|
35
35
|
var stampTapGesture: UITapGestureRecognizer?
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
var bookTapGesture: UITapGestureRecognizer?
|
|
37
|
+
var bookPanGesture: UIPanGestureRecognizer?
|
|
38
|
+
var bookEdgeDoubleTapGesture: UITapGestureRecognizer?
|
|
39
|
+
/// Tap count of the touch the book tap last received, so the second tap of a double-tap is skipped.
|
|
40
|
+
var bookTapCount = 1
|
|
41
|
+
var bookTurnSnapshot: UIView?
|
|
42
|
+
var bookGestureDelegate: BookGestureDelegate?
|
|
43
|
+
var markupOverlayView: InkOverlayView?
|
|
44
|
+
var markupPreviewLayer: CAShapeLayer?
|
|
45
|
+
var markupPage: PDFPage?
|
|
46
|
+
var markupStartPoint: CGPoint?
|
|
47
|
+
var markupEndPoint: CGPoint?
|
|
48
|
+
var markupDisabledGestures: [UIGestureRecognizer] = []
|
|
49
|
+
var markupScrollWasEnabled: Bool?
|
|
39
50
|
var movingPage: PDFPage?
|
|
40
51
|
var movingOriginalModel: AnnotationModel?
|
|
41
52
|
var movingPreviewModel: AnnotationModel?
|
|
@@ -72,23 +83,41 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
72
83
|
name: .PDFViewPageChanged,
|
|
73
84
|
object: pdfView
|
|
74
85
|
)
|
|
86
|
+
// When a text input anywhere lets go of the keyboard, book mode takes the keys back.
|
|
87
|
+
for name in [UITextField.textDidEndEditingNotification, UITextView.textDidEndEditingNotification] {
|
|
88
|
+
NotificationCenter.default.addObserver(self, selector: #selector(handleTextEditingEnded), name: name, object: nil)
|
|
89
|
+
}
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
deinit {
|
|
78
93
|
NotificationCenter.default.removeObserver(self)
|
|
79
|
-
if let selectionObserver {
|
|
80
|
-
NotificationCenter.default.removeObserver(selectionObserver)
|
|
81
|
-
}
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
override func layoutSubviews() {
|
|
85
97
|
super.layoutSubviews()
|
|
86
|
-
|
|
98
|
+
// Bounds and centre rather than frame: book mode slides pdfView with a transform.
|
|
99
|
+
pdfView.bounds = CGRect(origin: .zero, size: bounds.size)
|
|
100
|
+
pdfView.center = CGPoint(x: bounds.midX, y: bounds.midY)
|
|
87
101
|
inkShapeLayer?.frame = bounds
|
|
88
102
|
editingOverlayLayer?.frame = pdfView.documentView?.bounds ?? .zero
|
|
89
103
|
updateEditingOverlay()
|
|
90
104
|
}
|
|
91
105
|
|
|
106
|
+
override func didMoveToWindow() {
|
|
107
|
+
super.didMoveToWindow()
|
|
108
|
+
claimBookKeyFocus()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// MARK: - Hardware keys (book mode only, see ExpoPdfMarkupView+Book.swift)
|
|
112
|
+
|
|
113
|
+
override var canBecomeFirstResponder: Bool {
|
|
114
|
+
isBookMode
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
override var keyCommands: [UIKeyCommand]? {
|
|
118
|
+
isBookMode ? bookKeyCommands : nil
|
|
119
|
+
}
|
|
120
|
+
|
|
92
121
|
// MARK: - Props
|
|
93
122
|
|
|
94
123
|
func setBackgroundColor(_ color: UIColor?) {
|
|
@@ -97,6 +126,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
97
126
|
|
|
98
127
|
func loadPdf(from source: String) {
|
|
99
128
|
guard source != currentSource else { return }
|
|
129
|
+
cancelAreaMarkup()
|
|
100
130
|
currentSource = source
|
|
101
131
|
|
|
102
132
|
let fileURL = URL(fileURLWithPath: source)
|
|
@@ -127,7 +157,12 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
127
157
|
return
|
|
128
158
|
}
|
|
129
159
|
|
|
130
|
-
|
|
160
|
+
cancelAreaMarkup()
|
|
161
|
+
if isBookMode {
|
|
162
|
+
jumpToBookPage(page)
|
|
163
|
+
} else {
|
|
164
|
+
pdfView.go(to: page)
|
|
165
|
+
}
|
|
131
166
|
}
|
|
132
167
|
|
|
133
168
|
// MARK: - Annotation management
|
|
@@ -153,7 +188,11 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
153
188
|
guard model.page >= 0, model.page < document.pageCount else { continue }
|
|
154
189
|
guard let page = document.page(at: model.page) else { continue }
|
|
155
190
|
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
156
|
-
guard let pdfAnnotation = AnnotationSerializer.toPDFAnnotation(
|
|
191
|
+
guard let pdfAnnotation = AnnotationSerializer.toPDFAnnotation(
|
|
192
|
+
model,
|
|
193
|
+
pageBounds: pageBounds,
|
|
194
|
+
pageRotation: page.rotation
|
|
195
|
+
) else {
|
|
157
196
|
continue
|
|
158
197
|
}
|
|
159
198
|
page.addAnnotation(pdfAnnotation)
|
|
@@ -200,6 +239,19 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
200
239
|
updateEditingOverlay()
|
|
201
240
|
}
|
|
202
241
|
|
|
242
|
+
// MARK: - Display mode
|
|
243
|
+
|
|
244
|
+
/// `"continuous"` (default) or `"book"`. See `ExpoPdfMarkupView+Book.swift`.
|
|
245
|
+
private(set) var displayMode: String = "continuous"
|
|
246
|
+
|
|
247
|
+
func setDisplayMode(_ mode: String) {
|
|
248
|
+
let normalised = mode == "book" ? "book" : "continuous"
|
|
249
|
+
guard normalised != displayMode else { return }
|
|
250
|
+
cancelAreaMarkup()
|
|
251
|
+
displayMode = normalised
|
|
252
|
+
applyDisplayMode()
|
|
253
|
+
}
|
|
254
|
+
|
|
203
255
|
// MARK: - Annotation mode
|
|
204
256
|
|
|
205
257
|
func setAnnotationMode(_ mode: String) {
|
|
@@ -208,12 +260,15 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
208
260
|
|
|
209
261
|
// Clean up previous mode
|
|
210
262
|
if previousMode != mode {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
263
|
+
switch previousMode {
|
|
264
|
+
case "ink": removeInkMode()
|
|
265
|
+
case "eraser": removeEraserGesture()
|
|
266
|
+
case "move": removeMoveMode()
|
|
267
|
+
case "text": removeTextGesture()
|
|
268
|
+
case "stamp": removeStampGesture()
|
|
269
|
+
case "highlight", "underline": removeAreaMarkupMode()
|
|
270
|
+
default: break
|
|
271
|
+
}
|
|
217
272
|
}
|
|
218
273
|
|
|
219
274
|
switch mode {
|
|
@@ -224,7 +279,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
224
279
|
case "move":
|
|
225
280
|
setupMoveMode()
|
|
226
281
|
case "highlight", "underline":
|
|
227
|
-
|
|
282
|
+
setupAreaMarkupMode()
|
|
228
283
|
case "text":
|
|
229
284
|
setupTextGesture()
|
|
230
285
|
case "stamp":
|
|
@@ -281,83 +336,19 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
281
336
|
lineJoin: .round,
|
|
282
337
|
miterLimit: 0
|
|
283
338
|
)
|
|
284
|
-
|
|
339
|
+
let localPoint = CGPoint(x: point.x - annotation.bounds.minX, y: point.y - annotation.bounds.minY)
|
|
340
|
+
if hitPath.contains(localPoint) {
|
|
285
341
|
return annotation
|
|
286
342
|
}
|
|
287
343
|
}
|
|
288
|
-
} else if annotation
|
|
344
|
+
} else if ((annotation as? AreaUnderlinePDFAnnotation)?.renderedBounds ?? (annotation as? StampPDFAnnotation)?
|
|
345
|
+
.contentBounds ?? annotation.bounds).contains(point) {
|
|
289
346
|
return annotation
|
|
290
347
|
}
|
|
291
348
|
}
|
|
292
349
|
return nil
|
|
293
350
|
}
|
|
294
351
|
|
|
295
|
-
// MARK: - Highlight / Underline
|
|
296
|
-
|
|
297
|
-
private func setupSelectionObserver(markupType: String) {
|
|
298
|
-
guard selectionObserver == nil else { return }
|
|
299
|
-
currentMarkupType = markupType
|
|
300
|
-
|
|
301
|
-
// Observe selection changes with a debounce — the notification fires continuously
|
|
302
|
-
// during drag. We wait for the selection to settle before creating the annotation.
|
|
303
|
-
selectionObserver = NotificationCenter.default.addObserver(
|
|
304
|
-
forName: .PDFViewSelectionChanged,
|
|
305
|
-
object: pdfView,
|
|
306
|
-
queue: .main
|
|
307
|
-
) { [weak self] _ in
|
|
308
|
-
self?.scheduleSelectionHandler()
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
private func removeSelectionObserver() {
|
|
313
|
-
selectionDebounceTimer?.invalidate()
|
|
314
|
-
selectionDebounceTimer = nil
|
|
315
|
-
currentMarkupType = nil
|
|
316
|
-
if let observer = selectionObserver {
|
|
317
|
-
NotificationCenter.default.removeObserver(observer)
|
|
318
|
-
selectionObserver = nil
|
|
319
|
-
}
|
|
320
|
-
pdfView.clearSelection()
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
private func scheduleSelectionHandler() {
|
|
324
|
-
selectionDebounceTimer?.invalidate()
|
|
325
|
-
selectionDebounceTimer = Timer.scheduledTimer(
|
|
326
|
-
withTimeInterval: 0.5,
|
|
327
|
-
repeats: false
|
|
328
|
-
) { [weak self] _ in
|
|
329
|
-
self?.applyMarkupToSelection()
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
private func applyMarkupToSelection() {
|
|
334
|
-
guard let markupType = currentMarkupType else { return }
|
|
335
|
-
guard let selection = pdfView.currentSelection else { return }
|
|
336
|
-
let selectionsByPage = selection.selectionsByLine()
|
|
337
|
-
|
|
338
|
-
var didAdd = false
|
|
339
|
-
for lineSelection in selectionsByPage {
|
|
340
|
-
guard let page = lineSelection.pages.first else { continue }
|
|
341
|
-
let bounds = lineSelection.bounds(for: page)
|
|
342
|
-
guard bounds.width > 0, bounds.height > 0 else { continue }
|
|
343
|
-
|
|
344
|
-
let subtype: PDFAnnotationSubtype = markupType == "highlight" ? .highlight : .underline
|
|
345
|
-
let annotation = PDFAnnotation(bounds: bounds, forType: subtype, withProperties: nil)
|
|
346
|
-
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .yellow
|
|
347
|
-
annotation.color = markupType == "highlight" ? color.withAlphaComponent(0.5) : color
|
|
348
|
-
|
|
349
|
-
let id = UUID().uuidString
|
|
350
|
-
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
351
|
-
page.addAnnotation(annotation)
|
|
352
|
-
didAdd = true
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if didAdd {
|
|
356
|
-
pdfView.clearSelection()
|
|
357
|
-
emitAnnotationsChanged()
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
352
|
// MARK: - UIGestureRecognizerDelegate
|
|
362
353
|
|
|
363
354
|
func gestureRecognizer(
|
|
@@ -370,6 +361,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
370
361
|
// MARK: - Page change notification
|
|
371
362
|
|
|
372
363
|
@objc private func handlePageChanged() {
|
|
364
|
+
cancelAreaMarkup()
|
|
373
365
|
guard let document = pdfView.document,
|
|
374
366
|
let currentPage = pdfView.currentPage else { return }
|
|
375
367
|
|
|
@@ -16,7 +16,18 @@ final class StampPDFAnnotation: PDFAnnotation {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
override init(bounds: CGRect, forType type: PDFAnnotationSubtype, withProperties properties: [AnyHashable: Any]?) {
|
|
19
|
-
super.init(
|
|
19
|
+
super.init(
|
|
20
|
+
bounds: bounds.insetBy(
|
|
21
|
+
dx: -AnnotationCoordinates.appearancePadding,
|
|
22
|
+
dy: -AnnotationCoordinates.appearancePadding
|
|
23
|
+
),
|
|
24
|
+
forType: type,
|
|
25
|
+
withProperties: properties
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var contentBounds: CGRect {
|
|
30
|
+
bounds.insetBy(dx: AnnotationCoordinates.appearancePadding, dy: AnnotationCoordinates.appearancePadding)
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
func setStampText(_ text: String) {
|
|
@@ -27,12 +38,19 @@ final class StampPDFAnnotation: PDFAnnotation {
|
|
|
27
38
|
value(forAnnotationKey: Self.textKey) as? String
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
override func draw(with
|
|
41
|
+
override func draw(with box: PDFDisplayBox, in context: CGContext) {
|
|
31
42
|
guard let text = stampText else { return }
|
|
32
43
|
let colorHex = (value(forAnnotationKey: Self.colorKey) as? String) ?? "#000000"
|
|
33
44
|
guard let cgImage = Self.image(forText: text, colorHex: colorHex) else { return }
|
|
34
45
|
|
|
35
|
-
|
|
46
|
+
let coordinates = page.map {
|
|
47
|
+
AnnotationCoordinates(bounds: $0.bounds(for: box), rotation: $0.rotation)
|
|
48
|
+
} ?? AnnotationCoordinates(bounds: .zero)
|
|
49
|
+
let visibleBounds = contentBounds.applying(coordinates.nativeToVisible)
|
|
50
|
+
context.saveGState()
|
|
51
|
+
coordinates.prepareForDrawing(in: context, annotationBounds: bounds)
|
|
52
|
+
context.draw(cgImage, in: visibleBounds)
|
|
53
|
+
context.restoreGState()
|
|
36
54
|
}
|
|
37
55
|
|
|
38
56
|
private static func image(forText text: String, colorHex: String) -> CGImage? {
|