Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit ad5a88e

Browse files
committed
Added support for saving external documents
1 parent c70ee37 commit ad5a88e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/clients/WebViewerModuleClient.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ class WebViewerModuleClient {
2323
console.info("[Module] Failed to save document.", e);
2424
}
2525
}
26+
async saveFile(fileData: ArrayBuffer): Promise<string | null | undefined> {
27+
try {
28+
const res = await fetch(`/rest/documentstore/v1/documents`, {
29+
method: "POST",
30+
body: fileData
31+
});
32+
return await res.text();
33+
} catch (e) {
34+
console.info("[Module] Failed to save document.", e);
35+
}
36+
37+
return null;
38+
}
2639
}
2740

2841
export default new WebViewerModuleClient();

src/components/PDFViewer.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const PDFViewer: React.FC<InputProps> = props => {
3131
const viewerRef = useRef<HTMLDivElement>(null);
3232
const [wvInstance, setInstance] = useState<null | WebViewerInstance>(null);
3333

34+
let documentLoadCount = 0;
35+
let currentFileId: string | null | undefined | void = null;
36+
3437
// Perform clean-up of WV when unmounted
3538
useEffect(() => {
3639
return () => {
@@ -99,7 +102,7 @@ const PDFViewer: React.FC<InputProps> = props => {
99102
const fileData = await Core.documentViewer.getDocument().getFileData({ xfdfString });
100103

101104
// Send it merged with the document data to REST API to update
102-
const updateTask = WebViewerModuleClient.updateFile(props.fileId || "", fileData);
105+
const updateTask = currentFileId ? WebViewerModuleClient.updateFile(props.fileId || "", fileData) : WebViewerModuleClient.saveFile(fileData);
103106

104107
// Add minimum artificial delay to make it look like work is being done
105108
// Otherwise, requests may complete too fast
@@ -115,12 +118,26 @@ const PDFViewer: React.FC<InputProps> = props => {
115118
// Complete when one of them finish
116119
await Promise.all([uiDelay, updateTask]);
117120

121+
if (!currentFileId) {
122+
const currentId = await updateTask;
123+
currentFileId = currentId;
124+
}
125+
118126
instance.UI.closeElements(["loadingModal"]);
119127
}
120128
});
121129
}
122130
});
123131
});
132+
133+
Core.documentViewer.addEventListener('documentLoaded', () => {
134+
documentLoadCount++;
135+
if (documentLoadCount > 1 && currentFileId) {
136+
currentFileId = null;
137+
} else {
138+
currentFileId = props.fileId;
139+
}
140+
});
124141
});
125142
}, [viewer]);
126143

0 commit comments

Comments
 (0)