@garage-panda/react-pdf-export
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,15 +0,8 @@ | ||
import React, { MutableRefObject, RefObject } from 'react'; | ||
import React, { RefObject } from 'react'; | ||
import { HeadOptions } from '../types'; | ||
import '../css/index.css'; | ||
interface PdfExportProps { | ||
containerRef: { | ||
iframeRef: RefObject<HTMLIFrameElement>; | ||
populateRef: MutableRefObject<() => void>; | ||
}; | ||
className?: string; | ||
showInDom?: boolean; | ||
containerRef: RefObject<HTMLIFrameElement>; | ||
options?: HeadOptions; | ||
lazyLoad?: boolean; | ||
} | ||
declare const PdfExport: React.FC<PdfExportProps>; | ||
export default PdfExport; |
@@ -28,47 +28,11 @@ "use strict"; | ||
var common_utils_1 = require("../utils/common-utils"); | ||
require("../css/index.css"); | ||
var use_await_dom_render_1 = require("@garage-panda/use-await-dom-render"); | ||
var PdfExport = function (_a) { | ||
var containerRef = _a.containerRef, className = _a.className, _b = _a.showInDom, showInDom = _b === void 0 ? true : _b, options = _a.options, _c = _a.lazyLoad, lazyLoad = _c === void 0 ? false : _c, children = _a.children; | ||
var _d = use_await_dom_render_1.useAwaitDomRender(), observer = _d[0], startWait = _d[1]; | ||
var attachObserverListener = function (mountNode, pdfContainer) { | ||
observer.on('load', function () { | ||
observer.removeListeners(); | ||
containerRef.iframeRef.current.contentWindow.print(); | ||
mountNode.head.innerHTML = ''; | ||
mountNode.body.innerHTML = ''; | ||
}); | ||
startWait(pdfContainer); | ||
}; | ||
var populateChildren = function (mountNode) { | ||
var pdfContainer = mountNode.createElement('div'); | ||
mountNode.body.appendChild(pdfContainer); | ||
if (lazyLoad) { | ||
attachObserverListener(mountNode, pdfContainer); | ||
} | ||
react_dom_1.default.render(children, pdfContainer); | ||
}; | ||
var containerRef = _a.containerRef, options = _a.options, children = _a.children; | ||
react_1.useEffect(function () { | ||
if (!lazyLoad) { | ||
if (!containerRef.current) { | ||
return; | ||
} | ||
var mountNode = containerRef.iframeRef.current.contentWindow.document; | ||
containerRef.populateRef.current = function () { | ||
populateChildren(mountNode); | ||
}; | ||
}, [lazyLoad]); | ||
react_1.useEffect(function () { | ||
if (!containerRef.iframeRef.current) { | ||
return; | ||
} | ||
var _a = common_utils_1.mergeOptions(options), includeParentStyles = _a.includeParentStyles, styles = _a.styles, scripts = _a.scripts; | ||
var mountNode = containerRef.iframeRef.current.contentWindow.document; | ||
if (includeParentStyles) { | ||
var parentStyles = document.querySelectorAll("style"); | ||
parentStyles.forEach(function (style) { | ||
var clone = style.cloneNode(true); | ||
mountNode.head.appendChild(clone); | ||
}); | ||
} | ||
styles.forEach(function (styleHref) { | ||
var mergedOptions = common_utils_1.mergeOptions(options); | ||
var mountNode = containerRef.current.contentWindow.document; | ||
mergedOptions.styles.forEach(function (styleHref) { | ||
var linkElement = mountNode.createElement('link'); | ||
@@ -80,3 +44,3 @@ linkElement.type = 'text/css'; | ||
}); | ||
scripts.forEach(function (scriptSrc) { | ||
mergedOptions.scripts.forEach(function (scriptSrc) { | ||
var scriptElement = mountNode.createElement('script'); | ||
@@ -86,12 +50,8 @@ scriptElement.src = scriptSrc; | ||
}); | ||
if (!lazyLoad) { | ||
populateChildren(mountNode); | ||
} | ||
return function () { | ||
mountNode.head.innerHTML = ''; | ||
mountNode.body.innerHTML = ''; | ||
}; | ||
var pdfContainer = mountNode.createElement('div'); | ||
mountNode.body.appendChild(pdfContainer); | ||
react_dom_1.default.render(children, pdfContainer); | ||
}, [options]); | ||
return (react_1.default.createElement("iframe", { ref: containerRef.iframeRef, className: className + " " + (!showInDom ? 'react-pdf-export-hide' : '') })); | ||
return (react_1.default.createElement("iframe", { ref: containerRef, title: 'hahha' })); | ||
}; | ||
exports.default = PdfExport; |
declare const _default: () => { | ||
generatePdf: any; | ||
containerRef: { | ||
iframeRef: any; | ||
populateRef: any; | ||
}; | ||
containerRef: any; | ||
}; | ||
export default _default; |
@@ -5,21 +5,11 @@ "use strict"; | ||
exports.default = (function () { | ||
var iframeRef = react_1.useRef(); | ||
var populateRef = react_1.useRef(null); | ||
var containerRef = react_1.useRef(); | ||
var generatePdf = react_1.useCallback(function () { | ||
if (!iframeRef.current) { | ||
if (!containerRef.current) { | ||
return; | ||
} | ||
; | ||
var isLazyLoad = !!populateRef.current; | ||
if (isLazyLoad) { | ||
populateRef.current(); | ||
} | ||
else { | ||
iframeRef.current.contentWindow.print(); | ||
} | ||
}, [iframeRef]); | ||
return { generatePdf: generatePdf, containerRef: { | ||
iframeRef: iframeRef, | ||
populateRef: populateRef | ||
} }; | ||
containerRef.current.contentWindow.print(); | ||
}, [containerRef]); | ||
return { generatePdf: generatePdf, containerRef: containerRef }; | ||
}); |
export interface HeadOptions { | ||
styles?: string[]; | ||
scripts?: string[]; | ||
includeParentStyles?: boolean; | ||
} |
@@ -5,3 +5,2 @@ import { HeadOptions } from '../types'; | ||
scripts?: string[]; | ||
includeParentStyles?: boolean; | ||
}; |
@@ -18,3 +18,2 @@ "use strict"; | ||
scripts: [], | ||
includeParentStyles: true, | ||
}; | ||
@@ -21,0 +20,0 @@ var mergeOptions = function (options) { |
{ | ||
"name": "@garage-panda/react-pdf-export", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A PDF generator for React applications", | ||
@@ -38,3 +38,15 @@ "main": "lib/index.js", | ||
"@garage-panda/use-await-dom-render": "0.0.5" | ||
}, | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^4.29.0", | ||
"@typescript-eslint/parser": "^4.29.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
"eslint-import-resolver-typescript": "^2.4.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-react": "^7.24.0", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
@@ -73,4 +73,11 @@ # Important | ||
## Support | ||
If you like what you see, feel free to support us! | ||
<a href="https://www.buymeacoffee.com/garage.panda"> | ||
<img src="https://img.buymeacoffee.com/button-api/?text=Buy us a beer&emoji=:beer:&slug=garage.panda&button_colour=FFDD00&font_colour=000000&font_family=Poppins&outline_colour=000000&coffee_colour=ffffff"></a> | ||
## License | ||
[MIT](https://choosealicense.com/licenses/mit/) |
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
82
11889
10
13
138