This commit is contained in:
戚辰先生 2024-12-05 21:54:23 +08:00
parent c1da34e46b
commit f050b8228c

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect } from 'react';
import { Space, Button, Tooltip, Divider } from 'antd'; import { Space, Button, Tooltip, Divider } from 'antd';
import { import {
ZoomInOutlined, ZoomInOutlined,
@ -12,7 +12,11 @@ import {
CopyOutlined, CopyOutlined,
SnippetsOutlined, SnippetsOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { Graph } from '@antv/x6'; import { Graph, Cell } from '@antv/x6';
import { Selection } from '@antv/x6-plugin-selection';
import { History } from '@antv/x6-plugin-history';
import { Clipboard } from '@antv/x6-plugin-clipboard';
import { Transform } from '@antv/x6-plugin-transform';
import './index.less'; import './index.less';
interface ToolbarProps { interface ToolbarProps {
@ -20,6 +24,39 @@ interface ToolbarProps {
} }
const Toolbar: React.FC<ToolbarProps> = ({ graph }) => { const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
useEffect(() => {
if (graph) {
// 启用选择插件
graph.use(
new Selection({
enabled: true,
multiple: true,
rubberband: true,
movable: true,
showNodeSelectionBox: true,
})
);
// 启用历史记录
graph.use(
new History({
enabled: true,
})
);
// 启用剪贴板
graph.use(
new Clipboard({
enabled: true,
})
);
// 启用变换
graph.use(
new Transform({
enabled: true,
})
);
}
}, [graph]);
// 缩放画布 // 缩放画布
const zoom = (delta: number) => { const zoom = (delta: number) => {
if (!graph) return; if (!graph) return;
@ -43,55 +80,77 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
// 全选 // 全选
const selectAll = () => { const selectAll = () => {
if (!graph) return; if (!graph) return;
const nodes = graph.getNodes(); const cells = graph.getCells();
const edges = graph.getEdges(); const selection = graph.getPlugin<Selection>('selection');
graph.resetSelection(); selection?.clean();
graph.select(nodes); cells.forEach((cell: Cell) => selection?.select(cell));
graph.select(edges);
}; };
// 删除选中 // 删除选中
const deleteSelected = () => { const deleteSelected = () => {
if (!graph) return; if (!graph) return;
const cells = graph.getSelectedCells(); const selection = graph.getPlugin<Selection>('selection');
graph.removeCells(cells); const cells = selection?.getSelectedCells();
if (cells?.length) {
graph.removeCells(cells);
}
}; };
// 撤销 // 撤销
const undo = () => { const undo = () => {
if (!graph) return; if (!graph) return;
if (graph.canUndo()) { const history = graph.getPlugin<History>('history');
graph.undo(); if (history?.canUndo()) {
history.undo();
} }
}; };
// 重做 // 重做
const redo = () => { const redo = () => {
if (!graph) return; if (!graph) return;
if (graph.canRedo()) { const history = graph.getPlugin<History>('history');
graph.redo(); if (history?.canRedo()) {
history.redo();
} }
}; };
// 复制 // 复制
const copy = () => { const copy = () => {
if (!graph) return; if (!graph) return;
const cells = graph.getSelectedCells(); const clipboard = graph.getPlugin<Clipboard>('clipboard');
if (cells.length > 0) { const selection = graph.getPlugin<Selection>('selection');
graph.copy(cells); const cells = selection?.getSelectedCells();
if (cells?.length) {
clipboard?.copy(cells);
} }
}; };
// 粘贴 // 粘贴
const paste = () => { const paste = () => {
if (!graph) return; if (!graph) return;
if (!graph.isClipboardEmpty()) { const clipboard = graph.getPlugin<Clipboard>('clipboard');
const cells = graph.paste({ offset: 20 }); const selection = graph.getPlugin<Selection>('selection');
graph.cleanSelection(); if (!clipboard?.isEmpty()) {
graph.select(cells); const cells = clipboard.paste();
selection?.clean();
cells.forEach((cell: Cell) => selection?.select(cell));
} }
}; };
// 检查是否有选中的节点或边
const hasSelection = () => {
if (!graph) return false;
const selection = graph.getPlugin<Selection>('selection');
return (selection?.getSelectedCells().length || 0) > 0;
};
// 检查是否可以粘贴
const canPaste = () => {
if (!graph) return false;
const clipboard = graph.getPlugin<Clipboard>('clipboard');
return !clipboard?.isEmpty();
};
return ( return (
<div className="workflow-toolbar"> <div className="workflow-toolbar">
<Space split={<Divider type="vertical" />}> <Space split={<Divider type="vertical" />}>
@ -118,6 +177,7 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
<Button <Button
icon={<DeleteOutlined />} icon={<DeleteOutlined />}
onClick={deleteSelected} onClick={deleteSelected}
disabled={!hasSelection()}
danger danger
/> />
</Tooltip> </Tooltip>
@ -128,14 +188,14 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
<Button <Button
icon={<UndoOutlined />} icon={<UndoOutlined />}
onClick={undo} onClick={undo}
disabled={!graph?.canUndo()} disabled={!graph?.getPlugin<History>('history')?.canUndo()}
/> />
</Tooltip> </Tooltip>
<Tooltip title="重做"> <Tooltip title="重做">
<Button <Button
icon={<RedoOutlined />} icon={<RedoOutlined />}
onClick={redo} onClick={redo}
disabled={!graph?.canRedo()} disabled={!graph?.getPlugin<History>('history')?.canRedo()}
/> />
</Tooltip> </Tooltip>
</Space> </Space>
@ -145,14 +205,14 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
<Button <Button
icon={<CopyOutlined />} icon={<CopyOutlined />}
onClick={copy} onClick={copy}
disabled={!graph?.getSelectedCells().length} disabled={!hasSelection()}
/> />
</Tooltip> </Tooltip>
<Tooltip title="粘贴"> <Tooltip title="粘贴">
<Button <Button
icon={<SnippetsOutlined />} icon={<SnippetsOutlined />}
onClick={paste} onClick={paste}
disabled={graph?.isClipboardEmpty()} disabled={!canPaste()}
/> />
</Tooltip> </Tooltip>
</Space> </Space>