# Comment

> 在文档页边呈现评论会话、新建评论，并让卡片贴近所评论的原文。

网页来源：/components/comment

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

CommentThread 呈现一条评论会话，CommentDraft 用于新建评论，CommentRail 把卡片排列在所评论原文的高度上。三者只负责呈现与交互，评论数据、权限和保存由使用方提供。

## 评论会话

`messages` 的第一条是发起评论，其余是回复；`quote` 是被评论的原文。`active` 时展开全部回复并显示回复框；`resolvable`、`replyable` 决定是否提供解决与回复。可编辑、可删除由每条消息的 `editable`、`deletable` 决定，删除第一条即删除整条评论。传入 `position`（`{ index, total }`）时，选中的卡片在标题栏显示上一条 / 下一条按钮和“2 / 5”这样的位置，分别触发 `previous`、`next`；只有一条时不显示。⌘/Ctrl + Enter 发送，Esc 取消。

### 示例：comment/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { QuireCommentThread, type QuireKitCommentMessage } from '@quirekit/ui/vue';

const me = { name: '林舟' };
const messages = shallowRef<QuireKitCommentMessage[]>([
  { id: 'm1', author: { name: '陈思' }, time: '2026-10-15T09:20:00Z', body: '这里的发布时间和公告不一致，需要确认。', editable: false, deletable: false },
  { id: 'm2', author: me, time: '2026-10-15T09:32:00Z', body: '已与运营核对，以公告为准。', editable: true, deletable: true },
]);
const active = shallowRef(true), resolved = shallowRef(false);

/** @param body - Submitted reply. */
function reply(body: string): void {
  messages.value = [...messages.value, { id: `m${messages.value.length + 1}`, author: me, time: new Date().toISOString(), body, editable: true, deletable: true }];
}
/** @param id - Edited message. @param body - New text. */
function edit(id: string, body: string): void {
  messages.value = messages.value.map(message => message.id === id ? { ...message, body, edited: true } : message);
}
/** @param id - Deleted message. */
function remove(id: string): void { messages.value = messages.value.filter(message => message.id !== id); }
</script>
<template>
  <div class="grid max-w-80 p-qk-surface" data-comment-basic>
    <QuireCommentThread :messages="messages" quote="新版本将于 10 月 20 日发布" :active="active" :resolved="resolved" resolvable replyable
      @activate="active = true" @resolve="resolved = true" @reopen="resolved = false" @reply="reply" @edit="edit" @delete="remove" />
  </div>
</template>
```

## 状态

未激活时只显示第一条和回复数量。`resolved` 使用浅底并标记已解决。`quote-state` 区分位置评论（`point`）、原文已删除（`deleted`）与找不到原文（`unavailable`）。

### 示例：comment/states

文件：`states.vue`

```vue
<script setup lang="ts">
import { QuireCommentThread, type QuireKitCommentMessage } from '@quirekit/ui/vue';

const root: QuireKitCommentMessage = { id: 'r', author: { name: 'Ada Lovelace' }, time: '2026-09-02T08:00:00Z', body: '请补充数据来源。' };
const replies: QuireKitCommentMessage[] = [root,
  { id: 'a', author: { name: '陈思' }, time: '2026-09-02T09:00:00Z', body: '已补充到附录。' },
  { id: 'b', author: { name: '林舟' }, time: '2026-09-02T10:00:00Z', body: '收到。' }];
</script>
<template>
  <div class="grid max-w-80 gap-qk-surface p-qk-surface" data-comment-states>
    <QuireCommentThread :messages="replies" quote="第三季度的增长主要来自海外市场" resolvable />
    <QuireCommentThread :messages="[root]" quote="第三季度的增长主要来自海外市场" resolved resolvable />
    <QuireCommentThread :messages="[root]" quote-state="point" />
    <QuireCommentThread :messages="[root]" quote-state="deleted" />
    <QuireCommentThread :messages="[root]" quote-state="unavailable" />
  </div>
</template>
```

## 由宿主保存草稿与编辑

绑定 `v-model:reply-draft` 与 `v-model:reply-open` 后，回复草稿由宿主保存，切换线程再回来仍能恢复；发送后由宿主清空。绑定 `editing-id` 并提供 `#editor` 插槽可换成自己的编辑器，例如富文本编辑；保存和取消都通过修改 `editing-id` 完成。`quote-label` 改写无原文时的标签，以匹配产品用词。

`actions` 插槽把宿主自己的按钮放进卡片头部，排在“解决”之前。用 `QuireCommentAction` 传入 `label` 和 `icon`，样式与内置按钮一致，并自带 Tooltip。

### 示例：comment/controlled

文件：`controlled.vue`

```vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { QuireButton, QuireCommentThread, QuireTextarea, type QuireKitCommentMessage } from '@quirekit/ui/vue';

const messages = shallowRef<QuireKitCommentMessage[]>([
  { id: 'm1', author: { name: '陈思' }, time: '2026-10-15T09:20:00Z', body: '标题建议改短一些。', editable: true },
]);
// The host keeps the reply draft and the editing state, e.g. to restore drafts after switching threads.
const replyDraft = shallowRef(''), replyOpen = shallowRef(false), editingId = shallowRef<string | null>(null), edited = shallowRef('');

/** @param id - Message the card asked to edit, or null to stop. */
function edit(id: string | null): void {
  editingId.value = id;
  edited.value = messages.value.find(message => message.id === id)?.body ?? '';
}
/** Saves the host editor's text. */
function save(): void {
  messages.value = messages.value.map(message => message.id === editingId.value ? { ...message, body: edited.value, edited: true } : message);
  editingId.value = null;
}
</script>
<template>
  <div class="grid max-w-80 gap-qk-inline p-qk-surface" data-comment-controlled>
    <QuireCommentThread :messages="messages" quote-state="point" quote-label="位置批注" active replyable
      :editing-id="editingId" v-model:reply-draft="replyDraft" v-model:reply-open="replyOpen" @update:editing-id="edit"
      @reply="replyDraft = ''; replyOpen = false">
      <template #editor>
        <QuireTextarea v-model="edited" size="sm" auto-resize :rows="2" aria-label="编辑批注" />
        <div class="flex justify-end gap-qk-inline-tight">
          <QuireButton variant="ghost" size="xs" @click="editingId = null">取消</QuireButton>
          <QuireButton color="primary" size="xs" @click="save">保存</QuireButton>
        </div>
      </template>
    </QuireCommentThread>
    <p class="m-0 text-qk-helper text-qk-text-secondary">宿主保存的草稿：{{ replyDraft || '（空）' }}</p>
  </div>
</template>
```

## 新建评论

CommentDraft 打开时聚焦输入框，`submit` 返回正文，`cancel` 放弃。

### 示例：comment/draft

文件：`draft.vue`

```vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { QuireCommentDraft } from '@quirekit/ui/vue';

const submitted = shallowRef('');
</script>
<template>
  <div class="grid max-w-80 gap-qk-inline p-qk-surface" data-comment-draft>
    <QuireCommentDraft :author="{ name: '林舟' }" quote="把发布窗口调整到周二上午" @submit="submitted = $event" @cancel="submitted = ''" />
    <p v-if="submitted" class="m-0 text-qk-helper text-qk-text-secondary">已提交：{{ submitted }}</p>
  </div>
</template>
```

## 页边排列

`items` 按文档顺序给出每张卡片锚点的高度。卡片落在锚点处；与上一张重叠时向下推开，间距由 `gap` 设置。`height` 通常传文档高度，卡片超出时栏高随之增加。

### 示例：comment/rail

文件：`rail.vue`

```vue
<script setup lang="ts">
import { shallowRef } from 'vue';
import { QuireCommentRail, QuireCommentThread } from '@quirekit/ui/vue';

const items = [{ id: 'a', anchorTop: 16 }, { id: 'b', anchorTop: 40 }, { id: 'c', anchorTop: 260 }];
const threads: Record<string, { quote: string; body: string }> = {
  a: { quote: '目标用户', body: '是否包含企业管理员？' },
  b: { quote: '核心场景', body: '建议补一个离线场景。' },
  c: { quote: '风险', body: '依赖的接口还没有排期。' },
};
const active = shallowRef('a');
</script>
<template>
  <div class="grid grid-cols-[1fr_240px] gap-qk-group p-qk-surface" data-comment-rail>
    <div class="grid content-start gap-qk-surface text-qk-body text-qk-text-primary">
      <p class="m-0">目标用户：中小团队的项目负责人。</p>
      <p class="m-0">核心场景：在一份文档里完成需求、讨论与决策。</p>
      <p class="m-0 text-qk-text-secondary">相邻的两条评论锚点很近，第二张卡片被推到第一张下方；第三条保持在自己的位置。</p>
      <p class="m-0 mt-24">风险：依赖的接口还没有排期。</p>
    </div>
    <QuireCommentRail :items="items" :height="320">
      <template #item="{ item }">
        <QuireCommentThread :messages="[{ id: item.id, author: { name: '陈思' }, time: '2026-10-15T09:00:00Z', body: threads[item.id]!.body }]"
          :quote="threads[item.id]!.quote" :active="active === item.id" @activate="active = item.id" />
      </template>
    </QuireCommentRail>
  </div>
</template>
```

## API

### QuireCommentThread

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| messages | 是 | — | Messages in order; the first is the root. |
| quote | 否 | — | Commented text. |
| quoteState | 否 | "text" | \`point\` for a position without text, \`deleted\` / \`unavailable\` when the text is gone, \`none\` to show no quote line. Defaults to \`text\`. |
| variant | 否 | "card" | \`plain\` drops the card frame\: a flat row that highlights on hover. Defaults to \`card\`. |
| quoteLabel | 否 | — | Replaces the built-in label shown for \`point\`, \`deleted\` and \`unavailable\`, e.g. to match product wording. |
| resolved | 否 | false | Whether the discussion is closed. |
| active | 否 | false | Whether this thread is focused; expands replies and the reply field. |
| resolvable | 否 | false | Whether to offer resolve / reopen. |
| replyable | 否 | false | Whether to offer the reply field. |
| busy | 否 | false | Disables actions while a change is being saved. |
| mentions | 否 | — | People offered after typing @ in the reply and edit fields. |
| position | 否 | — | 1-based place among the threads a reader steps through; the active card then shows previous / next buttons. |
| editingId | 否 | null | Message being edited; bind it to own editing, otherwise the card keeps it locally. |
| replyDraft | 否 | "" | Unsent reply; bind it to keep drafts outside the card. |
| replyOpen | 否 | false | Whether the reply field is open; bind it together with \`replyDraft\`. |

**messages**

```ts
readonly QuireKitCommentMessage[]
```

```ts
readonly ({
  /** Stable identity within the thread. */
  id: string;
  /** Message author. */
  author: {
    /** Display name; also the source of the avatar initials. */
    name: string;
    /** Optional avatar image URL. */
    avatar?: undefined | string;
    /** Short label shown after the name, such as "AI" for an assistant. */
    badge?: string | undefined;
  };
  /** ISO 8601 time, shown compactly with the full value as a tooltip. */
  time: string;
  /** Message text; render formatted content through the `body` slot. */
  body: string;
  /** Whether the message was edited after posting. */
  edited?: boolean | undefined;
  /** Whether the current user may edit this message. */
  editable?: boolean | undefined;
  /** Whether the current user may delete this message; deleting the root deletes the thread. */
  deletable?: boolean | undefined;
  /** Mentions in `body`, kept when the message is edited. */
  mentions?: undefined | readonly ({
    /** Stable host identity. */
    id: string;
    /** Display name without the leading */
    label: string;
    /** Destination written in the link. */
    url: string;
    /** Secondary text in the suggestion list. */
    description?: string | undefined;
  })[];
})[]
```

**quote**

```ts
string | undefined
```

```ts
undefined | string
```

**quoteState**

```ts
QuireKitCommentQuoteState | undefined
```

```ts
undefined | "text" | "point" | "deleted" | "unavailable" | "none"
```

**variant**

```ts
QuireKitCommentVariant | undefined
```

```ts
undefined | "card" | "plain"
```

**quoteLabel**

```ts
string | undefined
```

```ts
undefined | string
```

**resolved**

```ts
boolean | undefined
```

**active**

```ts
boolean | undefined
```

**resolvable**

```ts
boolean | undefined
```

**replyable**

```ts
boolean | undefined
```

**busy**

```ts
boolean | undefined
```

**mentions**

```ts
QuireKitCommentMentionSource | undefined
```

```ts
undefined | readonly ({
  /** Stable host identity. */
  id: string;
  /** Display name without the leading */
  label: string;
  /** Destination written in the link. */
  url: string;
  /** Secondary text in the suggestion list. */
  description?: undefined | string;
})[] | { (query: string): readonly QuireKitCommentMention[] | Promise<readonly QuireKitCommentMention[]>; }
```

**position**

```ts
QuireKitCommentPosition | undefined
```

```ts
undefined | {
  /** 1-based index of this thread. */
  index: number;
  /** Number of threads in the sequence. */
  total: number;
}
```

**editingId**

```ts
string | null | undefined
```

```ts
undefined | null | string
```

**replyDraft**

```ts
string | undefined
```

```ts
undefined | string
```

**replyOpen**

```ts
boolean | undefined
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:editingId | 否 | — |  |
| update\:replyDraft | 否 | — |  |
| update\:replyOpen | 否 | — |  |
| activate | 否 | — |  |
| resolve | 否 | — |  |
| reopen | 否 | — |  |
| reply | 否 | — |  |
| edit | 否 | — |  |
| delete | 否 | — |  |
| previous | 否 | — |  |
| next | 否 | — |  |

**update:editingId**

```ts
[value: string | null]
```

**update:replyDraft**

```ts
[value: string]
```

**update:replyOpen**

```ts
[value: boolean]
```

**activate**

```ts
[]
```

**resolve**

```ts
[]
```

**reopen**

```ts
[]
```

**reply**

```ts
[body: string, mentions: readonly QuireKitCommentMention[]]
```

**edit**

```ts
[messageId: string, body: string, mentions: readonly QuireKitCommentMention[]]
```

**delete**

```ts
[messageId: string]
```

**previous**

```ts
[]
```

**next**

```ts
[]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| body | 否 | — | Formatted message content; defaults to plain text with preserved line breaks. |
| editor | 否 | — | Replaces the built-in plain-text editor, e.g. with a rich editor; the host saves and cancels through \`editingId\`. |
| actions | 否 | — | Host controls placed before resolve in the card header, usually small icon buttons. |

**body**

```ts
{ readonly message: QuireKitCommentMessage; }
```

```ts
{
  message: {
    /** Stable identity within the thread. */
    id: string;
    /** Message author. */
    author: {
      /** Display name; also the source of the avatar initials. */
      name: string;
      /** Optional avatar image URL. */
      avatar?: string | undefined;
      /** Short label shown after the name, such as "AI" for an assistant. */
      badge?: string | undefined;
    };
    /** ISO 8601 time, shown compactly with the full value as a tooltip. */
    time: string;
    /** Message text; render formatted content through the `body` slot. */
    body: string;
    /** Whether the message was edited after posting. */
    edited?: boolean | undefined;
    /** Whether the current user may edit this message. */
    editable?: boolean | undefined;
    /** Whether the current user may delete this message; deleting the root deletes the thread. */
    deletable?: boolean | undefined;
    /** Mentions in `body`, kept when the message is edited. */
    mentions?: undefined | readonly ({
      /** Stable host identity. */
      id: string;
      /** Display name without the leading */
      label: string;
      /** Destination written in the link. */
      url: string;
      /** Secondary text in the suggestion list. */
      description?: string | undefined;
    })[];
  };
}
```

**editor**

```ts
{ readonly message: QuireKitCommentMessage; }
```

```ts
{
  message: {
    /** Stable identity within the thread. */
    id: string;
    /** Message author. */
    author: {
      /** Display name; also the source of the avatar initials. */
      name: string;
      /** Optional avatar image URL. */
      avatar?: string | undefined;
      /** Short label shown after the name, such as "AI" for an assistant. */
      badge?: string | undefined;
    };
    /** ISO 8601 time, shown compactly with the full value as a tooltip. */
    time: string;
    /** Message text; render formatted content through the `body` slot. */
    body: string;
    /** Whether the message was edited after posting. */
    edited?: boolean | undefined;
    /** Whether the current user may edit this message. */
    editable?: boolean | undefined;
    /** Whether the current user may delete this message; deleting the root deletes the thread. */
    deletable?: boolean | undefined;
    /** Mentions in `body`, kept when the message is edited. */
    mentions?: undefined | readonly ({
      /** Stable host identity. */
      id: string;
      /** Display name without the leading */
      label: string;
      /** Destination written in the link. */
      url: string;
      /** Secondary text in the suggestion list. */
      description?: string | undefined;
    })[];
  };
}
```

**actions**

```ts
any
```

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitCommentMessage = {
  /** Stable identity within the thread. */
  id: string;
  /** Message author. */
  author: {
    /** Display name; also the source of the avatar initials. */
    name: string;
    /** Optional avatar image URL. */
    avatar?: string | undefined;
    /** Short label shown after the name, such as "AI" for an assistant. */
    badge?: string | undefined;
  };
  /** ISO 8601 time, shown compactly with the full value as a tooltip. */
  time: string;
  /** Message text; render formatted content through the `body` slot. */
  body: string;
  /** Whether the message was edited after posting. */
  edited?: boolean | undefined;
  /** Whether the current user may edit this message. */
  editable?: boolean | undefined;
  /** Whether the current user may delete this message; deleting the root deletes the thread. */
  deletable?: boolean | undefined;
  /** Mentions in `body`, kept when the message is edited. */
  mentions?: undefined | readonly ({
    /** Stable host identity. */
    id: string;
    /** Display name without the leading */
    label: string;
    /** Destination written in the link. */
    url: string;
    /** Secondary text in the suggestion list. */
    description?: string | undefined;
  })[];
};
```

```ts
type QuireKitCommentAuthor = {
  /** Display name; also the source of the avatar initials. */
  name: string;
  /** Optional avatar image URL. */
  avatar?: undefined | string;
  /** Short label shown after the name, such as "AI" for an assistant. */
  badge?: string | undefined;
};
```

```ts
type QuireKitCommentMention = {
  /** Stable host identity. */
  id: string;
  /** Display name without the leading */
  label: string;
  /** Destination written in the link. */
  url: string;
  /** Secondary text in the suggestion list. */
  description?: string | undefined;
};
```

```ts
type QuireKitCommentPosition = {
  /** 1-based index of this thread. */
  index: number;
  /** Number of threads in the sequence. */
  total: number;
};
```

### QuireCommentDraft

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| quote | 否 | — | Text the new comment will refer to. |
| author | 否 | — | Author shown on the draft. |
| busy | 否 | false | Disables submission while saving. |
| variant | 否 | "card" | \`plain\` is a bare input that stays in place, e.g. under a list of comments; actions appear once text is typed. Defaults to \`card\`. |
| placeholder | 否 | — | Replaces the built-in placeholder. |
| autofocus | 否 | true | Focuses the field when the draft mounts. Defaults to true. |
| mentions | 否 | — | People offered after typing |

**quote**

```ts
string | undefined
```

```ts
undefined | string
```

**author**

```ts
QuireKitCommentAuthor | undefined
```

```ts
undefined | {
  /** Display name; also the source of the avatar initials. */
  name: string;
  /** Optional avatar image URL. */
  avatar?: undefined | string;
  /** Short label shown after the name, such as "AI" for an assistant. */
  badge?: string | undefined;
}
```

**busy**

```ts
boolean | undefined
```

**variant**

```ts
QuireKitCommentVariant | undefined
```

```ts
undefined | "card" | "plain"
```

**placeholder**

```ts
string | undefined
```

```ts
undefined | string
```

**autofocus**

```ts
boolean | undefined
```

**mentions**

```ts
QuireKitCommentMentionSource | undefined
```

```ts
undefined | readonly ({
  /** Stable host identity. */
  id: string;
  /** Display name without the leading */
  label: string;
  /** Destination written in the link. */
  url: string;
  /** Secondary text in the suggestion list. */
  description?: undefined | string;
})[] | { (query: string): readonly QuireKitCommentMention[] | Promise<readonly QuireKitCommentMention[]>; }
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| submit | 否 | — |  |
| cancel | 否 | — |  |

**submit**

```ts
[body: string, mentions: readonly QuireKitCommentMention[]]
```

**cancel**

```ts
[]
```

#### Slots

无。

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitCommentAuthor = {
  /** Display name; also the source of the avatar initials. */
  name: string;
  /** Optional avatar image URL. */
  avatar?: undefined | string;
  /** Short label shown after the name, such as "AI" for an assistant. */
  badge?: string | undefined;
};
```

```ts
type QuireKitCommentMention = {
  /** Stable host identity. */
  id: string;
  /** Display name without the leading */
  label: string;
  /** Destination written in the link. */
  url: string;
  /** Secondary text in the suggestion list. */
  description?: undefined | string;
};
```

### QuireCommentRail

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| items | 是 | — | Cards in document order. |
| height | 否 | 0 | Minimum rail height, usually the document height. |
| gap | 否 | 12 | Vertical gap between pushed cards. Defaults to 12. |

**items**

```ts
readonly QuireKitCommentRailItem[]
```

```ts
readonly ({
  /** Stable identity, also the slot key. */
  id: string;
  /** Anchor top relative to the rail origin, in CSS pixels. */
  anchorTop: number;
})[]
```

**height**

```ts
number | undefined
```

```ts
undefined | number
```

**gap**

```ts
number | undefined
```

```ts
undefined | number
```

#### Events

无。

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| item | 否 | — | One card; positioned by the rail at its anchor or just below the previous card. |

**item**

```ts
{ readonly item: QuireKitCommentRailItem; }
```

```ts
{
  item: {
    /** Stable identity, also the slot key. */
    id: string;
    /** Anchor top relative to the rail origin, in CSS pixels. */
    anchorTop: number;
  };
}
```

#### 公开实例成员

无。

#### 相关类型

```ts
type QuireKitCommentRailItem = {
  /** Stable identity, also the slot key. */
  id: string;
  /** Anchor top relative to the rail origin, in CSS pixels. */
  anchorTop: number;
};
```
