Skip to content

Commit 0fd8569

Browse files
authored
refactor(renderer): apply styles created by editor (#153)
1 parent b8734c0 commit 0fd8569

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/renderer/composites/icon.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ export function renderItemIcon(
1414
const value = datum.icon;
1515
if (!value) return null;
1616
const { themeConfig } = options;
17+
const dataAttrs = datum.attributes?.icon as Record<string, any> | undefined;
1718
const attrs: DynamicAttributes<IconAttributes> = {
1819
...themeConfig.item?.icon,
20+
...dataAttrs,
1921
};
2022

2123
const parsedAttrs = parseDynamicAttributes(node, attrs);

src/renderer/composites/illus.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ import {
1111
getAttributes,
1212
getOrCreateDefs,
1313
removeAttributes,
14+
setAttributes,
1415
uuid,
1516
} from '../../utils';
1617
import { ElementTypeEnum } from '../constants';
1718

19+
type IllusRenderAttributes = IllusAttributes & Record<string, any>;
20+
1821
export function renderIllus(
1922
svg: SVGSVGElement,
2023
node: SVGElement,
2124
value: string | ResourceConfig | undefined,
2225
datum?: ItemDatum,
26+
attrs: Record<string, any> = {},
2327
): IllusElement | null {
2428
if (!value) return null;
2529
const config = parseResourceConfig(value);
2630
if (!config) return null;
2731

2832
const id = getResourceId(config)!;
33+
if (attrs && Object.keys(attrs).length > 0) {
34+
setAttributes(node, attrs);
35+
}
2936
const clipPathId = createClipPath(svg, node, id);
3037

3138
loadResource(svg, 'illus', config, datum);
@@ -35,8 +42,9 @@ export function renderIllus(
3542
id,
3643
{
3744
...parseIllusBounds(node),
38-
'clip-path': `url(#${clipPathId})`,
3945
...(color ? { color } : {}),
46+
...attrs,
47+
'clip-path': `url(#${clipPathId})`,
4048
},
4149
data,
4250
);
@@ -48,7 +56,8 @@ export function renderItemIllus(
4856
datum: ItemDatum,
4957
) {
5058
const value = datum.illus;
51-
return renderIllus(svg, node, value, datum);
59+
const attrs = datum.attributes?.illus as Record<string, any> | undefined;
60+
return renderIllus(svg, node, value, datum, attrs);
5261
}
5362

5463
function createClipPath(
@@ -79,7 +88,11 @@ function createClipPath(
7988
return clipPathId;
8089
}
8190

82-
function createIllusElement(id: string, attrs: IllusAttributes, value: string) {
91+
function createIllusElement(
92+
id: string,
93+
attrs: IllusRenderAttributes,
94+
value: string,
95+
) {
8396
const { 'clip-path': clipPath, ...restAttrs } = attrs;
8497

8598
const { x = '0', y = '0', width = '0', height = '0' } = restAttrs;

src/renderer/composites/text.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { get, kebabCase } from 'lodash-es';
22
import { ParsedInfographicOptions } from '../../options';
33
import type { DynamicAttributes } from '../../themes';
4-
import type { TextAttributes } from '../../types';
4+
import type { ItemDatum, TextAttributes } from '../../types';
55
import {
66
createTextElement,
77
encodeFontFamily,
@@ -47,11 +47,16 @@ export function renderItemText(
4747
if (!textShape) return null;
4848
const { data, themeConfig } = options;
4949
const indexes = getItemIndexes(node.dataset.indexes || '0');
50-
const text = String(get(getDatumByIndexes(data, indexes), type, ''));
50+
const datum = getDatumByIndexes(data, indexes) as ItemDatum | undefined;
51+
const text = String(get(datum, type, ''));
52+
const dataAttrs = datum?.attributes?.[type] as
53+
| Record<string, any>
54+
| undefined;
5155
const attrs = Object.assign(
5256
{},
5357
themeConfig.base?.text,
5458
themeConfig.item?.[type],
59+
dataAttrs,
5560
);
5661

5762
const staticAttrs = parseDynamicAttributes(textShape, attrs);

src/renderer/renderer.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,36 @@ function fill(svg: SVGSVGElement, options: ParsedInfographicOptions) {
121121
const modified = renderText(
122122
element,
123123
data.title || '',
124-
Object.assign({}, themeConfig.base?.text, themeConfig.title),
124+
Object.assign(
125+
{},
126+
themeConfig.base?.text,
127+
themeConfig.title,
128+
data.attributes?.title,
129+
),
125130
);
126131
return upsert(element, modified);
127132
}
128133
if (isDesc(element)) {
129134
const modified = renderText(
130135
element,
131136
data.desc || '',
132-
Object.assign({}, themeConfig.base?.text, themeConfig.desc),
137+
Object.assign(
138+
{},
139+
themeConfig.base?.text,
140+
themeConfig.desc,
141+
data.attributes?.desc,
142+
),
133143
);
134144
return upsert(element, modified);
135145
}
136146
if (isIllus(element)) {
137-
const modified = renderIllus(svg, element, data.illus?.[id]);
147+
const modified = renderIllus(
148+
svg,
149+
element,
150+
data.illus?.[id],
151+
undefined,
152+
data.attributes?.illus as Record<string, any> | undefined,
153+
);
138154
return upsert(element, modified);
139155
}
140156

0 commit comments

Comments
 (0)