Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { useSetModalState } from '@/hooks/commonHooks';
  2. import React, { Dispatch, SetStateAction, useCallback, useState } from 'react';
  3. import { Node, Position, 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: 'textUpdater',
  40. position: position || {
  41. x: 0,
  42. y: 0,
  43. },
  44. data: { label: `${type}` },
  45. sourcePosition: Position.Right,
  46. targetPosition: Position.Left,
  47. };
  48. setNodes((nds) => nds.concat(newNode));
  49. },
  50. [reactFlowInstance, setNodes],
  51. );
  52. return { onDrop, onDragOver, setReactFlowInstance };
  53. };
  54. export const useShowDrawer = () => {
  55. const {
  56. visible: drawerVisible,
  57. hideModal: hideDrawer,
  58. showModal: showDrawer,
  59. } = useSetModalState();
  60. return {
  61. drawerVisible,
  62. hideDrawer,
  63. showDrawer,
  64. };
  65. };