d05d1309ca
* 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
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { memo, useCallback } from 'react'
|
|
|
|
import { useDebouncedRender } from './hooks/useDebouncedRender'
|
|
import ImagePreviewLayout from './ImagePreviewLayout'
|
|
import { BasicPreviewHandles } from './types'
|
|
import { renderSvgInShadowHost } from './utils'
|
|
|
|
interface SvgPreviewProps {
|
|
children: string
|
|
enableToolbar?: boolean
|
|
className?: string
|
|
ref?: React.RefObject<BasicPreviewHandles | null>
|
|
}
|
|
|
|
/**
|
|
* 使用 Shadow DOM 渲染 SVG
|
|
*/
|
|
const SvgPreview = ({ children, enableToolbar = false, className, ref }: SvgPreviewProps) => {
|
|
// 定义渲染函数
|
|
const renderSvg = useCallback(async (content: string, container: HTMLDivElement) => {
|
|
renderSvgInShadowHost(content, container)
|
|
}, [])
|
|
|
|
// 使用预览渲染器 hook
|
|
const { containerRef, error, isLoading } = useDebouncedRender(children, renderSvg, {
|
|
debounceDelay: 300
|
|
})
|
|
|
|
return (
|
|
<ImagePreviewLayout
|
|
loading={isLoading}
|
|
error={error}
|
|
enableToolbar={enableToolbar}
|
|
ref={ref}
|
|
imageRef={containerRef}
|
|
source="svg">
|
|
<div ref={containerRef} className={className ?? 'svg-preview special-preview'}></div>
|
|
</ImagePreviewLayout>
|
|
)
|
|
}
|
|
|
|
export default memo(SvgPreview)
|