Advertisement
PuriDevelopers

DraggableTest2

Jan 20th, 2024
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useRef, useEffect } from 'react';
  2. import interact from 'interactjs';
  3.  
  4. interface ResizableIframeProps {
  5.   windowIndex: number;
  6.   iframeSrc: string;
  7. }
  8.  
  9. const ResizableIframe: React.FC<ResizableIframeProps> = ({ windowIndex, iframeSrc }) => {
  10.   const resizableRef = useRef<HTMLDivElement>(null);
  11.   const iframeRef = useRef<HTMLIFrameElement>(null);
  12.  
  13.   const handleResizeStart = (event: any) => {
  14.     const target = event.target;
  15.     target.setAttribute('data-x', target.offsetLeft);
  16.     target.setAttribute('data-y', target.offsetTop);
  17.  
  18.     iframeRef.current?.style.setProperty('pointer-events', 'none');
  19.   };
  20.  
  21.   const handleResizeMove = (event: any) => {
  22.     const target = event.target;
  23.     const x = parseFloat(target.getAttribute('data-x')) || 0;
  24.     const y = parseFloat(target.getAttribute('data-y')) || 0;
  25.  
  26.     if (resizableRef.current) {
  27.       resizableRef.current.style.width = event.rect.width + 'px';
  28.       resizableRef.current.style.height = event.rect.height + 'px';
  29.  
  30.       const newX = x + event.deltaRect.left;
  31.       const newY = y + event.deltaRect.top;
  32.  
  33.       resizableRef.current.style.transform = `translate(${newX}px, ${newY}px)`;
  34.  
  35.       target.setAttribute('data-x', newX.toString());
  36.       target.setAttribute('data-y', newY.toString());
  37.     }
  38.   };
  39.  
  40.   const handleResizeEnd = () => {
  41.     iframeRef.current?.style.setProperty('pointer-events', 'auto');
  42.   };
  43.  
  44.   const handleDragMoveStart = () => {
  45.     iframeRef.current?.style.setProperty('pointer-events', 'none');
  46.   };
  47.  
  48.   const handleDragMoveEnd = () => {
  49.     iframeRef.current?.style.setProperty('pointer-events', 'auto');
  50.   };
  51.  
  52.   const dragMoveListener = (event: any) => {
  53.     const target = resizableRef.current;
  54.     const x = (parseFloat(target?.getAttribute('data-x')) || 0) + event.dx;
  55.     const y = (parseFloat(target?.getAttribute('data-y')) || 0) + event.dy;
  56.  
  57.     target?.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
  58.  
  59.     target?.setAttribute('data-x', x.toString());
  60.     target?.setAttribute('data-y', y.toString());
  61.   };
  62.  
  63.   useEffect(() => {
  64.     if (resizableRef.current) {
  65.       interact(resizableRef.current)
  66.         .resizable({
  67.           edges: { bottom: true, right: true },
  68.           restrictEdges: {
  69.             outer: 'parent',
  70.             endOnly: true,
  71.           },
  72.           restrictSize: {
  73.             min: { width: 50, height: 50 },
  74.           },
  75.           inertia: true,
  76.         })
  77.         .on('resizestart', handleResizeStart)
  78.         .on('resizemove', handleResizeMove)
  79.         .on('resizeend', handleResizeEnd);
  80.  
  81.       const draggableRef = resizableRef.current.querySelector('.draggable-window');
  82.       if (draggableRef) {
  83.         interact(draggableRef)
  84.           .draggable({
  85.             inertia: true,
  86.             modifiers: [
  87.               interact.modifiers.restrictRect({
  88.                 restriction: 'parent',
  89.                 endOnly: true,
  90.               }),
  91.             ],
  92.             autoScroll: true,
  93.             listeners: {
  94.               move: dragMoveListener,
  95.             },
  96.           })
  97.           .on('dragstart', handleDragMoveStart)
  98.           .on('dragend', handleDragMoveEnd);
  99.       }
  100.     }
  101.   }, []);
  102.  
  103.   return (
  104.     <div className="window" ref={resizableRef} style={{ transform: 'translate(0px, 0px)' }}>
  105.       <div className="border-window">
  106.         <div className="draggable-window">Draggable frame {windowIndex}</div>
  107.         <embed src={iframeSrc} ref={iframeRef}></embed>
  108.       </div>
  109.     </div>
  110.   );
  111. };
  112.  
  113. export default ResizableIframe;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement