@antv/infographic 0.2.16 → 0.2.17
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/dist/infographic.min.js +114 -113
- package/dist/infographic.min.js.map +1 -1
- package/esm/constants/service.d.ts +1 -1
- package/esm/constants/service.js +1 -1
- package/esm/designs/structures/chart-line.js +2 -1
- package/esm/exporter/svg.js +167 -1
- package/esm/options/parser.js +8 -6
- package/esm/options/types.d.ts +3 -3
- package/esm/renderer/renderer.js +1 -1
- package/esm/resource/loaders/search.js +2 -3
- package/esm/runtime/options.js +1 -1
- package/esm/syntax/index.js +56 -10
- package/esm/syntax/mapper.js +20 -6
- package/esm/syntax/parser.js +9 -0
- package/esm/syntax/types.d.ts +1 -1
- package/esm/templates/registry.d.ts +1 -0
- package/esm/templates/registry.js +6 -0
- package/esm/templates/utils.d.ts +1 -0
- package/esm/templates/utils.js +63 -0
- package/esm/themes/built-in.js +3 -0
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/constants/service.d.ts +1 -1
- package/lib/constants/service.js +1 -1
- package/lib/designs/structures/chart-line.js +2 -1
- package/lib/exporter/svg.js +167 -1
- package/lib/options/parser.js +7 -5
- package/lib/options/types.d.ts +3 -3
- package/lib/renderer/renderer.js +1 -1
- package/lib/resource/loaders/search.js +2 -3
- package/lib/runtime/options.js +1 -1
- package/lib/syntax/index.js +56 -10
- package/lib/syntax/mapper.js +20 -6
- package/lib/syntax/parser.js +9 -0
- package/lib/syntax/types.d.ts +1 -1
- package/lib/templates/registry.d.ts +1 -0
- package/lib/templates/registry.js +7 -0
- package/lib/templates/utils.d.ts +1 -0
- package/lib/templates/utils.js +66 -0
- package/lib/themes/built-in.js +3 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/src/constants/service.ts +1 -1
- package/src/designs/structures/chart-line.tsx +3 -1
- package/src/exporter/svg.ts +195 -1
- package/src/options/parser.ts +7 -6
- package/src/options/types.ts +3 -3
- package/src/renderer/renderer.ts +1 -1
- package/src/resource/loaders/search.ts +2 -2
- package/src/runtime/options.ts +1 -1
- package/src/syntax/index.ts +71 -10
- package/src/syntax/mapper.ts +20 -6
- package/src/syntax/parser.ts +10 -0
- package/src/syntax/types.ts +1 -0
- package/src/templates/registry.ts +6 -0
- package/src/templates/utils.ts +87 -0
- package/src/themes/built-in.ts +4 -0
- package/src/version.ts +1 -1
package/src/exporter/svg.ts
CHANGED
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
import { embedFonts } from './font';
|
|
11
11
|
import type { SVGExportOptions } from './types';
|
|
12
12
|
|
|
13
|
+
const VIEWBOX_CHANGE_TOLERANCE = 0.5;
|
|
14
|
+
|
|
13
15
|
export async function exportToSVGString(
|
|
14
16
|
svg: SVGSVGElement,
|
|
15
17
|
options: Omit<SVGExportOptions, 'type'> = {},
|
|
@@ -19,6 +21,190 @@ export async function exportToSVGString(
|
|
|
19
21
|
return 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(str);
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
function getExportViewBox(svg: SVGSVGElement) {
|
|
25
|
+
if (svg.hasAttribute('viewBox')) return getViewBox(svg);
|
|
26
|
+
|
|
27
|
+
const width = parseAbsoluteLength(svg.getAttribute('width'));
|
|
28
|
+
const height = parseAbsoluteLength(svg.getAttribute('height'));
|
|
29
|
+
if (width > 0 && height > 0) {
|
|
30
|
+
return { x: 0, y: 0, width, height };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const rect = svg.getBoundingClientRect();
|
|
34
|
+
if (rect.width > 0 && rect.height > 0) {
|
|
35
|
+
return { x: 0, y: 0, width: rect.width, height: rect.height };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseAbsoluteLength(value: string | null): number {
|
|
42
|
+
if (!value) return Number.NaN;
|
|
43
|
+
const trimmed = value.trim();
|
|
44
|
+
if (!trimmed) return Number.NaN;
|
|
45
|
+
if (!/^[-+]?(?:\d+\.?\d*|\.\d+)(?:px)?$/.test(trimmed)) return Number.NaN;
|
|
46
|
+
return Number.parseFloat(trimmed);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function measureSpanContentHeight(span: HTMLElement): number {
|
|
50
|
+
const prevHeight = span.style.height;
|
|
51
|
+
const prevOverflow = span.style.overflow;
|
|
52
|
+
try {
|
|
53
|
+
span.style.height = 'max-content';
|
|
54
|
+
span.style.overflow = 'hidden';
|
|
55
|
+
void span.offsetHeight; // force reflow
|
|
56
|
+
return span.scrollHeight;
|
|
57
|
+
} finally {
|
|
58
|
+
span.style.height = prevHeight;
|
|
59
|
+
span.style.overflow = prevOverflow;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function measureSpanContentWidth(span: HTMLElement): number {
|
|
64
|
+
const prevWidth = span.style.width;
|
|
65
|
+
const prevOverflow = span.style.overflow;
|
|
66
|
+
try {
|
|
67
|
+
span.style.width = 'max-content';
|
|
68
|
+
span.style.overflow = 'hidden';
|
|
69
|
+
void span.offsetWidth; // force reflow
|
|
70
|
+
return span.scrollWidth;
|
|
71
|
+
} finally {
|
|
72
|
+
span.style.width = prevWidth;
|
|
73
|
+
span.style.overflow = prevOverflow;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Returns [left, top, right, bottom] in SVG coordinates for a foreignObject,
|
|
78
|
+
// accounting for flex alignment: bottom/center-aligned content can overflow,
|
|
79
|
+
// and horizontally aligned content can overflow as well.
|
|
80
|
+
function getFOContentBoundsInSVG(
|
|
81
|
+
fo: SVGForeignObjectElement,
|
|
82
|
+
content: HTMLElement,
|
|
83
|
+
toSVGCoord: (x: number, y: number) => SVGPoint,
|
|
84
|
+
): [number, number, number, number] {
|
|
85
|
+
const foRect = fo.getBoundingClientRect();
|
|
86
|
+
const foTopLeft = toSVGCoord(foRect.left, foRect.top);
|
|
87
|
+
const foBottomRight = toSVGCoord(foRect.right, foRect.bottom);
|
|
88
|
+
|
|
89
|
+
const foLeftSVG = foTopLeft.x;
|
|
90
|
+
const foTopSVG = foTopLeft.y;
|
|
91
|
+
const foRightSVG = foBottomRight.x;
|
|
92
|
+
const foBottomSVG = foBottomRight.y;
|
|
93
|
+
|
|
94
|
+
const foWidthSVG = foRightSVG - foLeftSVG;
|
|
95
|
+
const foHeightSVG = foBottomSVG - foTopSVG;
|
|
96
|
+
|
|
97
|
+
const svgUnitsPerClientPxY =
|
|
98
|
+
foRect.height > 0 ? foHeightSVG / foRect.height : 1;
|
|
99
|
+
const svgUnitsPerClientPxX = foRect.width > 0 ? foWidthSVG / foRect.width : 1;
|
|
100
|
+
|
|
101
|
+
// Measure actual content dimensions
|
|
102
|
+
const realScrollHeight = measureSpanContentHeight(content);
|
|
103
|
+
const contentHeightSVG =
|
|
104
|
+
realScrollHeight > 0
|
|
105
|
+
? realScrollHeight * svgUnitsPerClientPxY
|
|
106
|
+
: foHeightSVG;
|
|
107
|
+
|
|
108
|
+
const realScrollWidth = measureSpanContentWidth(content);
|
|
109
|
+
const contentWidthSVG =
|
|
110
|
+
realScrollWidth > 0 ? realScrollWidth * svgUnitsPerClientPxX : foWidthSVG;
|
|
111
|
+
|
|
112
|
+
const computedStyle = window.getComputedStyle(content);
|
|
113
|
+
const alignItems = computedStyle.alignItems;
|
|
114
|
+
const justifyContent = computedStyle.justifyContent;
|
|
115
|
+
|
|
116
|
+
// Calculate vertical bounds
|
|
117
|
+
let top: number, bottom: number;
|
|
118
|
+
if (alignItems === 'flex-end' || alignItems === 'end') {
|
|
119
|
+
top = foBottomSVG - contentHeightSVG;
|
|
120
|
+
bottom = foBottomSVG;
|
|
121
|
+
} else if (alignItems === 'center') {
|
|
122
|
+
const overflowY = contentHeightSVG - foHeightSVG;
|
|
123
|
+
top = foTopSVG - overflowY / 2;
|
|
124
|
+
bottom = foBottomSVG + overflowY / 2;
|
|
125
|
+
} else {
|
|
126
|
+
top = foTopSVG;
|
|
127
|
+
bottom = foTopSVG + contentHeightSVG;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Calculate horizontal bounds
|
|
131
|
+
let left: number, right: number;
|
|
132
|
+
if (
|
|
133
|
+
justifyContent === 'flex-end' ||
|
|
134
|
+
justifyContent === 'end' ||
|
|
135
|
+
justifyContent === 'right'
|
|
136
|
+
) {
|
|
137
|
+
left = foRightSVG - contentWidthSVG;
|
|
138
|
+
right = foRightSVG;
|
|
139
|
+
} else if (justifyContent === 'center') {
|
|
140
|
+
const overflowX = contentWidthSVG - foWidthSVG;
|
|
141
|
+
left = foLeftSVG - overflowX / 2;
|
|
142
|
+
right = foRightSVG + overflowX / 2;
|
|
143
|
+
} else {
|
|
144
|
+
left = foLeftSVG;
|
|
145
|
+
right = foLeftSVG + contentWidthSVG;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return [left, top, right, bottom];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Computes a viewBox that fully covers all foreignObject text content,
|
|
153
|
+
* accounting for overflow caused by flex alignment (bottom/center align
|
|
154
|
+
* can push content outside the foreignObject bounds).
|
|
155
|
+
*/
|
|
156
|
+
function computeFullViewBox(svg: SVGSVGElement): string | null {
|
|
157
|
+
const viewBox = getExportViewBox(svg);
|
|
158
|
+
if (!viewBox) return null;
|
|
159
|
+
|
|
160
|
+
if (typeof svg.getScreenCTM !== 'function') return null;
|
|
161
|
+
const screenCTM = svg.getScreenCTM();
|
|
162
|
+
if (!screenCTM) return null;
|
|
163
|
+
const inverseCTM = screenCTM.inverse();
|
|
164
|
+
|
|
165
|
+
const toSVGCoord = (clientX: number, clientY: number) => {
|
|
166
|
+
const pt = svg.createSVGPoint();
|
|
167
|
+
pt.x = clientX;
|
|
168
|
+
pt.y = clientY;
|
|
169
|
+
return pt.matrixTransform(inverseCTM);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
let minX = viewBox.x;
|
|
173
|
+
let minY = viewBox.y;
|
|
174
|
+
let maxX = viewBox.x + viewBox.width;
|
|
175
|
+
let maxY = viewBox.y + viewBox.height;
|
|
176
|
+
|
|
177
|
+
svg
|
|
178
|
+
.querySelectorAll<SVGForeignObjectElement>('foreignObject')
|
|
179
|
+
.forEach((fo) => {
|
|
180
|
+
const content = fo.firstElementChild as HTMLElement;
|
|
181
|
+
if (!content) return;
|
|
182
|
+
const [left, top, right, bottom] = getFOContentBoundsInSVG(
|
|
183
|
+
fo,
|
|
184
|
+
content,
|
|
185
|
+
toSVGCoord,
|
|
186
|
+
);
|
|
187
|
+
minX = Math.min(minX, left);
|
|
188
|
+
minY = Math.min(minY, top);
|
|
189
|
+
maxX = Math.max(maxX, right);
|
|
190
|
+
maxY = Math.max(maxY, bottom);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const newX = minX;
|
|
194
|
+
const newY = minY;
|
|
195
|
+
const newWidth = maxX - newX;
|
|
196
|
+
const newHeight = maxY - newY;
|
|
197
|
+
if (
|
|
198
|
+
newWidth <= viewBox.width + VIEWBOX_CHANGE_TOLERANCE &&
|
|
199
|
+
newHeight <= viewBox.height + VIEWBOX_CHANGE_TOLERANCE &&
|
|
200
|
+
newX >= viewBox.x - VIEWBOX_CHANGE_TOLERANCE &&
|
|
201
|
+
newY >= viewBox.y - VIEWBOX_CHANGE_TOLERANCE
|
|
202
|
+
)
|
|
203
|
+
return null;
|
|
204
|
+
|
|
205
|
+
return `${newX} ${newY} ${newWidth} ${newHeight}`;
|
|
206
|
+
}
|
|
207
|
+
|
|
22
208
|
export async function exportToSVG(
|
|
23
209
|
svg: SVGSVGElement,
|
|
24
210
|
options: Omit<SVGExportOptions, 'type'> = {},
|
|
@@ -29,7 +215,15 @@ export async function exportToSVG(
|
|
|
29
215
|
removeIds = false,
|
|
30
216
|
} = options;
|
|
31
217
|
const clonedSVG = svg.cloneNode(true) as SVGSVGElement;
|
|
32
|
-
|
|
218
|
+
|
|
219
|
+
if (typeof document !== 'undefined') {
|
|
220
|
+
const fullViewBox = computeFullViewBox(svg);
|
|
221
|
+
if (fullViewBox) {
|
|
222
|
+
clonedSVG.setAttribute('viewBox', fullViewBox);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const { width, height } = getViewBox(clonedSVG);
|
|
33
227
|
setAttributes(clonedSVG, { width, height });
|
|
34
228
|
|
|
35
229
|
if (removeIds) {
|
package/src/options/parser.ts
CHANGED
|
@@ -3,13 +3,13 @@ import {
|
|
|
3
3
|
DesignOptions,
|
|
4
4
|
getItem,
|
|
5
5
|
getStructure,
|
|
6
|
-
getTemplate,
|
|
7
6
|
NullableParsedDesignsOptions,
|
|
8
7
|
ParsedDesignsOptions,
|
|
9
8
|
Title,
|
|
10
9
|
} from '../designs';
|
|
11
10
|
import { getPaletteColor } from '../renderer';
|
|
12
11
|
import type { TemplateOptions } from '../templates';
|
|
12
|
+
import { getTemplate, resolveTemplateKey } from '../templates/registry';
|
|
13
13
|
import { generateThemeColors, getTheme, type ThemeConfig } from '../themes';
|
|
14
14
|
import type { Data, ItemDatum, ParsedData } from '../types';
|
|
15
15
|
import {
|
|
@@ -32,14 +32,15 @@ export function parseOptions(
|
|
|
32
32
|
data,
|
|
33
33
|
...restOptions
|
|
34
34
|
} = options;
|
|
35
|
+
const resolvedTemplate = template ? resolveTemplateKey(template) : undefined;
|
|
35
36
|
|
|
36
37
|
const parsedContainer =
|
|
37
38
|
typeof container === 'string'
|
|
38
39
|
? document.querySelector(container) || document.createElement('div')
|
|
39
40
|
: container;
|
|
40
41
|
|
|
41
|
-
const templateOptions: TemplateOptions | undefined =
|
|
42
|
-
? getTemplate(
|
|
42
|
+
const templateOptions: TemplateOptions | undefined = resolvedTemplate
|
|
43
|
+
? getTemplate(resolvedTemplate)
|
|
43
44
|
: undefined;
|
|
44
45
|
const mergedThemeConfig = merge(
|
|
45
46
|
{},
|
|
@@ -52,7 +53,7 @@ export function parseOptions(
|
|
|
52
53
|
: undefined;
|
|
53
54
|
|
|
54
55
|
const parsed: Partial<ParsedInfographicOptions> = {
|
|
55
|
-
container: parsedContainer as
|
|
56
|
+
container: parsedContainer as Element | ShadowRoot,
|
|
56
57
|
padding: parsePadding(padding),
|
|
57
58
|
};
|
|
58
59
|
|
|
@@ -63,10 +64,10 @@ export function parseOptions(
|
|
|
63
64
|
|
|
64
65
|
Object.assign(parsed, restOptions);
|
|
65
66
|
|
|
66
|
-
const parsedData = parseData(data,
|
|
67
|
+
const parsedData = parseData(data, resolvedTemplate);
|
|
67
68
|
if (parsedData) parsed.data = parsedData;
|
|
68
69
|
|
|
69
|
-
if (
|
|
70
|
+
if (resolvedTemplate) parsed.template = resolvedTemplate;
|
|
70
71
|
if (templateOptions?.design || design) {
|
|
71
72
|
const designOptions = {
|
|
72
73
|
...(resolvedThemeConfig
|
package/src/options/types.ts
CHANGED
|
@@ -5,8 +5,8 @@ import type { Data, Padding, ParsedData } from '../types';
|
|
|
5
5
|
import type { Path } from '../utils';
|
|
6
6
|
|
|
7
7
|
export interface InfographicOptions {
|
|
8
|
-
/**
|
|
9
|
-
container?: string |
|
|
8
|
+
/** 容器,可以是选择器、Element 或 ShadowRoot */
|
|
9
|
+
container?: string | Element | ShadowRoot;
|
|
10
10
|
/** 宽度 */
|
|
11
11
|
width?: number | string;
|
|
12
12
|
/** 高度 */
|
|
@@ -37,7 +37,7 @@ export interface InfographicOptions {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export interface ParsedInfographicOptions {
|
|
40
|
-
container:
|
|
40
|
+
container: Element | ShadowRoot;
|
|
41
41
|
width?: number | string;
|
|
42
42
|
height?: number | string;
|
|
43
43
|
padding?: Padding;
|
package/src/renderer/renderer.ts
CHANGED
|
@@ -11,8 +11,8 @@ const queryIcon = async (query: string): Promise<string | null> => {
|
|
|
11
11
|
const response = await fetchWithCache(url);
|
|
12
12
|
if (!response.ok) return null;
|
|
13
13
|
const result = await response.json();
|
|
14
|
-
if (!result?.
|
|
15
|
-
return (result.data
|
|
14
|
+
if (!result?.success || !Array.isArray(result.data)) return null;
|
|
15
|
+
return (result.data[0] as string) || null;
|
|
16
16
|
} catch (error) {
|
|
17
17
|
console.error(`Failed to query icon for "${query}":`, error);
|
|
18
18
|
return null;
|
package/src/runtime/options.ts
CHANGED
package/src/syntax/index.ts
CHANGED
|
@@ -12,6 +12,16 @@ import {
|
|
|
12
12
|
} from './schema';
|
|
13
13
|
import type { ObjectSchema, SyntaxNode, SyntaxParseResult } from './types';
|
|
14
14
|
|
|
15
|
+
const ALLOWED_ROOT_KEYS = new Set([
|
|
16
|
+
'infographic',
|
|
17
|
+
'template',
|
|
18
|
+
'design',
|
|
19
|
+
'data',
|
|
20
|
+
'theme',
|
|
21
|
+
'width',
|
|
22
|
+
'height',
|
|
23
|
+
]);
|
|
24
|
+
|
|
15
25
|
function normalizeItems(items: ItemDatum[]) {
|
|
16
26
|
const seen = new Set<string>();
|
|
17
27
|
const normalized: ItemDatum[] = [];
|
|
@@ -30,6 +40,14 @@ function normalizeItems(items: ItemDatum[]) {
|
|
|
30
40
|
return normalized.reverse();
|
|
31
41
|
}
|
|
32
42
|
|
|
43
|
+
function assignMissingNodeIds(items: ItemDatum[]) {
|
|
44
|
+
items.forEach((item) => {
|
|
45
|
+
if (!item.id && item.label) {
|
|
46
|
+
item.id = item.label;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
33
51
|
function resolveTemplate(
|
|
34
52
|
node: SyntaxNode | undefined,
|
|
35
53
|
errors: SyntaxParseResult['errors'],
|
|
@@ -44,12 +62,54 @@ function resolveTemplate(
|
|
|
44
62
|
return undefined;
|
|
45
63
|
}
|
|
46
64
|
|
|
65
|
+
function inferTemplateFromBareFirstLine(
|
|
66
|
+
entries: Record<string, SyntaxNode>,
|
|
67
|
+
warnings: SyntaxParseResult['warnings'],
|
|
68
|
+
) {
|
|
69
|
+
if ('infographic' in entries || 'template' in entries) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const [firstEntry] = Object.entries(entries);
|
|
74
|
+
if (!firstEntry) return undefined;
|
|
75
|
+
|
|
76
|
+
const [key, node] = firstEntry;
|
|
77
|
+
if (ALLOWED_ROOT_KEYS.has(key) || node.kind !== 'object') {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
if (node.value !== undefined || Object.keys(node.entries).length > 0) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
warnings.push({
|
|
85
|
+
path: 'template',
|
|
86
|
+
line: node.line,
|
|
87
|
+
code: 'implicit_template',
|
|
88
|
+
message:
|
|
89
|
+
'Inferred template from a bare first line. Prefix it with "infographic" or "template" to make the syntax explicit.',
|
|
90
|
+
raw: key,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
template: key,
|
|
95
|
+
inferredKey: key,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
47
99
|
export function parseSyntax(input: string): SyntaxParseResult {
|
|
48
100
|
const { ast, errors } = parseSyntaxToAst(input);
|
|
49
101
|
const warnings: SyntaxParseResult['warnings'] = [];
|
|
50
102
|
const options: Partial<InfographicOptions> = {};
|
|
51
103
|
|
|
52
104
|
const mergedEntries = { ...ast.entries };
|
|
105
|
+
const inferredTemplate = inferTemplateFromBareFirstLine(
|
|
106
|
+
ast.entries,
|
|
107
|
+
warnings,
|
|
108
|
+
);
|
|
109
|
+
if (inferredTemplate) {
|
|
110
|
+
delete mergedEntries[inferredTemplate.inferredKey];
|
|
111
|
+
}
|
|
112
|
+
|
|
53
113
|
const infographicNode = ast.entries.infographic;
|
|
54
114
|
let templateFromInfographic: string | undefined;
|
|
55
115
|
if (infographicNode && infographicNode.kind === 'object') {
|
|
@@ -59,17 +119,8 @@ export function parseSyntax(input: string): SyntaxParseResult {
|
|
|
59
119
|
});
|
|
60
120
|
}
|
|
61
121
|
|
|
62
|
-
const allowedRootKeys = new Set([
|
|
63
|
-
'infographic',
|
|
64
|
-
'template',
|
|
65
|
-
'design',
|
|
66
|
-
'data',
|
|
67
|
-
'theme',
|
|
68
|
-
'width',
|
|
69
|
-
'height',
|
|
70
|
-
]);
|
|
71
122
|
Object.keys(mergedEntries).forEach((key) => {
|
|
72
|
-
if (!
|
|
123
|
+
if (!ALLOWED_ROOT_KEYS.has(key)) {
|
|
73
124
|
errors.push({
|
|
74
125
|
path: key,
|
|
75
126
|
line: (mergedEntries[key] as SyntaxNode).line,
|
|
@@ -86,6 +137,9 @@ export function parseSyntax(input: string): SyntaxParseResult {
|
|
|
86
137
|
if (!options.template && templateFromInfographic) {
|
|
87
138
|
options.template = templateFromInfographic;
|
|
88
139
|
}
|
|
140
|
+
if (!options.template && inferredTemplate) {
|
|
141
|
+
options.template = inferredTemplate.template;
|
|
142
|
+
}
|
|
89
143
|
|
|
90
144
|
const designNode = mergedEntries.design as SyntaxNode | undefined;
|
|
91
145
|
if (designNode) {
|
|
@@ -113,6 +167,13 @@ export function parseSyntax(input: string): SyntaxParseResult {
|
|
|
113
167
|
if (parsed.relations.length > 0 || parsed.items.length > 0) {
|
|
114
168
|
const current = (options.data ?? {}) as Record<string, any>;
|
|
115
169
|
|
|
170
|
+
if (Array.isArray(current.items)) {
|
|
171
|
+
assignMissingNodeIds(current.items as ItemDatum[]);
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(current.nodes)) {
|
|
174
|
+
assignMissingNodeIds(current.nodes as ItemDatum[]);
|
|
175
|
+
}
|
|
176
|
+
|
|
116
177
|
// 优先使用已存在的数据列表 (sequences, lists, etc.)
|
|
117
178
|
const dataKeys = Object.keys(
|
|
118
179
|
(DataSchema as ObjectSchema).fields,
|
package/src/syntax/mapper.ts
CHANGED
|
@@ -16,10 +16,18 @@ const HEX_COLOR_PATTERN =
|
|
|
16
16
|
/^(#[0-9a-f]{8}|#[0-9a-f]{6}|#[0-9a-f]{4}|#[0-9a-f]{3})/i;
|
|
17
17
|
const FUNCTION_COLOR_PATTERN = /^((?:rgb|rgba|hsl|hsla)\([^)]*\))/i;
|
|
18
18
|
|
|
19
|
+
function normalizeBooleanLiteral(value: string) {
|
|
20
|
+
const trimmed = value.trim();
|
|
21
|
+
if (/^true$/i.test(trimmed)) return 'true';
|
|
22
|
+
if (/^false$/i.test(trimmed)) return 'false';
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
function parseScalar(value: string) {
|
|
20
27
|
const trimmed = value.trim();
|
|
21
|
-
|
|
22
|
-
if (
|
|
28
|
+
const normalizedBoolean = normalizeBooleanLiteral(trimmed);
|
|
29
|
+
if (normalizedBoolean === 'true') return true;
|
|
30
|
+
if (normalizedBoolean === 'false') return false;
|
|
23
31
|
if (/^-?\d+(\.\d+)?$/.test(trimmed)) return parseFloat(trimmed);
|
|
24
32
|
return trimmed;
|
|
25
33
|
}
|
|
@@ -240,7 +248,8 @@ export function mapWithSchema(
|
|
|
240
248
|
);
|
|
241
249
|
return undefined;
|
|
242
250
|
}
|
|
243
|
-
|
|
251
|
+
const normalizedBoolean = normalizeBooleanLiteral(value);
|
|
252
|
+
if (!normalizedBoolean) {
|
|
244
253
|
addError(
|
|
245
254
|
errors,
|
|
246
255
|
node,
|
|
@@ -251,7 +260,7 @@ export function mapWithSchema(
|
|
|
251
260
|
);
|
|
252
261
|
return undefined;
|
|
253
262
|
}
|
|
254
|
-
return
|
|
263
|
+
return normalizedBoolean === 'true';
|
|
255
264
|
}
|
|
256
265
|
case 'enum': {
|
|
257
266
|
const value = readScalar(node);
|
|
@@ -259,7 +268,12 @@ export function mapWithSchema(
|
|
|
259
268
|
addError(errors, node, path, 'schema_mismatch', 'Expected enum value.');
|
|
260
269
|
return undefined;
|
|
261
270
|
}
|
|
262
|
-
|
|
271
|
+
const normalizedBoolean = normalizeBooleanLiteral(value);
|
|
272
|
+
const enumValue =
|
|
273
|
+
normalizedBoolean && schema.values.includes(normalizedBoolean)
|
|
274
|
+
? normalizedBoolean
|
|
275
|
+
: value;
|
|
276
|
+
if (!schema.values.includes(enumValue)) {
|
|
263
277
|
addError(
|
|
264
278
|
errors,
|
|
265
279
|
node,
|
|
@@ -270,7 +284,7 @@ export function mapWithSchema(
|
|
|
270
284
|
);
|
|
271
285
|
return undefined;
|
|
272
286
|
}
|
|
273
|
-
return
|
|
287
|
+
return enumValue;
|
|
274
288
|
}
|
|
275
289
|
case 'array': {
|
|
276
290
|
if (node.kind === 'array') {
|
package/src/syntax/parser.ts
CHANGED
|
@@ -40,6 +40,15 @@ function stripComments(content: string) {
|
|
|
40
40
|
return content.trimEnd();
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
function isCommentLine(content: string) {
|
|
44
|
+
const trimmed = content.trimStart();
|
|
45
|
+
return trimmed.startsWith('#') || trimmed.startsWith('//');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isCodeFenceLine(content: string) {
|
|
49
|
+
return /^```[\w-]*\s*$/.test(content.trim());
|
|
50
|
+
}
|
|
51
|
+
|
|
43
52
|
function looksLikeRelationExpression(text: string) {
|
|
44
53
|
return /[<>=o.x-]{2,}/.test(text);
|
|
45
54
|
}
|
|
@@ -169,6 +178,7 @@ export function parseSyntaxToAst(input: string): ParseResult {
|
|
|
169
178
|
const lineNumber = index + 1;
|
|
170
179
|
if (!line.trim()) return;
|
|
171
180
|
const { indent, content } = getIndentInfo(line);
|
|
181
|
+
if (isCommentLine(content) || isCodeFenceLine(content)) return;
|
|
172
182
|
const stripped = stripComments(content);
|
|
173
183
|
if (!stripped.trim()) return;
|
|
174
184
|
|
package/src/syntax/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TemplateOptions } from './types';
|
|
2
|
+
import { findClosestTemplateKey } from './utils';
|
|
2
3
|
|
|
3
4
|
const TEMPLATE_REGISTRY = new Map<string, TemplateOptions>();
|
|
4
5
|
|
|
@@ -6,6 +7,11 @@ export function registerTemplate(type: string, template: TemplateOptions) {
|
|
|
6
7
|
TEMPLATE_REGISTRY.set(type, template);
|
|
7
8
|
}
|
|
8
9
|
|
|
10
|
+
export function resolveTemplateKey(type: string): string | undefined {
|
|
11
|
+
if (TEMPLATE_REGISTRY.has(type)) return type;
|
|
12
|
+
return findClosestTemplateKey(type, TEMPLATE_REGISTRY.keys());
|
|
13
|
+
}
|
|
14
|
+
|
|
9
15
|
export function getTemplate(type: string): TemplateOptions | undefined {
|
|
10
16
|
return TEMPLATE_REGISTRY.get(type);
|
|
11
17
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
function normalizeTemplateKey(type: string): string {
|
|
2
|
+
return type
|
|
3
|
+
.trim()
|
|
4
|
+
.toLowerCase()
|
|
5
|
+
.replace(/[_\s]+/g, '-')
|
|
6
|
+
.replace(/-+/g, '-');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function getLevenshteinDistance(source: string, target: string): number {
|
|
10
|
+
if (source === target) return 0;
|
|
11
|
+
if (!source.length) return target.length;
|
|
12
|
+
if (!target.length) return source.length;
|
|
13
|
+
|
|
14
|
+
const previous = Array.from(
|
|
15
|
+
{ length: target.length + 1 },
|
|
16
|
+
(_, index) => index,
|
|
17
|
+
);
|
|
18
|
+
const current = new Array<number>(target.length + 1);
|
|
19
|
+
|
|
20
|
+
for (let sourceIndex = 1; sourceIndex <= source.length; sourceIndex += 1) {
|
|
21
|
+
current[0] = sourceIndex;
|
|
22
|
+
const sourceCode = source.charCodeAt(sourceIndex - 1);
|
|
23
|
+
|
|
24
|
+
for (let targetIndex = 1; targetIndex <= target.length; targetIndex += 1) {
|
|
25
|
+
const replaceCost =
|
|
26
|
+
sourceCode === target.charCodeAt(targetIndex - 1) ? 0 : 1;
|
|
27
|
+
current[targetIndex] = Math.min(
|
|
28
|
+
previous[targetIndex] + 1,
|
|
29
|
+
current[targetIndex - 1] + 1,
|
|
30
|
+
previous[targetIndex - 1] + replaceCost,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (let index = 0; index < current.length; index += 1) {
|
|
35
|
+
previous[index] = current[index];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return previous[target.length];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function getCommonPrefixLength(source: string, target: string): number {
|
|
43
|
+
const limit = Math.min(source.length, target.length);
|
|
44
|
+
let index = 0;
|
|
45
|
+
|
|
46
|
+
while (index < limit && source[index] === target[index]) {
|
|
47
|
+
index += 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return index;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function findClosestTemplateKey(
|
|
54
|
+
type: string,
|
|
55
|
+
keys: Iterable<string>,
|
|
56
|
+
): string | undefined {
|
|
57
|
+
const normalizedType = normalizeTemplateKey(type);
|
|
58
|
+
if (!normalizedType) return undefined;
|
|
59
|
+
|
|
60
|
+
let bestMatch: string | undefined;
|
|
61
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
62
|
+
let bestPrefixLength = -1;
|
|
63
|
+
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
const normalizedKey = normalizeTemplateKey(key);
|
|
66
|
+
if (normalizedKey === normalizedType) {
|
|
67
|
+
return key;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const distance = getLevenshteinDistance(normalizedType, normalizedKey);
|
|
71
|
+
const prefixLength = getCommonPrefixLength(normalizedType, normalizedKey);
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
distance < bestDistance ||
|
|
75
|
+
(distance === bestDistance && prefixLength > bestPrefixLength) ||
|
|
76
|
+
(distance === bestDistance &&
|
|
77
|
+
prefixLength === bestPrefixLength &&
|
|
78
|
+
(!bestMatch || key < bestMatch))
|
|
79
|
+
) {
|
|
80
|
+
bestMatch = key;
|
|
81
|
+
bestDistance = distance;
|
|
82
|
+
bestPrefixLength = prefixLength;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return bestMatch;
|
|
87
|
+
}
|
package/src/themes/built-in.ts
CHANGED
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.2.
|
|
1
|
+
export const VERSION = '0.2.17';
|