-
Notifications
You must be signed in to change notification settings - Fork 376
Add SaveAsHandler for custom save as behavior #1000
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
base: 8.11
Are you sure you want to change the base?
Changes from 3 commits
907ca6a
7c8244d
ee8f707
4c2f870
07a553f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -53,7 +54,12 @@ const FileAttachmentPanel = () => { | |
{fileAttachments.embeddedFiles.map((file, idx) => renderAttachment( | ||
file.filename, | ||
() => { | ||
saveAs(file.blob, file.filename); | ||
if (getSaveAsHandler() !== null) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(file.blob, file.filename); | ||
} else { | ||
saveAs(file.blob, file.filename); | ||
} | ||
}, | ||
`embeddedFile_${idx}`, | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(fileData, file.name); | ||
} else { | ||
saveAs(fileData, file.name); | ||
} | ||
}; | ||
|
||
return ( | ||
|
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) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(fileData, fileName); | ||
} else { | ||
saveAs(fileData, fileName); | ||
} | ||
}; |
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'; | ||
|
@@ -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) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(file, downloadName); | ||
} else { | ||
saveAs(file, downloadName); | ||
} | ||
dispatch(actions.closeElement('loadingModal')); | ||
fireEvent(Events.FINISHED_SAVING_PDF); | ||
fireEvent(Events.FILE_DOWNLOADED); | ||
|
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'; | ||
|
@@ -78,12 +79,22 @@ const extractPages = (pageNumbers, dispatch) => { | |
title, | ||
confirmBtnText, | ||
onConfirm: () => extractPagesWithAnnotations(pageNumbers).then((file) => { | ||
saveAs(file, 'extractedDocument.pdf'); | ||
if (getSaveAsHandler() !== null) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(file, 'extractedDocument.pdf'); | ||
} else { | ||
saveAs(file, 'extractedDocument.pdf'); | ||
} | ||
}), | ||
secondaryBtnText, | ||
onSecondary: () => { | ||
extractPagesWithAnnotations(pageNumbers).then((file) => { | ||
saveAs(file, 'extractedDocument.pdf'); | ||
if (getSaveAsHandler() !== null) { | ||
var handler = getSaveAsHandler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be a const here instead of a var |
||
handler(file, 'extractedDocument.pdf'); | ||
} else { | ||
saveAs(file, 'extractedDocument.pdf'); | ||
} | ||
core.removePages(pageNumbers).then(() => { | ||
dispatch(actions.setSelectedPageThumbnails([])); | ||
}); | ||
|
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have investigated the issue causing the checkboxes to go off-centre. We'll be implementing a fix for this soon. This particular CSS selector might affect other inputs so it would be best to leave this out for now.