# DiffViewer

> 阅读统一 diff 的文件元信息、双侧行号与可折叠 hunk。

网页来源：/components/diff-viewer

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

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

`QuireDiffViewer` 接收 `patch` 原始统一 diff 文本。组件只负责只读差异浏览：验证 hunk 的旧/新行数、呈现双侧行号、折叠片段和展开长片段中间行。它不生成、应用或撤销 patch，也不替业务决定文件修订。

## 差异阅读

元信息与 hunk 保留原始顺序。增删行使用轻量语义底色，行号区宽度随片段内最大行号自适应；片段标题可折叠，长片段优先显示变更附近的行，再按需展开隐藏上下文。切换“原文”时复用 CodeBlock；复制始终得到完整、未经格式化的 `patch`。

### 示例：diff-viewer/basic

文件：`basic.vue`

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

const patch = [
  'diff --git a/packages/ui/src/vue/chat/message.vue b/packages/ui/src/vue/chat/message.vue',
  '--- a/packages/ui/src/vue/chat/message.vue',
  '+++ b/packages/ui/src/vue/chat/message.vue',
  '@@ -12,7 +12,7 @@',
  ' const props = defineProps<Props>();',
  ' const latest = computed(() => props.latest);',
  '-const actionsVisible = true;',
  '+const actionsVisible = computed(() => latest.value || hovered.value);',
  ' const copied = ref(false);',
  ' const hovered = ref(false);',
  ' const timestamp = computed(() => props.time);',
  ' const canCopy = computed(() => props.content.length > 0);',
  '@@ -1242,3 +1243,4 @@',
  ' function copyMessage() {',
  '-  return copy(props.content);',
  '+  return copy(props.source);',
  '+  // Copy the original message source.',
  ' }'
].join('\n') + '\n';
</script>

<template>
  <div class="min-w-0 p-qk-surface" data-diff-viewer-basic>
    <QuireDiffViewer :patch="patch" label="Message 组件改动" :max-visible-lines="4" :max-height="300" />
  </div>
</template>
```

同一组件也能直接接到 Tool 的 `output` slot。工具状态和 patch 来源仍由宿主负责。

### 示例：diff-viewer/tool

文件：`tool.vue`

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

const patch = '--- a/plan.md\n+++ b/plan.md\n@@ -1,2 +1,2 @@\n # 下一步\n-确认范围\n+确认范围与验收条件\n';
const part: QuireKitChatToolPart = {
  id: 'propose-change', kind: 'tool', title: '提出文档改动', state: 'succeeded', output: patch
};
</script>

<template>
  <div class="min-w-0 p-qk-surface" data-diff-viewer-tool>
    <QuireChatTool :part="part" :default-open="true">
      <template #output>
        <QuireDiffViewer :patch="patch" label="计划改动" :max-height="220" />
      </template>
    </QuireChatTool>
  </div>
</template>
```

## 原文回退

不能通过 hunk 计数验证的文本，或超过 `maxStructuredCharacters`（默认 150,000 字符）的输入，不显示可能错误的行号，而是保留可复制的原文预览。`maxVisibleLines` 和 `maxHeight` 只影响阅读，不影响复制。`view` / `defaultView` 可选择初始或受控模式。并排 diff、文件写入和审阅决策不属于当前合同。视图切换、片段与行号说明等文案跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言。

### 示例：diff-viewer/fallback

文件：`fallback.vue`

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

const patch = '@@ -1,2 +1,2 @@\n-before\n+after\n';
</script>

<template>
  <div class="min-w-0 p-qk-surface" data-diff-viewer-fallback>
    <QuireDiffViewer :patch="patch" label="未完成的差异" />
  </div>
</template>
```

## API

### QuireDiffViewer

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| patch | 是 | — | Original unified diff, including metadata and trailing whitespace. |
| label | 否 | — | Accessible name for the viewer; defaults to the language pack's generic name. |
| view | 否 | — | Controlled view. Invalid or oversized patches always use source. |
| defaultView | 否 | "diff" | Initial view when view is uncontrolled. |
| maxStructuredCharacters | 否 | 150000 | Maximum source characters admitted to structured parsing. |
| maxVisibleLines | 否 | 120 | Initial number of lines shown per hunk; hidden lines can be revealed. |
| maxHeight | 否 | 480 | Height of the scrollable reading area in CSS pixels. |

**patch**

```ts
string
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**view**

```ts
QuireKitDiffViewerView | undefined
```

```ts
undefined | "diff" | "source"
```

**defaultView**

```ts
QuireKitDiffViewerView | undefined
```

```ts
undefined | "diff" | "source"
```

**maxStructuredCharacters**

```ts
number | undefined
```

```ts
undefined | number
```

**maxVisibleLines**

```ts
number | undefined
```

```ts
undefined | number
```

**maxHeight**

```ts
number | undefined
```

```ts
undefined | number
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:view | 否 | — |  |

**update:view**

```ts
[view: QuireKitDiffViewerView]
```

#### Slots

无。

#### 公开实例成员

无。
