@tobyt/expo-pdf-markup 0.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/LICENSE +21 -0
- package/README.md +54 -0
- package/android/.editorconfig +2 -0
- package/android/build.gradle +30 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +76 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +27 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +138 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +152 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +539 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +81 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +147 -0
- package/build/ExpoPdfMarkup.types.d.ts +96 -0
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -0
- package/build/ExpoPdfMarkup.types.js +2 -0
- package/build/ExpoPdfMarkup.types.js.map +1 -0
- package/build/ExpoPdfMarkupModule.d.ts +2 -0
- package/build/ExpoPdfMarkupModule.d.ts.map +1 -0
- package/build/ExpoPdfMarkupModule.js +4 -0
- package/build/ExpoPdfMarkupModule.js.map +1 -0
- package/build/ExpoPdfMarkupModule.web.d.ts +2 -0
- package/build/ExpoPdfMarkupModule.web.d.ts.map +1 -0
- package/build/ExpoPdfMarkupModule.web.js +4 -0
- package/build/ExpoPdfMarkupModule.web.js.map +1 -0
- package/build/ExpoPdfMarkupView.d.ts +4 -0
- package/build/ExpoPdfMarkupView.d.ts.map +1 -0
- package/build/ExpoPdfMarkupView.js +7 -0
- package/build/ExpoPdfMarkupView.js.map +1 -0
- package/build/ExpoPdfMarkupView.web.d.ts +4 -0
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -0
- package/build/ExpoPdfMarkupView.web.js +8 -0
- package/build/ExpoPdfMarkupView.web.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/AnnotationModels.swift +73 -0
- package/ios/AnnotationSerializer.swift +252 -0
- package/ios/ExpoPdfMarkup.podspec +28 -0
- package/ios/ExpoPdfMarkupModule.swift +39 -0
- package/ios/ExpoPdfMarkupView.swift +541 -0
- package/package.json +67 -0
- package/src/ExpoPdfMarkup.types.ts +80 -0
- package/src/ExpoPdfMarkupModule.ts +2 -0
- package/src/ExpoPdfMarkupModule.web.ts +2 -0
- package/src/ExpoPdfMarkupView.tsx +10 -0
- package/src/ExpoPdfMarkupView.web.tsx +12 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import PDFKit
|
|
3
|
+
|
|
4
|
+
class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
5
|
+
let pdfView = PDFView()
|
|
6
|
+
let onPageChanged = EventDispatcher()
|
|
7
|
+
let onLoadComplete = EventDispatcher()
|
|
8
|
+
let onError = EventDispatcher()
|
|
9
|
+
let onAnnotationsChanged = EventDispatcher()
|
|
10
|
+
|
|
11
|
+
private var currentSource: String?
|
|
12
|
+
private var currentAnnotationsJSON: String?
|
|
13
|
+
private var currentMode: String = "none"
|
|
14
|
+
var annotationColor: String = "#FF0000"
|
|
15
|
+
var annotationLineWidth: CGFloat = 2.0
|
|
16
|
+
|
|
17
|
+
private var inkPanGesture: UIPanGestureRecognizer?
|
|
18
|
+
private var inkShapeLayer: CAShapeLayer?
|
|
19
|
+
private var inkPoints: [CGPoint] = []
|
|
20
|
+
private var eraserTapGesture: UITapGestureRecognizer?
|
|
21
|
+
private var textTapGesture: UITapGestureRecognizer?
|
|
22
|
+
private var selectionObserver: NSObjectProtocol?
|
|
23
|
+
private var selectionDebounceTimer: Timer?
|
|
24
|
+
private var currentMarkupType: String?
|
|
25
|
+
|
|
26
|
+
required init(appContext: AppContext? = nil) {
|
|
27
|
+
super.init(appContext: appContext)
|
|
28
|
+
clipsToBounds = true
|
|
29
|
+
|
|
30
|
+
pdfView.autoScales = true
|
|
31
|
+
pdfView.displayMode = .singlePageContinuous
|
|
32
|
+
pdfView.displayDirection = .vertical
|
|
33
|
+
pdfView.pageBreakMargins = UIEdgeInsets(top: 4, left: 0, bottom: 4, right: 0)
|
|
34
|
+
pdfView.backgroundColor = UIColor(white: 0.92, alpha: 1)
|
|
35
|
+
|
|
36
|
+
addSubview(pdfView)
|
|
37
|
+
|
|
38
|
+
if let scrollView = pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView {
|
|
39
|
+
scrollView.contentInsetAdjustmentBehavior = .never
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
NotificationCenter.default.addObserver(
|
|
43
|
+
self,
|
|
44
|
+
selector: #selector(handlePageChanged),
|
|
45
|
+
name: .PDFViewPageChanged,
|
|
46
|
+
object: pdfView
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
deinit {
|
|
51
|
+
NotificationCenter.default.removeObserver(self)
|
|
52
|
+
if let selectionObserver {
|
|
53
|
+
NotificationCenter.default.removeObserver(selectionObserver)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
override func layoutSubviews() {
|
|
58
|
+
super.layoutSubviews()
|
|
59
|
+
pdfView.frame = bounds
|
|
60
|
+
inkShapeLayer?.frame = bounds
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// MARK: - Props
|
|
64
|
+
|
|
65
|
+
func setBackgroundColor(_ color: UIColor?) {
|
|
66
|
+
pdfView.backgroundColor = color ?? UIColor(white: 0.92, alpha: 1)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
func loadPdf(from source: String) {
|
|
70
|
+
guard source != currentSource else { return }
|
|
71
|
+
currentSource = source
|
|
72
|
+
|
|
73
|
+
let fileURL = URL(fileURLWithPath: source)
|
|
74
|
+
guard let document = PDFDocument(url: fileURL) else {
|
|
75
|
+
onError(["message": "Failed to load PDF from: \(source)"])
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pdfView.document = document
|
|
80
|
+
let pageCount = document.pageCount
|
|
81
|
+
onLoadComplete(["pageCount": pageCount])
|
|
82
|
+
|
|
83
|
+
// Re-apply annotations if we have them
|
|
84
|
+
if let json = currentAnnotationsJSON {
|
|
85
|
+
currentAnnotationsJSON = nil
|
|
86
|
+
loadAnnotations(from: json)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
func goToPage(_ pageIndex: Int) {
|
|
91
|
+
guard let document = pdfView.document else { return }
|
|
92
|
+
guard pageIndex >= 0, pageIndex < document.pageCount else { return }
|
|
93
|
+
guard let page = document.page(at: pageIndex) else { return }
|
|
94
|
+
|
|
95
|
+
if let currentPage = pdfView.currentPage, document.index(for: currentPage) == pageIndex {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
pdfView.go(to: page)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// MARK: - Annotation management
|
|
103
|
+
|
|
104
|
+
func loadAnnotations(from json: String?) {
|
|
105
|
+
guard let json, !json.isEmpty else {
|
|
106
|
+
clearModuleManagedAnnotations()
|
|
107
|
+
currentAnnotationsJSON = nil
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
guard json != currentAnnotationsJSON else { return }
|
|
112
|
+
currentAnnotationsJSON = json
|
|
113
|
+
|
|
114
|
+
guard let document = pdfView.document else { return }
|
|
115
|
+
|
|
116
|
+
clearModuleManagedAnnotations()
|
|
117
|
+
|
|
118
|
+
guard let data = AnnotationSerializer.deserialize(from: json) else { return }
|
|
119
|
+
|
|
120
|
+
for model in data.annotations {
|
|
121
|
+
guard model.page >= 0, model.page < document.pageCount else { continue }
|
|
122
|
+
guard let page = document.page(at: model.page) else { continue }
|
|
123
|
+
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
124
|
+
guard let pdfAnnotation = AnnotationSerializer.toPDFAnnotation(model, pageBounds: pageBounds) else {
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
page.addAnnotation(pdfAnnotation)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
func serializeCurrentAnnotations() -> String {
|
|
132
|
+
guard let document = pdfView.document else {
|
|
133
|
+
return "{\"version\":1,\"annotations\":[]}"
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
var models: [AnnotationModel] = []
|
|
137
|
+
for pageIndex in 0 ..< document.pageCount {
|
|
138
|
+
guard let page = document.page(at: pageIndex) else { continue }
|
|
139
|
+
for annotation in page.annotations {
|
|
140
|
+
if let model = AnnotationSerializer.toModel(from: annotation, page: pageIndex) {
|
|
141
|
+
models.append(model)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let data = AnnotationsData(version: 1, annotations: models)
|
|
147
|
+
return AnnotationSerializer.serialize(data) ?? "{\"version\":1,\"annotations\":[]}"
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
func emitAnnotationsChanged() {
|
|
151
|
+
let json = serializeCurrentAnnotations()
|
|
152
|
+
currentAnnotationsJSON = json
|
|
153
|
+
onAnnotationsChanged(["annotations": json])
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private func clearModuleManagedAnnotations() {
|
|
157
|
+
guard let document = pdfView.document else { return }
|
|
158
|
+
for pageIndex in 0 ..< document.pageCount {
|
|
159
|
+
guard let page = document.page(at: pageIndex) else { continue }
|
|
160
|
+
let toRemove = page.annotations.filter { AnnotationSerializer.isModuleManaged($0) }
|
|
161
|
+
for annotation in toRemove {
|
|
162
|
+
page.removeAnnotation(annotation)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// MARK: - Annotation mode
|
|
168
|
+
|
|
169
|
+
func setAnnotationMode(_ mode: String) {
|
|
170
|
+
let previousMode = currentMode
|
|
171
|
+
currentMode = mode
|
|
172
|
+
|
|
173
|
+
// Clean up previous mode
|
|
174
|
+
if previousMode != mode {
|
|
175
|
+
removeInkMode()
|
|
176
|
+
removeEraserGesture()
|
|
177
|
+
removeTextGesture()
|
|
178
|
+
removeSelectionObserver()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
switch mode {
|
|
182
|
+
case "ink":
|
|
183
|
+
setupInkMode()
|
|
184
|
+
case "eraser":
|
|
185
|
+
setupEraserGesture()
|
|
186
|
+
case "highlight", "underline":
|
|
187
|
+
setupSelectionObserver(markupType: mode)
|
|
188
|
+
case "text":
|
|
189
|
+
setupTextGesture()
|
|
190
|
+
default:
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// MARK: - Ink drawing
|
|
196
|
+
|
|
197
|
+
private var pdfScrollView: UIScrollView? {
|
|
198
|
+
pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private func setupInkMode() {
|
|
202
|
+
guard inkPanGesture == nil else { return }
|
|
203
|
+
|
|
204
|
+
// Disable scrolling so pan gestures go to our recognizer, not the scroll view
|
|
205
|
+
pdfScrollView?.isScrollEnabled = false
|
|
206
|
+
|
|
207
|
+
// Shape layer for live stroke feedback (added to self so it's above pdfView)
|
|
208
|
+
let shape = CAShapeLayer()
|
|
209
|
+
shape.fillColor = nil
|
|
210
|
+
shape.lineCap = .round
|
|
211
|
+
shape.lineJoin = .round
|
|
212
|
+
shape.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
213
|
+
shape.lineWidth = annotationLineWidth
|
|
214
|
+
shape.frame = bounds
|
|
215
|
+
layer.addSublayer(shape)
|
|
216
|
+
inkShapeLayer = shape
|
|
217
|
+
|
|
218
|
+
// Pan gesture on pdfView — points come in pdfView's coordinate space
|
|
219
|
+
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleInkPan(_:)))
|
|
220
|
+
pan.maximumNumberOfTouches = 1
|
|
221
|
+
pdfView.addGestureRecognizer(pan)
|
|
222
|
+
inkPanGesture = pan
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private func removeInkMode() {
|
|
226
|
+
if let gesture = inkPanGesture {
|
|
227
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
228
|
+
inkPanGesture = nil
|
|
229
|
+
}
|
|
230
|
+
inkShapeLayer?.removeFromSuperlayer()
|
|
231
|
+
inkShapeLayer = nil
|
|
232
|
+
inkPoints = []
|
|
233
|
+
pdfScrollView?.isScrollEnabled = true
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
@objc private func handleInkPan(_ gesture: UIPanGestureRecognizer) {
|
|
237
|
+
// Get point in self's coordinate space for visual feedback
|
|
238
|
+
let displayPoint = gesture.location(in: self)
|
|
239
|
+
|
|
240
|
+
switch gesture.state {
|
|
241
|
+
case .began:
|
|
242
|
+
inkPoints = [displayPoint]
|
|
243
|
+
updateInkShapeLayer()
|
|
244
|
+
case .changed:
|
|
245
|
+
inkPoints.append(displayPoint)
|
|
246
|
+
updateInkShapeLayer()
|
|
247
|
+
case .ended:
|
|
248
|
+
inkPoints.append(displayPoint)
|
|
249
|
+
updateInkShapeLayer()
|
|
250
|
+
finishInkStroke()
|
|
251
|
+
case .cancelled, .failed:
|
|
252
|
+
clearInkStroke()
|
|
253
|
+
default:
|
|
254
|
+
break
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private func updateInkShapeLayer() {
|
|
259
|
+
let path = UIBezierPath()
|
|
260
|
+
for (index, point) in inkPoints.enumerated() {
|
|
261
|
+
if index == 0 {
|
|
262
|
+
path.move(to: point)
|
|
263
|
+
} else {
|
|
264
|
+
path.addLine(to: point)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
inkShapeLayer?.path = path.cgPath
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private func finishInkStroke() {
|
|
271
|
+
defer { clearInkStroke() }
|
|
272
|
+
guard inkPoints.count >= 2 else { return }
|
|
273
|
+
|
|
274
|
+
// Determine which page the stroke is on using the midpoint
|
|
275
|
+
let midPoint = inkPoints[inkPoints.count / 2]
|
|
276
|
+
// Convert display point (in self's coordinate space) to pdfView coordinate space
|
|
277
|
+
let pdfViewMidPoint = pdfView.convert(midPoint, from: self)
|
|
278
|
+
guard let page = pdfView.page(for: pdfViewMidPoint, nearest: true) else { return }
|
|
279
|
+
|
|
280
|
+
// Convert all display points to PDF page coordinates
|
|
281
|
+
let pdfPoints = inkPoints.map { displayPoint -> CGPoint in
|
|
282
|
+
let pdfViewPoint = pdfView.convert(displayPoint, from: self)
|
|
283
|
+
return pdfView.convert(pdfViewPoint, to: page)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Use the full page bounds — PDFKit clips ink rendering to annotation bounds,
|
|
287
|
+
// and tight bounds calculations are fragile with coordinate transforms.
|
|
288
|
+
let annotationBounds = page.bounds(for: pdfView.displayBox)
|
|
289
|
+
|
|
290
|
+
let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
|
|
291
|
+
annotation.color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
292
|
+
|
|
293
|
+
let border = PDFBorder()
|
|
294
|
+
border.lineWidth = annotationLineWidth
|
|
295
|
+
annotation.border = border
|
|
296
|
+
|
|
297
|
+
let bezierPath = UIBezierPath()
|
|
298
|
+
for (index, point) in pdfPoints.enumerated() {
|
|
299
|
+
if index == 0 {
|
|
300
|
+
bezierPath.move(to: point)
|
|
301
|
+
} else {
|
|
302
|
+
bezierPath.addLine(to: point)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
annotation.add(bezierPath)
|
|
306
|
+
|
|
307
|
+
let id = UUID().uuidString
|
|
308
|
+
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
309
|
+
page.addAnnotation(annotation)
|
|
310
|
+
|
|
311
|
+
emitAnnotationsChanged()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private func clearInkStroke() {
|
|
315
|
+
inkPoints = []
|
|
316
|
+
inkShapeLayer?.path = nil
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// MARK: - Eraser
|
|
320
|
+
|
|
321
|
+
private func setupEraserGesture() {
|
|
322
|
+
guard eraserTapGesture == nil else { return }
|
|
323
|
+
let tap = UITapGestureRecognizer(target: self, action: #selector(handleEraserTap(_:)))
|
|
324
|
+
tap.delegate = self
|
|
325
|
+
pdfView.addGestureRecognizer(tap)
|
|
326
|
+
eraserTapGesture = tap
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private func removeEraserGesture() {
|
|
330
|
+
if let gesture = eraserTapGesture {
|
|
331
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
332
|
+
eraserTapGesture = nil
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
@objc private func handleEraserTap(_ gesture: UITapGestureRecognizer) {
|
|
337
|
+
let location = gesture.location(in: pdfView)
|
|
338
|
+
guard let page = pdfView.page(for: location, nearest: true) else { return }
|
|
339
|
+
|
|
340
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
341
|
+
|
|
342
|
+
// Use path-based hit testing — page.annotation(at:) only checks bounds rect,
|
|
343
|
+
// which is the full page for our ink annotations. Instead, test each annotation's
|
|
344
|
+
// actual path with a stroke tolerance.
|
|
345
|
+
if let annotation = hitTestAnnotation(at: pdfPoint, on: page) {
|
|
346
|
+
page.removeAnnotation(annotation)
|
|
347
|
+
emitAnnotationsChanged()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private func hitTestAnnotation(at point: CGPoint, on page: PDFPage) -> PDFAnnotation? {
|
|
352
|
+
for annotation in page.annotations.reversed() {
|
|
353
|
+
guard AnnotationSerializer.isModuleManaged(annotation) else { continue }
|
|
354
|
+
|
|
355
|
+
// For ink annotations, test against the actual paths
|
|
356
|
+
if let paths = annotation.paths {
|
|
357
|
+
let tolerance = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
358
|
+
for path in paths {
|
|
359
|
+
let hitPath = path.cgPath.copy(
|
|
360
|
+
strokingWithWidth: tolerance,
|
|
361
|
+
lineCap: .round,
|
|
362
|
+
lineJoin: .round,
|
|
363
|
+
miterLimit: 0
|
|
364
|
+
)
|
|
365
|
+
if hitPath.contains(point) {
|
|
366
|
+
return annotation
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
} else if annotation.bounds.contains(point) {
|
|
370
|
+
// For non-ink annotations (highlight, underline, text), bounds check is fine
|
|
371
|
+
return annotation
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return nil
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// MARK: - Highlight / Underline
|
|
378
|
+
|
|
379
|
+
private func setupSelectionObserver(markupType: String) {
|
|
380
|
+
guard selectionObserver == nil else { return }
|
|
381
|
+
currentMarkupType = markupType
|
|
382
|
+
|
|
383
|
+
// Observe selection changes with a debounce — the notification fires continuously
|
|
384
|
+
// during drag. We wait for the selection to settle before creating the annotation.
|
|
385
|
+
selectionObserver = NotificationCenter.default.addObserver(
|
|
386
|
+
forName: .PDFViewSelectionChanged,
|
|
387
|
+
object: pdfView,
|
|
388
|
+
queue: .main
|
|
389
|
+
) { [weak self] _ in
|
|
390
|
+
self?.scheduleSelectionHandler()
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private func removeSelectionObserver() {
|
|
395
|
+
selectionDebounceTimer?.invalidate()
|
|
396
|
+
selectionDebounceTimer = nil
|
|
397
|
+
currentMarkupType = nil
|
|
398
|
+
if let observer = selectionObserver {
|
|
399
|
+
NotificationCenter.default.removeObserver(observer)
|
|
400
|
+
selectionObserver = nil
|
|
401
|
+
}
|
|
402
|
+
pdfView.clearSelection()
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private func scheduleSelectionHandler() {
|
|
406
|
+
selectionDebounceTimer?.invalidate()
|
|
407
|
+
selectionDebounceTimer = Timer.scheduledTimer(
|
|
408
|
+
withTimeInterval: 0.5,
|
|
409
|
+
repeats: false
|
|
410
|
+
) { [weak self] _ in
|
|
411
|
+
self?.applyMarkupToSelection()
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private func applyMarkupToSelection() {
|
|
416
|
+
guard let markupType = currentMarkupType else { return }
|
|
417
|
+
guard let selection = pdfView.currentSelection else { return }
|
|
418
|
+
let selectionsByPage = selection.selectionsByLine()
|
|
419
|
+
|
|
420
|
+
var didAdd = false
|
|
421
|
+
for lineSelection in selectionsByPage {
|
|
422
|
+
guard let page = lineSelection.pages.first else { continue }
|
|
423
|
+
let bounds = lineSelection.bounds(for: page)
|
|
424
|
+
guard bounds.width > 0, bounds.height > 0 else { continue }
|
|
425
|
+
|
|
426
|
+
let subtype: PDFAnnotationSubtype = markupType == "highlight" ? .highlight : .underline
|
|
427
|
+
let annotation = PDFAnnotation(bounds: bounds, forType: subtype, withProperties: nil)
|
|
428
|
+
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .yellow
|
|
429
|
+
annotation.color = markupType == "highlight" ? color.withAlphaComponent(0.5) : color
|
|
430
|
+
|
|
431
|
+
let id = UUID().uuidString
|
|
432
|
+
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
433
|
+
page.addAnnotation(annotation)
|
|
434
|
+
didAdd = true
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if didAdd {
|
|
438
|
+
pdfView.clearSelection()
|
|
439
|
+
emitAnnotationsChanged()
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// MARK: - Text / Sticky notes
|
|
444
|
+
|
|
445
|
+
private func setupTextGesture() {
|
|
446
|
+
guard textTapGesture == nil else { return }
|
|
447
|
+
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTextTap(_:)))
|
|
448
|
+
tap.delegate = self
|
|
449
|
+
pdfView.addGestureRecognizer(tap)
|
|
450
|
+
textTapGesture = tap
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
private func removeTextGesture() {
|
|
454
|
+
if let gesture = textTapGesture {
|
|
455
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
456
|
+
textTapGesture = nil
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
@objc private func handleTextTap(_ gesture: UITapGestureRecognizer) {
|
|
461
|
+
let location = gesture.location(in: pdfView)
|
|
462
|
+
guard let page = pdfView.page(for: location, nearest: true) else { return }
|
|
463
|
+
|
|
464
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
465
|
+
|
|
466
|
+
// Show text input dialog
|
|
467
|
+
guard let viewController = findViewController() else { return }
|
|
468
|
+
let alert = UIAlertController(title: "Add Text", message: nil, preferredStyle: .alert)
|
|
469
|
+
alert.addTextField { textField in
|
|
470
|
+
textField.placeholder = "Enter text"
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
474
|
+
alert.addAction(UIAlertAction(title: "Add", style: .default) { [weak self] _ in
|
|
475
|
+
guard let self, let text = alert.textFields?.first?.text, !text.isEmpty else { return }
|
|
476
|
+
addFreeTextAnnotation(text: text, at: pdfPoint, on: page)
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
viewController.present(alert, animated: true)
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
private func addFreeTextAnnotation(text: String, at point: CGPoint, on page: PDFPage) {
|
|
483
|
+
let fontSize: CGFloat = 16.0
|
|
484
|
+
let font = UIFont.systemFont(ofSize: fontSize)
|
|
485
|
+
let color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
486
|
+
|
|
487
|
+
// Measure text size
|
|
488
|
+
let attributes: [NSAttributedString.Key: Any] = [.font: font]
|
|
489
|
+
let textSize = (text as NSString).size(withAttributes: attributes)
|
|
490
|
+
let padding: CGFloat = 4.0
|
|
491
|
+
let bounds = CGRect(
|
|
492
|
+
x: point.x,
|
|
493
|
+
y: point.y - textSize.height - padding,
|
|
494
|
+
width: textSize.width + padding * 2,
|
|
495
|
+
height: textSize.height + padding * 2
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
let annotation = PDFAnnotation(bounds: bounds, forType: .freeText, withProperties: nil)
|
|
499
|
+
annotation.font = font
|
|
500
|
+
annotation.fontColor = color
|
|
501
|
+
annotation.color = .clear
|
|
502
|
+
annotation.contents = text
|
|
503
|
+
|
|
504
|
+
let id = UUID().uuidString
|
|
505
|
+
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
506
|
+
page.addAnnotation(annotation)
|
|
507
|
+
|
|
508
|
+
emitAnnotationsChanged()
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
private func findViewController() -> UIViewController? {
|
|
512
|
+
var responder: UIResponder? = self
|
|
513
|
+
while let next = responder?.next {
|
|
514
|
+
if let vc = next as? UIViewController {
|
|
515
|
+
return vc
|
|
516
|
+
}
|
|
517
|
+
responder = next
|
|
518
|
+
}
|
|
519
|
+
return nil
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// MARK: - UIGestureRecognizerDelegate
|
|
523
|
+
|
|
524
|
+
func gestureRecognizer(
|
|
525
|
+
_: UIGestureRecognizer,
|
|
526
|
+
shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
|
|
527
|
+
) -> Bool {
|
|
528
|
+
true
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// MARK: - Page change notification
|
|
532
|
+
|
|
533
|
+
@objc private func handlePageChanged() {
|
|
534
|
+
guard let document = pdfView.document,
|
|
535
|
+
let currentPage = pdfView.currentPage else { return }
|
|
536
|
+
|
|
537
|
+
let pageIndex = document.index(for: currentPage)
|
|
538
|
+
let pageCount = document.pageCount
|
|
539
|
+
onPageChanged(["page": pageIndex, "pageCount": pageCount])
|
|
540
|
+
}
|
|
541
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tobyt/expo-pdf-markup",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "An Expo module for displaying and annotating PDFs",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "expo-module build",
|
|
9
|
+
"clean": "expo-module clean",
|
|
10
|
+
"lint": "expo-module lint",
|
|
11
|
+
"lint:swift": "swiftlint lint ios/",
|
|
12
|
+
"lint:kotlin": "ktlint 'android/src/**/*.kt'",
|
|
13
|
+
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|
|
14
|
+
"format:check": "prettier --check \"src/**/*.{ts,tsx}\"",
|
|
15
|
+
"format:swift": "swiftformat ios/",
|
|
16
|
+
"format:swift:check": "swiftformat --lint ios/",
|
|
17
|
+
"format:kotlin": "ktlint --format 'android/src/**/*.kt'",
|
|
18
|
+
"format:kotlin:check": "ktlint 'android/src/**/*.kt'",
|
|
19
|
+
"test": "expo-module test",
|
|
20
|
+
"test:swift": "bash scripts/test-swift.sh",
|
|
21
|
+
"test:kotlin": "cd example/android && ./gradlew :tobyt-expo-pdf-markup:testDebugUnitTest",
|
|
22
|
+
"typecheck": "expo-module typecheck",
|
|
23
|
+
"qa:ts": "export EXPO_NONINTERACTIVE=1 && npm run typecheck && npm run format && npm run lint && npm run test",
|
|
24
|
+
"qa:ios": "npm run format:swift && npm run lint:swift && npm run test:swift",
|
|
25
|
+
"qa:android": "npm run format:kotlin && npm run lint:kotlin && npm run test:kotlin",
|
|
26
|
+
"qa": "npm run qa:ts && npm run qa:ios && npm run qa:android",
|
|
27
|
+
"prepare": "expo-module prepare",
|
|
28
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
29
|
+
"expo-module": "expo-module",
|
|
30
|
+
"open:ios": "xed example/ios",
|
|
31
|
+
"open:android": "open -a \"Android Studio\" example/android",
|
|
32
|
+
"docs": "typedoc"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react-native",
|
|
36
|
+
"expo",
|
|
37
|
+
"@tobyt/expo-pdf-markup",
|
|
38
|
+
"ExpoPdfMarkup"
|
|
39
|
+
],
|
|
40
|
+
"repository": "https://github.com/tobyt42/expo-pdf-markup",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/tobyt42/expo-pdf-markup/issues"
|
|
43
|
+
},
|
|
44
|
+
"author": "Toby Terhoeven <toby@terhoeven.dev> (https://github.com/tobyt42)",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"homepage": "https://github.com/tobyt42/expo-pdf-markup#readme",
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/react": "~19.2.10",
|
|
52
|
+
"@types/react-test-renderer": "^19.1.0",
|
|
53
|
+
"expo": "^55.0.5",
|
|
54
|
+
"expo-module-scripts": "^55.0.2",
|
|
55
|
+
"react-native": "0.83.2",
|
|
56
|
+
"react-native-web": "^0.21.2",
|
|
57
|
+
"typedoc": "^0.28.17"
|
|
58
|
+
},
|
|
59
|
+
"jest": {
|
|
60
|
+
"preset": "expo-module-scripts"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"expo": "*",
|
|
64
|
+
"react": "*",
|
|
65
|
+
"react-native": "*"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export type AnnotationPoint = { x: number; y: number };
|
|
4
|
+
|
|
5
|
+
export type AnnotationBounds = { x: number; y: number; width: number; height: number };
|
|
6
|
+
|
|
7
|
+
export type InkAnnotation = {
|
|
8
|
+
id: string;
|
|
9
|
+
type: 'ink';
|
|
10
|
+
page: number;
|
|
11
|
+
color: string;
|
|
12
|
+
lineWidth: number;
|
|
13
|
+
paths: AnnotationPoint[][];
|
|
14
|
+
createdAt?: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type HighlightAnnotation = {
|
|
18
|
+
id: string;
|
|
19
|
+
type: 'highlight';
|
|
20
|
+
page: number;
|
|
21
|
+
color: string;
|
|
22
|
+
alpha?: number;
|
|
23
|
+
bounds: AnnotationBounds;
|
|
24
|
+
createdAt?: number;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type UnderlineAnnotation = {
|
|
28
|
+
id: string;
|
|
29
|
+
type: 'underline';
|
|
30
|
+
page: number;
|
|
31
|
+
color: string;
|
|
32
|
+
bounds: AnnotationBounds;
|
|
33
|
+
createdAt?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type TextAnnotation = {
|
|
37
|
+
id: string;
|
|
38
|
+
type: 'text' | 'freeText';
|
|
39
|
+
page: number;
|
|
40
|
+
color: string;
|
|
41
|
+
bounds: AnnotationBounds;
|
|
42
|
+
contents?: string;
|
|
43
|
+
fontSize?: number;
|
|
44
|
+
createdAt?: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type Annotation = InkAnnotation | HighlightAnnotation | UnderlineAnnotation | TextAnnotation;
|
|
48
|
+
|
|
49
|
+
export type AnnotationsData = {
|
|
50
|
+
version: 1;
|
|
51
|
+
annotations: Annotation[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type AnnotationMode = 'none' | 'ink' | 'highlight' | 'underline' | 'text' | 'eraser';
|
|
55
|
+
|
|
56
|
+
export type ExpoPdfMarkupViewProps = {
|
|
57
|
+
/** Local file path or remote URL of the PDF to display. */
|
|
58
|
+
source: string;
|
|
59
|
+
/** 0-based page number to display. Can be used to restore scroll position on orientation change. */
|
|
60
|
+
page?: number;
|
|
61
|
+
/** Background colour shown behind the PDF pages. */
|
|
62
|
+
backgroundColor?: string;
|
|
63
|
+
/** Serialised {@link AnnotationsData} JSON string to load when the document opens. */
|
|
64
|
+
annotations?: string;
|
|
65
|
+
/** Active annotation tool. Defaults to `'none'` (view-only). */
|
|
66
|
+
annotationMode?: AnnotationMode;
|
|
67
|
+
/** Colour applied to new annotations, as a CSS colour string (e.g. `'#FF0000'`). */
|
|
68
|
+
annotationColor?: string;
|
|
69
|
+
/** Stroke width in points applied to new ink annotations. */
|
|
70
|
+
annotationLineWidth?: number;
|
|
71
|
+
/** Fired when the visible page changes. */
|
|
72
|
+
onPageChanged?: (event: { nativeEvent: { page: number; pageCount: number } }) => void;
|
|
73
|
+
/** Fired once the document has finished loading. */
|
|
74
|
+
onLoadComplete?: (event: { nativeEvent: { pageCount: number } }) => void;
|
|
75
|
+
/** Fired when the document fails to load. */
|
|
76
|
+
onError?: (event: { nativeEvent: { message: string } }) => void;
|
|
77
|
+
/** Fired after annotations are created or edited. `annotations` is a serialised {@link AnnotationsData} JSON string. */
|
|
78
|
+
onAnnotationsChanged?: (event: { nativeEvent: { annotations: string } }) => void;
|
|
79
|
+
style?: StyleProp<ViewStyle>;
|
|
80
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { requireNativeView } from 'expo';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
import { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';
|
|
5
|
+
|
|
6
|
+
const NativeView: React.ComponentType<ExpoPdfMarkupViewProps> = requireNativeView('ExpoPdfMarkup');
|
|
7
|
+
|
|
8
|
+
export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
9
|
+
return <NativeView {...props} />;
|
|
10
|
+
}
|