@react-pdf-viewer/get-file
Advanced tools
Comparing version 2.10.1 to 2.11.0
@@ -69,7 +69,14 @@ 'use strict'; | ||
var downloadFile = function (file, saveAs) { | ||
var blobUrl = typeof file.data === 'string' ? '' : URL.createObjectURL(new Blob([file.data], { type: 'application/pdf' })); | ||
var isChromeIOS = function () { return /iphone|ipod|ipad/i.test(navigator.userAgent) && /CriOS/i.test(navigator.userAgent); }; | ||
var isSafariIOS = function () { return /iphone|ipod|ipad/i.test(navigator.userAgent) && !/CriOS/i.test(navigator.userAgent); }; | ||
var encodeUint8Array = function (data) { | ||
return btoa(Array(data.length) | ||
.fill('') | ||
.map(function (_, i) { return String.fromCharCode(data[i]); }) | ||
.join('')); | ||
}; | ||
var download = function (url, saveAs) { | ||
var link = document.createElement('a'); | ||
link.style.display = 'none'; | ||
link.href = blobUrl || file.name; | ||
link.href = url; | ||
link.setAttribute('download', saveAs); | ||
@@ -79,6 +86,21 @@ document.body.appendChild(link); | ||
document.body.removeChild(link); | ||
}; | ||
var downloadBlob = function (data, saveAs, mimeType) { | ||
var blobUrl = URL.createObjectURL(new Blob([data], { type: mimeType })); | ||
download(blobUrl, saveAs); | ||
if (blobUrl) { | ||
URL.revokeObjectURL(blobUrl); | ||
} | ||
return; | ||
}; | ||
var downloadFile = function (doc, saveAs) { | ||
doc.getData().then(function (data) { | ||
isSafariIOS() | ||
? | ||
downloadBlob(data, saveAs, 'application/octet-stream') | ||
: isChromeIOS() | ||
? download("data:application/pdf;base64," + encodeUint8Array(data), saveAs) | ||
: downloadBlob(data, saveAs, 'application/pdf'); | ||
}); | ||
}; | ||
@@ -88,2 +110,6 @@ var Download = function (_a) { | ||
var _b = React__namespace.useState(store.get('file')), currentFile = _b[0], setCurrentFile = _b[1]; | ||
var _c = React__namespace.useState(store.get('doc')), currentDocument = _c[0], setCurrentDocument = _c[1]; | ||
var handleDocumentChanged = function (doc) { | ||
setCurrentDocument(doc); | ||
}; | ||
var handleFileChanged = function (file) { | ||
@@ -93,4 +119,6 @@ setCurrentFile(file); | ||
React__namespace.useEffect(function () { | ||
store.subscribe('doc', handleDocumentChanged); | ||
store.subscribe('file', handleFileChanged); | ||
return function () { | ||
store.subscribe('doc', handleDocumentChanged); | ||
store.unsubscribe('file', handleFileChanged); | ||
@@ -100,4 +128,4 @@ }; | ||
var download = function () { | ||
if (currentFile) { | ||
downloadFile(currentFile, fileNameGenerator(currentFile)); | ||
if (currentDocument && currentFile) { | ||
downloadFile(currentDocument, fileNameGenerator(currentFile)); | ||
} | ||
@@ -126,5 +154,3 @@ }; | ||
var store = React__namespace.useMemo(function () { return core.createStore({}); }, []); | ||
var defaultFileNameGenerator = function (file) { | ||
return typeof file.data === 'object' ? 'document.pdf' : getFileName(file.name); | ||
}; | ||
var defaultFileNameGenerator = function (file) { return (file.name ? getFileName(file.name) : 'document.pdf'); }; | ||
var DownloadDecorator = function (downloadProps) { return (React__namespace.createElement(Download, __assign({}, downloadProps, { fileNameGenerator: props ? props.fileNameGenerator || defaultFileNameGenerator : defaultFileNameGenerator, store: store }))); }; | ||
@@ -137,5 +163,5 @@ var DownloadButtonDecorator = function () { return (React__namespace.createElement(DownloadDecorator, null, function (props) { return React__namespace.createElement(DownloadButton, __assign({}, props)); })); }; | ||
return { | ||
onViewerStateChange: function (viewerState) { | ||
store.update('file', viewerState.file); | ||
return viewerState; | ||
onDocumentLoad: function (props) { | ||
store.update('doc', props.doc); | ||
store.update('file', props.file); | ||
}, | ||
@@ -142,0 +168,0 @@ Download: DownloadDecorator, |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("@react-pdf-viewer/core");function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var o=n(e),r=function(){return r=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},r.apply(this,arguments)},a=function(){return o.createElement(t.Icon,{size:16},o.createElement("path",{d:"M11.5 19.5L11.5 5"}),o.createElement("path",{d:"M17 14L11.5 19.5 6 14"}),o.createElement("path",{d:"M17.5.5h5a1,1,0,0,1,1,1v21a1,1,0,0,1-1,1h-5"}),o.createElement("path",{d:"M6.5.5h-5a1,1,0,0,0-1,1v21a1,1,0,0,0,1,1h5"}))},l={left:0,top:8},c=function(e){var n=e.onClick,r=o.useContext(t.LocalizationContext).l10n,c=r&&r.download?r.download.download:"Download";return o.createElement(t.Tooltip,{ariaControlsSuffix:"get-file",position:t.Position.BottomCenter,target:o.createElement(t.MinimalButton,{ariaLabel:c,onClick:n},o.createElement(a,null)),content:function(){return c},offset:l})},i=function(e){var t=e.children,n=e.fileNameGenerator,r=e.store,a=o.useState(r.get("file")),l=a[0],i=a[1],u=function(e){i(e)};o.useEffect((function(){return r.subscribe("file",u),function(){r.unsubscribe("file",u)}}),[]);return(t||function(e){return o.createElement(c,{onClick:e.onClick})})({onClick:function(){var e,t,o,r;l&&(e=l,t=n(l),o="string"==typeof e.data?"":URL.createObjectURL(new Blob([e.data],{type:"application/pdf"})),(r=document.createElement("a")).style.display="none",r.href=o||e.name,r.setAttribute("download",t),document.body.appendChild(r),r.click(),document.body.removeChild(r),o&&URL.revokeObjectURL(o))}})},u=function(e){var n=e.onClick,r=o.useContext(t.LocalizationContext).l10n,l=r&&r.download?r.download.download:"Download";return o.createElement(t.MenuItem,{icon:o.createElement(a,null),onClick:n},l)}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("@react-pdf-viewer/core");function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var o=n(e),r=function(){return r=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},r.apply(this,arguments)},i=function(){return o.createElement(t.Icon,{size:16},o.createElement("path",{d:"M11.5 19.5L11.5 5"}),o.createElement("path",{d:"M17 14L11.5 19.5 6 14"}),o.createElement("path",{d:"M17.5.5h5a1,1,0,0,1,1,1v21a1,1,0,0,1-1,1h-5"}),o.createElement("path",{d:"M6.5.5h-5a1,1,0,0,0-1,1v21a1,1,0,0,0,1,1h5"}))},a={left:0,top:8},c=function(e){var n=e.onClick,r=o.useContext(t.LocalizationContext).l10n,c=r&&r.download?r.download.download:"Download";return o.createElement(t.Tooltip,{ariaControlsSuffix:"get-file",position:t.Position.BottomCenter,target:o.createElement(t.MinimalButton,{ariaLabel:c,onClick:n},o.createElement(i,null)),content:function(){return c},offset:a})},u=function(e,t){var n=document.createElement("a");n.style.display="none",n.href=e,n.setAttribute("download",t),document.body.appendChild(n),n.click(),document.body.removeChild(n)},l=function(e,t,n){var o=URL.createObjectURL(new Blob([e],{type:n}));u(o,t),o&&URL.revokeObjectURL(o)},f=function(e,t){e.getData().then((function(e){/iphone|ipod|ipad/i.test(navigator.userAgent)&&!/CriOS/i.test(navigator.userAgent)?l(e,t,"application/octet-stream"):/iphone|ipod|ipad/i.test(navigator.userAgent)&&/CriOS/i.test(navigator.userAgent)?u("data:application/pdf;base64,"+function(e){return btoa(Array(e.length).fill("").map((function(t,n){return String.fromCharCode(e[n])})).join(""))}(e),t):l(e,t,"application/pdf")}))},d=function(e){var t=e.children,n=e.fileNameGenerator,r=e.store,i=o.useState(r.get("file")),a=i[0],u=i[1],l=o.useState(r.get("doc")),d=l[0],s=l[1],p=function(e){s(e)},m=function(e){u(e)};o.useEffect((function(){return r.subscribe("doc",p),r.subscribe("file",m),function(){r.subscribe("doc",p),r.unsubscribe("file",m)}}),[]);return(t||function(e){return o.createElement(c,{onClick:e.onClick})})({onClick:function(){d&&a&&f(d,n(a))}})},s=function(e){var n=e.onClick,r=o.useContext(t.LocalizationContext).l10n,a=r&&r.download?r.download.download:"Download";return o.createElement(t.MenuItem,{icon:o.createElement(i,null),onClick:n},a)}; | ||
/*! ***************************************************************************** | ||
@@ -15,2 +15,2 @@ Copyright (c) Microsoft Corporation. | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */exports.DownloadIcon=a,exports.getFilePlugin=function(e){var n=o.useMemo((function(){return t.createStore({})}),[]),a=function(e){return"object"==typeof e.data?"document.pdf":(t=e.name,(n=t.split("/").pop())?n.split("#")[0].split("?")[0]:t);var t,n},l=function(t){return o.createElement(i,r({},t,{fileNameGenerator:e&&e.fileNameGenerator||a,store:n}))};return{onViewerStateChange:function(e){return n.update("file",e.file),e},Download:l,DownloadButton:function(){return o.createElement(l,null,(function(e){return o.createElement(c,r({},e))}))},DownloadMenuItem:function(e){return o.createElement(l,null,(function(t){return o.createElement(u,{onClick:function(){t.onClick(),e.onClick()}})}))}}}; | ||
***************************************************************************** */exports.DownloadIcon=i,exports.getFilePlugin=function(e){var n=o.useMemo((function(){return t.createStore({})}),[]),i=function(e){return e.name?(t=e.name,(n=t.split("/").pop())?n.split("#")[0].split("?")[0]:t):"document.pdf";var t,n},a=function(t){return o.createElement(d,r({},t,{fileNameGenerator:e&&e.fileNameGenerator||i,store:n}))};return{onDocumentLoad:function(e){n.update("doc",e.doc),n.update("file",e.file)},Download:a,DownloadButton:function(){return o.createElement(a,null,(function(e){return o.createElement(c,r({},e))}))},DownloadMenuItem:function(e){return o.createElement(a,null,(function(t){return o.createElement(s,{onClick:function(){t.onClick(),e.onClick()}})}))}}}; |
{ | ||
"name": "@react-pdf-viewer/get-file", | ||
"version": "2.10.1", | ||
"version": "2.11.0", | ||
"description": "A React component to view a PDF document", | ||
@@ -36,3 +36,3 @@ "license": "https://react-pdf-viewer.dev/license", | ||
"dependencies": { | ||
"@react-pdf-viewer/core": "2.10.1" | ||
"@react-pdf-viewer/core": "2.11.0" | ||
}, | ||
@@ -50,3 +50,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "1a83b6bdf2c509cccfca2c53cb946c4dd9ebffc7" | ||
"gitHead": "5e4074055f48210e89c1fdf5b1cc53d78d56be44" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16234
222
+ Added@react-pdf-viewer/core@2.11.0(transitive)
- Removed@react-pdf-viewer/core@2.10.1(transitive)