Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hzsrc committed Jan 13, 2025
1 parent da1953d commit 705e6aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
20 changes: 8 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
setImgDataUrl,
} from './util'


export async function toSvg<T extends HTMLElement>(
node: T,
options: Options = {},
Expand All @@ -25,12 +24,7 @@ export async function toSvg<T extends HTMLElement>(
// await embedWebFonts(clonedNode, options)
// await embedImages(clonedNode, options)
applyStyle(clonedNode, options)
const datauri = await nodeToDataURL(
clonedNode,
width,
height,
options,
)
const datauri = await nodeToDataURL(clonedNode, width, height, options)
return datauri
}

Expand All @@ -43,11 +37,13 @@ async function prepareNode(node: HTMLElement, options: Options = {}) {
}

export async function toOfflineHtml(node: HTMLElement, options: Options = {}) {
var node = await prepareNode(node, options)
var style = await getStyles()
return '<!DOCTYPE html><html>' +
'<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">' +
'<body><style>' + style + '</style>\n' + node.outerHTML + '</body></html>'
const node1 = await prepareNode(node, options)
const style = await getStyles()
return (
`<!DOCTYPE html><html>` +
`<meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">` +
`<body><style>${style}</style>\n${node1.outerHTML}</body></html>`
)
}

export async function toImage<T extends HTMLElement>(
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface Options {
* Use a <style> in svg to import all styles of current html page, and do not add computed styles to every node any more.
* This will make the svg content very small, to resolve problems when html has large amount of sub nodes.
* */
usePageCss?: boolean,
usePageCss?: boolean
/*
* Check whether the svg tail is integrated.
* This will fix some problems that the last page of exported pdf is truncated.
Expand Down
21 changes: 12 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ export function svgUrlToImg(urlIn: string, opt: Options = {}) {
// 原因诸如:4k屏的1px在html中为0.51px,而在svg中为1px;又如 overflow-y 在svg中失效;background定位不兼容等。
// 为了避免图像底部不完整的情况,这里每次额外增加60px高度,并寻找是否存在底部标志颜色(TailColor),直到已存在,说明已经到达底部。
function checkImg(i: number): Promise<HTMLImageElement> {
let url = replaceHeight(urlIn, TailHeight * i)
return createImage(url).then(function(img) {
const url = replaceHeight(urlIn, TailHeight * i)
return createImage(url).then(function (img) {
const prePx = 3
const canvasHeight = (TailHeight * 2) / deviceRatio + prePx
const ctx = get2dCtx(1, canvasHeight)
Expand Down Expand Up @@ -269,7 +269,7 @@ export function svgUrlToImg(urlIn: string, opt: Options = {}) {
if (color !== TailColor) {
// 分界点位置
const posY = -(canvasHeight - j / 4) * deviceRatio
var url1 = replaceHeight(url, posY)
const url1 = replaceHeight(url, posY)
return createImage(url1)
}
}
Expand All @@ -281,11 +281,11 @@ export function svgUrlToImg(urlIn: string, opt: Options = {}) {
return url
.replace(
/(viewBox%3D%220%200%20[\d.]+%20)([\d.]+)%22/,
function(_, m1, vpHeight) {
function (_, m1, vpHeight) {
return `${m1 + (+vpHeight + delta)}%22`
},
)
.replace(/(%20height%3D%22)([\d.]+)%22/, function(_, m1, height) {
.replace(/(%20height%3D%22)([\d.]+)%22/, function (_, m1, height) {
return `${m1 + (+height + delta / deviceRatio)}%22`
})
}
Expand All @@ -310,7 +310,7 @@ export function createImage(url: string): Promise<HTMLImageElement> {

export async function svgToDataURL(svg: SVGElement): Promise<string> {
return Promise.resolve()
.then(async function() {
.then(async function () {
const xml = new XMLSerializer().serializeToString(svg)
// open('about:blank').document.write('<plaintext>' + xml); //for debug
return xml
Expand Down Expand Up @@ -360,7 +360,9 @@ export async function nodeToDataURL(
if (opt.checkTail) {
foreignObject.insertAdjacentHTML(
'beforeend',
`<div style="background: #${TailColor};height:${TailHeight * 2}px"></div>`,
`<div style="background: #${TailColor};height:${
TailHeight * 2
}px"></div>`,
)
}
if (opt.usePageCss) {
Expand All @@ -372,8 +374,9 @@ export async function nodeToDataURL(
return svgToDataURL(svg)
}

export const isInstanceOfElement = <T extends typeof Element | typeof HTMLElement | typeof SVGImageElement,
>(
export const isInstanceOfElement = <
T extends typeof Element | typeof HTMLElement | typeof SVGImageElement,
>(
node: Element | HTMLElement | SVGImageElement,
instance: T,
): node is T['prototype'] => {
Expand Down

0 comments on commit 705e6aa

Please sign in to comment.