@antv/infographic 0.2.14 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -5
- package/README.zh-CN.md +12 -5
- package/dist/infographic.min.js +155 -153
- package/dist/infographic.min.js.map +1 -1
- package/esm/designs/structures/index.d.ts +1 -0
- package/esm/designs/structures/index.js +1 -0
- package/esm/designs/structures/relation-dagre-flow.js +4 -139
- package/esm/designs/structures/sequence-interaction.d.ts +54 -0
- package/esm/designs/structures/sequence-interaction.js +440 -0
- package/esm/designs/utils/geometry.d.ts +44 -0
- package/esm/designs/utils/geometry.js +244 -0
- package/esm/designs/utils/index.d.ts +1 -0
- package/esm/designs/utils/index.js +1 -0
- package/esm/editor/managers/sync-registry.d.ts +2 -1
- package/esm/editor/types/editor.d.ts +2 -1
- package/esm/editor/types/sync.d.ts +2 -1
- package/esm/editor/utils/object.js +46 -39
- package/esm/options/types.d.ts +6 -0
- package/esm/runtime/Infographic.js +20 -7
- package/esm/syntax/index.js +40 -20
- package/esm/syntax/relations.js +26 -2
- package/esm/syntax/schema.js +1 -0
- package/esm/templates/built-in.js +3 -2
- package/esm/templates/sequence-interaction.d.ts +2 -0
- package/esm/templates/sequence-interaction.js +76 -0
- package/esm/types/data.d.ts +1 -0
- package/esm/utils/index.d.ts +1 -0
- package/esm/utils/index.js +1 -0
- package/esm/utils/measure-text.js +31 -3
- package/esm/utils/types.d.ts +16 -0
- package/esm/utils/types.js +12 -0
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/designs/structures/index.d.ts +1 -0
- package/lib/designs/structures/index.js +1 -0
- package/lib/designs/structures/relation-dagre-flow.js +5 -140
- package/lib/designs/structures/sequence-interaction.d.ts +54 -0
- package/lib/designs/structures/sequence-interaction.js +444 -0
- package/lib/designs/utils/geometry.d.ts +44 -0
- package/lib/designs/utils/geometry.js +256 -0
- package/lib/designs/utils/index.d.ts +1 -0
- package/lib/designs/utils/index.js +1 -0
- package/lib/editor/managers/sync-registry.d.ts +2 -1
- package/lib/editor/types/editor.d.ts +2 -1
- package/lib/editor/types/sync.d.ts +2 -1
- package/lib/editor/utils/object.js +45 -38
- package/lib/options/types.d.ts +6 -0
- package/lib/runtime/Infographic.js +19 -6
- package/lib/syntax/index.js +40 -20
- package/lib/syntax/relations.js +26 -2
- package/lib/syntax/schema.js +1 -0
- package/lib/templates/built-in.js +3 -2
- package/lib/templates/sequence-interaction.d.ts +2 -0
- package/lib/templates/sequence-interaction.js +79 -0
- package/lib/types/data.d.ts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +1 -0
- package/lib/utils/measure-text.js +30 -2
- package/lib/utils/types.d.ts +16 -0
- package/lib/utils/types.js +13 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/src/designs/structures/index.ts +1 -0
- package/src/designs/structures/relation-dagre-flow.tsx +14 -178
- package/src/designs/structures/sequence-interaction.tsx +885 -0
- package/src/designs/utils/geometry.tsx +315 -0
- package/src/designs/utils/index.ts +1 -0
- package/src/editor/managers/sync-registry.ts +2 -1
- package/src/editor/types/editor.ts +2 -1
- package/src/editor/types/sync.ts +3 -1
- package/src/editor/utils/object.ts +50 -40
- package/src/options/types.ts +7 -0
- package/src/runtime/Infographic.tsx +27 -17
- package/src/syntax/index.ts +51 -18
- package/src/syntax/relations.ts +29 -2
- package/src/syntax/schema.ts +1 -0
- package/src/templates/built-in.ts +2 -0
- package/src/templates/sequence-interaction.ts +101 -0
- package/src/types/data.ts +1 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/measure-text.ts +35 -3
- package/src/utils/types.ts +61 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import type { JSXElement } from '../../jsx';
|
|
2
|
+
import { Path, Polygon } from '../../jsx';
|
|
3
|
+
|
|
4
|
+
export const getMidPoint = (points: [number, number][]) => {
|
|
5
|
+
if (points.length === 0) return null;
|
|
6
|
+
if (points.length === 1) return points[0];
|
|
7
|
+
let total = 0;
|
|
8
|
+
const segments: {
|
|
9
|
+
length: number;
|
|
10
|
+
start: [number, number];
|
|
11
|
+
end: [number, number];
|
|
12
|
+
}[] = [];
|
|
13
|
+
for (let i = 0; i < points.length - 1; i += 1) {
|
|
14
|
+
const start = points[i];
|
|
15
|
+
const end = points[i + 1];
|
|
16
|
+
const length = Math.hypot(end[0] - start[0], end[1] - start[1]);
|
|
17
|
+
segments.push({ length, start, end });
|
|
18
|
+
total += length;
|
|
19
|
+
}
|
|
20
|
+
if (total === 0) return points[0];
|
|
21
|
+
let target = total / 2;
|
|
22
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
23
|
+
const segment = segments[i];
|
|
24
|
+
if (target <= segment.length || i === segments.length - 1) {
|
|
25
|
+
const ratio =
|
|
26
|
+
segment.length === 0
|
|
27
|
+
? 0
|
|
28
|
+
: Math.max(0, Math.min(1, target / segment.length));
|
|
29
|
+
return [
|
|
30
|
+
segment.start[0] + (segment.end[0] - segment.start[0]) * ratio,
|
|
31
|
+
segment.start[1] + (segment.end[1] - segment.start[1]) * ratio,
|
|
32
|
+
] as [number, number];
|
|
33
|
+
}
|
|
34
|
+
target -= segment.length;
|
|
35
|
+
}
|
|
36
|
+
return points[Math.floor(points.length / 2)];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const createStraightPath = (
|
|
40
|
+
points: [number, number][],
|
|
41
|
+
dx: number,
|
|
42
|
+
dy: number,
|
|
43
|
+
) =>
|
|
44
|
+
points
|
|
45
|
+
.map(([x, y], index) => {
|
|
46
|
+
const prefix = index === 0 ? 'M' : 'L';
|
|
47
|
+
return `${prefix} ${x + dx} ${y + dy}`;
|
|
48
|
+
})
|
|
49
|
+
.join(' ');
|
|
50
|
+
|
|
51
|
+
export const createRoundedPath = (
|
|
52
|
+
points: [number, number][],
|
|
53
|
+
radius: number,
|
|
54
|
+
dx: number,
|
|
55
|
+
dy: number,
|
|
56
|
+
) => {
|
|
57
|
+
if (points.length < 2) return '';
|
|
58
|
+
const clamp = (value: number, min: number, max: number) =>
|
|
59
|
+
Math.min(max, Math.max(min, value));
|
|
60
|
+
const toPoint = ([x, y]: [number, number]) => ({
|
|
61
|
+
x: x + dx,
|
|
62
|
+
y: y + dy,
|
|
63
|
+
});
|
|
64
|
+
const output: string[] = [];
|
|
65
|
+
const first = toPoint(points[0]);
|
|
66
|
+
output.push(`M ${first.x} ${first.y}`);
|
|
67
|
+
|
|
68
|
+
if (points.length === 2) {
|
|
69
|
+
const last = toPoint(points[1]);
|
|
70
|
+
output.push(`L ${last.x} ${last.y}`);
|
|
71
|
+
return output.join(' ');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (let i = 1; i < points.length - 1; i += 1) {
|
|
75
|
+
const prev = points[i - 1];
|
|
76
|
+
const curr = points[i];
|
|
77
|
+
const next = points[i + 1];
|
|
78
|
+
const v0x = curr[0] - prev[0];
|
|
79
|
+
const v0y = curr[1] - prev[1];
|
|
80
|
+
const v1x = next[0] - curr[0];
|
|
81
|
+
const v1y = next[1] - curr[1];
|
|
82
|
+
const d0 = Math.hypot(v0x, v0y);
|
|
83
|
+
const d1 = Math.hypot(v1x, v1y);
|
|
84
|
+
if (d0 === 0 || d1 === 0) {
|
|
85
|
+
const currPoint = toPoint(curr);
|
|
86
|
+
output.push(`L ${currPoint.x} ${currPoint.y}`);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const r = clamp(radius, 0, Math.min(d0, d1) / 2);
|
|
90
|
+
if (r === 0) {
|
|
91
|
+
const currPoint = toPoint(curr);
|
|
92
|
+
output.push(`L ${currPoint.x} ${currPoint.y}`);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const u0x = v0x / d0;
|
|
96
|
+
const u0y = v0y / d0;
|
|
97
|
+
const u1x = v1x / d1;
|
|
98
|
+
const u1y = v1y / d1;
|
|
99
|
+
const start = toPoint([curr[0] - u0x * r, curr[1] - u0y * r]);
|
|
100
|
+
const end = toPoint([curr[0] + u1x * r, curr[1] + u1y * r]);
|
|
101
|
+
output.push(`L ${start.x} ${start.y}`);
|
|
102
|
+
const currPoint = toPoint(curr);
|
|
103
|
+
output.push(`Q ${currPoint.x} ${currPoint.y} ${end.x} ${end.y}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const last = toPoint(points[points.length - 1]);
|
|
107
|
+
output.push(`L ${last.x} ${last.y}`);
|
|
108
|
+
return output.join(' ');
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const createArrowElements = (
|
|
112
|
+
x: number,
|
|
113
|
+
y: number,
|
|
114
|
+
angle: number,
|
|
115
|
+
type: 'arrow' | 'triangle' | 'diamond',
|
|
116
|
+
fillColor: string,
|
|
117
|
+
edgeWidth: number,
|
|
118
|
+
arrowSize: number,
|
|
119
|
+
): JSXElement[] => {
|
|
120
|
+
const ux = Math.cos(angle);
|
|
121
|
+
const uy = Math.sin(angle);
|
|
122
|
+
const px = -uy;
|
|
123
|
+
const py = ux;
|
|
124
|
+
const length = arrowSize;
|
|
125
|
+
const halfWidth = arrowSize * 0.55;
|
|
126
|
+
|
|
127
|
+
if (type === 'arrow') {
|
|
128
|
+
const leftX = x - ux * length + px * halfWidth;
|
|
129
|
+
const leftY = y - uy * length + py * halfWidth;
|
|
130
|
+
const rightX = x - ux * length - px * halfWidth;
|
|
131
|
+
const rightY = y - uy * length - py * halfWidth;
|
|
132
|
+
return [
|
|
133
|
+
<Path
|
|
134
|
+
d={`M ${leftX} ${leftY} L ${x} ${y} L ${rightX} ${rightY}`}
|
|
135
|
+
stroke={fillColor}
|
|
136
|
+
strokeWidth={Math.max(1.5, edgeWidth)}
|
|
137
|
+
strokeLinecap="round"
|
|
138
|
+
strokeLinejoin="round"
|
|
139
|
+
fill="none"
|
|
140
|
+
/>,
|
|
141
|
+
];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (type === 'diamond') {
|
|
145
|
+
const diamondLength = length * 1.25;
|
|
146
|
+
const diamondWidth = halfWidth * 0.75;
|
|
147
|
+
const midX = x - ux * diamondLength * 0.5;
|
|
148
|
+
const midY = y - uy * diamondLength * 0.5;
|
|
149
|
+
const diamondPoints = [
|
|
150
|
+
{ x, y },
|
|
151
|
+
{ x: midX + px * diamondWidth, y: midY + py * diamondWidth },
|
|
152
|
+
{ x: x - ux * diamondLength, y: y - uy * diamondLength },
|
|
153
|
+
{ x: midX - px * diamondWidth, y: midY - py * diamondWidth },
|
|
154
|
+
];
|
|
155
|
+
return [
|
|
156
|
+
<Polygon
|
|
157
|
+
points={diamondPoints}
|
|
158
|
+
fill={fillColor}
|
|
159
|
+
stroke={fillColor}
|
|
160
|
+
strokeWidth={Math.max(1, edgeWidth * 0.8)}
|
|
161
|
+
/>,
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const trianglePoints = [
|
|
166
|
+
{ x, y },
|
|
167
|
+
{
|
|
168
|
+
x: x - ux * length + px * halfWidth,
|
|
169
|
+
y: y - uy * length + py * halfWidth,
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
x: x - ux * length - px * halfWidth,
|
|
173
|
+
y: y - uy * length - py * halfWidth,
|
|
174
|
+
},
|
|
175
|
+
];
|
|
176
|
+
return [
|
|
177
|
+
<Polygon
|
|
178
|
+
points={trianglePoints}
|
|
179
|
+
fill={fillColor}
|
|
180
|
+
stroke={fillColor}
|
|
181
|
+
strokeWidth={Math.max(1, edgeWidth * 0.8)}
|
|
182
|
+
/>,
|
|
183
|
+
];
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// LT: Left Top (radio), LC: Left Center (1/2), LB: Left Bottom (1 - radio)
|
|
187
|
+
// RT: Right Top (radio), RC: Right Center (1/2), RB: Right Bottom (1 - radio)
|
|
188
|
+
export const getNodesAnchors = (node: {
|
|
189
|
+
x: number;
|
|
190
|
+
y: number;
|
|
191
|
+
width: number;
|
|
192
|
+
height: number;
|
|
193
|
+
radio?: number;
|
|
194
|
+
}) => {
|
|
195
|
+
const { x, y, width, height, radio = 0.25 } = node;
|
|
196
|
+
const q1H = height * radio;
|
|
197
|
+
const halfH = height * 0.5;
|
|
198
|
+
const q3H = height * (1 - radio);
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
LT: { x, y: y + q1H },
|
|
202
|
+
LC: { x, y: y + halfH },
|
|
203
|
+
LB: { x, y: y + q3H },
|
|
204
|
+
RT: { x: x + width, y: y + q1H },
|
|
205
|
+
RC: { x: x + width, y: y + halfH },
|
|
206
|
+
RB: { x: x + width, y: y + q3H },
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
export const getTangentAngle = (points: [number, number][], t: 0 | 1) => {
|
|
211
|
+
const len = points.length;
|
|
212
|
+
// Cubic Bezier (Self loop)
|
|
213
|
+
if (len === 4) {
|
|
214
|
+
const p0 = points[0],
|
|
215
|
+
p1 = points[1],
|
|
216
|
+
p2 = points[2],
|
|
217
|
+
p3 = points[3];
|
|
218
|
+
if (t === 0) {
|
|
219
|
+
return Math.atan2(p1[1] - p0[1], p1[0] - p0[0]);
|
|
220
|
+
} else {
|
|
221
|
+
return Math.atan2(p3[1] - p2[1], p3[0] - p2[0]);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// Quad Bezier (Curved)
|
|
225
|
+
if (len === 3) {
|
|
226
|
+
const p0 = points[0],
|
|
227
|
+
p1 = points[1],
|
|
228
|
+
p2 = points[2];
|
|
229
|
+
if (t === 0) {
|
|
230
|
+
return Math.atan2(p1[1] - p0[1], p1[0] - p0[0]);
|
|
231
|
+
} else {
|
|
232
|
+
return Math.atan2(p2[1] - p1[1], p2[0] - p1[0]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// Line
|
|
236
|
+
if (len === 2) {
|
|
237
|
+
const p0 = points[0],
|
|
238
|
+
p1 = points[1];
|
|
239
|
+
const angle = Math.atan2(p1[1] - p0[1], p1[0] - p0[0]);
|
|
240
|
+
return angle;
|
|
241
|
+
}
|
|
242
|
+
return 0;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 计算贝塞尔曲线上任意 t (0-1) 位置的点
|
|
247
|
+
*/
|
|
248
|
+
export const getPointAtT = (
|
|
249
|
+
points: [number, number][],
|
|
250
|
+
t = 0.5,
|
|
251
|
+
): [number, number] => {
|
|
252
|
+
const len = points.length;
|
|
253
|
+
if (len === 4) {
|
|
254
|
+
const [p0, p1, p2, p3] = points;
|
|
255
|
+
const mt = 1 - t;
|
|
256
|
+
// B(t) = (1-t)^3*P0 + 3(1-t)^2*t*P1 + 3(1-t)*t^2*P2 + t^3*P3
|
|
257
|
+
return [
|
|
258
|
+
mt ** 3 * p0[0] +
|
|
259
|
+
3 * mt ** 2 * t * p1[0] +
|
|
260
|
+
3 * mt * t ** 2 * p2[0] +
|
|
261
|
+
t ** 3 * p3[0],
|
|
262
|
+
mt ** 3 * p0[1] +
|
|
263
|
+
3 * mt ** 2 * t * p1[1] +
|
|
264
|
+
3 * mt * t ** 2 * p2[1] +
|
|
265
|
+
t ** 3 * p3[1],
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
if (len === 3) {
|
|
269
|
+
const [p0, p1, p2] = points;
|
|
270
|
+
const mt = 1 - t;
|
|
271
|
+
// B(t) = (1-t)^2*P0 + 2(1-t)*t*P1 + t^2*P2
|
|
272
|
+
return [
|
|
273
|
+
mt ** 2 * p0[0] + 2 * mt * t * p1[0] + t ** 2 * p2[0],
|
|
274
|
+
mt ** 2 * p0[1] + 2 * mt * t * p1[1] + t ** 2 * p2[1],
|
|
275
|
+
];
|
|
276
|
+
}
|
|
277
|
+
if (len === 2) {
|
|
278
|
+
const [p0, p1] = points;
|
|
279
|
+
return [p0[0] + (p1[0] - p0[0]) * t, p0[1] + (p1[1] - p0[1]) * t];
|
|
280
|
+
}
|
|
281
|
+
return points[0] || [0, 0];
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export const getLabelPosition = (
|
|
285
|
+
points: [number, number][],
|
|
286
|
+
selfLoopOffset = 10,
|
|
287
|
+
) => {
|
|
288
|
+
const len = points.length;
|
|
289
|
+
// 默认取中点
|
|
290
|
+
const labelPoint = getPointAtT(points);
|
|
291
|
+
|
|
292
|
+
if (len === 4) {
|
|
293
|
+
// 针对自连接(len=4)的特殊偏移处理
|
|
294
|
+
labelPoint[0] += selfLoopOffset;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return labelPoint as [number, number];
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
export const getEdgePathD = (points: [number, number][]) => {
|
|
301
|
+
const len = points.length;
|
|
302
|
+
if (len === 4) {
|
|
303
|
+
const [p0, p1, p2, p3] = points;
|
|
304
|
+
return `M ${p0[0]} ${p0[1]} C ${p1[0]} ${p1[1]} ${p2[0]} ${p2[1]} ${p3[0]} ${p3[1]}`;
|
|
305
|
+
}
|
|
306
|
+
if (len === 3) {
|
|
307
|
+
const [p0, p1, p2] = points;
|
|
308
|
+
return `M ${p0[0]} ${p0[1]} Q ${p1[0]} ${p1[1]} ${p2[0]} ${p2[1]}`;
|
|
309
|
+
}
|
|
310
|
+
if (len === 2) {
|
|
311
|
+
const [p0, p1] = points;
|
|
312
|
+
return `M ${p0[0]} ${p0[1]} L ${p1[0]} ${p1[1]}`;
|
|
313
|
+
}
|
|
314
|
+
return '';
|
|
315
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { get } from 'lodash-es';
|
|
2
|
+
import type { InfographicOptionPath } from '../../options';
|
|
2
3
|
import { ISyncRegistry, SyncHandler } from '../types';
|
|
3
4
|
|
|
4
5
|
type OptionsGetter = () => any;
|
|
@@ -12,7 +13,7 @@ export class SyncRegistry implements ISyncRegistry {
|
|
|
12
13
|
constructor(private getOptions: OptionsGetter) {}
|
|
13
14
|
|
|
14
15
|
register(
|
|
15
|
-
path: string,
|
|
16
|
+
path: InfographicOptionPath | (string & {}),
|
|
16
17
|
handler: SyncHandler,
|
|
17
18
|
options?: { immediate?: boolean },
|
|
18
19
|
): () => void {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { InfographicOptionPath } from '../../options';
|
|
1
2
|
import type { ICommandManager } from './command';
|
|
2
3
|
import type { IInteractionManager } from './interaction';
|
|
3
4
|
import type { IPluginManager } from './plugin';
|
|
@@ -13,7 +14,7 @@ export interface IEditor {
|
|
|
13
14
|
|
|
14
15
|
getDocument(): SVGSVGElement;
|
|
15
16
|
registerSync(
|
|
16
|
-
path: string,
|
|
17
|
+
path: InfographicOptionPath | (string & {}),
|
|
17
18
|
handler: SyncHandler,
|
|
18
19
|
options?: { immediate?: boolean },
|
|
19
20
|
): () => void;
|
package/src/editor/types/sync.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { InfographicOptionPath } from '../../options';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Sync callback
|
|
3
5
|
* @param newValue The new value after modification
|
|
@@ -14,7 +16,7 @@ export interface ISyncRegistry {
|
|
|
14
16
|
* @returns unregister function
|
|
15
17
|
*/
|
|
16
18
|
register(
|
|
17
|
-
path: string,
|
|
19
|
+
path: InfographicOptionPath | (string & {}),
|
|
18
20
|
handler: SyncHandler,
|
|
19
21
|
options?: { immediate?: boolean },
|
|
20
22
|
): () => void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cloneDeep,
|
|
1
|
+
import { cloneDeep, isEqual, isPlainObject } from 'lodash-es';
|
|
2
2
|
|
|
3
3
|
export interface ApplyOptionUpdatesOptions {
|
|
4
4
|
/** Whether to notify parent paths of changes (bubbling) */
|
|
@@ -22,85 +22,95 @@ export function applyOptionUpdates(
|
|
|
22
22
|
options?: ApplyOptionUpdatesOptions,
|
|
23
23
|
): void {
|
|
24
24
|
const { bubbleUp = false, collector } = options ?? {};
|
|
25
|
-
// Set to store unique parent paths that need notification
|
|
26
|
-
const parentPathsToNotify = new Set<string>();
|
|
27
25
|
|
|
28
|
-
applyOptionUpdatesInternal(
|
|
26
|
+
const hasChange = applyOptionUpdatesInternal(
|
|
29
27
|
target,
|
|
30
28
|
source,
|
|
31
29
|
basePath,
|
|
32
30
|
collector,
|
|
33
31
|
bubbleUp,
|
|
34
|
-
parentPathsToNotify,
|
|
35
32
|
);
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// Sort by path depth in descending order (deepest first)
|
|
40
|
-
const sortedPaths = Array.from(parentPathsToNotify).sort((a, b) => {
|
|
41
|
-
const depthA = a === '' ? 0 : a.split('.').length;
|
|
42
|
-
const depthB = b === '' ? 0 : b.split('.').length;
|
|
43
|
-
return depthB - depthA;
|
|
44
|
-
});
|
|
45
|
-
for (const parentPath of sortedPaths) {
|
|
46
|
-
const newVal = parentPath ? get(target, parentPath) : target;
|
|
47
|
-
// For parent paths, we provide the cloned new value.
|
|
48
|
-
// oldVal is passed as undefined as tracking branch node state is complex.
|
|
49
|
-
collector(parentPath, cloneDeep(newVal), undefined);
|
|
50
|
-
}
|
|
34
|
+
if (basePath === '' && hasChange && bubbleUp && collector) {
|
|
35
|
+
collector('', cloneDeep(target), undefined);
|
|
51
36
|
}
|
|
52
37
|
}
|
|
53
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Internal recursive function.
|
|
41
|
+
* Returns true if any change occurred within this branch (or its children).
|
|
42
|
+
*/
|
|
54
43
|
function applyOptionUpdatesInternal(
|
|
55
44
|
target: any,
|
|
56
45
|
source: any,
|
|
57
46
|
basePath: string,
|
|
58
47
|
collector: ((path: string, newVal: any, oldVal: any) => void) | undefined,
|
|
59
48
|
bubbleUp: boolean,
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
): boolean {
|
|
50
|
+
let hasChange = false;
|
|
51
|
+
|
|
62
52
|
Object.keys(source).forEach((key) => {
|
|
53
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
63
57
|
const fullPath = basePath ? `${basePath}.${key}` : key;
|
|
64
58
|
const updateValue = source[key];
|
|
65
59
|
const oldValue = target[key];
|
|
60
|
+
let childChanged = false;
|
|
66
61
|
|
|
67
62
|
if (updateValue === undefined) {
|
|
68
|
-
delete
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
// Handle deletion: Only delete and notify if the key actually exists
|
|
64
|
+
if (key in target) {
|
|
65
|
+
delete target[key];
|
|
66
|
+
collector?.(fullPath, undefined, oldValue);
|
|
67
|
+
childChanged = true;
|
|
68
|
+
}
|
|
71
69
|
} else if (isPlainObject(updateValue)) {
|
|
72
|
-
|
|
70
|
+
// Handle nested object
|
|
71
|
+
const oldValueIsObject = isPlainObject(target[key]);
|
|
72
|
+
if (!oldValueIsObject) {
|
|
73
73
|
target[key] = {};
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
|
|
76
|
+
const grandChildChanged = applyOptionUpdatesInternal(
|
|
76
77
|
target[key],
|
|
77
78
|
updateValue,
|
|
78
79
|
fullPath,
|
|
79
80
|
collector,
|
|
80
81
|
bubbleUp,
|
|
81
|
-
parentPathsToNotify,
|
|
82
82
|
);
|
|
83
|
+
|
|
84
|
+
if (!oldValueIsObject) {
|
|
85
|
+
// Overwriting a primitive with an object is always a change.
|
|
86
|
+
childChanged = true;
|
|
87
|
+
// If the object was empty (grandChildChanged is false), we still need to report it.
|
|
88
|
+
if (!grandChildChanged) {
|
|
89
|
+
collector?.(fullPath, target[key], oldValue);
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
childChanged = grandChildChanged;
|
|
93
|
+
}
|
|
83
94
|
} else {
|
|
95
|
+
// Handle primitive update
|
|
84
96
|
target[key] = updateValue;
|
|
85
97
|
if (!isEqual(updateValue, oldValue)) {
|
|
86
98
|
collector?.(fullPath, updateValue, oldValue);
|
|
87
|
-
|
|
99
|
+
childChanged = true;
|
|
88
100
|
}
|
|
89
101
|
}
|
|
102
|
+
|
|
103
|
+
if (childChanged) {
|
|
104
|
+
hasChange = true;
|
|
105
|
+
}
|
|
90
106
|
});
|
|
91
|
-
}
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
set.add(''); // Root path
|
|
99
|
-
return;
|
|
108
|
+
// Bubbling: Notify if any child changed in this branch
|
|
109
|
+
// The recursion naturally ensures this happens in "deepest-first" (post-order) sequence.
|
|
110
|
+
if (hasChange && bubbleUp && basePath !== '') {
|
|
111
|
+
// Current target is now fully updated for this scope.
|
|
112
|
+
collector?.(basePath, cloneDeep(target), undefined);
|
|
100
113
|
}
|
|
101
114
|
|
|
102
|
-
|
|
103
|
-
for (let i = parts.length; i >= 0; i--) {
|
|
104
|
-
set.add(parts.slice(0, i).join('.'));
|
|
105
|
-
}
|
|
115
|
+
return hasChange;
|
|
106
116
|
}
|
package/src/options/types.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { DesignOptions, ParsedDesignsOptions } from '../designs';
|
|
|
2
2
|
import type { ElementProps, IInteraction, IPlugin } from '../editor';
|
|
3
3
|
import type { ThemeConfig } from '../themes';
|
|
4
4
|
import type { Data, Padding, ParsedData } from '../types';
|
|
5
|
+
import type { Path } from '../utils';
|
|
5
6
|
|
|
6
7
|
export interface InfographicOptions {
|
|
7
8
|
/** 容器,可以是选择器或者 HTMLElement */
|
|
@@ -66,3 +67,9 @@ interface SVGOptions {
|
|
|
66
67
|
/** 是否启用背景 */
|
|
67
68
|
background?: boolean;
|
|
68
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* All valid property paths for Infographic Options.
|
|
73
|
+
* Use this to validate paths in SyncRegistry and other places.
|
|
74
|
+
*/
|
|
75
|
+
export type InfographicOptionPath = Path<UpdatableInfographicOptions>;
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
ParsedInfographicOptions,
|
|
12
12
|
parseOptions,
|
|
13
13
|
} from '../options';
|
|
14
|
-
import { Renderer } from '../renderer';
|
|
14
|
+
import { DEFAULT_FONT, Renderer, setDefaultFont } from '../renderer';
|
|
15
15
|
import { waitForSvgLoads } from '../resource';
|
|
16
16
|
import { parseSyntax, type SyntaxError } from '../syntax';
|
|
17
17
|
import { IEventEmitter } from '../types';
|
|
@@ -133,29 +133,39 @@ export class Infographic {
|
|
|
133
133
|
* Compose the SVG template
|
|
134
134
|
*/
|
|
135
135
|
compose(parsedOptions: ParsedInfographicOptions): SVGSVGElement {
|
|
136
|
-
const { design, data } = parsedOptions;
|
|
136
|
+
const { design, data, themeConfig } = parsedOptions;
|
|
137
137
|
const { title, item, items, structure } = design;
|
|
138
138
|
const { component: Structure, props: structureProps } = structure;
|
|
139
139
|
const Title = title.component;
|
|
140
140
|
const Item = item.component;
|
|
141
141
|
const Items = items.map((it) => it.component);
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
143
|
+
// Apply theme font-family before measurement so measureText uses the correct font
|
|
144
|
+
const themeFontFamily = themeConfig?.base?.text?.['font-family'];
|
|
145
|
+
const previousDefaultFont = DEFAULT_FONT;
|
|
146
|
+
if (themeFontFamily) setDefaultFont(themeFontFamily);
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const svg = renderSVG(
|
|
150
|
+
<Structure
|
|
151
|
+
data={data}
|
|
152
|
+
Title={Title}
|
|
153
|
+
Item={Item}
|
|
154
|
+
Items={Items}
|
|
155
|
+
options={parsedOptions}
|
|
156
|
+
{...structureProps}
|
|
157
|
+
/>,
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const template = parseSVG(svg);
|
|
161
|
+
if (!template) {
|
|
162
|
+
throw new Error('Failed to parse SVG template');
|
|
163
|
+
}
|
|
164
|
+
return template;
|
|
165
|
+
} finally {
|
|
166
|
+
// Restore previous default font
|
|
167
|
+
if (themeFontFamily) setDefaultFont(previousDefaultFont);
|
|
157
168
|
}
|
|
158
|
-
return template;
|
|
159
169
|
}
|
|
160
170
|
|
|
161
171
|
getTypes() {
|
package/src/syntax/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
TemplateSchema,
|
|
11
11
|
ThemeSchema,
|
|
12
12
|
} from './schema';
|
|
13
|
-
import type { SyntaxNode, SyntaxParseResult } from './types';
|
|
13
|
+
import type { ObjectSchema, SyntaxNode, SyntaxParseResult } from './types';
|
|
14
14
|
|
|
15
15
|
function normalizeItems(items: ItemDatum[]) {
|
|
16
16
|
const seen = new Set<string>();
|
|
@@ -112,24 +112,57 @@ export function parseSyntax(input: string): SyntaxParseResult {
|
|
|
112
112
|
);
|
|
113
113
|
if (parsed.relations.length > 0 || parsed.items.length > 0) {
|
|
114
114
|
const current = (options.data ?? {}) as Record<string, any>;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
} else {
|
|
128
|
-
normalizedItems.push(item);
|
|
129
|
-
itemMap.set(item.id as string, item);
|
|
115
|
+
|
|
116
|
+
// 优先使用已存在的数据列表 (sequences, lists, etc.)
|
|
117
|
+
const dataKeys = Object.keys(
|
|
118
|
+
(DataSchema as ObjectSchema).fields,
|
|
119
|
+
).filter((key) => key !== 'items' && key !== 'relations');
|
|
120
|
+
let hasStructuredData = false;
|
|
121
|
+
|
|
122
|
+
// 尝试找到一个非空的数据源
|
|
123
|
+
for (const key of dataKeys) {
|
|
124
|
+
if (Array.isArray(current[key]) && current[key].length > 0) {
|
|
125
|
+
hasStructuredData = true;
|
|
126
|
+
break;
|
|
130
127
|
}
|
|
131
|
-
}
|
|
132
|
-
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 如果 items 已包含层级结构数据(带 children),也视为结构化数据
|
|
131
|
+
if (
|
|
132
|
+
!hasStructuredData &&
|
|
133
|
+
Array.isArray(current.items) &&
|
|
134
|
+
current.items.length > 0 &&
|
|
135
|
+
current.items.some(
|
|
136
|
+
(item: any) =>
|
|
137
|
+
Array.isArray(item.children) && item.children.length > 0,
|
|
138
|
+
)
|
|
139
|
+
) {
|
|
140
|
+
hasStructuredData = true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 如果没有找到其他数据源,才尝试合并 items
|
|
144
|
+
if (!hasStructuredData) {
|
|
145
|
+
const existingItems = Array.isArray(current.items)
|
|
146
|
+
? (current.items as ItemDatum[])
|
|
147
|
+
: [];
|
|
148
|
+
|
|
149
|
+
const normalizedItems = normalizeItems(existingItems);
|
|
150
|
+
const itemMap = new Map<string, ItemDatum>();
|
|
151
|
+
normalizedItems.forEach((item) => {
|
|
152
|
+
if (item.id) itemMap.set(item.id, item);
|
|
153
|
+
});
|
|
154
|
+
parsed.items.forEach((item) => {
|
|
155
|
+
const existing = itemMap.get(item.id as string);
|
|
156
|
+
if (existing) {
|
|
157
|
+
if (!existing.label && item.label) existing.label = item.label;
|
|
158
|
+
} else {
|
|
159
|
+
normalizedItems.push(item);
|
|
160
|
+
itemMap.set(item.id as string, item);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
current.items = normalizedItems;
|
|
164
|
+
}
|
|
165
|
+
|
|
133
166
|
current.relations = parsed.relations;
|
|
134
167
|
options.data = current as any;
|
|
135
168
|
}
|