拖拽组件封装

This commit is contained in:
mmc
2026-07-08 11:23:03 +08:00
parent 083a28b6c7
commit 82633d9f2c
2 changed files with 239 additions and 223 deletions

View File

@@ -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 (
<div
style={{
position: 'absolute',
width,
height,
zIndex: 10,
<DraggablePanel
initialX={x}
initialY={y}
width={width}
height={height}
bounds={bounds}
handleContent={
<div style={{
width: '60px',
height: '100%',
background: 'rgba(0,0,0,0.5)',
display: 'flex',
alignItems: 'center',
borderRadius: '8px',
}}>
<span style={{ color: '#e9f0eb', fontSize: 16, fontWeight: 'bold' }}>
⋮⋮ {label}
</span>
</div>
}
>
<div style={{
width: '100%',
height: '100%',
border: '1px solid #fff3',
borderRadius: 8,
overflow: 'hidden',
backgroundColor: '#000',
// GPU 加速,不触发页面回流
transform: `translate(${pos.x}px, ${pos.y}px)`,
willChange: 'transform',
transition: isSelfDragging ? 'none' : 'transform 0.1s ease-out',
pointerEvents: 'auto'
}}
>
{/* 拖拽手柄 */}
<div
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 28,
background: 'linear-gradient(to bottom, rgba(0,0,0,0.8), rgba(0,0,0,0.6))',
zIndex: 5,
cursor: isSelfDragging ? 'grabbing' : 'grab',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
userSelect: 'none'
}}
onMouseDown={(e) => onMouseDown(e, target)}
>
<span style={{ color: '#fff', fontSize: 12, fontWeight: 'bold' }}>
⋮⋮ {label}
</span>
</div>
<div style={{ position: 'absolute', top: 28, left: 0, right: 0, bottom: 0 }}>
}}>
<VideoPlayer
deviceId={serialNumber}
idx={idx}
@@ -73,140 +58,39 @@ const VideoPanel = memo(({
ready={ready}
/>
</div>
</div>
</DraggablePanel>
);
});
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 (
<div
@@ -215,11 +99,10 @@ export function MowerAllView({ serialNumber, robotVideo, error, ready, statusDat
position: 'relative',
width: '100%',
height: '100%',
overflow: 'hidden',
userSelect: dragState.isDragging ? 'none' : 'auto'
overflow: 'hidden'
}}
>
{/* 底层前视画面,稳定不重渲染 */}
{/* 前视主画面 - 铺满 */}
<div style={{ position: 'absolute', inset: 0, zIndex: 1 }}>
<VideoPlayer
deviceId={serialNumber}
@@ -229,60 +112,60 @@ export function MowerAllView({ serialNumber, robotVideo, error, ready, statusDat
error={error}
ready={ready}
/>
</div>
{/* 左视 */}
<VideoPanel
target="left"
idx={3}
label="左视"
width={panelSize.leftWidth}
height={panelSize.leftHeight}
pos={panelPositions.left}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 三个可拖拽子面板 */}
{initialized && panelSize && initialPositions && containerBounds && (
<>
{/* 左视 */}
<VideoSubPanel
idx={3}
label="左视"
x={initialPositions.left.x}
y={initialPositions.left.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 右视 */}
<VideoPanel
target="right"
idx={4}
label="右视"
width={panelSize.rightWidth}
height={panelSize.rightHeight}
pos={panelPositions.right}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 右视 */}
<VideoSubPanel
idx={4}
label="右视"
x={initialPositions.right.x}
y={initialPositions.right.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 后视 */}
<VideoPanel
target="back"
idx={2}
label="后视"
width={panelSize.backWidth}
height={panelSize.backHeight}
pos={panelPositions.back}
isDraggingAny={dragState.isDragging}
dragTarget={dragState.target}
onMouseDown={handleDragStart}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
{/* 后视 */}
<VideoSubPanel
idx={2}
label="后视"
x={initialPositions.back.x}
y={initialPositions.back.y}
width={panelSize.width}
height={panelSize.height}
bounds={containerBounds}
serialNumber={serialNumber}
robotVideo={robotVideo}
error={error}
ready={ready}
/>
</>
)}
{/* 底部状态面板 */}
{/* 状态详情面板 */}
{statusData && (
<div style={{
position: 'absolute',