Advertisement
PuriDevelopers

Undefined

Jan 24th, 2024
775
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 = (props) => {
  5.   const window_name = props.name;
  6.   const window_color = props.color;
  7.   const window_url = props.url || "https://gummysearch.com/?utm_source=iframetester.com";
  8.  
  9.   const resizableRefs = useRef<Array<HTMLDivElement | null>>([]);
  10.   const iframeRefs = useRef<Array<HTMLIFrameElement | null>>([]);
  11.  
  12.   const handleResizeStart = (event: any) => {
  13.     const target = event.target;
  14.     target.setAttribute('data-x', target.offsetLeft);
  15.     target.setAttribute('data-y', target.offsetTop);
  16.  
  17.     const index = resizableRefs.current.indexOf(target);
  18.     iframeRefs.current[index]?.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.     const index = resizableRefs.current.indexOf(target);
  27.     if (resizableRefs.current[index]) {
  28.       resizableRefs.current[index].style.width = event.rect.width + 'px';
  29.       resizableRefs.current[index].style.height = event.rect.height + 'px';
  30.  
  31.       const newX = x + event.deltaRect.left;
  32.       const newY = y + event.deltaRect.top;
  33.  
  34.       resizableRefs.current[index].style.transform = `translate(${newX}px, ${newY}px)`;
  35.  
  36.       target.setAttribute('data-x', newX.toString());
  37.       target.setAttribute('data-y', newY.toString());
  38.     }
  39.   };
  40.  
  41.   const handleResizeEnd = () => {
  42.     iframeRefs.current.forEach((iframe) => {
  43.       if (iframe) {
  44.         iframe.style.setProperty('pointer-events', 'auto');
  45.       }
  46.     });
  47.   };
  48.  
  49.   useEffect(() => {
  50.     resizableRefs.current.forEach((resizableRef, index) => {
  51.       if (resizableRef) {
  52.         interact(resizableRef)
  53.           .resizable({
  54.             edges: { bottom: true, right: true },
  55.             restrictEdges: {
  56.               outer: 'parent',
  57.               endOnly: true,
  58.             },
  59.             restrictSize: {
  60.               min: { width: 50, height: 50 },
  61.             },
  62.             inertia: true,
  63.           })
  64.           .on('resizestart', handleResizeStart)
  65.           .on('resizemove', handleResizeMove)
  66.           .on('resizeend', handleResizeEnd);
  67.       }
  68.  
  69.       const draggableRef = resizableRef?.querySelector('.draggable-window');
  70.       const iframeRef = resizableRef?.querySelector('embed');
  71.  
  72.       if (draggableRef && iframeRef) {
  73.         interact(draggableRef)
  74.           .draggable({
  75.             inertia: true,
  76.             modifiers: [
  77.               interact.modifiers.restrictRect({
  78.                 restriction: 'parent',
  79.                 endOnly: true,
  80.               }),
  81.             ],
  82.             autoScroll: true,
  83.             listeners: {
  84.               move: (event) => dragMoveListener(event, index),
  85.             },
  86.           })
  87.           .on('dragstart', () => handleDragMoveStart(index))
  88.           .on('dragend', handleDragMoveEnd);
  89.       }
  90.     });
  91.   }, []);
  92.  
  93.   const handleDragMoveStart = (index: number) => {
  94.     iframeRefs.current[index]?.style.setProperty('pointer-events', 'none');
  95.   };
  96.  
  97.   const handleDragMoveEnd = () => {
  98.     iframeRefs.current.forEach((iframe) => {
  99.       if (iframe) {
  100.         iframe.style.setProperty('pointer-events', 'auto');
  101.       }
  102.     });
  103.   };
  104.  
  105.   const dragMoveListener = (event: any, index: number) => {
  106.     const target = resizableRefs.current[index];
  107.     const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
  108.     const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
  109.  
  110.     target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
  111.  
  112.     target.setAttribute('data-x', x.toString());
  113.     target.setAttribute('data-y', y.toString());
  114.   };
  115.  
  116.   const window_exit = (event: any) => {
  117.     event.target.parentElement.parentElement.parentElement.parentElement.remove();
  118.   };
  119.   const window_maximize = (event: any) => {
  120.     event.target.parentElement.parentElement.parentElement.parentElement.classList.toggle("maximized");
  121.   }
  122.   const window_handleMoveStart = (event: any) => {
  123.     console.log("Start Move");
  124.     var container = document.getElementsByClassName('window');
  125.     for(var i = 0; i < container.length; i++) {
  126.       container[i].classList.remove("moved");
  127.     }
  128.     event.target.parentElement.parentElement.classList.add("moved");
  129.   }
  130.   const window_handleMoveStop = (event: any) => {
  131.     console.log("Stop Move");
  132.     // event.target.parentElement.parentElement.classList.remove("moved");
  133.   }
  134.   const soon = (event: any) => {
  135.     console.log("Function not implemented :(");
  136.   }
  137.  
  138.  
  139.   return (
  140.     <>
  141.       <div className="window" ref={(el) => (resizableRefs.current[0] = el)} style={{ transform: 'translate(0px, 0px)' }} onMouseDown={window_handleMoveStart} onMouseUp={window_handleMoveStop}>
  142.         <div className="border-window" style={{ borderColor: window_color }}>
  143.           <div className="draggable-window" style={{ backgroundColor: window_color }}>
  144.             {window_name}
  145.             <div className="actions">
  146.               <div id='a1' onClick={soon}></div>
  147.               <div id='a2' onClick={window_maximize}></div>
  148.               <div id='a3' onClick={window_exit}></div>
  149.             </div>
  150.           </div>
  151.           <embed src={window_url} ref={(el) => (iframeRefs.current[0] = el)}></embed>
  152.         </div>
  153.       </div>
  154.  
  155.     </>
  156.   );
  157. };
  158.  
  159. export default ResizableIframe;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement