Commit 2a777ad9 authored by rockyl's avatar rockyl

init

parent 7a7dcc5b
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
* Created by rockyl on 2021/1/26. * Created by rockyl on 2021/1/26.
*/ */
export const debugMode = window['html-shot/debug-mode'] || false; export const debugMode = window['html-shot/debug-mode'] || false
/** /**
* Created by rockyl on 2021/1/11. * Created by rockyl on 2021/1/11.
*/ */
import {debugMode} from "./config.js"; import {debugMode} from "./config.js"
const commonStyleKeys = [ const commonStyleKeys = [
'backgroundColor', 'backgroundColor',
...@@ -26,15 +26,15 @@ const includeStyleKeys = [ ...@@ -26,15 +26,15 @@ const includeStyleKeys = [
] ]
export function parseDom(el: HTMLElement = document.body) { export function parseDom(el: HTMLElement = document.body) {
const {left: pX, top: pY, width, height} = el.getBoundingClientRect(); const {left: pX, top: pY, width, height} = el.getBoundingClientRect()
let nodes = []; let nodes = []
walkNode(el, function (childNode) { walkNode(el, function (childNode) {
let vNode, bound, node, isText; let vNode, bound, node, isText
let skip = false; let skip = false
switch (childNode.nodeName) { switch (childNode.nodeName) {
case 'IMG': case 'IMG':
node = childNode; node = childNode
const src = node.getAttribute('src') //必须使用此方法,使用node.src且为空时会返回当前页面链接 const src = node.getAttribute('src') //必须使用此方法,使用node.src且为空时会返回当前页面链接
if (src) { //过滤src为空的图片 if (src) { //过滤src为空的图片
vNode = { vNode = {
...@@ -42,46 +42,46 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -42,46 +42,46 @@ export function parseDom(el: HTMLElement = document.body) {
src, src,
} }
} }
break; break
case 'CANVAS': case 'CANVAS':
node = childNode; node = childNode
vNode = { vNode = {
type: 3, type: 3,
img: node, img: node,
} }
break; break
case '#text': case '#text':
isText = true; isText = true
node = childNode.parentElement; node = childNode.parentElement
let text = childNode.data.trim(); let text = childNode.data.trim()
if (text) { //过滤空文本 if (text) { //过滤空文本
let range = document.createRange(); let range = document.createRange()
range.selectNode(childNode); range.selectNode(childNode)
bound = range.getBoundingClientRect(); bound = range.getBoundingClientRect()
range.detach(); range.detach()
vNode = { vNode = {
type: 1, type: 1,
text, text,
} }
} }
break; break
case '#comment': case '#comment':
skip = true; skip = true
break; break
default: default:
break; break
} }
if (skip) { if (skip) {
return; return
} }
if (!bound && childNode.getBoundingClientRect) { if (!bound && childNode.getBoundingClientRect) {
bound = childNode.getBoundingClientRect(); bound = childNode.getBoundingClientRect()
} }
let styles = getStylesWithoutDefaults(node || childNode, includeStyleKeys); let styles = getStylesWithoutDefaults(node || childNode, includeStyleKeys)
if (!isText) { if (!isText) {
for (let skey of commonStyleKeys) { for (let skey of commonStyleKeys) {
...@@ -95,64 +95,64 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -95,64 +95,64 @@ export function parseDom(el: HTMLElement = document.body) {
} }
} }
if (styles[skey] !== undefined) { if (styles[skey] !== undefined) {
vNode[skey] = styles[skey]; vNode[skey] = styles[skey]
} }
} }
} }
if (vNode && bound) { if (vNode && bound) {
const {left, top, width, height} = bound; const {left, top, width, height} = bound
vNode.x = left - pX; vNode.x = left - pX
vNode.y = top - pY; vNode.y = top - pY
vNode.width = width; vNode.width = width
vNode.height = height; vNode.height = height
for (let skey in styles) { for (let skey in styles) {
if (styleKeys.indexOf(skey) < 0) { if (styleKeys.indexOf(skey) < 0) {
continue continue
} }
vNode[skey] = styles[skey]; vNode[skey] = styles[skey]
} }
nodes.push(vNode); nodes.push(vNode)
} }
}); })
if (debugMode) { if (debugMode) {
console.info(nodes); console.info(nodes)
} }
return { return {
width, width,
height, height,
nodes, nodes,
}; }
} }
function walkNode(root, callback) { function walkNode(root, callback) {
callback(root); callback(root)
for (let i = 0, li = root.childNodes.length; i < li; i++) { for (let i = 0, li = root.childNodes.length; i < li; i++) {
const childNode = root.childNodes[i]; const childNode = root.childNodes[i]
walkNode(childNode, callback); walkNode(childNode, callback)
} }
} }
function getStylesWithoutDefaults(element, includes: string[] = []): any { function getStylesWithoutDefaults(element, includes: string[] = []): any {
let dummy = document.createElement('element-' + (new Date().getTime())); let dummy = document.createElement('element-' + (new Date().getTime()))
document.body.appendChild(dummy); document.body.appendChild(dummy)
let defaultStyles = getComputedStyle(dummy); let defaultStyles = getComputedStyle(dummy)
let elementStyles = getComputedStyle(element); let elementStyles = getComputedStyle(element)
let diff = {}; let diff = {}
for (let key in elementStyles) { for (let key in elementStyles) {
if (includes.indexOf(key) >= 0 || (elementStyles.hasOwnProperty(key) && defaultStyles[key] !== elementStyles[key])) { if (includes.indexOf(key) >= 0 || (elementStyles.hasOwnProperty(key) && defaultStyles[key] !== elementStyles[key])) {
diff[key] = elementStyles[key]; diff[key] = elementStyles[key]
} }
} }
dummy.remove(); dummy.remove()
return diff; return diff
} }
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
* Created by rockyl on 2021/1/11. * Created by rockyl on 2021/1/11.
*/ */
import {parseDom} from "./dom-parser.js"; import {parseDom} from "./dom-parser.js"
import {RenderOptions, toCanvas} from "./toCanvas.js"; import {RenderOptions, toCanvas} from "./toCanvas.js"
/** /**
* HTML截图 * HTML截图
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment