bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/whiteboard/hooks.js

216 lines
7.0 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
const useCursor = (publishCursorUpdate, whiteboardId) => {
const [cursorPosition, setCursorPosition] = useState({ x: '', y: '' });
const updateCursorPosition = (newX, newY) => {
setCursorPosition({ x: newX, y: newY });
};
useEffect(() => {
if (!cursorPosition || cursorPosition.x === '' || cursorPosition.y === '') {
return;
}
publishCursorUpdate({
2024-03-12 23:03:43 +08:00
whiteboardId,
xPercent: cursorPosition?.x,
yPercent: cursorPosition?.y,
});
}, [cursorPosition, publishCursorUpdate, whiteboardId]);
return [cursorPosition, updateCursorPosition];
};
const useMouseEvents = ({ whiteboardRef, tlEditorRef, isWheelZoomRef, initialZoomRef }, {
isPresenter,
hasWBAccess,
whiteboardToolbarAutoHide,
animations,
cursorPosition,
updateCursorPosition,
toggleToolsAnimations,
currentPresentationPage,
zoomChanger,
setIsMouseDown,
setIsWheelZoom,
setWheelZoomTimeout,
}) => {
const timeoutIdRef = React.useRef();
const handleMouseUp = () => {
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
}
timeoutIdRef.current = setTimeout(() => {
setIsMouseDown(false);
}, 1000);
2024-06-18 03:57:57 +08:00
tlEditorRef?.current?.updateInstanceState({ canMoveCamera: true, isReadonly: false });
};
const handleMouseDownWhiteboard = (event) => {
if (!isPresenter && !hasWBAccess) {
2024-06-18 03:57:57 +08:00
let updateProps = { isReadonly: false };
if (event.button === 1) {
updateProps.canMoveCamera = false;
}
tlEditorRef?.current?.updateInstanceState(updateProps);
}
setIsMouseDown(true);
};
const handleMouseDownWindow = (event) => {
const presentationInnerWrapper = document.getElementById('presentationInnerWrapper');
if (!(presentationInnerWrapper && presentationInnerWrapper.contains(event.target))) {
const editingShape = tlEditorRef.current?.getEditingShape();
if (editingShape) {
return tlEditorRef.current?.setEditingShape(null);
}
}
return undefined;
};
const handleMouseEnter = () => {
if (whiteboardToolbarAutoHide) {
toggleToolsAnimations(
"fade-out",
"fade-in",
animations ? ".3s" : "0s",
hasWBAccess || isPresenter
);
}
};
const handleMouseLeave = () => {
if (whiteboardToolbarAutoHide) {
toggleToolsAnimations(
"fade-in",
"fade-out",
animations ? "3s" : "0s",
hasWBAccess || isPresenter
);
}
setTimeout(() => {
updateCursorPosition(-1, -1);
}, 150);
};
const handleMouseWheel = (event) => {
event.preventDefault();
event.stopPropagation();
2024-03-16 04:11:56 +08:00
if (!tlEditorRef.current || !isPresenter || !currentPresentationPage) {
return;
}
setIsWheelZoom(true);
const MAX_ZOOM_FACTOR = 4; // Represents 400%
const MIN_ZOOM_FACTOR = 1; // Represents 100%
const ZOOM_IN_FACTOR = 0.1;
const ZOOM_OUT_FACTOR = 0.1;
const { x: cx, y: cy, z: cz } = tlEditorRef.current.getCamera();
let currentZoomLevel = tlEditorRef.current.getCamera().z / initialZoomRef.current;
if (event.deltaY < 0) {
currentZoomLevel = Math.min(currentZoomLevel + ZOOM_IN_FACTOR, MAX_ZOOM_FACTOR);
} else {
currentZoomLevel = Math.max(currentZoomLevel - ZOOM_OUT_FACTOR, MIN_ZOOM_FACTOR);
}
// Convert zoom level to a percentage for backend
const zoomPercentage = currentZoomLevel * 100;
zoomChanger(zoomPercentage);
// Calculate the new camera zoom factor
const newCameraZoomFactor = currentZoomLevel * initialZoomRef.current;
// Break down the calculations for deltaX
const scaleAdjustmentX = cursorPosition.x / newCameraZoomFactor - cursorPosition.x;
const zoomAdjustmentX = cursorPosition.x / cz - cursorPosition.x;
const deltaX = scaleAdjustmentX - zoomAdjustmentX;
// Break down the calculations for deltaY
const scaleAdjustmentY = cursorPosition.y / newCameraZoomFactor - cursorPosition.y;
const zoomAdjustmentY = cursorPosition.y / cz - cursorPosition.y;
const deltaY = scaleAdjustmentY - zoomAdjustmentY;
const nextCamera = {
x: cx + deltaX,
y: cy + deltaY,
z: newCameraZoomFactor,
};
tlEditorRef.current.setCamera(nextCamera, { duration: 175 });
if (isWheelZoomRef.currentTimeout) {
clearTimeout(isWheelZoomRef.currentTimeout);
}
setWheelZoomTimeout();
};
React.useEffect(() => {
if (whiteboardToolbarAutoHide) {
toggleToolsAnimations(
"fade-in",
"fade-out",
animations ? "3s" : "0s",
hasWBAccess || isPresenter
);
} else {
toggleToolsAnimations(
"fade-out",
"fade-in",
animations ? ".3s" : "0s",
hasWBAccess || isPresenter
);
}
}, [whiteboardToolbarAutoHide]);
React.useEffect(() => {
const whiteboardElement = whiteboardRef.current;
window.addEventListener('mousedown', handleMouseDownWindow);
if (whiteboardElement) {
whiteboardElement.addEventListener("mousedown", handleMouseDownWhiteboard);
whiteboardElement.addEventListener("mouseup", handleMouseUp);
whiteboardElement.addEventListener("mouseenter", handleMouseEnter);
whiteboardElement.addEventListener("mouseleave", handleMouseLeave);
whiteboardElement.addEventListener("wheel", handleMouseWheel, { passive: false, capture: true });
}
return () => {
if (whiteboardElement) {
whiteboardElement.removeEventListener("mousedown", handleMouseDownWhiteboard);
whiteboardElement.removeEventListener("mouseup", handleMouseUp);
whiteboardElement.removeEventListener("mouseenter", handleMouseEnter);
whiteboardElement.removeEventListener("mouseleave", handleMouseLeave);
whiteboardElement.removeEventListener("wheel", handleMouseWheel);
}
window.removeEventListener('mousedown', handleMouseDownWindow);
};
}, [
whiteboardRef,
tlEditorRef,
2024-06-18 04:25:56 +08:00
handleMouseDownWhiteboard,
handleMouseUp,
handleMouseEnter,
handleMouseLeave,
handleMouseWheel,
]);
};
export {
useMouseEvents,
useCursor,
2023-12-30 21:30:54 +08:00
};