Files
cherry-studio/docs/technical/CodeBlockView-zh.md
one d05d1309ca refactor(Preview,CodeBlock): preview components and tools (#8565)
* refactor(CodeBlockView): generalize tool and preview

Generalize code tool to action tool
- CodeTool -> ActionTool
- usePreviewTools -> useImagePreview
- rename code tool classname from icon to tool-icon

Generalize preview
- move image preview components to Preview dir
- simplify file names

* refactor(useImageTools): simplify implementation, add pan

* refactor(Preview): move image tools to floating toolbar

* refactor: add enableDrag, enable zooming for SvgPreview

* test: add tests for preview components

* feat(Preview): add download buttons to dropdown

* refactor(Preview): remove setTools from preview, improve SvgPreview

* refactor: add useTemporaryValue

* test: add tests for hooks

* test: add tests for CodeToolButton and CodeToolbar

* refactor(PreviewTool): add a setting item to enable preview tools

* test: update snapshot

* refactor: extract more code tools to hooks, add tests

* refactor: extract tools from CodeEditor and CodeViewer

* test: add tests for new tool hooks

* refactor(CodeEditor): change collapsible to expanded, change wrappable to unwrapped

* refactor: migrate codePreview to codeViewer

* docs: CodeBlockView

* refactor: add custom file icons, center the reset button

* refactor: improve code quality

* refactor: improve migration by deprecating codePreview

* refactor: improve PlantUml and svgToCanvas

* fix: plantuml style

* test: fix tests

* fix: button icon

* refactor(SvgPreview): debounce rendering

* feat(PreviewTool): add a dialog tool

* fix: remove isValidPlantUML, improve plantuml rendering

* refactor: extract shadow dom renderer

* refactor: improve plantuml error messages

* test: add tests for ImageToolbar and ImageToolButton

* refactor: add ImagePreviewLayout to simplify layout and tests

* refactor: add useDebouncedRender, update docs

* chore: clean up unused props

* refactor: clean transformation before copy/download/preview

* refactor: update migrate version

* refactor: style refactoring and fixes

- show header background in split view
- fix status bar radius
- reset special view transformation on theme change
- fix wrap tool icon
- add a divider to split view
- improve split view toggling (switch back to previous view)
- revert copy tool to separate tools
- fix top border radius for special views

* refactor: move GraphvizPreview to shadow DOM

- use renderString
- keep renderSvgInShadowHost api consistent with others

* fix: tests, icons, deleted files

* refactor: use ResetIcon in ImageToolbar

* test: remove unnecessary tests

* fix: min height for special preview

* fix: update migrate
2025-08-06 20:09:49 +08:00

4.4 KiB
Raw Blame History

CodeBlockView 组件结构说明

概述

CodeBlockView 是 Cherry Studio 中用于显示和操作代码块的核心组件。它支持多种视图模式和特殊语言的可视化预览,提供丰富的交互工具。

组件结构

graph TD
    A[CodeBlockView] --> B[CodeToolbar]
    A --> C[SourceView]
    A --> D[SpecialView]
    A --> E[StatusBar]

    B --> F[CodeToolButton]

    C --> G[CodeEditor / CodeViewer]

    D --> H[MermaidPreview]
    D --> I[PlantUmlPreview]
    D --> J[SvgPreview]
    D --> K[GraphvizPreview]

    F --> L[useCopyTool]
    F --> M[useDownloadTool]
    F --> N[useViewSourceTool]
    F --> O[useSplitViewTool]
    F --> P[useRunTool]
    F --> Q[useExpandTool]
    F --> R[useWrapTool]
    F --> S[useSaveTool]

核心概念

视图类型

  • preview: 预览视图,非源代码的是特殊视图
  • edit: 编辑视图

视图模式

  • source: 源代码视图模式
  • special: 特殊视图模式Mermaid、PlantUML、SVG
  • split: 分屏模式(源代码和特殊视图并排显示)

特殊视图语言

  • mermaid
  • plantuml
  • svg
  • dot
  • graphviz

组件详细说明

CodeBlockView 主组件

主要负责:

  1. 管理视图模式状态
  2. 协调源代码视图和特殊视图的显示
  3. 管理工具栏工具
  4. 处理代码执行状态

子组件

CodeToolbar 工具栏

  • 显示在代码块右上角的工具栏
  • 包含核心(core)和快捷(quick)两类工具
  • 根据上下文动态显示相关工具

CodeEditor/CodeViewer 源代码视图

  • 可编辑的代码编辑器或只读的代码查看器
  • 根据设置决定使用哪个组件
  • 支持多种编程语言高亮

特殊视图组件

  • MermaidPreview: Mermaid 图表预览
  • PlantUmlPreview: PlantUML 图表预览
  • SvgPreview: SVG 图像预览
  • GraphvizPreview: Graphviz 图表预览

所有特殊视图组件共享通用架构,以确保一致的用户体验和功能。有关这些组件及其实现的详细信息,请参阅 图像预览组件文档

StatusBar 状态栏

  • 显示 Python 代码执行结果
  • 可显示文本和图像结果

工具系统

CodeBlockView 使用基于 hooks 的工具系统:

graph TD
    A[CodeBlockView] --> B[useCopyTool]
    A --> C[useDownloadTool]
    A --> D[useViewSourceTool]
    A --> E[useSplitViewTool]
    A --> F[useRunTool]
    A --> G[useExpandTool]
    A --> H[useWrapTool]
    A --> I[useSaveTool]

    B --> J[ToolManager]
    C --> J
    D --> J
    E --> J
    F --> J
    G --> J
    H --> J
    I --> J

    J --> K[CodeToolbar]

每个工具 hook 负责注册特定功能的工具按钮到工具管理器,工具管理器再将这些工具传递给 CodeToolbar 组件进行渲染。

工具类型

  • core: 核心工具,始终显示在工具栏
  • quick: 快捷工具当数量大于1时通过下拉菜单显示

工具列表

  1. 复制(copy): 复制代码或图像
  2. 下载(download): 下载代码或图像
  3. 查看源码(view-source): 在特殊视图和源码视图间切换
  4. 分屏(split-view): 切换分屏模式
  5. 运行(run): 运行 Python 代码
  6. 展开/折叠(expand): 控制代码块的展开/折叠
  7. 换行(wrap): 控制代码的自动换行
  8. 保存(save): 保存编辑的代码

状态管理

CodeBlockView 通过 React hooks 管理以下状态:

  1. viewMode: 当前视图模式 ('source' | 'special' | 'split')
  2. isRunning: Python 代码执行状态
  3. executionResult: Python 代码执行结果
  4. tools: 工具栏工具列表
  5. expandOverride/unwrapOverride: 用户展开/换行的覆盖设置
  6. sourceScrollHeight: 源代码视图滚动高度

交互流程

sequenceDiagram
    participant U as User
    participant CB as CodeBlockView
    participant CT as CodeToolbar
    participant SV as SpecialView
    participant SE as SourceEditor

    U->>CB: 查看代码块
    CB->>CB: 初始化状态
    CB->>CT: 注册工具
    CB->>SV: 渲染特殊视图(如果适用)
    CB->>SE: 渲染源码视图
    U->>CT: 点击工具按钮
    CT->>CB: 触发工具回调
    CB->>CB: 更新状态
    CB->>CT: 重新注册工具(如果需要)

特殊处理

HTML 代码块

HTML 代码块会被特殊处理,使用 HtmlArtifactsCard 组件显示。

Python 代码执行

支持执行 Python 代码并显示结果,使用 Pyodide 在浏览器中运行 Python 代码。