api-render-ui 1.1.3 → 1.1.5

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 CHANGED
@@ -34,6 +34,36 @@ api-render-ui is a UI library that pursues simple and efficient data rendering f
34
34
  // 执行渲染
35
35
  apiRenderer.render(openapiSpec);
36
36
  ```
37
+
38
+ ### Theme support
39
+
40
+ You can control the theme via the `theme` option when creating `ApiRenderer`:
41
+
42
+ - Pass a string: `'light'` or `'dark'` to use built-in theme classes.
43
+ - Pass an object: supply a palette (CSS variables or short keys) to set custom colors.
44
+
45
+ Examples:
46
+
47
+ ```typescript
48
+ // built-in light
49
+ new ApiRenderer({ mountPoint: '#notebook', theme: 'light' }).render(openapiSpec);
50
+
51
+ // built-in dark
52
+ new ApiRenderer({ mountPoint: '#notebook', theme: 'dark' }).render(openapiSpec);
53
+
54
+ // custom palette
55
+ // custom palette (recommended keys)
56
+ new ApiRenderer({ mountPoint: '#notebook', theme: { main: '#ff7f50', aux: '#ffd1b3', embellish: '#ffc39e', surface: '#fff8f0' } }).render(openapiSpec);
57
+ ```
58
+
59
+ Palette keys supported:
60
+
61
+ - `main` — main (primary) color used for labels and primary accents.
62
+ - `aux` — auxiliary color used for hover and subtle accents.
63
+ - `embellish` / `embellishment` — embellishment color used for active states and outlines.
64
+ - `surface` — background surface color.
65
+ - `primary` — alias for `main`.
66
+ - Any CSS variable name (e.g. `--even-bg`) may also be provided directly.
37
67
  ### Usage in single html
38
68
  Please refer to [example](example/animal.html) in the source code.
39
69
 
package/dist/index.cjs CHANGED
@@ -27,13 +27,55 @@ module.exports = __toCommonJS(index_exports);
27
27
  // src/inlined-styles.ts
28
28
  var GLOBAL_STYLES = `.codigma-apioperatorlist {
29
29
  align-items: flex-start;
30
- background: white;
30
+ background: var(--surface, white);
31
31
  display: inline-flex;
32
32
  flex-direction: column;
33
33
  gap: 5px;
34
34
  justify-content: flex-start;
35
35
  width: 100%
36
36
  }
37
+ /* Theme variables and mappings */
38
+ :root {
39
+ --Labels---Vibrant---Controls-Primary-\u221A: #404040;
40
+ --primary: #404040;
41
+ --main-color: #404040; /* main / primary color */
42
+ --aux-color: #a9c7ef; /* auxiliary color for hover / accents */
43
+ --embellish-color: #ced8e7; /* embellishment color for active states */
44
+ --surface: white;
45
+ --text: black;
46
+ --even-bg: rgb(235, 239, 244);
47
+ --odd-bg: rgb(250, 246, 246);
48
+ --hover-bg: var(--aux-color, rgb(169, 199, 239));
49
+ --active-bg: var(--embellish-color, rgb(206, 216, 231));
50
+ }
51
+
52
+ .codigma-theme-light {
53
+ --main-color: #404040;
54
+ --aux-color: #a9c7ef;
55
+ --embellish-color: #ced8e7;
56
+ --primary: var(--main-color);
57
+ --Labels---Vibrant---Controls-Primary-\u221A: var(--main-color);
58
+ --surface: white;
59
+ --text: black;
60
+ --even-bg: rgb(235, 239, 244);
61
+ --odd-bg: rgb(250, 246, 246);
62
+ --hover-bg: var(--aux-color);
63
+ --active-bg: var(--embellish-color);
64
+ }
65
+
66
+ .codigma-theme-dark {
67
+ --main-color: #9fb8ff;
68
+ --aux-color: #334155;
69
+ --embellish-color: #22303a;
70
+ --primary: var(--main-color);
71
+ --Labels---Vibrant---Controls-Primary-\u221A: var(--main-color);
72
+ --surface: #1f1f1f;
73
+ --text: #e6e6e6;
74
+ --even-bg: #2a2a2a;
75
+ --odd-bg: #222222;
76
+ --hover-bg: var(--aux-color);
77
+ --active-bg: var(--embellish-color);
78
+ }
37
79
  .codigma-apioperator {
38
80
  align-items: center;
39
81
  align-self: stretch;
@@ -44,18 +86,21 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
44
86
  justify-content: flex-start
45
87
  }
46
88
  .codigma-apioperator:nth-child(even) {
47
- background-color: rgb(235, 239, 244);
89
+ color: var(--main-color, var(--primary, #404040));
48
90
  }
49
91
  .codigma-apioperator:nth-child(odd) {
50
- background-color: rgb(250, 246, 246);
92
+ color: var(--main-color, var(--primary, #404040));
51
93
  }
52
94
 
53
95
  .codigma-apioperator:hover {
54
96
  cursor: pointer;
55
- background: rgb(169, 199, 239);
97
+ color: var(--text, black);
98
+ background-color: var(--hover-bg, rgba(169,199,239,0.12));
99
+ transition: background-color .12s ease, color .12s ease;
100
+ border-radius: 4px;
56
101
  }
57
102
  .codigma-apioperator:active {
58
- background: rgb(206, 216, 231);
103
+ color: var(--main-color, var(--primary, #404040));
59
104
  }
60
105
  .codigma-method {
61
106
  align-items: center;
@@ -71,18 +116,18 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
71
116
  justify-content: center
72
117
  }
73
118
  .codigma-get {
74
- color: var(--Labels---Vibrant---Controls-Primary-\u221A, #404040);
119
+ color: var(--main-color, var(--primary, #404040));
75
120
  word-wrap: break-word
76
121
  }
77
122
  .codigma-requrl {
78
- color: black;
123
+ color: var(--text, black);
79
124
  flex: 1 1 0;
80
125
  word-wrap: break-word
81
126
  }
82
127
  /**********************apiunit css style*************************/
83
128
  .codigma-apiunit {
84
129
  align-items: flex-start;
85
- background: white;
130
+ background: var(--surface, white);
86
131
  box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
87
132
  display: inline-flex;
88
133
  flex-direction: column;
@@ -93,8 +138,8 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
93
138
  .codigma-apiunit-apioperator {
94
139
  align-items: center;
95
140
  align-self: stretch;
96
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
97
- border-top: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
141
+ border-bottom: 1px var(--main-color, var(--primary, #404040)) solid;
142
+ border-top: 1px var(--main-color, var(--primary, #404040)) solid;
98
143
  display: inline-flex;
99
144
  gap: 10px;
100
145
  height: 48px;
@@ -115,17 +160,18 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
115
160
  justify-content: center
116
161
  }
117
162
  .codigma-apiunit-post {
118
- color: var(--Labels---Vibrant---Controls-Primary-\u221A, #404040);
163
+ color: var(--main-color, var(--primary, #404040));
119
164
  word-wrap: break-word
120
165
  }
121
166
  .codigma-apiunit-requrl {
122
- color: black;
167
+ color: var(--text, black);
123
168
  flex: 1 1 0;
124
169
  word-wrap: break-word;
125
170
  border-left: 0;
126
171
  border-right: 0;
127
172
  border-top: 0;
128
- border-bottom: 1px solid black;
173
+ border-bottom: 1px var(--main-color, var(--primary, #404040)) solid;
174
+ height: 70%;
129
175
  }
130
176
  .codigma-apiunit-send-button {
131
177
  align-items: center;
@@ -136,7 +182,7 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
136
182
  width: 88px
137
183
  }
138
184
  .codigma-apiunit-send {
139
- color: black;
185
+ color: var(--text, black);
140
186
  word-wrap: break-word
141
187
  }
142
188
  .codigma-apiunit-send-button:hover {
@@ -165,7 +211,7 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
165
211
  }
166
212
  .codigma-apiunit-parameters-cnr {
167
213
  align-items: center;
168
- background: white;
214
+ background: var(--surface, white);
169
215
  display: flex;
170
216
  height: 42px;
171
217
  justify-content: space-between;
@@ -174,7 +220,7 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
174
220
  .codigma-apiunit-parakeyvalues {
175
221
  align-items: flex-start;
176
222
  align-self: stretch;
177
- background: white;
223
+ background: var(--surface, white);
178
224
  display: inline-flex;
179
225
  flex-direction: column;
180
226
  flex: 1 1 0;
@@ -192,11 +238,11 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
192
238
  }
193
239
  .codigma-apiunit-valuetext {
194
240
  align-self: stretch;
195
- background: white;
241
+ background: var(--surface, white);
196
242
  border-left: 0;
197
243
  border-right: 0;
198
244
  border-top: 0;
199
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
245
+ border-bottom: 1px var(--main-color, var(--primary, #404040)) solid;
200
246
  padding: 10px;
201
247
  width: 161px
202
248
  }
@@ -221,21 +267,21 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
221
267
  .codigma-apiunit-parakeyvalues {
222
268
  align-items: flex-start;
223
269
  align-self: stretch;
224
- background: white;
270
+ background: var(--surface, white);
225
271
  display: flex;
226
272
  flex: 1 1 0;
227
273
  gap: 10px;
228
274
  justify-content: flex-start;
229
275
  outline-offset: -1px;
230
- outline: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
276
+ outline: 1px var(--main-color, var(--primary, #404040)) solid;
231
277
  overflow: hidden;
232
278
  padding: 10px
233
279
  }
234
280
  .codigma-apiunit-reqresponse {
235
281
  align-items: flex-start;
236
282
  align-self: stretch;
237
- background: white;
238
- border-top: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
283
+ background: var(--surface, white);
284
+ border-top: 1px var(--main-color, var(--primary, #404040)) solid;
239
285
  display: flex;
240
286
  flex-direction: column;
241
287
  flex: 1 1 0;
@@ -253,7 +299,7 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
253
299
  }
254
300
  .codigma-apiunit-responsetitle {
255
301
  align-items: center;
256
- border-bottom: 1px var(--Labels---Vibrant---Controls-Primary-\u221A, #404040) solid;
302
+ border-bottom: 1px var(--main-color, var(--primary, #404040)) solid;
257
303
  display: flex;
258
304
  height: 42px;
259
305
  justify-content: space-between;
@@ -262,7 +308,7 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
262
308
  .codigma-apiunit-response-cnr {
263
309
  align-items: center;
264
310
  align-self: stretch;
265
- background: white;
311
+ background: var(--surface, white);
266
312
  display: flex;
267
313
  justify-content: space-between;
268
314
  padding-top: 2px;
@@ -274,6 +320,12 @@ var GLOBAL_STYLES = `.codigma-apioperatorlist {
274
320
  flex: 1 1 0;
275
321
  min-height: 5rem;
276
322
  /* padding: 2px */
323
+ }
324
+ .codigma-apiunit-apioperator:hover {
325
+ cursor: pointer;
326
+ color: var(--text, black);
327
+ background-color: var(--hover-bg, rgba(169,199,239,0.08));
328
+ transition: background-color .12s ease, color .12s ease;
277
329
  }`;
278
330
 
279
331
  // src/api-render-ui.ts
@@ -287,7 +339,9 @@ var ApiRenderer = class _ApiRenderer {
287
339
  mountPoint: document?.body,
288
340
  // 默认挂载到body
289
341
  className: "Apioperatorlist codigma-apioperatorlist",
290
- layerName: "apioperatorlist"
342
+ layerName: "apioperatorlist",
343
+ // theme: can be a string like 'light' or 'dark', or an object palette
344
+ theme: "light"
291
345
  }, options);
292
346
  this.container = null;
293
347
  }
@@ -306,6 +360,47 @@ var ApiRenderer = class _ApiRenderer {
306
360
  if (!mountElement) {
307
361
  throw new Error("Invalid mount point specified");
308
362
  }
363
+ try {
364
+ const themeOpt = this.options.theme;
365
+ if (typeof themeOpt === "string") {
366
+ mountElement.classList.add(`codigma-theme-${themeOpt}`);
367
+ } else if (themeOpt && typeof themeOpt === "object") {
368
+ const palette = themeOpt;
369
+ const setVar = (key, value) => {
370
+ if (value == null) return;
371
+ const varName = key.startsWith("--") ? key : `--${key}`;
372
+ try {
373
+ mountElement.style.setProperty(varName, String(value));
374
+ } catch {
375
+ }
376
+ };
377
+ if (palette.main) {
378
+ setVar("--main-color", palette.main);
379
+ setVar("--primary", palette.main);
380
+ setVar("--Labels---Vibrant---Controls-Primary-\u221A", palette.main);
381
+ }
382
+ if (palette.aux) {
383
+ setVar("--aux-color", palette.aux);
384
+ setVar("--hover-bg", palette.aux);
385
+ }
386
+ if (palette.embellish || palette.embellishment) {
387
+ setVar("--embellish-color", palette.embellish || palette.embellishment);
388
+ setVar("--active-bg", palette.embellish || palette.embellishment);
389
+ }
390
+ if (palette.primary) {
391
+ setVar("--primary", palette.primary);
392
+ setVar("--Labels---Vibrant---Controls-Primary-\u221A", palette.primary);
393
+ setVar("--main-color", palette.primary);
394
+ }
395
+ if (palette["--even-bg"]) setVar("--even-bg", palette["--even-bg"]);
396
+ if (palette["--odd-bg"]) setVar("--odd-bg", palette["--odd-bg"]);
397
+ if (palette.surface) setVar("--surface", palette.surface);
398
+ Object.entries(palette).forEach(([k, v]) => {
399
+ if (k.startsWith("--")) setVar(k, v);
400
+ });
401
+ }
402
+ } catch (e) {
403
+ }
309
404
  if (apiSpec["openapi"] && apiSpec["openapi"].startsWith("3.") || apiSpec["swagger"] && apiSpec["swagger"].startsWith("2.")) {
310
405
  const countApi = countOpenAPI(apiSpec);
311
406
  const apiOperatorList = parseOpenApiSpec(apiSpec, currentServer, serviceName);
@@ -1227,7 +1322,7 @@ function createSvg() {
1227
1322
  svg.setAttribute("xmlns", "http://www.w3.org/2000/svg");
1228
1323
  const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
1229
1324
  path.setAttribute("d", "M5.5 8.5L10.5 13.5L15.5 8.5");
1230
- path.setAttribute("stroke", "var(--Labels---Vibrant---Controls-Primary--, #404040)");
1325
+ path.setAttribute("stroke", "var(--main-color, var(--primary, #404040) )");
1231
1326
  path.setAttribute("stroke-width", "2");
1232
1327
  path.setAttribute("stroke-linecap", "round");
1233
1328
  path.setAttribute("stroke-linejoin", "round");
@@ -1395,6 +1490,9 @@ function parseOpenApiSpec(openapiSpec, currentServer, serviceName) {
1395
1490
  url = openapiSpec.servers[0].url + path;
1396
1491
  }
1397
1492
  const apiOperator = {
1493
+ id: uuid(),
1494
+ label: path,
1495
+ children: [],
1398
1496
  method: method.toUpperCase(),
1399
1497
  url,
1400
1498
  rawApiInfo: operation,
@@ -1487,6 +1585,21 @@ function parseOpenApiSpec(openapiSpec, currentServer, serviceName) {
1487
1585
  });
1488
1586
  return apiOperatorList;
1489
1587
  }
1588
+ function uuid() {
1589
+ let s = [];
1590
+ const hexDigits = "0123456789abcdef";
1591
+ for (let i = 0; i < 28; i++) {
1592
+ const start = Math.floor(Math.random() * 16);
1593
+ s[i] = hexDigits.substring(start, start + 1);
1594
+ }
1595
+ s[14] = "4";
1596
+ const start1 = s[19] & 3 | 8;
1597
+ s[19] = hexDigits.substring(start1, start1 + 1);
1598
+ s[8] = s[13] = s[18] = s[23] = "-";
1599
+ s[0] = "a";
1600
+ var uuid2 = s.join("");
1601
+ return uuid2;
1602
+ }
1490
1603
  function countOpenAPI(openapiSpec) {
1491
1604
  let apiCount = 0;
1492
1605
  const pathEntries = [];