-
Notifications
You must be signed in to change notification settings - Fork 268
/
index.tsx
204 lines (190 loc) · 5.5 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React, { ReactElement, CSSProperties } from 'react';
import { Graph } from '@antv/g6';
import {
FullscreenExitOutlined,
FullscreenOutlined,
ZoomOutOutlined,
ZoomInOutlined,
UndoOutlined,
RedoOutlined,
EyeOutlined,
EyeInvisibleOutlined,
} from '@ant-design/icons';
import { Tooltip, Button, Popover, Progress } from 'antd';
import useFishEye from './use-fisheye';
import useFullscreen from './use-fullscreen';
import useZoom from './use-zoom';
import './index.less';
const MIN_ZOOM = 0.1;
const MAX_ZOOM = 2;
/**
* @param {*} props 支持 刷新/放大/缩小/全屏 四功能
*/
interface MenuItem {
id: string;
name: string;
icon: ReactElement;
disabled?: boolean;
style?: object;
action: () => void;
renderTooltip?: () => ReactElement;
}
export interface RenderProps {
toolbarCfg: MenuItem[];
graph?: Graph;
apis?: any; // eslint-disable-line
graphVars?: {
width?: number;
height?: number;
};
direction?: string; // 指定步骤条方向。目前支持水平(horizontal)和竖直(vertical)两种方向
}
export type Tdirection = 'horizontal' | 'vertical';
export interface ToolbarProps {
style?: CSSProperties;
graphDOM?: HTMLElement;
graph?: Graph;
apis?: any; // eslint-disable-line
className?: string;
graphVars?: {
width?: number;
height?: number;
};
direction?: Tdirection; // 指定步骤条方向。目前支持水平(horizontal)和竖直(vertical)两种方向
render?(props: RenderProps): MenuItem[];
}
const defaultStyle: CSSProperties = {
position: 'absolute',
top: '48px',
left: '48px',
};
const Toolbar: React.FC<ToolbarProps> = (props) => {
const { graph: GraphFromProps, className = '', render, graphVars = {}, apis, direction = 'vertical', style } = props;
const graph = GraphFromProps!; // 断言一定存在,因为通过 React.cloneElement 的方式,历史原因,就不改这块了
const { history } = apis;
const { width = 0, height = 0 } = graphVars;
const graphinContainer = document.getElementById('graphin-container') as HTMLElement;
const [fishEyeState, toggleFishEye] = useFishEye(graph);
const [fullscreen, toggleFullscreen] = useFullscreen(graphinContainer);
const [zoom, handleZoom] = useZoom(1);
const handleGraphZoom = (isZoom: boolean) => {
const curZoom = +graph.getZoom().toFixed(2);
const center = {
x: width / 2,
y: height / 2,
};
const newZoom = handleZoom(isZoom, curZoom);
graph.zoomTo(newZoom, center);
};
const historyInfo = history.getInfo();
let buttonCfg: MenuItem[];
buttonCfg = [
{
id: 'fullscreen',
name: fullscreen ? '还原' : '全屏',
icon: fullscreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />,
disabled: false,
action: toggleFullscreen,
},
{
id: 'zoomIn',
name: '放大',
icon: <ZoomInOutlined />,
disabled: zoom >= MAX_ZOOM,
action: () => handleGraphZoom(true),
},
{
id: 'zoomOut',
name: '缩小',
icon: <ZoomOutOutlined />,
disabled: zoom <= MIN_ZOOM,
action: () => handleGraphZoom(false),
},
{
id: 'fishEye',
name: fishEyeState ? '关闭鱼眼放大镜' : '开启鱼眼放大镜',
icon: fishEyeState ? <EyeInvisibleOutlined /> : <EyeOutlined />,
action: toggleFishEye,
},
{
id: 'undo',
name: `撤销操作,进度:${historyInfo.currentStep} / ${historyInfo.allStep}`,
icon: <UndoOutlined />,
disabled: false,
action: () => {
history.undo();
},
style: {},
renderTooltip: () => {
const { currentStep, allStep } = historyInfo;
const percent = Math.round((currentStep / allStep) * 100);
return (
<div>
<Progress percent={percent} showInfo={false} />
</div>
);
},
},
{
id: 'redo',
name: `重做操作,进度:${historyInfo.currentStep} / ${historyInfo.allStep}`,
icon: <RedoOutlined />,
disabled: false,
action: () => {
history.redo();
},
style: {},
renderTooltip: () => {
const { currentStep, allStep } = historyInfo;
const percent = Math.round((currentStep / allStep) * 100);
return (
<div>
<Progress percent={percent} showInfo={false} />
</div>
);
},
},
];
if (render) {
buttonCfg = render({
toolbarCfg: buttonCfg,
graph,
graphVars,
apis,
direction,
});
}
const placement = direction === 'vertical' ? 'right' : 'bottom';
return (
<div>
<div className={`zoom-toolbar ${direction} ${className}`} style={style || defaultStyle}>
{buttonCfg.map((item) => {
/** 需要自定义渲染 */
if (item.renderTooltip) {
return (
<Popover
placement={placement}
content={item.renderTooltip()}
title={item.name}
trigger="hover"
key={item.id}
>
<Button onClick={item.action} disabled={item.disabled} style={item.style}>
{item.icon}
</Button>
</Popover>
);
}
return (
<Tooltip placement={placement} title={item.name} key={item.id}>
<Button onClick={item.action} disabled={item.disabled} style={item.style}>
{item.icon}
</Button>
</Tooltip>
);
})}
</div>
</div>
);
};
export default Toolbar;