Skip to content

Commit ed7d6d7

Browse files
authored
Add promise to save document and interaction mode (#185)
Fixes : https://github.com/PSPDFKit/react-native/issues/169 These are the last operations that require to pass back a promise. The PR will change the contract for `saveCurrentDocument` which now directly returns a promise and will use the standard `OperationResult` like other none data returning types.
1 parent 5e5b3f0 commit ed7d6d7

File tree

4 files changed

+94
-68
lines changed

4 files changed

+94
-68
lines changed

index.windows.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class PSPDFKitView extends React.Component {
2424
ref="pdfView"
2525
{...this.props}
2626
onAnnotationsChanged={this._onAnnotationsChanged}
27-
onDocumentSaved={this._onDocumentSaved}
28-
onDocumentSaveFailed={this._onDocumentSaveFailed}
2927
onDataReturned={this._onDataReturned}
3028
onOperationResult={this._onOperationResult}
3129
/>
@@ -38,18 +36,6 @@ class PSPDFKitView extends React.Component {
3836
}
3937
};
4038

41-
_onDocumentSaved = event => {
42-
if (this.props.onDocumentSaved) {
43-
this.props.onDocumentSaved(event.nativeEvent);
44-
}
45-
};
46-
47-
_onDocumentSaveFailed = event => {
48-
if (this.props.onDocumentSaveFailed) {
49-
this.props.onDocumentSaveFailed(event.nativeEvent);
50-
}
51-
};
52-
5339
_onDataReturned = event => {
5440
let {requestId, result, error} = event.nativeEvent;
5541
let promise = this._requestMap[requestId];
@@ -74,35 +60,71 @@ class PSPDFKitView extends React.Component {
7460

7561
/**
7662
* Enters the annotation creation mode, showing the annotation creation toolbar.
63+
*
64+
* @returns a promise resolving if successful or rejects if an error occurs with and error message
7765
*/
7866
enterAnnotationCreationMode() {
67+
let requestId = this._nextRequestId++;
68+
let requestMap = this._requestMap;
69+
70+
// We create a promise here that will be resolved once onDataReturned is called.
71+
let promise = new Promise((resolve, reject) => {
72+
requestMap[requestId] = {resolve: resolve, reject: reject};
73+
});
74+
7975
UIManager.dispatchViewManagerCommand(
8076
findNodeHandle(this.refs.pdfView),
8177
UIManager.RCTPSPDFKitView.Commands.enterAnnotationCreationMode,
82-
[]
78+
[requestId]
8379
);
80+
81+
return promise;
8482
}
8583

8684
/**
8785
* Exits the currently active mode, hiding all active sub-toolbars.
86+
*
87+
* @returns a promise resolving if successful or rejects if an error occurs with and error message
8888
*/
8989
exitCurrentlyActiveMode() {
90+
let requestId = this._nextRequestId++;
91+
let requestMap = this._requestMap;
92+
93+
// We create a promise here that will be resolved once onDataReturned is called.
94+
let promise = new Promise((resolve, reject) => {
95+
requestMap[requestId] = {resolve: resolve, reject: reject};
96+
});
97+
9098
UIManager.dispatchViewManagerCommand(
9199
findNodeHandle(this.refs.pdfView),
92100
UIManager.RCTPSPDFKitView.Commands.exitCurrentlyActiveMode,
93101
[]
94102
);
103+
104+
return promise;
95105
}
96106

97107
/**
98108
* Saves the currently opened document.
109+
*
110+
* @returns a promise resolving if successful or rejects if an error occurs with and error message
99111
*/
100112
saveCurrentDocument() {
113+
let requestId = this._nextRequestId++;
114+
let requestMap = this._requestMap;
115+
116+
// We create a promise here that will be resolved once onDataReturned is called.
117+
let promise = new Promise((resolve, reject) => {
118+
requestMap[requestId] = {resolve: resolve, reject: reject};
119+
});
120+
101121
UIManager.dispatchViewManagerCommand(
102122
findNodeHandle(this.refs.pdfView),
103123
UIManager.RCTPSPDFKitView.Commands.saveCurrentDocument,
104-
[]
124+
[requestId]
105125
);
126+
127+
return promise;
106128
}
107129

108130
/**
@@ -137,17 +159,14 @@ class PSPDFKitView extends React.Component {
137159
* @param annotation InstantJson of the annotation to add with the format of
138160
* https://pspdfkit.com/guides/windows/current/importing-exporting/instant-json/#instant-annotation-json-api
139161
*
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}
162+
* @returns a promise resolving if successful or rejects if an error occurs with and error message
144163
*/
145164
addAnnotation(annotation) {
146165
let requestId = this._nextRequestId++;
147166
let requestMap = this._requestMap;
148167

149168
// We create a promise here that will be resolved once onDataReturned is called.
150-
let promise = new Promise(function (resolve, reject) {
169+
let promise = new Promise((resolve, reject) => {
151170
requestMap[requestId] = {resolve: resolve, reject: reject};
152171
});
153172

samples/Catalog/Catalog.windows.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ class PdfViewListenersScreen extends Component<{}> {
328328

329329
componentWillMount() {
330330
this.props.navigation.setParams({
331-
handleSaveButtonPress: () => this.refs.pdfView.saveCurrentDocument()
331+
handleSaveButtonPress: () => this.refs.pdfView.saveCurrentDocument().then(() => {
332+
alert("Document was saved!");
333+
}).catch(error => {
334+
alert(error);
335+
})
332336
});
333337
}
334338

@@ -340,15 +344,6 @@ class PdfViewListenersScreen extends Component<{}> {
340344
style={styles.pdfView}
341345
// The default file to open.
342346
document="ms-appx:///Assets/pdf/annualReport.pdf"
343-
onDocumentSaved={e => {
344-
alert("Document was saved!");
345-
}}
346-
onDocumentSaveFailed={e => {
347-
alert("Document couldn't be saved: " + e.error);
348-
}}
349-
onAnnotationTapped={e => {
350-
alert("Annotation was tapped\n" + JSON.stringify(e));
351-
}}
352347
onAnnotationsChanged={e => {
353348
alert("Annotations changed\n" + JSON.stringify(e));
354349
}}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PDFViewPage.xaml.cs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public sealed partial class PDFViewPage : Page
2727
private StorageFile _fileToOpen;
2828
private bool _pdfViewInitialised = false;
2929
public readonly PdfView Pdfview;
30-
30+
3131
public PDFViewPage()
3232
{
3333
InitializeComponent();
@@ -70,7 +70,7 @@ internal async Task OpenFileAsync(StorageFile file)
7070
_fileToOpen = file;
7171

7272
// If the PdfView is already initialised we can show the new document.
73-
if(_pdfViewInitialised)
73+
if (_pdfViewInitialised)
7474
{
7575
try
7676
{
@@ -85,27 +85,19 @@ internal async Task OpenFileAsync(StorageFile file)
8585
}
8686
}
8787

88-
internal async Task ExportCurrentDocument()
88+
internal async Task ExportCurrentDocument(int requestId)
8989
{
90-
try
91-
{
92-
// Get the StorageFile
93-
if (_fileToOpen != null)
90+
await RunOperationAndFireEvent(requestId,
91+
async () =>
9492
{
95-
// Save it.
96-
await PDFView.Document.ExportAsync(_fileToOpen);
93+
// Get the StorageFile
94+
if (_fileToOpen != null)
95+
{
96+
// Save it.
97+
await PDFView.Document.ExportAsync(_fileToOpen);
98+
}
9799
}
98-
99-
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
100-
new PdfViewDocumentSavedEvent(this.GetTag())
101-
);
102-
}
103-
catch (Exception e)
104-
{
105-
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
106-
new PdfViewDocumentSaveFailedEvent(this.GetTag(), e.Message)
107-
);
108-
}
100+
);
109101
}
110102

111103
internal async Task GetAnnotations(int requestId, int pageIndex)
@@ -148,7 +140,8 @@ internal async Task SetToolbarItems(int requestId, string toolbarItemsJson)
148140
{
149141
try
150142
{
151-
var toolbarItems = PSPDFKit.UI.ToolbarComponents.Factory.FromJsonArray(JsonArray.Parse(toolbarItemsJson));
143+
var toolbarItems =
144+
PSPDFKit.UI.ToolbarComponents.Factory.FromJsonArray(JsonArray.Parse(toolbarItemsJson));
152145
await Pdfview.Controller.SetToolbarItemsAsync(toolbarItems.ToList());
153146
}
154147
catch (Exception e)
@@ -159,11 +152,29 @@ internal async Task SetToolbarItems(int requestId, string toolbarItemsJson)
159152
}
160153
}
161154

162-
public async Task CreateAnnotation(int requestId, string annotationJsonString)
155+
internal async Task CreateAnnotation(int requestId, string annotationJsonString)
156+
{
157+
await RunOperationAndFireEvent(requestId,
158+
async () =>
159+
{
160+
await Pdfview.Document.CreateAnnotationAsync(
161+
Factory.FromJson(JsonObject.Parse(annotationJsonString)));
162+
}
163+
);
164+
}
165+
166+
internal async Task SetInteractionMode(int requestId, InteractionMode interactionMode)
167+
{
168+
await RunOperationAndFireEvent(requestId,
169+
async () => { await Pdfview.Controller.SetInteractionModeAsync(interactionMode); }
170+
);
171+
}
172+
173+
private async Task RunOperationAndFireEvent(int requestId, Func<Task> operation)
163174
{
164175
try
165176
{
166-
await Pdfview.Document.CreateAnnotationAsync(Factory.FromJson(JsonObject.Parse(annotationJsonString)));
177+
await operation();
167178

168179
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
169180
new PdfViewOperationResult(this.GetTag(), requestId)
@@ -179,7 +190,7 @@ public async Task CreateAnnotation(int requestId, string annotationJsonString)
179190

180191
internal async Task SetPageIndexAsync(int index)
181192
{
182-
await PDFView.Controller.SetCurrentPageIndexAsync(index);
193+
await PDFView.Controller.SetCurrentPageIndexAsync(index);
183194
}
184195

185196
internal void SetShowToolbar(bool showToolbar)
@@ -194,6 +205,7 @@ private async void PDFView_InitializationCompletedHandlerAsync(PdfView sender, P
194205
{
195206
await PDFView.OpenStorageFileAsync(_fileToOpen);
196207
}
208+
197209
_pdfViewInitialised = true;
198210
}
199211

@@ -202,9 +214,9 @@ private void DocumentOnAnnotationsCreated(object sender, IList<IAnnotation> anno
202214
foreach (var annotation in annotationList)
203215
{
204216
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
205-
new PdfViewAnnotationChangedEvent(this.GetTag(),
206-
PdfViewAnnotationChangedEvent.EVENT_TYPE_ADDED, annotation)
207-
);
217+
new PdfViewAnnotationChangedEvent(this.GetTag(),
218+
PdfViewAnnotationChangedEvent.EVENT_TYPE_ADDED, annotation)
219+
);
208220
}
209221
}
210222

@@ -213,9 +225,9 @@ private void DocumentOnAnnotationsUpdated(object sender, IList<IAnnotation> anno
213225
foreach (var annotation in annotationList)
214226
{
215227
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
216-
new PdfViewAnnotationChangedEvent(this.GetTag(),
217-
PdfViewAnnotationChangedEvent.EVENT_TYPE_CHANGED, annotation)
218-
);
228+
new PdfViewAnnotationChangedEvent(this.GetTag(),
229+
PdfViewAnnotationChangedEvent.EVENT_TYPE_CHANGED, annotation)
230+
);
219231
}
220232
}
221233

@@ -224,10 +236,10 @@ private void DocumentOnAnnotationsDeleted(object sender, IList<IAnnotation> anno
224236
foreach (var annotation in annotationList)
225237
{
226238
this.GetReactContext().GetNativeModule<UIManagerModule>().EventDispatcher.DispatchEvent(
227-
new PdfViewAnnotationChangedEvent(this.GetTag(),
228-
PdfViewAnnotationChangedEvent.EVENT_TYPE_REMOVED, annotation)
229-
);
239+
new PdfViewAnnotationChangedEvent(this.GetTag(),
240+
PdfViewAnnotationChangedEvent.EVENT_TYPE_REMOVED, annotation)
241+
);
230242
}
231243
}
232244
}
233-
}
245+
}

windows/ReactNativePSPDFKit/ReactNativePSPDFKit/PSPDFKitViewManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ public override async void ReceiveCommand(PDFViewPage view, int commandId, JArra
9393
switch (commandId)
9494
{
9595
case COMMAND_ENTER_ANNOTATION_CREATION_MODE:
96-
await PdfViewPage.Pdfview.Controller.SetInteractionModeAsync(InteractionMode.Note);
96+
await PdfViewPage.SetInteractionMode(args[0].Value<int>(), InteractionMode.Note);
9797
break;
9898
case COMMAND_EXIT_CURRENTLY_ACTIVE_MODE:
99-
await PdfViewPage.Pdfview.Controller.SetInteractionModeAsync(InteractionMode.None);
99+
await PdfViewPage.SetInteractionMode(args[0].Value<int>(), InteractionMode.None);
100100
break;
101101
case COMMAND_SAVE_CURRENT_DOCUMENT:
102-
await PdfViewPage.ExportCurrentDocument();
102+
await PdfViewPage.ExportCurrentDocument(args[0].Value<int>());
103103
break;
104104
case COMMAND_GET_ANNOTATIONS:
105105
await PdfViewPage.GetAnnotations(args[0].Value<int>(), args[1].Value<int>());

0 commit comments

Comments
 (0)