@tobyt/expo-pdf-markup 1.1.4 → 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 (39) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookPager.kt +66 -0
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/BookTurnPolicy.kt +117 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +370 -57
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +96 -5
  7. package/build/ExpoPdfMarkup.types.d.ts +12 -0
  8. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  9. package/build/ExpoPdfMarkup.types.js.map +1 -1
  10. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  11. package/build/ExpoPdfMarkupView.web.js +369 -44
  12. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  13. package/build/web/bookMode.d.ts +77 -0
  14. package/build/web/bookMode.d.ts.map +1 -0
  15. package/build/web/bookMode.js +169 -0
  16. package/build/web/bookMode.js.map +1 -0
  17. package/build/web/layout.d.ts +8 -0
  18. package/build/web/layout.d.ts.map +1 -0
  19. package/build/web/layout.js +22 -0
  20. package/build/web/layout.js.map +1 -0
  21. package/build/web/types.d.ts +2 -0
  22. package/build/web/types.d.ts.map +1 -1
  23. package/build/web/types.js.map +1 -1
  24. package/docs/agents/domain.md +51 -0
  25. package/docs/agents/issue-tracker.md +46 -0
  26. package/docs/agents/triage-labels.md +15 -0
  27. package/ios/ExpoPdfMarkupModule.swift +4 -0
  28. package/ios/ExpoPdfMarkupView+Book.swift +455 -0
  29. package/ios/ExpoPdfMarkupView.swift +46 -2
  30. package/ios/Tests/BookModeTests.swift +534 -0
  31. package/ios/Tests/book-sample.pdf +207 -0
  32. package/ios/Tests/single-page.pdf +52 -0
  33. package/package.json +3 -2
  34. package/src/ExpoPdfMarkup.types.ts +13 -0
  35. package/src/ExpoPdfMarkupView.web.tsx +443 -52
  36. package/src/web/bookMode.ts +186 -0
  37. package/src/web/layout.ts +30 -0
  38. package/src/web/types.ts +3 -0
  39. package/tsconfig.test.json +10 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @tobyt/expo-pdf-markup
2
2
 
3
+ ## 1.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 055cf02: Add book mode. `ExpoPdfMarkupView` takes a new `displayMode` prop, `'continuous'` (the default) or `'book'`. Book mode shows one page at a time, fitted to the view, and turns pages sideways with a slide animation: swipe horizontally, tap the outer quarter of the view on either side, or press the arrow or Page Up / Page Down keys, which is what Bluetooth page-turner pedals send. Pinch to zoom still works, and while the page is zoomed in a swipe pans instead of turning. Markup tools keep working in book mode and take priority over swipes and taps, while hardware keys still turn the page. `page` and `onPageChanged` behave the same in both modes, and changing `displayMode` or rotating keeps the current page. Leaving `displayMode` out keeps today's continuous behaviour. The library adds no user-facing text: page indicators and announcements are up to your app. Book mode needs a new native build.
8
+
3
9
  ## 1.1.4
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,66 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ /**
4
+ * Which page book mode shows, and how a turn moves between pages. Pure Kotlin
5
+ * so the turn rules can be tested in the JVM; the view animates whatever
6
+ * [turn] returns and calls [finishSlide] when the slide has landed.
7
+ */
8
+ class BookPager(private val onPageChanged: (page: Int) -> Unit) {
9
+ sealed interface Turn {
10
+ data class Slide(val from: Int, val to: Int, val direction: BookTurnPolicy.Direction) : Turn
11
+
12
+ /** No page that way: nudge towards [direction] and spring back. Nothing wraps round. */
13
+ data class Bounce(val direction: BookTurnPolicy.Direction) : Turn
14
+ }
15
+
16
+ var pageCount = 0
17
+ private set
18
+ var currentPage = 0
19
+ private set
20
+
21
+ private var inFlight: Turn.Slide? = null
22
+
23
+ /** True while a slide has started but not landed. */
24
+ val isSliding: Boolean get() = inFlight != null
25
+
26
+ /**
27
+ * Sets the document and page without reporting a change, clamping [page]
28
+ * into the document. Used on load, mode switch, rotation and resize.
29
+ */
30
+ fun reset(pageCount: Int, page: Int) {
31
+ inFlight = null
32
+ this.pageCount = maxOf(0, pageCount)
33
+ currentPage = page.coerceIn(0, maxOf(0, this.pageCount - 1))
34
+ }
35
+
36
+ /**
37
+ * Starts a turn. A slide already in flight lands first, so rapid turns
38
+ * each count and each report once. Returns [Turn.Bounce] at either end.
39
+ */
40
+ fun turn(direction: BookTurnPolicy.Direction): Turn {
41
+ finishSlide()
42
+ val target = currentPage + direction.delta
43
+ if (target !in 0 until pageCount) return Turn.Bounce(direction)
44
+ val slide = Turn.Slide(currentPage, target, direction)
45
+ inFlight = slide
46
+ return slide
47
+ }
48
+
49
+ /** Shows [page] straight away (the `page` prop), cancelling any slide. */
50
+ fun jumpTo(page: Int): Boolean {
51
+ if (page !in 0 until pageCount) return false
52
+ inFlight = null
53
+ if (page == currentPage) return false
54
+ currentPage = page
55
+ onPageChanged(page)
56
+ return true
57
+ }
58
+
59
+ /** Lands the slide in flight, if any, and reports the new page. */
60
+ fun finishSlide() {
61
+ val slide = inFlight ?: return
62
+ inFlight = null
63
+ currentPage = slide.to
64
+ onPageChanged(currentPage)
65
+ }
66
+ }
@@ -0,0 +1,117 @@
1
+ package dev.terhoeven.expopdfmarkup
2
+
3
+ import android.view.KeyEvent
4
+
5
+ /**
6
+ * Turn decisions for book mode. Every caller is book mode only, so nothing
7
+ * here takes the display mode.
8
+ */
9
+ object BookTurnPolicy {
10
+ /** Scales within this of fit (1.0) count as "at fit", absorbing pinch rounding. */
11
+ private const val FIT_TOLERANCE = 0.01f
12
+
13
+ private const val EDGE_ZONE_FRACTION = 0.25f
14
+ private const val MIN_EDGE_ZONE_DP = 44f
15
+
16
+ /** Share of the view's width a slow swipe has to travel to turn the page. */
17
+ private const val SWIPE_TURN_FRACTION = 0.25f
18
+
19
+ enum class SwipeAction { TURN, PAN }
20
+
21
+ /** Direction of a page turn. [delta] is the change in page index. */
22
+ enum class Direction(val delta: Int) { NEXT(1), PREVIOUS(-1) }
23
+
24
+ /**
25
+ * Width of each edge tap zone: the outer quarter of the view, at least
26
+ * 44 dp, but never more than half the view so the zones can't overlap.
27
+ */
28
+ fun edgeZoneWidth(viewWidth: Float, density: Float): Float =
29
+ maxOf(viewWidth * EDGE_ZONE_FRACTION, MIN_EDGE_ZONE_DP * density)
30
+ .coerceAtMost(viewWidth / 2f)
31
+
32
+ /** The turn an edge tap at [x] asks for, or null for no turn. */
33
+ fun edgeTapTurnFor(
34
+ annotationMode: String,
35
+ scale: Float,
36
+ x: Float,
37
+ viewWidth: Float,
38
+ density: Float
39
+ ): Direction? {
40
+ if (!touchCanTurn(annotationMode, scale) || viewWidth <= 0f) return null
41
+ val zone = edgeZoneWidth(viewWidth, density)
42
+ return when {
43
+ x < zone -> Direction.PREVIOUS
44
+ x > viewWidth - zone -> Direction.NEXT
45
+ else -> null
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Whether a double-tap at [x] zooms. Where an edge tap would turn, the
51
+ * first tap of the pair has already turned the page, so the second is
52
+ * swallowed: a double-tap zooms only in the middle region, or anywhere
53
+ * while edge taps can't turn (zoomed in or with a markup tool active).
54
+ */
55
+ fun doubleTapZooms(
56
+ annotationMode: String,
57
+ scale: Float,
58
+ x: Float,
59
+ viewWidth: Float,
60
+ density: Float
61
+ ): Boolean = edgeTapTurnFor(annotationMode, scale, x, viewWidth, density) == null
62
+
63
+ /**
64
+ * Swipes and edge taps turn the page only with no markup tool active,
65
+ * while the page is not zoomed in beyond fit (scale 1).
66
+ */
67
+ fun touchCanTurn(annotationMode: String, scale: Float): Boolean =
68
+ annotationMode == "none" && scale <= 1f + FIT_TOLERANCE
69
+
70
+ /**
71
+ * The turn a released horizontal swipe asks for, or null to settle back.
72
+ * [dragX] is the finger's total horizontal travel (negative = leftwards).
73
+ * Leftwards always means next, whatever the layout direction. A fast
74
+ * fling turns in its own direction unless it flicks back against the
75
+ * drag; a slow release turns once the drag passes a quarter of the view.
76
+ */
77
+ fun swipeReleaseTurnFor(
78
+ dragX: Float,
79
+ velocityX: Float,
80
+ viewWidth: Float,
81
+ flingVelocity: Float
82
+ ): Direction? {
83
+ val fromDrag = when {
84
+ dragX <= -viewWidth * SWIPE_TURN_FRACTION -> Direction.NEXT
85
+ dragX >= viewWidth * SWIPE_TURN_FRACTION -> Direction.PREVIOUS
86
+ else -> null
87
+ }
88
+ if (kotlin.math.abs(velocityX) < flingVelocity) return fromDrag
89
+ val fromFling = if (velocityX < 0f) Direction.NEXT else Direction.PREVIOUS
90
+ val dragOpposes = (fromFling == Direction.NEXT && dragX > 0f) ||
91
+ (fromFling == Direction.PREVIOUS && dragX < 0f)
92
+ return if (dragOpposes) null else fromFling
93
+ }
94
+
95
+ /**
96
+ * The turn a hardware key (keyboard or page-turner pedal) asks for, or
97
+ * null to leave the key alone. Right, Down and Page Down go forward; Left,
98
+ * Up and Page Up go back; Space isn't mapped. Keys turn whatever the zoom
99
+ * or markup mode, but never while a text input has focus, and never with
100
+ * a modifier held.
101
+ */
102
+ fun keyTurnFor(keyCode: Int, textInputHasFocus: Boolean, hasModifiers: Boolean): Direction? {
103
+ if (textInputHasFocus || hasModifiers) return null
104
+ return when (keyCode) {
105
+ KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_PAGE_DOWN ->
106
+ Direction.NEXT
107
+
108
+ KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_PAGE_UP ->
109
+ Direction.PREVIOUS
110
+
111
+ else -> null
112
+ }
113
+ }
114
+
115
+ fun swipeActionFor(annotationMode: String, scale: Float): SwipeAction =
116
+ if (touchCanTurn(annotationMode, scale)) SwipeAction.TURN else SwipeAction.PAN
117
+ }