2024-03-25 01:22:49 +08:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2023-12-06 02:15:25 +08:00
|
|
|
|
|
|
|
const useCursor = (publishCursorUpdate, whiteboardId) => {
|
2024-05-07 20:13:41 +08:00
|
|
|
const [cursorPosition, setCursorPosition] = useState({ x: '', y: '' });
|
2023-12-06 02:15:25 +08:00
|
|
|
|
|
|
|
const updateCursorPosition = (newX, newY) => {
|
|
|
|
setCursorPosition({ x: newX, y: newY });
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-07 20:13:41 +08:00
|
|
|
if (!cursorPosition || cursorPosition.x === '' || cursorPosition.y === '') {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
publishCursorUpdate({
|
2024-03-12 23:03:43 +08:00
|
|
|
whiteboardId,
|
2023-12-06 02:15:25 +08:00
|
|
|
xPercent: cursorPosition?.x,
|
|
|
|
yPercent: cursorPosition?.y,
|
|
|
|
});
|
|
|
|
}, [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,
|
|
|
|
whiteboardToolbarAutoHide,
|
|
|
|
animations,
|
|
|
|
cursorPosition,
|
|
|
|
updateCursorPosition,
|
2024-01-09 22:31:08 +08:00
|
|
|
toggleToolsAnimations,
|
|
|
|
currentPresentationPage,
|
2024-02-01 02:53:05 +08:00
|
|
|
zoomChanger,
|
2024-03-25 01:22:49 +08:00
|
|
|
setIsMouseDown,
|
|
|
|
setIsWheelZoom,
|
|
|
|
setWheelZoomTimeout,
|
2023-12-06 02:15:25 +08:00
|
|
|
}) => {
|
|
|
|
|
|
|
|
const timeoutIdRef = React.useRef();
|
|
|
|
|
|
|
|
const handleMouseUp = () => {
|
|
|
|
if (timeoutIdRef.current) {
|
|
|
|
clearTimeout(timeoutIdRef.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutIdRef.current = setTimeout(() => {
|
2024-03-25 01:22:49 +08:00
|
|
|
setIsMouseDown(false);
|
2023-12-06 02:15:25 +08:00
|
|
|
}, 1000);
|
2024-03-25 01:22:49 +08:00
|
|
|
|
2024-06-18 03:57:57 +08:00
|
|
|
tlEditorRef?.current?.updateInstanceState({ canMoveCamera: true, isReadonly: false });
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
2024-06-15 00:24:03 +08:00
|
|
|
const handleMouseDownWhiteboard = (event) => {
|
2024-03-25 01:22:49 +08:00
|
|
|
if (!isPresenter && !hasWBAccess) {
|
2024-06-18 03:57:57 +08:00
|
|
|
let updateProps = { isReadonly: false };
|
2024-03-25 01:22:49 +08:00
|
|
|
|
|
|
|
if (event.button === 1) {
|
|
|
|
updateProps.canMoveCamera = false;
|
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
tlEditorRef?.current?.updateInstanceState(updateProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsMouseDown(true);
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
2024-06-15 00:24:03 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const handleMouseEnter = () => {
|
2024-03-25 01:22:49 +08:00
|
|
|
if (whiteboardToolbarAutoHide) {
|
2023-12-06 02:15:25 +08:00
|
|
|
toggleToolsAnimations(
|
|
|
|
"fade-out",
|
|
|
|
"fade-in",
|
|
|
|
animations ? ".3s" : "0s",
|
|
|
|
hasWBAccess || isPresenter
|
|
|
|
);
|
2024-03-25 01:22:49 +08:00
|
|
|
}
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
|
|
|
if (whiteboardToolbarAutoHide) {
|
|
|
|
toggleToolsAnimations(
|
|
|
|
"fade-in",
|
|
|
|
"fade-out",
|
|
|
|
animations ? "3s" : "0s",
|
|
|
|
hasWBAccess || isPresenter
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
updateCursorPosition(-1, -1);
|
|
|
|
}, 150);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseWheel = (event) => {
|
2024-02-01 02:53:05 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2024-03-16 04:11:56 +08:00
|
|
|
if (!tlEditorRef.current || !isPresenter || !currentPresentationPage) {
|
2023-12-06 02:15:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
setIsWheelZoom(true);
|
2024-01-09 22:31:08 +08:00
|
|
|
|
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
|
|
|
|
2024-04-05 23:14:08 +08:00
|
|
|
const { x: cx, y: cy, z: cz } = tlEditorRef.current.getCamera();
|
2023-12-06 02:15:25 +08:00
|
|
|
|
2024-04-05 23:14:08 +08:00
|
|
|
let currentZoomLevel = tlEditorRef.current.getCamera().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
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
// 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;
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
const nextCamera = {
|
2024-03-25 01:22:49 +08:00
|
|
|
x: cx + deltaX,
|
|
|
|
y: cy + deltaY,
|
2024-02-01 02:53:05 +08:00
|
|
|
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
|
2024-04-05 23:14:08 +08:00
|
|
|
const { maxX, maxY, minX, minY } = tlEditorRef.current.getViewportPageBounds();
|
2024-01-09 22:31:08 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-03-25 01:22:49 +08:00
|
|
|
setWheelZoomTimeout();
|
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;
|
|
|
|
|
2024-06-15 00:24:03 +08:00
|
|
|
window.addEventListener('mousedown', handleMouseDownWindow);
|
|
|
|
|
2023-12-06 02:15:25 +08:00
|
|
|
if (whiteboardElement) {
|
2024-06-15 00:24:03 +08:00
|
|
|
whiteboardElement.addEventListener("mousedown", handleMouseDownWhiteboard);
|
2023-12-06 02:15:25 +08:00
|
|
|
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) {
|
2024-06-15 00:24:03 +08:00
|
|
|
whiteboardElement.removeEventListener("mousedown", handleMouseDownWhiteboard);
|
2023-12-06 02:15:25 +08:00
|
|
|
whiteboardElement.removeEventListener("mouseup", handleMouseUp);
|
|
|
|
whiteboardElement.removeEventListener("mouseenter", handleMouseEnter);
|
|
|
|
whiteboardElement.removeEventListener("mouseleave", handleMouseLeave);
|
|
|
|
whiteboardElement.removeEventListener("wheel", handleMouseWheel);
|
|
|
|
}
|
2024-06-15 00:24:03 +08:00
|
|
|
|
|
|
|
window.removeEventListener('mousedown', handleMouseDownWindow);
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
2024-03-25 01:22:49 +08:00
|
|
|
}, [
|
|
|
|
whiteboardRef,
|
2024-06-15 00:24:03 +08:00
|
|
|
tlEditorRef,
|
2024-06-18 04:25:56 +08:00
|
|
|
handleMouseDownWhiteboard,
|
|
|
|
handleMouseUp,
|
|
|
|
handleMouseEnter,
|
|
|
|
handleMouseLeave,
|
|
|
|
handleMouseWheel,
|
2024-03-25 01:22:49 +08:00
|
|
|
]);
|
2023-12-06 02:15:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
useMouseEvents,
|
|
|
|
useCursor,
|
2023-12-30 21:30:54 +08:00
|
|
|
};
|