feat:形状
This commit is contained in:
@@ -11,7 +11,7 @@ Component({
|
||||
type: String,
|
||||
value: '',
|
||||
},
|
||||
color: {
|
||||
fillColor: {
|
||||
type: String,
|
||||
value: '',
|
||||
},
|
||||
@@ -34,7 +34,7 @@ Component({
|
||||
* 监听属性变化
|
||||
*/
|
||||
observers: {
|
||||
'svg, color': function () {
|
||||
'svg, fillColor': function () {
|
||||
this.generateSvgDataUri();
|
||||
}
|
||||
},
|
||||
@@ -46,12 +46,24 @@ Component({
|
||||
* 生成完整的 SVG Data URI
|
||||
*/
|
||||
generateSvgDataUri() {
|
||||
const { svg, color } = this.data;
|
||||
const { svg, fillColor } = this.data;
|
||||
if (fillColor === 'transparent') return;
|
||||
if (!svg) return;
|
||||
|
||||
// 创建完整的 SVG 字符串,替换填充颜色
|
||||
const coloredSvg = svg.replace(/fill="#ff0000"/g, `fill="${color || '#ff0000'}"`);
|
||||
const completeSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">${coloredSvg}</svg>`;
|
||||
// 处理 fill 属性:无论原来有没有 fill,都要赋值为 fillColor
|
||||
let processedSvg = svg;
|
||||
|
||||
// 1. 如果有 fill 属性,全部替换为 fillColor
|
||||
if (/fill="[^"]*"/.test(processedSvg)) {
|
||||
processedSvg = processedSvg.replace(/fill="[^"]*"/g, `fill="${fillColor || '#ff0000'}"`);
|
||||
} else {
|
||||
// 2. 没有 fill 属性,则在第一个标签内加上 fill
|
||||
processedSvg = processedSvg.replace(
|
||||
/(<(?:circle|rect|polygon|ellipse|path|line)[^>]*?)(\/?>)/,
|
||||
`$1 fill="${fillColor || '#ff0000'}"$2`
|
||||
);
|
||||
}
|
||||
const completeSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">${processedSvg}</svg>`;
|
||||
|
||||
// 转换为 Data URI
|
||||
const svgDataUri = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(completeSvg)}`;
|
||||
@@ -65,8 +77,13 @@ Component({
|
||||
this.triggerEvent('onDelete', { key });
|
||||
},
|
||||
onColorTap() {
|
||||
const { key, color } = this.data;
|
||||
this.triggerEvent('onColorTap', { key, color });
|
||||
const { key, fillColor } = this.data;
|
||||
this.triggerEvent('onColorTap', { key, fillColor });
|
||||
},
|
||||
|
||||
onShapeTap() {
|
||||
const { key } = this.data;
|
||||
this.triggerEvent('onShapeTap', { key });
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user