Commit bedcef9e authored by rockyl's avatar rockyl

支持解析背景和边框

parent bbc45333
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
* Created by rockyl on 2021/1/11. * Created by rockyl on 2021/1/11.
*/ */
const commonStyleKeys = [
'borderRadius',
'borderColor',
'borderStyle',
'borderWidth',
]
const styleKeys = [ const styleKeys = [
'fontSize', 'fontSize',
'fontWeight', 'fontWeight',
...@@ -17,26 +24,24 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -17,26 +24,24 @@ export function parseDom(el: HTMLElement = document.body) {
let nodes = []; let nodes = [];
walkNode(el, function (childNode) { walkNode(el, function (childNode) {
let vNode, bound, node; let vNode, bound, node, isText;
switch(childNode.nodeName){ switch (childNode.nodeName) {
case 'IMG': case 'IMG':
node = childNode; node = childNode;
vNode = { vNode = {
type: 1, type: 2,
src: childNode.src, src: childNode.src,
} }
bound = childNode.getBoundingClientRect();
break; break;
case 'CANVAS': case 'CANVAS':
node = childNode; node = childNode;
vNode = { vNode = {
type: 2, type: 3,
img: childNode, img: childNode,
} }
bound = childNode.getBoundingClientRect();
break; break;
default: case '#text':
if (childNode.data) { isText = true;
node = childNode.parentElement; node = childNode.parentElement;
let text = childNode.data.trim(); let text = childNode.data.trim();
if (text) { //过滤空文本 if (text) { //过滤空文本
...@@ -46,22 +51,43 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -46,22 +51,43 @@ export function parseDom(el: HTMLElement = document.body) {
range.detach(); range.detach();
vNode = { vNode = {
type: 0, type: 1,
text, text,
} }
} }
} break;
default:
break; break;
} }
if(!bound && childNode.getBoundingClientRect){
bound = childNode.getBoundingClientRect();
}
let styles = getStylesWithoutDefaults(node || childNode);
if (!isText) {
for(let skey of commonStyleKeys){
if(commonStyleKeys.indexOf(skey) < 0){
continue
}
if (vNode) { if (!vNode) {
vNode = {
type: 0,
}
}
vNode[skey] = styles[skey];
}
}
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;
let styles = getStylesWithoutDefaults(node);
for (let skey in styles) { for (let skey in styles) {
if (styleKeys.indexOf(skey) < 0) { if (styleKeys.indexOf(skey) < 0) {
continue continue
...@@ -72,7 +98,7 @@ export function parseDom(el: HTMLElement = document.body) { ...@@ -72,7 +98,7 @@ export function parseDom(el: HTMLElement = document.body) {
nodes.push(vNode); nodes.push(vNode);
} }
}); });
console.log(nodes);
return { return {
width, width,
height, height,
...@@ -89,7 +115,7 @@ function walkNode(root, callback) { ...@@ -89,7 +115,7 @@ function walkNode(root, callback) {
} }
} }
function getStylesWithoutDefaults(element) { function getStylesWithoutDefaults(element): 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);
......
...@@ -11,7 +11,8 @@ interface ICData { ...@@ -11,7 +11,8 @@ interface ICData {
* 节点类型 * 节点类型
*/ */
enum NodeType { enum NodeType {
TEXT = 0, ANY = 0,
TEXT,
IMAGE, IMAGE,
CANVAS, CANVAS,
} }
......
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
<style> <style>
.poster{ .poster{
position: relative; position: relative;
background-color: orange;
border-radius: 2.00rem;
border: 0.1rem solid blue;
} }
.avatar{ .avatar{
border-radius: 1.00rem; border-radius: 1.00rem;
...@@ -27,6 +30,7 @@ ...@@ -27,6 +30,7 @@
word-wrap: break-word; word-wrap: break-word;
font-family: Arial; font-family: Arial;
font-style: italic; font-style: italic;
background-color: red;
} }
</style> </style>
...@@ -36,7 +40,7 @@ ...@@ -36,7 +40,7 @@
<img class="avatar" src="//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"/> <img class="avatar" src="//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"/>
<canvas id="canvas" style="position: absolute; left: 10px; top: 10px;"></canvas> <canvas id="canvas" style="position: absolute; left: 10px; top: 10px;"></canvas>
<p class="ppp"> <p class="ppp">
a<br/>bcdefghij a<br/>bcde<span style="background-color: rgba(0,255,0,0.5);">span</span>fghij
</p> </p>
</div> </div>
......
...@@ -10,9 +10,10 @@ interface ICData { ...@@ -10,9 +10,10 @@ interface ICData {
* 节点类型 * 节点类型
*/ */
declare enum NodeType { declare enum NodeType {
TEXT = 0, ANY = 0,
IMAGE = 1, TEXT = 1,
CANVAS = 2 IMAGE = 2,
CANVAS = 3
} }
/** /**
* 节点数据 * 节点数据
......
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