From 82633d9f2c6cf6baf5776c1c0de8e77afa27e6de Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Wed, 8 Jul 2026 11:23:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=96=E6=8B=BD=E7=BB=84=E4=BB=B6=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/devices/DraggablePanel.jsx | 133 +++++++++ src/components/devices/mowerAllView.jsx | 329 +++++++--------------- 2 files changed, 239 insertions(+), 223 deletions(-) create mode 100644 src/components/devices/DraggablePanel.jsx diff --git a/src/components/devices/DraggablePanel.jsx b/src/components/devices/DraggablePanel.jsx new file mode 100644 index 0000000..f1d0d19 --- /dev/null +++ b/src/components/devices/DraggablePanel.jsx @@ -0,0 +1,133 @@ +import React, { useRef, useCallback, useEffect, memo } from 'react'; + +// 独立的可拖拽面板组件 +export const DraggablePanel = memo(({ + initialX = 0, + initialY = 0, + width = 200, + height = 150, + bounds, + children, + handleContent, + onPositionChange +}) => { + const panelRef = useRef(null); + const dragRef = useRef({ + isDragging: false, + startX: 0, + startY: 0, + originX: initialX, + originY: initialY + }); + const positionRef = useRef({ x: initialX, y: initialY }); + + // 初始化位置 + useEffect(() => { + if (panelRef.current) { + panelRef.current.style.transform = `translate(${initialX}px, ${initialY}px)`; + } + positionRef.current = { x: initialX, y: initialY }; + }, [initialX, initialY]); + + // 鼠标移动处理 + const handleMouseMove = useCallback((e) => { + const drag = dragRef.current; + if (!drag.isDragging || !panelRef.current) return; + + let newX = drag.originX + (e.clientX - drag.startX); + let newY = drag.originY + (e.clientY - drag.startY); + + // 边界限制 + if (bounds) { + newX = Math.max(0, Math.min(newX, bounds.width - width)); + newY = Math.max(0, Math.min(newY, bounds.height - height)); + } + + // 直接更新 DOM,性能最优 + panelRef.current.style.transform = `translate(${newX}px, ${newY}px)`; + positionRef.current = { x: newX, y: newY }; + + // 可选的位置变化回调 + if (onPositionChange) { + onPositionChange({ x: newX, y: newY }); + } + }, [bounds, width, height, onPositionChange]); + + const handleMouseUp = useCallback(() => { + dragRef.current.isDragging = false; + }, []); + + // 全局鼠标事件 + useEffect(() => { + window.addEventListener('mousemove', handleMouseMove); + window.addEventListener('mouseup', handleMouseUp); + return () => { + window.removeEventListener('mousemove', handleMouseMove); + window.removeEventListener('mouseup', handleMouseUp); + }; + }, [handleMouseMove, handleMouseUp]); + + // 开始拖拽 + const handleMouseDown = useCallback((e) => { + e.preventDefault(); + e.stopPropagation(); + dragRef.current = { + isDragging: true, + startX: e.clientX, + startY: e.clientY, + originX: positionRef.current.x, + originY: positionRef.current.y + }; + }, []); + + return ( +
+ {/* 拖拽手柄 */} + {handleContent && ( +
+ {handleContent} +
+ )} + + {/* 子内容 */} +
+ {children} +
+
+ ); +}); + +export default DraggablePanel; diff --git a/src/components/devices/mowerAllView.jsx b/src/components/devices/mowerAllView.jsx index 18553fe..7ab78eb 100644 --- a/src/components/devices/mowerAllView.jsx +++ b/src/components/devices/mowerAllView.jsx @@ -1,69 +1,54 @@ -import React, { useState, useRef, useEffect, useCallback, memo } from 'react'; +import React, { useState, useRef, useEffect, memo } from 'react'; import VideoPlayer from '../VideoPlayer.tsx'; +import DraggablePanel from './DraggablePanel.jsx'; -// 单独抽离视频面板,memo 避免无关重渲染 -const VideoPanel = memo(({ - target, +// 视频子面板,使用 memo 避免不必要的重渲染 +const VideoSubPanel = memo(({ idx, label, + x, + y, width, height, - pos, - isDraggingAny, - dragTarget, - onMouseDown, + bounds, serialNumber, robotVideo, error, ready }) => { - // 空值直接不渲染,防止 pos.x 报错 - if (!pos) return null; - - const isSelfDragging = isDraggingAny && dragTarget === target; - + const hasVideo = ready && !error; + if (!hasVideo) return null; return ( -
+ + ⋮⋮ {label} + +
+ + + } + > +
- {/* 拖拽手柄 */} -
onMouseDown(e, target)} - > - - ⋮⋮ {label} - -
- -
+ }}>
-
+ ); }); export function MowerAllView({ serialNumber, robotVideo, error, ready, statusData }) { const containerRef = useRef(null); - // 拖拽临时状态,仅拖动过程使用 - const dragInfoRef = useRef({ - isDragging: false, - target: null, - startX: 0, - startY: 0, - originX: 0, - originY: 0 - }); + const [panelSize, setPanelSize] = useState(null); + const [initialPositions, setInitialPositions] = useState(null); + const [containerBounds, setContainerBounds] = useState(null); + const [initialized, setInitialized] = useState(false); - // 面板尺寸 state(初始化一次) - const [panelSize, setPanelSize] = useState({ - leftWidth: 200, - leftHeight: 150, - rightWidth: 200, - rightHeight: 150, - backWidth: 300, - backHeight: 150 - }); - - // 面板位置 state,拖拽只更新对应一项,局部渲染 - const [panelPositions, setPanelPositions] = useState({ - left: null, - right: null, - back: null - }); - - // 全局鼠标移动统一处理,只维护一份逻辑 - const globalMouseMove = useCallback((e) => { - const drag = dragInfoRef.current; - if (!drag.isDragging || !containerRef.current) return; - - const containerRect = containerRef.current.getBoundingClientRect(); - const deltaX = e.clientX - drag.startX; - const deltaY = e.clientY - drag.startY; - - let newX = drag.originX + deltaX; - let newY = drag.originY + deltaY; - let maxW, maxH; - - switch (drag.target) { - case 'left': - maxW = panelSize.leftWidth; - maxH = panelSize.leftHeight; - break; - case 'right': - maxW = panelSize.rightWidth; - maxH = panelSize.rightHeight; - break; - case 'back': - maxW = panelSize.backWidth; - maxH = panelSize.backHeight; - break; - default: - return; - } - - // 边界约束 - newX = Math.max(0, Math.min(newX, containerRect.width - maxW)); - newY = Math.max(0, Math.min(newY, containerRect.height - maxH)); - - // 仅更新当前拖拽面板位置,不刷新全部组件 - setPanelPositions(prev => ({ - ...prev, - [drag.target]: { x: newX, y: newY } - })); - }, [panelSize]); - - const globalMouseUp = useCallback(() => { - dragInfoRef.current.isDragging = false; - dragInfoRef.current.target = null; - }, []); - - // 全局一次性挂载鼠标事件,组件销毁解绑 + // 初始化尺寸和位置 useEffect(() => { - window.addEventListener('mousemove', globalMouseMove); - window.addEventListener('mouseup', globalMouseUp); - return () => { - window.removeEventListener('mousemove', globalMouseMove); - window.removeEventListener('mouseup', globalMouseUp); - }; - }, [globalMouseMove, globalMouseUp]); + if (!containerRef.current || initialized) return; - // 初始化尺寸与位置(只执行一次) - useEffect(() => { - if (!containerRef.current || panelPositions.left) return; const rect = containerRef.current.getBoundingClientRect(); - // 三块面板统一尺寸,消除后视大小不一致问题 - const baseW = Math.max(200, Math.floor(rect.width * 0.25)); - const baseH = Math.max(150, Math.floor(rect.height * 0.3)); - const size = { - leftWidth: baseW, - leftHeight: baseH, - rightWidth: baseW, - rightHeight: baseH, - backWidth: baseW, - backHeight: baseH - }; - setPanelSize(size); + // 统一三个面板的尺寸 + const w = Math.max(200, Math.floor(rect.width * 0.25)); + const h = Math.max(150, Math.floor(rect.height * 0.3)); - // 初始坐标直接赋值,不会为null - setPanelPositions({ + setPanelSize({ width: w, height: h }); + setContainerBounds({ width: rect.width, height: rect.height }); + + // 设置初始位置 + setInitialPositions({ left: { x: 8, y: 8 }, - right: { x: rect.width - baseW - 8, y: 8 }, - back: { x: (rect.width - baseW) / 2, y: rect.height - baseH - 8 } + right: { x: rect.width - w - 8, y: 8 }, + back: { x: (rect.width - w) / 2, y: rect.height - h - 8 } }); - }, [panelPositions.left]); - // 鼠标按下,开启拖拽 - const handleDragStart = useCallback((e, target) => { - e.preventDefault(); - e.stopPropagation(); - const pos = panelPositions[target]; - // 二次做空校验,防止初始化未完成拖拽触发 - if (!pos) return; - - dragInfoRef.current = { - isDragging: true, - target, - startX: e.clientX, - startY: e.clientY, - originX: pos.x, - originY: pos.y - }; - }, [panelPositions]); - - const dragState = dragInfoRef.current; + setInitialized(true); + }, [initialized]); return (
- {/* 底层前视画面,稳定不重渲染 */} + {/* 前视主画面 - 铺满 */}
+
- {/* 左视 */} - + {/* 三个可拖拽子面板 */} + {initialized && panelSize && initialPositions && containerBounds && ( + <> + {/* 左视 */} + - {/* 右视 */} - + {/* 右视 */} + - {/* 后视 */} - + {/* 后视 */} + + + )} - {/* 底部状态面板 */} + {/* 状态详情面板 */} {statusData && (