@tobyt/expo-pdf-markup 1.1.3 → 1.2.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +7 -1
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookPager.kt +66 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookTurnPolicy.kt +117 -0
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +370 -57
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +96 -5
  8. package/build/ExpoPdfMarkup.types.d.ts +12 -0
  9. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  10. package/build/ExpoPdfMarkup.types.js.map +1 -1
  11. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  12. package/build/ExpoPdfMarkupView.web.js +369 -44
  13. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  14. package/build/web/bookMode.d.ts +77 -0
  15. package/build/web/bookMode.d.ts.map +1 -0
  16. package/build/web/bookMode.js +169 -0
  17. package/build/web/bookMode.js.map +1 -0
  18. package/build/web/layout.d.ts +8 -0
  19. package/build/web/layout.d.ts.map +1 -0
  20. package/build/web/layout.js +22 -0
  21. package/build/web/layout.js.map +1 -0
  22. package/build/web/types.d.ts +2 -0
  23. package/build/web/types.d.ts.map +1 -1
  24. package/build/web/types.js.map +1 -1
  25. package/docs/agents/domain.md +51 -0
  26. package/docs/agents/issue-tracker.md +46 -0
  27. package/docs/agents/triage-labels.md +15 -0
  28. package/ios/AnnotationSerializer.swift +36 -7
  29. package/ios/ExpoPdfMarkupModule.swift +4 -0
  30. package/ios/ExpoPdfMarkupView+Book.swift +455 -0
  31. package/ios/ExpoPdfMarkupView+Ink.swift +10 -2
  32. package/ios/ExpoPdfMarkupView+Move.swift +3 -4
  33. package/ios/ExpoPdfMarkupView.swift +49 -6
  34. package/ios/Tests/AnnotationSerializerTests.swift +0 -22
  35. package/ios/Tests/BookModeTests.swift +534 -0
  36. package/ios/Tests/book-sample.pdf +207 -0
  37. package/ios/Tests/single-page.pdf +52 -0
  38. package/metro.js +32 -14
  39. package/package.json +4 -3
  40. package/src/ExpoPdfMarkup.types.ts +13 -0
  41. package/src/ExpoPdfMarkupView.web.tsx +443 -52
  42. package/src/web/bookMode.ts +186 -0
  43. package/src/web/layout.ts +30 -0
  44. package/src/web/types.ts +3 -0
  45. package/tsconfig.test.json +10 -0
  46. package/docs/.nojekyll +0 -1
  47. package/docs/assets/hierarchy.js +0 -1
  48. package/docs/assets/highlight.css +0 -113
  49. package/docs/assets/icons.js +0 -18
  50. package/docs/assets/icons.svg +0 -1
  51. package/docs/assets/main.js +0 -60
  52. package/docs/assets/navigation.js +0 -1
  53. package/docs/assets/search.js +0 -1
  54. package/docs/assets/style.css +0 -1648
  55. package/docs/functions/ExpoPdfMarkupView.html +0 -42
  56. package/docs/functions/setPdfJsWorkerSrc.html +0 -42
  57. package/docs/hierarchy.html +0 -29
  58. package/docs/index.html +0 -148
  59. package/docs/modules.html +0 -40
  60. package/docs/types/Annotation.html +0 -33
  61. package/docs/types/AnnotationBounds.html +0 -73
  62. package/docs/types/AnnotationMode.html +0 -33
  63. package/docs/types/AnnotationPoint.html +0 -61
  64. package/docs/types/AnnotationsData.html +0 -61
  65. package/docs/types/ExpoPdfMarkupViewProps.html +0 -184
  66. package/docs/types/HighlightAnnotation.html +0 -91
  67. package/docs/types/InkAnnotation.html +0 -91
  68. package/docs/types/StampAnnotation.html +0 -95
  69. package/docs/types/StampDefinition.html +0 -72
  70. package/docs/types/TextAnnotation.html +0 -110
  71. package/docs/types/TextInputRequest.html +0 -73
  72. package/docs/types/UnderlineAnnotation.html +0 -85
  73. package/ios/InkPDFAnnotation.swift +0 -45
@@ -0,0 +1,455 @@
1
+ import PDFKit
2
+ import UIKit
3
+
4
+ /// Which way a book mode turn goes. `next` is always the page after, whatever the layout direction.
5
+ enum BookTurnDirection {
6
+ case previous
7
+ case next
8
+
9
+ var step: Int {
10
+ self == .next ? 1 : -1
11
+ }
12
+ }
13
+
14
+ /// What asked for a turn. The turn policy treats touch input and hardware keys differently.
15
+ enum BookTurnInput {
16
+ case swipe
17
+ case edgeTap
18
+ /// Hardware keys (#98): turn in book mode whatever the zoom or markup mode.
19
+ case key
20
+ }
21
+
22
+ /// Book mode tuning. The names follow the web (`src/web/bookMode.ts`) and Android
23
+ /// (`BookTurnPolicy`, `ContinuousPdfView`) constants, and the turn thresholds have the same values
24
+ /// on every platform. Only the bounce nudge distance differs.
25
+ enum BookMode {
26
+ /// Share of the view's width that each edge tap zone covers.
27
+ static let edgeZoneFraction: CGFloat = 0.25
28
+ /// Minimum width of an edge tap zone, in points.
29
+ static let minEdgeZone: CGFloat = 44
30
+ /// Share of the view's width a slow swipe has to travel to turn the page.
31
+ static let swipeTurnFraction: CGFloat = 0.25
32
+ /// A release faster than this (points per second) turns the page even after a short drag.
33
+ static let flingVelocity: CGFloat = 500
34
+ /// How much of the finger's travel the page follows where there's no page to turn to.
35
+ static let edgeResistance: CGFloat = 0.3
36
+ /// How far (points) the page nudges when a tap or key bounces at either end.
37
+ static let bouncePoints: CGFloat = 32
38
+ /// Scales within this share of fit count as "at fit", absorbing pinch rounding.
39
+ static let fitTolerance: CGFloat = 0.01
40
+ /// Duration of the slide to the next or previous page, in seconds.
41
+ static let turnDuration: TimeInterval = 0.25
42
+ /// Duration of each half of the bounce at either end, in seconds.
43
+ static let bounceDuration: TimeInterval = 0.12
44
+ }
45
+
46
+ extension ExpoPdfMarkupView {
47
+ // MARK: - Book mode presentation
48
+
49
+ var isBookMode: Bool {
50
+ displayMode == "book"
51
+ }
52
+
53
+ /// Switches PDFKit between the continuous scroll and the one-page-at-a-time book layout,
54
+ /// keeping the page the reader is on.
55
+ func applyDisplayMode() {
56
+ let keptPage = pdfView.currentPage
57
+
58
+ if isBookMode {
59
+ pdfView.displayMode = .singlePage
60
+ pdfView.displayDirection = .horizontal
61
+ pdfView.pageBreakMargins = .zero
62
+ installBookGestures()
63
+ } else {
64
+ removeBookGestures()
65
+ if isFirstResponder {
66
+ resignFirstResponder()
67
+ }
68
+ pdfView.displayMode = .singlePageContinuous
69
+ pdfView.displayDirection = .vertical
70
+ pdfView.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)
71
+ }
72
+ pdfView.autoScales = true
73
+
74
+ if let keptPage {
75
+ pdfView.go(to: keptPage)
76
+ }
77
+ claimBookKeyFocus()
78
+ }
79
+
80
+ // MARK: - Hardware keys
81
+
82
+ /// The turn a hardware key asks for: Right, Down and Page Down go forward; Left, Up and
83
+ /// Page Up go back. Space isn't mapped.
84
+ func bookTurnDirection(forKey input: String) -> BookTurnDirection? {
85
+ switch input {
86
+ case UIKeyCommand.inputRightArrow, UIKeyCommand.inputDownArrow, UIKeyCommand.inputPageDown:
87
+ .next
88
+ case UIKeyCommand.inputLeftArrow, UIKeyCommand.inputUpArrow, UIKeyCommand.inputPageUp:
89
+ .previous
90
+ default:
91
+ nil
92
+ }
93
+ }
94
+
95
+ /// Key commands for keyboards and page-turner pedals, offered to UIKit only in book mode.
96
+ /// Unmodified keys only, and ahead of the system's own arrow-key scrolling.
97
+ var bookKeyCommands: [UIKeyCommand] {
98
+ [
99
+ UIKeyCommand.inputRightArrow, UIKeyCommand.inputDownArrow, UIKeyCommand.inputPageDown,
100
+ UIKeyCommand.inputLeftArrow, UIKeyCommand.inputUpArrow, UIKeyCommand.inputPageUp,
101
+ ].map { input in
102
+ let command = UIKeyCommand(input: input, modifierFlags: [], action: #selector(handleBookKeyCommand(_:)))
103
+ command.wantsPriorityOverSystemBehavior = true
104
+ return command
105
+ }
106
+ }
107
+
108
+ @objc func handleBookKeyCommand(_ command: UIKeyCommand) {
109
+ guard let input = command.input, let direction = bookTurnDirection(forKey: input) else { return }
110
+ // Keys typed into a text input (including the host's text annotation input) are its own.
111
+ guard !isTextInputActive else { return }
112
+ turnBookPage(direction, from: .key)
113
+ }
114
+
115
+ /// Whether a text input outside this view is first responder, so keys belong to it.
116
+ /// PDFKit's own views inside this one don't count.
117
+ var isTextInputActive: Bool {
118
+ guard let responder = UIResponder.currentFirstResponder, responder is UITextInput else { return false }
119
+ return !((responder as? UIView)?.isDescendant(of: self) ?? false)
120
+ }
121
+
122
+ /// Becomes first responder in book mode while on screen, so a pedal or keyboard turns the page
123
+ /// without the host app doing anything. Never takes focus from a text input, and leaves it
124
+ /// alone when it's already with this view or PDFKit's views inside it.
125
+ func claimBookKeyFocus() {
126
+ guard isBookMode, window != nil, !isTextInputActive else { return }
127
+ if let current = UIResponder.currentFirstResponder as? UIView, current.isDescendant(of: self) {
128
+ return
129
+ }
130
+ becomeFirstResponder()
131
+ }
132
+
133
+ @objc func handleTextEditingEnded() {
134
+ // Wait for the responder change to settle before taking the keys back.
135
+ DispatchQueue.main.async { [weak self] in
136
+ self?.claimBookKeyFocus()
137
+ }
138
+ }
139
+
140
+ // MARK: - Turn path
141
+
142
+ /// The single entry point for every book mode turn: swipes, edge taps and hardware keys.
143
+ /// Applies the turn policy, then either slides to the neighbouring page (one `onPageChanged`)
144
+ /// or bounces at the first and last page.
145
+ ///
146
+ /// `dragOffset` is how far a swipe has already dragged the page, so the slide carries on from there.
147
+ @discardableResult
148
+ func turnBookPage(_ direction: BookTurnDirection, from input: BookTurnInput, dragOffset: CGFloat = 0) -> Bool {
149
+ guard allowsBookTurn(from: input) else {
150
+ springBookPageBack()
151
+ return false
152
+ }
153
+ guard let target = bookNeighbour(direction) else {
154
+ if dragOffset == 0 {
155
+ animateBookBounce(direction)
156
+ } else {
157
+ springBookPageBack()
158
+ }
159
+ return false
160
+ }
161
+ showBookPage(target, direction: direction, dragOffset: dragOffset)
162
+ return true
163
+ }
164
+
165
+ /// Spec turn policy: touch input turns only with no markup tool active and the page at fit.
166
+ /// Keys turn in book mode regardless.
167
+ func allowsBookTurn(from input: BookTurnInput) -> Bool {
168
+ guard isBookMode else { return false }
169
+ switch input {
170
+ case .key:
171
+ return true
172
+ case .swipe, .edgeTap:
173
+ return currentMode == "none" && !isZoomedBeyondFit
174
+ }
175
+ }
176
+
177
+ var isZoomedBeyondFit: Bool {
178
+ pdfView.scaleFactor > pdfView.scaleFactorForSizeToFit * (1 + BookMode.fitTolerance)
179
+ }
180
+
181
+ func bookNeighbour(_ direction: BookTurnDirection) -> PDFPage? {
182
+ guard let document = pdfView.document, let current = pdfView.currentPage else { return nil }
183
+ let index = document.index(for: current) + direction.step
184
+ guard index >= 0, index < document.pageCount else { return nil }
185
+ return document.page(at: index)
186
+ }
187
+
188
+ private func showBookPage(_ page: PDFPage, direction: BookTurnDirection, dragOffset: CGFloat = 0) {
189
+ let width = max(pdfView.bounds.width, 1)
190
+ let outgoing = pdfView.snapshotView(afterScreenUpdates: false)
191
+ finishBookAnimation()
192
+
193
+ pdfView.transform = .identity
194
+ pdfView.go(to: page)
195
+ resetBookPageToFit()
196
+
197
+ guard window != nil else { return }
198
+
199
+ if let outgoing {
200
+ outgoing.frame = pdfView.frame.offsetBy(dx: dragOffset, dy: 0)
201
+ addSubview(outgoing)
202
+ bookTurnSnapshot = outgoing
203
+ }
204
+ // Next slides in from the right, previous from the left.
205
+ let incomingStart = direction == .next ? width + dragOffset : -width + dragOffset
206
+ pdfView.transform = CGAffineTransform(translationX: incomingStart, y: 0)
207
+ UIView.animate(withDuration: BookMode.turnDuration, delay: 0, options: [.curveEaseOut, .allowUserInteraction]) {
208
+ self.pdfView.transform = .identity
209
+ outgoing?.transform = CGAffineTransform(translationX: direction == .next ? -width : width, y: 0)
210
+ } completion: { _ in
211
+ outgoing?.removeFromSuperview()
212
+ if self.bookTurnSnapshot === outgoing {
213
+ self.bookTurnSnapshot = nil
214
+ }
215
+ }
216
+ }
217
+
218
+ /// Jumps straight to `page` with no slide, as the `page` prop does, and starts it at fit.
219
+ /// `go(to:)` fires the one `onPageChanged`.
220
+ func jumpToBookPage(_ page: PDFPage) {
221
+ finishBookAnimation()
222
+ pdfView.transform = .identity
223
+ pdfView.go(to: page)
224
+ resetBookPageToFit()
225
+ }
226
+
227
+ private func resetBookPageToFit() {
228
+ pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
229
+ pdfView.autoScales = true
230
+ }
231
+
232
+ private func animateBookBounce(_ direction: BookTurnDirection) {
233
+ guard window != nil else { return }
234
+ let nudge = direction == .next ? -BookMode.bouncePoints : BookMode.bouncePoints
235
+ UIView.animate(withDuration: BookMode.bounceDuration, delay: 0, options: [.curveEaseOut, .allowUserInteraction]) {
236
+ self.pdfView.transform = CGAffineTransform(translationX: nudge, y: 0)
237
+ } completion: { _ in
238
+ self.springBookPageBack()
239
+ }
240
+ }
241
+
242
+ func springBookPageBack() {
243
+ UIView.animate(
244
+ withDuration: 0.3,
245
+ delay: 0,
246
+ usingSpringWithDamping: 0.7,
247
+ initialSpringVelocity: 0,
248
+ options: [.allowUserInteraction]
249
+ ) {
250
+ self.pdfView.transform = .identity
251
+ }
252
+ }
253
+
254
+ private func finishBookAnimation() {
255
+ pdfView.layer.removeAllAnimations()
256
+ bookTurnSnapshot?.removeFromSuperview()
257
+ bookTurnSnapshot = nil
258
+ }
259
+
260
+ // MARK: - Edge taps
261
+
262
+ /// Width of each edge tap zone: the outer quarter of the view, at least 44 pt.
263
+ var bookEdgeZoneWidth: CGFloat {
264
+ min(max(bounds.width * BookMode.edgeZoneFraction, BookMode.minEdgeZone), bounds.width / 2)
265
+ }
266
+
267
+ /// The turn a tap at `location` (in this view's coordinates) asks for: one in an edge zone while
268
+ /// the turn policy allows edge taps. `nil` in the middle, where a double-tap zooms instead.
269
+ func bookEdgeTurn(at location: CGPoint) -> BookTurnDirection? {
270
+ guard allowsBookTurn(from: .edgeTap) else { return nil }
271
+ let zone = bookEdgeZoneWidth
272
+ if location.x <= zone {
273
+ return .previous
274
+ }
275
+ if location.x >= bounds.width - zone {
276
+ return .next
277
+ }
278
+ return nil
279
+ }
280
+
281
+ /// Turns the page for a tap at `location` if it lands in an edge zone. `tapCount` is the
282
+ /// touch's tap count: the second tap of a double-tap is skipped, so a double-tap on an edge
283
+ /// turns once (as on Android, a third quick tap turns again).
284
+ func handleBookTap(at location: CGPoint, tapCount: Int = 1) {
285
+ guard tapCount % 2 == 1, let direction = bookEdgeTurn(at: location) else { return }
286
+ turnBookPage(direction, from: .edgeTap)
287
+ }
288
+
289
+ @objc func handleBookTapGesture(_ gesture: UITapGestureRecognizer) {
290
+ guard gesture.state == .ended else { return }
291
+ claimBookKeyFocus()
292
+ handleBookTap(at: gesture.location(in: self), tapCount: bookTapCount)
293
+ }
294
+
295
+ /// A double-tap on an edge: recognised only so that PDFKit's double-tap zoom, which waits for
296
+ /// it to fail, doesn't zoom there. The first tap has already turned the page.
297
+ @objc func handleBookEdgeDoubleTapGesture(_: UITapGestureRecognizer) {}
298
+
299
+ // MARK: - Swipes
300
+
301
+ @objc func handleBookPanGesture(_ gesture: UIPanGestureRecognizer) {
302
+ let translationX = gesture.translation(in: self).x
303
+ switch gesture.state {
304
+ case .changed:
305
+ // Follow the finger, with resistance where there is no page to turn to.
306
+ let direction: BookTurnDirection = translationX < 0 ? .next : .previous
307
+ let offset = bookNeighbour(direction) == nil ? translationX * BookMode.edgeResistance : translationX
308
+ pdfView.transform = CGAffineTransform(translationX: offset, y: 0)
309
+ case .ended:
310
+ endBookSwipe(translationX: translationX, velocityX: gesture.velocity(in: self).x)
311
+ case .cancelled, .failed:
312
+ springBookPageBack()
313
+ default:
314
+ break
315
+ }
316
+ }
317
+
318
+ /// Ends a horizontal swipe: turns when it went far enough (a quarter of the width) or was
319
+ /// flicked, otherwise springs back. Swipe left always means next.
320
+ func endBookSwipe(translationX: CGFloat, velocityX: CGFloat) {
321
+ let reference = translationX != 0 ? translationX : velocityX
322
+ guard reference != 0 else {
323
+ springBookPageBack()
324
+ return
325
+ }
326
+ let direction: BookTurnDirection = reference < 0 ? .next : .previous
327
+ let farEnough = abs(translationX) > bounds.width * BookMode.swipeTurnFraction
328
+ let flicked = abs(velocityX) > BookMode.flingVelocity && (velocityX < 0) == (reference < 0)
329
+ guard farEnough || flicked else {
330
+ springBookPageBack()
331
+ return
332
+ }
333
+ turnBookPage(direction, from: .swipe, dragOffset: pdfView.transform.tx)
334
+ }
335
+
336
+ /// Whether a book mode gesture may start. A pan starts only when it's mostly horizontal and the
337
+ /// turn policy allows a swipe, so while zoomed in the scroll view pans instead.
338
+ func shouldBeginBookGesture(_ gesture: UIGestureRecognizer) -> Bool {
339
+ if gesture === bookPanGesture, let pan = gesture as? UIPanGestureRecognizer {
340
+ let velocity = pan.velocity(in: self)
341
+ return allowsBookTurn(from: .swipe) && abs(velocity.x) > abs(velocity.y)
342
+ }
343
+ if gesture === bookTapGesture {
344
+ return allowsBookTurn(from: .edgeTap)
345
+ }
346
+ if gesture === bookEdgeDoubleTapGesture {
347
+ return bookEdgeTurn(at: gesture.location(in: self)) != nil
348
+ }
349
+ return true
350
+ }
351
+
352
+ /// Whether `other` has to wait for `gesture` to fail: PDFKit's own double-tap zoom waits for
353
+ /// the edge double-tap, so a double-tap zooms only in the middle region.
354
+ func bookGesture(_ gesture: UIGestureRecognizer, isRequiredToFailBy other: UIGestureRecognizer) -> Bool {
355
+ guard gesture === bookEdgeDoubleTapGesture, other !== gesture,
356
+ let tap = other as? UITapGestureRecognizer else { return false }
357
+ return tap.numberOfTapsRequired >= 2
358
+ }
359
+
360
+ // MARK: - Gestures
361
+
362
+ private func installBookGestures() {
363
+ guard bookTapGesture == nil else { return }
364
+ let delegate = BookGestureDelegate(view: self)
365
+ bookGestureDelegate = delegate
366
+
367
+ let tap = UITapGestureRecognizer(target: self, action: #selector(handleBookTapGesture(_:)))
368
+ tap.delegate = delegate
369
+ pdfView.addGestureRecognizer(tap)
370
+ bookTapGesture = tap
371
+
372
+ let edgeDoubleTap = UITapGestureRecognizer(
373
+ target: self,
374
+ action: #selector(handleBookEdgeDoubleTapGesture(_:))
375
+ )
376
+ edgeDoubleTap.numberOfTapsRequired = 2
377
+ edgeDoubleTap.delegate = delegate
378
+ pdfView.addGestureRecognizer(edgeDoubleTap)
379
+ bookEdgeDoubleTapGesture = edgeDoubleTap
380
+
381
+ let pan = UIPanGestureRecognizer(target: self, action: #selector(handleBookPanGesture(_:)))
382
+ pan.maximumNumberOfTouches = 1
383
+ pan.delegate = delegate
384
+ pdfView.addGestureRecognizer(pan)
385
+ bookPanGesture = pan
386
+ }
387
+
388
+ private func removeBookGestures() {
389
+ finishBookAnimation()
390
+ pdfView.transform = .identity
391
+ if let tap = bookTapGesture {
392
+ pdfView.removeGestureRecognizer(tap)
393
+ bookTapGesture = nil
394
+ }
395
+ if let pan = bookPanGesture {
396
+ pdfView.removeGestureRecognizer(pan)
397
+ bookPanGesture = nil
398
+ }
399
+ if let edgeDoubleTap = bookEdgeDoubleTapGesture {
400
+ pdfView.removeGestureRecognizer(edgeDoubleTap)
401
+ bookEdgeDoubleTapGesture = nil
402
+ }
403
+ bookGestureDelegate = nil
404
+ }
405
+ }
406
+
407
+ /// Gates the book mode tap and pan with the turn policy, and lets them run alongside PDFKit's
408
+ /// own gestures. Kept apart from the view's delegate so the markup gestures are unaffected.
409
+ final class BookGestureDelegate: NSObject, UIGestureRecognizerDelegate {
410
+ private weak var view: ExpoPdfMarkupView?
411
+
412
+ init(view: ExpoPdfMarkupView) {
413
+ self.view = view
414
+ }
415
+
416
+ func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
417
+ view?.shouldBeginBookGesture(gestureRecognizer) ?? false
418
+ }
419
+
420
+ func gestureRecognizer(
421
+ _: UIGestureRecognizer,
422
+ shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
423
+ ) -> Bool {
424
+ true
425
+ }
426
+
427
+ func gestureRecognizer(
428
+ _ gestureRecognizer: UIGestureRecognizer,
429
+ shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer
430
+ ) -> Bool {
431
+ view?.bookGesture(gestureRecognizer, isRequiredToFailBy: otherGestureRecognizer) ?? false
432
+ }
433
+
434
+ func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
435
+ if let view, gestureRecognizer === view.bookTapGesture {
436
+ view.bookTapCount = touch.tapCount
437
+ }
438
+ return true
439
+ }
440
+ }
441
+
442
+ extension UIResponder {
443
+ @MainActor private weak static var probedFirstResponder: UIResponder?
444
+
445
+ /// The app's current first responder, if any, found by sending an action down the responder chain.
446
+ @MainActor static var currentFirstResponder: UIResponder? {
447
+ probedFirstResponder = nil
448
+ UIApplication.shared.sendAction(#selector(probeFirstResponder), to: nil, from: nil, for: nil)
449
+ return probedFirstResponder
450
+ }
451
+
452
+ @objc private func probeFirstResponder() {
453
+ UIResponder.probedFirstResponder = self
454
+ }
455
+ }
@@ -94,14 +94,22 @@ extension ExpoPdfMarkupView {
94
94
  // and tight bounds calculations are fragile with coordinate transforms.
95
95
  let annotationBounds = page.bounds(for: pdfView.displayBox)
96
96
 
97
- let annotation = InkPDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
97
+ let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
98
98
  annotation.color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
99
99
 
100
100
  let border = PDFBorder()
101
101
  border.lineWidth = annotationLineWidth
102
102
  annotation.border = border
103
103
 
104
- annotation.strokes = [pdfPoints]
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)
105
113
 
106
114
  let id = UUID().uuidString
107
115
  AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
@@ -166,11 +166,10 @@ extension ExpoPdfMarkupView {
166
166
  in documentView: UIView
167
167
  ) -> CGRect? {
168
168
  let pdfRect: CGRect
169
- if let inkAnnotation = annotation as? InkPDFAnnotation, !inkAnnotation.strokes.isEmpty {
169
+ if let paths = annotation.paths, !paths.isEmpty {
170
170
  var combinedBounds = CGRect.null
171
- for stroke in inkAnnotation.strokes {
172
- guard let cgPath = InkPDFAnnotation.cgPath(for: stroke) else { continue }
173
- combinedBounds = combinedBounds.union(cgPath.boundingBoxOfPath)
171
+ for path in paths {
172
+ combinedBounds = combinedBounds.union(path.cgPath.boundingBoxOfPath)
174
173
  }
175
174
  guard !combinedBounds.isNull else { return nil }
176
175
  let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
@@ -33,6 +33,13 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
33
33
  var editingOverlayLayer: CAShapeLayer?
34
34
  var textTapGesture: UITapGestureRecognizer?
35
35
  var stampTapGesture: UITapGestureRecognizer?
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?
36
43
  private var selectionObserver: NSObjectProtocol?
37
44
  private var selectionDebounceTimer: Timer?
38
45
  private var currentMarkupType: String?
@@ -72,6 +79,10 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
72
79
  name: .PDFViewPageChanged,
73
80
  object: pdfView
74
81
  )
82
+ // When a text input anywhere lets go of the keyboard, book mode takes the keys back.
83
+ for name in [UITextField.textDidEndEditingNotification, UITextView.textDidEndEditingNotification] {
84
+ NotificationCenter.default.addObserver(self, selector: #selector(handleTextEditingEnded), name: name, object: nil)
85
+ }
75
86
  }
76
87
 
77
88
  deinit {
@@ -83,12 +94,29 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
83
94
 
84
95
  override func layoutSubviews() {
85
96
  super.layoutSubviews()
86
- pdfView.frame = bounds
97
+ // Bounds and centre rather than frame: book mode slides pdfView with a transform.
98
+ pdfView.bounds = CGRect(origin: .zero, size: bounds.size)
99
+ pdfView.center = CGPoint(x: bounds.midX, y: bounds.midY)
87
100
  inkShapeLayer?.frame = bounds
88
101
  editingOverlayLayer?.frame = pdfView.documentView?.bounds ?? .zero
89
102
  updateEditingOverlay()
90
103
  }
91
104
 
105
+ override func didMoveToWindow() {
106
+ super.didMoveToWindow()
107
+ claimBookKeyFocus()
108
+ }
109
+
110
+ // MARK: - Hardware keys (book mode only, see ExpoPdfMarkupView+Book.swift)
111
+
112
+ override var canBecomeFirstResponder: Bool {
113
+ isBookMode
114
+ }
115
+
116
+ override var keyCommands: [UIKeyCommand]? {
117
+ isBookMode ? bookKeyCommands : nil
118
+ }
119
+
92
120
  // MARK: - Props
93
121
 
94
122
  func setBackgroundColor(_ color: UIColor?) {
@@ -127,7 +155,11 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
127
155
  return
128
156
  }
129
157
 
130
- pdfView.go(to: page)
158
+ if isBookMode {
159
+ jumpToBookPage(page)
160
+ } else {
161
+ pdfView.go(to: page)
162
+ }
131
163
  }
132
164
 
133
165
  // MARK: - Annotation management
@@ -200,6 +232,18 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
200
232
  updateEditingOverlay()
201
233
  }
202
234
 
235
+ // MARK: - Display mode
236
+
237
+ /// `"continuous"` (default) or `"book"`. See `ExpoPdfMarkupView+Book.swift`.
238
+ private(set) var displayMode: String = "continuous"
239
+
240
+ func setDisplayMode(_ mode: String) {
241
+ let normalised = mode == "book" ? "book" : "continuous"
242
+ guard normalised != displayMode else { return }
243
+ displayMode = normalised
244
+ applyDisplayMode()
245
+ }
246
+
203
247
  // MARK: - Annotation mode
204
248
 
205
249
  func setAnnotationMode(_ mode: String) {
@@ -272,11 +316,10 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
272
316
  for annotation in page.annotations.reversed() {
273
317
  guard AnnotationSerializer.isModuleManaged(annotation) else { continue }
274
318
 
275
- if let inkAnnotation = annotation as? InkPDFAnnotation {
319
+ if let paths = annotation.paths {
276
320
  let tolerance = max(annotation.border?.lineWidth ?? 2.0, 10.0)
277
- for stroke in inkAnnotation.strokes {
278
- guard let cgPath = InkPDFAnnotation.cgPath(for: stroke) else { continue }
279
- let hitPath = cgPath.copy(
321
+ for path in paths {
322
+ let hitPath = path.cgPath.copy(
280
323
  strokingWithWidth: tolerance,
281
324
  lineCap: .round,
282
325
  lineJoin: .round,
@@ -174,28 +174,6 @@ final class AnnotationSerializerTests: XCTestCase {
174
174
  )
175
175
  }
176
176
 
177
- func testInkAnnotationStoresStrokesOnCustomSubclass() throws {
178
- let model = AnnotationModel(
179
- id: "ink-strokes-1",
180
- type: "ink",
181
- page: 0,
182
- color: "#00FF00",
183
- lineWidth: 4.0,
184
- paths: [[["x": 1, "y": 2], ["x": 3, "y": 4]], [["x": 5, "y": 6]]],
185
- createdAt: 1_741_340_000
186
- )
187
-
188
- let annotation = try XCTUnwrap(AnnotationSerializer.toPDFAnnotation(model))
189
- let inkAnnotation = try XCTUnwrap(annotation as? InkPDFAnnotation)
190
-
191
- // Regression coverage: on iOS 27 beta, PDFAnnotation.paths silently drops stroke
192
- // geometry added via .add(_:), so ink annotations must not depend on it (report/).
193
- XCTAssertEqual(
194
- inkAnnotation.strokes,
195
- [[CGPoint(x: 1, y: 2), CGPoint(x: 3, y: 4)], [CGPoint(x: 5, y: 6)]]
196
- )
197
- }
198
-
199
177
  func testFreeTextColorRoundTrip() throws {
200
178
  let model = AnnotationModel(
201
179
  id: "ft-color-1",