config-toolbar.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import {
  2. createToolbarConfig,
  3. IModelService,
  4. IToolbarItemOptions,
  5. NsGroupCmd,
  6. uuidv4,
  7. XFlowGroupCommands,
  8. XFlowNodeCommands,
  9. XFlowGraphCommands,
  10. NsGraphCmd,
  11. NsNodeCmd,
  12. IconStore,
  13. MODELS,
  14. } from '@antv/xflow';
  15. import {
  16. UngroupOutlined,
  17. SaveOutlined,
  18. GroupOutlined,
  19. GatewayOutlined,
  20. UndoOutlined,
  21. RedoOutlined,
  22. VerticalAlignTopOutlined,
  23. VerticalAlignBottomOutlined,
  24. CopyOutlined,
  25. SnippetsOutlined,
  26. } from '@ant-design/icons';
  27. const GROUP_NODE_RENDER_ID = 'GROUP_NODE_RENDER_ID';
  28. export namespace TOOLBAR_ITEMS {
  29. export const BACK_NODE = XFlowNodeCommands.BACK_NODE.id;
  30. export const FRONT_NODE = XFlowNodeCommands.FRONT_NODE.id;
  31. export const SAVE_GRAPH_DATA = XFlowGraphCommands.SAVE_GRAPH_DATA.id;
  32. export const REDO_CMD = `${XFlowGraphCommands.REDO_CMD.id}`;
  33. export const UNDO_CMD = `${XFlowGraphCommands.UNDO_CMD.id}`;
  34. export const MULTI_SELECT = `${XFlowGraphCommands.GRAPH_TOGGLE_MULTI_SELECT.id}`;
  35. export const ADD_GROUP = `${XFlowGroupCommands.ADD_GROUP.id}`;
  36. export const DEL_GROUP = `${XFlowGroupCommands.DEL_GROUP.id}`;
  37. export const COPY = `${XFlowGraphCommands.GRAPH_COPY.id}`;
  38. export const PASTE = `${XFlowGraphCommands.GRAPH_PASTE.id}`;
  39. }
  40. namespace NSToolbarConfig {
  41. /** toolbar依赖的状态 */
  42. export interface IToolbarState {
  43. isMultiSelectionActive: boolean;
  44. isGroupSelected: boolean;
  45. isNodeSelected: boolean;
  46. isUndoable: boolean;
  47. isRedoable: boolean;
  48. }
  49. export const getDependencies = async (modelService: IModelService) => {
  50. return [
  51. await MODELS.SELECTED_NODES.getModel(modelService),
  52. await MODELS.GRAPH_ENABLE_MULTI_SELECT.getModel(modelService),
  53. ];
  54. };
  55. /** toolbar依赖的状态 */
  56. export const getToolbarState = async (modelService: IModelService) => {
  57. // isMultiSelectionActive
  58. const { isEnable: isMultiSelectionActive } = await MODELS.GRAPH_ENABLE_MULTI_SELECT.useValue(
  59. modelService
  60. );
  61. // isGroupSelected
  62. const isGroupSelected = await MODELS.IS_GROUP_SELECTED.useValue(modelService);
  63. // isNormalNodesSelected: node不能是GroupNode
  64. const isNormalNodesSelected = await MODELS.IS_NORMAL_NODES_SELECTED.useValue(modelService);
  65. // undo redo
  66. const isUndoable = await MODELS.COMMAND_UNDOABLE.useValue(modelService);
  67. const isRedoable = await MODELS.COMMAND_REDOABLE.useValue(modelService);
  68. return {
  69. isUndoable,
  70. isRedoable,
  71. isNodeSelected: isNormalNodesSelected,
  72. isGroupSelected,
  73. isMultiSelectionActive,
  74. } as NSToolbarConfig.IToolbarState;
  75. };
  76. export const getToolbarItems = async (state: IToolbarState) => {
  77. const toolbarGroup: IToolbarItemOptions[] = [];
  78. // const history = getGraphHistory()
  79. // /** 撤销 */
  80. // toolbarGroup.push({
  81. // tooltip: '撤销',
  82. // iconName: 'UndoOutlined',
  83. // id: TOOLBAR_ITEMS.UNDO_CMD,
  84. // isEnabled: history.canUndo(),
  85. // onClick: async () => {
  86. // history.undo()
  87. // },
  88. // })
  89. // /** 重做 */
  90. // toolbarGroup.push({
  91. // tooltip: '重做',
  92. // iconName: 'RedoOutlined',
  93. // id: TOOLBAR_ITEMS.REDO_CMD,
  94. // isEnabled: history.canRedo(),
  95. // onClick: async () => {
  96. // history.redo()
  97. // },
  98. // })
  99. /** FRONT_NODE */
  100. toolbarGroup.push({
  101. tooltip: '置前',
  102. iconName: 'VerticalAlignTopOutlined',
  103. id: TOOLBAR_ITEMS.FRONT_NODE,
  104. isEnabled: state.isNodeSelected,
  105. onClick: async ({ commandService, modelService }) => {
  106. const node = await MODELS.SELECTED_NODE.useValue(modelService);
  107. commandService.executeCommand<NsNodeCmd.FrontNode.IArgs>(TOOLBAR_ITEMS.FRONT_NODE, {
  108. nodeId: node?.id,
  109. });
  110. },
  111. });
  112. /** BACK_NODE */
  113. toolbarGroup.push({
  114. tooltip: '置后',
  115. iconName: 'VerticalAlignBottomOutlined',
  116. id: TOOLBAR_ITEMS.BACK_NODE,
  117. isEnabled: state.isNodeSelected,
  118. onClick: async ({ commandService, modelService }) => {
  119. const node = await MODELS.SELECTED_NODE.useValue(modelService);
  120. commandService.executeCommand<NsNodeCmd.FrontNode.IArgs>(TOOLBAR_ITEMS.BACK_NODE, {
  121. nodeId: node?.id,
  122. });
  123. },
  124. });
  125. /** 开启框选 */
  126. toolbarGroup.push({
  127. tooltip: '开启框选',
  128. iconName: 'GatewayOutlined',
  129. id: TOOLBAR_ITEMS.MULTI_SELECT,
  130. active: state.isMultiSelectionActive,
  131. onClick: async ({ commandService }) => {
  132. commandService.executeCommand<NsGraphCmd.GraphToggleMultiSelect.IArgs>(
  133. TOOLBAR_ITEMS.MULTI_SELECT,
  134. {}
  135. );
  136. },
  137. });
  138. /** 新建群组 */
  139. toolbarGroup.push({
  140. tooltip: '新建群组',
  141. iconName: 'GroupOutlined',
  142. id: TOOLBAR_ITEMS.ADD_GROUP,
  143. isEnabled: state.isNodeSelected,
  144. onClick: async ({ commandService, modelService }) => {
  145. const cells = await MODELS.SELECTED_CELLS.useValue(modelService);
  146. const groupChildren = cells.map(cell => cell.id);
  147. commandService.executeCommand<NsGroupCmd.AddGroup.IArgs>(TOOLBAR_ITEMS.ADD_GROUP, {
  148. nodeConfig: {
  149. id: uuidv4(),
  150. renderKey: GROUP_NODE_RENDER_ID,
  151. groupChildren,
  152. groupCollapsedSize: { width: 200, height: 40 },
  153. label: '新建群组',
  154. },
  155. });
  156. },
  157. });
  158. /** 解散群组 */
  159. toolbarGroup.push({
  160. tooltip: '解散群组',
  161. iconName: 'UngroupOutlined',
  162. id: TOOLBAR_ITEMS.DEL_GROUP,
  163. isEnabled: state.isGroupSelected,
  164. onClick: async ({ commandService, modelService }) => {
  165. const cell = await MODELS.SELECTED_NODE.useValue(modelService);
  166. const nodeConfig = cell.getData();
  167. commandService.executeCommand<NsGroupCmd.AddGroup.IArgs>(XFlowGroupCommands.DEL_GROUP.id, {
  168. nodeConfig: nodeConfig,
  169. });
  170. },
  171. });
  172. /** 保存数据 */
  173. toolbarGroup.push({
  174. tooltip: '保存',
  175. iconName: 'SaveOutlined',
  176. id: TOOLBAR_ITEMS.SAVE_GRAPH_DATA,
  177. onClick: async ({ commandService }) => {
  178. commandService.executeCommand<NsGraphCmd.SaveGraphData.IArgs>(
  179. TOOLBAR_ITEMS.SAVE_GRAPH_DATA,
  180. {
  181. saveGraphDataService: (meta, graphData) => {
  182. let data = JSON.parse(JSON.stringify(graphData));
  183. data.nodes = data.nodes.map(item => {
  184. // delete item.incomingEdges;
  185. // delete item.originData;
  186. // delete item.outgoingEdges;
  187. // delete item.ports.groups
  188. let ports = { ...item.ports };
  189. delete ports.groups;
  190. return {
  191. id: item.id,
  192. renderKey: item.renderKey,
  193. name: item.name,
  194. label: item.label,
  195. width: item.width,
  196. height: item.height,
  197. ports: ports,
  198. isCustom: item.isCustom,
  199. parentKey: item.parentKey,
  200. x: item.x,
  201. y: item.y,
  202. zIndex: item.zIndex,
  203. flow_id: item.flow_id || 0,
  204. node_type: item.name == 'custom-circle' ? 1 : 0,
  205. count: 0,
  206. role_list: item.role_list,
  207. // count: item.count,
  208. };
  209. });
  210. // graphData.edges = []
  211. data.edges = data.edges.map(item => {
  212. // delete item.data;
  213. // delete item.attrs;
  214. // delete item.sourcePort;
  215. // delete item.sourcePortId;
  216. // delete item.targetPort;
  217. // delete item.targetPortId;
  218. // delete item.zIndex;
  219. return {
  220. id: item.id,
  221. source: item.source,
  222. target: item.target,
  223. attr: JSON.stringify(item.attrs),
  224. };
  225. });
  226. console.log(data);
  227. console.log(JSON.stringify(data));
  228. localStorage.graphData = JSON.stringify(data);
  229. return null;
  230. },
  231. }
  232. );
  233. },
  234. });
  235. return [
  236. {
  237. name: 'graphData',
  238. items: toolbarGroup,
  239. },
  240. ];
  241. };
  242. }
  243. /** 注册icon 类型 */
  244. const registerIcon = () => {
  245. IconStore.set('SaveOutlined', SaveOutlined);
  246. IconStore.set('UndoOutlined', UndoOutlined);
  247. IconStore.set('RedoOutlined', RedoOutlined);
  248. IconStore.set('VerticalAlignTopOutlined', VerticalAlignTopOutlined);
  249. IconStore.set('VerticalAlignBottomOutlined', VerticalAlignBottomOutlined);
  250. IconStore.set('GatewayOutlined', GatewayOutlined);
  251. IconStore.set('GroupOutlined', GroupOutlined);
  252. IconStore.set('UngroupOutlined', UngroupOutlined);
  253. IconStore.set('CopyOutlined', CopyOutlined);
  254. IconStore.set('SnippetsOutlined', SnippetsOutlined);
  255. };
  256. export const useToolbarConfig = createToolbarConfig((toolbarConfig, proxy) => {
  257. registerIcon();
  258. /** 生产 toolbar item */
  259. toolbarConfig.setToolbarModelService(async (toolbarModel, modelService, toDispose) => {
  260. const updateToolbarModel = async () => {
  261. const state = await NSToolbarConfig.getToolbarState(modelService);
  262. const toolbarItems = await NSToolbarConfig.getToolbarItems(state);
  263. toolbarModel.setValue(toolbar => {
  264. toolbar.mainGroups = toolbarItems;
  265. });
  266. };
  267. const models = await NSToolbarConfig.getDependencies(modelService);
  268. const subscriptions = models.map(model => {
  269. return model.watch(async () => {
  270. updateToolbarModel();
  271. });
  272. });
  273. toDispose.pushAll(subscriptions);
  274. });
  275. });