# 视图

> 插件用 JSON 描述侧边栏内容，atomd 用自己的样式绘制：可用的元素、按钮与输入框的交互、图标列表。

- 网页：https://dinox.info/guides/atomd-plugins/views
- 本页 Markdown：https://dinox.info/guides/atomd-plugins/views.md
- 全部文档索引：https://dinox.info/llms.txt

插件不直接画界面，而是把「要显示什么」作为数据发给 atomd：

```ts
host.post("atomd.ui.render", {
  view: "sidebar",
  content: {
    actions: [{ id: "new-chat", icon: "rotate", label: "New chat" }],
    body: [ /* 元素，可滚动 */ ],
    footer: [ /* 元素，固定在底部 */ ],
    follow: true,
  },
});
```

| 字段        | 说明                             |
| --------- | ------------------------------ |
| `actions` | 侧边栏标题栏右侧的图标按钮，按下时发送 `press` 事件 |
| `body`    | 主体内容，超出时滚动                     |
| `footer`  | 固定在底部的内容，通常放输入框                |
| `follow`  | 为 `true` 时，每次渲染后滚动到底部，适合聊天类插件  |

每次 `render` 都会**整体替换**视图内容。状态由插件自己保存，变化后重新渲染整份内容即可，不用担心性能。

## 元素

元素是带 `type` 的对象。atomd 不认识的 `type` 会被跳过，不会报错。带 `id` 的元素在事件里报告这个 `id`，同时获得测试 ID `<type>-<id>`。

| `type`         | 字段                                                                                                                             | 说明                                   |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------ |
| `column`、`row` | `children`、`gap`；`row` 还有 `justify`（`start`/`center`/`end`/`between`）                                                          | 纵向、横向排列                              |
| `card`         | `children`                                                                                                                     | 带边框的卡片                               |
| `bubble`       | `children`                                                                                                                     | 靠右的消息气泡，适合显示用户说的话                    |
| `composer`     | `children`                                                                                                                     | 输入区域的外框                              |
| `text`         | `text`、`tone`（`muted`/`faint`/`danger`/`accent`）、`size`（`xs`/`sm`/`lg`）、`weight`（`medium`/`semibold`）、`mono`、`strike`、`center` | 文字                                   |
| `icon`         | `name`、`size`、`tone`                                                                                                           | 图标                                   |
| `chip`         | `text`、`icon`                                                                                                                  | 小标签                                  |
| `status`       | `text`                                                                                                                         | 一行带动画的进行中状态，例如「正在思考…」                |
| `empty`        | `icon`、`title`、`text`                                                                                                          | 空状态                                  |
| `diff`         | `before`、`after`                                                                                                               | 修改前后对比，配合 `atomd.note.applyEdits` 使用 |
| `divider`      |                                                                                                                                | 分隔线                                  |
| `button`       | `id`、`label`、`variant`（`primary`）、`submits`                                                                                    | 按钮                                   |
| `icon_button`  | `id`、`icon`、`label`、`variant`（`round`）                                                                                         | 图标按钮，`label` 用于无障碍和提示                |
| `input`        | `id`、`placeholder`、`multiline`、`autofocus`                                                                                     | 输入框                                  |

## 按钮与输入框

* 按下 `button` 或 `icon_button`，插件收到 `{ action: "press", id }`。
* 在 `input` 里按回车，插件收到 `{ action: "submit", id, value }`，atomd 同时清空输入框。
* 按钮带上 `"submits": "<input id>"` 时，按下它等同于在那个输入框里按回车，适合「发送」按钮。
* 用户已经输入但还没提交的文字，在重新渲染后会保留。

一个最小的输入框：

```ts
footer: [{
  type: "composer",
  children: [
    { type: "input", id: "ask", placeholder: "Ask about this note", multiline: true },
    { type: "row", justify: "end", children: [
      { type: "icon_button", id: "send", icon: "arrow-up", label: "Send", variant: "round", submits: "ask" },
    ]},
  ],
}]
```

## 图标

`sparkles`、`rotate`、`x`、`arrow-up`、`stop`、`note`、`check`、`plus`、`search`、`copy`、`trash`、`link`、`list`、`todo`、`settings`、`folder`。

其他名称会显示成拼图图标。

## 以后会有什么

API 1 的视图只支持数据描述的侧边栏。由插件自己用 TSX 绘制的完整面板正在规划中，需要 NSH 支持把插件视图嵌进宿主窗口后才会推出。