diff --git a/packages/x-markdown/src/plugins/HighlightCode/index.tsx b/packages/x-markdown/src/plugins/HighlightCode/index.tsx index 71aba8cbf..47bf123bd 100644 --- a/packages/x-markdown/src/plugins/HighlightCode/index.tsx +++ b/packages/x-markdown/src/plugins/HighlightCode/index.tsx @@ -1,9 +1,6 @@ import useXComponentConfig from '@ant-design/x/es/_util/hooks/use-x-component-config'; import Actions from '@ant-design/x/es/actions'; -import useLocale from '@ant-design/x/es/locale/useLocale'; import useXProviderContext from '@ant-design/x/es/x-provider/hooks/use-x-provider-context'; -import locale_EN from '@ant-design/x/locale/en_US'; -import { message } from 'antd'; import classnames from 'classnames'; import React from 'react'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; @@ -32,9 +29,6 @@ const HighlightCode: React.FC = (props) => { highlightProps, } = props; - // ============================ locale ============================ - const [contextLocale] = useLocale('HighlightCode', locale_EN.HighlightCode); - // ============================ Prefix ============================ const { getPrefixCls, direction } = useXProviderContext(); const prefixCls = getPrefixCls('highlightCode', customizePrefixCls); @@ -57,23 +51,6 @@ const HighlightCode: React.FC = (props) => { }, ); - // ============================ locale ============================ - const [messageApi, contextHolder] = message.useMessage(); - - const handleCopyCode = async () => { - if (!children) return; - - try { - await navigator.clipboard.writeText(children.trim()); - messageApi.open({ - type: 'success', - content: contextLocale.copySuccess, - }); - } catch (error) { - console.error('Failed to copy code:', error); - } - }; - const renderTitle = () => { if (header === null) return null; @@ -88,7 +65,6 @@ const HighlightCode: React.FC = (props) => { )} style={{ ...contextConfig.styles.header, ...styles.header }} > - {contextHolder} = (props) => { > {lang} - + ); }; diff --git a/packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx b/packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx index 90ef2c0c6..58bc7a432 100644 --- a/packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx +++ b/packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx @@ -135,7 +135,7 @@ describe('Mermaid Plugin', () => { render({mermaidContent}); - const copyButton = screen.getByLabelText('copy'); + const copyButton = screen.getByRole('button', { name: 'Copy' }); fireEvent.click(copyButton); await waitFor(() => { @@ -154,7 +154,7 @@ describe('Mermaid Plugin', () => { render({mermaidContent}); - const copyButton = screen.getByLabelText('copy'); + const copyButton = screen.getByRole('button', { name: 'Copy' }); // 确保点击不会抛出错误 expect(() => fireEvent.click(copyButton)).not.toThrow(); @@ -177,12 +177,11 @@ describe('Mermaid Plugin', () => { render({mermaidContent}); - const copyButton = screen.getByLabelText('copy'); + const copyButton = screen.getByRole('button', { name: 'Copy' }); fireEvent.click(copyButton); - await waitFor(() => { - expect(consoleSpy).toHaveBeenCalledWith('Failed to copy code:', expect.any(Error)); - }); + // 由于Actions.Copy组件有自己的错误处理,我们只需要确保点击不会抛出错误 + expect(() => fireEvent.click(copyButton)).not.toThrow(); consoleSpy.mockRestore(); }); @@ -194,7 +193,7 @@ describe('Mermaid Plugin', () => { expect(screen.getByLabelText('zoom-in')).toBeInTheDocument(); expect(screen.getByLabelText('zoom-out')).toBeInTheDocument(); - expect(screen.getByRole('button', { name: 'Reset' })).toBeInTheDocument(); + expect(screen.getByLabelText('one-to-one')).toBeInTheDocument(); expect(screen.getByLabelText('download')).toBeInTheDocument(); const codeButton = screen.getByText('Code'); @@ -217,7 +216,7 @@ describe('Mermaid Plugin', () => { it('should handle reset functionality', () => { render({mermaidContent}); - const resetButton = screen.getByRole('button', { name: 'Reset' }); + const resetButton = screen.getByLabelText('one-to-one'); fireEvent.click(resetButton); }); }); diff --git a/packages/x-markdown/src/plugins/Mermaid/index.tsx b/packages/x-markdown/src/plugins/Mermaid/index.tsx index ab8fbe29e..f2eed349d 100644 --- a/packages/x-markdown/src/plugins/Mermaid/index.tsx +++ b/packages/x-markdown/src/plugins/Mermaid/index.tsx @@ -1,11 +1,15 @@ -import { CopyOutlined, DownloadOutlined, ZoomInOutlined, ZoomOutOutlined } from '@ant-design/icons'; +import { + DownloadOutlined, + OneToOneOutlined, + ZoomInOutlined, + ZoomOutOutlined, +} from '@ant-design/icons'; import useXComponentConfig from '@ant-design/x/es/_util/hooks/use-x-component-config'; import Actions from '@ant-design/x/es/actions'; -import { ItemType } from '@ant-design/x/es/actions/interface'; import useLocale from '@ant-design/x/es/locale/useLocale'; import useXProviderContext from '@ant-design/x/es/x-provider/hooks/use-x-provider-context'; import locale_EN from '@ant-design/x/locale/en_US'; -import { Button, message, Segmented, Tooltip } from 'antd'; +import { Segmented, Space } from 'antd'; import classnames from 'classnames'; import throttle from 'lodash.throttle'; import mermaid from 'mermaid'; @@ -46,7 +50,6 @@ const Mermaid: React.FC = React.memo((props) => { const [lastMousePos, setLastMousePos] = useState({ x: 0, y: 0 }); const containerRef = useRef(null); const id = `mermaid-${uuid++}-${children?.length || 0}`; - const [messageApi, contextHolder] = message.useMessage(); // ============================ locale ============================ const [contextLocale] = useLocale('Mermaid', locale_EN.Mermaid); @@ -210,65 +213,10 @@ const Mermaid: React.FC = React.memo((props) => { setScale((prev) => Math.max(prev - 0.2, 0.5)); }; - const handleCopyCode = async () => { - if (!children) return; - - try { - await navigator.clipboard.writeText(children.trim()); - messageApi.open({ - type: 'success', - content: contextLocale.copySuccess, - }); - } catch (error) { - console.error('Failed to copy code:', error); - } - }; - const renderHeader = () => { if (header === null) return null; if (header) return header; - const items: ItemType[] = [ - { - key: 'copy', - icon: , - label: contextLocale.copy, - onItemClick: handleCopyCode, - }, - ...(renderType === RenderType.Image - ? [ - { - key: 'zoomIn', - icon: , - label: contextLocale.zoomIn, - onItemClick: handleZoomIn, - }, - { - key: 'zoomOut', - icon: , - label: contextLocale.zoomOut, - onItemClick: handleZoomOut, - }, - { - key: 'zoomReset', - actionRender: () => ( - - - - ), - }, - { - key: 'download', - icon: , - label: contextLocale.download, - onItemClick: handleDownload, - }, - ] - : []), - ]; - return (
= React.memo((props) => { )} style={{ ...contextConfig.styles.header, ...styles.header }} > - {contextHolder} = React.memo((props) => { value={renderType} onChange={setRenderType} /> - + + + {renderType === RenderType.Image ? ( + <> + , + onItemClick: handleZoomIn, + }, + { + label: contextLocale.zoomIn, + key: 'zoomIn', + icon: , + onItemClick: handleZoomOut, + }, + { + label: contextLocale.zoomReset, + key: 'zoomReset', + icon: , + onItemClick: handleReset, + }, + { + label: contextLocale.download, + key: 'download', + icon: , + onItemClick: handleDownload, + }, + ]} + /> + + ) : null} +
); };