wt-huatu 1.3.4
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 +223 -0
- package/component/HuatuComponentCache.js +89 -0
- package/component/HuatuComponentDef.js +249 -0
- package/component/HuatuComponentPlacement.js +172 -0
- package/component/HuatuComponentPort.js +58 -0
- package/component/IHuatuComponent.js +2 -0
- package/for-node/node-cli-funcs.js +36 -0
- package/geometry/AngDeg.js +68 -0
- package/geometry/Angle.js +77 -0
- package/geometry/Bezier.js +623 -0
- package/geometry/Circle.js +143 -0
- package/geometry/Ellipse.js +546 -0
- package/geometry/EllipticalArc.js +337 -0
- package/geometry/IGeometry.js +1 -0
- package/geometry/Intersection.js +601 -0
- package/geometry/Line.js +136 -0
- package/geometry/LineSeg.js +179 -0
- package/geometry/Point.js +88 -0
- package/geometry/PolyLine.js +97 -0
- package/geometry/Polygon.js +122 -0
- package/geometry/Rect.js +149 -0
- package/geometry/Ring.js +91 -0
- package/geometry/SegmentModel.js +206 -0
- package/geometry/Triangle.js +168 -0
- package/geometry/Vector.js +92 -0
- package/huatu/Huatu.js +4279 -0
- package/huatu/HuatuAngleMark.js +400 -0
- package/huatu/HuatuBlock.js +26 -0
- package/huatu/HuatuClipPath.js +29 -0
- package/huatu/HuatuCompDrawing.js +77 -0
- package/huatu/HuatuDimMark.js +579 -0
- package/huatu/HuatuDrawing.js +185 -0
- package/huatu/HuatuDrawingGroup.js +52 -0
- package/huatu/HuatuJob.js +347 -0
- package/huatu/HuatuLabel.js +311 -0
- package/huatu/HuatuLabelStyle.js +190 -0
- package/huatu/HuatuLayer.js +10 -0
- package/huatu/HuatuMarker.js +209 -0
- package/huatu/HuatuParser.js +435 -0
- package/huatu/HuatuPath.js +131 -0
- package/huatu/HuatuPathSeries.js +51 -0
- package/huatu/HuatuPattern.js +54 -0
- package/huatu/HuatuPoint.js +20 -0
- package/huatu/HuatuPointSeries.js +67 -0
- package/huatu/HuatuStyle.js +394 -0
- package/huatu/HuatuSymbol.js +81 -0
- package/huatu/HuatuSymbolArray.js +235 -0
- package/huatu/HuatuTransform.js +113 -0
- package/huatu/HuatuVar.js +17 -0
- package/huatu/HuatuYml.js +271 -0
- package/huatu/IHuatu.js +2 -0
- package/huatu/IHuatuYml.js +1 -0
- package/huatu/IndentParsingStack.js +82 -0
- package/index.d.ts +1747 -0
- package/index.js +247 -0
- package/math/Complex.js +72 -0
- package/math/Matrix.js +31 -0
- package/math/NumberRange.js +38 -0
- package/math/VarFuncs.js +24 -0
- package/math/WtMath.js +217 -0
- package/package.json +34 -0
- package/path/PathBezier2.js +75 -0
- package/path/PathBezier3.js +89 -0
- package/path/PathCircle.js +82 -0
- package/path/PathConfig.js +81 -0
- package/path/PathContinuousSegmentedPath.js +390 -0
- package/path/PathEllipse.js +99 -0
- package/path/PathEllipticalArc.js +111 -0
- package/path/PathGeneric.js +59 -0
- package/path/PathLine.js +75 -0
- package/path/PathLines.js +46 -0
- package/path/PathPolygon.js +142 -0
- package/path/PathPolyline.js +266 -0
- package/path/PathRect.js +125 -0
- package/path/PathRing.js +51 -0
- package/path/PathSegmentedPath.js +199 -0
- package/path/PathTriangle.js +90 -0
- package/presets/marker.js +37 -0
- package/presets/pattern.js +85 -0
- package/shape/SvgShapeArrowHead.js +92 -0
- package/shape/SvgShapeCircle.js +26 -0
- package/shape/SvgShapeCross.js +43 -0
- package/shape/SvgShapeEllipse.js +28 -0
- package/shape/SvgShapeGeneric.js +50 -0
- package/shape/SvgShapeHeart.js +40 -0
- package/shape/SvgShapeIsoscelesTriangle.js +52 -0
- package/shape/SvgShapePolygon.js +39 -0
- package/shape/SvgShapeRect.js +26 -0
- package/shape/SvgShapeRhombus.js +27 -0
- package/shape/SvgShapeStar.js +103 -0
- package/svg/ISvg.js +1 -0
- package/svg/SvgBBox.js +149 -0
- package/svg/SvgConstants.js +14 -0
- package/svg/SvgGradient.js +97 -0
- package/svg/SvgMarker.js +221 -0
- package/svg/SvgPattern.js +228 -0
- package/svg/SvgPoint.js +33 -0
- package/svg/SvgPointSet.js +41 -0
- package/templates/lines.js +35 -0
- package/tools/gen-id.js +2 -0
- package/tools/html.js +2 -0
- package/tools/katex.js +75 -0
- package/tools/rand-str.js +122 -0
- package/tools/regex.js +15 -0
- package/tools/utils.js +438 -0
- package/tools/webColor.js +700 -0
- package/wire/HuatuNet.js +22 -0
- package/wire/HuatuWire.js +415 -0
- package/wire/HuatuWireDrawing.js +17 -0
- package/wire/IHuatuWire.js +1 -0
package/Readme.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
<div style="text-align:center;">
|
|
2
|
+
<svg data-v-ab95988f="" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="-5.45 -5.6 10.9 11.2" width="141.70000000000002" height="145.6"><polyline points="-2.250,5.0 -4.850,5.0 -4.850,-5.0 -2.250,-5.0 -2.250,-1.180 2.250,-1.180 2.250,-5.0 4.850,-5.0 4.850,5.0 2.250,5.0 2.250,1.180 -2.250,1.180 -2.250,3.180" fill="#0b4d4233" stroke="currentColor" stroke-width="0.4"></polyline><circle cx="-2.250" cy="5.0" r="0.40" fill="black"></circle><circle cx="-2.250" cy="1.180" r="0.40" fill="black"></circle><circle cx="-2.250" cy="3.180" r="0.40" fill="red"></circle></svg>
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
`wt-huatu`, is a Drawing Library to draw something mathematically, logically, hierarchically and programmatically.
|
|
6
|
+
|
|
7
|
+
For quick and fruitful info, please go to the official site: [https://wt-huatu.org](https://wt-huatu.org).
|
|
8
|
+
|
|
9
|
+
It is written in `Typescript`, can be used as a library, to be integrated into your project. For example, you can embed it into a customized markdown renderer to use it as some text-to-image renderer. Or it can be used as some drawing tool when using the online editor at [https://wt-huatu.org/editor](https://wt-huatu.org/editor).
|
|
10
|
+
|
|
11
|
+
Quick and intuitive explanation. It will convert some text description like this:
|
|
12
|
+
|
|
13
|
+
```yml
|
|
14
|
+
# --- Vars ------
|
|
15
|
+
vars:
|
|
16
|
+
r: 100
|
|
17
|
+
degAB: datan(0.25/1.25)
|
|
18
|
+
|
|
19
|
+
# --- Points ------
|
|
20
|
+
points:
|
|
21
|
+
pA: (0, 0)
|
|
22
|
+
|
|
23
|
+
# --- Shapes ------
|
|
24
|
+
shapes:
|
|
25
|
+
lnAB: line; pA; -degAB
|
|
26
|
+
cpoints:
|
|
27
|
+
pB: lnAB; r
|
|
28
|
+
|
|
29
|
+
shapes2:
|
|
30
|
+
cA: circle; pA; r
|
|
31
|
+
cB: circle; pB; r
|
|
32
|
+
|
|
33
|
+
xpoints2:
|
|
34
|
+
pC: cA; cB; n; # Pick the northern point of the intersections
|
|
35
|
+
pD: lnAB; cA; w; # Pick the western
|
|
36
|
+
pE: lnAB; cB; e; # Pick the eastern
|
|
37
|
+
|
|
38
|
+
shapes3:
|
|
39
|
+
triABC: triangle; pA; pB; pC;
|
|
40
|
+
|
|
41
|
+
# --- Draw --------
|
|
42
|
+
draw:
|
|
43
|
+
- cA; fill, none; stroke; 1, cc
|
|
44
|
+
- cB; fill, none; stroke; 1, cc
|
|
45
|
+
- triABC; fill, yellow; stroke; 1, cc
|
|
46
|
+
- circle; pA; 3 ||| fill; grey
|
|
47
|
+
- circle; pB; 3 ||| fill; grey
|
|
48
|
+
- circle; pC; 3 ||| fill; grey
|
|
49
|
+
- circle; pD; 3 ||| fill; grey
|
|
50
|
+
- circle; pE; 3 ||| fill; grey
|
|
51
|
+
- label; at; pA; w; 20; h; 22; anchor; mr ||| $A$
|
|
52
|
+
- label; at; pB; w; 20; h; 22; anchor; ml ||| $B$
|
|
53
|
+
- label; at; pC; w; 20; h; 22; anchor; bm ||| $C$
|
|
54
|
+
- label; at; pD; w; 20; h; 22; anchor; tr ||| $D$
|
|
55
|
+
- label; at; pE; w; 20; h; 22; anchor; bl ||| $E$
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
to some SVG image like this:
|
|
59
|
+
|
|
60
|
+
<div style="text-align:center;">
|
|
61
|
+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-118.05806756909202 -120.4116135138184 334.17420270727604 221.2116135138184" width="334.174" height="221.212">
|
|
62
|
+
<defs>
|
|
63
|
+
<style type="text/css">
|
|
64
|
+
foreignObject > body { margin: 0; width: 100%; height: 100%; }
|
|
65
|
+
.wthuatu-label-box-2b25127a { fill: none; }
|
|
66
|
+
.huatu-label-container-2b25127a { width: 100%; height: 100%; padding: 0; display: flex; }
|
|
67
|
+
.huatu-label-text-2b25127a { margin: auto 0; text-align: center; max-height: 100%; width: 100%; line-height: 1.0; }
|
|
68
|
+
</style>
|
|
69
|
+
</defs>
|
|
70
|
+
<circle cx="0.000000" cy="0.000000" r="100.000000" fill="none" stroke="currentColor" stroke-width="1.000000"></circle>
|
|
71
|
+
<circle cx="98.058068" cy="-19.611614" r="100.000000" fill="none" stroke="currentColor" stroke-width="1.000000"></circle>
|
|
72
|
+
<path d="M0.000000,0.000000 L98.058068,-19.611614 L32.044878,-94.726584 z" fill="yellow" stroke="currentColor" stroke-width="1.000000"></path>
|
|
73
|
+
<circle cx="0.000000" cy="0.000000" r="3.000000" fill="grey"></circle>
|
|
74
|
+
<circle cx="98.058068" cy="-19.611614" r="3.000000" fill="grey"></circle>
|
|
75
|
+
<circle cx="32.044878" cy="-94.726584" r="3.000000" fill="grey"></circle>
|
|
76
|
+
<circle cx="-98.058068" cy="19.611614" r="3.000000" fill="grey"></circle>
|
|
77
|
+
<circle cx="196.116135" cy="-39.223227" r="3.000000" fill="grey"></circle>
|
|
78
|
+
<g transform="translate(-20.000000,-11.000000)">
|
|
79
|
+
<foreignObject x="0" y="0" width="20" height="22">
|
|
80
|
+
<div class="huatu-label-container-2b25127a">
|
|
81
|
+
<div class="huatu-label-text-2b25127a">
|
|
82
|
+
<span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">A</span></span></span></span>
|
|
83
|
+
</div>
|
|
84
|
+
</div>
|
|
85
|
+
</foreignObject>
|
|
86
|
+
</g>
|
|
87
|
+
<g transform="translate(0.000000,-11.000000)">
|
|
88
|
+
<foreignObject x="98.05806756909202" y="-19.611613513818398" width="20" height="22">
|
|
89
|
+
<div class="huatu-label-container-2b25127a">
|
|
90
|
+
<div class="huatu-label-text-2b25127a">
|
|
91
|
+
<span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span></span></span></span>
|
|
92
|
+
</div>
|
|
93
|
+
</div>
|
|
94
|
+
</foreignObject>
|
|
95
|
+
</g>
|
|
96
|
+
<g transform="translate(-10.000000,-22.000000)">
|
|
97
|
+
<foreignObject x="32.044878272377076" y="-94.72658431775389" width="20" height="22">
|
|
98
|
+
<div class="huatu-label-container-2b25127a">
|
|
99
|
+
<div class="huatu-label-text-2b25127a">
|
|
100
|
+
<span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span></span></span></span>
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
</foreignObject>
|
|
104
|
+
</g>
|
|
105
|
+
<g transform="translate(-20.000000,0.000000)">
|
|
106
|
+
<foreignObject x="-98.05806756909202" y="19.611613513818384" width="20" height="22">
|
|
107
|
+
<div class="huatu-label-container-2b25127a">
|
|
108
|
+
<div class="huatu-label-text-2b25127a">
|
|
109
|
+
<span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span></span></span></span>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
</foreignObject>
|
|
113
|
+
</g>
|
|
114
|
+
<g transform="translate(0.000000,-22.000000)">
|
|
115
|
+
<foreignObject x="196.11613513818403" y="-39.223227027636796" width="20" height="22">
|
|
116
|
+
<div class="huatu-label-container-2b25127a">
|
|
117
|
+
<div class="huatu-label-text-2b25127a">
|
|
118
|
+
<span class="katex"><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">E</span></span></span></span>
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
</foreignObject>
|
|
122
|
+
</g>
|
|
123
|
+
</svg>
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
There are some examples in the folder `examples`, which are used as the library is developed. They are simply `.yml` files, except some special syntax.
|
|
127
|
+
|
|
128
|
+
There are more examples in the shared repository at [https://wt-huatu.org/repo](https://wt-huatu.org/repo).
|
|
129
|
+
|
|
130
|
+
And there are some documents presented at [https://wt-huatu.org/doc](https://wt-huatu.org/doc). The documents are not fully covering all the features of the lib, it is a continuing process, as we add more, it will be updated.
|
|
131
|
+
|
|
132
|
+
You can use it as a npm package.
|
|
133
|
+
|
|
134
|
+
Install by
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm install wt-huatu
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Then use it in `node.js`, or some front-end browser-like environment, following is some example (in node.js):
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import {
|
|
144
|
+
WtHuatu,
|
|
145
|
+
renderWtHuatuYml,
|
|
146
|
+
renderYmlInput
|
|
147
|
+
} from "wt-huatu";
|
|
148
|
+
|
|
149
|
+
import * as fs from "fs";
|
|
150
|
+
|
|
151
|
+
// 设计输入
|
|
152
|
+
// The design input file
|
|
153
|
+
const htDesignInput = `
|
|
154
|
+
# Example WT-HUATU design file
|
|
155
|
+
# --- Vars ------
|
|
156
|
+
vars:
|
|
157
|
+
r: 100
|
|
158
|
+
degAB: datan(0.25/1.25)
|
|
159
|
+
|
|
160
|
+
# --- Points ------
|
|
161
|
+
points:
|
|
162
|
+
pA: (0, 0)
|
|
163
|
+
|
|
164
|
+
# --- Shapes ------
|
|
165
|
+
shapes:
|
|
166
|
+
lnAB: line; pA; -degAB
|
|
167
|
+
cpoints:
|
|
168
|
+
pB: lnAB; r
|
|
169
|
+
|
|
170
|
+
shapes2:
|
|
171
|
+
cA: circle; pA; r
|
|
172
|
+
cB: circle; pB; r
|
|
173
|
+
|
|
174
|
+
xpoints2:
|
|
175
|
+
pC: cA; cB; n;
|
|
176
|
+
pD: lnAB; cA; w;
|
|
177
|
+
pE: lnAB; cB; e;
|
|
178
|
+
|
|
179
|
+
shapes3:
|
|
180
|
+
triABC: triangle; pA; pB; pC;
|
|
181
|
+
|
|
182
|
+
# --- Draw --------
|
|
183
|
+
draw:
|
|
184
|
+
- cA; fill, none; stroke; 1, cc
|
|
185
|
+
- cB; fill, none; stroke; 1, cc
|
|
186
|
+
- triABC; fill, yellow; stroke; 1, cc
|
|
187
|
+
- circle; pA; 2 ||| fill; grey
|
|
188
|
+
- circle; pB; 2 ||| fill; grey
|
|
189
|
+
- circle; pC; 2 ||| fill; grey
|
|
190
|
+
- circle; pD; 2 ||| fill; grey
|
|
191
|
+
- circle; pE; 2 ||| fill; grey
|
|
192
|
+
- label; at; pA; w; 20; h; 22; anchor; mr ||| $A$
|
|
193
|
+
- label; at; pB; w; 20; h; 22; anchor; ml ||| $B$
|
|
194
|
+
- label; at; pC; w; 20; h; 22; anchor; bm ||| $C$
|
|
195
|
+
- label; at; pD; w; 20; h; 22; anchor; tr ||| $D$
|
|
196
|
+
- label; at; pE; w; 20; h; 22; anchor; bl ||| $E$
|
|
197
|
+
`;
|
|
198
|
+
|
|
199
|
+
// 用例1:直接生成嵌入svg文件的Html文件(字符串),可直接在浏览器中查看及打印。
|
|
200
|
+
// Usage 1: Generate the svg-image-embedded html directly, you can browse it in a browser.
|
|
201
|
+
const svgHtml1 = renderWtHuatuYml(htDesignInput);
|
|
202
|
+
fs.writeFileSync("./test-wt-huatu.html", svgHtml1);
|
|
203
|
+
|
|
204
|
+
// 用例2:编译解析后获取完全体结果
|
|
205
|
+
// Usage 2: Render the input to get a full-functional Object `WtHuatu`
|
|
206
|
+
const parsedRes = renderYmlInput(htDesignInput, {});
|
|
207
|
+
// WtHuatu对象, 可以查看其所有成员变量,以便于进行二次开发
|
|
208
|
+
// 或者选择性生成全部或部分所设计/定义的图形元素
|
|
209
|
+
// The parsed WtHuatu object, you can inspect all the members and call the methods, make secondary developments or intergrate into your own applications
|
|
210
|
+
// Or you can use this object to generate part or all the designed drawing elements ever designed
|
|
211
|
+
const htObj = parsedRes.huatu!;
|
|
212
|
+
// 从唯一的输出任务(draw)生成svg文件(字符串),可以将此SVG文件保存为单独的文件或者嵌入到
|
|
213
|
+
// 其他支持SVG格式的图形处理软件中
|
|
214
|
+
// Output the svg file of the only job `draw`, you can save this to seperate file
|
|
215
|
+
// Or embed itinto other systems that can handle SVGs.
|
|
216
|
+
const svg1 = parsedRes.rendered.find(x => x.jobName === 'draw')!.html;
|
|
217
|
+
// 保存svg到文件
|
|
218
|
+
// Save to local SVG format image file
|
|
219
|
+
fs.writeFileSync("./test-wt-huatu.svg", svg1);
|
|
220
|
+
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
If you are interested, let's draw!
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export class HuatuComponentCache {
|
|
2
|
+
static pushToCache(url, yml) {
|
|
3
|
+
if (!globalThis.wthtCompCache) {
|
|
4
|
+
globalThis.wthtCompCache = [];
|
|
5
|
+
}
|
|
6
|
+
globalThis.wthtCompCache.push({
|
|
7
|
+
url,
|
|
8
|
+
yml
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
static pushError(url, error) {
|
|
12
|
+
if (!globalThis.wthtCompFetchErrors) {
|
|
13
|
+
globalThis.wthtCompFetchErrors = [];
|
|
14
|
+
}
|
|
15
|
+
let existing = globalThis.wthtCompFetchErrors.find(x => x.url === url);
|
|
16
|
+
if (existing) {
|
|
17
|
+
existing.error = error;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
globalThis.wthtCompFetchErrors.push({
|
|
21
|
+
url,
|
|
22
|
+
error
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
static getCachedYml(url) {
|
|
27
|
+
return globalThis.wthtCompCache?.find(x => x.url === url)?.yml;
|
|
28
|
+
}
|
|
29
|
+
static getFetchError(url) {
|
|
30
|
+
return globalThis.wthtCompFetchErrors?.find(x => x.url === url)?.error;
|
|
31
|
+
}
|
|
32
|
+
static async fetchDef(url) {
|
|
33
|
+
// Already cached, nothing to do
|
|
34
|
+
if (HuatuComponentCache.getCachedYml(url))
|
|
35
|
+
return;
|
|
36
|
+
// If errored, stop trying
|
|
37
|
+
if (HuatuComponentCache.getFetchError(url))
|
|
38
|
+
return;
|
|
39
|
+
// Try fetch from remote server
|
|
40
|
+
const reBsonObjId = /^[0-9a-f]{24}$/i;
|
|
41
|
+
const reHttpUrl = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
|
|
42
|
+
const rePathInvalid = /[,:;*?"'`<>|\0#%&+$]/;
|
|
43
|
+
const HUATU_REPO_URL = "https://wt-huatu.org/huatu";
|
|
44
|
+
// let pmsFetch: Promise<Response>;
|
|
45
|
+
let query;
|
|
46
|
+
if (reHttpUrl.exec(url)) {
|
|
47
|
+
// If http link, use it directly
|
|
48
|
+
query = new URL(url);
|
|
49
|
+
}
|
|
50
|
+
else if (reBsonObjId.exec(url)) {
|
|
51
|
+
// If it is some huatu Id, use it as ID
|
|
52
|
+
query = new URL(`${HUATU_REPO_URL}/${url.toLowerCase()}`);
|
|
53
|
+
}
|
|
54
|
+
else if (!rePathInvalid.exec(url)) {
|
|
55
|
+
// Some valid path like path/to/some/repo/huatu.yml
|
|
56
|
+
query = new URL(HUATU_REPO_URL);
|
|
57
|
+
query.searchParams.append('path', url);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// The path is invalid, will skip fetching and return error
|
|
61
|
+
HuatuComponentCache.pushError(url, `Component path '${url}' not recognized/found!`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
let res = await globalThis.fetch(query);
|
|
66
|
+
if (res.status === 200) {
|
|
67
|
+
let yml = await res.text();
|
|
68
|
+
HuatuComponentCache.pushToCache(url, yml);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
HuatuComponentCache.pushError(url, `Component Fetch Error (Code: ${res.status}) for ${url}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
HuatuComponentCache.pushError(url, `Component Fetch Error for ${url}: ${error}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
static async prepareComponents(ymlObj) {
|
|
79
|
+
let comps = ymlObj["components"];
|
|
80
|
+
if (!comps || typeof comps !== "object" || Array.isArray(comps))
|
|
81
|
+
return;
|
|
82
|
+
for (const key in comps) {
|
|
83
|
+
let comp = comps[key];
|
|
84
|
+
if (typeof comp === "string" && comp) {
|
|
85
|
+
await HuatuComponentCache.fetchDef(comp);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { FORBIDDEN_NAMES, resolveYmlModelOfComponent } from "../huatu/HuatuParser.js";
|
|
2
|
+
import { HuatuYml } from "../huatu/HuatuYml.js";
|
|
3
|
+
import { WT_REGEX } from "../tools/regex.js";
|
|
4
|
+
import { isValidColor } from "../tools/webColor.js";
|
|
5
|
+
import { HuatuComponentCache } from "./HuatuComponentCache.js";
|
|
6
|
+
export class HuatuComponentDef {
|
|
7
|
+
static REGEX_INPUT_VAR_USE_MATCH = /[$]([a-z][a-z0-9_]*)(?=([^$0-9a-z_]|$))/i;
|
|
8
|
+
name;
|
|
9
|
+
desc;
|
|
10
|
+
def = {};
|
|
11
|
+
inputVars = [];
|
|
12
|
+
isValid = false;
|
|
13
|
+
errors = [];
|
|
14
|
+
constructor(name, def) {
|
|
15
|
+
this.name = name;
|
|
16
|
+
if (def) {
|
|
17
|
+
if (typeof def === "string") {
|
|
18
|
+
this.defineFromString(def);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.defineFromObj(def);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
defineFromString(url) {
|
|
26
|
+
// Check if used in CLI, i.e. read relative path
|
|
27
|
+
if (globalThis.wthtCli) {
|
|
28
|
+
const ymlFile = globalThis.wthtCli.getFileContent(url);
|
|
29
|
+
if (ymlFile) {
|
|
30
|
+
const parsed = HuatuYml.parseYml(ymlFile);
|
|
31
|
+
if (parsed.succeeded) {
|
|
32
|
+
this.defineFromObj(parsed.rootObj);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.errors.push({ title: `Component Definition Parsing Failed: ${parsed.errorMessages.map(parsed => parsed.message).join("\n")}` });
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let ymlCached = HuatuComponentCache.getCachedYml(url);
|
|
41
|
+
if (ymlCached) {
|
|
42
|
+
let ymlObj = HuatuYml.parseYml(ymlCached);
|
|
43
|
+
if (ymlObj.succeeded) {
|
|
44
|
+
this.defineFromObj(ymlObj.rootObj);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.errors.push({ title: `Component Definition Parsing Failed: ${ymlObj.errorMessages.map(m => m.message).join("\n")}` });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
let error = HuatuComponentCache.getFetchError(url);
|
|
52
|
+
this.errors.push({ title: error || `'${url}' cannot resolve to some component!` });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
defineFromObj(obj) {
|
|
57
|
+
const ivError = this.parseInputVars(obj);
|
|
58
|
+
if (ivError) {
|
|
59
|
+
this.errors.push(ivError);
|
|
60
|
+
}
|
|
61
|
+
// Here, use the cleaned version, because it will be used to replace IVs when placed
|
|
62
|
+
this.def = this.cleanDefObj(obj);
|
|
63
|
+
const ivReplaced = this.replaceInputVars({});
|
|
64
|
+
if (Array.isArray(ivReplaced)) {
|
|
65
|
+
this.errors.push({
|
|
66
|
+
title: `Some Input Vars not defined!`,
|
|
67
|
+
msgs: ivReplaced
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// The IV replacing is successful, now test to make it a Huatu
|
|
72
|
+
const comp = resolveYmlModelOfComponent(ivReplaced);
|
|
73
|
+
if (comp.resolvingErrors.length > 0) {
|
|
74
|
+
this.errors.push(...comp.resolvingErrors);
|
|
75
|
+
}
|
|
76
|
+
else if (!comp.draw) {
|
|
77
|
+
this.errors.push({
|
|
78
|
+
title: `There is no 'draw' under root, you at least need to draw something!`
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// Now it is confirmed to a valid design of component
|
|
83
|
+
this.isValid = true;
|
|
84
|
+
// Try parsing Ports to see if any errors
|
|
85
|
+
const portErrors = comp.parseComponentPorts(ivReplaced);
|
|
86
|
+
if (portErrors.length > 0) {
|
|
87
|
+
this.errors.push({
|
|
88
|
+
title: `Port Definitions have some error`,
|
|
89
|
+
msgs: portErrors
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
replaceInputVars(vars) {
|
|
96
|
+
const notReplacedIvs = [];
|
|
97
|
+
const replaceTable = this.inputVars.map(x => [x.name, `${x.value}`]);
|
|
98
|
+
// Update the replacing table by newly given Input Vars
|
|
99
|
+
for (let k in vars) {
|
|
100
|
+
let idx = replaceTable.findIndex(x => x[0] === k);
|
|
101
|
+
if (idx >= 0)
|
|
102
|
+
replaceTable[idx][1] = `${vars[k]}`;
|
|
103
|
+
}
|
|
104
|
+
const replaceFunc = (originalStr) => {
|
|
105
|
+
let replacedStr = "";
|
|
106
|
+
let toBeReplaced = originalStr;
|
|
107
|
+
while (toBeReplaced) {
|
|
108
|
+
let rp = HuatuComponentDef.REGEX_INPUT_VAR_USE_MATCH.exec(toBeReplaced);
|
|
109
|
+
if (rp) {
|
|
110
|
+
const ivName = rp[1];
|
|
111
|
+
const idx = replaceTable.findIndex(x => x[0] === ivName);
|
|
112
|
+
if (idx >= 0) { // Found
|
|
113
|
+
const strToReplace = replaceTable[idx][1].toLowerCase() === "[[empty]]" ? "" : replaceTable[idx][1];
|
|
114
|
+
// Make replacement
|
|
115
|
+
replacedStr += toBeReplaced.substring(0, rp.index) + strToReplace;
|
|
116
|
+
}
|
|
117
|
+
else { // Not Found
|
|
118
|
+
notReplacedIvs.push(rp[0]);
|
|
119
|
+
// Just copy and not replace
|
|
120
|
+
replacedStr += toBeReplaced.substring(0, rp.index) + rp[0];
|
|
121
|
+
}
|
|
122
|
+
// Move forward
|
|
123
|
+
toBeReplaced = toBeReplaced.substring(rp.index + rp[0].length);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
replacedStr += toBeReplaced;
|
|
127
|
+
toBeReplaced = "";
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return replacedStr;
|
|
131
|
+
};
|
|
132
|
+
const objCopied = HuatuYml.copyObj(this.def, undefined, replaceFunc);
|
|
133
|
+
return notReplacedIvs.length > 0
|
|
134
|
+
? notReplacedIvs.map(x => `Input Var [${x}] cannot be understood!`)
|
|
135
|
+
: objCopied;
|
|
136
|
+
}
|
|
137
|
+
parseInputVars(obj) {
|
|
138
|
+
const ivs = obj.inputVars;
|
|
139
|
+
if (!ivs || Array.isArray(ivs) || typeof ivs !== "object")
|
|
140
|
+
return undefined;
|
|
141
|
+
const errors = [];
|
|
142
|
+
for (let ivName in ivs) {
|
|
143
|
+
const iv = this.inputVars.find(x => x.name === ivName);
|
|
144
|
+
if (iv) {
|
|
145
|
+
errors.push(`Input Var [${ivName}] has already been defined, with value "${iv.value}"`);
|
|
146
|
+
}
|
|
147
|
+
else if (FORBIDDEN_NAMES.includes(ivName.toLowerCase())) {
|
|
148
|
+
errors.push(`Input Var name [${ivName}] cannot be used, please pick another one!`);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const ivVal = ivs[ivName];
|
|
152
|
+
if (typeof ivVal !== "string") {
|
|
153
|
+
errors.push(`Input Var Definition can only be a single line (string)!`);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const ivMatch = /^((num|number|text|color)\s*;)?(.*)$/i.exec(ivVal);
|
|
157
|
+
ivMatch[2] = ivMatch[2] ? ivMatch[2].toLowerCase() : ivMatch[2];
|
|
158
|
+
const ivType = !ivMatch[2] ? "text"
|
|
159
|
+
: (ivMatch[2] === "num" || ivMatch[2] === "number") ? "number"
|
|
160
|
+
: (ivMatch[2] === "color") ? "color"
|
|
161
|
+
: "text";
|
|
162
|
+
const explicitText = ivMatch[2] === "text";
|
|
163
|
+
const valPart = ivMatch[3].trim();
|
|
164
|
+
const tryColor = isValidColor(valPart);
|
|
165
|
+
if (!valPart) {
|
|
166
|
+
errors.push(`Definition for input Var [${ivName}] is empty!`);
|
|
167
|
+
}
|
|
168
|
+
else if (ivType === "number") {
|
|
169
|
+
if (WT_REGEX.FLOAT_NUMBER.test(valPart)) {
|
|
170
|
+
this.inputVars.push({
|
|
171
|
+
name: ivName,
|
|
172
|
+
type: "number",
|
|
173
|
+
value: parseFloat(valPart),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
errors.push(`"${valPart}" cannot resolve to be a NUMBER`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else if (ivType === "color") {
|
|
181
|
+
if (tryColor) {
|
|
182
|
+
this.inputVars.push({
|
|
183
|
+
name: ivName,
|
|
184
|
+
type: "color",
|
|
185
|
+
value: tryColor
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
errors.push(`"${valPart}" cannot resolve to be some COLOR!`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
if (explicitText) {
|
|
194
|
+
this.inputVars.push({
|
|
195
|
+
name: ivName,
|
|
196
|
+
type: "text",
|
|
197
|
+
value: valPart,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
else if (WT_REGEX.FLOAT_NUMBER.test(valPart)) {
|
|
201
|
+
this.inputVars.push({
|
|
202
|
+
name: ivName,
|
|
203
|
+
type: "number",
|
|
204
|
+
value: parseFloat(valPart)
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
else if (tryColor) {
|
|
208
|
+
this.inputVars.push({
|
|
209
|
+
name: ivName,
|
|
210
|
+
type: "color",
|
|
211
|
+
value: tryColor
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.inputVars.push({
|
|
216
|
+
name: ivName,
|
|
217
|
+
type: "text",
|
|
218
|
+
value: valPart
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (errors.length > 0) {
|
|
226
|
+
return {
|
|
227
|
+
title: `Input Vars resolving failed`,
|
|
228
|
+
msgs: errors
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
cleanDefObj(obj) {
|
|
234
|
+
const acceptedFields = [
|
|
235
|
+
"config",
|
|
236
|
+
"viewBox", "viewArea", "viewBetween",
|
|
237
|
+
"vars",
|
|
238
|
+
"points", "shapes", "cpoints", "xpoints", "pointSeries", "shapeSeries",
|
|
239
|
+
"points2", "shapes2", "cpoints2", "xpoints2", "pointSeries2", "shapeSeries2",
|
|
240
|
+
"points3", "shapes3", "cpoints3", "xpoints3", "pointSeries3", "shapeSeries3",
|
|
241
|
+
"points4", "shapes4", "cpoints4", "xpoints4", "pointSeries4", "shapeSeries4",
|
|
242
|
+
"paths", "markers", "patterns",
|
|
243
|
+
"styles", "labelStyles",
|
|
244
|
+
"draw",
|
|
245
|
+
"ports",
|
|
246
|
+
];
|
|
247
|
+
return HuatuYml.copyObj(obj, acceptedFields);
|
|
248
|
+
}
|
|
249
|
+
}
|