Skip to content

Commit 0afb184

Browse files
committed
style: Format generated file
1 parent a99fb38 commit 0afb184

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/libs/fetchXml.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface XmlData {
1212
path: Array<{
1313
$: {
1414
d: string;
15+
fill?: string;
1516
};
1617
}>;
1718
}>;

src/libs/generateComponent.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {
1717
import { whitespace } from './whitespace';
1818
import { GENERATE_MODE } from './generateMode';
1919

20-
const DOM_MAP = {
20+
const SVG_MAP = {
2121
path: 'Path',
2222
};
2323

24+
const ATTRIBUTE_FILL_MAP = ['path'];
25+
2426
export const generateComponent = (data: XmlData) => {
2527
const config = getConfig();
2628
const svgComponents: Set<string> = new Set();
@@ -129,7 +131,7 @@ const generateCase = (data: XmlData['svg']['symbol'][number], baseIdent: number)
129131
let template = `\n${whitespace(baseIdent)}<Svg viewBox="${data.$.viewBox}" width={size} height={size}>\n`;
130132

131133
for (const domName of Object.keys(data)) {
132-
let realDomName = DOM_MAP[domName];
134+
let realDomName = SVG_MAP[domName];
133135

134136
if (domName === '$') {
135137
continue;
@@ -142,13 +144,14 @@ const generateCase = (data: XmlData['svg']['symbol'][number], baseIdent: number)
142144

143145
const counter = {
144146
colorIndex: 0,
147+
baseIdent,
145148
};
146149

147150
if (data[domName].$) {
148-
template += `${whitespace(baseIdent + 2)}<${realDomName}${addAttribute(data[domName], counter)} />\n`;
151+
template += `${whitespace(baseIdent + 2)}<${realDomName}${addAttribute(domName, data[domName], counter)}\n${whitespace(baseIdent + 2)}/>\n`;
149152
} else if (Array.isArray(data[domName])) {
150153
data[domName].forEach((sub) => {
151-
template += `${whitespace(baseIdent + 2)}<${realDomName}${addAttribute(sub, counter)} />\n`;
154+
template += `${whitespace(baseIdent + 2)}<${realDomName}${addAttribute(domName, sub, counter)}\n${whitespace(baseIdent + 2)}/>\n`;
152155
});
153156
}
154157
}
@@ -158,16 +161,22 @@ const generateCase = (data: XmlData['svg']['symbol'][number], baseIdent: number)
158161
return template;
159162
};
160163

161-
const addAttribute = (sub: XmlData['svg']['symbol'][number]['path'][number], counter: { colorIndex: number }) => {
164+
const addAttribute = (domName: string, sub: XmlData['svg']['symbol'][number]['path'][number], counter: { colorIndex: number, baseIdent: number }) => {
162165
let template = '';
163166

164167
if (sub && sub.$) {
168+
if (ATTRIBUTE_FILL_MAP.includes(domName)) {
169+
// Set default color same as in iconfont.cn
170+
// And create placeholder to inject color by user's behavior
171+
sub.$.fill = sub.$.fill || '#333333';
172+
}
173+
165174
for (const attributeName of Object.keys(sub.$)) {
166175
if (attributeName === 'fill') {
167-
template += ` ${attributeName}={color ? typeof color === 'string' && color || color[${counter.colorIndex}] || '${sub.$[attributeName]}' : '${sub.$[attributeName]}'}`;
176+
template += `\n${whitespace(counter.baseIdent + 4)}${attributeName}={color ? typeof color === 'string' && color || color[${counter.colorIndex}] || '${sub.$[attributeName]}' : '${sub.$[attributeName]}'}`;
168177
counter.colorIndex += 1;
169178
} else {
170-
template += ` ${attributeName}="${sub.$[attributeName]}"`;
179+
template += `\n${whitespace(counter.baseIdent + 4)}${attributeName}="${sub.$[attributeName]}"`;
171180
}
172181
}
173182
}

0 commit comments

Comments
 (0)