@tobyt/expo-pdf-markup 0.4.0 → 0.6.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 (36) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +12 -3
  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 +49 -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 +20 -1
  9. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +52 -6
  10. package/build/ExpoPdfMarkup.types.d.ts +29 -1
  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 +1 -1
  14. package/build/ExpoPdfMarkupView.d.ts.map +1 -1
  15. package/build/ExpoPdfMarkupView.js +15 -3
  16. package/build/ExpoPdfMarkupView.js.map +1 -1
  17. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  18. package/build/ExpoPdfMarkupView.web.js +131 -43
  19. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  20. package/build/pdfjs.d.ts.map +1 -1
  21. package/build/pdfjs.js +0 -1
  22. package/build/pdfjs.js.map +1 -1
  23. package/build/web/annotationUtils.d.ts +9 -1
  24. package/build/web/annotationUtils.d.ts.map +1 -1
  25. package/build/web/annotationUtils.js +88 -5
  26. package/build/web/annotationUtils.js.map +1 -1
  27. package/ios/AnnotationModels.swift +185 -0
  28. package/ios/AnnotationSerializer.swift +39 -31
  29. package/ios/ExpoPdfMarkupModule.swift +16 -1
  30. package/ios/ExpoPdfMarkupView.swift +287 -74
  31. package/package.json +1 -1
  32. package/src/ExpoPdfMarkup.types.ts +37 -1
  33. package/src/ExpoPdfMarkupView.tsx +38 -5
  34. package/src/ExpoPdfMarkupView.web.tsx +170 -41
  35. package/src/pdfjs.ts +0 -1
  36. package/src/web/annotationUtils.ts +121 -4
@@ -19,11 +19,17 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
19
19
  private val onLoadComplete by EventDispatcher()
20
20
  private val onError by EventDispatcher()
21
21
  private val onAnnotationsChanged by EventDispatcher()
22
+ private val onTextInputRequested by EventDispatcher()
22
23
 
23
24
  private var renderer: PdfRenderer? = null
24
25
  private var fileDescriptor: ParcelFileDescriptor? = null
25
26
  private var currentSource: String? = null
26
27
  private var pendingAnnotationsJson: String? = null
28
+ var useJsTextDialog: Boolean = false
29
+
30
+ private var pendingTextPage: Int = -1
31
+ private var pendingTextPoint: AnnotationPoint? = null
32
+ private var pendingTextAnnotationId: String? = null
27
33
 
28
34
  init {
29
35
  addView(pdfView)
@@ -40,8 +46,21 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
40
46
  pdfView.onAnnotationsChangedListener = {
41
47
  emitAnnotationsChanged()
42
48
  }
43
- pdfView.onTextInputRequested = { page, point ->
44
- showTextInputDialog(page, point)
49
+ pdfView.onTextInputRequested = { page, point, annotation ->
50
+ if (useJsTextDialog) {
51
+ pendingTextPage = page
52
+ pendingTextPoint = point
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)
61
+ } else {
62
+ showTextInputDialog(page, point, annotation)
63
+ }
45
64
  }
46
65
  }
47
66
 
@@ -107,6 +126,10 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
107
126
  pdfView.annotationLineWidth = width?.toFloat() ?: 2f
108
127
  }
109
128
 
129
+ fun setAnnotationFontFamily(font: String?) {
130
+ pdfView.annotationFontFamily = font
131
+ }
132
+
110
133
  private fun emitAnnotationsChanged() {
111
134
  val data = AnnotationsData(version = 1, annotations = pdfView.annotations)
112
135
  val json = AnnotationSerializer.serialize(data)
@@ -114,18 +137,40 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
114
137
  onAnnotationsChanged(mapOf("annotations" to json))
115
138
  }
116
139
 
117
- private fun showTextInputDialog(page: Int, point: AnnotationPoint) {
140
+ fun provideTextInput(text: String?) {
141
+ val page = pendingTextPage.also { pendingTextPage = -1 }
142
+ val point = pendingTextPoint.also { pendingTextPoint = null }
143
+ val annotationId = pendingTextAnnotationId.also { pendingTextAnnotationId = null }
144
+ if (page < 0 || point == null || text.isNullOrEmpty()) return
145
+ if (annotationId != null) {
146
+ pdfView.updateTextAnnotation(annotationId, text)
147
+ } else {
148
+ pdfView.addTextAnnotation(page, point, text)
149
+ }
150
+ }
151
+
152
+ private fun showTextInputDialog(
153
+ page: Int,
154
+ point: AnnotationPoint,
155
+ annotation: AnnotationModel?
156
+ ) {
118
157
  val editText = EditText(context).apply {
119
158
  hint = "Enter text"
120
159
  setPadding(48, 32, 48, 32)
160
+ setText(annotation?.contents.orEmpty())
161
+ setSelection(text.length)
121
162
  }
122
163
  AlertDialog.Builder(context)
123
- .setTitle("Add Text")
164
+ .setTitle(if (annotation == null) "Add Text" else "Edit Text")
124
165
  .setView(editText)
125
- .setPositiveButton("Add") { _, _ ->
166
+ .setPositiveButton(if (annotation == null) "Add" else "Update") { _, _ ->
126
167
  val text = editText.text.toString().trim()
127
168
  if (text.isNotEmpty()) {
128
- pdfView.addTextAnnotation(page, point, text)
169
+ if (annotation != null) {
170
+ pdfView.updateTextAnnotation(annotation.id, text)
171
+ } else {
172
+ pdfView.addTextAnnotation(page, point, text)
173
+ }
129
174
  }
130
175
  }
131
176
  .setNegativeButton("Cancel", null)
@@ -138,6 +183,7 @@ class ExpoPdfMarkupView(context: Context, appContext: AppContext) : ExpoView(con
138
183
  renderer = null
139
184
  fileDescriptor?.close()
140
185
  fileDescriptor = null
186
+ currentSource = null
141
187
  }
142
188
 
143
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: family name (`"serif"`, `"monospace"`) 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: {
@@ -93,6 +113,14 @@ export type ExpoPdfMarkupViewProps = {
93
113
  annotations: string;
94
114
  };
95
115
  }) => void;
116
+ /**
117
+ * Optional async callback for the text annotation tool. When provided, this is called instead of
118
+ * the native platform dialog (UIAlertController / AlertDialog) when the user taps to place a text
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.
122
+ */
123
+ onTextInputRequested?: (request: TextInputRequest) => Promise<string | null>;
96
124
  style?: StyleProp<ViewStyle>;
97
125
  };
98
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,SA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,MAAM,CAAA;SAAE,CAAC;KACzF,KAAK,IAAI,CAAC;IACX,oDAAoD;IACpD,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,WAAW,EAAE;YAAE,SAAS,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,KAAK,IAAI,CAAC;IACzE,6CAA6C;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 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: family name (`\"serif\"`, `\"monospace\"`) 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,4 +1,4 @@
1
1
  import * as React from 'react';
2
2
  import { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';
3
- export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps): React.JSX.Element;
3
+ export default function ExpoPdfMarkupView({ onTextInputRequested, ...props }: ExpoPdfMarkupViewProps): React.JSX.Element;
4
4
  //# sourceMappingURL=ExpoPdfMarkupView.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkupView.d.ts","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAI/D,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,qBAEtE"}
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"}
@@ -1,7 +1,19 @@
1
- import { requireNativeView } from 'expo';
1
+ import { requireNativeModule, requireNativeView } from 'expo';
2
2
  import * as React from 'react';
3
+ import { findNodeHandle } from 'react-native';
3
4
  const NativeView = requireNativeView('ExpoPdfMarkup');
4
- export default function ExpoPdfMarkupView(props) {
5
- return <NativeView {...props}/>;
5
+ const NativeModule = requireNativeModule('ExpoPdfMarkup');
6
+ export default function ExpoPdfMarkupView({ onTextInputRequested, ...props }) {
7
+ const nativeRef = React.useRef(null);
8
+ const handleNativeTextInputRequested = React.useCallback(async (event) => {
9
+ if (!onTextInputRequested)
10
+ return;
11
+ const tag = findNodeHandle(nativeRef.current);
12
+ if (tag == null)
13
+ return;
14
+ const text = await onTextInputRequested(event.nativeEvent);
15
+ await NativeModule.provideTextInput(tag, text ?? null);
16
+ }, [onTextInputRequested]);
17
+ return (<NativeView ref={nativeRef} {...props} useJsTextDialog={!!onTextInputRequested} onTextInputRequested={onTextInputRequested ? handleNativeTextInputRequested : undefined}/>);
6
18
  }
7
19
  //# sourceMappingURL=ExpoPdfMarkupView.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpoPdfMarkupView.js","sourceRoot":"","sources":["../src/ExpoPdfMarkupView.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAI/B,MAAM,UAAU,GAAgD,iBAAiB,CAAC,eAAe,CAAC,CAAC;AAEnG,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,KAA6B;IACrE,OAAO,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,EAAG,CAAC;AACnC,CAAC","sourcesContent":["import { requireNativeView } from 'expo';\nimport * as React from 'react';\n\nimport { ExpoPdfMarkupViewProps } from './ExpoPdfMarkup.types';\n\nconst NativeView: React.ComponentType<ExpoPdfMarkupViewProps> = requireNativeView('ExpoPdfMarkup');\n\nexport default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {\n return <NativeView {...props} />;\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;AAmB/B,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAEnD;AA+QD,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,qBAwTtE"}
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,11 +1,13 @@
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.
7
+ // Relative URL so the worker resolves correctly regardless of the sub-path
8
+ // the app is hosted at (e.g. /example/ on GitHub Pages).
7
9
  // Override with setPdfJsWorkerSrc() if using a different bundler or CDN.
8
- pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.mjs';
10
+ pdfjs.GlobalWorkerOptions.workerSrc = './pdf.worker.min.mjs';
9
11
  }
10
12
  /** Allow consumers to override the pdfjs worker URL before mounting the view. */
11
13
  export function setPdfJsWorkerSrc(url) {
@@ -16,7 +18,7 @@ export function setPdfJsWorkerSrc(url) {
16
18
  // ---------------------------------------------------------------------------
17
19
  const MIN_ZOOM = 1;
18
20
  const MAX_ZOOM = 5;
19
- 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, }) {
20
22
  const pdfCanvasRef = React.useRef(null);
21
23
  const annotCanvasRef = React.useRef(null);
22
24
  const pageRef = React.useRef(null);
@@ -25,9 +27,13 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
25
27
  const inkPointsRef = React.useRef([]);
26
28
  const dragStartRef = React.useRef(null);
27
29
  const dragCurrentRef = React.useRef(null);
28
- 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;
29
34
  const cssWidth = meta.canvasWidth / dpr;
30
35
  const cssHeight = meta.canvasHeight / dpr;
36
+ const showEditingHighlights = annotationMode === 'move' || annotationMode === 'eraser';
31
37
  // Render PDF page onto pdfCanvas
32
38
  React.useEffect(() => {
33
39
  let cancelled = false;
@@ -77,7 +83,11 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
77
83
  if (!ctx)
78
84
  return;
79
85
  ctx.scale(dpr, dpr);
80
- 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
+ });
81
91
  // Live ink preview
82
92
  if (liveInkPoints && liveInkPoints.length > 1) {
83
93
  ctx.save();
@@ -105,10 +115,24 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
105
115
  ctx.strokeRect(dragRect.x, dragRect.y, dragRect.width, dragRect.height);
106
116
  ctx.restore();
107
117
  }
108
- }, [annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth]);
118
+ }, [annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth, showEditingHighlights]);
109
119
  React.useEffect(() => {
110
- redrawAnnotations();
111
- }, [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]);
112
136
  function getCanvasPoint(e) {
113
137
  const rect = e.currentTarget.getBoundingClientRect();
114
138
  return { x: (e.clientX - rect.left) / zoomLevel, y: (e.clientY - rect.top) / zoomLevel };
@@ -116,16 +140,30 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
116
140
  function handlePointerDown(e) {
117
141
  if (annotationMode === 'none')
118
142
  return;
119
- e.currentTarget.setPointerCapture(e.pointerId);
120
143
  const pt = getCanvasPoint(e);
121
- pointerDownRef.current = true;
122
144
  if (annotationMode === 'ink') {
145
+ e.currentTarget.setPointerCapture(e.pointerId);
146
+ pointerDownRef.current = true;
123
147
  inkPointsRef.current = [pt];
124
148
  }
125
149
  else if (annotationMode === 'highlight' || annotationMode === 'underline') {
150
+ e.currentTarget.setPointerCapture(e.pointerId);
151
+ pointerDownRef.current = true;
126
152
  dragStartRef.current = pt;
127
153
  dragCurrentRef.current = pt;
128
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
+ }
129
167
  }
130
168
  function handlePointerMove(e) {
131
169
  if (!pointerDownRef.current)
@@ -144,6 +182,16 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
144
182
  const h = Math.abs(pt.y - start.y);
145
183
  redrawAnnotations(undefined, new DOMRect(minX, minY, w, h));
146
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
+ }
147
195
  }
148
196
  function handlePointerUp(e) {
149
197
  if (!pointerDownRef.current && annotationMode !== 'text' && annotationMode !== 'eraser')
@@ -202,9 +250,29 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
202
250
  });
203
251
  }
204
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
+ }
205
266
  else if (annotationMode === 'text') {
206
267
  const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
207
- 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);
208
276
  }
209
277
  else if (annotationMode === 'eraser') {
210
278
  const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
@@ -214,6 +282,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
214
282
  }
215
283
  }
216
284
  const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
285
+ const cursor = annotationMode === 'move' ? 'grab' : annotationMode === 'eraser' ? 'not-allowed' : 'crosshair';
217
286
  return (<div ref={containerRef} style={{ position: 'relative', width: cssWidth, height: cssHeight, margin: '0 auto' }}>
218
287
  <canvas ref={pdfCanvasRef} style={{
219
288
  position: 'absolute',
@@ -229,7 +298,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
229
298
  left: 0,
230
299
  width: cssWidth,
231
300
  height: cssHeight,
232
- cursor: 'crosshair',
301
+ cursor,
233
302
  pointerEvents,
234
303
  }} onPointerDown={handlePointerDown} onPointerMove={handlePointerMove} onPointerUp={handlePointerUp}/>
235
304
  </div>);
@@ -238,7 +307,7 @@ function PageView({ doc, pageIndex, meta, annotations, annotationMode, annotatio
238
307
  // Main component
239
308
  // ---------------------------------------------------------------------------
240
309
  export default function ExpoPdfMarkupView(props) {
241
- const { source, page: propPage, backgroundColor = '#f0f0f0', annotations: annotationsProp, annotationMode = 'none', annotationColor = '#FF0000', annotationLineWidth = 2, onPageChanged, onLoadComplete, onError, onAnnotationsChanged, 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;
242
311
  const scrollContainerRef = React.useRef(null);
243
312
  const [containerWidth, setContainerWidth] = React.useState(0);
244
313
  const [doc, setDoc] = React.useState(null);
@@ -251,6 +320,15 @@ export default function ExpoPdfMarkupView(props) {
251
320
  const [zoomLevel, setZoomLevel] = React.useState(1);
252
321
  const zoomRef = React.useRef(1);
253
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]);
254
332
  React.useEffect(() => {
255
333
  const el = scrollContainerRef.current;
256
334
  if (!el)
@@ -440,46 +518,56 @@ export default function ExpoPdfMarkupView(props) {
440
518
  return () => io.disconnect();
441
519
  }, [doc, pageMetas, onPageChanged]);
442
520
  const handleAnnotationAdded = React.useCallback((annotation) => {
443
- setAnnotations((prev) => {
444
- const next = [...prev, annotation];
445
- const json = serializeAnnotations({ version: 1, annotations: next });
446
- lastAnnotationsJsonRef.current = json;
447
- onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
448
- return next;
449
- });
450
- }, [onAnnotationsChanged]);
521
+ applyAnnotations((prev) => [...prev, annotation]);
522
+ }, [applyAnnotations]);
451
523
  const handleAnnotationRemoved = React.useCallback((id) => {
452
- setAnnotations((prev) => {
453
- const next = prev.filter((a) => a.id !== id);
454
- const json = serializeAnnotations({ version: 1, annotations: next });
455
- lastAnnotationsJsonRef.current = json;
456
- onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
457
- return next;
458
- });
459
- }, [onAnnotationsChanged]);
460
- const handleTextInputRequested = React.useCallback((pageIndex, pdfPoint) => {
461
- const text = window.prompt('Enter annotation text:');
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) => {
530
+ const text = onTextInputRequested
531
+ ? await onTextInputRequested(request)
532
+ : window.prompt(request.mode === 'edit' ? 'Edit annotation text:' : 'Enter annotation text:', request.currentText ?? '');
462
533
  if (!text)
463
534
  return;
464
- const fontSize = 16;
465
- const estimatedWidth = text.length * fontSize * 0.6;
466
- const bounds = {
467
- x: pdfPoint.x,
468
- y: pdfPoint.y,
469
- width: estimatedWidth,
470
- height: fontSize * 1.5,
471
- };
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
+ }
472
548
  handleAnnotationAdded({
473
549
  id: newAnnotationId(),
474
550
  type: 'text',
475
- page: pageIndex,
551
+ page: request.page,
476
552
  color: annotationColor,
477
- bounds,
553
+ bounds: {
554
+ x: request.point.x,
555
+ y: request.point.y,
556
+ width,
557
+ height,
558
+ },
478
559
  contents: text,
479
560
  fontSize,
561
+ fontFamily: annotationFontFamily,
480
562
  createdAt: Date.now(),
481
563
  });
482
- }, [annotationColor, handleAnnotationAdded]);
564
+ }, [
565
+ annotationColor,
566
+ annotationFontFamily,
567
+ applyAnnotations,
568
+ handleAnnotationAdded,
569
+ onTextInputRequested,
570
+ ]);
483
571
  const styleObj = style;
484
572
  return (<div ref={scrollContainerRef} style={{
485
573
  overflowY: 'scroll',
@@ -491,7 +579,7 @@ export default function ExpoPdfMarkupView(props) {
491
579
  <div style={{ zoom: zoomLevel }}>
492
580
  {doc &&
493
581
  pageMetas.map((meta, i) => (<React.Fragment key={i}>
494
- <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) => {
495
583
  pageContainerRefs.current[i] = el;
496
584
  }}/>
497
585
  {i < pageMetas.length - 1 && <div style={{ height: 8 }} aria-hidden/>}