@slexkit/mcp 0.2.0 → 0.3.1
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 +3 -3
- package/dist/data/llms-authoring.txt +2 -0
- package/dist/data/llms-capabilities.txt +126 -0
- package/dist/data/llms-components.txt +29 -7
- package/dist/data/llms-full.txt +1909 -153
- package/dist/data/llms-runtime.txt +18 -13
- package/dist/data/llms.txt +22 -1
- package/dist/data/slexkit-ai-manifest.json +717 -62
- package/dist/index.js +1483 -41
- package/dist/index.js.map +5 -5
- package/package.json +39 -39
package/dist/data/llms-full.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# SlexKit Full LLM Documentation
|
|
2
2
|
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
|
|
5
5
|
This file concatenates SlexKit's canonical English Markdown docs. `slex` fences are preserved exactly.
|
|
6
6
|
|
|
@@ -27,7 +27,7 @@ Source: README.md
|
|
|
27
27
|
<a href="README.zh-CN.md">简体中文</a>
|
|
28
28
|
</p>
|
|
29
29
|
<p>
|
|
30
|
-
<img alt="version" src="https://img.shields.io/badge/version-0.
|
|
30
|
+
<img alt="version" src="https://img.shields.io/badge/version-0.3.1-18181b">
|
|
31
31
|
<img alt="script" src="https://img.shields.io/badge/Slex-v0.1-18181b">
|
|
32
32
|
<img alt="TypeScript" src="https://img.shields.io/badge/runtime-TypeScript-3178c6">
|
|
33
33
|
<img alt="Svelte 5" src="https://img.shields.io/badge/components-Svelte_5-ff3e00">
|
|
@@ -43,6 +43,8 @@ It is built for chat messages, documents, agent panels, tool results, and AI-aut
|
|
|
43
43
|
|
|
44
44
|
## Installation
|
|
45
45
|
|
|
46
|
+
> Just want to use SlexKit in Obsidian? Open **Settings -> Community plugins**, search for **SlexKit**, then install and enable it. The npm package below is for developers integrating SlexKit into web apps, Markdown renderers, Streamdown, or custom hosts.
|
|
47
|
+
|
|
46
48
|
```sh
|
|
47
49
|
npm install slexkit
|
|
48
50
|
```
|
|
@@ -127,7 +129,6 @@ Markdown platforms without SlexKit support show the fallback text. Hosts with Sl
|
|
|
127
129
|
| `@slexkit/components-svelte` | `npm install slexkit @slexkit/runtime @slexkit/components-svelte` | Svelte component registration |
|
|
128
130
|
| `@slexkit/theme-shadcn` | `npm install @slexkit/theme-shadcn` | CSS theme tokens |
|
|
129
131
|
| `@slexkit/streamdown` | `npm install slexkit @slexkit/theme-shadcn @slexkit/streamdown streamdown react react-dom` | React / Streamdown Markdown renderer |
|
|
130
|
-
| `@slexkit/obsidian` | `npm install slexkit @slexkit/obsidian` | Obsidian plugin adapter |
|
|
131
132
|
| `@slexkit/mcp` | `npx -y @slexkit/mcp` | Read-only MCP server for docs, examples, and source validation |
|
|
132
133
|
|
|
133
134
|
See [Package Boundaries](site/content/reference/packages/en-US.md) for details.
|
|
@@ -139,7 +140,7 @@ See [Package Boundaries](site/content/reference/packages/en-US.md) for details.
|
|
|
139
140
|
| Browser DOM | `mount()`, `ingest()`, `boot()`, `disposeNamespace()` |
|
|
140
141
|
| Markdown renderers | `createSlexKitMarkdownRuntimeHost()` |
|
|
141
142
|
| React / Streamdown | `@slexkit/streamdown` |
|
|
142
|
-
| Obsidian |
|
|
143
|
+
| Obsidian | Install **SlexKit** from Community Plugins; release repo: <https://github.com/slexkit/obsidian-slexkit> |
|
|
143
144
|
| AI agents | `@slexkit/mcp`, `llms.txt`, SlexKit skill docs |
|
|
144
145
|
| Custom components | `register()`, `registerSvelteComponent()`, `registerSubset()` |
|
|
145
146
|
|
|
@@ -320,6 +321,8 @@ slexkitRenderMode: component
|
|
|
320
321
|
|
|
321
322
|
# Getting Started
|
|
322
323
|
|
|
324
|
+
> Just want to install the Obsidian plugin? Open **Settings -> Community plugins**, search for **SlexKit**, then install and enable it. The rest of this page is for developers integrating SlexKit into web apps, Markdown hosts, Streamdown, or custom runtimes.
|
|
325
|
+
|
|
323
326
|
Install `slexkit`, mount a trusted fragment, and you're off. Hand off Markdown / React / Obsidian details to dedicated guides — this page keeps the core integration path focused.
|
|
324
327
|
|
|
325
328
|
## Installation Entry
|
|
@@ -343,7 +346,7 @@ For clearer package boundaries, choose scoped packages by host:
|
|
|
343
346
|
| Official Svelte component registration | `npm install slexkit @slexkit/runtime @slexkit/components-svelte` |
|
|
344
347
|
| Standalone shadcn-token theme CSS | `npm install @slexkit/theme-shadcn` |
|
|
345
348
|
| React + Streamdown Markdown host | `npm install slexkit @slexkit/theme-shadcn @slexkit/streamdown streamdown react react-dom` |
|
|
346
|
-
| Obsidian vault rendering |
|
|
349
|
+
| Obsidian vault rendering | Install **SlexKit** from Obsidian Community Plugins |
|
|
347
350
|
|
|
348
351
|
`@slexkit/runtime` and `@slexkit/components-svelte` are thin wrappers around the root package, not independent implementations.
|
|
349
352
|
|
|
@@ -458,17 +461,17 @@ slexkitRenderMode: component
|
|
|
458
461
|
|
|
459
462
|
# Integration
|
|
460
463
|
|
|
461
|
-
SlexKit ships
|
|
464
|
+
SlexKit ships the Streamdown package in this repository and maintains the official Obsidian plugin in a separate release repository. Both integrations process only explicit `slex` fences — they don't scan ordinary JavaScript, JSON, or unlabeled code blocks. For the full API and host contract, see the [Host Integration reference](/docs/reference/integration).
|
|
462
465
|
|
|
463
466
|
## Plugin Selection
|
|
464
467
|
|
|
465
468
|
| Host | Package | Use case | Runtime boundary |
|
|
466
469
|
|---|---|---|---|
|
|
467
470
|
| React / Streamdown | `@slexkit/streamdown` | Chat messages, AI output, React Markdown pages | trusted or secure |
|
|
468
|
-
| Obsidian |
|
|
471
|
+
| Obsidian | `slexkit/obsidian-slexkit` | Slex fences in local vault reading mode | trusted readonly |
|
|
469
472
|
| Custom Markdown host | `slexkit` | Product-specific Markdown renderer or document viewer | trusted or secure |
|
|
470
473
|
|
|
471
|
-
Use the packaged plugin when the host is Streamdown
|
|
474
|
+
Use the packaged plugin when the host is Streamdown. Use the separate [SlexKit plugin repository](https://github.com/slexkit/obsidian-slexkit) for Obsidian installs and releases. Use `createSlexKitMarkdownRuntimeHost` directly for custom Markdown renderers.
|
|
472
475
|
|
|
473
476
|
Package installation details and release boundaries are tracked in [Package Boundaries](/docs/reference/packages).
|
|
474
477
|
|
|
@@ -559,15 +562,26 @@ The secure runtime deployment checklist lives in [Secure Runtime Setup](security
|
|
|
559
562
|
|
|
560
563
|
## Obsidian
|
|
561
564
|
|
|
565
|
+
> If your goal is only to install the Obsidian plugin, you do not need the developer integration material above. Search for **SlexKit** in Obsidian **Community plugins**, then install and enable it.
|
|
566
|
+
|
|
562
567
|
The Obsidian plugin targets local vault content. It registers a `slex` code block processor in reading mode, renders the fence as a readonly interactive fragment, and does not write output back to notes.
|
|
563
568
|
|
|
564
|
-
|
|
569
|
+
Install the plugin from Obsidian Community Plugins:
|
|
565
570
|
|
|
566
|
-
|
|
567
|
-
|
|
571
|
+
1. Open **Settings -> Community plugins**.
|
|
572
|
+
2. Disable **Restricted mode** if needed.
|
|
573
|
+
3. Search for **SlexKit**.
|
|
574
|
+
4. Install and enable the plugin.
|
|
575
|
+
|
|
576
|
+
The current community release is desktop-only until mobile vault testing is complete. The plugin is compatible with Obsidian 1.5.0+.
|
|
577
|
+
|
|
578
|
+
BRAT and manual release assets remain useful for testing unreleased builds:
|
|
579
|
+
|
|
580
|
+
```text
|
|
581
|
+
BRAT repository: https://github.com/slexkit/obsidian-slexkit
|
|
568
582
|
```
|
|
569
583
|
|
|
570
|
-
|
|
584
|
+
Manual installs copy the GitHub release assets into the vault:
|
|
571
585
|
|
|
572
586
|
```text
|
|
573
587
|
.obsidian/plugins/slexkit/
|
|
@@ -603,7 +617,7 @@ Blocks in the same note share one Markdown artifact runtime, so a state-only fen
|
|
|
603
617
|
|
|
604
618
|
## Obsidian Boundary
|
|
605
619
|
|
|
606
|
-
|
|
620
|
+
The official plugin is a trusted readonly adapter. Content comes from the user's local vault; the plugin is not a sandbox for third-party Markdown or agent output.
|
|
607
621
|
|
|
608
622
|
For untrusted content, use secure mode in a web host with an explicit sandbox frame and host policy.
|
|
609
623
|
|
|
@@ -1058,7 +1072,7 @@ For exact policy fields and allowed adapter hooks, use the [Security Runtime Con
|
|
|
1058
1072
|
|
|
1059
1073
|
`@slexkit/streamdown` can run trusted or secure. Use secure mode for chat messages and agent output unless the message source is already trusted by the host.
|
|
1060
1074
|
|
|
1061
|
-
|
|
1075
|
+
The official Obsidian plugin is a trusted readonly adapter for local vault content. It should not be used as the isolation boundary for third-party Markdown or direct agent output.
|
|
1062
1076
|
|
|
1063
1077
|
Custom Markdown hosts should still process only fences whose language is exactly `slex` and should preserve readable Markdown fallback for non-SlexKit environments.
|
|
1064
1078
|
|
|
@@ -1089,175 +1103,1776 @@ summary: "LLM docs, MCP server, skills, and authoring rules for SlexKit agents."
|
|
|
1089
1103
|
slexkitRenderMode: component
|
|
1090
1104
|
---
|
|
1091
1105
|
|
|
1092
|
-
# AI / Agents
|
|
1106
|
+
# AI / Agents
|
|
1107
|
+
|
|
1108
|
+
## AI Accessible Documentation
|
|
1109
|
+
|
|
1110
|
+
SlexKit follows the assistant-ui information architecture: a clear index, a full-context file, task-oriented skills, and a minimal MCP surface. Raw docs stay as `.md` pages, and interactive examples use explicit `slex` fences.
|
|
1111
|
+
|
|
1112
|
+
```slex
|
|
1113
|
+
{
|
|
1114
|
+
namespace: "ai_docs_links",
|
|
1115
|
+
layout: {
|
|
1116
|
+
"column:links": {
|
|
1117
|
+
gap: "sm",
|
|
1118
|
+
"link:index": { href: "/llms.txt", text: "/llms.txt - docs index", icon: "list-magnifying-glass" },
|
|
1119
|
+
"link:full": { href: "/llms-full.txt", text: "/llms-full.txt - full English context", icon: "book-open-text" },
|
|
1120
|
+
"link:components": { href: "/llms-components.txt", text: "/llms-components.txt - components and API", icon: "puzzle-piece" },
|
|
1121
|
+
"link:runtime": { href: "/llms-runtime.txt", text: "/llms-runtime.txt - runtime and host integration", icon: "cpu" },
|
|
1122
|
+
"link:capabilities": { href: "/llms-capabilities.txt", text: "/llms-capabilities.txt - std and api capabilities", icon: "function" },
|
|
1123
|
+
"link:toolhost": { href: "/llms-toolhost.txt", text: "/llms-toolhost.txt - structured input", icon: "cursor-click" },
|
|
1124
|
+
"link:authoring": { href: "/llms-authoring.txt", text: "/llms-authoring.txt - slex fence authoring rules", icon: "pencil-simple" },
|
|
1125
|
+
"link:manifest": { href: "/slexkit-ai-manifest.json", text: "/slexkit-ai-manifest.json - machine-readable index", icon: "brackets-curly" },
|
|
1126
|
+
"text:note": { text: "Raw docs use .md routes such as /docs/components/card.md. Do not add .mdx routes." }
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
```
|
|
1131
|
+
|
|
1132
|
+
Minimal reading path:
|
|
1133
|
+
|
|
1134
|
+
1. Start with [`/llms.txt`](/llms.txt) for the grouped index.
|
|
1135
|
+
2. Use [`/llms-full.txt`](/llms-full.txt) when the agent needs broad context.
|
|
1136
|
+
3. Use [`/llms-components.txt`](/llms-components.txt) and raw component `.md` pages when authoring UI.
|
|
1137
|
+
4. Use [`/llms-capabilities.txt`](/llms-capabilities.txt) for `std.*` and policy-gated `api.*`.
|
|
1138
|
+
5. Use [`/llms-runtime.txt`](/llms-runtime.txt) for host and secure runtime integration.
|
|
1139
|
+
6. Use [`/llms-toolhost.txt`](/llms-toolhost.txt) only when user input must return structured data to the host.
|
|
1140
|
+
|
|
1141
|
+
SlexKit raw docs are ordinary `.md` pages with explicit `slex` fences. There is no `.mdx` route — `slex` fences are the interactive layer.
|
|
1142
|
+
|
|
1143
|
+
## Context Files
|
|
1144
|
+
|
|
1145
|
+
Add SlexKit context to `AGENTS.md`, `CLAUDE.md`, or `.cursorrules`:
|
|
1146
|
+
|
|
1147
|
+
````md
|
|
1148
|
+
## SlexKit
|
|
1149
|
+
|
|
1150
|
+
This project uses SlexKit for Markdown-native interactive AI output.
|
|
1151
|
+
|
|
1152
|
+
Documentation: https://slexkit.dev/llms-full.txt
|
|
1153
|
+
|
|
1154
|
+
Key patterns:
|
|
1155
|
+
- Display UI uses explicit `slex` fenced blocks plus Markdown fallback.
|
|
1156
|
+
- Slex source uses `{ slex, namespace, g, layout }`; use `slex: "0.1"` for the current public protocol.
|
|
1157
|
+
- Use `std.*` for common calculations, formatting, units, and small statistics.
|
|
1158
|
+
- ToolHost is only for structured user input flows.
|
|
1159
|
+
- Untrusted or agent-generated source should use the secure runtime.
|
|
1160
|
+
- Raw docs are `.md` files with `slex` fences, not `.mdx`.
|
|
1161
|
+
````
|
|
1162
|
+
|
|
1163
|
+
## Skills
|
|
1164
|
+
|
|
1165
|
+
The `skills/` directory provides these task entry points:
|
|
1166
|
+
|
|
1167
|
+
- `/slexkit`: overview, architecture boundaries, and positioning
|
|
1168
|
+
- `/author`: write display-oriented `slex` fences with Markdown fallback
|
|
1169
|
+
- `/host`: integrate Markdown, Streamdown, Obsidian, or custom hosts
|
|
1170
|
+
- `/toolhost`: build confirmations, choices, and structured forms
|
|
1171
|
+
- `/secure`: configure sandbox runtime and host policy
|
|
1172
|
+
- `/update`: regenerate AI docs after API, docs, or component changes
|
|
1173
|
+
|
|
1174
|
+
Use `/author` for display UI. Use `/toolhost` when the host must receive a submitted result.
|
|
1175
|
+
|
|
1176
|
+
## MCP
|
|
1177
|
+
|
|
1178
|
+
`@slexkit/mcp` provides read-only access to SlexKit documentation, examples, and Slex source validation. Keep the public surface small and natural: docs, examples, validate.
|
|
1179
|
+
|
|
1180
|
+
```slex
|
|
1181
|
+
{
|
|
1182
|
+
namespace: "ai_mcp_tools",
|
|
1183
|
+
layout: {
|
|
1184
|
+
"grid:tools": {
|
|
1185
|
+
columns: 1,
|
|
1186
|
+
mdColumns: 3,
|
|
1187
|
+
"card:docs": {
|
|
1188
|
+
title: "slexkitDocs",
|
|
1189
|
+
icon: "book-open-text",
|
|
1190
|
+
"text:body": { text: "Search or fetch Markdown docs by query, group, slug, or raw .md URL." }
|
|
1191
|
+
},
|
|
1192
|
+
"card:examples": {
|
|
1193
|
+
title: "slexkitExamples",
|
|
1194
|
+
icon: "code",
|
|
1195
|
+
"text:body": { text: "Browse component examples, ToolHost templates, and host integration snippets." }
|
|
1196
|
+
},
|
|
1197
|
+
"card:validate": {
|
|
1198
|
+
title: "slexkitValidate",
|
|
1199
|
+
icon: "check-circle",
|
|
1200
|
+
"text:body": { text: "Parse Slex source and return diagnostics plus component usage." }
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
```
|
|
1206
|
+
|
|
1207
|
+
### Quick Install
|
|
1208
|
+
|
|
1209
|
+
```sh
|
|
1210
|
+
npx add-mcp @slexkit/mcp
|
|
1211
|
+
```
|
|
1212
|
+
|
|
1213
|
+
Or specify an app:
|
|
1214
|
+
|
|
1215
|
+
```sh
|
|
1216
|
+
npx add-mcp @slexkit/mcp -a claude-code
|
|
1217
|
+
npx add-mcp @slexkit/mcp -a codex
|
|
1218
|
+
npx add-mcp @slexkit/mcp -a cursor
|
|
1219
|
+
npx add-mcp @slexkit/mcp -a vscode
|
|
1220
|
+
npx add-mcp @slexkit/mcp -a zed
|
|
1221
|
+
```
|
|
1222
|
+
|
|
1223
|
+
### Manual Installation
|
|
1224
|
+
|
|
1225
|
+
```slex
|
|
1226
|
+
{
|
|
1227
|
+
namespace: "ai_manual_configs",
|
|
1228
|
+
layout: {
|
|
1229
|
+
"tabs:manualConfigs": {
|
|
1230
|
+
value: "cursor",
|
|
1231
|
+
tabs: [
|
|
1232
|
+
{
|
|
1233
|
+
value: "cursor",
|
|
1234
|
+
label: "Cursor",
|
|
1235
|
+
content: {
|
|
1236
|
+
"code-block:cursor": {
|
|
1237
|
+
title: ".cursor/mcp.json",
|
|
1238
|
+
language: "json",
|
|
1239
|
+
code: "{\n \"mcpServers\": {\n \"slexkit\": {\n \"command\": \"npx\",\n \"args\": [\"-y\", \"@slexkit/mcp\"]\n }\n }\n}"
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
},
|
|
1243
|
+
{
|
|
1244
|
+
value: "codex",
|
|
1245
|
+
label: "Codex",
|
|
1246
|
+
content: {
|
|
1247
|
+
"code-block:codex": {
|
|
1248
|
+
title: "config.toml",
|
|
1249
|
+
language: "toml",
|
|
1250
|
+
code: "[mcp_servers.slexkit]\ncommand = \"npx\"\nargs = [\"-y\", \"@slexkit/mcp\"]"
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
},
|
|
1254
|
+
{
|
|
1255
|
+
value: "vscode",
|
|
1256
|
+
label: "VS Code",
|
|
1257
|
+
content: {
|
|
1258
|
+
"code-block:vscode": {
|
|
1259
|
+
title: ".vscode/mcp.json",
|
|
1260
|
+
language: "json",
|
|
1261
|
+
code: "{\n \"servers\": {\n \"slexkit\": {\n \"command\": \"npx\",\n \"args\": [\"-y\", \"@slexkit/mcp\"],\n \"type\": \"stdio\"\n }\n }\n}"
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
]
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
```
|
|
1270
|
+
|
|
1271
|
+
## Troubleshooting
|
|
1272
|
+
|
|
1273
|
+
- MCP server does not start: verify `npx` and MCP config JSON, then restart the IDE.
|
|
1274
|
+
- Tool calls fail: restart the MCP server and confirm `tools/list` exposes only `slexkitDocs`, `slexkitExamples`, and `slexkitValidate`.
|
|
1275
|
+
- Docs are stale: run `bun run ai:docs` or `bun run build:core`.
|
|
1276
|
+
- Wrong raw source route: use `.md` routes with `slex` fences. Do not request `.mdx`.
|
|
1277
|
+
|
|
1278
|
+
---
|
|
1279
|
+
|
|
1280
|
+
# 你的第一个 SlexKit 卡片
|
|
1281
|
+
|
|
1282
|
+
URL: /zh-CN/examples/hello-slexkit
|
|
1283
|
+
Raw Markdown: /zh-CN/examples/hello-slexkit.md
|
|
1284
|
+
Source: content/examples/hello-slexkit/zh-CN.md
|
|
1285
|
+
|
|
1286
|
+
# 你的第一个 SlexKit 卡片
|
|
1287
|
+
|
|
1288
|
+
SlexKit 的核心思想:**用声明式 JSON 描述 UI,而不仅是 Markdown**。下面是一个纯静态的卡片——没有 `g` 对象,没有交互,所有内容都直接写在结构中。
|
|
1289
|
+
|
|
1290
|
+
```slex
|
|
1291
|
+
{
|
|
1292
|
+
slex: "0.1",
|
|
1293
|
+
namespace: "learn_hello_slexkit",
|
|
1294
|
+
layout: {
|
|
1295
|
+
"section:hello": {
|
|
1296
|
+
eyebrow: "入门教程 · 1/4",
|
|
1297
|
+
title: "你的第一个 SlexKit 卡片",
|
|
1298
|
+
subtitle: "所有内容都是声明式的——数字、颜色、布局,全部来自 DSL。",
|
|
1299
|
+
"grid:top-stats": {
|
|
1300
|
+
columns: 1, mdColumns: 3,
|
|
1301
|
+
"stat:users": { label: "活跃用户", value: "12,847", unit: "人" },
|
|
1302
|
+
"stat:uptime": { label: "正常运行", value: "99.97", unit: "%" },
|
|
1303
|
+
"stat:latency": { label: "服务延迟", value: "42", unit: "ms" }
|
|
1304
|
+
},
|
|
1305
|
+
"table:pricing": {
|
|
1306
|
+
columns: ["功能", "免费版", "专业版"],
|
|
1307
|
+
rows: [
|
|
1308
|
+
["可用组件", "全部", "全部"],
|
|
1309
|
+
["自定义主题", "3 种", "无限"],
|
|
1310
|
+
["数据导出", "JSON", "JSON / CSV / SQL"]
|
|
1311
|
+
]
|
|
1312
|
+
},
|
|
1313
|
+
"callout:tip": {
|
|
1314
|
+
tone: "info",
|
|
1315
|
+
text: "你现在看到的每一样东西——标题、数值、表格、颜色——都来自上面的 DSL 声明,没有一行 HTML。这就是 SlexKit 的核心理念:Markdown 提供叙事,DSL 提供交互。"
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
```
|
|
1321
|
+
|
|
1322
|
+
只看不动,感受一下结构和布局语法。下一节我们给卡片加上第一条响应式数据。
|
|
1323
|
+
|
|
1324
|
+
---
|
|
1325
|
+
|
|
1326
|
+
|
|
1327
|
+
思考:如果 `"12,847"` 需要从里面计算出来,显然直接写死字符串不够。这就需要引入 **`g` 对象**——下一节的主角。
|
|
1328
|
+
|
|
1329
|
+
---
|
|
1330
|
+
|
|
1331
|
+
# 第一个交互:滑块操控数据
|
|
1332
|
+
|
|
1333
|
+
URL: /zh-CN/examples/first-interaction
|
|
1334
|
+
Raw Markdown: /zh-CN/examples/first-interaction.md
|
|
1335
|
+
Source: content/examples/first-interaction/zh-CN.md
|
|
1336
|
+
|
|
1337
|
+
# 第一个交互:滑块操控数据
|
|
1338
|
+
|
|
1339
|
+
上一节全是静态数据。要让 UI "活起来",需要三样东西:
|
|
1340
|
+
|
|
1341
|
+
1. **`g` 对象** — 响应式状态容器
|
|
1342
|
+
2. **`$value` / `$label` / `$tone`** — 读表达式(从 g 取值渲染)
|
|
1343
|
+
3. **`onchange`** — 写表达式(用户操作写入 g)
|
|
1344
|
+
|
|
1345
|
+
```slex
|
|
1346
|
+
{
|
|
1347
|
+
slex: "0.1",
|
|
1348
|
+
namespace: "learn_first_interaction",
|
|
1349
|
+
g: { count: 42 },
|
|
1350
|
+
layout: {
|
|
1351
|
+
"section:interact": {
|
|
1352
|
+
eyebrow: "入门教程 · 2/4",
|
|
1353
|
+
title: "第一个交互:滑块操控数据",
|
|
1354
|
+
subtitle: "滑动下面的滑块,stat 和 callout 会跟着变。这就是 SlexKit 的响应式核心。",
|
|
1355
|
+
"column:controls": {
|
|
1356
|
+
"slider:count": {
|
|
1357
|
+
label: "选择数值",
|
|
1358
|
+
"$value": "g.count",
|
|
1359
|
+
min: 0,
|
|
1360
|
+
max: 100,
|
|
1361
|
+
step: 1,
|
|
1362
|
+
onchange: "g.count = Number($event)"
|
|
1363
|
+
},
|
|
1364
|
+
"stat:countStat": { label: "当前数值", "$value": "g.count" },
|
|
1365
|
+
"badge:level": {
|
|
1366
|
+
"$label": "g.count < 30 ? '低' : g.count < 70 ? '中' : '高'",
|
|
1367
|
+
"$tone": "g.count < 30 ? 'success' : g.count < 70 ? 'warning' : 'danger'"
|
|
1368
|
+
},
|
|
1369
|
+
"callout:note": {
|
|
1370
|
+
"$tone": "g.count < 50 ? 'success' : 'info'",
|
|
1371
|
+
"$text": "g.count < 50 ? '当前值偏低,适合入门级负载。' : '当前值偏高,注意监控资源消耗。'"
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
```
|
|
1378
|
+
|
|
1379
|
+
核心公式:
|
|
1380
|
+
```
|
|
1381
|
+
用户操作 → onchange → g → SlexKit 检测变化 → 所有 $value/$tone/$text 自动重算 → UI 更新
|
|
1382
|
+
```
|
|
1383
|
+
|
|
1384
|
+
这就是**单向数据流** + **响应式重渲染**。不需要手动 `setState`,不需要 DOM 操作。
|
|
1385
|
+
|
|
1386
|
+
---
|
|
1387
|
+
|
|
1388
|
+
# 多输入协同:两个滑块联动
|
|
1389
|
+
|
|
1390
|
+
URL: /zh-CN/examples/multi-input-coordination
|
|
1391
|
+
Raw Markdown: /zh-CN/examples/multi-input-coordination.md
|
|
1392
|
+
Source: content/examples/multi-input-coordination/zh-CN.md
|
|
1393
|
+
|
|
1394
|
+
# 多输入协同:两个滑块联动
|
|
1395
|
+
|
|
1396
|
+
真实场景中往往有多个输入变量,它们相互影响。这需要引入两样新东西:
|
|
1397
|
+
1. **`g.method()`** — 依赖其他状态的计算值(类似 Vue computed)
|
|
1398
|
+
2. **`$if`** — 条件渲染(根据状态决定显示或隐藏组件)
|
|
1399
|
+
|
|
1400
|
+
```slex
|
|
1401
|
+
{
|
|
1402
|
+
slex: "0.1",
|
|
1403
|
+
namespace: "learn_multi_coordination",
|
|
1404
|
+
g: {
|
|
1405
|
+
width: 120,
|
|
1406
|
+
height: 80,
|
|
1407
|
+
area: function () { return this.width * this.height; },
|
|
1408
|
+
isLandscape: function () { return this.width > this.height; },
|
|
1409
|
+
ratio: function () { return (this.width / this.height).toFixed(2); }
|
|
1410
|
+
},
|
|
1411
|
+
layout: {
|
|
1412
|
+
"section:coordinated": {
|
|
1413
|
+
eyebrow: "入门教程 · 3/4",
|
|
1414
|
+
title: "多输入协同:矩形尺寸联动",
|
|
1415
|
+
subtitle: "同时调整宽和高,面积和宽高比自动重新计算。这就是 g 方法的力量。",
|
|
1416
|
+
"grid:params": {
|
|
1417
|
+
columns: 1, mdColumns: 2,
|
|
1418
|
+
"column:w": {
|
|
1419
|
+
"input:width": { label: "宽度", "$value": "g.width", type: "number", unit: "px", onchange: "g.width = Number($event || 0)" },
|
|
1420
|
+
"slider:width": { label: "宽度", "$value": "g.width", min: 20, max: 300, step: 5, unit: "px", onchange: "g.width = Number($event)" }
|
|
1421
|
+
},
|
|
1422
|
+
"column:h": {
|
|
1423
|
+
"input:height": { label: "高度", "$value": "g.height", type: "number", unit: "px", onchange: "g.height = Number($event || 0)" },
|
|
1424
|
+
"slider:height": { label: "高度", "$value": "g.height", min: 20, max: 300, step: 5, unit: "px", onchange: "g.height = Number($event)" }
|
|
1425
|
+
}
|
|
1426
|
+
},
|
|
1427
|
+
"grid:results": {
|
|
1428
|
+
columns: 1, mdColumns: 3,
|
|
1429
|
+
"stat:area": { label: "面积", "$value": "g.area()", unit: "px²" },
|
|
1430
|
+
"stat:ratio": { label: "宽高比", "$value": "g.ratio()" },
|
|
1431
|
+
"badge:orientation": {
|
|
1432
|
+
"$label": "g.isLandscape() ? '横向' : g.width === g.height ? '正方形' : '纵向'",
|
|
1433
|
+
"$tone": "g.isLandscape() ? 'info' : g.width === g.height ? 'success' : 'warning'"
|
|
1434
|
+
}
|
|
1435
|
+
},
|
|
1436
|
+
"formula:areaEq": { "$tex": "'\\\\text{面积} = ' + g.width + ' \\\\times ' + g.height + ' = ' + g.area() + '\\\\text{ px}^2'" },
|
|
1437
|
+
"callout:tip": {
|
|
1438
|
+
"$tone": "g.isLandscape() ? 'info' : 'warning'",
|
|
1439
|
+
"$text": "g.isLandscape() ? '当前为横向(Landscape)。横向更适用于宽屏展示。' : '当前为纵向(Portrait)。纵向更适用于移动端阅读。'"
|
|
1440
|
+
},
|
|
1441
|
+
"callout:squareTip": {
|
|
1442
|
+
"$if": "g.width === g.height",
|
|
1443
|
+
tone: "success",
|
|
1444
|
+
text: "这是一个正方形!宽高完全相等。"
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
```
|
|
1450
|
+
|
|
1451
|
+
**三个新知识点:**
|
|
1452
|
+
|
|
1453
|
+
| 概念 | 写法 | 含义 |
|
|
1454
|
+
|:---|:---|:---|
|
|
1455
|
+
| g 方法 | `area: function() { return this.width * this.height; }` | `this` 指向 g 对象本身,返回动态计算值 |
|
|
1456
|
+
| 动态公式 | `"$tex": "'...' + g.width + '...'"` | formula 组件可根据 g 值实时渲染 KaTeX |
|
|
1457
|
+
| 条件渲染 | `"$if": "g.width === g.height"` | 只有表达式返回 true 时该组件才渲染 |
|
|
1458
|
+
|
|
1459
|
+
---
|
|
1460
|
+
|
|
1461
|
+
# 分支与切换:模式选择器
|
|
1462
|
+
|
|
1463
|
+
URL: /zh-CN/examples/tabs-and-branching
|
|
1464
|
+
Raw Markdown: /zh-CN/examples/tabs-and-branching.md
|
|
1465
|
+
Source: content/examples/tabs-and-branching/zh-CN.md
|
|
1466
|
+
|
|
1467
|
+
# 分支与切换:模式选择器
|
|
1468
|
+
|
|
1469
|
+
上一节是一个场景内的协同。现实中有多个场景需要在同一空间内切换——这时候用 **select** 实现分支。
|
|
1470
|
+
|
|
1471
|
+
核心思想:**UI = f(state)**。切换 `mode` 状态变量,整个视图区域自动切换。
|
|
1472
|
+
|
|
1473
|
+
```slex
|
|
1474
|
+
{
|
|
1475
|
+
slex: "0.1",
|
|
1476
|
+
namespace: "learn_tabs_branching",
|
|
1477
|
+
g: {
|
|
1478
|
+
mode: "length",
|
|
1479
|
+
value: 100,
|
|
1480
|
+
convert: function () {
|
|
1481
|
+
if (this.mode === "length") return (this.value / 100).toFixed(2) + " m";
|
|
1482
|
+
if (this.mode === "weight") return (this.value * 2.20462).toFixed(2) + " 磅 (lbs)";
|
|
1483
|
+
if (this.mode === "temp") return (this.value * 9 / 5 + 32).toFixed(1) + " °F";
|
|
1484
|
+
return "—";
|
|
1485
|
+
},
|
|
1486
|
+
label: function () {
|
|
1487
|
+
if (this.mode === "length") return "厘米转米";
|
|
1488
|
+
if (this.mode === "weight") return "公斤转磅";
|
|
1489
|
+
return "摄氏度转华氏度";
|
|
1490
|
+
}
|
|
1491
|
+
},
|
|
1492
|
+
layout: {
|
|
1493
|
+
"section:branching": {
|
|
1494
|
+
eyebrow: "入门教程 · 4/4",
|
|
1495
|
+
title: "分支与切换:模式选择器",
|
|
1496
|
+
subtitle: "切换下面的模式,输入的参数和计算结果会跟着变化。一种模式 = 一种 UI 状态。",
|
|
1497
|
+
"select:mode": {
|
|
1498
|
+
label: "转换模式",
|
|
1499
|
+
"$value": "g.mode",
|
|
1500
|
+
options: [
|
|
1501
|
+
{ label: "长度 (cm → m)", value: "length" },
|
|
1502
|
+
{ label: "重量 (kg → lbs)", value: "weight" },
|
|
1503
|
+
{ label: "温度 (°C → °F)", value: "temp" }
|
|
1504
|
+
],
|
|
1505
|
+
onchange: "g.mode = String($event)"
|
|
1506
|
+
},
|
|
1507
|
+
"input:value": { label: "输入值", "$value": "g.value", type: "number", onchange: "g.value = Number($event || 0)" },
|
|
1508
|
+
"stat:result": { "$label": "g.label()", "$value": "g.convert()" },
|
|
1509
|
+
"callout:guide": {
|
|
1510
|
+
"$tone": "g.mode === 'temp' ? 'warning' : 'info'",
|
|
1511
|
+
"$text": "g.mode === 'length' ? '1 米 = 100 厘米,除以 100 即可。' : g.mode === 'weight' ? '1 公斤 ≈ 2.20462 磅。' : '°F = °C × 9/5 + 32。华氏度范围更大,注意精度。'"
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
```
|
|
1517
|
+
|
|
1518
|
+
---
|
|
1519
|
+
|
|
1520
|
+
# 五险一金计算器
|
|
1521
|
+
|
|
1522
|
+
URL: /zh-CN/examples/salary-calculator
|
|
1523
|
+
Raw Markdown: /zh-CN/examples/salary-calculator.md
|
|
1524
|
+
Source: content/examples/salary-calculator/zh-CN.md
|
|
1525
|
+
|
|
1526
|
+
# 五险一金计算器
|
|
1527
|
+
|
|
1528
|
+
你面试拿到一个offer,月薪2万,到手能拿多少?HR说五险一金要扣一大笔,但具体扣多少、怎么算,每个城市还不一样。
|
|
1529
|
+
|
|
1530
|
+
```slex
|
|
1531
|
+
{
|
|
1532
|
+
slex: "0.1",
|
|
1533
|
+
namespace: "example_salary_calculator",
|
|
1534
|
+
g: {
|
|
1535
|
+
base: 20000,
|
|
1536
|
+
city: "beijing",
|
|
1537
|
+
rates: {
|
|
1538
|
+
beijing: { pensionP: 8, medicalP: 2, unemploymentP: 0.5, pensionC: 16, medicalC: 8, unemploymentC: 0.5, injury: 0.2, maternity: 0.8, housing: 12 },
|
|
1539
|
+
shanghai: { pensionP: 8, medicalP: 2, unemploymentP: 0.5, pensionC: 16, medicalC: 8, unemploymentC: 0.5, injury: 0.2, maternity: 0.8, housing: 7 },
|
|
1540
|
+
guangzhou: { pensionP: 8, medicalP: 2, unemploymentP: 0.5, pensionC: 16, medicalC: 8, unemploymentC: 0.5, injury: 0.2, maternity: 0.8, housing: 5 },
|
|
1541
|
+
shenzhen: { pensionP: 8, medicalP: 2, unemploymentP: 0.5, pensionC: 16, medicalC: 8, unemploymentC: 0.5, injury: 0.2, maternity: 0.8, housing: 5 }
|
|
1542
|
+
},
|
|
1543
|
+
currentRate: function () { return this.rates[this.city] || this.rates.beijing; },
|
|
1544
|
+
personalRate: function () { var r = this.currentRate(); return r.pensionP + r.medicalP + r.unemploymentP + r.housing; },
|
|
1545
|
+
companyRate: function () { var r = this.currentRate(); return r.pensionC + r.medicalC + r.unemploymentC + r.injury + r.maternity + r.housing; },
|
|
1546
|
+
personalTotal: function () { return this.base * this.personalRate() / 100; },
|
|
1547
|
+
companyTotal: function () { return this.base * this.companyRate() / 100; },
|
|
1548
|
+
total: function () { return this.personalTotal() + this.companyTotal(); },
|
|
1549
|
+
takeHome: function () { return this.base - this.personalTotal(); },
|
|
1550
|
+
cityLabel: function () { return { beijing: "北京", shanghai: "上海", guangzhou: "广州", shenzhen: "深圳" }[this.city] || this.city; }
|
|
1551
|
+
},
|
|
1552
|
+
layout: {
|
|
1553
|
+
"section:salary": {
|
|
1554
|
+
title: "五险一金计算器",
|
|
1555
|
+
subtitle: "输入税前工资和城市,实时计算五险一金明细。",
|
|
1556
|
+
"grid:params": {
|
|
1557
|
+
columns: 1, mdColumns: 2,
|
|
1558
|
+
"column:baseField": {
|
|
1559
|
+
"input:base": { label: "税前工资", "$value": "g.base", type: "number", unit: "元/月", onchange: "g.base = Number($event || 0)" },
|
|
1560
|
+
"slider:base": { label: "税前工资", "$value": "g.base", min: 3000, max: 50000, step: 500, unit: "元", onchange: "g.base = Number($event)" }
|
|
1561
|
+
},
|
|
1562
|
+
"column:cityField": {
|
|
1563
|
+
"select:city": {
|
|
1564
|
+
label: "缴纳城市",
|
|
1565
|
+
"$value": "g.city",
|
|
1566
|
+
options: [
|
|
1567
|
+
{ label: "北京", value: "beijing" },
|
|
1568
|
+
{ label: "上海", value: "shanghai" },
|
|
1569
|
+
{ label: "广州", value: "guangzhou" },
|
|
1570
|
+
{ label: "深圳", value: "shenzhen" }
|
|
1571
|
+
],
|
|
1572
|
+
onchange: "g.city = String($event)"
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
},
|
|
1576
|
+
"grid:summary": {
|
|
1577
|
+
columns: 1, mdColumns: 3,
|
|
1578
|
+
"stat:personal": { label: "个人扣除", "$value": "g.personalTotal().toFixed(0)", unit: "元" },
|
|
1579
|
+
"stat:company": { label: "公司缴纳", "$value": "g.companyTotal().toFixed(0)", unit: "元" },
|
|
1580
|
+
"stat:takehome": { label: "到手工资", "$value": "g.takeHome().toFixed(0)", unit: "元" }
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
```
|
|
1586
|
+
|
|
1587
|
+
换个城市看看,公积金比例差很多——北京12%,上海7%,到手工资能差好几百。
|
|
1588
|
+
|
|
1589
|
+
```slex
|
|
1590
|
+
{
|
|
1591
|
+
slex: "0.1",
|
|
1592
|
+
namespace: "example_salary_calculator",
|
|
1593
|
+
layout: {
|
|
1594
|
+
"card:detail": {
|
|
1595
|
+
title: "各项明细",
|
|
1596
|
+
"grid:personal": {
|
|
1597
|
+
columns: 1, mdColumns: 4,
|
|
1598
|
+
"stat:pension_p": { label: "养老(8%)", "$value": "(g.base * 0.08).toFixed(0)", unit: "元" },
|
|
1599
|
+
"stat:medical_p": { label: "医疗(2%)", "$value": "(g.base * 0.02).toFixed(0)", unit: "元" },
|
|
1600
|
+
"stat:unemployment_p": { label: "失业(0.5%)", "$value": "(g.base * 0.005).toFixed(0)", unit: "元" },
|
|
1601
|
+
"stat:housing_p": { label: "公积金", "$value": "(g.base * g.currentRate().housing / 100).toFixed(0)", unit: "元" }
|
|
1602
|
+
},
|
|
1603
|
+
"grid:company": {
|
|
1604
|
+
columns: 1, mdColumns: 4,
|
|
1605
|
+
"stat:pension_c": { label: "养老(16%)", "$value": "(g.base * 0.16).toFixed(0)", unit: "元" },
|
|
1606
|
+
"stat:medical_c": { label: "医疗(8%)", "$value": "(g.base * 0.08).toFixed(0)", unit: "元" },
|
|
1607
|
+
"stat:unemployment_c": { label: "失业(0.5%)", "$value": "(g.base * 0.005).toFixed(0)", unit: "元" },
|
|
1608
|
+
"stat:other_c": { label: "工伤+生育", "$value": "(g.base * 0.01).toFixed(0)", unit: "元" }
|
|
1609
|
+
},
|
|
1610
|
+
"callout:note": {
|
|
1611
|
+
"$tone": "g.personalTotal() > 3000 ? 'warning' : 'info'",
|
|
1612
|
+
"$text": "g.personalTotal() > 3000 ? '个人扣除超过3000元,到手工资可能低于预期。' : '公积金比例越高,到手工资越少,但公积金可以提取使用。'"
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
```
|
|
1618
|
+
|
|
1619
|
+
| 城市 | 养老 | 医疗 | 失业 | 公积金 | 个人合计 |
|
|
1620
|
+
|------|------|------|------|--------|----------|
|
|
1621
|
+
| 北京 | 8% | 2% | 0.5% | 12% | 22.5% |
|
|
1622
|
+
| 上海 | 8% | 2% | 0.5% | 7% | 17.5% |
|
|
1623
|
+
| 广州 | 8% | 2% | 0.5% | 5% | 15.5% |
|
|
1624
|
+
| 深圳 | 8% | 2% | 0.5% | 5% | 15.5% |
|
|
1625
|
+
|
|
1626
|
+
---
|
|
1627
|
+
|
|
1628
|
+
# 软件项目成本估算器
|
|
1629
|
+
|
|
1630
|
+
URL: /zh-CN/examples/project-cost-estimator
|
|
1631
|
+
Raw Markdown: /zh-CN/examples/project-cost-estimator.md
|
|
1632
|
+
Source: content/examples/project-cost-estimator/zh-CN.md
|
|
1633
|
+
|
|
1634
|
+
# 软件项目成本估算器
|
|
1635
|
+
|
|
1636
|
+
老板问你:"这个项目要多少钱?"你说:"我算算。"然后打开Excel,填一堆公式。其实不用——输入团队配置和周期,成本自动算出来。
|
|
1637
|
+
|
|
1638
|
+
```slex
|
|
1639
|
+
{
|
|
1640
|
+
slex: "0.1",
|
|
1641
|
+
namespace: "example_project_cost",
|
|
1642
|
+
g: {
|
|
1643
|
+
frontend: 2, backend: 3, tester: 1, designer: 1,
|
|
1644
|
+
months: 6,
|
|
1645
|
+
salary: 15000,
|
|
1646
|
+
teamSize: function () { return this.frontend + this.backend + this.tester + this.designer; },
|
|
1647
|
+
laborCost: function () { return this.teamSize() * this.salary * this.months; },
|
|
1648
|
+
equipmentCost: function () { return this.teamSize() * 5000; },
|
|
1649
|
+
officeCost: function () { return this.teamSize() * 2000 * this.months; },
|
|
1650
|
+
subtotal: function () { return this.laborCost() + this.equipmentCost() + this.officeCost(); },
|
|
1651
|
+
riskBuffer: function () { return this.subtotal() * 0.15; },
|
|
1652
|
+
totalCost: function () { return this.subtotal() + this.riskBuffer(); },
|
|
1653
|
+
perPersonCost: function () { return this.teamSize() > 0 ? this.totalCost() / this.teamSize() : 0; },
|
|
1654
|
+
monthlyBurn: function () { return this.months > 0 ? this.totalCost() / this.months : 0; }
|
|
1655
|
+
},
|
|
1656
|
+
layout: {
|
|
1657
|
+
"section:estimator": {
|
|
1658
|
+
title: "软件项目成本估算器",
|
|
1659
|
+
subtitle: "输入团队配置和周期,成本自动算出来。",
|
|
1660
|
+
"card:estimator": {
|
|
1661
|
+
title: "项目成本估算",
|
|
1662
|
+
"grid:team": {
|
|
1663
|
+
columns: 1, mdColumns: 4,
|
|
1664
|
+
"column:fe": {
|
|
1665
|
+
"input:frontend": { label: "前端", "$value": "g.frontend", type: "number", unit: "人", onchange: "g.frontend = Number($event || 0)" }
|
|
1666
|
+
},
|
|
1667
|
+
"column:be": {
|
|
1668
|
+
"input:backend": { label: "后端", "$value": "g.backend", type: "number", unit: "人", onchange: "g.backend = Number($event || 0)" }
|
|
1669
|
+
},
|
|
1670
|
+
"column:qa": {
|
|
1671
|
+
"input:tester": { label: "测试", "$value": "g.tester", type: "number", unit: "人", onchange: "g.tester = Number($event || 0)" }
|
|
1672
|
+
},
|
|
1673
|
+
"column:ui": {
|
|
1674
|
+
"input:designer": { label: "设计", "$value": "g.designer", type: "number", unit: "人", onchange: "g.designer = Number($event || 0)" }
|
|
1675
|
+
}
|
|
1676
|
+
},
|
|
1677
|
+
"grid:params": {
|
|
1678
|
+
columns: 1, mdColumns: 2,
|
|
1679
|
+
"column:period": {
|
|
1680
|
+
"input:months": { label: "开发周期", "$value": "g.months", type: "number", unit: "个月", onchange: "g.months = Number($event || 0)" },
|
|
1681
|
+
"slider:months": { label: "开发周期", "$value": "g.months", min: 1, max: 24, step: 1, unit: "月", onchange: "g.months = Number($event)" }
|
|
1682
|
+
},
|
|
1683
|
+
"column:salaryField": {
|
|
1684
|
+
"input:salary": { label: "人均月薪", "$value": "g.salary", type: "number", unit: "元", onchange: "g.salary = Number($event || 0)" },
|
|
1685
|
+
"slider:salary": { label: "人均月薪", "$value": "g.salary", min: 8000, max: 50000, step: 1000, unit: "元", onchange: "g.salary = Number($event)" }
|
|
1686
|
+
}
|
|
1687
|
+
},
|
|
1688
|
+
"grid:results": {
|
|
1689
|
+
columns: 1, mdColumns: 4,
|
|
1690
|
+
"stat:team": { label: "团队", "$value": "g.teamSize()", unit: "人" },
|
|
1691
|
+
"stat:total": { label: "总成本", "$value": "g.totalCost().toFixed(0)", unit: "元" },
|
|
1692
|
+
"stat:perperson": { label: "人均成本", "$value": "g.perPersonCost().toFixed(0)", unit: "元" },
|
|
1693
|
+
"stat:monthly": { label: "月均消耗", "$value": "g.monthlyBurn().toFixed(0)", unit: "元" }
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
```
|
|
1700
|
+
|
|
1701
|
+
7个人做半年,月均消耗多少?加个测试人员会不会超预算?拖一下就知道。
|
|
1702
|
+
|
|
1703
|
+
```slex
|
|
1704
|
+
{
|
|
1705
|
+
slex: "0.1",
|
|
1706
|
+
namespace: "example_project_cost",
|
|
1707
|
+
layout: {
|
|
1708
|
+
"card:breakdown": {
|
|
1709
|
+
title: "成本构成",
|
|
1710
|
+
"grid:costs": {
|
|
1711
|
+
columns: 1, mdColumns: 3,
|
|
1712
|
+
"stat:labor": { label: "人力成本", "$value": "g.laborCost().toFixed(0)", unit: "元" },
|
|
1713
|
+
"stat:equipment": { label: "设备成本", "$value": "g.equipmentCost().toFixed(0)", unit: "元" },
|
|
1714
|
+
"stat:office": { label: "办公成本", "$value": "g.officeCost().toFixed(0)", unit: "元" }
|
|
1715
|
+
},
|
|
1716
|
+
"grid:extra": {
|
|
1717
|
+
columns: 1, mdColumns: 2,
|
|
1718
|
+
"stat:risk": { label: "风险缓冲(15%)", "$value": "g.riskBuffer().toFixed(0)", unit: "元" },
|
|
1719
|
+
"stat:total": { label: "总计", "$value": "g.totalCost().toFixed(0)", unit: "元" }
|
|
1720
|
+
},
|
|
1721
|
+
"callout:tip": {
|
|
1722
|
+
"$tone": "g.months > 12 ? 'warning' : 'info'",
|
|
1723
|
+
"$text": "g.months > 12 ? '项目周期超过1年,建议分阶段交付以降低风险。' : '风险缓冲15%是经验值,复杂项目可调高到20-25%。'"
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
```
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
常见配置参考:
|
|
1732
|
+
|
|
1733
|
+
| 团队 | 周期 | 月薪 | 总成本 |
|
|
1734
|
+
|------|------|------|--------|
|
|
1735
|
+
| 3人 | 3个月 | 15k | 196,650 |
|
|
1736
|
+
| 5人 | 6个月 | 15k | 655,500 |
|
|
1737
|
+
| 8人 | 9个月 | 20k | 1,989,000 |
|
|
1738
|
+
| 10人 | 12个月 | 25k | 4,140,000 |
|
|
1739
|
+
|
|
1740
|
+
风险缓冲15%是经验值,复杂项目可以调到20-25%。设备和办公成本按人头估算,不含服务器和第三方服务。
|
|
1741
|
+
|
|
1742
|
+
---
|
|
1743
|
+
|
|
1744
|
+
# 分压器计算器
|
|
1745
|
+
|
|
1746
|
+
URL: /zh-CN/examples/voltage-divider
|
|
1747
|
+
Raw Markdown: /zh-CN/examples/voltage-divider.md
|
|
1748
|
+
Source: content/examples/voltage-divider/zh-CN.md
|
|
1749
|
+
|
|
1750
|
+
# 分压器计算器
|
|
1751
|
+
|
|
1752
|
+
两个电阻串联,从中间引出电压——模拟电路中最简单的信号调理手段。做 ADC 电平转换、设定阈值电压、生成偏置电压,处处都在用。
|
|
1753
|
+
|
|
1754
|
+
## 核心公式
|
|
1755
|
+
|
|
1756
|
+
空载分压:$V_{out} = V_{in} \times \frac{R_2}{R_1 + R_2}$
|
|
1757
|
+
|
|
1758
|
+
带负载 $R_L$ 时:$V_{out,loaded} = V_{in} \times \frac{R_2 \parallel R_L}{R_1 + R_2 \parallel R_L}$
|
|
1759
|
+
|
|
1760
|
+
**关键经验**:分压器阻抗($R_1 \parallel R_2$)应至少 < 后级输入阻抗的 1/10。
|
|
1761
|
+
|
|
1762
|
+
```slex
|
|
1763
|
+
{
|
|
1764
|
+
slex: "0.1",
|
|
1765
|
+
namespace: "example_voltage_divider",
|
|
1766
|
+
g: {
|
|
1767
|
+
vin: 5, r1: 10000, r2: 10000, rl: 100000,
|
|
1768
|
+
rParallel: function () { return this.r2 * this.rl / (this.r2 + this.rl); },
|
|
1769
|
+
vout: function () { return this.vin * this.r2 / (this.r1 + this.r2); },
|
|
1770
|
+
voutLoaded: function () { return this.vin * this.rParallel() / (this.r1 + this.rParallel()); },
|
|
1771
|
+
errorPercent: function () { return Math.abs((this.voutLoaded() - this.vout()) / this.vout() * 100); },
|
|
1772
|
+
impedanceRatio: function () { var zout = this.r1 * this.r2 / (this.r1 + this.r2); return this.rl / zout; },
|
|
1773
|
+
loadWarning: function () { return this.impedanceRatio() < 10 ? "负载效应显著,增大 RL 或减小 R1/R2" : "分压器阻抗足够低"; }
|
|
1774
|
+
},
|
|
1775
|
+
layout: {
|
|
1776
|
+
"section:divider": {
|
|
1777
|
+
title: "分压器计算器",
|
|
1778
|
+
subtitle: "两个电阻串联,从中间引出电压。",
|
|
1779
|
+
"card:divider": {
|
|
1780
|
+
title: "分压器计算器",
|
|
1781
|
+
"grid:params": {
|
|
1782
|
+
columns: 1, mdColumns: 2,
|
|
1783
|
+
"column:r1Field": { "input:r1": { label: "R1", "$value": "g.r1", type: "number", unit: "Ω", onchange: "g.r1 = Number($event || 0)" }, "slider:r1": { label: "R1", "$value": "g.r1", min: 100, max: 1000000, step: 100, unit: "Ω", onchange: "g.r1 = Number($event)" } },
|
|
1784
|
+
"column:r2Field": { "input:r2": { label: "R2", "$value": "g.r2", type: "number", unit: "Ω", onchange: "g.r2 = Number($event || 0)" }, "slider:r2": { label: "R2", "$value": "g.r2", min: 100, max: 1000000, step: 100, unit: "Ω", onchange: "g.r2 = Number($event)" } }
|
|
1785
|
+
},
|
|
1786
|
+
"grid:params2": {
|
|
1787
|
+
columns: 1, mdColumns: 2,
|
|
1788
|
+
"column:vinField": { "input:vin": { label: "输入电压 Vin", "$value": "g.vin", type: "number", unit: "V", onchange: "g.vin = Number($event || 0)" }, "slider:vin": { label: "Vin", "$value": "g.vin", min: 0.1, max: 48, step: 0.1, unit: "V", onchange: "g.vin = Number($event)" } },
|
|
1789
|
+
"column:rlField": { "input:rl": { label: "负载电阻 RL", "$value": "g.rl", type: "number", unit: "Ω", onchange: "g.rl = Number($event || 0)" }, "slider:rl": { label: "RL", "$value": "g.rl", min: 1000, max: 10000000, step: 1000, unit: "Ω", onchange: "g.rl = Number($event)" } }
|
|
1790
|
+
},
|
|
1791
|
+
"formula:eq1": { "$tex": "'V_{out} = ' + g.vin.toFixed(1) + ' \\\\times \\\\frac{' + (g.r2/1000).toFixed(1) + '\\\\text{k}}{' + (g.r1/1000).toFixed(1) + '\\\\text{k} + ' + (g.r2/1000).toFixed(1) + '\\\\text{k}} = ' + g.vout().toFixed(3) + '\\\\text{ V}'" },
|
|
1792
|
+
"grid:results": {
|
|
1793
|
+
columns: 1, mdColumns: 4,
|
|
1794
|
+
"stat:vout": { label: "空载 Vout", "$value": "g.vout().toFixed(3)", unit: "V" },
|
|
1795
|
+
"stat:voutLoaded": { label: "带载 Vout", "$value": "g.voutLoaded().toFixed(3)", unit: "V" },
|
|
1796
|
+
"stat:error": { label: "负载误差", "$value": "g.errorPercent().toFixed(2)", unit: "%" },
|
|
1797
|
+
"badge:ratio": { "$label": "g.impedanceRatio() < 10 ? '⚠ 负载效应' : '✓ 匹配良好'", "$tone": "g.impedanceRatio() < 10 ? 'warning' : 'success'" }
|
|
1798
|
+
},
|
|
1799
|
+
"callout:warning": { "$tone": "g.impedanceRatio() < 10 ? 'warning' : 'info'", "$text": "g.loadWarning()" }
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
```
|
|
1805
|
+
|
|
1806
|
+
|
|
1807
|
+
## 工程笔记
|
|
1808
|
+
|
|
1809
|
+
| R1 | R2 | Vout/Vin | 阻抗 |
|
|
1810
|
+
|----|----|---------|------|
|
|
1811
|
+
| 10k | 10k | 0.50 | 5k |
|
|
1812
|
+
| 10k | 3.3k | 0.25 | 2.5k |
|
|
1813
|
+
| 10k | 1k | 0.09 | 909 |
|
|
1814
|
+
| 33k | 10k | 0.23 | 7.7k |
|
|
1815
|
+
|
|
1816
|
+
- **ADC 应用**:分压器阻抗应 < ADC 输入阻抗的 1/10,必要时加缓冲运放
|
|
1817
|
+
- **高精度场景**:用 1% 精度电阻,R1 和 R2 用同一批次减少温漂差异
|
|
1818
|
+
- **功率限制**:$P = V^2/R$,小阻值分压器注意发热
|
|
1819
|
+
|
|
1820
|
+
---
|
|
1821
|
+
|
|
1822
|
+
# RC 低通滤波器
|
|
1823
|
+
|
|
1824
|
+
URL: /zh-CN/examples/rc-low-pass-filter
|
|
1825
|
+
Raw Markdown: /zh-CN/examples/rc-low-pass-filter.md
|
|
1826
|
+
Source: content/examples/rc-low-pass-filter/zh-CN.md
|
|
1827
|
+
|
|
1828
|
+
# RC 低通滤波器
|
|
1829
|
+
|
|
1830
|
+
RC 低通滤波器是模拟电路中最基础的无源滤波器——一个电阻加一个电容,就能把高频噪声从信号路径上滤掉。做 ADC 前端抗混叠、音频去嘶声、PWM 平滑输出,都会用到它。
|
|
1831
|
+
|
|
1832
|
+
## 核心公式
|
|
1833
|
+
|
|
1834
|
+
截止频率 $f_c$(-3dB 点):
|
|
1835
|
+
|
|
1836
|
+
$$f_c = \frac{1}{2\pi RC}$$
|
|
1837
|
+
|
|
1838
|
+
对于任意频率 $f$ 的输入信号,输出幅度增益为:
|
|
1839
|
+
|
|
1840
|
+
$$|H(f)| = \frac{1}{\sqrt{1 + (f/f_c)^2}}$$
|
|
1841
|
+
|
|
1842
|
+
## 参数区
|
|
1843
|
+
|
|
1844
|
+
```slex
|
|
1845
|
+
{
|
|
1846
|
+
slex: "0.1",
|
|
1847
|
+
namespace: "example_rc_low_pass_filter",
|
|
1848
|
+
g: {
|
|
1849
|
+
r: 10000,
|
|
1850
|
+
c: 100,
|
|
1851
|
+
f: 1000,
|
|
1852
|
+
cutoff: function () { return 1 / (2 * Math.PI * this.r * this.c * 1e-9); },
|
|
1853
|
+
gain: function () { return 1 / Math.sqrt(1 + Math.pow(this.f / this.cutoff(), 2)); },
|
|
1854
|
+
gainDb: function () { return (20 * Math.log10(this.gain())).toFixed(1); },
|
|
1855
|
+
regimeLabel: function () { return this.f < this.cutoff() * 0.1 ? "通带" : this.f > this.cutoff() * 10 ? "阻带" : "过渡带"; }
|
|
1856
|
+
},
|
|
1857
|
+
layout: {
|
|
1858
|
+
"section:params": {
|
|
1859
|
+
title: "RC 低通滤波器",
|
|
1860
|
+
subtitle: "一个电阻加一个电容,把高频噪声滤掉。",
|
|
1861
|
+
"card:params": {
|
|
1862
|
+
title: "参数输入",
|
|
1863
|
+
"grid:inputs": {
|
|
1864
|
+
columns: 1, mdColumns: 2,
|
|
1865
|
+
"column:rField": { "input:r": { label: "电阻 R", "$value": "g.r", type: "number", unit: "Ω", onchange: "g.r = Number($event || 0)" }, "slider:r": { label: "R", "$value": "g.r", min: 100, max: 100000, step: 100, unit: "Ω", onchange: "g.r = Number($event)" } },
|
|
1866
|
+
"column:cField": { "input:c": { label: "电容 C", "$value": "g.c", type: "number", unit: "nF", onchange: "g.c = Number($event || 0)" }, "slider:c": { label: "C", "$value": "g.c", min: 1, max: 1000, step: 1, unit: "nF", onchange: "g.c = Number($event)" } },
|
|
1867
|
+
"column:fField": { "input:f": { label: "输入频率 f", "$value": "g.f", type: "number", unit: "Hz", onchange: "g.f = Number($event || 0)" }, "slider:f": { label: "f", "$value": "g.f", min: 1, max: 100000, step: 1, unit: "Hz", onchange: "g.f = Number($event)" } }
|
|
1868
|
+
},
|
|
1869
|
+
"stat:fc": { label: "截止频率", "$value": "g.cutoff().toFixed(1)", unit: "Hz" },
|
|
1870
|
+
"badge:regime": { "$label": "g.regimeLabel()", "$tone": "g.f < g.cutoff() * 0.1 ? 'success' : g.f > g.cutoff() * 10 ? 'danger' : 'warning'" }
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
```
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
## 计算结果
|
|
1879
|
+
|
|
1880
|
+
```slex
|
|
1881
|
+
{
|
|
1882
|
+
slex: "0.1",
|
|
1883
|
+
namespace: "example_rc_low_pass_filter",
|
|
1884
|
+
layout: {
|
|
1885
|
+
"card:results": {
|
|
1886
|
+
title: "计算结果",
|
|
1887
|
+
"formula:fc_eq": { "$tex": "'f_c = \\\\frac{1}{2\\\\pi \\\\times ' + (g.r/1000).toFixed(1) + 'k\\\\Omega \\\\times ' + g.c + '\\\\text{nF}} = ' + g.cutoff().toFixed(1) + '\\\\text{ Hz}'" },
|
|
1888
|
+
"stat:gain_val": { label: "幅值增益 |H(f)|", "$value": "g.gain().toFixed(4)" },
|
|
1889
|
+
"stat:gain_db": { label: "增益", "$value": "g.gainDb()", unit: "dB" },
|
|
1890
|
+
"callout:verdict": { "$tone": "g.f < g.cutoff() * 0.1 ? 'success' : g.f > g.cutoff() * 10 ? 'danger' : 'warning'", "$text": "g.f < g.cutoff() * 0.1 ? '信号完整通过,衰减 < 0.04 dB。' : g.f > g.cutoff() * 10 ? '信号被强烈衰减超过 20 dB,滤波器有效工作。' : '信号处于过渡带,衰减约 ' + (-20 * Math.log10(1 / Math.sqrt(1 + Math.pow(g.f / g.cutoff(), 2)))).toFixed(1) + ' dB。'" }
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
```
|
|
1895
|
+
|
|
1896
|
+
|
|
1897
|
+
## 选型参考
|
|
1898
|
+
|
|
1899
|
+
下表是常见场景下的经验参数组合。把截止频率设为目标信号最高频率的 5-10 倍,可以保证通带平坦。
|
|
1900
|
+
|
|
1901
|
+
```slex
|
|
1902
|
+
{
|
|
1903
|
+
slex: "0.1",
|
|
1904
|
+
namespace: "example_rc_low_pass_filter",
|
|
1905
|
+
layout: {
|
|
1906
|
+
"card:selection": {
|
|
1907
|
+
title: "选型建议",
|
|
1908
|
+
"table:guide": {
|
|
1909
|
+
columns: ["R", "C", "fc", "典型用途"],
|
|
1910
|
+
rows: [
|
|
1911
|
+
["1 kΩ", "100 nF", "1592 Hz", "音频低通"],
|
|
1912
|
+
["10 kΩ", "100 nF", "159 Hz", "ADC 抗混叠"],
|
|
1913
|
+
["100 kΩ", "10 nF", "159 Hz", "PWM 平滑"],
|
|
1914
|
+
["1 kΩ", "1 µF", "159 Hz", "电源纹波滤波"],
|
|
1915
|
+
["10 kΩ", "1 nF", "15915 Hz", "高频噪声抑制"]
|
|
1916
|
+
]
|
|
1917
|
+
},
|
|
1918
|
+
"callout:tip": { "$tone": "g.cutoff() < 100 ? 'info' : g.cutoff() > 10000 ? 'warning' : 'success'", "$text": "g.cutoff() < 100 ? '低截止频率适合电源滤波和慢信号。' : g.cutoff() > 10000 ? '高截止频率可能无法有效滤除高频噪声。' : '当前截止频率适合大多数应用场景。'" }
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
```
|
|
1923
|
+
|
|
1924
|
+
|
|
1925
|
+
## 工程笔记
|
|
1926
|
+
|
|
1927
|
+
- **R 取值**:太大会增加输出阻抗和后级负载效应;太大会增大热噪声。1kΩ–100kΩ 是常用范围
|
|
1928
|
+
- **C 取值**:nF 级 NP0/C0G 陶瓷电容温漂小、精度高;超过 100nF 考虑薄膜电容
|
|
1929
|
+
- **负载效应**:后级输入阻抗应至少 > 10 × R,否则 $f_c$ 会偏移
|
|
1930
|
+
- **级联**:两级 RC 串联可得到 -40dB/dec 的二阶滚降,但截止频率会降低到约 $0.37f_c$
|
|
1931
|
+
|
|
1932
|
+
| $f/f_c$ | 增益 | 衰减 |
|
|
1933
|
+
|:---:|:---:|:---:|
|
|
1934
|
+
| 0.1 | 0.995 | -0.04 dB |
|
|
1935
|
+
| 1.0 | 0.707 | -3 dB |
|
|
1936
|
+
| 5.0 | 0.196 | -14.2 dB |
|
|
1937
|
+
| 10.0 | 0.100 | -20 dB |
|
|
1938
|
+
|
|
1939
|
+
---
|
|
1940
|
+
|
|
1941
|
+
# 波特率误差计算器
|
|
1942
|
+
|
|
1943
|
+
URL: /zh-CN/examples/baud-rate-calculator
|
|
1944
|
+
Raw Markdown: /zh-CN/examples/baud-rate-calculator.md
|
|
1945
|
+
Source: content/examples/baud-rate-calculator/zh-CN.md
|
|
1946
|
+
|
|
1947
|
+
# 波特率误差计算器
|
|
1948
|
+
|
|
1949
|
+
嵌入式开发中,UART 波特率由系统时钟分频得到。晶振频率不能整除目标波特率时会产生误差,误差超过 ±2% 通信就可能丢帧。
|
|
1950
|
+
|
|
1951
|
+
## 核心公式
|
|
1952
|
+
|
|
1953
|
+
$$Error = \frac{|BR_{actual} - BR_{target}|}{BR_{target}} \times 100\%$$
|
|
1954
|
+
|
|
1955
|
+
实际波特率 $BR_{actual} = f_{osc} / (16 \times N)$,其中 $N = \text{round}(f_{osc} / (16 \times BR_{target}))$ 为分频寄存器整数。
|
|
1956
|
+
|
|
1957
|
+
```slex
|
|
1958
|
+
{
|
|
1959
|
+
slex: "0.1",
|
|
1960
|
+
namespace: "example_baud_rate_calculator",
|
|
1961
|
+
g: {
|
|
1962
|
+
freq: 8,
|
|
1963
|
+
freqUnit: "MHz",
|
|
1964
|
+
baud: 115200,
|
|
1965
|
+
freqHz: function () { var m = { MHz: 1e6, kHz: 1e3, Hz: 1 }; return this.freq * (m[this.freqUnit] || 1e6); },
|
|
1966
|
+
divisor: function () { return this.freqHz() / (16 * this.baud); },
|
|
1967
|
+
regValue: function () { return Math.round(this.divisor()); },
|
|
1968
|
+
actualBaud: function () { return this.freqHz() / (16 * this.regValue()); },
|
|
1969
|
+
error: function () { return Math.abs((this.actualBaud() - this.baud) / this.baud * 100); },
|
|
1970
|
+
reliability: function () { var e = this.error(); return e < 0.5 ? "优秀" : e < 2 ? "可接受" : e < 5 ? "有风险" : "不可用"; },
|
|
1971
|
+
tone: function () { var e = this.error(); return e < 0.5 ? "success" : e < 2 ? "info" : e < 5 ? "warning" : "danger"; }
|
|
1972
|
+
},
|
|
1973
|
+
layout: {
|
|
1974
|
+
"section:baudCalculator": {
|
|
1975
|
+
title: "波特率误差计算器",
|
|
1976
|
+
subtitle: "输入晶振频率和目标波特率,计算误差。",
|
|
1977
|
+
"card:baud": {
|
|
1978
|
+
title: "波特率误差计算器",
|
|
1979
|
+
"grid:params": {
|
|
1980
|
+
columns: 1, mdColumns: 2,
|
|
1981
|
+
"column:freqField": {
|
|
1982
|
+
"input:freq": { label: "晶振频率", "$value": "g.freq", type: "number", unit: "MHz", onchange: "g.freq = Number($event || 0)" }
|
|
1983
|
+
},
|
|
1984
|
+
"column:baudField": {
|
|
1985
|
+
"select:baud": { label: "目标波特率", "$value": "g.baud", options: [{ label: "9600", value: 9600 }, { label: "19200", value: 19200 }, { label: "38400", value: 38400 }, { label: "57600", value: 57600 }, { label: "115200", value: 115200 }, { label: "230400", value: 230400 }, { label: "460800", value: 460800 }], onchange: "g.baud = Number($event)" }
|
|
1986
|
+
},
|
|
1987
|
+
"column:unitField": {
|
|
1988
|
+
"select:unit": { label: "频率单位", "$value": "g.freqUnit", options: [{ label: "MHz", value: "MHz" }, { label: "kHz", value: "kHz" }, { label: "Hz", value: "Hz" }], onchange: "g.freqUnit = String($event)" }
|
|
1989
|
+
},
|
|
1990
|
+
"column:divField": {
|
|
1991
|
+
"stat:divisor": { label: "分频比", "$value": "g.divisor().toFixed(3)" }
|
|
1992
|
+
}
|
|
1993
|
+
},
|
|
1994
|
+
"formula:equation": { "$tex": "'\\\\text{Error} = \\\\frac{|' + g.actualBaud().toFixed(0) + ' - ' + g.baud + '|}{' + g.baud + '} \\\\times 100\\\\% = ' + g.error().toFixed(2) + '\\\\%'" },
|
|
1995
|
+
"grid:results": {
|
|
1996
|
+
columns: 1, mdColumns: 4,
|
|
1997
|
+
"stat:actualBaud": { label: "实际波特率", "$value": "g.actualBaud().toFixed(0)", unit: "bps" },
|
|
1998
|
+
"stat:error": { label: "误差", "$value": "g.error().toFixed(2)", unit: "%" },
|
|
1999
|
+
"stat:reliability": { label: "可靠性", "$value": "g.reliability()", "$tone": "g.tone()" },
|
|
2000
|
+
"stat:regValue": { label: "寄存器值", "$value": "g.regValue()" }
|
|
2001
|
+
},
|
|
2002
|
+
"callout:advice": { "$tone": "g.tone()", "$text": "g.error() < 0.5 ? '误差极小,通信可靠。' : g.error() < 2 ? '误差在可接受范围内(<2%),绝大多数场景可用。' : g.error() < 5 ? '误差偏大,长帧通信可能失败,建议更换晶振或降低波特率。' : '误差过大,通信不可靠。请选择能整除的晶振频率。'" }
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
```
|
|
2008
|
+
|
|
2009
|
+
|
|
2010
|
+
## 常用晶振频率与波特率误差表
|
|
2011
|
+
|
|
2012
|
+
| 晶振 | 9600 | 19200 | 38400 | 57600 | 115200 |
|
|
2013
|
+
|------|------|-------|-------|-------|--------|
|
|
2014
|
+
| 1.8432 MHz | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
|
|
2015
|
+
| 3.6864 MHz | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
|
|
2016
|
+
| 4.0000 MHz | 8.51 | 8.51 | 8.51 | 8.51 | 8.51 |
|
|
2017
|
+
| 7.3728 MHz | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
|
|
2018
|
+
| 8.0000 MHz | 8.51 | 8.51 | 8.51 | 8.51 | 8.51 |
|
|
2019
|
+
| 11.0592 MHz | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
|
|
2020
|
+
| 14.7456 MHz | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
|
|
2021
|
+
| 16.0000 MHz | 8.51 | 8.51 | 8.51 | 8.51 | 8.51 |
|
|
2022
|
+
|
|
2023
|
+
**工程笔记**:
|
|
2024
|
+
|
|
2025
|
+
- 零误差的关键:晶振频率能被 $16 \times BR$ 整除
|
|
2026
|
+
- **11.0592 MHz** 是 UART 最经典的选择,对所有标准波特率零误差
|
|
2027
|
+
- **7.3728 MHz** 和 **14.7456 MHz** 同样零误差
|
|
2028
|
+
- 错误 < 2% 在实际工程中通常可工作,但高速长帧场景建议 < 1%
|
|
2029
|
+
|
|
2030
|
+
---
|
|
2031
|
+
|
|
2032
|
+
# 搜索与过滤表格
|
|
2033
|
+
|
|
2034
|
+
URL: /zh-CN/examples/search-filter-table
|
|
2035
|
+
Raw Markdown: /zh-CN/examples/search-filter-table.md
|
|
2036
|
+
Source: content/examples/search-filter-table/zh-CN.md
|
|
2037
|
+
|
|
2038
|
+
# 搜索与过滤表格
|
|
2039
|
+
|
|
2040
|
+
文档里放一个静态表格很常见,但加上搜索和过滤就会变成立即可用的工具。这里用 `input` 绑搜索关键词 + 动态过滤 + `collapsible` 展开行详情。
|
|
2041
|
+
|
|
2042
|
+
```slex
|
|
2043
|
+
{
|
|
2044
|
+
slex: "0.1",
|
|
2045
|
+
namespace: "example_search_filter",
|
|
2046
|
+
g: {
|
|
2047
|
+
query: "", selected: "", selectedItem: null,
|
|
2048
|
+
allItems: [
|
|
2049
|
+
{ id: "btn-1", name: "Button 按钮", category: "Action", status: "ready", notes: "支持 variant 和 size。" },
|
|
2050
|
+
{ id: "card-1", name: "Card 卡片", category: "Layout", status: "ready", notes: "最常用的分组容器。" },
|
|
2051
|
+
{ id: "input-1", name: "Input 输入框", category: "Input", status: "ready", notes: "支持 type、unit 和 placeholder。" },
|
|
2052
|
+
{ id: "tabs-1", name: "Tabs 选项卡", category: "Navigation", status: "ready", notes: "支持水平和垂直方向。" },
|
|
2053
|
+
{ id: "table-1", name: "Table 表格", category: "Display", status: "ready", notes: "columns 数组 + rows 数组。" },
|
|
2054
|
+
{ id: "formula-1", name: "Formula 公式", category: "Display", status: "ready", notes: "依赖 KaTeX 渲染 LaTeX。" },
|
|
2055
|
+
{ id: "toast-1", name: "Toast 通知", category: "Feedback", status: "experimental", notes: "支持 type 变体。" },
|
|
2056
|
+
{ id: "secure-1", name: "Secure 安全运行时", category: "Tooling", status: "draft", notes: "基于 iframe 沙箱。" }
|
|
2057
|
+
],
|
|
2058
|
+
filtered: function () {
|
|
2059
|
+
var q = this.query.toLowerCase();
|
|
2060
|
+
if (!q) return this.allItems;
|
|
2061
|
+
return this.allItems.filter(function (item) { return item.name.toLowerCase().includes(q) || item.category.toLowerCase().includes(q); });
|
|
2062
|
+
},
|
|
2063
|
+
matched: function () { return this.filtered().length; },
|
|
2064
|
+
select: function (id) { this.selected = this.selected === id ? "" : id; }
|
|
2065
|
+
},
|
|
2066
|
+
layout: {
|
|
2067
|
+
"section:search": {
|
|
2068
|
+
eyebrow: "组件查询",
|
|
2069
|
+
title: "搜索与过滤表格",
|
|
2070
|
+
subtitle: "输入关键词实时过滤组件列表,点击任意行查看详情。",
|
|
2071
|
+
"input:query": { label: "搜索组件", "$value": "g.query", type: "text", placeholder: "输入名称或分类关键词...", onchange: "g.query = String($event || '')" },
|
|
2072
|
+
"stat:matched": { "$label": "'匹配结果'", "$value": "g.matched()", "$unit": "'/' + g.allItems.length + ' 项'" },
|
|
2073
|
+
"table:list": {
|
|
2074
|
+
columns: ["名称", "分类", "状态", ""],
|
|
2075
|
+
"$rows": "g.filtered().map(function(item) { return [item.name, item.category, item.status, item.id]; })"
|
|
2076
|
+
},
|
|
2077
|
+
"callout:empty": {
|
|
2078
|
+
"$if": "g.matched() === 0",
|
|
2079
|
+
tone: "warning",
|
|
2080
|
+
"$text": "'未找到匹配「' + g.query + '」的组件。'"
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
```
|
|
2086
|
+
|
|
2087
|
+
**这个示例的核心技巧:**
|
|
2088
|
+
|
|
2089
|
+
- `input` 的 `onchange` 更新 `g.query` → 触发 `g.filtered()` 重新计算
|
|
2090
|
+
- `g.filtered()` 用 `filter` 过滤 `allItems` 数组
|
|
2091
|
+
- `"$rows"` 动态计算行的二维数组——每个 item 映射为一行
|
|
2092
|
+
- `g.matched()` 返回过滤后数量,用于 stat 和 callout 的条件显示
|
|
2093
|
+
- `$if` 在无匹配时显示空结果提示
|
|
2094
|
+
|
|
2095
|
+
这就是 "搜索框 → 动态表格" 的基础范式。相比硬编码的 table rows,动态 rows 可以随输入实时变化。
|
|
2096
|
+
|
|
2097
|
+
---
|
|
2098
|
+
|
|
2099
|
+
## 进阶玩法:展开行详情
|
|
2100
|
+
|
|
2101
|
+
```slex
|
|
2102
|
+
{
|
|
2103
|
+
slex: "0.1",
|
|
2104
|
+
namespace: "example_search_filter",
|
|
2105
|
+
layout: {
|
|
2106
|
+
"section:detail": {
|
|
2107
|
+
eyebrow: "组件详情",
|
|
2108
|
+
title: "选中查看详情",
|
|
2109
|
+
"accordion:faq": {
|
|
2110
|
+
multiple: true,
|
|
2111
|
+
items: [
|
|
2112
|
+
{ value: "btn-1", label: "Button 按钮", icon: "cursor-click", content: "触发操作的基础组件。支持 variant (solid/outline/ghost) 和 size (sm/md/lg) 等变体。" },
|
|
2113
|
+
{ value: "card-1", label: "Card 卡片", icon: "rectangle", content: "内容分组容器。最常用的布局组件之一,比 section 更轻量。" },
|
|
2114
|
+
{ value: "input-1", label: "Input 输入框", icon: "textbox", content: "单行或数字输入。支持 type、unit、placeholder 等属性。" }
|
|
2115
|
+
]
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
```
|
|
2121
|
+
|
|
2122
|
+
## 为什么动态 rows 值得学?
|
|
2123
|
+
|
|
2124
|
+
在静态表格中,rows 是硬编码的二维数组。一旦数据量增加,或者需要按条件筛选,静态 rows 就力不从心了。`$rows` 允许你在 g 方法中动态生成行数据——这对于:
|
|
2125
|
+
|
|
2126
|
+
- **文档内数据浏览**:目录、API 列表、配置项
|
|
2127
|
+
- **AI 输出展示**:大模型生成的表格结果需要可筛选
|
|
2128
|
+
- **知识库查询**:搜索内部组件/API/术语
|
|
2129
|
+
|
|
2130
|
+
都非常实用。
|
|
2131
|
+
|
|
2132
|
+
---
|
|
2133
|
+
|
|
2134
|
+
---
|
|
2135
|
+
|
|
2136
|
+
# 项目仪表盘
|
|
2137
|
+
|
|
2138
|
+
URL: /zh-CN/examples/project-dashboard
|
|
2139
|
+
Raw Markdown: /zh-CN/examples/project-dashboard.md
|
|
2140
|
+
Source: content/examples/project-dashboard/zh-CN.md
|
|
2141
|
+
|
|
2142
|
+
# 项目仪表盘
|
|
2143
|
+
|
|
2144
|
+
真实项目需要一览全局——不是单一张计算卡片,而是一个**仪表盘**。用 `section` 做区块分组,`grid` 做多列布局,每个 card 展示一个关注领域。
|
|
2145
|
+
|
|
2146
|
+
```slex
|
|
2147
|
+
{
|
|
2148
|
+
slex: "0.1",
|
|
2149
|
+
namespace: "example_dashboard",
|
|
2150
|
+
g: {
|
|
2151
|
+
sprint: 8, team: 6, sprintProgress: 72, bugs: 12, resolved: 9,
|
|
2152
|
+
scope: "on-track",
|
|
2153
|
+
bugRate: function () { return this.resolved / this.bugs * 100; },
|
|
2154
|
+
teamLoad: function () { return Math.min(100, this.sprint / this.team * 20); },
|
|
2155
|
+
scopeStatus: function () { return this.scope === "on-track" ? "正常" : this.scope === "at-risk" ? "有风险" : "已偏离"; }
|
|
2156
|
+
},
|
|
2157
|
+
layout: {
|
|
2158
|
+
"section:dashboard": {
|
|
2159
|
+
eyebrow: "Sprint 概览",
|
|
2160
|
+
title: "项目仪表盘 · Sprint 24",
|
|
2161
|
+
subtitle: "数据截止今日 10:00 AM。拖滑动条查看不同假想数据的效果。",
|
|
2162
|
+
"grid:row1": {
|
|
2163
|
+
columns: 1, mdColumns: 3,
|
|
2164
|
+
"card:sprint": {
|
|
2165
|
+
title: "Sprint 进度",
|
|
2166
|
+
"progress:sp": { label: "完成度", "$value": "g.sprintProgress" },
|
|
2167
|
+
"stat:scope": { label: "范围状态", "$value": "g.scopeStatus()" },
|
|
2168
|
+
"badge:flag": { "$label": "g.scope === 'on-track' ? '正常' : g.scope === 'at-risk' ? '⚠ 预警' : '🚨 偏离'", "$tone": "g.scope === 'on-track' ? 'success' : g.scope === 'at-risk' ? 'warning' : 'danger'" }
|
|
2169
|
+
},
|
|
2170
|
+
"card:quality": {
|
|
2171
|
+
title: "质量指标",
|
|
2172
|
+
"stat:bugs": { label: "剩余缺陷", value: "3", unit: "个" },
|
|
2173
|
+
"progress:bugFix": { label: "修复率", "$value": "g.bugRate()" },
|
|
2174
|
+
"badge:trend": { label: "趋势:改善中", tone: "success" }
|
|
2175
|
+
},
|
|
2176
|
+
"card:team": {
|
|
2177
|
+
title: "团队健康度",
|
|
2178
|
+
"stat:members": { label: "团队成员", value: "6", unit: "人" },
|
|
2179
|
+
"progress:load": { label: "负载指数", "$value": "g.teamLoad()" },
|
|
2180
|
+
"callout:note": { "$tone": "g.teamLoad() > 80 ? 'warning' : 'info'", "$text": "g.teamLoad() > 80 ? '负载偏高,建议调整任务分配。' : '团队负载在健康范围内。'" }
|
|
2181
|
+
}
|
|
2182
|
+
},
|
|
2183
|
+
"card:detail": {
|
|
2184
|
+
title: "详细数据",
|
|
2185
|
+
"table:tasks": {
|
|
2186
|
+
columns: ["任务", "负责人", "状态", "耗时"],
|
|
2187
|
+
rows: [["API 重构", "张三", "已完成", "3d"], ["前端组件", "李四", "进行中", "2d"], ["集成测试", "王五", "代码审查", "1.5d"], ["性能优化", "赵六", "待开始", "—"]]
|
|
2188
|
+
},
|
|
2189
|
+
"callout:help": { "$tone": "g.bugRate() < 80 ? 'warning' : 'success'", "$text": "g.bugRate() < 80 ? '缺陷修复率低于80%,建议优先处理高优先级缺陷。' : '缺陷修复率良好,项目质量可控。'" }
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
```
|
|
2195
|
+
|
|
2196
|
+
**仪表盘的设计思路:**
|
|
2197
|
+
|
|
2198
|
+
- 用 `section` 做顶层分组(带 eyebrow 和 subtitle,语义清晰)
|
|
2199
|
+
- 用 `grid` 做多列布局,每个格子放一个 `card`
|
|
2200
|
+
- 每个 card 内放不同关注维度的 stat / progress / badge
|
|
2201
|
+
- 底部放一个详情 card,展示表格数据(任务列表)
|
|
2202
|
+
- 通过 `$text` 和 `$tone` 在 callout 中做条件提示
|
|
2203
|
+
|
|
2204
|
+
|
|
2205
|
+
这个模式适用于:技术 Leader 周报面板、发布质检看板、团队 OKR 追踪、SRE 服务大盘。
|
|
2206
|
+
|
|
2207
|
+
---
|
|
2208
|
+
|
|
2209
|
+
# ToolHost 表单提问
|
|
2210
|
+
|
|
2211
|
+
URL: /zh-CN/examples/form-wizard-steps
|
|
2212
|
+
Raw Markdown: /zh-CN/examples/form-wizard-steps.md
|
|
2213
|
+
Source: content/examples/form-wizard-steps/zh-CN.md
|
|
2214
|
+
|
|
2215
|
+
# ToolHost 表单提问
|
|
2216
|
+
|
|
2217
|
+
AI 对话过程中,有时需要收集用户信息——创建项目、配置服务、提交工单。这时候 AI 会弹出一个表单卡片,用户填写后提交,AI 继续处理。
|
|
2218
|
+
|
|
2219
|
+
```slex
|
|
2220
|
+
{
|
|
2221
|
+
slex: "0.1",
|
|
2222
|
+
namespace: "example_form_wizard",
|
|
2223
|
+
g: {
|
|
2224
|
+
submitted: false,
|
|
2225
|
+
formData: null,
|
|
2226
|
+
fields: { name: "", description: "", type: "web", priority: "medium" },
|
|
2227
|
+
submit: function () {
|
|
2228
|
+
this.submitted = true;
|
|
2229
|
+
this.formData = { name: this.fields.name, description: this.fields.description, type: this.fields.type, priority: this.fields.priority, timestamp: new Date().toLocaleString() };
|
|
2230
|
+
}
|
|
2231
|
+
},
|
|
2232
|
+
layout: {
|
|
2233
|
+
"section:toolhost": {
|
|
2234
|
+
eyebrow: "ToolHost · 表单提问",
|
|
2235
|
+
title: "AI 需要收集信息",
|
|
2236
|
+
subtitle: "AI 弹出表单,用户填写后提交,AI 继续处理。",
|
|
2237
|
+
"callout:context": {
|
|
2238
|
+
tone: "info",
|
|
2239
|
+
text: "AI:我需要为你创建一个新项目,请填写以下信息。"
|
|
2240
|
+
},
|
|
2241
|
+
"grid:fields": {
|
|
2242
|
+
columns: 1, mdColumns: 2,
|
|
2243
|
+
"input:name": { label: "项目名称", "$value": "g.fields.name", placeholder: "my-project", onchange: "g.fields.name = String($event || '')" },
|
|
2244
|
+
"input:description": { label: "项目描述", "$value": "g.fields.description", placeholder: "简短描述项目用途", onchange: "g.fields.description = String($event || '')" },
|
|
2245
|
+
"select:type": {
|
|
2246
|
+
label: "项目类型",
|
|
2247
|
+
"$value": "g.fields.type",
|
|
2248
|
+
options: [
|
|
2249
|
+
{ label: "Web 应用", value: "web" },
|
|
2250
|
+
{ label: "API 服务", value: "api" },
|
|
2251
|
+
{ label: "CLI 工具", value: "cli" }
|
|
2252
|
+
],
|
|
2253
|
+
onchange: "g.fields.type = String($event)"
|
|
2254
|
+
},
|
|
2255
|
+
"select:priority": {
|
|
2256
|
+
label: "优先级",
|
|
2257
|
+
"$value": "g.fields.priority",
|
|
2258
|
+
options: [
|
|
2259
|
+
{ label: "低", value: "low" },
|
|
2260
|
+
{ label: "中", value: "medium" },
|
|
2261
|
+
{ label: "高", value: "high" }
|
|
2262
|
+
],
|
|
2263
|
+
onchange: "g.fields.priority = String($event)"
|
|
2264
|
+
}
|
|
2265
|
+
},
|
|
2266
|
+
"grid:actions": {
|
|
2267
|
+
columns: 2,
|
|
2268
|
+
"button:submit": { label: "提交", onclick: "g.submit()" },
|
|
2269
|
+
"button:skip": { label: "跳过" }
|
|
2270
|
+
},
|
|
2271
|
+
"callout:result": {
|
|
2272
|
+
"$tone": "g.submitted ? 'success' : 'info'",
|
|
2273
|
+
"$text": "g.submitted ? '已提交:' + g.formData.name + '(' + g.formData.type + ')' : '等待用户填写...'"
|
|
2274
|
+
},
|
|
2275
|
+
"code-block:toolresult": {
|
|
2276
|
+
title: "返回给 AI 的 ToolResult",
|
|
2277
|
+
language: "json",
|
|
2278
|
+
"$code": "g.submitted ? JSON.stringify({ toolCallId: 'call_abc123', toolName: 'create-project', status: 'submitted', value: g.formData }, null, 2) : '// 提交后显示 ToolResult'"
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
}
|
|
2283
|
+
```
|
|
2284
|
+
|
|
2285
|
+
---
|
|
2286
|
+
|
|
2287
|
+
---
|
|
2288
|
+
|
|
2289
|
+
# 技术选型评估
|
|
2290
|
+
|
|
2291
|
+
URL: /zh-CN/examples/tech-selection-evaluator
|
|
2292
|
+
Raw Markdown: /zh-CN/examples/tech-selection-evaluator.md
|
|
2293
|
+
Source: content/examples/tech-selection-evaluator/zh-CN.md
|
|
2294
|
+
|
|
2295
|
+
# 技术选型评估
|
|
2296
|
+
|
|
2297
|
+
团队要选前端框架,React、Vue、Svelte、Angular,各有各的好。性能、生态、学习曲线、维护成本——怎么量化比较?这里用跨 fence 联动,选一个技术栈,下面的评分和结论自动跟着变。
|
|
2298
|
+
|
|
2299
|
+
```slex
|
|
2300
|
+
{
|
|
2301
|
+
slex: "0.1",
|
|
2302
|
+
namespace: "example_tech_selection",
|
|
2303
|
+
g: {
|
|
2304
|
+
tech: "react",
|
|
2305
|
+
performance: 85,
|
|
2306
|
+
ecosystem: 95,
|
|
2307
|
+
learning: 70,
|
|
2308
|
+
maintenance: 80,
|
|
2309
|
+
techLabel: function () { return { react: "React", vue: "Vue", svelte: "Svelte", angular: "Angular" }[this.tech] || this.tech; },
|
|
2310
|
+
totalScore: function () { return (this.performance * 0.3 + this.ecosystem * 0.25 + this.learning * 0.2 + this.maintenance * 0.25).toFixed(1); },
|
|
2311
|
+
recommendation: function () { var s = parseFloat(this.totalScore()); return s >= 85 ? "强烈推荐" : s >= 75 ? "推荐" : s >= 60 ? "可以考虑" : "不推荐"; },
|
|
2312
|
+
riskLevel: function () { var s = parseFloat(this.totalScore()); return s >= 85 ? "低" : s >= 75 ? "中" : "高"; },
|
|
2313
|
+
scores: function () {
|
|
2314
|
+
var data = {
|
|
2315
|
+
react: { performance: 85, ecosystem: 95, learning: 70, maintenance: 80 },
|
|
2316
|
+
vue: { performance: 80, ecosystem: 85, learning: 85, maintenance: 85 },
|
|
2317
|
+
svelte: { performance: 95, ecosystem: 70, learning: 90, maintenance: 90 },
|
|
2318
|
+
angular: { performance: 80, ecosystem: 80, learning: 60, maintenance: 75 }
|
|
2319
|
+
};
|
|
2320
|
+
return data[this.tech] || data.react;
|
|
2321
|
+
}
|
|
2322
|
+
},
|
|
2323
|
+
layout: {
|
|
2324
|
+
"section:select": {
|
|
2325
|
+
eyebrow: "决策辅助",
|
|
2326
|
+
title: "技术选型评估",
|
|
2327
|
+
subtitle: "选一个技术栈,下面的评分和结论自动跟着变。",
|
|
2328
|
+
"card:select": {
|
|
2329
|
+
title: "选择技术栈",
|
|
2330
|
+
"select:tech": {
|
|
2331
|
+
label: "技术栈",
|
|
2332
|
+
"$value": "g.tech",
|
|
2333
|
+
options: [
|
|
2334
|
+
{ label: "React", value: "react" },
|
|
2335
|
+
{ label: "Vue", value: "vue" },
|
|
2336
|
+
{ label: "Svelte", value: "svelte" },
|
|
2337
|
+
{ label: "Angular", value: "angular" }
|
|
2338
|
+
],
|
|
2339
|
+
onchange: "g.tech = String($event); var s = g.scores(); g.performance = s.performance; g.ecosystem = s.ecosystem; g.learning = s.learning; g.maintenance = s.maintenance;"
|
|
2340
|
+
},
|
|
2341
|
+
"badge:current": {
|
|
2342
|
+
"$label": "'当前:' + g.techLabel()",
|
|
2343
|
+
tone: "info"
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
```
|
|
2350
|
+
|
|
2351
|
+
选了React,觉得性能分太低?拖一下滑块调整,下面的推荐和风险等级实时更新。这就是跨 fence 联动的价值——三个独立的代码块,共享同一份状态。
|
|
2352
|
+
|
|
2353
|
+
```slex
|
|
2354
|
+
{
|
|
2355
|
+
slex: "0.1",
|
|
2356
|
+
namespace: "example_tech_selection",
|
|
2357
|
+
layout: {
|
|
2358
|
+
"card:scoring": {
|
|
2359
|
+
title: "评分调整",
|
|
2360
|
+
"grid:sliders": {
|
|
2361
|
+
columns: 1, mdColumns: 2,
|
|
2362
|
+
"column:left": {
|
|
2363
|
+
"slider:performance": { label: "性能(30%)", "$value": "g.performance", min: 0, max: 100, step: 5, onchange: "g.performance = Number($event)" },
|
|
2364
|
+
"slider:ecosystem": { label: "生态系统(25%)", "$value": "g.ecosystem", min: 0, max: 100, step: 5, onchange: "g.ecosystem = Number($event)" }
|
|
2365
|
+
},
|
|
2366
|
+
"column:right": {
|
|
2367
|
+
"slider:learning": { label: "学习曲线(20%)", "$value": "g.learning", min: 0, max: 100, step: 5, onchange: "g.learning = Number($event)" },
|
|
2368
|
+
"slider:maintenance": { label: "维护成本(25%)", "$value": "g.maintenance", min: 0, max: 100, step: 5, onchange: "g.maintenance = Number($event)" }
|
|
2369
|
+
}
|
|
2370
|
+
},
|
|
2371
|
+
"grid:weights": {
|
|
2372
|
+
columns: 1, mdColumns: 4,
|
|
2373
|
+
"stat:perf": { label: "性能(30%)", "$value": "g.performance", unit: "分" },
|
|
2374
|
+
"stat:eco": { label: "生态(25%)", "$value": "g.ecosystem", unit: "分" },
|
|
2375
|
+
"stat:learn": { label: "学习(20%)", "$value": "g.learning", unit: "分" },
|
|
2376
|
+
"stat:maint": { label: "维护(25%)", "$value": "g.maintenance", unit: "分" }
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
```
|
|
2382
|
+
|
|
2383
|
+
```slex
|
|
2384
|
+
{
|
|
2385
|
+
slex: "0.1",
|
|
2386
|
+
namespace: "example_tech_selection",
|
|
2387
|
+
layout: {
|
|
2388
|
+
"card:result": {
|
|
2389
|
+
title: "综合评估",
|
|
2390
|
+
"grid:scores": {
|
|
2391
|
+
columns: 1, mdColumns: 3,
|
|
2392
|
+
"stat:total": { label: "综合评分", "$value": "g.totalScore()" },
|
|
2393
|
+
"stat:recommendation": { label: "推荐程度", "$value": "g.recommendation()", "$tone": "parseFloat(g.totalScore()) >= 85 ? 'success' : parseFloat(g.totalScore()) >= 75 ? 'info' : 'warning'" },
|
|
2394
|
+
"stat:risk": { label: "风险等级", "$value": "g.riskLevel()", "$tone": "parseFloat(g.totalScore()) >= 85 ? 'success' : parseFloat(g.totalScore()) >= 75 ? 'warning' : 'danger'" }
|
|
2395
|
+
},
|
|
2396
|
+
"callout:advice": {
|
|
2397
|
+
"$tone": "parseFloat(g.totalScore()) >= 85 ? 'success' : parseFloat(g.totalScore()) >= 75 ? 'info' : 'warning'",
|
|
2398
|
+
"$text": "parseFloat(g.totalScore()) >= 85 ? g.techLabel() + ' 综合优秀,强烈推荐。' : parseFloat(g.totalScore()) >= 75 ? g.techLabel() + ' 综合良好,推荐采用。' : g.techLabel() + ' 综合一般,建议谨慎。'"
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
}
|
|
2403
|
+
```
|
|
2404
|
+
|
|
2405
|
+
|
|
2406
|
+
默认评分参考:
|
|
2407
|
+
|
|
2408
|
+
| 技术栈 | 性能 | 生态 | 学习 | 维护 | 综合 |
|
|
2409
|
+
|--------|------|------|------|------|------|
|
|
2410
|
+
| React | 85 | 95 | 70 | 80 | 82.5 |
|
|
2411
|
+
| Vue | 80 | 85 | 85 | 85 | 83.75 |
|
|
2412
|
+
| Svelte | 95 | 70 | 90 | 90 | 85.5 |
|
|
2413
|
+
| Angular | 80 | 80 | 60 | 75 | 73.75 |
|
|
2414
|
+
|
|
2415
|
+
权重分配:性能30%、生态25%、学习曲线20%、维护成本25%。可以按团队实际情况调整。
|
|
2416
|
+
|
|
2417
|
+
---
|
|
2418
|
+
|
|
2419
|
+
# ToolHost 对话演示
|
|
2420
|
+
|
|
2421
|
+
URL: /zh-CN/examples/toolhost-demo
|
|
2422
|
+
Raw Markdown: /zh-CN/examples/toolhost-demo.md
|
|
2423
|
+
Source: content/examples/toolhost-demo/zh-CN.md
|
|
2424
|
+
|
|
2425
|
+
## ToolHost 对话演示
|
|
2426
|
+
|
|
2427
|
+
AI 对话中需要收集用户信息时,会调用 **ToolHost** 弹出交互式表单卡片。用户填写并提交后,表单数据以结构化的 `ToolResult` 返回给 AI,AI 继续处理。
|
|
2428
|
+
|
|
2429
|
+
下方是一个完整的对话演示——模拟用户请求创建项目,AI 调用 `fill-form` 模板收集项目信息,提交后返回 ToolResult。
|
|
2430
|
+
|
|
2431
|
+
**流程:** 用户发起请求 → AI 判断需要工具调用 → ToolHost 弹出表单 → 用户填写并提交 → 返回 ToolResult → AI 继续响应
|
|
2432
|
+
|
|
2433
|
+
AI 端调用 ToolHost 的代码如下:
|
|
2434
|
+
|
|
2435
|
+
```js
|
|
2436
|
+
import { renderToolCall } from "slexkit/toolhost";
|
|
2437
|
+
|
|
2438
|
+
const { promise } = renderToolCall({
|
|
2439
|
+
name: "fill-form",
|
|
2440
|
+
arguments: {
|
|
2441
|
+
title: "创建新项目",
|
|
2442
|
+
description: "请填写项目的基本信息。",
|
|
2443
|
+
submitLabel: "提交",
|
|
2444
|
+
ignoreLabel: "取消",
|
|
2445
|
+
fields: [
|
|
2446
|
+
{ name: "name", label: "项目名称", type: "text", required: true },
|
|
2447
|
+
{ name: "type", label: "项目类型", type: "select", options: [
|
|
2448
|
+
{ label: "Web 应用", value: "web" },
|
|
2449
|
+
{ label: "API 服务", value: "api" },
|
|
2450
|
+
{ label: "CLI 工具", value: "cli" },
|
|
2451
|
+
]},
|
|
2452
|
+
{ name: "priority", label: "优先级", type: "select", options: [
|
|
2453
|
+
{ label: "低", value: "low" },
|
|
2454
|
+
{ label: "中", value: "medium" },
|
|
2455
|
+
{ label: "高", value: "high" },
|
|
2456
|
+
]},
|
|
2457
|
+
],
|
|
2458
|
+
},
|
|
2459
|
+
}, container);
|
|
2460
|
+
|
|
2461
|
+
// 用户提交后,promise resolve 为 ToolResult
|
|
2462
|
+
const result = await promise;
|
|
2463
|
+
// → { toolName: "fill-form", status: "submitted", value: { name, type, priority } }
|
|
2464
|
+
```
|
|
2465
|
+
|
|
2466
|
+
---
|
|
2467
|
+
|
|
2468
|
+
# 自建还是外购决策
|
|
2469
|
+
|
|
2470
|
+
URL: /zh-CN/examples/roi-estimator
|
|
2471
|
+
Raw Markdown: /zh-CN/examples/roi-estimator.md
|
|
2472
|
+
Source: content/examples/roi-estimator/zh-CN.md
|
|
1093
2473
|
|
|
1094
|
-
|
|
2474
|
+
# 自建还是外购决策
|
|
1095
2475
|
|
|
1096
|
-
|
|
2476
|
+
技术团队永恒的难题:这个功能是自己做,还是买现成的?这里提供一个结构化决策框架——不是算一个数字,而是多维度权衡。
|
|
1097
2477
|
|
|
1098
2478
|
```slex
|
|
1099
2479
|
{
|
|
1100
|
-
|
|
2480
|
+
slex: "0.1",
|
|
2481
|
+
namespace: "example_build_vs_buy",
|
|
2482
|
+
g: {
|
|
2483
|
+
scope: "core",
|
|
2484
|
+
buildFit: 60, buildTime: 6, buildCost: 80,
|
|
2485
|
+
buyFit: 85, buyTime: 1, buyCost: 40, buyVendorLock: 30,
|
|
2486
|
+
buildTotal: function () { return (this.buildFit + (100 - this.buildCost) + (100 - this.buildTime * 10)) / 3; },
|
|
2487
|
+
buyTotal: function () { return (this.buyFit + (100 - this.buyCost) + (100 - this.buyTime * 10) - this.buyVendorLock * 0.5) / 3; },
|
|
2488
|
+
recommendation: function () {
|
|
2489
|
+
if (this.scope === "core") return this.buildTotal() >= this.buyTotal() ? "自建" : "外购(但建议评估长期成本)";
|
|
2490
|
+
return this.buyTotal() >= this.buildTotal() ? "外购" : "自建(非核心功能自建需谨慎)";
|
|
2491
|
+
},
|
|
2492
|
+
diff: function () { return Math.abs(this.buildTotal() - this.buyTotal()); }
|
|
2493
|
+
},
|
|
1101
2494
|
layout: {
|
|
1102
|
-
"
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
"
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
2495
|
+
"section:decision": {
|
|
2496
|
+
eyebrow: "技术决策",
|
|
2497
|
+
title: "自建还是外购决策",
|
|
2498
|
+
subtitle: "对比两个方案的各维度评分,系统综合推荐。拖滑块调整评分看结论变化。",
|
|
2499
|
+
"select:scope": {
|
|
2500
|
+
label: "功能定位",
|
|
2501
|
+
"$value": "g.scope",
|
|
2502
|
+
options: [
|
|
2503
|
+
{ label: "核心业务(差异化的关键)", value: "core" },
|
|
2504
|
+
{ label: "辅助功能(非核心)", value: "non-core" }
|
|
2505
|
+
],
|
|
2506
|
+
onchange: "g.scope = String($event)"
|
|
2507
|
+
},
|
|
2508
|
+
"table:comparison": {
|
|
2509
|
+
columns: ["维度", "自建方案", "外购方案"],
|
|
2510
|
+
rows: [
|
|
2511
|
+
["功能匹配度", "g.buildFit + '%'", "g.buyFit + '%'"],
|
|
2512
|
+
["上线时间", "g.buildTime + ' 个月'", "g.buyTime + ' 个月'"],
|
|
2513
|
+
["成本评分", "g.buildCost + '/100'", "g.buyCost + '/100'"],
|
|
2514
|
+
["供应商锁定", "—", "g.buyVendorLock + '/100'"]
|
|
2515
|
+
]
|
|
2516
|
+
},
|
|
2517
|
+
"grid:sliders": {
|
|
2518
|
+
columns: 1, mdColumns: 2,
|
|
2519
|
+
"column:build": {
|
|
2520
|
+
"card:buildSliders": {
|
|
2521
|
+
title: "自建方案",
|
|
2522
|
+
"slider:buildFit": { label: "功能匹配度", "$value": "g.buildFit", min: 0, max: 100, step: 5, onchange: "g.buildFit = Number($event)" },
|
|
2523
|
+
"slider:buildTime": { label: "上线时间(月)", "$value": "g.buildTime", min: 1, max: 24, step: 1, unit: "月", onchange: "g.buildTime = Number($event)" },
|
|
2524
|
+
"slider:buildCost": { label: "成本评分", "$value": "g.buildCost", min: 0, max: 100, step: 5, onchange: "g.buildCost = Number($event)" }
|
|
2525
|
+
}
|
|
2526
|
+
},
|
|
2527
|
+
"column:buy": {
|
|
2528
|
+
"card:buySliders": {
|
|
2529
|
+
title: "外购方案",
|
|
2530
|
+
"slider:buyFit": { label: "功能匹配度", "$value": "g.buyFit", min: 0, max: 100, step: 5, onchange: "g.buyFit = Number($event)" },
|
|
2531
|
+
"slider:buyTime": { label: "上线时间(月)", "$value": "g.buyTime", min: 1, max: 24, step: 1, unit: "月", onchange: "g.buyTime = Number($event)" },
|
|
2532
|
+
"slider:buyCost": { label: "成本评分", "$value": "g.buyCost", min: 0, max: 100, step: 5, onchange: "g.buyCost = Number($event)" },
|
|
2533
|
+
"slider:lock": { label: "供应商锁定风险", "$value": "g.buyVendorLock", min: 0, max: 100, step: 5, onchange: "g.buyVendorLock = Number($event)" }
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
},
|
|
2537
|
+
"grid:results": {
|
|
2538
|
+
columns: 1, mdColumns: 3,
|
|
2539
|
+
"stat:buildScore": { label: "自建综合得分", "$value": "g.buildTotal().toFixed(1)" },
|
|
2540
|
+
"stat:buyScore": { label: "外购综合得分", "$value": "g.buyTotal().toFixed(1)" },
|
|
2541
|
+
"badge:winner": { "$label": "g.recommendation().startsWith('自建') ? '建议自建' : '建议外购'", "$tone": "g.diff() < 10 ? 'warning' : g.recommendation().startsWith('自建') ? 'info' : 'success'" }
|
|
2542
|
+
},
|
|
2543
|
+
"callout:advice": {
|
|
2544
|
+
"$tone": "g.diff() < 10 ? 'warning' : 'info'",
|
|
2545
|
+
"$text": "g.diff() < 10 ? '两个方案得分非常接近——建议引入更多决策者参与讨论,或做小范围 POC。' : g.recommendation() + ' 方案的得分明显更高。但请结合团队实际能力和战略方向做最终决定。'"
|
|
2546
|
+
},
|
|
2547
|
+
"accordion:detail": {
|
|
2548
|
+
multiple: true,
|
|
2549
|
+
items: [
|
|
2550
|
+
{ value: "cost", label: "成本说明", content: "成本评分越低越好(0 = 零成本,100 = 极高成本)。外购方案需额外考虑供应商锁定风险。" },
|
|
2551
|
+
{ value: "scope", label: "核心 vs 非核心策略", content: "核心业务功能通常倾向自建以保持控制力和差异化。非核心功能外购可以释放团队精力。" },
|
|
2552
|
+
{ value: "hybrid", label: "第三条路:混合方案", content: "也可以考虑先外购快速上线,同时内部规划自建替代方案,等自建成熟后迁移。" }
|
|
2553
|
+
]
|
|
2554
|
+
}
|
|
1112
2555
|
}
|
|
1113
2556
|
}
|
|
1114
2557
|
}
|
|
1115
2558
|
```
|
|
1116
2559
|
|
|
1117
|
-
Minimal reading path:
|
|
1118
|
-
|
|
1119
|
-
1. Start with [`/llms.txt`](/llms.txt) for the grouped index.
|
|
1120
|
-
2. Use [`/llms-full.txt`](/llms-full.txt) when the agent needs broad context.
|
|
1121
|
-
3. Use [`/llms-components.txt`](/llms-components.txt) and raw component `.md` pages when authoring UI.
|
|
1122
|
-
4. Use [`/llms-runtime.txt`](/llms-runtime.txt) for host and secure runtime integration.
|
|
1123
|
-
5. Use [`/llms-toolhost.txt`](/llms-toolhost.txt) only when user input must return structured data to the host.
|
|
1124
|
-
|
|
1125
|
-
SlexKit raw docs are ordinary `.md` pages with explicit `slex` fences. There is no `.mdx` route — `slex` fences are the interactive layer.
|
|
1126
2560
|
|
|
1127
|
-
|
|
2561
|
+
**Build vs Buy 决策模型的要点:**
|
|
1128
2562
|
|
|
1129
|
-
|
|
2563
|
+
- `select` 定义功能定位(核心/非核心),影响决策偏向
|
|
2564
|
+
- 两套 slider 独立调整各自评分
|
|
2565
|
+
- `buildTotal()` 和 `buyTotal()` 用加权公式计算综合得分
|
|
2566
|
+
- `recommendation()` 结合功能定位和得分给出建议
|
|
2567
|
+
- accordion 提供额外的决策指南(成本说明、策略建议、混合方案)
|
|
1130
2568
|
|
|
1131
|
-
|
|
1132
|
-
## SlexKit
|
|
2569
|
+
这个框架帮你把"凭感觉拍脑袋"变成"有依据的比较"。
|
|
1133
2570
|
|
|
1134
|
-
|
|
2571
|
+
---
|
|
1135
2572
|
|
|
1136
|
-
|
|
2573
|
+
# 跨文档状态实验室
|
|
1137
2574
|
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
- ToolHost is only for structured user input flows.
|
|
1142
|
-
- Untrusted or agent-generated source should use the secure runtime.
|
|
1143
|
-
- Raw docs are `.md` files with `slex` fences, not `.mdx`.
|
|
1144
|
-
````
|
|
2575
|
+
URL: /zh-CN/examples/cross-doc-state-lab
|
|
2576
|
+
Raw Markdown: /zh-CN/examples/cross-doc-state-lab.md
|
|
2577
|
+
Source: content/examples/cross-doc-state-lab/zh-CN.md
|
|
1145
2578
|
|
|
1146
|
-
|
|
2579
|
+
# 跨文档状态实验室
|
|
1147
2580
|
|
|
1148
|
-
|
|
2581
|
+
这是 SlexKit 最独特的能力之一:**在同一个 Markdown 文档的不同位置放置多块 `slex` 代码,只要它们使用相同的 `namespace`,就会共享同一份 `g` 状态**。
|
|
1149
2582
|
|
|
1150
|
-
|
|
1151
|
-
- `/author`: write display-oriented `slex` fences with Markdown fallback
|
|
1152
|
-
- `/host`: integrate Markdown, Streamdown, Obsidian, or custom hosts
|
|
1153
|
-
- `/toolhost`: build confirmations, choices, and structured forms
|
|
1154
|
-
- `/secure`: configure sandbox runtime and host policy
|
|
1155
|
-
- `/update`: regenerate AI docs after API, docs, or component changes
|
|
2583
|
+
下面是三个独立的 ` ```slex ` fence——一个控制面板和两个观察面板。试试修改控制面板的值,看下方两个面板实时响应。
|
|
1156
2584
|
|
|
1157
|
-
|
|
2585
|
+
## 主控面板
|
|
1158
2586
|
|
|
1159
|
-
|
|
2587
|
+
```slex
|
|
2588
|
+
{
|
|
2589
|
+
slex: "0.1",
|
|
2590
|
+
namespace: "example_cross_doc_lab",
|
|
2591
|
+
g: {
|
|
2592
|
+
color: "blue", size: 16, theme: "light",
|
|
2593
|
+
style: function () {
|
|
2594
|
+
return 'color: ' + this.color + '; font-size: ' + this.size + 'px;';
|
|
2595
|
+
}
|
|
2596
|
+
},
|
|
2597
|
+
layout: {
|
|
2598
|
+
"section:control": {
|
|
2599
|
+
eyebrow: "平台能力",
|
|
2600
|
+
title: "跨文档状态实验室 · 主控面板",
|
|
2601
|
+
subtitle: "修改以下任何参数——下方两个独立 fence 块的卡片会同步更新。",
|
|
2602
|
+
"grid:controls": {
|
|
2603
|
+
columns: 1, mdColumns: 3,
|
|
2604
|
+
"select:color": {
|
|
2605
|
+
label: "文字颜色",
|
|
2606
|
+
"$value": "g.color",
|
|
2607
|
+
options: [
|
|
2608
|
+
{ label: "蓝色", value: "blue" },
|
|
2609
|
+
{ label: "绿色", value: "green" },
|
|
2610
|
+
{ label: "橙色", value: "orange" },
|
|
2611
|
+
{ label: "紫色", value: "purple" }
|
|
2612
|
+
],
|
|
2613
|
+
onchange: "g.color = String($event)"
|
|
2614
|
+
},
|
|
2615
|
+
"slider:size": { label: "字体大小", "$value": "g.size", min: 8, max: 48, step: 2, unit: "px", onchange: "g.size = Number($event)" },
|
|
2616
|
+
"select:theme": {
|
|
2617
|
+
label: "卡片主题",
|
|
2618
|
+
"$value": "g.theme",
|
|
2619
|
+
options: [
|
|
2620
|
+
{ label: "明亮", value: "light" },
|
|
2621
|
+
{ label: "暗色", value: "dark" },
|
|
2622
|
+
{ label: "信息", value: "info" }
|
|
2623
|
+
],
|
|
2624
|
+
onchange: "g.theme = String($event)"
|
|
2625
|
+
}
|
|
2626
|
+
},
|
|
2627
|
+
"badge:note": { "$label": "'样式 ' + g.color + ' ' + g.size + 'px'", tone: "info" }
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
```
|
|
1160
2632
|
|
|
1161
|
-
|
|
2633
|
+
## 观察面板 A(同一 namespace,不同 fence 块)
|
|
1162
2634
|
|
|
1163
2635
|
```slex
|
|
1164
2636
|
{
|
|
1165
|
-
|
|
2637
|
+
slex: "0.1",
|
|
2638
|
+
namespace: "example_cross_doc_lab",
|
|
1166
2639
|
layout: {
|
|
1167
|
-
"
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
icon: "book-open-text",
|
|
1173
|
-
"text:body": { text: "Search or fetch Markdown docs by query, group, slug, or raw .md URL." }
|
|
1174
|
-
},
|
|
1175
|
-
"card:examples": {
|
|
1176
|
-
title: "slexkitExamples",
|
|
1177
|
-
icon: "code",
|
|
1178
|
-
"text:body": { text: "Browse component examples, ToolHost templates, and host integration snippets." }
|
|
2640
|
+
"card:a": {
|
|
2641
|
+
title: "观察面板 A — 纯文本样式",
|
|
2642
|
+
"column:stylePreview": {
|
|
2643
|
+
"text:styleMeta": { "$text": "'字体大小:' + g.size + 'px'" },
|
|
2644
|
+
"text:styledValue": { "$text": "g.color", "$color": "g.color", "$size": "g.size" }
|
|
1179
2645
|
},
|
|
1180
|
-
"
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
"text:body": { text: "Parse Slex source and return diagnostics plus component usage." }
|
|
2646
|
+
"callout:preview": {
|
|
2647
|
+
"$tone": "g.theme === 'dark' ? 'danger' : g.theme === 'info' ? 'info' : 'success'",
|
|
2648
|
+
"$text": "g.theme === 'dark' ? '暗色模式:适合夜间阅读的配色方案。' : g.theme === 'info' ? '信息模式:用于强调技术细节。' : '明亮模式:默认的文档阅读配色。'"
|
|
1184
2649
|
}
|
|
1185
2650
|
}
|
|
1186
2651
|
}
|
|
1187
2652
|
}
|
|
1188
2653
|
```
|
|
1189
2654
|
|
|
1190
|
-
|
|
2655
|
+
## 观察面板 B
|
|
1191
2656
|
|
|
1192
|
-
```
|
|
1193
|
-
|
|
2657
|
+
```slex
|
|
2658
|
+
{
|
|
2659
|
+
slex: "0.1",
|
|
2660
|
+
namespace: "example_cross_doc_lab",
|
|
2661
|
+
layout: {
|
|
2662
|
+
"card:b": {
|
|
2663
|
+
title: "观察面板 B — 参数详情",
|
|
2664
|
+
"grid:params": {
|
|
2665
|
+
columns: 1, mdColumns: 3,
|
|
2666
|
+
"stat:col": { label: "颜色", "$value": "g.color" },
|
|
2667
|
+
"stat:sz": { label: "字号", "$value": "g.size", unit: "px" },
|
|
2668
|
+
"stat:th": { label: "主题", "$value": "g.theme" }
|
|
2669
|
+
},
|
|
2670
|
+
"badge:sync": { "$label": "'已同步 ' + g.color", "$tone": "g.color === 'blue' ? 'info' : g.color === 'green' ? 'success' : g.color === 'orange' ? 'warning' : 'info'" }
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2673
|
+
}
|
|
1194
2674
|
```
|
|
1195
2675
|
|
|
1196
|
-
|
|
2676
|
+
**三个 fence 块**,同一个 `namespace: "example_cross_doc_lab"`,所有组件共享 `g` 对象。上面你在主控面板改颜色和大小,下面两个观察面板马上更新。
|
|
2677
|
+
|
|
2678
|
+
---
|
|
2679
|
+
|
|
2680
|
+
### 这意味着什么?
|
|
2681
|
+
|
|
2682
|
+
假设你在一篇长篇 Markdown 文档中:
|
|
1197
2683
|
|
|
1198
|
-
```
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
2684
|
+
```
|
|
2685
|
+
[控制面板 — 选择行业/指标/时间范围]
|
|
2686
|
+
... 30 段 Markdown 叙事 ...
|
|
2687
|
+
[图表 A — 自动反映控制面板的选项]
|
|
2688
|
+
... 更多分析文字 ...
|
|
2689
|
+
[图表 B — 同一份状态的不同可视化]
|
|
1204
2690
|
```
|
|
1205
2691
|
|
|
1206
|
-
|
|
2692
|
+
每个 ` ```slex ` 块可以独立渲染,但只要 namespace 相同,它们就共享状态。这对于:
|
|
2693
|
+
|
|
2694
|
+
- **技术白皮书**:顶部选参数,中间分析,底部结论,全程联动
|
|
2695
|
+
- **项目协作文档**:状态跟踪表格在顶部,各团队任务卡片散布在正文中
|
|
2696
|
+
- **AI 输出增强**:模型生成的多个可视化节点共享同一份推理结果
|
|
2697
|
+
|
|
2698
|
+
|
|
2699
|
+
都是极其强大的模式。这不是一个 "组件库" 能做到的——这是 SlexKit 的核心设计。
|
|
2700
|
+
|
|
2701
|
+
---
|
|
2702
|
+
|
|
2703
|
+
# JSONPlaceholder 网络请求实验台
|
|
2704
|
+
|
|
2705
|
+
URL: /zh-CN/examples/network-policy-fetch-card
|
|
2706
|
+
Raw Markdown: /zh-CN/examples/network-policy-fetch-card.md
|
|
2707
|
+
Source: content/examples/network-policy-fetch-card/zh-CN.md
|
|
2708
|
+
|
|
2709
|
+
# JSONPlaceholder 网络请求实验台
|
|
2710
|
+
|
|
2711
|
+
这个示例不再只是展示一段策略配置,而是让用户在沙盒内选择真实的网络任务:读取 posts、查看详情、拉取评论、按用户过滤,或者向 JSONPlaceholder 发起一个演示用 POST。请求不会从 iframe 直接出网,而是通过 `api.fetch()` 交给宿主,由宿主按 allowlist、方法、超时、请求体大小和响应类型决定是否代理。
|
|
1207
2712
|
|
|
1208
2713
|
```slex
|
|
1209
2714
|
{
|
|
1210
|
-
|
|
2715
|
+
slex: "0.1",
|
|
2716
|
+
namespace: "example_jsonplaceholder_network_lab",
|
|
2717
|
+
g: {
|
|
2718
|
+
scenario: "posts",
|
|
2719
|
+
postId: 1,
|
|
2720
|
+
userId: 1,
|
|
2721
|
+
timeout: 3000,
|
|
2722
|
+
title: "SlexKit network demo",
|
|
2723
|
+
status: "未请求",
|
|
2724
|
+
statusCode: "-",
|
|
2725
|
+
elapsed: "-",
|
|
2726
|
+
response: "选择一个网络任务,然后点击“发起请求”。",
|
|
2727
|
+
lastUrl: "https://jsonplaceholder.typicode.com/posts",
|
|
2728
|
+
method: function () { return this.scenario === "create" ? "POST" : "GET"; },
|
|
2729
|
+
url: function () {
|
|
2730
|
+
if (this.scenario === "detail") return "https://jsonplaceholder.typicode.com/posts/" + this.postId;
|
|
2731
|
+
if (this.scenario === "comments") return "https://jsonplaceholder.typicode.com/posts/" + this.postId + "/comments";
|
|
2732
|
+
if (this.scenario === "user-posts") return "https://jsonplaceholder.typicode.com/users/" + this.userId + "/posts";
|
|
2733
|
+
if (this.scenario === "create") return "https://jsonplaceholder.typicode.com/posts";
|
|
2734
|
+
return "https://jsonplaceholder.typicode.com/posts?_limit=5";
|
|
2735
|
+
},
|
|
2736
|
+
requestBody: function () {
|
|
2737
|
+
if (this.scenario !== "create") return undefined;
|
|
2738
|
+
return {
|
|
2739
|
+
title: this.title,
|
|
2740
|
+
body: "这是一条从 SlexKit secure runtime 发起、由宿主代理的演示请求。",
|
|
2741
|
+
userId: this.userId
|
|
2742
|
+
};
|
|
2743
|
+
},
|
|
2744
|
+
description: function () {
|
|
2745
|
+
if (this.scenario === "detail") return "读取单篇 post,适合详情页或引用卡片。";
|
|
2746
|
+
if (this.scenario === "comments") return "读取某篇 post 的评论,适合评论摘要、审阅流和证据面板。";
|
|
2747
|
+
if (this.scenario === "user-posts") return "按用户读取 posts,适合个人空间、作者档案或关联资源列表。";
|
|
2748
|
+
if (this.scenario === "create") return "提交一条演示 post。JSONPlaceholder 会返回假写入结果,不会持久化。";
|
|
2749
|
+
return "读取最新 posts 列表,适合 feed、任务列表和知识库索引。";
|
|
2750
|
+
},
|
|
2751
|
+
riskText: function () {
|
|
2752
|
+
if (this.method() === "POST") return "POST 已被限制为 JSONPlaceholder origin,且请求体大小受 policy 约束。";
|
|
2753
|
+
return "GET 请求仍然要经过 origin、超时、响应大小和 content-type 校验。";
|
|
2754
|
+
},
|
|
2755
|
+
requestSnippet: function () {
|
|
2756
|
+
var body = this.requestBody();
|
|
2757
|
+
var lines = [
|
|
2758
|
+
"await api.fetch('" + this.url() + "', {",
|
|
2759
|
+
" method: '" + this.method() + "',",
|
|
2760
|
+
" timeoutMs: " + this.timeout + ","
|
|
2761
|
+
];
|
|
2762
|
+
if (body) lines.push(" body: " + JSON.stringify(body, null, 2).replace(/\n/g, "\n ") + ",");
|
|
2763
|
+
lines.push(" credentials: 'omit'");
|
|
2764
|
+
lines.push("})");
|
|
2765
|
+
return lines.join("\n");
|
|
2766
|
+
},
|
|
2767
|
+
policyRows: function () {
|
|
2768
|
+
return [
|
|
2769
|
+
{ item: "Origin", value: "https://jsonplaceholder.typicode.com", reason: "只允许演示 API" },
|
|
2770
|
+
{ item: "Method", value: "GET, POST", reason: "读列表/详情和创建演示数据" },
|
|
2771
|
+
{ item: "Credentials", value: "omit", reason: "不携带 cookie 或站点身份" },
|
|
2772
|
+
{ item: "Content-Type", value: "application/json", reason: "拒绝非 JSON 响应进入沙盒" },
|
|
2773
|
+
{ item: "Body", value: "<= 4096 bytes", reason: "避免大请求体被模型输出滥用" }
|
|
2774
|
+
];
|
|
2775
|
+
},
|
|
2776
|
+
async run(api) {
|
|
2777
|
+
var targetUrl = String(this.url());
|
|
2778
|
+
this.status = "请求中";
|
|
2779
|
+
this.statusCode = "-";
|
|
2780
|
+
this.elapsed = "-";
|
|
2781
|
+
this.lastUrl = targetUrl;
|
|
2782
|
+
this.response = "等待宿主代理 " + this.method() + " " + targetUrl;
|
|
2783
|
+
try {
|
|
2784
|
+
var result = await api.fetch(targetUrl, {
|
|
2785
|
+
method: this.method(),
|
|
2786
|
+
timeoutMs: this.timeout,
|
|
2787
|
+
body: this.requestBody(),
|
|
2788
|
+
credentials: "omit"
|
|
2789
|
+
});
|
|
2790
|
+
this.status = result.ok ? "成功" : "HTTP 错误";
|
|
2791
|
+
this.statusCode = String(result.status) + " " + result.statusText;
|
|
2792
|
+
this.elapsed = Math.round(result.elapsedMs) + " ms";
|
|
2793
|
+
this.response = JSON.stringify(result.data === undefined ? result.text : result.data, null, 2).slice(0, 2400);
|
|
2794
|
+
} catch (error) {
|
|
2795
|
+
this.status = api.isPolicyError(error) ? "Policy 拦截" : api.isTimeoutError(error) ? "超时" : "网络失败";
|
|
2796
|
+
this.statusCode = api.isPolicyError(error) ? "policy" : api.isTimeoutError(error) ? "timeout" : "network";
|
|
2797
|
+
var elapsedMs = error && typeof error === "object" && typeof error.elapsedMs === "number" ? error.elapsedMs : undefined;
|
|
2798
|
+
this.elapsed = elapsedMs === undefined ? "-" : Math.round(elapsedMs) + " ms";
|
|
2799
|
+
this.response = targetUrl + "\n" + api.errorMessage(error);
|
|
2800
|
+
}
|
|
2801
|
+
},
|
|
2802
|
+
async runBlocked(api) {
|
|
2803
|
+
var targetUrl = "https://example.com/posts/1";
|
|
2804
|
+
this.status = "请求中";
|
|
2805
|
+
this.statusCode = "-";
|
|
2806
|
+
this.elapsed = "-";
|
|
2807
|
+
this.lastUrl = targetUrl;
|
|
2808
|
+
this.response = "这次请求故意访问 allowlist 外的 origin,应该被 host policy 拦截。";
|
|
2809
|
+
try {
|
|
2810
|
+
await api.get(targetUrl, { timeoutMs: this.timeout, credentials: "omit" });
|
|
2811
|
+
this.status = "异常通过";
|
|
2812
|
+
this.statusCode = "unexpected";
|
|
2813
|
+
this.response = "如果看到这行,说明 policy 没有按预期拦截。";
|
|
2814
|
+
} catch (error) {
|
|
2815
|
+
this.status = api.isPolicyError(error) ? "Policy 拦截" : "失败";
|
|
2816
|
+
this.statusCode = api.isPolicyError(error) ? "origin_blocked" : "network";
|
|
2817
|
+
this.response = targetUrl + "\n" + api.errorMessage(error);
|
|
2818
|
+
}
|
|
2819
|
+
}
|
|
2820
|
+
},
|
|
1211
2821
|
layout: {
|
|
1212
|
-
"
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
code: "[mcp_servers.slexkit]\ncommand = \"npx\"\nargs = [\"-y\", \"@slexkit/mcp\"]"
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
2822
|
+
"section:network": {
|
|
2823
|
+
eyebrow: "平台能力",
|
|
2824
|
+
title: "JSONPlaceholder 网络请求实验台",
|
|
2825
|
+
subtitle: "在沙盒内选择网络任务,请求通过 host policy 代理。",
|
|
2826
|
+
"card:network": {
|
|
2827
|
+
title: "JSONPlaceholder 请求实验台",
|
|
2828
|
+
"callout:intent": { tone: "info", "$text": "g.description()" },
|
|
2829
|
+
"grid:controls": {
|
|
2830
|
+
columns: 1,
|
|
2831
|
+
mdColumns: 2,
|
|
2832
|
+
"select:scenario": {
|
|
2833
|
+
label: "网络任务",
|
|
2834
|
+
"$value": "g.scenario",
|
|
2835
|
+
options: [
|
|
2836
|
+
{ label: "Posts 列表 GET /posts", value: "posts" },
|
|
2837
|
+
{ label: "Post 详情 GET /posts/:id", value: "detail" },
|
|
2838
|
+
{ label: "评论列表 GET /posts/:id/comments", value: "comments" },
|
|
2839
|
+
{ label: "用户 posts GET /users/:id/posts", value: "user-posts" },
|
|
2840
|
+
{ label: "创建 post POST /posts", value: "create" }
|
|
2841
|
+
],
|
|
2842
|
+
onchange: "g.scenario = String($event)"
|
|
1236
2843
|
},
|
|
1237
|
-
{
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
2844
|
+
"slider:postId": { label: "Post ID", "$value": "g.postId", min: 1, max: 10, step: 1, onchange: "g.postId = Number($event)" },
|
|
2845
|
+
"slider:userId": { label: "User ID", "$value": "g.userId", min: 1, max: 10, step: 1, onchange: "g.userId = Number($event)" },
|
|
2846
|
+
"slider:timeout": { label: "超时上限", "$value": "g.timeout", min: 500, max: 8000, step: 500, unit: "ms", onchange: "g.timeout = Number($event)" }
|
|
2847
|
+
},
|
|
2848
|
+
"row:actions": {
|
|
2849
|
+
"button:run": { label: "发起请求", icon: "paper-plane-tilt", onclick: "g.run(api)" },
|
|
2850
|
+
"button:block": { label: "测试拦截", variant: "secondary", icon: "shield-warning", onclick: "g.runBlocked(api)" }
|
|
2851
|
+
},
|
|
2852
|
+
"grid:status": {
|
|
2853
|
+
columns: 1,
|
|
2854
|
+
mdColumns: 3,
|
|
2855
|
+
"stat:method": { label: "方法", "$value": "g.method()" },
|
|
2856
|
+
"stat:status": { label: "状态", "$value": "g.status" },
|
|
2857
|
+
"stat:elapsed": { label: "耗时", "$value": "g.elapsed" }
|
|
2858
|
+
},
|
|
2859
|
+
"code-block:request": { title: "沙盒内请求代码", language: "ts", "$code": "g.requestSnippet()" },
|
|
2860
|
+
"code-block:response": { title: "宿主代理响应", language: "json", "$code": "g.response" },
|
|
2861
|
+
"table:policy_matrix": {
|
|
2862
|
+
columns: [
|
|
2863
|
+
{ key: "item", label: "Policy 项" },
|
|
2864
|
+
{ key: "value", label: "允许值" },
|
|
2865
|
+
{ key: "reason", label: "原因" }
|
|
2866
|
+
],
|
|
2867
|
+
"$rows": "g.policyRows()"
|
|
2868
|
+
},
|
|
2869
|
+
"callout:policy_note": { "$tone": "g.status === 'Policy 拦截' ? 'warning' : 'success'", "$text": "g.riskText()" }
|
|
2870
|
+
}
|
|
1249
2871
|
}
|
|
1250
2872
|
}
|
|
1251
2873
|
}
|
|
1252
2874
|
```
|
|
1253
2875
|
|
|
1254
|
-
## Troubleshooting
|
|
1255
|
-
|
|
1256
|
-
- MCP server does not start: verify `npx` and MCP config JSON, then restart the IDE.
|
|
1257
|
-
- Tool calls fail: restart the MCP server and confirm `tools/list` exposes only `slexkitDocs`, `slexkitExamples`, and `slexkitValidate`.
|
|
1258
|
-
- Docs are stale: run `bun run ai:docs` or `bun run build:core`.
|
|
1259
|
-
- Wrong raw source route: use `.md` routes with `slex` fences. Do not request `.mdx`.
|
|
1260
|
-
|
|
1261
2876
|
---
|
|
1262
2877
|
|
|
1263
2878
|
# Accordion
|
|
@@ -2048,6 +3663,64 @@ Horizontal separator, optionally with a centered text label.
|
|
|
2048
3663
|
|
|
2049
3664
|
---
|
|
2050
3665
|
|
|
3666
|
+
# Formula
|
|
3667
|
+
|
|
3668
|
+
URL: /docs/components/formula
|
|
3669
|
+
Raw Markdown: /docs/components/formula.md
|
|
3670
|
+
Source: site/content/components/formula/en-US.md
|
|
3671
|
+
|
|
3672
|
+
---
|
|
3673
|
+
title: "Formula"
|
|
3674
|
+
category: Display
|
|
3675
|
+
status: ready
|
|
3676
|
+
order: 11
|
|
3677
|
+
summary: "Reactive KaTeX formula display."
|
|
3678
|
+
---
|
|
3679
|
+
# Formula
|
|
3680
|
+
|
|
3681
|
+
Render SlexKit state and computed values through KaTeX. Use it when Markdown explains the model and the interactive block needs the formula itself to update.
|
|
3682
|
+
|
|
3683
|
+
<!-- slex:spec-example:start component="formula" id="basic" sourceHash="1578d25d" -->
|
|
3684
|
+
```slex
|
|
3685
|
+
{
|
|
3686
|
+
"slex": "0.1",
|
|
3687
|
+
"namespace": "doc_formula_typical",
|
|
3688
|
+
"g": {
|
|
3689
|
+
"r": 10000,
|
|
3690
|
+
"c": 100,
|
|
3691
|
+
"fc": 159.15
|
|
3692
|
+
},
|
|
3693
|
+
"layout": {
|
|
3694
|
+
"formula:cutoff": {
|
|
3695
|
+
"$tex": "'f_c = \\\\frac{1}{2\\\\pi RC} = ' + g.fc + '\\\\text{ Hz}'"
|
|
3696
|
+
}
|
|
3697
|
+
}
|
|
3698
|
+
}
|
|
3699
|
+
```
|
|
3700
|
+
<!-- slex:spec-example:end -->
|
|
3701
|
+
|
|
3702
|
+
## Usage Notes
|
|
3703
|
+
|
|
3704
|
+
- Use for formulas whose variables come from SlexKit state.
|
|
3705
|
+
- Keep the explanatory derivation in Markdown and use `formula` for the live expression.
|
|
3706
|
+
- Use `displayMode: false` for inline formula fragments.
|
|
3707
|
+
- Invalid TeX is rendered by KaTeX as an error expression instead of throwing.
|
|
3708
|
+
|
|
3709
|
+
## API Reference {#api}
|
|
3710
|
+
|
|
3711
|
+
<!-- slex:spec-api:start component="formula" sourceHash="144588e3" -->
|
|
3712
|
+
| Field | Type | Required | Dynamic | Default | Description |
|
|
3713
|
+
|---|---|---|---|---|---|
|
|
3714
|
+
| `tex` | string | No | Yes | | KaTeX source to render. |
|
|
3715
|
+
| `formula` | string | No | Yes | | Alias for tex. |
|
|
3716
|
+
| `value` | string | No | Yes | | Alias for tex. |
|
|
3717
|
+
| `displayMode` | boolean | No | No | `true` | Render as display math when true; inline math when false. |
|
|
3718
|
+
| `display` | boolean | No | No | `true` | Alias for displayMode. |
|
|
3719
|
+
| `block` | boolean | No | No | `true` | Alias for displayMode. |
|
|
3720
|
+
<!-- slex:spec-api:end -->
|
|
3721
|
+
|
|
3722
|
+
---
|
|
3723
|
+
|
|
2051
3724
|
# Grid
|
|
2052
3725
|
|
|
2053
3726
|
URL: /docs/components/grid
|
|
@@ -2429,8 +4102,8 @@ Single-line text input with controlled value, placeholder, label, description, n
|
|
|
2429
4102
|
- Related components: `select` for option selection, `slider` for numeric ranges.
|
|
2430
4103
|
- Typically placed inside a `column` to compose forms.
|
|
2431
4104
|
- Use `$value` and `onchange` for state binding.
|
|
2432
|
-
- Numeric
|
|
2433
|
-
- `onchange` fires when the user edits the value
|
|
4105
|
+
- Numeric and engineering inputs render only the native input. Use `slider` for range adjustment; SlexKit no longer adds custom decrement or increment buttons.
|
|
4106
|
+
- `onchange` fires when the user edits the value.
|
|
2434
4107
|
- `type: "number"` still emits a string value. Convert with `Number($event)` or use `type: "engineering"` to read parsed results.
|
|
2435
4108
|
- Use `invalid` plus `error` for validation feedback. Error text is linked through `aria-describedby`.
|
|
2436
4109
|
|
|
@@ -2497,7 +4170,7 @@ Supports scientific notation and SI prefixes: `p`, `n`, `u`, `µ`, `m`, `k`, `K`
|
|
|
2497
4170
|
|
|
2498
4171
|
## API Reference {#api}
|
|
2499
4172
|
|
|
2500
|
-
<!-- slex:spec-api:start component="input" sourceHash="
|
|
4173
|
+
<!-- slex:spec-api:start component="input" sourceHash="a1afe57e" -->
|
|
2501
4174
|
| Field | Type | Required | Dynamic | Default | Description |
|
|
2502
4175
|
|---|---|---|---|---|---|
|
|
2503
4176
|
| `value` | string | No | Yes | | Current input value. |
|
|
@@ -2520,7 +4193,6 @@ Supports scientific notation and SI prefixes: `p`, `n`, `u`, `µ`, `m`, `k`, `K`
|
|
|
2520
4193
|
| `min` | string \| number | No | Yes | | Minimum value used by numeric input controls. |
|
|
2521
4194
|
| `max` | string \| number | No | Yes | | Maximum value used by numeric input controls. |
|
|
2522
4195
|
| `step` | string \| number | No | Yes | | Step size used by numeric input controls. |
|
|
2523
|
-
| `controls` | boolean | No | Yes | `true` | Show decrement and increment buttons for numeric inputs. |
|
|
2524
4196
|
| `onchange` | write-expression | No | No | | Write expression invoked when the value changes. |
|
|
2525
4197
|
<!-- slex:spec-api:end -->
|
|
2526
4198
|
|
|
@@ -3595,13 +5267,15 @@ Output text content for status messages, descriptions, and result display.
|
|
|
3595
5267
|
|
|
3596
5268
|
## API Reference {#api}
|
|
3597
5269
|
|
|
3598
|
-
<!-- slex:spec-api:start component="text" sourceHash="
|
|
5270
|
+
<!-- slex:spec-api:start component="text" sourceHash="745fea9a" -->
|
|
3599
5271
|
| Field | Type | Required | Dynamic | Default | Description |
|
|
3600
5272
|
|---|---|---|---|---|---|
|
|
3601
5273
|
| `text` | string | No | Yes | | Displayed text. |
|
|
3602
5274
|
| `content` | string | No | Yes | | Alias for text. |
|
|
3603
5275
|
| `label` | string | No | Yes | | Alias for text. |
|
|
3604
5276
|
| `variant` | string: default, muted | No | No | `"default"` | Text visual variant. |
|
|
5277
|
+
| `color` | string | No | Yes | | Optional CSS color for controlled previews. |
|
|
5278
|
+
| `size` | string \| number | No | Yes | | Optional font size. Numbers are treated as px. |
|
|
3605
5279
|
| `class` | string | No | No | | Additional host-controlled CSS class. |
|
|
3606
5280
|
<!-- slex:spec-api:end -->
|
|
3607
5281
|
|
|
@@ -3757,7 +5431,7 @@ ComponentKey = ComponentType ":" Identifier
|
|
|
3757
5431
|
- Named components use `Identifier` for instance state and lifecycle hooks.
|
|
3758
5432
|
- Keys without `:` are not rendered as component nodes.
|
|
3759
5433
|
|
|
3760
|
-
Reserved context names: `g`, `api`, `$event`, `$item`, `$index`, `$key`.
|
|
5434
|
+
Reserved context names: `g`, `std`, `api`, `$event`, `$item`, `$index`, `$key`.
|
|
3761
5435
|
|
|
3762
5436
|
## 3. Props classification
|
|
3763
5437
|
|
|
@@ -3814,6 +5488,7 @@ Expressions can access these variables:
|
|
|
3814
5488
|
| Variable | Type | Scope |
|
|
3815
5489
|
| ------------------ | ----------------------------- | ------------------------ |
|
|
3816
5490
|
| `g` | Reactive state proxy | Always |
|
|
5491
|
+
| `std` | Pure SlexKit standard library | Always |
|
|
3817
5492
|
| Component state | e.g. `slider.value` | Named components |
|
|
3818
5493
|
| `api` | Host-injected object | If `api` option provided |
|
|
3819
5494
|
| `$event` | Event data | `on*` handlers only |
|
|
@@ -3822,6 +5497,8 @@ Expressions can access these variables:
|
|
|
3822
5497
|
| `$key` | Current item key | `$for` context only |
|
|
3823
5498
|
| Named `$for` alias | e.g. `user` for `"card:user"` | `$for` context only |
|
|
3824
5499
|
|
|
5500
|
+
`std` contains deterministic helpers for math, formatting, units, and small statistics. Sensitive capabilities stay under host-injected `api.*` and may require secure runtime policy.
|
|
5501
|
+
|
|
3825
5502
|
Expression evaluation errors are caught and produce a warning with namespace and path information. The last known value is returned as a fallback.
|
|
3826
5503
|
|
|
3827
5504
|
## 6. Component instance state
|
|
@@ -4232,6 +5909,7 @@ The `RenderContext` provides:
|
|
|
4232
5909
|
| Property | Type | Description |
|
|
4233
5910
|
|----------|------|-------------|
|
|
4234
5911
|
| `g` | reactive proxy | Global state |
|
|
5912
|
+
| `std` | `SlexKitStdlib` | Pure deterministic helpers |
|
|
4235
5913
|
| `api` | `Record<string, unknown>` | Host-injected capabilities |
|
|
4236
5914
|
| `dir` | `"ltr"` or `"rtl"` | Resolved direction |
|
|
4237
5915
|
| `labels` | `Partial<Record<string, string>>` | Runtime labels |
|
|
@@ -4301,6 +5979,8 @@ type MountOptions = {
|
|
|
4301
5979
|
|
|
4302
5980
|
Calling `mount()` again on the same `container` clears the old root first, then appends a new root. The returned cleanup only unmounts the current root; it does not delete the namespace store.
|
|
4303
5981
|
|
|
5982
|
+
Every expression receives `std`, SlexKit's pure deterministic standard library. Hosts can still inject capability objects through `api`, but network, timers, animation, and canvas should remain policy-gated in secure mode.
|
|
5983
|
+
|
|
4304
5984
|
### `ingest(input)`
|
|
4305
5985
|
|
|
4306
5986
|
Ingests state-only Slex: updates `g` without rendering UI. Used by the Markdown runtime host for state-only fences. Returns `true` if parsing succeeded.
|
|
@@ -4655,16 +6335,16 @@ The renderer handles `slex` fences. It supports both trusted and secure runtime
|
|
|
4655
6335
|
|
|
4656
6336
|
## Obsidian integration
|
|
4657
6337
|
|
|
4658
|
-
The
|
|
6338
|
+
The official Obsidian plugin lives in <https://github.com/slexkit/obsidian-slexkit> and registers the `slex` fenced code block processor:
|
|
4659
6339
|
|
|
4660
6340
|
```ts
|
|
4661
|
-
// In the
|
|
6341
|
+
// In the plugin:
|
|
4662
6342
|
registerMarkdownCodeBlockProcessor("slex", (source, el, ctx) => { ... });
|
|
4663
6343
|
```
|
|
4664
6344
|
|
|
4665
6345
|
The adapter renders blocks in **reading mode only** and does not write back to the vault. Blocks within the same note share a trusted artifact runtime.
|
|
4666
6346
|
|
|
4667
|
-
**Important**: The Obsidian adapter uses trusted mode because it renders content from the user's
|
|
6347
|
+
**Important**: The Obsidian adapter uses trusted mode because it renders content from the user's local vault. It is not designed as a security boundary for untrusted or agent-generated Markdown.
|
|
4668
6348
|
|
|
4669
6349
|
## Writing a custom host adapter
|
|
4670
6350
|
|
|
@@ -5048,7 +6728,6 @@ slexkit (root - real code)
|
|
|
5048
6728
|
@slexkit/components-svelte (thin wrapper) ─── re-exports slexkit/components-svelte
|
|
5049
6729
|
@slexkit/theme-shadcn ─── CSS only
|
|
5050
6730
|
@slexkit/streamdown ─── React/Streamdown renderer
|
|
5051
|
-
@slexkit/obsidian ─── Obsidian plugin
|
|
5052
6731
|
@slexkit/mcp ─── read-only MCP server for AI agents
|
|
5053
6732
|
```
|
|
5054
6733
|
|
|
@@ -5140,15 +6819,15 @@ export function Message({ markdown }: { markdown: string }) {
|
|
|
5140
6819
|
|
|
5141
6820
|
Processes `slex` fences. Supports both trusted and secure runtime modes.
|
|
5142
6821
|
|
|
5143
|
-
##
|
|
6822
|
+
## Obsidian plugin
|
|
5144
6823
|
|
|
5145
|
-
Obsidian plugin
|
|
6824
|
+
The official Obsidian plugin lives in a separate release repository: <https://github.com/slexkit/obsidian-slexkit>.
|
|
5146
6825
|
|
|
5147
|
-
|
|
5148
|
-
npm install slexkit @slexkit/obsidian
|
|
5149
|
-
```
|
|
6826
|
+
Install **SlexKit** through Obsidian Community Plugins for normal vault use. Use BRAT or manual GitHub release assets only when testing unreleased builds from `slexkit/obsidian-slexkit`.
|
|
5150
6827
|
|
|
5151
|
-
The
|
|
6828
|
+
The community plugin is currently desktop-only and compatible with Obsidian 1.5.0+. Mobile support should be enabled only after real mobile vault testing.
|
|
6829
|
+
|
|
6830
|
+
The adapter uses trusted runtime mode - it renders content from the user's local vault and is not designed as a sandbox for third-party or agent-generated Markdown. Secure sandbox support is not part of the v0 adapter.
|
|
5152
6831
|
|
|
5153
6832
|
## @slexkit/mcp
|
|
5154
6833
|
|
|
@@ -5169,7 +6848,7 @@ The server does not modify project files. Use it when an agent needs current Sle
|
|
|
5169
6848
|
| With Svelte components | `npm install slexkit @slexkit/runtime @slexkit/components-svelte` |
|
|
5170
6849
|
| Add shadcn theme | `npm install @slexkit/theme-shadcn` |
|
|
5171
6850
|
| React/Streamdown host | `npm install slexkit @slexkit/theme-shadcn @slexkit/streamdown streamdown react react-dom` |
|
|
5172
|
-
| Obsidian plugin |
|
|
6851
|
+
| Obsidian plugin | Install **SlexKit** from Obsidian Community Plugins |
|
|
5173
6852
|
| AI agent MCP server | `npx -y @slexkit/mcp` |
|
|
5174
6853
|
|
|
5175
6854
|
## v0 packaging strategy
|
|
@@ -5186,7 +6865,7 @@ bun run test
|
|
|
5186
6865
|
bun run smoke:release
|
|
5187
6866
|
```
|
|
5188
6867
|
|
|
5189
|
-
The release smoke packs and installs every scoped package, verifies public entry points, verifies CSS subpath exports,
|
|
6868
|
+
The release smoke packs and installs every scoped package in this repository, verifies public entry points, verifies CSS subpath exports, and starts the MCP stdio binary to check `initialize`, `tools/list`, and `slexkitValidate`.
|
|
5190
6869
|
|
|
5191
6870
|
---
|
|
5192
6871
|
|
|
@@ -5822,7 +7501,7 @@ A generation should include a plain Markdown fallback so the output degrades gra
|
|
|
5822
7501
|
|
|
5823
7502
|
~~~~md
|
|
5824
7503
|
```slex
|
|
5825
|
-
{ namespace: "status",
|
|
7504
|
+
{ namespace: "status", layout: { "badge:state": { label: "3/4 complete", tone: "info" } } }
|
|
5826
7505
|
```
|
|
5827
7506
|
|
|
5828
7507
|
**Status:** 3/4 complete
|
|
@@ -5895,6 +7574,62 @@ slexkitRenderMode: component
|
|
|
5895
7574
|
|
|
5896
7575
|
All notable changes to SlexKit.
|
|
5897
7576
|
|
|
7577
|
+
## v0.3.1 - Host stability and control rendering hardening
|
|
7578
|
+
|
|
7579
|
+
### Added
|
|
7580
|
+
- Runtime style safety tests that block broad `:has()` selectors, `clip-path`, and slider track regressions in shipped CSS.
|
|
7581
|
+
- Regression coverage for disabled Switch, Checkbox, and Radio state attributes.
|
|
7582
|
+
|
|
7583
|
+
### Changed
|
|
7584
|
+
- CI now installs dependencies with `bun install --frozen-lockfile` and runs lint before tests.
|
|
7585
|
+
- Disabled Switch, Checkbox, and Radio styling now uses explicit `data-disabled` attributes instead of broad relational selectors.
|
|
7586
|
+
- Select and sr-only helper styles avoid `clip-path` for better host and Obsidian CSS compatibility.
|
|
7587
|
+
|
|
7588
|
+
### Fixed
|
|
7589
|
+
- Slider thumb rendering artifacts caused by painting the range track on the native input box.
|
|
7590
|
+
- Input focus visibility after removing custom engineering steppers.
|
|
7591
|
+
- Home RC example input labels now use native Input component labels instead of separate text labels.
|
|
7592
|
+
- Stat cards no longer clip updated text during cross-document state examples.
|
|
7593
|
+
- Markdown calculator examples no longer render duplicate section labels.
|
|
7594
|
+
|
|
7595
|
+
## v0.3.0 - Examples overhaul with component audit and i18n
|
|
7596
|
+
|
|
7597
|
+
### Added
|
|
7598
|
+
- Example gallery: 17 high-quality examples organized by usage scenario (Getting Started, Calculators, Data Browsing, Dashboards, Config Wizards, Decision Support, Platform Features)
|
|
7599
|
+
- English translations for all 17 example pages
|
|
7600
|
+
- `toolhost-demo`: real `renderToolCall` API with chat-style conversation UI
|
|
7601
|
+
- Example rendering infrastructure: `site/routes/examples.js`, `site/pages/examples.slex.js`, `site/data/examples.js`
|
|
7602
|
+
- Formula component (`src/components/svelte/content/Formula.svelte`) with KaTeX rendering
|
|
7603
|
+
- `src/engine/capabilities.ts`: structured capability docs for AI agents
|
|
7604
|
+
- `src/engine/validation.ts`: SPEC contract validation
|
|
7605
|
+
- `src/engine/stdlib.ts`: standard library with `math.clamp`, `math.safeDivide`, and other utilities
|
|
7606
|
+
- `src/engine/sandbox-runner.ts`: sandbox runner for secure runtime
|
|
7607
|
+
- Component state eval context shadowing test suite (`component-state-shadowing.test.ts`)
|
|
7608
|
+
- Collapsible and Callout double-rendering regression tests
|
|
7609
|
+
- Slider component name shadowing regression test
|
|
7610
|
+
|
|
7611
|
+
### Changed
|
|
7612
|
+
- Examples reduced from 64 to 17 high-quality examples, organized by user story
|
|
7613
|
+
- Example source locale: `zh-CN` (with `en-US` translations)
|
|
7614
|
+
- `renderChildren` (`helpers.ts`) now clears existing content when children are present
|
|
7615
|
+
- Switch component now accepts `checked`/`value` props for initialization consistency with Checkbox
|
|
7616
|
+
- Site UI: DocsShell, DocRail, router, shell improvements
|
|
7617
|
+
- Components: Input, Select, Tabs, Table, PlaygroundMarkdown refinements
|
|
7618
|
+
- CSS: theme-shadcn, text-input, docs-shell styling updates
|
|
7619
|
+
|
|
7620
|
+
### Fixed
|
|
7621
|
+
- Eval context shadowing: component names `g` and `api` no longer overwrite reserved context keys
|
|
7622
|
+
- `renderChildren` double rendering in Collapsible and Callout
|
|
7623
|
+
- Voltage divider summary typo ("输入输入电压")
|
|
7624
|
+
- Salary calculator fallback numbers to match actual calculator output
|
|
7625
|
+
- Tabs-and-branching: title and length conversion mismatch
|
|
7626
|
+
- 4 pre-existing test failures (ai-docs, page-structure, theme, markdown-content)
|
|
7627
|
+
|
|
7628
|
+
### Removed
|
|
7629
|
+
- 47 low-quality/duplicate examples (reduced from 64 to 17)
|
|
7630
|
+
- Dead "Fallback" copywriting from all example files
|
|
7631
|
+
- Unused `DialogShell.svelte` component
|
|
7632
|
+
|
|
5898
7633
|
## v0.2.0 - First public release
|
|
5899
7634
|
|
|
5900
7635
|
### Added
|
|
@@ -6182,6 +7917,26 @@ No child components.
|
|
|
6182
7917
|
|
|
6183
7918
|
---
|
|
6184
7919
|
|
|
7920
|
+
## Formula API (`formula`)
|
|
7921
|
+
|
|
7922
|
+
Category: Display
|
|
7923
|
+
Status: ready
|
|
7924
|
+
State mode: none
|
|
7925
|
+
Since: 0.1.0
|
|
7926
|
+
|
|
7927
|
+
### Props
|
|
7928
|
+
- `tex` (string; optional; dynamic): KaTeX source to render.
|
|
7929
|
+
- `formula` (string; optional; dynamic): Alias for tex.
|
|
7930
|
+
- `value` (string; optional; dynamic): Alias for tex.
|
|
7931
|
+
- `displayMode` (boolean; optional; static; default: true): Render as display math when true; inline math when false.
|
|
7932
|
+
- `display` (boolean; optional; static; default: true): Alias for displayMode.
|
|
7933
|
+
- `block` (boolean; optional; static; default: true): Alias for displayMode.
|
|
7934
|
+
|
|
7935
|
+
### Children
|
|
7936
|
+
No child components.
|
|
7937
|
+
|
|
7938
|
+
---
|
|
7939
|
+
|
|
6185
7940
|
## Grid API (`grid`)
|
|
6186
7941
|
|
|
6187
7942
|
Category: Layout
|
|
@@ -6251,7 +8006,6 @@ Since: 0.1.0
|
|
|
6251
8006
|
- `min` (string | number; optional; dynamic): Minimum value used by numeric input controls.
|
|
6252
8007
|
- `max` (string | number; optional; dynamic): Maximum value used by numeric input controls.
|
|
6253
8008
|
- `step` (string | number; optional; dynamic): Step size used by numeric input controls.
|
|
6254
|
-
- `controls` (boolean; optional; dynamic; default: true): Show decrement and increment buttons for numeric inputs.
|
|
6255
8009
|
- `onchange` (write-expression; optional; static): Write expression invoked when the value changes.
|
|
6256
8010
|
|
|
6257
8011
|
### Children
|
|
@@ -6551,6 +8305,8 @@ Since: 0.1.0
|
|
|
6551
8305
|
- `content` (string; optional; dynamic): Alias for text.
|
|
6552
8306
|
- `label` (string; optional; dynamic): Alias for text.
|
|
6553
8307
|
- `variant` (string; optional; static; default: "default"; values: default, muted): Text visual variant.
|
|
8308
|
+
- `color` (string; optional; dynamic): Optional CSS color for controlled previews.
|
|
8309
|
+
- `size` (string | number; optional; dynamic): Optional font size. Numbers are treated as px.
|
|
6554
8310
|
- `class` (string; optional; static): Additional host-controlled CSS class.
|
|
6555
8311
|
|
|
6556
8312
|
### Children
|