ToolBar.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import React from 'react';
  2. import { message, Modal, Form, Input } from 'antd';
  3. import { Menu, Toolbar } from '@antv/x6-react-components';
  4. import '@antv/x6-react-components/es/menu/style/index.css';
  5. import '@antv/x6-react-components/es/toolbar/style/index.css';
  6. // import { MainAreaWidget, ReactWidget } from '@jupyterlab/apputils';
  7. // import { JupyterFrontEnd } from '@jupyterlab/application';
  8. // import { dagDataIcon } from './icons';
  9. import {
  10. ZoomInOutlined,
  11. ZoomOutOutlined,
  12. ToolOutlined,
  13. PlayCircleOutlined,
  14. SaveOutlined
  15. } from '@ant-design/icons';
  16. import { DagToData } from './utils';
  17. // import MediateDataTable from './MediateDataTable';
  18. import { uploadFile } from './request';
  19. const Item = Toolbar.Item; // eslint-disable-line
  20. const Group = Toolbar.Group; // eslint-disable-line
  21. export default class ToolBar extends React.Component<any, any> {
  22. constructor(props: any) {
  23. super(props);
  24. this.state = {
  25. modalVisible: false,
  26. packageData: ''
  27. };
  28. }
  29. showModal = () => {
  30. this.setState({ modalVisible: true });
  31. };
  32. submitEdit = () => {
  33. const node = this.props.graph.getSelectedCells()[0];
  34. node.data.packageData = this.state.packageData;
  35. this.setState({ modalVisible: false });
  36. };
  37. onClick = (name: string) => {
  38. // const zoomNumber = this.props.graph.zoom()
  39. switch (name) {
  40. case 'save':
  41. this.props.saveGraph(DagToData(this.props.graph, this.props.dagId));
  42. message.success('保存成功!');
  43. break;
  44. case 'zoomOut':
  45. this.props.graph.zoom(-0.1, {
  46. center: { x: 0, y: 0 }
  47. });
  48. break;
  49. case 'zoomIn':
  50. this.props.graph.zoom(0.1, {
  51. center: { x: 0, y: 0 }
  52. });
  53. break;
  54. default:
  55. }
  56. };
  57. async uploadDagFile() {
  58. const text = this.props.context.current;
  59. const filename = text.path.split('/').pop();
  60. const content = JSON.stringify(
  61. DagToData(this.props.graph, this.props.dagId)
  62. );
  63. let blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
  64. let file = new File([blob], filename, {
  65. lastModified: Date.now()
  66. });
  67. const dagfile = new FormData();
  68. dagfile.append('file', file);
  69. dagfile.append('project_id', 'test');
  70. dagfile.append('file_type', 'dag');
  71. const { data } = await uploadFile(dagfile);
  72. if (data.code === 200) {
  73. message.success('上传成功');
  74. } else {
  75. message.error(data.msg);
  76. }
  77. }
  78. DropDown(toolType: string) {
  79. const MenuItem = Menu.Item; // eslint-disable-line
  80. return (
  81. <>
  82. {toolType === 'run' && (
  83. <Menu>
  84. <MenuItem name="start" onClick={this.props.runDag}>
  85. 启动执行
  86. </MenuItem>
  87. </Menu>
  88. )}
  89. {toolType === 'tool' && (
  90. <Menu>
  91. {/* <MenuItem
  92. name="dataList"
  93. onClick={() => {
  94. const widget = new MainAreaWidget({
  95. content: ReactWidget.create(<MediateDataTable />)
  96. });
  97. widget.title.label = '中间数据列表';
  98. widget.title.icon = dagDataIcon;
  99. const app: JupyterFrontEnd = (window as any).jupyterlab;
  100. app.shell.add(widget, 'main');
  101. }}
  102. >
  103. 中间数据列表
  104. </MenuItem> */}
  105. <MenuItem
  106. name="packageEdit"
  107. onClick={() => {
  108. const selected = this.props.graph.getSelectedCells();
  109. if (
  110. selected.length === 1 &&
  111. selected[0].data &&
  112. selected[0].data.type === 'script'
  113. ) {
  114. this.setState({
  115. modalVisible: true,
  116. packageData: selected[0].data.packageData
  117. });
  118. } else {
  119. message.warning('请选择单个脚本节点');
  120. }
  121. }}
  122. >
  123. 依赖编辑
  124. </MenuItem>
  125. <MenuItem
  126. name="uploadDag"
  127. onClick={() => {
  128. this.uploadDagFile();
  129. }}
  130. >
  131. 上传至项目目录
  132. </MenuItem>
  133. </Menu>
  134. )}
  135. </>
  136. );
  137. }
  138. render() {
  139. return (
  140. <div style={{ paddingRight: 16 }} className={this.props.className}>
  141. <Toolbar hoverEffect={true} size="big" onClick={this.onClick}>
  142. <Group>
  143. <Item
  144. name="save"
  145. icon={<SaveOutlined />}
  146. // tooltip="Save (Cmd/Ctrl + S)"
  147. />
  148. </Group>
  149. <Group>
  150. <Item
  151. name="run"
  152. dropdown={this.DropDown('run')}
  153. icon={<PlayCircleOutlined />}
  154. />
  155. </Group>
  156. <Group>
  157. <Item
  158. name="tool"
  159. dropdown={this.DropDown('tool')}
  160. icon={<ToolOutlined />}
  161. />
  162. </Group>
  163. <Group>
  164. <Item name="zoomOut" icon={<ZoomOutOutlined />} />
  165. <Item name="zoomIn" icon={<ZoomInOutlined />} />
  166. </Group>
  167. </Toolbar>
  168. {/* 脚本编辑弹窗 */}
  169. <Modal
  170. title="依赖编辑"
  171. open={this.state.modalVisible}
  172. onOk={this.submitEdit}
  173. okText="确定"
  174. destroyOnClose={true}
  175. className="package-modal"
  176. cancelText="取消"
  177. onCancel={() => {
  178. this.setState({ modalVisible: false });
  179. }}
  180. >
  181. <Form
  182. initialValues={{
  183. packageData: this.state.packageData
  184. }}
  185. >
  186. <Form.Item label="依赖编辑" name="packageData" required>
  187. <Input.TextArea
  188. placeholder="请输入节点需要的依赖"
  189. style={{ height: '80px' }}
  190. onChange={e => {
  191. this.setState({ packageData: e.target.value });
  192. }}
  193. />
  194. </Form.Item>
  195. </Form>
  196. </Modal>
  197. </div>
  198. );
  199. }
  200. }