@tobyt/expo-pdf-markup 0.5.0 → 0.6.1

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 (32) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +7 -0
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +83 -3
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
  8. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +4 -0
  9. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +36 -8
  10. package/build/ExpoPdfMarkup.types.d.ts +25 -3
  11. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  12. package/build/ExpoPdfMarkup.types.js.map +1 -1
  13. package/build/ExpoPdfMarkupView.d.ts.map +1 -1
  14. package/build/ExpoPdfMarkupView.js +2 -2
  15. package/build/ExpoPdfMarkupView.js.map +1 -1
  16. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  17. package/build/ExpoPdfMarkupView.web.js +127 -43
  18. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  19. package/build/web/annotationUtils.d.ts +9 -1
  20. package/build/web/annotationUtils.d.ts.map +1 -1
  21. package/build/web/annotationUtils.js +88 -5
  22. package/build/web/annotationUtils.js.map +1 -1
  23. package/ios/AnnotationModels.swift +185 -0
  24. package/ios/AnnotationSerializer.swift +63 -33
  25. package/ios/ExpoPdfMarkupModule.swift +4 -0
  26. package/ios/ExpoPdfMarkupView.swift +279 -86
  27. package/ios/Tests/AnnotationSerializerTests.swift +18 -0
  28. package/package.json +1 -1
  29. package/src/ExpoPdfMarkup.types.ts +33 -3
  30. package/src/ExpoPdfMarkupView.tsx +12 -9
  31. package/src/ExpoPdfMarkupView.web.tsx +165 -41
  32. package/src/web/annotationUtils.ts +121 -4
@@ -29,6 +29,7 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
29
29
 
30
30
  private var pendingTextPage: Int = -1
31
31
  private var pendingTextPoint: AnnotationPoint? = null
32
+ private var pendingTextAnnotationId: String? = null
32
33
 
33
34
  init {
34
35
  addView(pdfView)
@@ -45,13 +46,20 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
45
46
  pdfView.onAnnotationsChangedListener = {
46
47
  emitAnnotationsChanged()
47
48
  }
48
- pdfView.onTextInputRequested = { page, point ->
49
+ pdfView.onTextInputRequested = { page, point, annotation ->
49
50
  if (useJsTextDialog) {
50
51
  pendingTextPage = page
51
52
  pendingTextPoint = point
52
- onTextInputRequested(emptyMap<String, Any>())
53
+ pendingTextAnnotationId = annotation?.id
54
+ val payload = mutableMapOf<String, Any>(
55
+ "mode" to if (annotation == null) "create" else "edit",
56
+ "page" to page,
57
+ "point" to mapOf("x" to point.x, "y" to point.y)
58
+ )
59
+ annotation?.contents?.let { payload["currentText"] = it }
60
+ onTextInputRequested(payload)
53
61
  } else {
54
- showTextInputDialog(page, point)
62
+ showTextInputDialog(page, point, annotation)
55
63
  }
56
64
  }
57
65
  }
@@ -118,6 +126,10 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
118
126
  pdfView.annotationLineWidth = width?.toFloat() ?: 2f
119
127
  }
120
128
 
129
+ fun setAnnotationFontFamily(font: String?) {
130
+ pdfView.annotationFontFamily = font
131
+ }
132
+
121
133
  private fun emitAnnotationsChanged() {
122
134
  val data = AnnotationsData(version = 1, annotations = pdfView.annotations)
123
135
  val json = AnnotationSerializer.serialize(data)
@@ -128,22 +140,37 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
128
140
  fun provideTextInput(text: String?) {
129
141
  val page = pendingTextPage.also { pendingTextPage = -1 }
130
142
  val point = pendingTextPoint.also { pendingTextPoint = null }
143
+ val annotationId = pendingTextAnnotationId.also { pendingTextAnnotationId = null }
131
144
  if (page < 0 || point == null || text.isNullOrEmpty()) return
132
- pdfView.addTextAnnotation(page, point, text)
145
+ if (annotationId != null) {
146
+ pdfView.updateTextAnnotation(annotationId, text)
147
+ } else {
148
+ pdfView.addTextAnnotation(page, point, text)
149
+ }
133
150
  }
134
151
 
135
- private fun showTextInputDialog(page: Int, point: AnnotationPoint) {
152
+ private fun showTextInputDialog(
153
+ page: Int,
154
+ point: AnnotationPoint,
155
+ annotation: AnnotationModel?
156
+ ) {
136
157
  val editText = EditText(context).apply {
137
158
  hint = "Enter text"
138
159
  setPadding(48, 32, 48, 32)
160
+ setText(annotation?.contents.orEmpty())
161
+ setSelection(text.length)
139
162
  }
140
163
  AlertDialog.Builder(context)
141
- .setTitle("Add Text")
164
+ .setTitle(if (annotation == null) "Add Text" else "Edit Text")
142
165
  .setView(editText)
143
- .setPositiveButton("Add") { _, _ ->
166
+ .setPositiveButton(if (annotation == null) "Add" else "Update") { _, _ ->
144
167
  val text = editText.text.toString().trim()
145
168
  if (text.isNotEmpty()) {
146
- pdfView.addTextAnnotation(page, point, text)
169
+ if (annotation != null) {
170
+ pdfView.updateTextAnnotation(annotation.id, text)
171
+ } else {
172
+ pdfView.addTextAnnotation(page, point, text)
173
+ }
147
174
  }
148
175
  }
149
176
  .setNegativeButton("Cancel", null)
@@ -156,6 +183,7 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
156
183
  renderer = null
157
184
  fileDescriptor?.close()
158
185
  fileDescriptor = null
186
+ currentSource = null
159
187
  }
160
188
 
161
189
  override fun onDetachedFromWindow() {
@@ -43,6 +43,13 @@ export type TextAnnotation = {
43
43
  bounds: AnnotationBounds;
44
44
  contents?: string;
45
45
  fontSize?: number;
46
+ /**
47
+ * Font family for the text annotation. Platform-specific:
48
+ * - iOS: PostScript name (`"Georgia"`, `"Courier"`) or `undefined` for system font (San Francisco)
49
+ * - Android: family name (`"serif"`, `"monospace"`) or `undefined` for default sans-serif
50
+ * - Web: CSS font family (`"Georgia, serif"`) or `undefined` for `sans-serif`
51
+ */
52
+ fontFamily?: string;
46
53
  createdAt?: number;
47
54
  };
48
55
  export type Annotation = InkAnnotation | HighlightAnnotation | UnderlineAnnotation | TextAnnotation;
@@ -50,7 +57,13 @@ export type AnnotationsData = {
50
57
  version: 1;
51
58
  annotations: Annotation[];
52
59
  };
53
- export type AnnotationMode = 'none' | 'ink' | 'highlight' | 'underline' | 'text' | 'eraser';
60
+ export type AnnotationMode = 'none' | 'ink' | 'highlight' | 'underline' | 'text' | 'eraser' | 'move';
61
+ export type TextInputRequest = {
62
+ mode: 'create' | 'edit';
63
+ page: number;
64
+ point: AnnotationPoint;
65
+ currentText?: string;
66
+ };
54
67
  export type ExpoPdfMarkupViewProps = {
55
68
  /** Absolute local file path of the PDF to display. */
56
69
  source: string;
@@ -66,6 +79,13 @@ export type ExpoPdfMarkupViewProps = {
66
79
  annotationColor?: string;
67
80
  /** Stroke width in points applied to new ink annotations. */
68
81
  annotationLineWidth?: number;
82
+ /**
83
+ * Default font family for new text annotations. Platform-specific:
84
+ * - iOS: PostScript name (`"Georgia"`, `"Courier"`) or `undefined` for system font (San Francisco)
85
+ * - Android: React Native / Expo font family name (`"Montserrat-Regular"`, `"serif"`) or `undefined` for default sans-serif
86
+ * - Web: CSS font family (`"Georgia, serif"`) or `undefined` for `sans-serif`
87
+ */
88
+ annotationFontFamily?: string;
69
89
  /** Fired when the visible page changes. */
70
90
  onPageChanged?: (event: {
71
91
  nativeEvent: {
@@ -96,9 +116,11 @@ export type ExpoPdfMarkupViewProps = {
96
116
  /**
97
117
  * Optional async callback for the text annotation tool. When provided, this is called instead of
98
118
  * the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text
99
- * annotation. Return the text string to create the annotation, or `null` to cancel.
119
+ * annotation. When editing an existing text annotation, `request.currentText` is populated and
120
+ * `request.mode` is `'edit'`. Return the text string to create/update the annotation, or `null`
121
+ * to cancel.
100
122
  */
101
- onTextInputRequested?: () => Promise<string | null>;
123
+ onTextInputRequested?: (request: TextInputRequest) => Promise<string | null>;
102
124
  style?: StyleProp<ViewStyle>;
103
125
  };
104
126
  //# sourceMappingURL=ExpoPdfMarkup.types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkup.types.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkup.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,MAAM,eAAe,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,MAAM,MAAM,gBAAgB,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvF,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,KAAK,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,eAAe,EAAE,EAAE,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,gBAAgB,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CA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6C;IAC7C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,WAAW,EAAE;YAAE,OAAO,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,KAAK,IAAI,CAAC;IAChE,wHAAwH;IACxH,oBAAoB,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,WAAW,EAAE;YAAE,WAAW,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,KAAK,IAAI,CAAC;IACjF;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC7E,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;CAC9B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkup.types.js","sourceRoot":"","sources":["../src/ExpoPdfMarkup.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { StyleProp, ViewStyle } from 'react-native';\n\nexport type AnnotationPoint = { x: number; y: number };\n\nexport type AnnotationBounds = { x: number; y: number; width: number; height: number };\n\nexport type InkAnnotation = {\n id: string;\n type: 'ink';\n page: number;\n color: string;\n lineWidth: number;\n paths: AnnotationPoint[][];\n createdAt?: number;\n};\n\nexport type HighlightAnnotation = {\n id: string;\n type: 'highlight';\n page: number;\n color: string;\n alpha?: number;\n bounds: AnnotationBounds;\n createdAt?: number;\n};\n\nexport type UnderlineAnnotation = {\n id: string;\n type: 'underline';\n page: number;\n color: string;\n bounds: AnnotationBounds;\n createdAt?: number;\n};\n\nexport type TextAnnotation = {\n id: string;\n type: 'text' | 'freeText';\n page: number;\n color: string;\n bounds: AnnotationBounds;\n contents?: string;\n fontSize?: number;\n createdAt?: number;\n};\n\nexport type Annotation = InkAnnotation | HighlightAnnotation | UnderlineAnnotation | TextAnnotation;\n\nexport type AnnotationsData = {\n version: 1;\n annotations: Annotation[];\n};\n\nexport type AnnotationMode = 'none' | 'ink' | 'highlight' | 'underline' | 'text' | 'eraser';\n\nexport type ExpoPdfMarkupViewProps = {\n /** Absolute local file path of the PDF to display. */\n source: string;\n /** 0-based page number to display. Can be used to restore scroll position on orientation change. */\n page?: number;\n /** Background colour shown behind the PDF pages. */\n backgroundColor?: string;\n /** Serialised {@link AnnotationsData} JSON string to load when the document opens. */\n annotations?: string;\n /** Active annotation tool. Defaults to `'none'` (view-only). */\n annotationMode?: AnnotationMode;\n /** Colour applied to new annotations, as a CSS colour string (e.g. `'#FF0000'`). */\n annotationColor?: string;\n /** Stroke width in points applied to new ink annotations. */\n annotationLineWidth?: number;\n /** Fired when the visible page changes. */\n onPageChanged?: (event: {\n nativeEvent: { page: number; pageCount: number; pageWidth: number; pageHeight: number };\n }) => void;\n /** Fired once the document has finished loading. */\n onLoadComplete?: (event: { nativeEvent: { pageCount: number } }) => void;\n /** Fired when the document fails to load. */\n onError?: (event: { nativeEvent: { message: string } }) => void;\n /** Fired after annotations are created or edited. `annotations` is a serialised {@link AnnotationsData} JSON string. */\n onAnnotationsChanged?: (event: { nativeEvent: { annotations: string } }) => void;\n /**\n * Optional async callback for the text annotation tool. When provided, this is called instead of\n * the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text\n * annotation. Return the text string to create the annotation, or `null` to cancel.\n */\n onTextInputRequested?: () => Promise<string | null>;\n style?: StyleProp<ViewStyle>;\n};\n"]}
1
+ {"version":3,"file":"ExpoPdfMarkup.types.js","sourceRoot":"","sources":["../src/ExpoPdfMarkup.types.ts"],"names":[],"mappings":"","sourcesContent":["import type { StyleProp, ViewStyle } from 'react-native';\n\nexport type AnnotationPoint = { x: number; y: number };\n\nexport type AnnotationBounds = { x: number; y: number; width: number; height: number };\n\nexport type InkAnnotation = {\n id: string;\n type: 'ink';\n page: number;\n color: string;\n lineWidth: number;\n paths: AnnotationPoint[][];\n createdAt?: number;\n};\n\nexport type HighlightAnnotation = {\n id: string;\n type: 'highlight';\n page: number;\n color: string;\n alpha?: number;\n bounds: AnnotationBounds;\n createdAt?: number;\n};\n\nexport type UnderlineAnnotation = {\n id: string;\n type: 'underline';\n page: number;\n color: string;\n bounds: AnnotationBounds;\n createdAt?: number;\n};\n\nexport type TextAnnotation = {\n id: string;\n type: 'text' | 'freeText';\n page: number;\n color: string;\n bounds: AnnotationBounds;\n contents?: string;\n fontSize?: number;\n /**\n * Font family for the text annotation. Platform-specific:\n * - iOS: PostScript name (`\"Georgia\"`, `\"Courier\"`) or `undefined` for system font (San Francisco)\n * - Android: family name (`\"serif\"`, `\"monospace\"`) or `undefined` for default sans-serif\n * - Web: CSS font family (`\"Georgia, serif\"`) or `undefined` for `sans-serif`\n */\n fontFamily?: string;\n createdAt?: number;\n};\n\nexport type Annotation = InkAnnotation | HighlightAnnotation | UnderlineAnnotation | TextAnnotation;\n\nexport type AnnotationsData = {\n version: 1;\n annotations: Annotation[];\n};\n\nexport type AnnotationMode =\n | 'none'\n | 'ink'\n | 'highlight'\n | 'underline'\n | 'text'\n | 'eraser'\n | 'move';\n\nexport type TextInputRequest = {\n mode: 'create' | 'edit';\n page: number;\n point: AnnotationPoint;\n currentText?: string;\n};\n\nexport type ExpoPdfMarkupViewProps = {\n /** Absolute local file path of the PDF to display. */\n source: string;\n /** 0-based page number to display. Can be used to restore scroll position on orientation change. */\n page?: number;\n /** Background colour shown behind the PDF pages. */\n backgroundColor?: string;\n /** Serialised {@link AnnotationsData} JSON string to load when the document opens. */\n annotations?: string;\n /** Active annotation tool. Defaults to `'none'` (view-only). */\n annotationMode?: AnnotationMode;\n /** Colour applied to new annotations, as a CSS colour string (e.g. `'#FF0000'`). */\n annotationColor?: string;\n /** Stroke width in points applied to new ink annotations. */\n annotationLineWidth?: number;\n /**\n * Default font family for new text annotations. Platform-specific:\n * - iOS: PostScript name (`\"Georgia\"`, `\"Courier\"`) or `undefined` for system font (San Francisco)\n * - Android: React Native / Expo font family name (`\"Montserrat-Regular\"`, `\"serif\"`) or `undefined` for default sans-serif\n * - Web: CSS font family (`\"Georgia, serif\"`) or `undefined` for `sans-serif`\n */\n annotationFontFamily?: string;\n /** Fired when the visible page changes. */\n onPageChanged?: (event: {\n nativeEvent: { page: number; pageCount: number; pageWidth: number; pageHeight: number };\n }) => void;\n /** Fired once the document has finished loading. */\n onLoadComplete?: (event: { nativeEvent: { pageCount: number } }) => void;\n /** Fired when the document fails to load. */\n onError?: (event: { nativeEvent: { message: string } }) => void;\n /** Fired after annotations are created or edited. `annotations` is a serialised {@link AnnotationsData} JSON string. */\n onAnnotationsChanged?: (event: { nativeEvent: { annotations: string } }) => void;\n /**\n * Optional async callback for the text annotation tool. When provided, this is called instead of\n * the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text\n * annotation. When editing an existing text annotation, `request.currentText` is populated and\n * `request.mode` is `'edit'`. Return the text string to create/update the annotation, or `null`\n * to cancel.\n */\n onTextInputRequested?: (request: TextInputRequest) => Promise<string | null>;\n style?: StyleProp<ViewStyle>;\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkupView.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAa/D,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,EACxC,oBAAoB,EACpB,GAAG,KAAK,EACT,EAAE,sBAAsB,qBAmBxB"}
1
+ {"version":3,"file":"ExpoPdfMarkupView.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,sBAAsB,EAAoB,MAAM,uBAAuB,CAAC;AAajF,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,EACxC,oBAAoB,EACpB,GAAG,KAAK,EACT,EAAE,sBAAsB,qBAsBxB"}
@@ -5,13 +5,13 @@ const NativeView = requireNativeView('ExpoPdfMarkup');
5
5
  const NativeModule = requireNativeModule('ExpoPdfMarkup');
6
6
  export default function ExpoPdfMarkupView({ onTextInputRequested, ...props }) {
7
7
  const nativeRef = React.useRef(null);
8
- const handleNativeTextInputRequested = React.useCallback(async () => {
8
+ const handleNativeTextInputRequested = React.useCallback(async (event) => {
9
9
  if (!onTextInputRequested)
10
10
  return;
11
11
  const tag = findNodeHandle(nativeRef.current);
12
12
  if (tag == null)
13
13
  return;
14
- const text = await onTextInputRequested();
14
+ const text = await onTextInputRequested(event.nativeEvent);
15
15
  await NativeModule.provideTextInput(tag, text ?? null);
16
16
  }, [onTextInputRequested]);
17
17
  return (<NativeView ref={nativeRef} {...props} useJsTextDialog={!!onTextInputRequested} onTextInputRequested={onTextInputRequested ? handleNativeTextInputRequested : undefined}/>);
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkupView.js","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAS9C,MAAM,UAAU,GAAG,iBAAiB,CAAC,eAAe,CAEnD,CAAC;AAEF,MAAM,YAAY,GAAG,mBAAmB,CAAC,eAAe,CAAC,CAAC;AAE1D,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,EACxC,oBAAoB,EACpB,GAAG,KAAK,EACe;IACvB,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAU,IAAI,CAAC,CAAC;IAE9C,MAAM,8BAA8B,GAAG,KAAK,CAAC,WAAW,CAAC,KAAK,IAAI,EAAE;QAClE,IAAI,CAAC,oBAAoB;YAAE,OAAO;QAClC,MAAM,GAAG,GAAG,cAAc,CAAC,SAAS,CAAC,OAA+C,CAAC,CAAC;QACtF,IAAI,GAAG,IAAI,IAAI;YAAE,OAAO;QACxB,MAAM,IAAI,GAAG,MAAM,oBAAoB,EAAE,CAAC;QAC1C,MAAM,YAAY,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC;IACzD,CAAC,EAAE,CAAC,oBAAoB,CAAC,CAAC,CAAC;IAE3B,OAAO,CACL,CAAC,UAAU,CACT,GAAG,CAAC,CAAC,SAAS,CAAC,CACf,IAAI,KAAK,CAAC,CACV,eAAe,CAAC,CAAC,CAAC,CAAC,oBAAoB,CAAC,CACxC,oBAAoB,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,8BAA8B,CAAC,CAAC,CAAC,SAAS,CAAC,EACxF,CACH,CAAC;AACJ,CAAC","sourcesContent":["import { requireNativeModule, requireNativeView } from 'expo';\nimport * as React from 'react';\nimport { findNodeHandle } from 'react-native';\n\nimport { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';\n\ntype NativeViewProps = Omit<ExpoPdfMarkupViewProps, 'onTextInputRequested'> & {\n useJsTextDialog?: boolean;\n onTextInputRequested?: (event: { nativeEvent: Record<string, never> }) => void;\n};\n\nconst NativeView = requireNativeView('ExpoPdfMarkup') as React.ForwardRefExoticComponent<\n NativeViewProps & React.RefAttributes<unknown>\n>;\n\nconst NativeModule = requireNativeModule('ExpoPdfMarkup');\n\nexport default function ExpoPdfMarkupView({\n onTextInputRequested,\n ...props\n}: ExpoPdfMarkupViewProps) {\n const nativeRef = React.useRef<unknown>(null);\n\n const handleNativeTextInputRequested = React.useCallback(async () => {\n if (!onTextInputRequested) return;\n const tag = findNodeHandle(nativeRef.current as Parameters<typeof findNodeHandle>[0]);\n if (tag == null) return;\n const text = await onTextInputRequested();\n await NativeModule.provideTextInput(tag, text ?? null);\n }, [onTextInputRequested]);\n\n return (\n <NativeView\n ref={nativeRef}\n {...props}\n useJsTextDialog={!!onTextInputRequested}\n onTextInputRequested={onTextInputRequested ? handleNativeTextInputRequested : undefined}\n />\n );\n}\n"]}
1
+ {"version":3,"file":"ExpoPdfMarkupView.js","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9D,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAS9C,MAAM,UAAU,GAAG,iBAAiB,CAAC,eAAe,CAEnD,CAAC;AAEF,MAAM,YAAY,GAAG,mBAAmB,CAAC,eAAe,CAAC,CAAC;AAE1D,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,EACxC,oBAAoB,EACpB,GAAG,KAAK,EACe;IACvB,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAU,IAAI,CAAC,CAAC;IAE9C,MAAM,8BAA8B,GAAG,KAAK,CAAC,WAAW,CACtD,KAAK,EAAE,KAAwC,EAAE,EAAE;QACjD,IAAI,CAAC,oBAAoB;YAAE,OAAO;QAClC,MAAM,GAAG,GAAG,cAAc,CAAC,SAAS,CAAC,OAA+C,CAAC,CAAC;QACtF,IAAI,GAAG,IAAI,IAAI;YAAE,OAAO;QACxB,MAAM,IAAI,GAAG,MAAM,oBAAoB,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC3D,MAAM,YAAY,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC;IACzD,CAAC,EACD,CAAC,oBAAoB,CAAC,CACvB,CAAC;IAEF,OAAO,CACL,CAAC,UAAU,CACT,GAAG,CAAC,CAAC,SAAS,CAAC,CACf,IAAI,KAAK,CAAC,CACV,eAAe,CAAC,CAAC,CAAC,CAAC,oBAAoB,CAAC,CACxC,oBAAoB,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,8BAA8B,CAAC,CAAC,CAAC,SAAS,CAAC,EACxF,CACH,CAAC;AACJ,CAAC","sourcesContent":["import { requireNativeModule, requireNativeView } from 'expo';\nimport * as React from 'react';\nimport { findNodeHandle } from 'react-native';\n\nimport { ExpoPdfMarkupViewProps, TextInputRequest } from './ExpoPdfMarkup.types';\n\ntype NativeViewProps = Omit<ExpoPdfMarkupViewProps, 'onTextInputRequested'> & {\n useJsTextDialog?: boolean;\n onTextInputRequested?: (event: { nativeEvent: TextInputRequest }) => void;\n};\n\nconst NativeView = requireNativeView('ExpoPdfMarkup') as React.ForwardRefExoticComponent<\n NativeViewProps & React.RefAttributes<unknown>\n>;\n\nconst NativeModule = requireNativeModule('ExpoPdfMarkup');\n\nexport default function ExpoPdfMarkupView({\n onTextInputRequested,\n ...props\n}: ExpoPdfMarkupViewProps) {\n const nativeRef = React.useRef<unknown>(null);\n\n const handleNativeTextInputRequested = React.useCallback(\n async (event: { nativeEvent: TextInputRequest }) => {\n if (!onTextInputRequested) return;\n const tag = findNodeHandle(nativeRef.current as Parameters<typeof findNodeHandle>[0]);\n if (tag == null) return;\n const text = await onTextInputRequested(event.nativeEvent);\n await NativeModule.provideTextInput(tag, text ?? null);\n },\n [onTextInputRequested]\n );\n\n return (\n <NativeView\n ref={nativeRef}\n {...props}\n useJsTextDialog={!!onTextInputRequested}\n onTextInputRequested={onTextInputRequested ? handleNativeTextInputRequested : undefined}\n />\n );\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkupView.web.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.web.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,KAAK,EAIV,sBAAsB,EACvB,MAAM,uBAAuB,CAAC;AAqB/B,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAEnD;AA+QD,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,qBA2TtE"}
1
+ {"version":3,"file":"ExpoPdfMarkupView.web.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.web.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,KAAK,EAGV,sBAAsB,EAGvB,MAAM,uBAAuB,CAAC;AAuB/B,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAEnD;AAkWD,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,qBAiWtE"}
@@ -1,6 +1,6 @@
1
1
  import * as pdfjs from 'pdfjs-dist';
2
2
  import * as React from 'react';
3
- import { canvasRectToPdfBounds, canvasToPdf, drawAnnotationsOnCanvas, hitTestAnnotation, newAnnotationId, parseAnnotations, serializeAnnotations, } from './web/annotationUtils';
3
+ import { clampAnnotationTranslation, canvasRectToPdfBounds, canvasToPdf, drawAnnotationsOnCanvas, hitTestAnnotation, newAnnotationId, parseAnnotations, serializeAnnotations, translateAnnotation, } from './web/annotationUtils';
4
4
  // Configure pdfjs worker once at module level (consumers can override before mounting)
5
5
  if (!pdfjs.GlobalWorkerOptions.workerSrc) {
6
6
  // Default: served from public/ by withPdfMarkup() in metro.js.
@@ -18,7 +18,7 @@ export function setPdfJsWorkerSrc(url) {
18
18
  // ---------------------------------------------------------------------------
19
19
  const MIN_ZOOM = 1;
20
20
  const MAX_ZOOM = 5;
21
- function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotationColor, annotationLineWidth, zoomLevel, onAnnotationAdded, onAnnotationRemoved, onTextInputRequested, containerRef, }) {
21
+ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotationColor, annotationLineWidth, zoomLevel, onAnnotationAdded, onAnnotationRemoved, onAnnotationUpdated, onTextInputRequested, containerRef, }) {
22
22
  const pdfCanvasRef = React.useRef(null);
23
23
  const annotCanvasRef = React.useRef(null);
24
24
  const pageRef = React.useRef(null);
@@ -27,9 +27,13 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
27
27
  const inkPointsRef = React.useRef([]);
28
28
  const dragStartRef = React.useRef(null);
29
29
  const dragCurrentRef = React.useRef(null);
30
- const dpr = typeof window !== 'undefined' ? window.devicePixelRatio ?? 1 : 1;
30
+ const moveTargetRef = React.useRef(null);
31
+ const moveStartPdfRef = React.useRef(null);
32
+ const movePreviewRef = React.useRef(null);
33
+ const dpr = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
31
34
  const cssWidth = meta.canvasWidth / dpr;
32
35
  const cssHeight = meta.canvasHeight / dpr;
36
+ const showEditingHighlights = annotationMode === 'move' || annotationMode === 'eraser';
33
37
  // Render PDF page onto pdfCanvas
34
38
  React.useEffect(() => {
35
39
  let cancelled = false;
@@ -79,7 +83,11 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
79
83
  if (!ctx)
80
84
  return;
81
85
  ctx.scale(dpr, dpr);
82
- drawAnnotationsOnCanvas(ctx, annotations, pageIndex, meta);
86
+ drawAnnotationsOnCanvas(ctx, annotations, pageIndex, meta, {
87
+ highlightAnnotations: showEditingHighlights,
88
+ previewAnnotationId: moveTargetRef.current?.id ?? null,
89
+ previewAnnotation: movePreviewRef.current,
90
+ });
83
91
  // Live ink preview
84
92
  if (liveInkPoints && liveInkPoints.length > 1) {
85
93
  ctx.save();
@@ -107,10 +115,24 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
107
115
  ctx.strokeRect(dragRect.x, dragRect.y, dragRect.width, dragRect.height);
108
116
  ctx.restore();
109
117
  }
110
- }, [annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth]);
118
+ }, [annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth, showEditingHighlights]);
111
119
  React.useEffect(() => {
112
- redrawAnnotations();
113
- }, [redrawAnnotations]);
120
+ // CSS @font-face fonts (e.g. loaded via expo-font on web) may not be available to canvas
121
+ // until explicitly requested via document.fonts.load(). Collect the unique font families
122
+ // used by text annotations on this page and load them before drawing.
123
+ const fontFamilies = [
124
+ ...new Set(annotations
125
+ .filter((a) => a.page === pageIndex && (a.type === 'text' || a.type === 'freeText'))
126
+ .map((a) => a.fontFamily)
127
+ .filter((f) => Boolean(f))),
128
+ ];
129
+ if (fontFamilies.length > 0 && typeof document !== 'undefined') {
130
+ Promise.all(fontFamilies.map((f) => document.fonts.load(`16px ${f}`))).finally(() => redrawAnnotations());
131
+ }
132
+ else {
133
+ redrawAnnotations();
134
+ }
135
+ }, [redrawAnnotations, annotations, pageIndex]);
114
136
  function getCanvasPoint(e) {
115
137
  const rect = e.currentTarget.getBoundingClientRect();
116
138
  return { x: (e.clientX - rect.left) / zoomLevel, y: (e.clientY - rect.top) / zoomLevel };
@@ -118,16 +140,30 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
118
140
  function handlePointerDown(e) {
119
141
  if (annotationMode === 'none')
120
142
  return;
121
- e.currentTarget.setPointerCapture(e.pointerId);
122
143
  const pt = getCanvasPoint(e);
123
- pointerDownRef.current = true;
124
144
  if (annotationMode === 'ink') {
145
+ e.currentTarget.setPointerCapture(e.pointerId);
146
+ pointerDownRef.current = true;
125
147
  inkPointsRef.current = [pt];
126
148
  }
127
149
  else if (annotationMode === 'highlight' || annotationMode === 'underline') {
150
+ e.currentTarget.setPointerCapture(e.pointerId);
151
+ pointerDownRef.current = true;
128
152
  dragStartRef.current = pt;
129
153
  dragCurrentRef.current = pt;
130
154
  }
155
+ else if (annotationMode === 'move') {
156
+ const pdfPoint = canvasToPdf(pt.x, pt.y, meta.scale, meta.pdfHeight);
157
+ const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
158
+ if (!hit)
159
+ return;
160
+ e.currentTarget.setPointerCapture(e.pointerId);
161
+ pointerDownRef.current = true;
162
+ moveTargetRef.current = hit;
163
+ moveStartPdfRef.current = pdfPoint;
164
+ movePreviewRef.current = hit;
165
+ redrawAnnotations();
166
+ }
131
167
  }
132
168
  function handlePointerMove(e) {
133
169
  if (!pointerDownRef.current)
@@ -146,6 +182,16 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
146
182
  const h = Math.abs(pt.y - start.y);
147
183
  redrawAnnotations(undefined, new DOMRect(minX, minY, w, h));
148
184
  }
185
+ else if (annotationMode === 'move') {
186
+ const target = moveTargetRef.current;
187
+ const startPdfPoint = moveStartPdfRef.current;
188
+ if (!target || !startPdfPoint)
189
+ return;
190
+ const pdfPoint = canvasToPdf(pt.x, pt.y, meta.scale, meta.pdfHeight);
191
+ const delta = clampAnnotationTranslation(target, pdfPoint.x - startPdfPoint.x, pdfPoint.y - startPdfPoint.y, meta.pdfWidth, meta.pdfHeight);
192
+ movePreviewRef.current = translateAnnotation(target, delta.x, delta.y);
193
+ redrawAnnotations();
194
+ }
149
195
  }
150
196
  function handlePointerUp(e) {
151
197
  if (!pointerDownRef.current && annotationMode !== 'text' && annotationMode !== 'eraser')
@@ -204,9 +250,29 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
204
250
  });
205
251
  }
206
252
  }
253
+ else if (annotationMode === 'move') {
254
+ const target = moveTargetRef.current;
255
+ const preview = movePreviewRef.current;
256
+ moveTargetRef.current = null;
257
+ moveStartPdfRef.current = null;
258
+ movePreviewRef.current = null;
259
+ if (target && preview && preview !== target) {
260
+ onAnnotationUpdated(preview);
261
+ }
262
+ else {
263
+ redrawAnnotations();
264
+ }
265
+ }
207
266
  else if (annotationMode === 'text') {
208
267
  const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
209
- onTextInputRequested(pageIndex, pdfPoint);
268
+ const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
269
+ const annotation = hit?.type === 'text' || hit?.type === 'freeText' ? hit : null;
270
+ onTextInputRequested({
271
+ mode: annotation ? 'edit' : 'create',
272
+ page: pageIndex,
273
+ point: pdfPoint,
274
+ currentText: annotation?.contents,
275
+ }, annotation);
210
276
  }
211
277
  else if (annotationMode === 'eraser') {
212
278
  const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
@@ -216,6 +282,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
216
282
  }
217
283
  }
218
284
  const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
285
+ const cursor = annotationMode === 'move' ? 'grab' : annotationMode === 'eraser' ? 'not-allowed' : 'crosshair';
219
286
  return (<div ref={containerRef} style={{ position: 'relative', width: cssWidth, height: cssHeight, margin: '0 auto' }}>
220
287
  <canvas ref={pdfCanvasRef} style={{
221
288
  position: 'absolute',
@@ -231,7 +298,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
231
298
  left: 0,
232
299
  width: cssWidth,
233
300
  height: cssHeight,
234
- cursor: 'crosshair',
301
+ cursor,
235
302
  pointerEvents,
236
303
  }} onPointerDown={handlePointerDown} onPointerMove={handlePointerMove} onPointerUp={handlePointerUp}/>
237
304
  </div>);
@@ -240,7 +307,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
240
307
  // Main component
241
308
  // ---------------------------------------------------------------------------
242
309
  export default function ExpoPdfMarkupView(props) {
243
- const { source, page: propPage, backgroundColor = '#f0f0f0', annotations: annotationsProp, annotationMode = 'none', annotationColor = '#FF0000', annotationLineWidth = 2, onPageChanged, onLoadComplete, onError, onAnnotationsChanged, onTextInputRequested, style, } = props;
310
+ const { source, page: propPage, backgroundColor = '#f0f0f0', annotations: annotationsProp, annotationMode = 'none', annotationColor = '#FF0000', annotationLineWidth = 2, annotationFontFamily, onPageChanged, onLoadComplete, onError, onAnnotationsChanged, onTextInputRequested, style, } = props;
244
311
  const scrollContainerRef = React.useRef(null);
245
312
  const [containerWidth, setContainerWidth] = React.useState(0);
246
313
  const [doc, setDoc] = React.useState(null);
@@ -253,6 +320,15 @@ export default function ExpoPdfMarkupView(props) {
253
320
  const [zoomLevel, setZoomLevel] = React.useState(1);
254
321
  const zoomRef = React.useRef(1);
255
322
  // ResizeObserver → containerWidth
323
+ const applyAnnotations = React.useCallback((updater) => {
324
+ setAnnotations((prev) => {
325
+ const next = updater(prev);
326
+ const json = serializeAnnotations({ version: 1, annotations: next });
327
+ lastAnnotationsJsonRef.current = json;
328
+ onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
329
+ return next;
330
+ });
331
+ }, [onAnnotationsChanged]);
256
332
  React.useEffect(() => {
257
333
  const el = scrollContainerRef.current;
258
334
  if (!el)
@@ -442,48 +518,56 @@ export default function ExpoPdfMarkupView(props) {
442
518
  return () => io.disconnect();
443
519
  }, [doc, pageMetas, onPageChanged]);
444
520
  const handleAnnotationAdded = React.useCallback((annotation) => {
445
- setAnnotations((prev) => {
446
- const next = [...prev, annotation];
447
- const json = serializeAnnotations({ version: 1, annotations: next });
448
- lastAnnotationsJsonRef.current = json;
449
- onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
450
- return next;
451
- });
452
- }, [onAnnotationsChanged]);
521
+ applyAnnotations((prev) => [...prev, annotation]);
522
+ }, [applyAnnotations]);
453
523
  const handleAnnotationRemoved = React.useCallback((id) => {
454
- setAnnotations((prev) => {
455
- const next = prev.filter((a) => a.id !== id);
456
- const json = serializeAnnotations({ version: 1, annotations: next });
457
- lastAnnotationsJsonRef.current = json;
458
- onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
459
- return next;
460
- });
461
- }, [onAnnotationsChanged]);
462
- const handleTextInputRequested = React.useCallback(async (pageIndex, pdfPoint) => {
524
+ applyAnnotations((prev) => prev.filter((a) => a.id !== id));
525
+ }, [applyAnnotations]);
526
+ const handleAnnotationUpdated = React.useCallback((annotation) => {
527
+ applyAnnotations((prev) => prev.map((existing) => (existing.id === annotation.id ? annotation : existing)));
528
+ }, [applyAnnotations]);
529
+ const handleTextInputRequested = React.useCallback(async (request, annotation) => {
463
530
  const text = onTextInputRequested
464
- ? await onTextInputRequested()
465
- : window.prompt('Enter annotation text:');
531
+ ? await onTextInputRequested(request)
532
+ : window.prompt(request.mode === 'edit' ? 'Edit annotation text:' : 'Enter annotation text:', request.currentText ?? '');
466
533
  if (!text)
467
534
  return;
468
- const fontSize = 16;
469
- const estimatedWidth = text.length * fontSize * 0.6;
470
- const bounds = {
471
- x: pdfPoint.x,
472
- y: pdfPoint.y,
473
- width: estimatedWidth,
474
- height: fontSize * 1.5,
475
- };
535
+ const fontSize = annotation?.fontSize ?? 16;
536
+ const width = text.length * fontSize * 0.6;
537
+ const height = fontSize * 1.5;
538
+ if (annotation) {
539
+ const bounds = {
540
+ x: annotation.bounds.x,
541
+ y: annotation.bounds.y + annotation.bounds.height - height,
542
+ width,
543
+ height,
544
+ };
545
+ applyAnnotations((prev) => prev.map((existing) => existing.id === annotation.id ? { ...annotation, bounds, contents: text } : existing));
546
+ return;
547
+ }
476
548
  handleAnnotationAdded({
477
549
  id: newAnnotationId(),
478
550
  type: 'text',
479
- page: pageIndex,
551
+ page: request.page,
480
552
  color: annotationColor,
481
- bounds,
553
+ bounds: {
554
+ x: request.point.x,
555
+ y: request.point.y,
556
+ width,
557
+ height,
558
+ },
482
559
  contents: text,
483
560
  fontSize,
561
+ fontFamily: annotationFontFamily,
484
562
  createdAt: Date.now(),
485
563
  });
486
- }, [annotationColor, handleAnnotationAdded, onTextInputRequested]);
564
+ }, [
565
+ annotationColor,
566
+ annotationFontFamily,
567
+ applyAnnotations,
568
+ handleAnnotationAdded,
569
+ onTextInputRequested,
570
+ ]);
487
571
  const styleObj = style;
488
572
  return (<div ref={scrollContainerRef} style={{
489
573
  overflowY: 'scroll',
@@ -495,7 +579,7 @@ export default function ExpoPdfMarkupView(props) {
495
579
  <div style={{ zoom: zoomLevel }}>
496
580
  {doc &&
497
581
  pageMetas.map((meta, i) => (<React.Fragment key={i}>
498
- <PageView doc={doc} pageIndex={i} meta={meta} annotations={annotations} annotationMode={annotationMode} annotationColor={annotationColor} annotationLineWidth={annotationLineWidth} zoomLevel={zoomLevel} onAnnotationAdded={handleAnnotationAdded} onAnnotationRemoved={handleAnnotationRemoved} onTextInputRequested={handleTextInputRequested} containerRef={(el) => {
582
+ <PageView doc={doc} pageIndex={i} meta={meta} annotations={annotations} annotationMode={annotationMode} annotationColor={annotationColor} annotationLineWidth={annotationLineWidth} zoomLevel={zoomLevel} onAnnotationAdded={handleAnnotationAdded} onAnnotationRemoved={handleAnnotationRemoved} onAnnotationUpdated={handleAnnotationUpdated} onTextInputRequested={handleTextInputRequested} containerRef={(el) => {
499
583
  pageContainerRefs.current[i] = el;
500
584
  }}/>
501
585
  {i < pageMetas.length - 1 && <div style={{ height: 8 }} aria-hidden/>}