refactor: refactor drawing html

This commit is contained in:
chenshenhai 2023-05-06 10:45:16 +08:00
parent eec1ecf74a
commit d079ea4836
6 changed files with 86 additions and 3 deletions

View file

@ -143,6 +143,64 @@ const data: Data = {
desc: {
svg: `<svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M336 421m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" ></path><path d="M688 421m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" ></path><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m263 711c-34.2 34.2-74 61-118.3 79.8C611 874.2 562.3 884 512 884c-50.3 0-99-9.8-144.8-29.2-44.3-18.7-84.1-45.6-118.3-79.8-34.2-34.2-61-74-79.8-118.3C149.8 611 140 562.3 140 512s9.8-99 29.2-144.8c18.7-44.3 45.6-84.1 79.8-118.3 34.2-34.2 74-61 118.3-79.8C413 149.8 461.7 140 512 140c50.3 0 99 9.8 144.8 29.2 44.3 18.7 84.1 45.6 118.3 79.8 34.2 34.2 61 74 79.8 118.3C874.2 413 884 461.7 884 512s-9.8 99-29.2 144.8c-18.7 44.3-45.6 84.1-79.8 118.2z"></path><path d="M664 533h-48.1c-4.2 0-7.8 3.2-8.1 7.4C604 589.9 562.5 629 512 629s-92.1-39.1-95.8-88.6c-0.3-4.2-3.9-7.4-8.1-7.4H360c-4.6 0-8.2 3.8-8 8.4 4.4 84.3 74.5 151.6 160 151.6s155.6-67.3 160-151.6c0.2-4.6-3.4-8.4-8-8.4z" ></path></svg>`
}
},
{
name: 'html-001',
uuid: 'xxx-0012',
x: 400,
y: 200,
w: 150,
h: 100,
type: 'html',
angle: 0,
desc: {
html: `
<style>
.btn-box {
margin: 10px 0;
}
.btn {
line-height: 1.5715;
position: relative;
display: inline-block;
font-weight: 400;
white-space: nowrap;
text-align: center;
background-image: none;
border: 1px solid transparent;
box-shadow: 0 2px #00000004;
cursor: pointer;
user-select: none;
height: 32px;
padding: 4px 15px;
font-size: 14px;
border-radius: 2px;
color: #000000d9;
background: #fff;
border-color: #d9d9d9;
}
.btn-primary {
color: #fff;
background: #1890ff;
border-color: #1890ff;
text-shadow: 0 -1px 0 rgb(0 0 0 / 12%);
box-shadow: 0 2px #0000000b;
}
</style>
<div>
<div class="btn-box">
<button class="btn">
<span> Hello &nbsp; Button</span>
</button>
</div>
<div class="btn-box">
<button class="btn btn-primary">
<span>Button Primary</span>
</button>
</div>
</div>
`
}
}
]
};

View file

@ -4,6 +4,7 @@ import { drawRect } from './rect';
import { drawImage } from './image';
import { drawText } from './text';
import { drawSVG } from './svg';
import { drawHTML } from './html';
export function drawElement(ctx: ViewContext2D, elem: Element<ElementType>, opts: RendererDrawElementOptions) {
try {
@ -28,6 +29,10 @@ export function drawElement(ctx: ViewContext2D, elem: Element<ElementType>, opts
drawSVG(ctx, elem as Element<'svg'>, opts);
break;
}
case 'html': {
drawHTML(ctx, elem as Element<'html'>, opts);
break;
}
default: {
break;
}

View file

@ -0,0 +1,16 @@
import type { Element, RendererDrawElementOptions, ViewContext2D } from '@idraw/types';
import { rotateElement } from '@idraw/util';
export function drawHTML(ctx: ViewContext2D, elem: Element<'html'>, opts: RendererDrawElementOptions) {
const content = opts.loader.getContent(elem.uuid);
const { calculator, scaleInfo } = opts;
const { x, y, w, h, angle } = calculator.elementSize(elem, scaleInfo);
rotateElement(ctx, { x, y, w, h, angle }, () => {
if (!content) {
opts.loader.load(elem as Element<'html'>);
}
if (elem.type === 'html' && content) {
ctx.drawImage(content, x, y, w, h);
}
});
}

View file

@ -2,5 +2,6 @@ export { drawCircle } from './circle';
export { drawRect } from './rect';
export { drawImage } from './image';
export { drawSVG } from './svg';
export { drawHTML } from './html';
export { drawText } from './text';
export { drawElementList, drawElement } from './elements';

View file

@ -24,7 +24,10 @@ export class Loader extends EventEmitter<LoaderEventMap> implements RendererLoad
};
});
this._registerLoadFunc<'html'>('html', async (elem: Element<'html'>) => {
const content = await loadHTML(elem.desc.html, elem.desc);
const content = await loadHTML(elem.desc.html, {
width: elem.desc.width || elem.w,
height: elem.desc.height || elem.h
});
return {
uuid: elem.uuid,
lastModified: Date.now(),

View file

@ -46,8 +46,8 @@ interface ElementCircleDesc extends ElementBaseDesc {
interface ElementHTMLDesc extends ElementBaseDesc {
html: string;
width: number;
height: number;
width?: number;
height?: number;
}
interface ElementImageDesc extends ElementBaseDesc {