您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { useSetModalState } from '@/hooks/commonHooks';
  2. import React, { Dispatch, SetStateAction, useCallback, useState } from 'react';
  3. import { Node, ReactFlowInstance } from 'reactflow';
  4. import { v4 as uuidv4 } from 'uuid';
  5. export const useHandleDrag = () => {
  6. const handleDragStart = useCallback(
  7. (operatorId: string) => (ev: React.DragEvent<HTMLDivElement>) => {
  8. ev.dataTransfer.setData('application/reactflow', operatorId);
  9. ev.dataTransfer.effectAllowed = 'move';
  10. },
  11. [],
  12. );
  13. return { handleDragStart };
  14. };
  15. export const useHandleDrop = (setNodes: Dispatch<SetStateAction<Node[]>>) => {
  16. const [reactFlowInstance, setReactFlowInstance] =
  17. useState<ReactFlowInstance<any, any>>();
  18. const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
  19. event.preventDefault();
  20. event.dataTransfer.dropEffect = 'move';
  21. }, []);
  22. const onDrop = useCallback(
  23. (event: React.DragEvent<HTMLDivElement>) => {
  24. event.preventDefault();
  25. const type = event.dataTransfer.getData('application/reactflow');
  26. // check if the dropped element is valid
  27. if (typeof type === 'undefined' || !type) {
  28. return;
  29. }
  30. // reactFlowInstance.project was renamed to reactFlowInstance.screenToFlowPosition
  31. // and you don't need to subtract the reactFlowBounds.left/top anymore
  32. // details: https://reactflow.dev/whats-new/2023-11-10
  33. const position = reactFlowInstance?.screenToFlowPosition({
  34. x: event.clientX,
  35. y: event.clientY,
  36. });
  37. const newNode = {
  38. id: uuidv4(),
  39. type,
  40. position: position || {
  41. x: 0,
  42. y: 0,
  43. },
  44. data: { label: `${type} node` },
  45. };
  46. setNodes((nds) => nds.concat(newNode));
  47. },
  48. [reactFlowInstance, setNodes],
  49. );
  50. return { onDrop, onDragOver, setReactFlowInstance };
  51. };
  52. export const useShowDrawer = () => {
  53. const {
  54. visible: drawerVisible,
  55. hideModal: hideDrawer,
  56. showModal: showDrawer,
  57. } = useSetModalState();
  58. return {
  59. drawerVisible,
  60. hideDrawer,
  61. showDrawer,
  62. };
  63. };