@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.
- package/CHANGELOG.md +6 -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 +12 -0
- 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 +369 -44
- 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/ios/ExpoPdfMarkupModule.swift +4 -0
- package/ios/ExpoPdfMarkupView+Book.swift +455 -0
- package/ios/ExpoPdfMarkupView.swift +46 -2
- package/ios/Tests/BookModeTests.swift +534 -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 +13 -0
- package/src/ExpoPdfMarkupView.web.tsx +443 -52
- 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
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { AnnotationMode } from '../ExpoPdfMarkup.types';
|
|
2
|
+
|
|
3
|
+
/** Which way a turn goes. `'next'` is always forward in the document, whatever the layout direction. */
|
|
4
|
+
export type TurnDirection = 'next' | 'previous';
|
|
5
|
+
|
|
6
|
+
/** Minimum width of an edge tap zone, in CSS px. */
|
|
7
|
+
export const MIN_EDGE_ZONE_PX = 44;
|
|
8
|
+
/** Share of the view's width that each edge tap zone covers. */
|
|
9
|
+
export const EDGE_ZONE_FRACTION = 0.25;
|
|
10
|
+
/** Pointer travel (CSS px) below which a press counts as a tap rather than a drag. */
|
|
11
|
+
export const TAP_SLOP_PX = 10;
|
|
12
|
+
/** Share of the view's width a slow swipe has to travel to turn the page, as on iOS and Android. */
|
|
13
|
+
export const SWIPE_TURN_FRACTION = 0.25;
|
|
14
|
+
/**
|
|
15
|
+
* A release faster than this (CSS px per ms) turns the page even after a short drag. It's the
|
|
16
|
+
* 500 pt/s (iOS) and 500 dp/s (Android) fling speed.
|
|
17
|
+
*/
|
|
18
|
+
export const FLING_VELOCITY_PX_PER_MS = 0.5;
|
|
19
|
+
/** How far back (ms) the pointer's movement counts towards its release speed. */
|
|
20
|
+
export const VELOCITY_WINDOW_MS = 100;
|
|
21
|
+
/** How much of the drag the page follows past the first or last page. */
|
|
22
|
+
export const EDGE_RESISTANCE = 0.3;
|
|
23
|
+
/** How far (CSS px) the page nudges when a tap or key bounces at either end. */
|
|
24
|
+
export const BOUNCE_PX = 40;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Turn policy for book mode swipes and edge taps: they turn the page only with no markup tool
|
|
28
|
+
* active, and when the page isn't zoomed in beyond fit. Hardware keys don't go through this.
|
|
29
|
+
*/
|
|
30
|
+
export function canGestureTurn(annotationMode: AnnotationMode, zoom: number): boolean {
|
|
31
|
+
return annotationMode === 'none' && zoom <= 1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const KEY_TURNS: Record<string, TurnDirection> = {
|
|
35
|
+
ArrowRight: 'next',
|
|
36
|
+
ArrowDown: 'next',
|
|
37
|
+
PageDown: 'next',
|
|
38
|
+
ArrowLeft: 'previous',
|
|
39
|
+
ArrowUp: 'previous',
|
|
40
|
+
PageUp: 'previous',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The turn a hardware key asks for in book mode, or `null` for a key that doesn't turn.
|
|
45
|
+
* Right, Down and Page Down go forward; Left, Up and Page Up go back. Space isn't mapped.
|
|
46
|
+
* Keys turn whatever the zoom or markup mode, so they don't go through {@link canGestureTurn}.
|
|
47
|
+
*/
|
|
48
|
+
export function keyTurnFor(key: string): TurnDirection | null {
|
|
49
|
+
return KEY_TURNS[key] ?? null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* `<input>` types that use neither typed text nor arrow keys, so arrow keys there can still turn
|
|
54
|
+
* the page. Range and radio inputs aren't here: arrows move a slider or pick the next radio.
|
|
55
|
+
*/
|
|
56
|
+
const KEYLESS_INPUT_TYPES = new Set([
|
|
57
|
+
'button',
|
|
58
|
+
'checkbox',
|
|
59
|
+
'color',
|
|
60
|
+
'file',
|
|
61
|
+
'hidden',
|
|
62
|
+
'image',
|
|
63
|
+
'reset',
|
|
64
|
+
'submit',
|
|
65
|
+
]);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* ARIA roles of widgets that move with arrow keys. A key pressed on one, or on an item inside
|
|
69
|
+
* one (a tab in a tablist, an option in a listbox), belongs to it.
|
|
70
|
+
*/
|
|
71
|
+
const ARROW_KEY_ROLES = [
|
|
72
|
+
'combobox',
|
|
73
|
+
'grid',
|
|
74
|
+
'listbox',
|
|
75
|
+
'menu',
|
|
76
|
+
'menubar',
|
|
77
|
+
'radiogroup',
|
|
78
|
+
'slider',
|
|
79
|
+
'spinbutton',
|
|
80
|
+
'tablist',
|
|
81
|
+
'tree',
|
|
82
|
+
'treegrid',
|
|
83
|
+
];
|
|
84
|
+
const ARROW_KEY_WIDGET = ARROW_KEY_ROLES.map((role) => '[role~="' + role + '"]').join(',');
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Whether keys pressed at `el` belong to it, never to the page turn: a text `<input>`, a
|
|
88
|
+
* `<textarea>`, anything contenteditable, or a control that uses arrow keys (a `<select>`, a
|
|
89
|
+
* range or radio input, or an ARIA widget such as a slider, radiogroup, listbox, menu or tablist).
|
|
90
|
+
*/
|
|
91
|
+
export function ownsArrowKeys(el: Element | null): boolean {
|
|
92
|
+
if (!el) return false;
|
|
93
|
+
const tag = el.tagName;
|
|
94
|
+
if (tag === 'TEXTAREA' || tag === 'SELECT') return true;
|
|
95
|
+
if (tag === 'INPUT') return !KEYLESS_INPUT_TYPES.has((el as HTMLInputElement).type);
|
|
96
|
+
if (el.closest(ARROW_KEY_WIDGET) != null) return true;
|
|
97
|
+
return el.closest('[contenteditable]:not([contenteditable="false"])') != null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Whether the view is on screen, so keys may turn its page: it has a size, and neither it nor
|
|
102
|
+
* an ancestor is hidden, `display: none` or `visibility: hidden` (as on a screen kept mounted in
|
|
103
|
+
* a navigation back stack).
|
|
104
|
+
*/
|
|
105
|
+
export function isViewShown(el: Element | null, width: number, height: number): boolean {
|
|
106
|
+
if (!el?.isConnected || width <= 0 || height <= 0) return false;
|
|
107
|
+
if (typeof el.checkVisibility === 'function') {
|
|
108
|
+
return el.checkVisibility({ visibilityProperty: true });
|
|
109
|
+
}
|
|
110
|
+
for (let node: Element | null = el; node; node = node.parentElement) {
|
|
111
|
+
if (node.hasAttribute('hidden')) return false;
|
|
112
|
+
const style = getComputedStyle(node);
|
|
113
|
+
if (style.display === 'none' || style.visibility === 'hidden') return false;
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Where the page strip moves to slide `distance` px in `direction`: left (negative) for next,
|
|
120
|
+
* right for previous, so the page that comes in enters from the side the turn heads towards.
|
|
121
|
+
*/
|
|
122
|
+
export function slideOffsetFor(direction: TurnDirection, distance: number): number {
|
|
123
|
+
return direction === 'next' ? -distance : distance;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The page a turn lands on, or `null` when it would go before the first page or past the last.
|
|
128
|
+
* Nothing wraps round.
|
|
129
|
+
*/
|
|
130
|
+
export function resolveTurn(
|
|
131
|
+
current: number,
|
|
132
|
+
direction: TurnDirection,
|
|
133
|
+
pageCount: number
|
|
134
|
+
): number | null {
|
|
135
|
+
const target = direction === 'next' ? current + 1 : current - 1;
|
|
136
|
+
return target >= 0 && target < pageCount ? target : null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Width of each edge tap zone: the outer quarter of the view, at least 44 CSS px. */
|
|
140
|
+
export function edgeZoneWidth(viewWidth: number): number {
|
|
141
|
+
return Math.min(viewWidth / 2, Math.max(MIN_EDGE_ZONE_PX, viewWidth * EDGE_ZONE_FRACTION));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The turn an edge tap at `x` (CSS px from the view's left edge) asks for, or `null` in the middle. */
|
|
145
|
+
export function classifyTap(x: number, viewWidth: number): TurnDirection | null {
|
|
146
|
+
const zone = edgeZoneWidth(viewWidth);
|
|
147
|
+
if (x < zone) return 'previous';
|
|
148
|
+
if (x > viewWidth - zone) return 'next';
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The turn a finished drag asks for, or `null` to settle back. `velocityX` is the horizontal speed
|
|
154
|
+
* at release in px/ms (negative = leftwards). Swipe left always means next, whatever the layout
|
|
155
|
+
* direction. A slow release turns once the drag passes a quarter of the view; a fast fling turns
|
|
156
|
+
* in its own direction unless it flicks back against the drag. A mostly vertical drag never turns.
|
|
157
|
+
*/
|
|
158
|
+
export function classifySwipe(
|
|
159
|
+
dx: number,
|
|
160
|
+
dy: number,
|
|
161
|
+
viewWidth: number,
|
|
162
|
+
velocityX: number
|
|
163
|
+
): TurnDirection | null {
|
|
164
|
+
if (Math.abs(dx) < Math.abs(dy)) return null;
|
|
165
|
+
const threshold = viewWidth * SWIPE_TURN_FRACTION;
|
|
166
|
+
const fromDrag = dx <= -threshold ? 'next' : dx >= threshold ? 'previous' : null;
|
|
167
|
+
if (Math.abs(velocityX) < FLING_VELOCITY_PX_PER_MS) return fromDrag;
|
|
168
|
+
const fromFling = velocityX < 0 ? 'next' : 'previous';
|
|
169
|
+
const dragOpposes = (fromFling === 'next' && dx > 0) || (fromFling === 'previous' && dx < 0);
|
|
170
|
+
return dragOpposes ? null : fromFling;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A pointer position at a moment, for working out the release speed. */
|
|
174
|
+
export type PointerSample = { x: number; t: number };
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Horizontal speed (px/ms) over the samples from the last {@link VELOCITY_WINDOW_MS}, or 0 when
|
|
178
|
+
* they span no time. `samples` are oldest first, ending with the release.
|
|
179
|
+
*/
|
|
180
|
+
export function releaseVelocityX(samples: PointerSample[]): number {
|
|
181
|
+
const last = samples[samples.length - 1];
|
|
182
|
+
if (!last) return 0;
|
|
183
|
+
const first = samples.find((s) => last.t - s.t <= VELOCITY_WINDOW_MS) ?? last;
|
|
184
|
+
const dt = last.t - first.t;
|
|
185
|
+
return dt > 0 ? (last.x - first.x) / dt : 0;
|
|
186
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { DisplayMode } from '../ExpoPdfMarkup.types';
|
|
2
|
+
import type { PageSize, PdfPageMeta } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Layout for every page. Continuous mode fits each page to the view's width. Book mode fits the
|
|
6
|
+
* whole page into the view, falling back to width when the height isn't known yet.
|
|
7
|
+
*/
|
|
8
|
+
export function computePageMetas(
|
|
9
|
+
sizes: PageSize[],
|
|
10
|
+
viewWidth: number,
|
|
11
|
+
viewHeight: number,
|
|
12
|
+
displayMode: DisplayMode,
|
|
13
|
+
dpr: number
|
|
14
|
+
): PdfPageMeta[] {
|
|
15
|
+
if (viewWidth <= 0) return [];
|
|
16
|
+
return sizes.map(({ pdfWidth, pdfHeight }) => {
|
|
17
|
+
const widthScale = viewWidth / pdfWidth;
|
|
18
|
+
const scale =
|
|
19
|
+
displayMode === 'book' && viewHeight > 0
|
|
20
|
+
? Math.min(widthScale, viewHeight / pdfHeight)
|
|
21
|
+
: widthScale;
|
|
22
|
+
return {
|
|
23
|
+
pdfWidth,
|
|
24
|
+
pdfHeight,
|
|
25
|
+
scale,
|
|
26
|
+
canvasWidth: Math.round(pdfWidth * scale * dpr),
|
|
27
|
+
canvasHeight: Math.round(pdfHeight * scale * dpr),
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
package/src/web/types.ts
CHANGED
|
@@ -10,3 +10,6 @@ export type PdfPageMeta = {
|
|
|
10
10
|
/** Canvas backing height in physical pixels (CSS height × devicePixelRatio) */
|
|
11
11
|
canvasHeight: number;
|
|
12
12
|
};
|
|
13
|
+
|
|
14
|
+
/** A page's size in PDF user-space points, as read from the document before any layout. */
|
|
15
|
+
export type PageSize = Pick<PdfPageMeta, 'pdfWidth' | 'pdfHeight'>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Type-checks the test suites, including the compile-time type tests, which
|
|
2
|
+
// Jest (Babel) and `expo-module typecheck` (tests excluded) never type-check.
|
|
3
|
+
{
|
|
4
|
+
"extends": "./tsconfig.json",
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"noEmit": true
|
|
7
|
+
},
|
|
8
|
+
"include": ["./src"],
|
|
9
|
+
"exclude": []
|
|
10
|
+
}
|