From e2062b98b16d4d705b3053ddb9c1b65fdace1881 Mon Sep 17 00:00:00 2001 From: laercioacj Date: Thu, 16 Mar 2023 10:39:13 -0300 Subject: [PATCH 1/5] update to typeescrip --- package.json | 7 ++++- src/index.d.ts | 4 +-- src/{index.js => index.ts} | 0 src/js/{browser.js => browser.ts} | 2 +- src/js/{functions.js => functions.ts} | 6 ++--- src/js/{html.js => html.ts} | 0 src/js/{image.js => image.ts} | 0 src/js/{init.js => init.ts} | 2 +- src/js/{json.js => json.ts} | 0 src/js/{modal.js => modal.ts} | 0 src/js/{pdf.js => pdf.ts} | 8 +++--- src/js/{print.js => print.ts} | 8 +++--- src/js/{raw-html.js => raw-html.ts} | 0 tsconfig.json | 39 +++++++++++++++++++++++++++ webpack.config.js | 2 +- 15 files changed, 61 insertions(+), 17 deletions(-) rename src/{index.js => index.ts} (100%) rename src/js/{browser.js => browser.ts} (95%) rename src/js/{functions.js => functions.ts} (95%) rename src/js/{html.js => html.ts} (100%) rename src/js/{image.js => image.ts} (100%) rename src/js/{init.js => init.ts} (99%) rename src/js/{json.js => json.ts} (100%) rename src/js/{modal.js => modal.ts} (100%) rename src/js/{pdf.js => pdf.ts} (86%) rename src/js/{print.js => print.ts} (96%) rename src/js/{raw-html.js => raw-html.ts} (100%) create mode 100644 tsconfig.json diff --git a/package.json b/package.json index 225fb7d..dc45a3e 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,8 @@ "sass-loader": "10.0.2", "standard": "14.3.4", "terser-webpack-plugin": "4.1.0", + "ts-loader": "^9.4.2", + "typescript": "^3.9.10", "webpack": "4.44.1", "webpack-cli": "3.3.12", "webpack-dev-server": "3.11.0" @@ -56,5 +58,8 @@ "printjs", "print.js", "print-js" - ] + ], + "dependencies": { + "source-map-loader": "^4.0.1" + } } diff --git a/src/index.d.ts b/src/index.d.ts index 5c5d325..51ad2c2 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -5,7 +5,7 @@ declare namespace printJS { type PrintTypes = 'pdf' | 'html' | 'image' | 'json' | 'raw-html'; interface Configuration { - printable: any; + printable: string; fallbackPrintable?: string; type?: PrintTypes; documentTitle?: string; @@ -45,4 +45,4 @@ declare namespace printJS { } } -export = printJS; +export = printJS; \ No newline at end of file diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts diff --git a/src/js/browser.js b/src/js/browser.ts similarity index 95% rename from src/js/browser.js rename to src/js/browser.ts index f600929..81150dc 100644 --- a/src/js/browser.js +++ b/src/js/browser.ts @@ -3,7 +3,7 @@ const Browser = { isFirefox: () => { return typeof InstallTrigger !== 'undefined' }, - getFirefoxMajorVersion: (userAgent) => { + getFirefoxMajorVersion: (userAgent?) => { userAgent = userAgent || navigator.userAgent const firefoxVersionRegex = /firefox\/(\S+)/ const match = userAgent.toLowerCase().match(firefoxVersionRegex) diff --git a/src/js/functions.js b/src/js/functions.ts similarity index 95% rename from src/js/functions.js rename to src/js/functions.ts index e8548f4..ed70c3f 100644 --- a/src/js/functions.js +++ b/src/js/functions.ts @@ -64,17 +64,17 @@ export function addHeader (printElement, params) { export function addFooter (printElement, params) { // Create the footer container div - let footerContainer = document.createElement('div') + const footerContainer = document.createElement('div') // Check if the footer is text or raw html if (isRawHTML(params.footer)) { footerContainer.innerHTML = params.footer } else { // Create footer element - let footerElement = document.createElement('h1') + const footerElement = document.createElement('h1') // Create footer text node - let footerNode = document.createTextNode(params.footer) + const footerNode = document.createTextNode(params.footer) // Build and style footerElement.appendChild(footerNode) diff --git a/src/js/html.js b/src/js/html.ts similarity index 100% rename from src/js/html.js rename to src/js/html.ts diff --git a/src/js/image.js b/src/js/image.ts similarity index 100% rename from src/js/image.js rename to src/js/image.ts diff --git a/src/js/init.js b/src/js/init.ts similarity index 99% rename from src/js/init.js rename to src/js/init.ts index 8d2515e..3d222e4 100644 --- a/src/js/init.js +++ b/src/js/init.ts @@ -140,7 +140,7 @@ export default { if (Browser.isIE()) { try { console.info('Print.js doesn\'t support PDF printing in Internet Explorer.') - const win = window.open(params.fallbackPrintable, '_blank') + const win = window.open(params.fallbackPrintable, '_blank') as Window win.focus() params.onIncompatibleBrowser() } catch (error) { diff --git a/src/js/json.js b/src/js/json.ts similarity index 100% rename from src/js/json.js rename to src/js/json.ts diff --git a/src/js/modal.js b/src/js/modal.ts similarity index 100% rename from src/js/modal.js rename to src/js/modal.ts diff --git a/src/js/pdf.js b/src/js/pdf.ts similarity index 86% rename from src/js/pdf.js rename to src/js/pdf.ts index 9dc420b..cd43829 100644 --- a/src/js/pdf.js +++ b/src/js/pdf.ts @@ -5,10 +5,10 @@ export default { print: (params, printFrame) => { // Check if we have base64 data if (params.base64) { - if (params.printable.indexOf(',') !== -1) { - //If pdf base64 start with `data:application/pdf;base64,`,Excute the atob function will throw an error.So we get the content after `,` - params.printable = params.printable.split(',')[1]; - } + if (params.printable.indexOf(',') !== -1) { + // If pdf base64 start with `data:application/pdf;base64,`,Excute the atob function will throw an error.So we get the content after `,` + params.printable = params.printable.split(',')[1] + } const bytesArray = Uint8Array.from(atob(params.printable), c => c.charCodeAt(0)) createBlobAndPrint(params, printFrame, bytesArray) return diff --git a/src/js/print.js b/src/js/print.ts similarity index 96% rename from src/js/print.js rename to src/js/print.ts index 40f9629..b27b7d0 100644 --- a/src/js/print.js +++ b/src/js/print.ts @@ -59,15 +59,15 @@ function performPrint (iframeElement, params) { try { iframeElement.contentWindow.document.execCommand('print', false, null) } catch (e) { - setTimeout(function(){ + setTimeout(function () { iframeElement.contentWindow.print() - },1000) + }, 1000) } } else { // Other browsers - setTimeout(function(){ + setTimeout(function () { iframeElement.contentWindow.print() - },1000) + }, 1000) } } catch (error) { params.onError(error) diff --git a/src/js/raw-html.js b/src/js/raw-html.ts similarity index 100% rename from src/js/raw-html.js rename to src/js/raw-html.ts diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..e8ad32e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,39 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "esnext", + "strict": true, + "jsx": "preserve", + "importHelpers": true, + "moduleResolution": "node", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "sourceMap": true, + "baseUrl": ".", + "allowJs": true, + "experimentalDecorators": true, + "noImplicitAny": false, + "types": [ + "webpack-env", + "jest" + ], + "paths": { + "@/*": [ + "src/*" + ] + }, + "lib": [ + "esnext", + "dom", + "dom.iterable", + "scripthost" + ] + }, + "include": [ + "src/**/*.ts", + "tests/**/*.ts" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/webpack.config.js b/webpack.config.js index a719b37..4c6d153 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,7 +7,7 @@ module.exports = { mode: 'development', devtool: 'source-map', entry: [ - './src/index.js' + './src/index.ts' ], output: { library: 'printJS', From 1790697ac4ff962bb0584c80d94582ce3f961b6e Mon Sep 17 00:00:00 2001 From: laercioacj Date: Sat, 18 Mar 2023 14:23:12 -0300 Subject: [PATCH 2/5] fix unit tests --- jest.config.js | 6 +++++ package.json | 2 ++ src/js/browser.ts | 2 +- src/js/functions.ts | 4 ++-- src/js/html.ts | 4 ++-- src/js/image.ts | 6 ++--- src/js/init.ts | 46 ++++++++++++++++++++++--------------- src/js/json.ts | 4 ++-- src/js/pdf.ts | 4 ++-- src/js/print.ts | 4 ++-- src/js/raw-html.ts | 4 ++-- test/unit/browser.spec.js | 2 +- test/unit/functions.spec.js | 2 +- test/unit/html.spec.js | 2 +- test/unit/image.spec.js | 2 +- test/unit/init.spec.js | 2 +- test/unit/json.spec.js | 2 +- test/unit/modal.spec.js | 2 +- test/unit/pdf.spec.js | 2 +- test/unit/print.spec.js | 2 +- webpack.config.js | 5 +++- 21 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 jest.config.js diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..c6873e5 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + transform: { '^.+\\.ts?$': 'ts-jest' }, + testEnvironment: 'node', + testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] +} diff --git a/package.json b/package.json index dc45a3e..13d3047 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,13 @@ "devDependencies": { "@babel/core": "7.11.6", "@babel/preset-env": "7.11.5", + "@types/jest": "^29.5.0", "babel-loader": "8.1.0", "coveralls": "3.1.0", "css-loader": "4.2.2", "istanbul-instrumenter-loader": "3.0.1", "jasmine-core": "3.6.0", + "jest": "^29.5.0", "karma": "5.2.1", "karma-chrome-launcher": "3.1.0", "karma-coverage": "2.0.3", diff --git a/src/js/browser.ts b/src/js/browser.ts index 81150dc..f600929 100644 --- a/src/js/browser.ts +++ b/src/js/browser.ts @@ -3,7 +3,7 @@ const Browser = { isFirefox: () => { return typeof InstallTrigger !== 'undefined' }, - getFirefoxMajorVersion: (userAgent?) => { + getFirefoxMajorVersion: (userAgent) => { userAgent = userAgent || navigator.userAgent const firefoxVersionRegex = /firefox\/(\S+)/ const match = userAgent.toLowerCase().match(firefoxVersionRegex) diff --git a/src/js/functions.ts b/src/js/functions.ts index ed70c3f..2f543a5 100644 --- a/src/js/functions.ts +++ b/src/js/functions.ts @@ -1,5 +1,5 @@ -import Modal from './modal' -import Browser from './browser' +import Modal from './modal.ts' +import Browser from './browser.ts' export function addWrapper (htmlData, params) { const bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;' diff --git a/src/js/html.ts b/src/js/html.ts index 559166a..08e51c1 100644 --- a/src/js/html.ts +++ b/src/js/html.ts @@ -1,5 +1,5 @@ -import { collectStyles, addHeader, addFooter } from './functions' -import Print from './print' +import { collectStyles, addHeader, addFooter } from './functions.ts' +import Print from './print.ts' export default { print: (params, printFrame) => { diff --git a/src/js/image.ts b/src/js/image.ts index 4277fba..3b71b3f 100644 --- a/src/js/image.ts +++ b/src/js/image.ts @@ -1,6 +1,6 @@ -import { addHeader, addFooter } from './functions' -import Print from './print' -import Browser from './browser' +import { addHeader, addFooter } from './functions.ts' +import Print from './print.ts' +import Browser from './browser.ts' export default { print: (params, printFrame) => { diff --git a/src/js/init.ts b/src/js/init.ts index 3d222e4..369852b 100644 --- a/src/js/init.ts +++ b/src/js/init.ts @@ -1,20 +1,20 @@ 'use strict' -import Browser from './browser' -import Modal from './modal' -import Pdf from './pdf' -import Html from './html' -import RawHtml from './raw-html' -import Image from './image' -import Json from './json' +import Browser from './browser.ts' +import Modal from './modal.ts' +import Pdf from './pdf.ts' +import Html from './html.ts' +import RawHtml from './raw-html.ts' +import Image from './image.ts' +import Json from './json.ts' const printTypes = ['pdf', 'html', 'image', 'json', 'raw-html'] export default { init () { const params = { - printable: null, - fallbackPrintable: null, + printable: '', + fallbackPrintable: '', type: 'pdf', header: null, headerStyle: 'font-weight: 300;', @@ -26,8 +26,8 @@ export default { gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;', showModal: false, onError: (error) => { throw error }, - onLoadingStart: null, - onLoadingEnd: null, + onLoadingStart: () => {}, + onLoadingEnd: () => {}, onPrintDialogClose: () => {}, onIncompatibleBrowser: () => {}, modalMessage: 'Retrieving Document...', @@ -39,8 +39,8 @@ export default { targetStyles: ['border', 'box', 'break', 'text-decoration'], ignoreElements: [], repeatTableHeader: true, - css: null, - style: null, + css: [], + style: '', scanStyles: true, base64: false, @@ -84,7 +84,7 @@ export default { if (!params.printable) throw new Error('Missing printable information.') // Validate type - if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) { + if (!params.type || (typeof params.type !== 'string') || printTypes.indexOf(params.type.toLowerCase()) === -1) { throw new Error('Invalid print type. Available types are: pdf, html, image and json.') } @@ -97,7 +97,9 @@ export default { // To prevent duplication and issues, remove any used printFrame from the DOM const usedFrame = document.getElementById(params.frameId) - if (usedFrame) usedFrame.parentNode.removeChild(usedFrame) + if (usedFrame) { + usedFrame.parentNode.removeChild(usedFrame) + } // Create a new iframe for the print job const printFrame = document.createElement('iframe') @@ -122,7 +124,9 @@ export default { // Attach css files if (params.css) { // Add support for single file - if (!Array.isArray(params.css)) params.css = [params.css] + if (!Array.isArray(params.css)) { + params.css = [params.css] + } // Create link tags for each css file params.css.forEach(file => { @@ -140,15 +144,19 @@ export default { if (Browser.isIE()) { try { console.info('Print.js doesn\'t support PDF printing in Internet Explorer.') - const win = window.open(params.fallbackPrintable, '_blank') as Window + const win = window.open(params.fallbackPrintable, '_blank') win.focus() params.onIncompatibleBrowser() } catch (error) { params.onError(error) } finally { // Make sure there is no loading modal opened - if (params.showModal) Modal.close() - if (params.onLoadingEnd) params.onLoadingEnd() + if (params.showModal) { + Modal.close() + } + if (params.onLoadingEnd) { + params.onLoadingEnd() + } } } else { Pdf.print(params, printFrame) diff --git a/src/js/json.ts b/src/js/json.ts index 36e072c..93a178f 100644 --- a/src/js/json.ts +++ b/src/js/json.ts @@ -1,5 +1,5 @@ -import { capitalizePrint, addHeader, addFooter } from './functions' -import Print from './print' +import { capitalizePrint, addHeader, addFooter } from './functions.ts' +import Print from './print.ts' export default { print: (params, printFrame) => { diff --git a/src/js/pdf.ts b/src/js/pdf.ts index cd43829..fdd01fc 100644 --- a/src/js/pdf.ts +++ b/src/js/pdf.ts @@ -1,5 +1,5 @@ -import Print from './print' -import { cleanUp } from './functions' +import Print from './print.ts' +import { cleanUp } from './functions.ts' export default { print: (params, printFrame) => { diff --git a/src/js/print.ts b/src/js/print.ts index b27b7d0..c3e8094 100644 --- a/src/js/print.ts +++ b/src/js/print.ts @@ -1,5 +1,5 @@ -import Browser from './browser' -import { cleanUp } from './functions' +import Browser from './browser.ts' +import { cleanUp } from './functions.ts' const Print = { send: (params, printFrame) => { diff --git a/src/js/raw-html.ts b/src/js/raw-html.ts index 71ea651..88deab0 100644 --- a/src/js/raw-html.ts +++ b/src/js/raw-html.ts @@ -1,5 +1,5 @@ -import Print from './print' -import { addHeader, addFooter } from './functions' +import Print from './print.ts' +import { addHeader, addFooter } from './functions.ts' export default { print: (params, printFrame) => { diff --git a/test/unit/browser.spec.js b/test/unit/browser.spec.js index 342a576..6342d1b 100644 --- a/test/unit/browser.spec.js +++ b/test/unit/browser.spec.js @@ -1,4 +1,4 @@ -import Browser from '../../src/js/browser' +import Browser from '../../src/js/browser.ts' describe('Browser', () => { it('has a function named isFirefox that returns a boolean value', () => { diff --git a/test/unit/functions.spec.js b/test/unit/functions.spec.js index 10ac6af..f868d2a 100644 --- a/test/unit/functions.spec.js +++ b/test/unit/functions.spec.js @@ -1,4 +1,4 @@ -import { addWrapper, capitalizePrint, isRawHTML } from '../../src/js/functions' +import { addWrapper, capitalizePrint, isRawHTML } from '../../src/js/functions.ts' describe('addWrapper()', () => { it('add a div wrapper to a raw html', () => { diff --git a/test/unit/html.spec.js b/test/unit/html.spec.js index 561d8b4..4217ba2 100644 --- a/test/unit/html.spec.js +++ b/test/unit/html.spec.js @@ -1,4 +1,4 @@ -import Html from '../../src/js/html' +import Html from '../../src/js/html.ts' describe('Html', () => { it('has a method named print', () => { diff --git a/test/unit/image.spec.js b/test/unit/image.spec.js index cc1cbd5..2a14831 100644 --- a/test/unit/image.spec.js +++ b/test/unit/image.spec.js @@ -1,4 +1,4 @@ -import Image from '../../src/js/image' +import Image from '../../src/js/image.ts' describe('Image', () => { it('has a method named print', () => { diff --git a/test/unit/init.spec.js b/test/unit/init.spec.js index 043808b..7baf94e 100644 --- a/test/unit/init.spec.js +++ b/test/unit/init.spec.js @@ -1,4 +1,4 @@ -import Init from '../../src/js/init' +import Init from '../../src/js/init.ts' describe('Init Module', () => { it('has a function named init', () => { diff --git a/test/unit/json.spec.js b/test/unit/json.spec.js index 4c5ffa3..de385f0 100644 --- a/test/unit/json.spec.js +++ b/test/unit/json.spec.js @@ -1,4 +1,4 @@ -import Json from '../../src/js/json' +import Json from '../../src/js/json.ts' describe('Json', () => { it('has a method named print', () => { diff --git a/test/unit/modal.spec.js b/test/unit/modal.spec.js index 18216cb..61d858d 100644 --- a/test/unit/modal.spec.js +++ b/test/unit/modal.spec.js @@ -1,4 +1,4 @@ -import Modal from '../../src/js/modal' +import Modal from '../../src/js/modal.ts' describe('Modal', () => { it('has a method named show', () => { diff --git a/test/unit/pdf.spec.js b/test/unit/pdf.spec.js index 67ece89..301dc96 100644 --- a/test/unit/pdf.spec.js +++ b/test/unit/pdf.spec.js @@ -1,4 +1,4 @@ -import Pdf from '../../src/js/pdf' +import Pdf from '../../src/js/pdf.ts' describe('Pdf', () => { it('has a method named print', () => { diff --git a/test/unit/print.spec.js b/test/unit/print.spec.js index 44c9b35..08aa82f 100644 --- a/test/unit/print.spec.js +++ b/test/unit/print.spec.js @@ -1,4 +1,4 @@ -import Print from '../../src/js/print' +import Print from '../../src/js/print.ts' describe('Print', () => { it('has a method named send', () => { diff --git a/webpack.config.js b/webpack.config.js index 4c6d153..41eae9a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -9,6 +9,9 @@ module.exports = { entry: [ './src/index.ts' ], + resolve: { + extensions: ['.ts', '.js', '.json'] + }, output: { library: 'printJS', libraryTarget: 'umd', @@ -23,7 +26,7 @@ module.exports = { test: /\.js$/, exclude: /node_modules/, use: { - loader: 'babel-loader' + loader: 'ts-loader' } }, // TODO: Configure istanbul to interpret how webpack bundles files From 9e1df3657bc2de4e86ca64e5a3af9aa8b8f313d3 Mon Sep 17 00:00:00 2001 From: laercioacj Date: Sat, 18 Mar 2023 18:46:40 -0300 Subject: [PATCH 3/5] fix tests --- jest.config.js | 6 ------ karma.conf.js | 4 ++-- package.json | 11 ++++------- src/js/init.ts | 8 ++++---- test/unit/{browser.spec.js => browser.spec.ts} | 0 test/unit/{functions.spec.js => functions.spec.ts} | 0 test/unit/{html.spec.js => html.spec.ts} | 0 test/unit/{image.spec.js => image.spec.ts} | 0 test/unit/{init.spec.js => init.spec.ts} | 0 test/unit/{json.spec.js => json.spec.ts} | 0 test/unit/{modal.spec.js => modal.spec.ts} | 0 test/unit/{pdf.spec.js => pdf.spec.ts} | 0 test/unit/{print.spec.js => print.spec.ts} | 0 13 files changed, 10 insertions(+), 19 deletions(-) delete mode 100644 jest.config.js rename test/unit/{browser.spec.js => browser.spec.ts} (100%) rename test/unit/{functions.spec.js => functions.spec.ts} (100%) rename test/unit/{html.spec.js => html.spec.ts} (100%) rename test/unit/{image.spec.js => image.spec.ts} (100%) rename test/unit/{init.spec.js => init.spec.ts} (100%) rename test/unit/{json.spec.js => json.spec.ts} (100%) rename test/unit/{modal.spec.js => modal.spec.ts} (100%) rename test/unit/{pdf.spec.js => pdf.spec.ts} (100%) rename test/unit/{print.spec.js => print.spec.ts} (100%) diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index c6873e5..0000000 --- a/jest.config.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - transform: { '^.+\\.ts?$': 'ts-jest' }, - testEnvironment: 'node', - testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] -} diff --git a/karma.conf.js b/karma.conf.js index 3ff26d5..db326fa 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -4,11 +4,11 @@ module.exports = function (config) { config.set({ frameworks: ['jasmine'], files: [ - 'test/**/*.spec.js' + 'test/**/*.spec.ts' ], exclude: [], preprocessors: { - 'test/**/*.js': ['webpack', 'sourcemap', 'coverage'] + 'test/**/*.ts': ['webpack', 'sourcemap', 'coverage'] }, reporters: ['progress', 'coverage'], coverageReporter: { diff --git a/package.json b/package.json index 13d3047..90a07a1 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,11 @@ "devDependencies": { "@babel/core": "7.11.6", "@babel/preset-env": "7.11.5", - "@types/jest": "^29.5.0", "babel-loader": "8.1.0", "coveralls": "3.1.0", "css-loader": "4.2.2", "istanbul-instrumenter-loader": "3.0.1", "jasmine-core": "3.6.0", - "jest": "^29.5.0", "karma": "5.2.1", "karma-chrome-launcher": "3.1.0", "karma-coverage": "2.0.3", @@ -34,7 +32,9 @@ "typescript": "^3.9.10", "webpack": "4.44.1", "webpack-cli": "3.3.12", - "webpack-dev-server": "3.11.0" + "webpack-dev-server": "3.11.0", + "karma-typescript": "^5.5.3", + "source-map-loader": "^4.0.1" }, "scripts": { "test": "standard && karma start", @@ -60,8 +60,5 @@ "printjs", "print.js", "print-js" - ], - "dependencies": { - "source-map-loader": "^4.0.1" - } + ] } diff --git a/src/js/init.ts b/src/js/init.ts index 369852b..3b300d6 100644 --- a/src/js/init.ts +++ b/src/js/init.ts @@ -26,8 +26,8 @@ export default { gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;', showModal: false, onError: (error) => { throw error }, - onLoadingStart: () => {}, - onLoadingEnd: () => {}, + onLoadingStart: null, + onLoadingEnd: null, onPrintDialogClose: () => {}, onIncompatibleBrowser: () => {}, modalMessage: 'Retrieving Document...', @@ -39,8 +39,8 @@ export default { targetStyles: ['border', 'box', 'break', 'text-decoration'], ignoreElements: [], repeatTableHeader: true, - css: [], - style: '', + css: null, + style: null, scanStyles: true, base64: false, diff --git a/test/unit/browser.spec.js b/test/unit/browser.spec.ts similarity index 100% rename from test/unit/browser.spec.js rename to test/unit/browser.spec.ts diff --git a/test/unit/functions.spec.js b/test/unit/functions.spec.ts similarity index 100% rename from test/unit/functions.spec.js rename to test/unit/functions.spec.ts diff --git a/test/unit/html.spec.js b/test/unit/html.spec.ts similarity index 100% rename from test/unit/html.spec.js rename to test/unit/html.spec.ts diff --git a/test/unit/image.spec.js b/test/unit/image.spec.ts similarity index 100% rename from test/unit/image.spec.js rename to test/unit/image.spec.ts diff --git a/test/unit/init.spec.js b/test/unit/init.spec.ts similarity index 100% rename from test/unit/init.spec.js rename to test/unit/init.spec.ts diff --git a/test/unit/json.spec.js b/test/unit/json.spec.ts similarity index 100% rename from test/unit/json.spec.js rename to test/unit/json.spec.ts diff --git a/test/unit/modal.spec.js b/test/unit/modal.spec.ts similarity index 100% rename from test/unit/modal.spec.js rename to test/unit/modal.spec.ts diff --git a/test/unit/pdf.spec.js b/test/unit/pdf.spec.ts similarity index 100% rename from test/unit/pdf.spec.js rename to test/unit/pdf.spec.ts diff --git a/test/unit/print.spec.js b/test/unit/print.spec.ts similarity index 100% rename from test/unit/print.spec.js rename to test/unit/print.spec.ts From 08da47a52c3c42f781f222d66b34feaa8fc70376 Mon Sep 17 00:00:00 2001 From: laercioacj Date: Sat, 18 Mar 2023 18:52:22 -0300 Subject: [PATCH 4/5] revert index.d.ts changes --- src/index.d.ts | 2 +- src/js/init.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 51ad2c2..a694261 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -5,7 +5,7 @@ declare namespace printJS { type PrintTypes = 'pdf' | 'html' | 'image' | 'json' | 'raw-html'; interface Configuration { - printable: string; + printable: any; fallbackPrintable?: string; type?: PrintTypes; documentTitle?: string; diff --git a/src/js/init.ts b/src/js/init.ts index 3b300d6..0cf28aa 100644 --- a/src/js/init.ts +++ b/src/js/init.ts @@ -13,8 +13,8 @@ const printTypes = ['pdf', 'html', 'image', 'json', 'raw-html'] export default { init () { const params = { - printable: '', - fallbackPrintable: '', + printable: null, + fallbackPrintable: null, type: 'pdf', header: null, headerStyle: 'font-weight: 300;', From 6b91caec840d6f51952c65d497ebcea54dcba1eb Mon Sep 17 00:00:00 2001 From: laercioacj Date: Mon, 27 Mar 2023 11:47:11 -0300 Subject: [PATCH 5/5] Update files --- karma.conf.js | 15 ++++++++------- package.json | 12 +++++++----- src/js/browser.ts | 2 +- src/js/functions.ts | 4 ++-- src/js/html.ts | 4 ++-- src/js/image.ts | 6 +++--- src/js/init.ts | 14 +++++++------- src/js/json.ts | 4 ++-- src/js/pdf.ts | 7 +++---- src/js/print.ts | 10 +++++----- src/js/raw-html.ts | 4 ++-- src/js/types/browser.d.ts | 1 + src/{ => js/types}/index.d.ts | 22 +++++++++++++++++++--- test/unit/browser.spec.ts | 6 +++--- test/unit/functions.spec.ts | 2 +- test/unit/html.spec.ts | 2 +- test/unit/image.spec.ts | 2 +- test/unit/init.spec.ts | 2 +- test/unit/json.spec.ts | 2 +- test/unit/modal.spec.ts | 2 +- test/unit/pdf.spec.ts | 2 +- test/unit/print.spec.ts | 2 +- tsconfig.json | 23 ++++++++++++----------- 23 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 src/js/types/browser.d.ts rename src/{ => js/types}/index.d.ts (84%) diff --git a/karma.conf.js b/karma.conf.js index db326fa..f70d0fc 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -1,16 +1,17 @@ -// const path = require('path') - module.exports = function (config) { config.set({ - frameworks: ['jasmine'], + + frameworks: ['jasmine', 'karma-typescript'], + files: [ - 'test/**/*.spec.ts' + { pattern: 'test/**/*.ts' }, + { pattern: 'src/**/*.ts' } ], - exclude: [], + preprocessors: { - 'test/**/*.ts': ['webpack', 'sourcemap', 'coverage'] + '**/*.ts': ['karma-typescript', 'sourcemap', 'coverage'] }, - reporters: ['progress', 'coverage'], + reporters: ['dots', 'coverage', 'karma-typescript'], coverageReporter: { dir: 'coverage/', type: 'lcov', diff --git a/package.json b/package.json index 90a07a1..fedd147 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,11 @@ "repository": "https://github.com/crabbly/Print.js", "license": "MIT", "devDependencies": { + "@types/jasmine": "^3.4.0", + "karma": "^6.1.0", + "karma-cli": "^2.0.0", + "karma-typescript": "^5.5.3", + "typescript": "^5.0.2", "@babel/core": "7.11.6", "@babel/preset-env": "7.11.5", "babel-loader": "8.1.0", @@ -15,7 +20,6 @@ "css-loader": "4.2.2", "istanbul-instrumenter-loader": "3.0.1", "jasmine-core": "3.6.0", - "karma": "5.2.1", "karma-chrome-launcher": "3.1.0", "karma-coverage": "2.0.3", "karma-jasmine": "4.0.1", @@ -29,12 +33,10 @@ "standard": "14.3.4", "terser-webpack-plugin": "4.1.0", "ts-loader": "^9.4.2", - "typescript": "^3.9.10", "webpack": "4.44.1", "webpack-cli": "3.3.12", - "webpack-dev-server": "3.11.0", - "karma-typescript": "^5.5.3", - "source-map-loader": "^4.0.1" + "source-map-loader": "^4.0.1", + "webpack-dev-server": "3.11.0" }, "scripts": { "test": "standard && karma start", diff --git a/src/js/browser.ts b/src/js/browser.ts index f600929..8aef10c 100644 --- a/src/js/browser.ts +++ b/src/js/browser.ts @@ -3,7 +3,7 @@ const Browser = { isFirefox: () => { return typeof InstallTrigger !== 'undefined' }, - getFirefoxMajorVersion: (userAgent) => { + getFirefoxMajorVersion: (userAgent = null) => { userAgent = userAgent || navigator.userAgent const firefoxVersionRegex = /firefox\/(\S+)/ const match = userAgent.toLowerCase().match(firefoxVersionRegex) diff --git a/src/js/functions.ts b/src/js/functions.ts index 2f543a5..ed70c3f 100644 --- a/src/js/functions.ts +++ b/src/js/functions.ts @@ -1,5 +1,5 @@ -import Modal from './modal.ts' -import Browser from './browser.ts' +import Modal from './modal' +import Browser from './browser' export function addWrapper (htmlData, params) { const bodyStyle = 'font-family:' + params.font + ' !important; font-size: ' + params.font_size + ' !important; width:100%;' diff --git a/src/js/html.ts b/src/js/html.ts index 08e51c1..559166a 100644 --- a/src/js/html.ts +++ b/src/js/html.ts @@ -1,5 +1,5 @@ -import { collectStyles, addHeader, addFooter } from './functions.ts' -import Print from './print.ts' +import { collectStyles, addHeader, addFooter } from './functions' +import Print from './print' export default { print: (params, printFrame) => { diff --git a/src/js/image.ts b/src/js/image.ts index 3b71b3f..4277fba 100644 --- a/src/js/image.ts +++ b/src/js/image.ts @@ -1,6 +1,6 @@ -import { addHeader, addFooter } from './functions.ts' -import Print from './print.ts' -import Browser from './browser.ts' +import { addHeader, addFooter } from './functions' +import Print from './print' +import Browser from './browser' export default { print: (params, printFrame) => { diff --git a/src/js/init.ts b/src/js/init.ts index 0cf28aa..5eedb75 100644 --- a/src/js/init.ts +++ b/src/js/init.ts @@ -1,12 +1,12 @@ 'use strict' -import Browser from './browser.ts' -import Modal from './modal.ts' -import Pdf from './pdf.ts' -import Html from './html.ts' -import RawHtml from './raw-html.ts' -import Image from './image.ts' -import Json from './json.ts' +import Browser from './browser' +import Modal from './modal' +import Pdf from './pdf' +import Html from './html' +import RawHtml from './raw-html' +import Image from './image' +import Json from './json' const printTypes = ['pdf', 'html', 'image', 'json', 'raw-html'] diff --git a/src/js/json.ts b/src/js/json.ts index 93a178f..36e072c 100644 --- a/src/js/json.ts +++ b/src/js/json.ts @@ -1,5 +1,5 @@ -import { capitalizePrint, addHeader, addFooter } from './functions.ts' -import Print from './print.ts' +import { capitalizePrint, addHeader, addFooter } from './functions' +import Print from './print' export default { print: (params, printFrame) => { diff --git a/src/js/pdf.ts b/src/js/pdf.ts index fdd01fc..24e5b9f 100644 --- a/src/js/pdf.ts +++ b/src/js/pdf.ts @@ -1,5 +1,5 @@ -import Print from './print.ts' -import { cleanUp } from './functions.ts' +import Print from './print' +import { cleanUp } from './functions' export default { print: (params, printFrame) => { @@ -51,8 +51,7 @@ export default { function createBlobAndPrint (params, printFrame, data) { // Pass response or base64 data to a blob and create a local object url - let localPdf = new window.Blob([data], { type: 'application/pdf' }) - localPdf = window.URL.createObjectURL(localPdf) + let localPdf = window.URL.createObjectURL(new window.Blob([data], { type: 'application/pdf' })) // Set iframe src with pdf document url printFrame.setAttribute('src', localPdf) diff --git a/src/js/print.ts b/src/js/print.ts index c3e8094..df27e0d 100644 --- a/src/js/print.ts +++ b/src/js/print.ts @@ -1,5 +1,5 @@ -import Browser from './browser.ts' -import { cleanUp } from './functions.ts' +import Browser from './browser' +import { cleanUp } from './functions' const Print = { send: (params, printFrame) => { @@ -82,7 +82,7 @@ function performPrint (iframeElement, params) { } } -function loadIframeImages (images) { +function loadIframeImages (images){ const promises = images.map(image => { if (image.src && image.src !== window.location.href) { return loadIframeImage(image) @@ -93,11 +93,11 @@ function loadIframeImages (images) { } function loadIframeImage (image) { - return new Promise(resolve => { + return new Promise (resolve => { const pollImage = () => { !image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0 || !image.complete ? setTimeout(pollImage, 500) - : resolve() + : resolve(true) } pollImage() }) diff --git a/src/js/raw-html.ts b/src/js/raw-html.ts index 88deab0..71ea651 100644 --- a/src/js/raw-html.ts +++ b/src/js/raw-html.ts @@ -1,5 +1,5 @@ -import Print from './print.ts' -import { addHeader, addFooter } from './functions.ts' +import Print from './print' +import { addHeader, addFooter } from './functions' export default { print: (params, printFrame) => { diff --git a/src/js/types/browser.d.ts b/src/js/types/browser.d.ts new file mode 100644 index 0000000..dd32988 --- /dev/null +++ b/src/js/types/browser.d.ts @@ -0,0 +1 @@ +declare const InstallTrigger: any; \ No newline at end of file diff --git a/src/index.d.ts b/src/js/types/index.d.ts similarity index 84% rename from src/index.d.ts rename to src/js/types/index.d.ts index a694261..fcc27ff 100644 --- a/src/index.d.ts +++ b/src/js/types/index.d.ts @@ -1,6 +1,24 @@ +export {} + declare function printJS(configuration: printJS.Configuration): void; declare function printJS(source: string, type?: printJS.PrintTypes): void; +declare global { + interface Window { + printJS: Function; + StyleMedia: any; + chrome: any; + } + + interface Document { + documentMode: any; + } + + interface HTMLElement { + contentWindow: any; + contentDocument: any; + } +} declare namespace printJS { type PrintTypes = 'pdf' | 'html' | 'image' | 'json' | 'raw-html'; @@ -43,6 +61,4 @@ declare namespace printJS { honorColor?: boolean; imageStyle?: string; } -} - -export = printJS; \ No newline at end of file +} \ No newline at end of file diff --git a/test/unit/browser.spec.ts b/test/unit/browser.spec.ts index 6342d1b..bacbeb6 100644 --- a/test/unit/browser.spec.ts +++ b/test/unit/browser.spec.ts @@ -1,4 +1,4 @@ -import Browser from '../../src/js/browser.ts' +import Browser from '../../src/js/browser' describe('Browser', () => { it('has a function named isFirefox that returns a boolean value', () => { @@ -35,12 +35,12 @@ describe('Browser', () => { describe('isChrome()', () => { it('returns true for Google Chrome', () => { - const stubbedWindow = { chrome: {} } + const stubbedWindow = { chrome: {} } expect(Browser.isChrome(stubbedWindow)).toBeTruthy() }) it('returns false for non Google Chrome', () => { - const stubbedWindow = {} + const stubbedWindow = {} expect(Browser.isChrome(stubbedWindow)).toBeFalsy() }) }) diff --git a/test/unit/functions.spec.ts b/test/unit/functions.spec.ts index f868d2a..10ac6af 100644 --- a/test/unit/functions.spec.ts +++ b/test/unit/functions.spec.ts @@ -1,4 +1,4 @@ -import { addWrapper, capitalizePrint, isRawHTML } from '../../src/js/functions.ts' +import { addWrapper, capitalizePrint, isRawHTML } from '../../src/js/functions' describe('addWrapper()', () => { it('add a div wrapper to a raw html', () => { diff --git a/test/unit/html.spec.ts b/test/unit/html.spec.ts index 4217ba2..561d8b4 100644 --- a/test/unit/html.spec.ts +++ b/test/unit/html.spec.ts @@ -1,4 +1,4 @@ -import Html from '../../src/js/html.ts' +import Html from '../../src/js/html' describe('Html', () => { it('has a method named print', () => { diff --git a/test/unit/image.spec.ts b/test/unit/image.spec.ts index 2a14831..cc1cbd5 100644 --- a/test/unit/image.spec.ts +++ b/test/unit/image.spec.ts @@ -1,4 +1,4 @@ -import Image from '../../src/js/image.ts' +import Image from '../../src/js/image' describe('Image', () => { it('has a method named print', () => { diff --git a/test/unit/init.spec.ts b/test/unit/init.spec.ts index 7baf94e..043808b 100644 --- a/test/unit/init.spec.ts +++ b/test/unit/init.spec.ts @@ -1,4 +1,4 @@ -import Init from '../../src/js/init.ts' +import Init from '../../src/js/init' describe('Init Module', () => { it('has a function named init', () => { diff --git a/test/unit/json.spec.ts b/test/unit/json.spec.ts index de385f0..4c5ffa3 100644 --- a/test/unit/json.spec.ts +++ b/test/unit/json.spec.ts @@ -1,4 +1,4 @@ -import Json from '../../src/js/json.ts' +import Json from '../../src/js/json' describe('Json', () => { it('has a method named print', () => { diff --git a/test/unit/modal.spec.ts b/test/unit/modal.spec.ts index 61d858d..18216cb 100644 --- a/test/unit/modal.spec.ts +++ b/test/unit/modal.spec.ts @@ -1,4 +1,4 @@ -import Modal from '../../src/js/modal.ts' +import Modal from '../../src/js/modal' describe('Modal', () => { it('has a method named show', () => { diff --git a/test/unit/pdf.spec.ts b/test/unit/pdf.spec.ts index 301dc96..67ece89 100644 --- a/test/unit/pdf.spec.ts +++ b/test/unit/pdf.spec.ts @@ -1,4 +1,4 @@ -import Pdf from '../../src/js/pdf.ts' +import Pdf from '../../src/js/pdf' describe('Pdf', () => { it('has a method named print', () => { diff --git a/test/unit/print.spec.ts b/test/unit/print.spec.ts index 08aa82f..44c9b35 100644 --- a/test/unit/print.spec.ts +++ b/test/unit/print.spec.ts @@ -1,4 +1,4 @@ -import Print from '../../src/js/print.ts' +import Print from '../../src/js/print' describe('Print', () => { it('has a method named send', () => { diff --git a/tsconfig.json b/tsconfig.json index e8ad32e..5602ac8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,22 +1,22 @@ { + "compileOnSave": false, "compilerOptions": { - "target": "es5", "module": "esnext", "strict": true, - "jsx": "preserve", - "importHelpers": true, + "noImplicitAny": false, + "outDir": "tmp", "moduleResolution": "node", "esModuleInterop": true, - "allowSyntheticDefaultImports": true, + "experimentalDecorators": true, + "target": "es5", "sourceMap": true, "baseUrl": ".", - "allowJs": true, - "experimentalDecorators": true, - "noImplicitAny": false, - "types": [ - "webpack-env", - "jest" + "allowSyntheticDefaultImports": true, + "types" : [ + "jasmine", + "node" ], + "typeRoots": ["./node_modules/@types", "./src/js/types"], "paths": { "@/*": [ "src/*" @@ -28,10 +28,11 @@ "dom.iterable", "scripthost" ] + }, "include": [ "src/**/*.ts", - "tests/**/*.ts" + "test/**/*.ts" ], "exclude": [ "node_modules"