* 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
4.6 KiB
CodeBlockView Component Structure
Overview
CodeBlockView is the core component in Cherry Studio for displaying and manipulating code blocks. It supports multiple view modes and visual previews for special languages, providing rich interactive tools.
Component Structure
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]
Core Concepts
View Types
- preview: Preview view, where non-source code is displayed as special views
- edit: Edit view
View Modes
- source: Source code view mode
- special: Special view mode (Mermaid, PlantUML, SVG)
- split: Split view mode (source code and special view displayed side by side)
Special View Languages
- mermaid
- plantuml
- svg
- dot
- graphviz
Component Details
CodeBlockView Main Component
Main responsibilities:
- Managing view mode state
- Coordinating the display of source code view and special view
- Managing toolbar tools
- Handling code execution state
Subcomponents
CodeToolbar
- Toolbar displayed at the top-right corner of the code block
- Contains core and quick tools
- Dynamically displays relevant tools based on context
CodeEditor/CodeViewer Source View
- Editable code editor or read-only code viewer
- Uses either component based on settings
- Supports syntax highlighting for multiple programming languages
Special View Components
- MermaidPreview: Mermaid diagram preview
- PlantUmlPreview: PlantUML diagram preview
- SvgPreview: SVG image preview
- GraphvizPreview: Graphviz diagram preview
All special view components share a common architecture for consistent user experience and functionality. For detailed information about these components and their implementation, see Image Preview Components Documentation.
StatusBar
- Displays Python code execution results
- Can show both text and image results
Tool System
CodeBlockView uses a hook-based tool system:
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]
Each tool hook is responsible for registering specific function tool buttons to the tool manager, which then passes these tools to the CodeToolbar component for rendering.
Tool Types
- core: Core tools, always displayed in the toolbar
- quick: Quick tools, displayed in a dropdown menu when there are more than one
Tool List
- Copy: Copy code or image
- Download: Download code or image
- View Source: Switch between special view and source code view
- Split View: Toggle split view mode
- Run: Run Python code
- Expand/Collapse: Control code block expansion/collapse
- Wrap: Control automatic line wrapping
- Save: Save edited code
State Management
CodeBlockView manages the following states through React hooks:
- viewMode: Current view mode ('source' | 'special' | 'split')
- isRunning: Python code execution status
- executionResult: Python code execution result
- tools: Toolbar tool list
- expandOverride/unwrapOverride: User override settings for expand/wrap
- sourceScrollHeight: Source code view scroll height
Interaction Flow
sequenceDiagram
participant U as User
participant CB as CodeBlockView
participant CT as CodeToolbar
participant SV as SpecialView
participant SE as SourceEditor
U->>CB: View code block
CB->>CB: Initialize state
CB->>CT: Register tools
CB->>SV: Render special view (if applicable)
CB->>SE: Render source view
U->>CT: Click tool button
CT->>CB: Trigger tool callback
CB->>CB: Update state
CB->>CT: Re-register tools (if needed)
Special Handling
HTML Code Blocks
HTML code blocks are specially handled using the HtmlArtifactsCard component.
Python Code Execution
Supports executing Python code and displaying results using Pyodide to run Python code in the browser.