Skip to content

Commit 57286c3

Browse files
authored
Added a Operation result event for addAnnotation. (#181)
Partially fixes https://github.com/PSPDFKit/react-native/issues/169 Adds a promise to add annotation to ensure that the creation is successful.
1 parent 6488156 commit 57286c3

File tree

6 files changed

+129
-35
lines changed

6 files changed

+129
-35
lines changed

index.windows.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class PSPDFKitView extends React.Component {
2727
onDocumentSaved={this._onDocumentSaved}
2828
onDocumentSaveFailed={this._onDocumentSaveFailed}
2929
onDataReturned={this._onDataReturned}
30+
onOperationResult={this._onOperationResult}
3031
/>
3132
);
3233
}
@@ -60,6 +61,17 @@ class PSPDFKitView extends React.Component {
6061
this._requestMap.delete(requestId);
6162
};
6263

64+
_onOperationResult = event => {
65+
let {requestId, success, error} = event.nativeEvent;
66+
let promise = this._requestMap[requestId];
67+
if (success) {
68+
promise.resolve(success);
69+
} else {
70+
promise.reject(error);
71+
}
72+
this._requestMap.delete(requestId);
73+
};
74+
6375
/**
6476
* Enters the annotation creation mode, showing the annotation creation toolbar.
6577
*/
@@ -124,14 +136,29 @@ class PSPDFKitView extends React.Component {
124136
*
125137
* @param annotation InstantJson of the annotation to add with the format of
126138
* https://pspdfkit.com/guides/windows/current/importing-exporting/instant-json/#instant-annotation-json-api
139+
*
140+
* @returns a promise resolving with a successful payload if annotation is created.
141+
* {'success' : boolean}
142+
* If success is false an error will also be held.
143+
* {'error' : string}
127144
*/
128145
addAnnotation(annotation) {
146+
let requestId = this._nextRequestId++;
147+
let requestMap = this._requestMap;
148+
149+
// We create a promise here that will be resolved once onDataReturned is called.
150+
let promise = new Promise(function (resolve, reject) {
151+
requestMap[requestId] = {resolve: resolve, reject: reject};
152+
});
153+
129154
UIManager.dispatchViewManagerCommand(
130155
findNodeHandle(this.refs.pdfView),
131156
UIManager.RCTPSPDFKitView.Commands.addAnnotation,
132-
[annotation]
157+
[requestId, annotation]
133158
);
134-
}
159+
160+
return promise;
161+
};
135162

136163
/**
137164
* Gets toolbar items currently shown.

samples/Catalog/Catalog.windows.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ const complexSearchConfiguration = {
4444
previewRange: {position: 20, length: 120}
4545
};
4646

47+
const annotationToAdd = {
48+
bbox: [
49+
89.58633422851562,
50+
98.5791015625,
51+
143.12948608398438,
52+
207.1583251953125
53+
],
54+
blendMode: "normal",
55+
createdAt: "2018-07-03T13:53:03Z",
56+
isDrawnNaturally: false,
57+
lineWidth: 5,
58+
lines: {
59+
intensities: [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
60+
points: [
61+
[
62+
[92.08633422851562, 101.07916259765625],
63+
[92.08633422851562, 202.15826416015625],
64+
[138.12950134277344, 303.2374267578125]
65+
],
66+
[
67+
[184.17266845703125, 101.07916259765625],
68+
[184.17266845703125, 202.15826416015625],
69+
[230.2158203125, 303.2374267578125]
70+
]
71+
]
72+
},
73+
opacity: 1,
74+
pageIndex: 0,
75+
strokeColor: "#AA47BE",
76+
type: "pspdfkit/ink",
77+
updatedAt: "2018-07-03T13:53:03Z",
78+
v: 1
79+
};
80+
4781
const simpleSearch = {
4882
searchString: "the"
4983
};
@@ -363,38 +397,10 @@ class PdfViewInstantJsonScreen extends Component<{}> {
363397
<Button
364398
onPress={() => {
365399
// This adds a new ink annotation to the first page.
366-
this.refs.pdfView.addAnnotation({
367-
bbox: [
368-
89.58633422851562,
369-
98.5791015625,
370-
143.12948608398438,
371-
207.1583251953125
372-
],
373-
blendMode: "normal",
374-
createdAt: "2018-07-03T13:53:03Z",
375-
isDrawnNaturally: false,
376-
lineWidth: 5,
377-
lines: {
378-
intensities: [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
379-
points: [
380-
[
381-
[92.08633422851562, 101.07916259765625],
382-
[92.08633422851562, 202.15826416015625],
383-
[138.12950134277344, 303.2374267578125]
384-
],
385-
[
386-
[184.17266845703125, 101.07916259765625],
387-
[184.17266845703125, 202.15826416015625],
388-
[230.2158203125, 303.2374267578125]
389-
]
390-
]
391-
},
392-
opacity: 1,
393-
pageIndex: 0,
394-
strokeColor: "#AA47BE",
395-
type: "pspdfkit/ink",
396-
updatedAt: "2018-07-03T13:53:03Z",
397-
v: 1
400+
this.refs.pdfView.addAnnotation(annotationToAdd).then(() => {
401+
alert("Annotation Creation was successful.");
402+
}).catch(error => {
403+
alert(error);
398404
});
399405
}}
400406
title="Add annotation"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Newtonsoft.Json.Linq;
2+
using ReactNative.UIManager.Events;
3+
4+
namespace ReactNativePSPDFKit.Events
5+
{
6+
class PdfViewOperationResult : Event
7+
{
8+
/**
9+
* Event sent by the PDFViewPage when a operation has succeeded
10+
*/
11+
public const string EVENT_NAME = "pdfViewOperationSuccessful";
12+
13+
private readonly JObject _payload = new JObject();
14+
15+
public PdfViewOperationResult(int viewId, int requestId) : base(viewId)
16+
{
17+
_payload.Add("requestId", requestId);
18+
_payload.Add("success", true);
19+
}
20+
21+
public PdfViewOperationResult(int viewId, int requestId, string errorMessage) : base(viewId)
22+
{
23+
_payload.Add("requestId", requestId);
24+
_payload.Add("success", false);
25+
_payload.Add("error", errorMessage);
26+
}
27+
28+
public override string EventName => EVENT_NAME;
29+
30+
public override void Dispatch(RCTEventEmitter rctEventEmitter)
31+
{
32+
rctEventEmitter.receiveEvent(ViewTag, EventName, _payload);
33+
}
34+
}
35+
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PDFViewPage.xaml.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ internal async Task SetToolbarItems(int requestId, string toolbarItemsJson)
159159
}
160160
}
161161

162+
public async Task CreateAnnotation(int requestId, string annotationJsonString)
163+
{
164+
try
165+
{
166+
await Pdfview.Document.CreateAnnotationAsync(Factory.FromJson(JsonObject.Parse(annotationJsonString)));
167+
168+
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
169+
new PdfViewOperationResult(this.GetTag(), requestId)
170+
);
171+
}
172+
catch (Exception e)
173+
{
174+
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
175+
new PdfViewOperationResult(this.GetTag(), requestId, e.Message)
176+
);
177+
}
178+
}
179+
162180
internal async Task SetPageIndexAsync(int index)
163181
{
164182
await PDFView.Controller.SetCurrentPageIndexAsync(index);

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitViewManager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public override async void ReceiveCommand(PDFViewPage view, int commandId, JArra
105105
await PdfViewPage.GetAnnotations(args[0].Value<int>(), args[1].Value<int>());
106106
break;
107107
case COMMAND_ADD_ANNOTATION:
108-
await PdfViewPage.Pdfview.Document.CreateAnnotationAsync(Factory.FromJson(JsonObject.Parse(args[0].ToString())));
108+
await PdfViewPage.CreateAnnotation(args[0].Value<int>(), args[1].ToString());
109109
break;
110110
case COMMAND_GET_TOOLBAR_ITEMS:
111111
PdfViewPage.GetToolbarItems(args[0].Value<int>());
@@ -146,6 +146,13 @@ public override async void ReceiveCommand(PDFViewPage view, int commandId, JArra
146146
{
147147
{"registrationName", "onDataReturned"},
148148
}
149+
},
150+
{
151+
PdfViewOperationResult.EVENT_NAME,
152+
new JObject
153+
{
154+
{"registrationName", "onOperationResult"},
155+
}
149156
}
150157
};
151158
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/ReactNativePSPDFKit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<Compile Include="Events\PdfViewDocumentSavedEvent.cs" />
111111
<Compile Include="Events\PdfViewDataReturnedEvent.cs" />
112112
<Compile Include="Events\PdfViewDocumentSaveFailedEvent.cs" />
113+
<Compile Include="Events\PdfViewOperationResult.cs" />
113114
<Compile Include="JsonUtils.cs" />
114115
<Compile Include="LibraryModule.cs" />
115116
<Compile Include="PSPDFKitModule.cs" />

0 commit comments

Comments
 (0)