Comparing version 2.0.0 to 2.1.0
# Change Log | ||
## v2.1.0 2018-09-20 | ||
- Add `log` option | ||
- Fix image loading issue | ||
## v2.0.0 2018-09-14 | ||
@@ -4,0 +9,0 @@ |
@@ -8,2 +8,3 @@ import { Style } from './utils'; | ||
injectGlobalCss?: boolean; | ||
log?: boolean; | ||
} | ||
@@ -10,0 +11,0 @@ export interface PrintFn { |
@@ -49,2 +49,4 @@ "use strict"; | ||
if (options === void 0) { options = {}; } | ||
if (options.log) | ||
utils_1.timeStart(); | ||
var sources = utils_1.ensureArray(source).filter(function (s) { return s && s.parentNode; }); | ||
@@ -90,2 +92,4 @@ var clones = sources.map(function (s) { return s.cloneNode(true); }); | ||
var body = pages.join(pageBreak); | ||
if (options.log) | ||
utils_1.timeEnd(); | ||
return { styles: styles, body: body }; | ||
@@ -97,10 +101,14 @@ } | ||
if (options === void 0) { options = {}; } | ||
var win = window.open(); | ||
var _a = generate(source, options), styles = _a.styles, body = _a.body; | ||
utils_1.injectStyles(win, styles); | ||
win.document.body.innerHTML = body; | ||
// 打印前处理 | ||
var beforePrint = options.beforePrint || utils_1.noop; | ||
beforePrint(win); | ||
return Promise.resolve(); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var win, _a, styles, body, beforePrint; | ||
return __generator(this, function (_b) { | ||
win = window.open(); | ||
_a = generate(source, options), styles = _a.styles, body = _a.body; | ||
utils_1.injectStyles(win, styles); | ||
win.document.body.innerHTML = body; | ||
beforePrint = options.beforePrint || utils_1.noop; | ||
beforePrint(win); | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
} | ||
@@ -112,3 +120,3 @@ exports.preview = preview; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var iframe, iframeWindow, iframeDocument, _a, styles, body, images, beforePrint; | ||
var iframe, iframeWindow, iframeDocument, _a, styles, body, beforePrint, images; | ||
return __generator(this, function (_b) { | ||
@@ -128,10 +136,8 @@ switch (_b.label) { | ||
iframeDocument.body.innerHTML = body; | ||
beforePrint = options.beforePrint || utils_1.noop; | ||
beforePrint(iframeWindow); | ||
images = Array.from(iframeDocument.querySelectorAll('img[src]')); | ||
return [4 /*yield*/, Promise.all(images.map(function (img) { return utils_1.resolveImage(img); })) | ||
// 打印前处理 | ||
]; | ||
return [4 /*yield*/, Promise.all(images.map(function (img) { return utils_1.resolveImage(img); }))]; | ||
case 1: | ||
_b.sent(); | ||
beforePrint = options.beforePrint || utils_1.noop; | ||
beforePrint(iframeWindow); | ||
iframeWindow.print(); | ||
@@ -138,0 +144,0 @@ document.body.removeChild(iframe); |
@@ -0,1 +1,3 @@ | ||
export declare function timeStart(): void; | ||
export declare function timeEnd(): void; | ||
export declare function noop(...args: any[]): void; | ||
@@ -2,0 +4,0 @@ export declare function ensureArray<T>(arr: T | T[]): T[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var time = 0; | ||
// 计时开始 | ||
function timeStart() { | ||
time = Date.now(); | ||
} | ||
exports.timeStart = timeStart; | ||
// 计时结束 | ||
function timeEnd() { | ||
console.log(Date.now() - time + 'ms'); | ||
} | ||
exports.timeEnd = timeEnd; | ||
// 空函数 | ||
@@ -50,7 +61,10 @@ function noop() { | ||
pre.innerHTML = isSelect(el) ? el.options[el.selectedIndex].text : el.value; | ||
pre.setAttribute('style', style); | ||
pre.className = el.className; | ||
if (style) | ||
pre.setAttribute('style', style); | ||
el.parentNode.replaceChild(pre, el); | ||
} | ||
else { | ||
el.setAttribute('style', style); | ||
if (style) | ||
el.setAttribute('style', style); | ||
if (el.children && el.children.length) { | ||
@@ -83,9 +97,10 @@ var children = el.children; | ||
return new Promise(function (resolve) { | ||
if (img.getAttribute('src')) { | ||
var src = img.getAttribute('src'); | ||
if (!src || src.startsWith('data:image')) { | ||
resolve(); | ||
} | ||
else { | ||
img.onerror = function () { return resolve(); }; | ||
img.onload = function () { return resolve(); }; | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
@@ -92,0 +107,0 @@ } |
{ | ||
"name": "printing", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Help to print specified dom instead of whole page.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,2 +0,2 @@ | ||
import { ensureArray, injectStyles, loopStyles, noop, resolveImage, Style } from './utils' | ||
import { ensureArray, injectStyles, loopStyles, noop, resolveImage, Style, timeEnd, timeStart } from './utils' | ||
@@ -18,2 +18,3 @@ const ignoreTags = ['COLGROUP', 'COL'] | ||
injectGlobalCss?: boolean | ||
log?: boolean | ||
} | ||
@@ -28,2 +29,4 @@ | ||
export function generate (source: HTMLElement | HTMLElement[], options: Options = {}) { | ||
if (options.log) timeStart() | ||
const sources = ensureArray(source).filter((s) => s && s.parentNode) | ||
@@ -84,2 +87,4 @@ const clones = sources.map((s) => s.cloneNode(true) as HTMLElement) | ||
if (options.log) timeEnd() | ||
return { styles, body } | ||
@@ -89,3 +94,3 @@ } | ||
// 预览 | ||
export function preview (source: HTMLElement | HTMLElement[], options: Options = {}) { | ||
export async function preview (source: HTMLElement | HTMLElement[], options: Options = {}) { | ||
const win = window.open()! | ||
@@ -100,4 +105,2 @@ const { styles, body } = generate(source, options) | ||
beforePrint(win) | ||
return Promise.resolve() | ||
} | ||
@@ -121,2 +124,6 @@ | ||
// 打印前处理 | ||
const beforePrint = options.beforePrint || noop | ||
beforePrint(iframeWindow) | ||
// 等待图片加载完成 | ||
@@ -126,6 +133,2 @@ const images: HTMLImageElement[] = Array.from(iframeDocument.querySelectorAll('img[src]')) | ||
// 打印前处理 | ||
const beforePrint = options.beforePrint || noop | ||
beforePrint(iframeWindow) | ||
iframeWindow.print() | ||
@@ -132,0 +135,0 @@ document.body.removeChild(iframe) |
@@ -0,1 +1,15 @@ | ||
declare const process: any | ||
let time: number = 0 | ||
// 计时开始 | ||
export function timeStart () { | ||
time = Date.now() | ||
} | ||
// 计时结束 | ||
export function timeEnd () { | ||
console.log(Date.now() - time + 'ms') | ||
} | ||
// 空函数 | ||
@@ -49,7 +63,9 @@ export function noop (...args: any[]) {/* empty */} | ||
pre.innerHTML = isSelect(el) ? el.options[el.selectedIndex].text : (el as HTMLInputElement).value | ||
pre.setAttribute('style', style) | ||
pre.className = el.className | ||
if (style) pre.setAttribute('style', style) | ||
el.parentNode!.replaceChild(pre, el) | ||
} else { | ||
el.setAttribute('style', style) | ||
if (style) el.setAttribute('style', style) | ||
@@ -91,9 +107,10 @@ if (el.children && el.children.length) { | ||
return new Promise((resolve) => { | ||
if (img.getAttribute('src')) { | ||
const src = img.getAttribute('src') | ||
if (!src || src.startsWith('data:image')) { | ||
resolve() | ||
} else { | ||
img.onerror = () => resolve() | ||
img.onload = () => resolve() | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
41988
597