Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions packages/x-markdown/src/plugins/HighlightCode/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,9 +29,6 @@ const HighlightCode: React.FC<HighlightCodeProps> = (props) => {
highlightProps,
} = props;

// ============================ locale ============================
const [contextLocale] = useLocale('HighlightCode', locale_EN.HighlightCode);

// ============================ Prefix ============================
const { getPrefixCls, direction } = useXProviderContext();
const prefixCls = getPrefixCls('highlightCode', customizePrefixCls);
Expand All @@ -57,23 +51,6 @@ const HighlightCode: React.FC<HighlightCodeProps> = (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;

Expand All @@ -88,7 +65,6 @@ const HighlightCode: React.FC<HighlightCodeProps> = (props) => {
)}
style={{ ...contextConfig.styles.header, ...styles.header }}
>
{contextHolder}
<span
className={classnames(
`${prefixCls}-header-title`,
Expand All @@ -99,7 +75,7 @@ const HighlightCode: React.FC<HighlightCodeProps> = (props) => {
>
{lang}
</span>
<Actions.Copy text={contextLocale.copy} onClick={handleCopyCode} />
<Actions.Copy text={children.trim()} />
</div>
);
};
Expand Down
15 changes: 7 additions & 8 deletions packages/x-markdown/src/plugins/Mermaid/__test__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Mermaid Plugin', () => {

render(<Mermaid>{mermaidContent}</Mermaid>);

const copyButton = screen.getByLabelText('copy');
const copyButton = screen.getByRole('button', { name: 'Copy' });
fireEvent.click(copyButton);

await waitFor(() => {
Expand All @@ -154,7 +154,7 @@ describe('Mermaid Plugin', () => {

render(<Mermaid>{mermaidContent}</Mermaid>);

const copyButton = screen.getByLabelText('copy');
const copyButton = screen.getByRole('button', { name: 'Copy' });

// 确保点击不会抛出错误
expect(() => fireEvent.click(copyButton)).not.toThrow();
Expand All @@ -177,12 +177,11 @@ describe('Mermaid Plugin', () => {

render(<Mermaid>{mermaidContent}</Mermaid>);

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();
});
Expand All @@ -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');
Expand All @@ -217,7 +216,7 @@ describe('Mermaid Plugin', () => {
it('should handle reset functionality', () => {
render(<Mermaid>{mermaidContent}</Mermaid>);

const resetButton = screen.getByRole('button', { name: 'Reset' });
const resetButton = screen.getByLabelText('one-to-one');
fireEvent.click(resetButton);
});
});
Expand Down
103 changes: 42 additions & 61 deletions packages/x-markdown/src/plugins/Mermaid/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,7 +50,6 @@ const Mermaid: React.FC<MermaidProps> = React.memo((props) => {
const [lastMousePos, setLastMousePos] = useState({ x: 0, y: 0 });
const containerRef = useRef<HTMLDivElement>(null);
const id = `mermaid-${uuid++}-${children?.length || 0}`;
const [messageApi, contextHolder] = message.useMessage();

// ============================ locale ============================
const [contextLocale] = useLocale('Mermaid', locale_EN.Mermaid);
Expand Down Expand Up @@ -210,65 +213,10 @@ const Mermaid: React.FC<MermaidProps> = 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: <CopyOutlined />,
label: contextLocale.copy,
onItemClick: handleCopyCode,
},
...(renderType === RenderType.Image
? [
{
key: 'zoomIn',
icon: <ZoomInOutlined />,
label: contextLocale.zoomIn,
onItemClick: handleZoomIn,
},
{
key: 'zoomOut',
icon: <ZoomOutOutlined />,
label: contextLocale.zoomOut,
onItemClick: handleZoomOut,
},
{
key: 'zoomReset',
actionRender: () => (
<Tooltip title={contextLocale.zoomReset}>
<Button type="text" size="small" onClick={handleReset}>
{contextLocale.zoomReset}
</Button>
</Tooltip>
),
},
{
key: 'download',
icon: <DownloadOutlined />,
label: contextLocale.download,
onItemClick: handleDownload,
},
]
: []),
];

return (
<div
className={classnames(
Expand All @@ -278,7 +226,6 @@ const Mermaid: React.FC<MermaidProps> = React.memo((props) => {
)}
style={{ ...contextConfig.styles.header, ...styles.header }}
>
{contextHolder}
<Segmented
options={[
{ label: contextLocale.image, value: RenderType.Image },
Expand All @@ -287,7 +234,41 @@ const Mermaid: React.FC<MermaidProps> = React.memo((props) => {
value={renderType}
onChange={setRenderType}
/>
<Actions items={items} />
<Space>
<Actions.Copy text={children.trim()} />
{renderType === RenderType.Image ? (
<>
<Actions
items={[
{
label: contextLocale.zoomOut,
key: 'zoomOut',
icon: <ZoomInOutlined />,
onItemClick: handleZoomIn,
},
{
label: contextLocale.zoomIn,
key: 'zoomIn',
icon: <ZoomOutOutlined />,
onItemClick: handleZoomOut,
},
{
label: contextLocale.zoomReset,
key: 'zoomReset',
icon: <OneToOneOutlined />,
onItemClick: handleReset,
},
{
label: contextLocale.download,
key: 'download',
icon: <DownloadOutlined />,
onItemClick: handleDownload,
},
]}
/>
</>
) : null}
</Space>
</div>
);
};
Expand Down
Loading