Skip to content

Commit 519b792

Browse files
committed
feat(annotation): support imperative changes
1 parent cf4609b commit 519b792

6 files changed

Lines changed: 52 additions & 7 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ class PdfViewManager(private val pdfMutex: Lock) : SimpleViewManager<PdfView>(),
9191
@ReactProp(name = "source")
9292
override fun setSource(view: PdfView, source: String?) = view.setSource(source ?: "")
9393

94+
override fun imperativeApplyAnnotation(view: PdfView, annotation: String) {
95+
view.setAnnotation(annotation, file = false)
96+
view.renderPdf()
97+
}
98+
9499
companion object {
95100
const val NAME = "PdfView"
96101
}

ios/PdfView.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ - (void)updateState:(const facebook::react::State::Shared &)state oldState:(cons
8686
[super updateState:state oldState:oldState];
8787
}
8888

89+
- (void)handleCommand:(nonnull const NSString *)commandName args:(nonnull const NSArray *)args {
90+
RCTPdfViewHandleCommand(self, commandName, args);
91+
}
92+
93+
- (void)imperativeApplyAnnotation:(NSString *)annotation
94+
{
95+
[_view updatePropsWithAnnotStr:annotation];
96+
}
97+
8998
Class<RCTComponentViewProtocol> PdfViewCls(void)
9099
{
91100
return PdfView.class;

ios/PdfView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public class PdfView: UIView {
4747
dispatcher(nextWidth, nextHeight)
4848
}
4949

50+
@objc public func updateProps(annotStr: String) {
51+
if annotationStr != annotStr {
52+
annotationStr = annotStr
53+
renderPdf()
54+
}
55+
}
56+
5057
@objc public func updateProps(annot: String, annotStr: String, pg: Int, rsMd: ResizeMode, src: String) {
5158
var isDirty = false
5259
var needsMeasure = false

src/PdfView.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useCallback } from 'react';
1+
import { useCallback, useImperativeHandle, useRef } from 'react';
22
import type {
33
LayoutChangeEvent,
44
NativeSyntheticEvent,
55
ViewStyle,
66
} from 'react-native';
77

8-
import PdfViewNative from './PdfViewNativeComponent';
8+
import PdfViewNative, { Commands } from './PdfViewNativeComponent';
99
import { asPath } from './Util';
1010

1111
export type ErrorEvent = { message: string };
@@ -14,6 +14,8 @@ export type LoadCompleteEvent = { height: number; width: number };
1414

1515
export type ResizeMode = 'contain' | 'fitWidth';
1616

17+
export type PdfViewRef = { setAnnotation(annotation: string): void };
18+
1719
export type PdfViewProps = {
1820
/**
1921
* PAS v1 annotation JSON string.
@@ -49,6 +51,8 @@ export type PdfViewProps = {
4951
*/
5052
page: number;
5153

54+
ref?: React.RefObject<PdfViewRef | null>;
55+
5256
/**
5357
* How pdf page should be scaled to fit in view dimensions.
5458
*
@@ -79,6 +83,16 @@ export type PdfViewProps = {
7983
export function PdfView(props: PdfViewProps) {
8084
const { onError, onLayout, onLoadComplete } = props;
8185

86+
const nativeRef = useRef<React.ComponentRef<typeof PdfViewNative>>(null);
87+
88+
useImperativeHandle(props.ref, () => ({
89+
setAnnotation: (annotation: string) => {
90+
if (nativeRef.current != null) {
91+
Commands.imperativeApplyAnnotation(nativeRef.current, annotation);
92+
}
93+
},
94+
}));
95+
8296
const onPdfError = useCallback(
8397
(event: NativeSyntheticEvent<ErrorEvent>) => {
8498
if (onError != null) {
@@ -104,6 +118,7 @@ export function PdfView(props: PdfViewProps) {
104118
onPdfError={onPdfError}
105119
onPdfLoadComplete={onPdfLoadComplete}
106120
page={props.page}
121+
ref={nativeRef}
107122
resizeMode={props.resizeMode}
108123
source={asPath(props.source)}
109124
style={props.style}

src/PdfViewNativeComponent.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import {
2-
codegenNativeComponent,
3-
type CodegenTypes,
4-
type ViewProps,
5-
} from 'react-native';
1+
import type { CodegenTypes, HostComponent, ViewProps } from 'react-native';
2+
import { codegenNativeCommands, codegenNativeComponent } from 'react-native';
63

74
export type ErrorEvent = { message: string };
85

@@ -23,6 +20,17 @@ interface NativeProps extends ViewProps {
2320
source: string;
2421
}
2522

23+
interface NativeCommands {
24+
imperativeApplyAnnotation(
25+
viewRef: React.ElementRef<HostComponent<NativeProps>>,
26+
annotation: string
27+
): void;
28+
}
29+
30+
export const Commands = codegenNativeCommands<NativeCommands>({
31+
supportedCommands: ['imperativeApplyAnnotation'],
32+
});
33+
2634
export default codegenNativeComponent<NativeProps>('PdfView', {
2735
interfaceOnly: true,
2836
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type {
44
ErrorEvent,
55
LoadCompleteEvent,
66
PdfViewProps,
7+
PdfViewRef,
78
ResizeMode,
89
} from './PdfView';
910

0 commit comments

Comments
 (0)