Advertisement
PuriDevelopers

DraggableTest

Jan 20th, 2024 (edited)
976
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. const ResizableIframe: React.FC = () => {
  5.   const resizableRefs = useRef<Array<HTMLDivElement | null>>([]);
  6.   const iframeRefs = useRef<Array<HTMLIFrameElement | null>>([]);
  7.  
  8.   const handleResizeStart = (event: any) => {
  9.     const target = event.target;
  10.     target.setAttribute('data-x', target.offsetLeft);
  11.     target.setAttribute('data-y', target.offsetTop);
  12.  
  13.     const index = resizableRefs.current.indexOf(target);
  14.     iframeRefs.current[index]?.style.setProperty('pointer-events', 'none');
  15.   };
  16.  
  17.   const handleResizeMove = (event: any) => {
  18.     const target = event.target;
  19.     const x = parseFloat(target.getAttribute('data-x')) || 0;
  20.     const y = parseFloat(target.getAttribute('data-y')) || 0;
  21.  
  22.     const index = resizableRefs.current.indexOf(target);
  23.     if (resizableRefs.current[index]) {
  24.       resizableRefs.current[index].style.width = event.rect.width + 'px';
  25.       resizableRefs.current[index].style.height = event.rect.height + 'px';
  26.  
  27.       const newX = x + event.deltaRect.left;
  28.       const newY = y + event.deltaRect.top;
  29.  
  30.       resizableRefs.current[index].style.transform = `translate(${newX}px, ${newY}px)`;
  31.  
  32.       target.setAttribute('data-x', newX.toString());
  33.       target.setAttribute('data-y', newY.toString());
  34.     }
  35.   };
  36.  
  37.   const handleResizeEnd = () => {
  38.     iframeRefs.current.forEach((iframe) => {
  39.       if (iframe) {
  40.         iframe.style.setProperty('pointer-events', 'auto');
  41.       }
  42.     });
  43.   };
  44.  
  45.   useEffect(() => {
  46.     resizableRefs.current.forEach((resizableRef, index) => {
  47.       if (resizableRef) {
  48.         interact(resizableRef)
  49.           .resizable({
  50.             edges: { bottom: true, right: true },
  51.             restrictEdges: {
  52.               outer: 'parent',
  53.               endOnly: true,
  54.             },
  55.             restrictSize: {
  56.               min: { width: 50, height: 50 },
  57.             },
  58.             inertia: true,
  59.           })
  60.           .on('resizestart', handleResizeStart)
  61.           .on('resizemove', handleResizeMove)
  62.           .on('resizeend', handleResizeEnd);
  63.       }
  64.  
  65.       const draggableRef = resizableRef?.querySelector('.draggable-window');
  66.       const iframeRef = resizableRef?.querySelector('embed');
  67.  
  68.       if (draggableRef && iframeRef) {
  69.         interact(draggableRef)
  70.           .draggable({
  71.             inertia: true,
  72.             modifiers: [
  73.               interact.modifiers.restrictRect({
  74.                 restriction: 'parent',
  75.                 endOnly: true,
  76.               }),
  77.             ],
  78.             autoScroll: true,
  79.             listeners: {
  80.               move: (event) => dragMoveListener(event, index),
  81.             },
  82.           })
  83.           .on('dragstart', () => handleDragMoveStart(index))
  84.           .on('dragend', handleDragMoveEnd);
  85.       }
  86.     });
  87.   }, []);
  88.  
  89.   const handleDragMoveStart = (index: number) => {
  90.     iframeRefs.current[index]?.style.setProperty('pointer-events', 'none');
  91.   };
  92.  
  93.   const handleDragMoveEnd = () => {
  94.     iframeRefs.current.forEach((iframe) => {
  95.       if (iframe) {
  96.         iframe.style.setProperty('pointer-events', 'auto');
  97.       }
  98.     });
  99.   };
  100.  
  101.   const dragMoveListener = (event: any, index: number) => {
  102.     const target = resizableRefs.current[index];
  103.     const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
  104.     const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
  105.  
  106.     target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
  107.  
  108.     target.setAttribute('data-x', x.toString());
  109.     target.setAttribute('data-y', y.toString());
  110.   };
  111.  
  112.   return (
  113.     <>
  114.       <div className="window" ref={(el) => (resizableRefs.current[0] = el)} style={{ transform: 'translate(0px, 0px)' }}>
  115.         <div className="border-window">
  116.           <div className="draggable-window">Draggable frame 1</div>
  117.           <embed src="https://gummysearch.com/?utm_source=iframetester.com" ref={(el) => (iframeRefs.current[0] = el)}></embed>
  118.         </div>
  119.       </div>
  120.  
  121.       <div className="window" ref={(el) => (resizableRefs.current[1] = el)} style={{ transform: 'translate(0px, 0px)' }}>
  122.         <div className="border-window">
  123.           <div className="draggable-window">Draggable frame 2</div>
  124.           <embed src="https://gummysearch.com/?utm_source=iframetester.com" ref={(el) => (iframeRefs.current[1] = el)}></embed>
  125.         </div>
  126.       </div>
  127.       {/* Add more windows as needed */}
  128.     </>
  129.   );
  130. };
  131.  
  132. export default ResizableIframe;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement