2023-12-06 02:15:25 +08:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2024-01-09 23:02:32 +08:00
|
|
|
import {
|
2024-01-09 22:31:08 +08:00
|
|
|
HUNDRED_PERCENT,
|
|
|
|
MAX_PERCENT,
|
|
|
|
} from "/imports/utils/slideCalcUtils";
|
2023-12-06 02:15:25 +08:00
|
|
|
|
|
|
|
const useCursor = (publishCursorUpdate, whiteboardId) => {
|
|
|
|
const [cursorPosition, setCursorPosition] = useState({ x: -1, y: -1 });
|
|
|
|
|
|
|
|
const updateCursorPosition = (newX, newY) => {
|
|
|
|
setCursorPosition({ x: newX, y: newY });
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
publishCursorUpdate({
|
|
|
|
xPercent: cursorPosition?.x,
|
|
|
|
yPercent: cursorPosition?.y,
|
|
|
|
whiteboardId,
|
|
|
|
});
|
|
|
|
}, [cursorPosition, publishCursorUpdate, whiteboardId]);
|
|
|
|
|
|
|
|
return [cursorPosition, updateCursorPosition];
|
|
|
|
};
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
const useMouseEvents = ({ whiteboardRef, tlEditorRef, isWheelZoomRef, initialZoomRef }, {
|
2023-12-06 02:15:25 +08:00
|
|
|
isPresenter,
|
|
|
|
hasWBAccess,
|
|
|
|
isMouseDownRef,
|
|
|
|
whiteboardToolbarAutoHide,
|
|
|
|
animations,
|
|
|
|
publishCursorUpdate,
|
|
|
|
whiteboardId,
|
|
|
|
cursorPosition,
|
|
|
|
updateCursorPosition,
|
2024-01-09 22:31:08 +08:00
|
|
|
toggleToolsAnimations,
|
|
|
|
currentPresentationPage,
|
2024-02-01 02:53:05 +08:00
|
|
|
zoomChanger,
|
2023-12-06 02:15:25 +08:00
|
|
|
}) => {
|
|
|
|
|
|
|
|
const timeoutIdRef = React.useRef();
|
|
|
|
|
|
|
|
const handleMouseUp = () => {
|
|
|
|
if (!isPresenter && !hasWBAccess) {
|
|
|
|
tlEditorRef?.current?.updateInstanceState({ isReadonly: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeoutIdRef.current) {
|
|
|
|
clearTimeout(timeoutIdRef.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutIdRef.current = setTimeout(() => {
|
|
|
|
isMouseDownRef.current = false;
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseDown = () => {
|
|
|
|
!isPresenter &&
|
|
|
|
!hasWBAccess &&
|
|
|
|
tlEditorRef?.current?.updateInstanceState({ isReadonly: true });
|
|
|
|
|
|
|
|
isMouseDownRef.current = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseEnter = () => {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const handleMouseWheel = (event) => {
|
2024-02-01 02:53:05 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2023-12-06 02:15:25 +08:00
|
|
|
if (!tlEditorRef.current || !isPresenter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
isWheelZoomRef.current = true;
|
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
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;
|
2023-12-06 02:15:25 +08:00
|
|
|
|
|
|
|
const { x: cx, y: cy, z: cz } = tlEditorRef.current.camera;
|
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
let currentZoomLevel = tlEditorRef.current.camera.z / initialZoomRef.current;
|
2023-12-06 02:15:25 +08:00
|
|
|
if (event.deltaY < 0) {
|
2024-02-01 02:53:05 +08:00
|
|
|
currentZoomLevel = Math.min(currentZoomLevel + ZOOM_IN_FACTOR, MAX_ZOOM_FACTOR);
|
2023-12-06 02:15:25 +08:00
|
|
|
} else {
|
2024-02-01 02:53:05 +08:00
|
|
|
currentZoomLevel = Math.max(currentZoomLevel - ZOOM_OUT_FACTOR, MIN_ZOOM_FACTOR);
|
2023-12-06 02:15:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
// Convert zoom level to a percentage for backend
|
|
|
|
const zoomPercentage = currentZoomLevel * 100;
|
|
|
|
zoomChanger(zoomPercentage);
|
2024-01-09 22:31:08 +08:00
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
// Calculate the new camera zoom factor
|
|
|
|
const newCameraZoomFactor = currentZoomLevel * initialZoomRef.current;
|
2024-01-09 22:31:08 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const nextCamera = {
|
2024-02-01 02:53:05 +08:00
|
|
|
x: cx + (cursorPosition.x / newCameraZoomFactor - cursorPosition.x) - (cursorPosition.x / cz - cursorPosition.x),
|
|
|
|
y: cy + (cursorPosition.y / newCameraZoomFactor - cursorPosition.y) - (cursorPosition.y / cz - cursorPosition.y),
|
|
|
|
z: newCameraZoomFactor,
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
// Apply the bounds restriction logic after the camera has been updated
|
|
|
|
const { maxX, maxY, minX, minY } = tlEditorRef.current.viewportPageBounds;
|
|
|
|
const { scaledWidth, scaledHeight } = currentPresentationPage;
|
|
|
|
|
|
|
|
if (maxX > scaledWidth) {
|
|
|
|
nextCamera.x += maxX - scaledWidth;
|
|
|
|
}
|
|
|
|
if (maxY > scaledHeight) {
|
|
|
|
nextCamera.y += maxY - scaledHeight;
|
|
|
|
}
|
|
|
|
if (nextCamera.x > 0 || minX < 0) {
|
|
|
|
nextCamera.x = 0;
|
|
|
|
}
|
|
|
|
if (nextCamera.y > 0 || minY < 0) {
|
|
|
|
nextCamera.y = 0;
|
|
|
|
}
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
tlEditorRef.current.setCamera(nextCamera, { duration: 300 });
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
if (isWheelZoomRef.currentTimeout) {
|
|
|
|
clearTimeout(isWheelZoomRef.currentTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
isWheelZoomRef.currentTimeout = setTimeout(() => {
|
|
|
|
isWheelZoomRef.current = false;
|
|
|
|
}, 300);
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
2024-01-09 22:31:08 +08:00
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (whiteboardElement) {
|
|
|
|
whiteboardElement.addEventListener("mousedown", handleMouseDown);
|
|
|
|
whiteboardElement.addEventListener("mouseup", handleMouseUp);
|
|
|
|
whiteboardElement.addEventListener("mouseenter", handleMouseEnter);
|
|
|
|
whiteboardElement.addEventListener("mouseleave", handleMouseLeave);
|
2023-12-12 03:31:21 +08:00
|
|
|
whiteboardElement.addEventListener("wheel", handleMouseWheel, { passive: false, capture: true });
|
2023-12-06 02:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (whiteboardElement) {
|
|
|
|
whiteboardElement.removeEventListener("mousedown", handleMouseDown);
|
|
|
|
whiteboardElement.removeEventListener("mouseup", handleMouseUp);
|
|
|
|
whiteboardElement.removeEventListener("mouseenter", handleMouseEnter);
|
|
|
|
whiteboardElement.removeEventListener("mouseleave", handleMouseLeave);
|
|
|
|
whiteboardElement.removeEventListener("wheel", handleMouseWheel);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [whiteboardRef, tlEditorRef, handleMouseDown, handleMouseUp, handleMouseEnter, handleMouseLeave, handleMouseWheel]);
|
|
|
|
};
|
|
|
|
|
2024-02-01 02:53:05 +08:00
|
|
|
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
export {
|
|
|
|
useMouseEvents,
|
|
|
|
useCursor,
|
2023-12-30 21:30:54 +08:00
|
|
|
};
|