@syncfusion/ej2-pdfviewer
Version:
Essential JS 2 PDF viewer Component
142 lines (141 loc) • 5.59 kB
JavaScript
import { isNullOrUndefined } from '@syncfusion/ej2-base';
/**
* @private
* Helper function to restore organize state and metadata
* @param {any} context - The context object
* @param {string} fileName - The file name
* @param {string} downloadFileName - The download file name
* @param {string} jsonDocumentId - The JSON document ID
* @returns {void}
*/
function restoreOrganizeState(context, fileName, downloadFileName, jsonDocumentId) {
context.showOrganizeLoadingIndicator(false);
context.organizeDialog.hide();
context.undoOrganizeCollection = [];
context.redoOrganizeCollection = [];
context.pdfViewer.fileName = fileName;
if (!isNullOrUndefined(downloadFileName)) {
context.pdfViewer.downloadFileName = downloadFileName;
}
else {
context.pdfViewer.downloadFileName = fileName;
}
context.pdfViewerBase.jsonDocumentId = jsonDocumentId;
context.isOrganizeWindowOpen = false;
context.pdfViewer.isPageOrganizerOpen = false;
}
/**
* @private
* Helper function to handle save-as download
* @param {any} context - The context object
* @param {string} fileName - The file name
* @param {any} data - The PDF data
* @param {any} temp - Temporary organize pages collection
* @returns {void}
*/
function handleSaveAsDownload(context, fileName, data, temp) {
var canDownload = context.pdfViewer.firePageOrganizerSaveAsEventArgs(fileName, data);
if (canDownload) {
context.pdfViewerBase.fileDownload(data, context.pdfViewerBase, true);
context.organizePagesCollection = JSON.parse(JSON.stringify(temp));
}
}
/**
* @private
* @returns { void }
*/
export function onSaveClicked() {
var _this = this;
this.isSkipRevert = true;
this.showOrganizeLoadingIndicator(true);
if ((JSON.stringify(this.tempOrganizePagesCollection) !== JSON.stringify(this.organizePagesCollection)) ||
this.isDocumentModified) {
this.updateOrganizePageCollection();
this.totalCheckedCount = 0;
this.isDocumentModified = true;
var fileName_1 = this.pdfViewer.fileName;
var downloadFileName_1 = this.pdfViewer.downloadFileName;
var jsonDocumentId_1 = this.pdfViewerBase.jsonDocumentId;
if (this.pdfViewerBase.clientSideRendering) {
this.pdfViewerBase.isOrganizePageSaveAction = true;
this.pdfViewer.saveAsBlob().then(function (data) {
_this.pdfViewerBase.isOrganizePageSaveAction = false;
if (!isNullOrUndefined(data) && data.length > 0) {
restoreOrganizeState(_this, fileName_1, downloadFileName_1, jsonDocumentId_1);
_this.pdfViewer.loadDocInternally(data, null, false);
_this.pdfViewerBase.updateDocumentEditedProperty(true);
}
}).catch(function (error) {
_this.pdfViewerBase.isOrganizePageSaveAction = false;
_this.showOrganizeLoadingIndicator(false);
console.error('Error saving organized PDF:', error);
});
}
else {
var pdfBlob_1;
this.pdfViewer.saveAsBlob().then(function (blob) {
pdfBlob_1 = blob;
_this.pdfViewerBase.blobToBase64(pdfBlob_1).then(function (base64) {
if (!isNullOrUndefined(base64) && base64 !== '') {
restoreOrganizeState(_this, fileName_1, downloadFileName_1, jsonDocumentId_1);
_this.pdfViewer.loadDocInternally(base64, null, false);
_this.pdfViewerBase.updateDocumentEditedProperty(true);
}
});
});
}
}
else {
this.showOrganizeLoadingIndicator(false);
this.organizeDialog.hide();
this.undoOrganizeCollection = [];
this.redoOrganizeCollection = [];
this.isOrganizeWindowOpen = false;
this.pdfViewer.isPageOrganizerOpen = false;
}
}
/**
* @private
* @returns { void }
*/
export function onSaveasClicked() {
var _this = this;
if (JSON.stringify(this.tempOrganizePagesCollection) !== JSON.stringify(this.organizePagesCollection)) {
this.updateOrganizePageActions();
}
var fileName = this.pdfViewer.fileName;
var temp = JSON.parse(JSON.stringify(this.organizePagesCollection));
if (this.pdfViewerBase.clientSideRendering) {
this.pdfViewerBase.isOrganizePageSaveAction = true;
this.pdfViewer.saveAsBlob().then(function (data) {
_this.pdfViewerBase.isOrganizePageSaveAction = false;
if (!isNullOrUndefined(data) && data.length > 0) {
handleSaveAsDownload(_this, fileName, data, temp);
}
}).catch(function (error) {
_this.pdfViewerBase.isOrganizePageSaveAction = false;
console.error('Error saving organized PDF as file:', error);
});
}
else {
var pdfBlob_2;
this.pdfViewer.saveAsBlob().then(function (blob) {
pdfBlob_2 = blob;
_this.pdfViewerBase.blobToBase64(pdfBlob_2).then(function (base64) {
if (!isNullOrUndefined(base64) && base64 !== '') {
handleSaveAsDownload(_this, fileName, base64, temp);
}
});
});
}
}
/**
* @private
* @returns { void }
*/
export function updateOrganizePageActions() {
this.updateOrganizePageCollection();
this.totalCheckedCount = 0;
this.isDocumentModified = true;
this.pdfViewerBase.updateDocumentEditedProperty(true);
}