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

View File

@ -17,6 +17,7 @@ import { Selection } from '@antv/x6-plugin-selection';
import { History } from '@antv/x6-plugin-history'; import { History } from '@antv/x6-plugin-history';
import { Clipboard } from '@antv/x6-plugin-clipboard'; import { Clipboard } from '@antv/x6-plugin-clipboard';
import { Transform } from '@antv/x6-plugin-transform'; import { Transform } from '@antv/x6-plugin-transform';
import { Keyboard } from '@antv/x6-plugin-keyboard';
import './index.less'; import './index.less';
interface ToolbarProps { interface ToolbarProps {
@ -34,6 +35,7 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
rubberband: true, rubberband: true,
movable: true, movable: true,
showNodeSelectionBox: true, showNodeSelectionBox: true,
showEdgeSelectionBox: true,
}) })
); );
// 启用历史记录 // 启用历史记录
@ -51,9 +53,83 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
// 启用变换 // 启用变换
graph.use( graph.use(
new Transform({ new Transform({
enabled: true, resizing: {
enabled: true,
},
rotating: {
enabled: true,
},
}) })
); );
// 启用键盘
graph.use(new Keyboard({
enabled: true,
}));
// 启用画布交互
graph.enableSelection();
graph.enableRubberband();
const keyboard = graph.getPlugin<Keyboard>('keyboard');
if (keyboard) {
// 复制粘贴快捷键
keyboard.bindKey(['meta+c', 'ctrl+c'], () => {
const selection = graph.getPlugin<Selection>('selection');
const clipboard = graph.getPlugin<Clipboard>('clipboard');
const cells = selection?.getSelectedCells();
if (cells?.length) {
clipboard?.copy(cells);
}
return false;
});
keyboard.bindKey(['meta+v', 'ctrl+v'], () => {
const clipboard = graph.getPlugin<Clipboard>('clipboard');
const selection = graph.getPlugin<Selection>('selection');
if (clipboard && !clipboard.isEmpty()) {
const cells = clipboard.paste();
selection?.clean();
cells.forEach((cell: Cell) => selection?.select(cell));
}
return false;
});
// 全选快捷键
keyboard.bindKey(['meta+a', 'ctrl+a'], () => {
const selection = graph.getPlugin<Selection>('selection');
const cells = graph.getCells();
selection?.clean();
cells.forEach((cell: Cell) => selection?.select(cell));
return false;
});
// 删除快捷键
keyboard.bindKey(['backspace', 'delete'], () => {
const selection = graph.getPlugin<Selection>('selection');
const cells = selection?.getSelectedCells();
if (cells?.length) {
graph.removeCells(cells);
}
return false;
});
// 撤销重做快捷键
keyboard.bindKey(['meta+z', 'ctrl+z'], () => {
const history = graph.getPlugin<History>('history');
if (history?.canUndo()) {
history.undo();
}
return false;
});
keyboard.bindKey(['meta+shift+z', 'ctrl+shift+z'], () => {
const history = graph.getPlugin<History>('history');
if (history?.canRedo()) {
history.redo();
}
return false;
});
}
} }
}, [graph]); }, [graph]);
@ -130,7 +206,7 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
if (!graph) return; if (!graph) return;
const clipboard = graph.getPlugin<Clipboard>('clipboard'); const clipboard = graph.getPlugin<Clipboard>('clipboard');
const selection = graph.getPlugin<Selection>('selection'); const selection = graph.getPlugin<Selection>('selection');
if (!clipboard?.isEmpty()) { if (clipboard && !clipboard.isEmpty()) {
const cells = clipboard.paste(); const cells = clipboard.paste();
selection?.clean(); selection?.clean();
cells.forEach((cell: Cell) => selection?.select(cell)); cells.forEach((cell: Cell) => selection?.select(cell));
@ -170,10 +246,10 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
</Space> </Space>
<Space> <Space>
<Tooltip title="全选"> <Tooltip title="全选 (Ctrl + A)">
<Button icon={<SelectOutlined />} onClick={selectAll} /> <Button icon={<SelectOutlined />} onClick={selectAll} />
</Tooltip> </Tooltip>
<Tooltip title="删除"> <Tooltip title="删除 (Delete)">
<Button <Button
icon={<DeleteOutlined />} icon={<DeleteOutlined />}
onClick={deleteSelected} onClick={deleteSelected}
@ -184,14 +260,14 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
</Space> </Space>
<Space> <Space>
<Tooltip title="撤销"> <Tooltip title="撤销 (Ctrl + Z)">
<Button <Button
icon={<UndoOutlined />} icon={<UndoOutlined />}
onClick={undo} onClick={undo}
disabled={!graph?.getPlugin<History>('history')?.canUndo()} disabled={!graph?.getPlugin<History>('history')?.canUndo()}
/> />
</Tooltip> </Tooltip>
<Tooltip title="重做"> <Tooltip title="重做 (Ctrl + Shift + Z)">
<Button <Button
icon={<RedoOutlined />} icon={<RedoOutlined />}
onClick={redo} onClick={redo}
@ -201,14 +277,14 @@ const Toolbar: React.FC<ToolbarProps> = ({ graph }) => {
</Space> </Space>
<Space> <Space>
<Tooltip title="复制"> <Tooltip title="复制 (Ctrl + C)">
<Button <Button
icon={<CopyOutlined />} icon={<CopyOutlined />}
onClick={copy} onClick={copy}
disabled={!hasSelection()} disabled={!hasSelection()}
/> />
</Tooltip> </Tooltip>
<Tooltip title="粘贴"> <Tooltip title="粘贴 (Ctrl + V)">
<Button <Button
icon={<SnippetsOutlined />} icon={<SnippetsOutlined />}
onClick={paste} onClick={paste}