Skip to content

Commit 95c79a3

Browse files
authored
feat(annotation): support read from JSON string
Merge pull request #15 from LupulescuAlexandru/master Support both annotation JSON string and file path
2 parents fe1a2b2 + 48c637f commit 95c79a3

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

android/src/main/java/com/alpha0010/pdf/PdfView.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PdfView(context: Context, private val pdfMutex: Lock) : View(context) {
4545
private var mSource = ""
4646
private val mViewRects = List(SLICES) { Rect() }
4747

48-
fun setAnnotation(source: String) {
48+
fun setAnnotation(source: String, file: Boolean = false) {
4949
if (source.isEmpty()) {
5050
if (mAnnotation.isNotEmpty()) {
5151
mAnnotation = emptyList()
@@ -55,7 +55,11 @@ class PdfView(context: Context, private val pdfMutex: Lock) : View(context) {
5555
}
5656

5757
try {
58-
mAnnotation = Json.decodeFromString(File(source).readText())
58+
if (file) {
59+
mAnnotation = Json.decodeFromString(File(source).readText())
60+
} else {
61+
mAnnotation = Json.decodeFromString(source);
62+
}
5963
mDirty = true
6064
} catch (e: Exception) {
6165
onError("Failed to load annotation from '$source'. ${e.message}")

android/src/main/java/com/alpha0010/pdf/PdfViewManager.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ class PdfViewManager(private val pdfMutex: Lock) : BaseViewManager<PdfView, PdfV
3838
view.renderPdf()
3939
}
4040

41-
@ReactProp(name = "annotation")
41+
/**
42+
* Set annotation from a PAS v1 JSON string
43+
*/
44+
@ReactProp(name = "annotationStr")
4245
fun setAnnotation(view: PdfView, source: String?) = view.setAnnotation(source ?: "")
4346

47+
/**
48+
* Set annotation from file containing a PAS v1 JSON string
49+
*/
50+
@ReactProp(name = "annotation")
51+
fun setAnnotationWithPath(view: PdfView, source: String?) = view.setAnnotation(source ?: "", true)
52+
4453
/**
4554
* Page (0-indexed) of document to display.
4655
*/

ios/PdfView.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ enum ResizeMode: String {
44
}
55

66
class PdfView: UIView {
7-
@objc var annotation = "" { didSet { loadAnnotation() } }
7+
@objc var annotationStr = "" { didSet { loadAnnotation() } }
8+
@objc var annotation = "" { didSet { loadAnnotation(file: true) } }
89
@objc var page: NSNumber = 0 { didSet { renderPdf() } }
910
@objc var resizeMode = ResizeMode.CONTAIN.rawValue { didSet { validateResizeMode() } }
1011
@objc var source = "" { didSet { renderPdf() } }
@@ -23,8 +24,8 @@ class PdfView: UIView {
2324
super.layoutSubviews()
2425
}
2526

26-
private func loadAnnotation() {
27-
guard !annotation.isEmpty else {
27+
private func loadAnnotation(file: Bool = false) {
28+
guard !annotation.isEmpty || !annotationStr.isEmpty else {
2829
if !annotationData.isEmpty {
2930
annotationData.removeAll()
3031
renderPdf()
@@ -34,7 +35,13 @@ class PdfView: UIView {
3435

3536
let decoder = JSONDecoder()
3637
do {
37-
let data = try Data(contentsOf: URL(fileURLWithPath: annotation))
38+
var data:Data;
39+
if (file) {
40+
data = try Data(contentsOf: URL(fileURLWithPath: annotation))
41+
}
42+
else {
43+
data = annotationStr.data(using: .utf8)!;
44+
}
3845
annotationData = try decoder.decode([AnnotationPage].self, from: data)
3946
} catch {
4047
dispatchOnError(

ios/PdfViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@interface RCT_EXTERN_REMAP_MODULE(RNPdfView, PdfViewManager, RCTViewManager)
44

55
RCT_EXPORT_VIEW_PROPERTY(annotation, NSString)
6+
RCT_EXPORT_VIEW_PROPERTY(annotationStr, NSString)
67
RCT_EXPORT_VIEW_PROPERTY(page, NSNumber)
78
RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString)
89
RCT_EXPORT_VIEW_PROPERTY(source, NSString)

src/Pdf.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ type BaseListProps = {
105105
};
106106

107107
type PdfProps = BaseListProps & {
108+
/**
109+
* PAS v1 annotation JSON string.
110+
*/
111+
annotationStr?: string;
112+
108113
/**
109114
* Path to annotation data.
110115
*/
@@ -314,14 +319,15 @@ export const Pdf = forwardRef((props: PdfProps, ref: React.Ref<PdfRef>) => {
314319
<View>
315320
<PdfView
316321
annotation={props.annotation}
322+
annotationStr={props.annotationStr}
317323
page={index}
318324
source={source}
319325
style={styles.page}
320326
/>
321327
</View>
322328
</View>
323329
),
324-
[maxPageHeight, props.annotation, source]
330+
[maxPageHeight, props.annotation, props.annotationStr, source]
325331
);
326332

327333
return (

src/PdfView.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type ResizeMode = 'contain' | 'fitWidth';
1515

1616
type PdfViewNativeProps = {
1717
annotation?: string;
18+
annotationStr?: string;
1819
onLayout?: (event: LayoutChangeEvent) => void;
1920
onPdfError: (event: NativeSyntheticEvent<ErrorEvent>) => void;
2021
onPdfLoadComplete: (event: NativeSyntheticEvent<LoadCompleteEvent>) => void;
@@ -25,11 +26,15 @@ type PdfViewNativeProps = {
2526
};
2627

2728
export type PdfViewProps = {
29+
/**
30+
* PAS v1 annotation JSON string.
31+
*/
32+
annotationStr?: string;
33+
2834
/**
2935
* Path to annotation data.
3036
*/
3137
annotation?: string;
32-
3338
/**
3439
* Callback to handle errors.
3540
*/
@@ -106,6 +111,7 @@ export function PdfView(props: PdfViewProps) {
106111
return (
107112
<PdfViewNative
108113
annotation={asPath(props.annotation)}
114+
annotationStr={props.annotationStr}
109115
onLayout={onLayout}
110116
onPdfError={onPdfError}
111117
onPdfLoadComplete={onPdfLoadComplete}

0 commit comments

Comments
 (0)