react可视化编辑器 第三章 限制移动范围

在这里插入图片描述
代码

import React, {
  useState,
  DragEvent,
  useRef,
  useEffect,
  MouseEvent,
} from 'react';
// import { throttle } from 'lodash';

interface Demo {
  id: number;
  x: number;
  y: number;
}

const App: React.FC = () => {
  const [demos, setDemos] = useState<Demo[]>([]);
  // let currentDiv: HTMLDivElement | null = null;
  const divRef = useRef<HTMLDivElement | null>(null);

  const handleDragStart = (e: DragEvent<HTMLDivElement>, id: number) => {
    e.dataTransfer.setData('text/plain', id.toString());

    const offsetX = e.clientX - e.currentTarget.getBoundingClientRect().left;
    const offsetY = e.clientY - e.currentTarget.getBoundingClientRect().top;
    e.dataTransfer.setData('offsetX', offsetX.toString());
    e.dataTransfer.setData('offsetY', offsetY.toString());
  };

  const handleDrop = (e: DragEvent<HTMLDivElement>) => {
    e.preventDefault();

    const clientX = e.clientX;
    const clientY = e.clientY;
    const contentStyle = document
      .getElementById('content')
      .getBoundingClientRect();
    const offsetX = e.dataTransfer.getData('offsetX');
    const offsetY = e.dataTransfer.getData('offsetY');
    const x = clientX - contentStyle.left - offsetX;
    const y = clientY - contentStyle.top - offsetY;
    const newDemo: Demo = { x, y, id: +new Date() };
    setDemos([...demos, newDemo]);
  };

  const handleDragOver = (e: DragEvent<HTMLDivElement>) => {
    e.preventDefault();
  };
  
  const handleMouseDown = (e: React.MouseEvent<HTMLDivElement>) => {
    e.stopPropagation();

    divRef.current = e.currentTarget;

    let { top, left } = divRef.current.style;
    console.info('top, left', top, left);

    // 如果直接修改属性,值的类型会变为字符串,所以要转为数值型
    const startTop = top ? Number(top.replace('px', '')) : 0;
    const startLeft = left ? Number(left.replace('px', '')) : 0;

    const startY = e.clientY;
    const startX = e.clientX;

    const move = (moveEvent: { clientX: number; clientY: number }) => {
      if (!divRef.current) return; // 检查divRef.current是否存在

      const currX = moveEvent.clientX;
      const currY = moveEvent.clientY;
      console.info('move', currX, currY);

      const newTop = currY - startY + startTop;
      const newLeft = currX - startX + startLeft;

      // 获取父元素的边界
      const parent = divRef.current.parentElement;
      if (!parent) return;

      const parentRect = parent.getBoundingClientRect();
      const divRect = divRef.current.getBoundingClientRect();

      // 限制div不超过父元素的边界
      const maxTop = parentRect.height - divRect.height;
      const maxLeft = parentRect.width - divRect.width;

      top = `${Math.min(Math.max(newTop, 0), maxTop)}px`;
      left = `${Math.min(Math.max(newLeft, 0), maxLeft)}px`;

      // 修改当前组件样式
      divRef.current.style.left = left;
      divRef.current.style.top = top;
    };

    const up = () => {
      document.removeEventListener('mousemove', move);
      document.removeEventListener('mouseup', up);
      divRef.current = null; // 清除对元素的引用
    };

    document.addEventListener('mousemove', move);
    document.addEventListener('mouseup', up);
  };

  return (
    <div>
      <div
        id="demo"
        draggable
        onDragStart={(e) => handleDragStart(e, 1)}
        style={{
          width: '100px',
          height: '100px',
          backgroundColor: 'red',
          margin: '30px',
          cursor: 'pointer',
        }}
      >
        demo2
      </div>
      <div
        id="content"
        onDrop={handleDrop}
        onDragOver={handleDragOver}
        style={{
          width: '300px',
          height: '300px',
          margin: '30px',
          backgroundColor: 'blue',
          position: 'relative',
        }}
      >
        content
        {demos.map((demo) => (
          <div
            onMouseDown={handleMouseDown}
            key={demo.id}
            style={{
              width: '100px',
              height: '100px',
              backgroundColor: 'red',
              cursor: 'pointer',
              position: 'absolute',
              left: `${demo.x}px`,
              top: `${demo.y}px`,
            }}
          >
            demo {demo.id}
          </div>
        ))}
      </div>
    </div>
  );
};

export default App;

相关推荐

  1. 数据—pyecharts

    2024-03-17 05:28:06       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-17 05:28:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-17 05:28:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-17 05:28:06       82 阅读
  4. Python语言-面向对象

    2024-03-17 05:28:06       91 阅读

热门阅读

  1. Spring-Kafka笔记整理

    2024-03-17 05:28:06       43 阅读
  2. ZooKeeper

    ZooKeeper

    2024-03-17 05:28:06      42 阅读
  3. Spring Boot集成mapstruct快速入门指南

    2024-03-17 05:28:06       44 阅读
  4. 封装promise请求方式

    2024-03-17 05:28:06       48 阅读
  5. OLLAMA:如何像云端一样运行本地大语言模型

    2024-03-17 05:28:06       44 阅读
  6. alibaba cloud linux 3 安装 psql 16

    2024-03-17 05:28:06       46 阅读
  7. Python强大的库和框架——TensorFlow

    2024-03-17 05:28:06       44 阅读
  8. springBoot整合Redis(四、整合redis 实现分布式锁)

    2024-03-17 05:28:06       45 阅读
  9. lammps从NVT或者NPT切换到NVE时温度持续上升

    2024-03-17 05:28:06       42 阅读