Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SaveAsHandler for custom save as behavior #1000

Open
wants to merge 5 commits into
base: 8.11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions i18n/translation-de.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"markAllRead": "Als Gelesen markieren",
"insertPage": "Seite einfügen",
"insertBlankPageAbove": "Leere Seite oben einfügen",
"insertBlankPageBelow": "Leere Seite unten einfügen page",
"insertBlankPageBelow": "Leere Seite unten einfügen",
"pageManipulation": "Seitenmanipulation",
"replace": "Ersetzen",
"setDestination": "Festgelegtes Ziel",
Expand All @@ -88,9 +88,9 @@
"redactPages": "Seiten schwärzen",
"playAudio": "Audio abspielen",
"pauseAudio": "Audiopause",
"selectAll": "Wählen Sie Alle",
"selectAll": "Alle auswählen",
"unselect": "Auswahl aufheben",
"addMark": "Markus hinzufügen",
"addMark": "Markieren",
"viewFile": "Datei ansehen",
"multiReplyAnnotations": "Auf ausgewählte Anmerkungen antworten ({{count}})",
"comparePages": "Seiten vergleichen",
Expand Down Expand Up @@ -317,8 +317,8 @@
"toolbarGroup-Measure": "Messen",
"toolbarGroup-Edit": "Bearbeiten",
"toolbarGroup-EditText": "Text bearbeiten",
"toolbarGroup-FillAndSign": "Füllen und unterschreiben",
"toolbarGroup-Forms": "Formen",
"toolbarGroup-FillAndSign": "Formular",
"toolbarGroup-Forms": "Zeichnen",
"toolbarGroup-Redact": "Schwärzen"
},
"annotationColor": {
Expand Down Expand Up @@ -584,7 +584,7 @@
"backgroundColor": "Hintergrundfarbe"
},
"pageRedactModal": {
"addMark": "Markus hinzufügen",
"addMark": "Markieren",
"pageSelection": "Seitenauswahl",
"current": "Aktuelle Seite",
"specify": "Seiten angeben",
Expand All @@ -599,18 +599,18 @@
"settings": "Einstellungen",
"general": "Allgemein",
"keyboardShortcut": "Tastaturkürzel",
"advancedSetting": "Erweiterte Einstellung",
"advancedSetting": "Erweitert",
"language": "Sprache",
"theme": "Thema",
"darkMode": "Dunkler Modus",
"lightMode": "Lichtmodus",
"darkMode": "Dark-Mode",
"lightMode": "Light-Mode",
"viewing": "Anzeigen",
"disableFadePageNavigationComponent": "Deaktivieren Sie die Seitennavigationskomponente ausblenden",
"disableFadePageNavigationComponent": "Seitennavigationskomponente nicht automatisch ausblenden",
"disableFadePageNavigationComponentDesc": "Lassen Sie die Seitennavigationskomponente immer auf dem Bildschirm. Das Standardverhalten besteht darin, es nach einer bestimmten Zeit der Inaktivität auszublenden.",
"disableNativeScrolling": "Natives Scrollen deaktivieren",
"disableNativeScrollingDesc": "Deaktivieren Sie das native Scrollverhalten mobiler Geräte, wenn es zuvor aktiviert wurde. Beachten Sie, dass das native Scrollverhalten mobiler Geräte standardmäßig deaktiviert ist.",
"annotations": "Anmerkungen",
"disableToolDefaultStyleUpdateFromAnnotationPopup": "Deaktivieren Sie die Aktualisierung des Werkzeug-Standardstils aus dem Anmerkungs-Popup",
"disableToolDefaultStyleUpdateFromAnnotationPopup": "Aktualisierung des Werkzeug-Standardstils aus dem Anmerkungs-Popup deaktivieren",
"disableToolDefaultStyleUpdateFromAnnotationPopupDesc": "Deaktiviert die Synchronisierung von Anmerkungsstilaktualisierungen mit dem zugeordneten Werkzeug, das die Anmerkung erstellt hat. Wenn also der Stil einer Anmerkung geändert wird, werden die Standardstile des Werkzeugs nicht aktualisiert.",
"notesPanel": "Notizenbereich",
"disableNoteSubmissionWithEnter": "Deaktivieren Sie das Senden von Notizen mit der Eingabetaste",
Expand Down Expand Up @@ -962,9 +962,9 @@
},
"redactionPanel": {
"noMarkedRedactions": "Beginnen Sie mit dem Schwärzen, indem Sie Text, Regionen, Seiten markieren oder eine Suche durchführen.",
"redactionSearchPlaceholder": "Hinzufügen nach Schlüsselwortsuche oder Mustern",
"redactionSearchPlaceholder": "Schlüsselwortsuche oder Muster",
"redactionCounter": "Zur Schwärzung markiert",
"clearMarked": "Klar",
"clearMarked": "Abbrechen",
"redactAllMarked": "Alle schwärzen",
"redactionItem": {
"regionRedaction": "Regionsredaktion",
Expand Down
2 changes: 2 additions & 0 deletions src/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Theme from 'constants/theme';
import RedactionSearchPatterns from 'constants/redactionSearchPatterns';
import { languageEnum } from 'constants/languages';
import addSearchListener from './addSearchListener';
import setSaveAsHandler from './setSaveAsHandler';
import addSortStrategy from './addSortStrategy';
import annotationPopup from './annotationPopup';
import closeDocument from './closeDocument';
Expand Down Expand Up @@ -278,6 +279,7 @@ export default (store) => {
Theme,
RedactionSearchPatterns,
addSearchListener,
setSaveAsHandler,
addSortStrategy: addSortStrategy(store),
annotationPopup: annotationPopup(store),
closeDocument: closeDocument(store),
Expand Down
26 changes: 26 additions & 0 deletions src/apis/setSaveAsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Set save as handler that will be triggered in case of a save action instead of the default method.
* @method UI.setSaveAsHandler
* @param {UI.saveAsHandler} saveAsHandler Callback function that will be triggered when download started
* @example
WebViewer(...)
.then(function(instance) {
function onDownload(data, filename) {
console.log(filename);
};

instance.UI.setSaveAsHandler(onDownload);
});
*/
/**
* Callback that gets passed to {@link UI.setSaveAsHandler setSaveAsHandler}.
* @callback UI.saveAsHandler
* @param {Blob|File} data data
* @param {string} filename filename
*/

import { setSaveAsHandler as saveAsHandlerHelper } from 'helpers/saveAs';

export default function setSaveAsHandler(handler) {
saveAsHandlerHelper(handler);
}
8 changes: 7 additions & 1 deletion src/components/FileAttachmentPanel/FileAttachmentPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { getFileAttachments } from 'helpers/getFileAttachments';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import Icon from 'components/Icon';
import core from 'core';
import './FileAttachmentPanel.scss';
Expand Down Expand Up @@ -53,7 +54,12 @@ const FileAttachmentPanel = () => {
{fileAttachments.embeddedFiles.map((file, idx) => renderAttachment(
file.filename,
() => {
saveAs(file.blob, file.filename);
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file.blob, file.filename);
} else {
saveAs(file.blob, file.filename);
}
},
`embeddedFile_${idx}`,
),
Expand Down
8 changes: 7 additions & 1 deletion src/components/ReplyAttachmentList/ReplyAttachmentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import selectors from 'selectors';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import Button from 'components/Button';
import Icon from 'components/Icon';
import Tooltip from 'components/Tooltip';
Expand Down Expand Up @@ -67,7 +68,12 @@ const ReplyAttachmentList = ({ files, isEditing, fileDeleted }) => {
e.stopPropagation();

const fileData = file.url ? file.url : await decompressFileContent(file);
saveAs(fileData, file.name);
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(fileData, file.name);
} else {
saveAs(fileData, file.name);
}
};

return (
Expand Down
8 changes: 7 additions & 1 deletion src/event-listeners/onFileAttachmentDataAvailable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';

export default () => (fileMeta) => {
const { fileData, fileName } = fileMeta;
saveAs(fileData, fileName);
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(fileData, fileName);
} else {
saveAs(fileData, fileName);
}
};
8 changes: 7 additions & 1 deletion src/helpers/downloadPdf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import core from 'core';
import { isIE } from 'helpers/device';
import fireEvent from 'helpers/fireEvent';
Expand Down Expand Up @@ -238,7 +239,12 @@ export default async (dispatch, options = {}, documentViewerKey = 1) => {
} else {
file = new File([arr], downloadName, { type: downloadType });
}
saveAs(file, downloadName);
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file, downloadName);
} else {
saveAs(file, downloadName);
}
dispatch(actions.closeElement('loadingModal'));
fireEvent(Events.FINISHED_SAVING_PDF);
fireEvent(Events.FILE_DOWNLOADED);
Expand Down
15 changes: 13 additions & 2 deletions src/helpers/pageManipulationFunctions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import extractPagesWithAnnotations from 'helpers/extractPagesWithAnnotations';
import core from 'core';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import actions from 'actions';
import i18next from 'i18next';
import { workerTypes } from 'constants/types';
Expand Down Expand Up @@ -78,12 +79,22 @@ const extractPages = (pageNumbers, dispatch) => {
title,
confirmBtnText,
onConfirm: () => extractPagesWithAnnotations(pageNumbers).then((file) => {
saveAs(file, 'extractedDocument.pdf');
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file, 'extractedDocument.pdf');
} else {
saveAs(file, 'extractedDocument.pdf');
}
}),
secondaryBtnText,
onSecondary: () => {
extractPagesWithAnnotations(pageNumbers).then((file) => {
saveAs(file, 'extractedDocument.pdf');
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(file, 'extractedDocument.pdf');
} else {
saveAs(file, 'extractedDocument.pdf');
}
core.removePages(pageNumbers).then(() => {
dispatch(actions.setSelectedPageThumbnails([]));
});
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/saveAs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let saveAsHandler = null;

export function setSaveAsHandler(handler) {
saveAsHandler = handler;
}

export function clearSaveAsHandler() {
saveAsHandler = null;
}

export function getSaveAsHandler() {
return saveAsHandler;
}
4 changes: 4 additions & 0 deletions src/redux/actions/internalActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ export const removeSearchListener = (func) => ({
type: 'REMOVE_SEARCH_LISTENER',
payload: { func },
});
export const setSaveAsHandler = (func) => ({
type: 'ADD_SAVE_AS_HANDLER',
payload: { func },
});
export const setSearchValue = (value) => ({
type: 'SET_SEARCH_VALUE',
payload: { value },
Expand Down