2023-03-01 11:32:24 +08:00
|
|
|
import * as React from 'react';
|
2023-03-10 19:30:46 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2023-01-12 03:32:03 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
2023-03-10 19:30:46 +08:00
|
|
|
import Cursor from './cursor/component';
|
|
|
|
import PositionLabel from './position-label/component';
|
2022-11-05 03:29:30 +08:00
|
|
|
|
|
|
|
const XS_OFFSET = 8;
|
|
|
|
const SMALL_OFFSET = 18;
|
|
|
|
const XL_OFFSET = 85;
|
|
|
|
const BOTTOM_CAM_HANDLE_HEIGHT = 10;
|
|
|
|
const PRES_TOOLBAR_HEIGHT = 35;
|
|
|
|
|
2023-01-12 03:32:03 +08:00
|
|
|
const baseName = Meteor.settings.public.app.cdn + Meteor.settings.public.app.basename;
|
|
|
|
const makeCursorUrl = (filename) => `${baseName}/resources/images/whiteboard-cursor/${filename}`;
|
|
|
|
|
|
|
|
const TOOL_CURSORS = {
|
2023-03-06 07:20:56 +08:00
|
|
|
select: 'default',
|
2023-01-12 03:32:03 +08:00
|
|
|
erase: 'none',
|
|
|
|
arrow: 'none',
|
|
|
|
draw: `url('${makeCursorUrl('pencil.png')}') 2 22, default`,
|
|
|
|
rectangle: `url('${makeCursorUrl('square.png')}'), default`,
|
|
|
|
ellipse: `url('${makeCursorUrl('ellipse.png')}'), default`,
|
|
|
|
triangle: `url('${makeCursorUrl('triangle.png')}'), default`,
|
|
|
|
line: `url('${makeCursorUrl('line.png')}'), default`,
|
|
|
|
text: `url('${makeCursorUrl('text.png')}'), default`,
|
|
|
|
sticky: `url('${makeCursorUrl('square.png')}'), default`,
|
2023-03-10 19:30:46 +08:00
|
|
|
pan: 'grab',
|
2023-03-01 10:03:33 +08:00
|
|
|
grabbing: 'grabbing',
|
2023-02-11 02:30:24 +08:00
|
|
|
moving: 'move',
|
2023-01-12 03:32:03 +08:00
|
|
|
};
|
2023-03-10 19:30:46 +08:00
|
|
|
const Cursors = (props) => {
|
2023-02-07 20:52:24 +08:00
|
|
|
const cursorWrapper = React.useRef();
|
2022-11-05 03:29:30 +08:00
|
|
|
const [active, setActive] = React.useState(false);
|
|
|
|
const [pos, setPos] = React.useState({ x: 0, y: 0 });
|
|
|
|
const {
|
|
|
|
whiteboardId,
|
|
|
|
otherCursors,
|
|
|
|
currentUser,
|
2023-02-14 03:33:06 +08:00
|
|
|
currentPoint,
|
|
|
|
tldrawCamera,
|
2022-11-05 03:29:30 +08:00
|
|
|
publishCursorUpdate,
|
|
|
|
children,
|
|
|
|
isViewersCursorLocked,
|
|
|
|
hasMultiUserAccess,
|
|
|
|
isMultiUserActive,
|
|
|
|
isPanning,
|
2023-02-11 02:30:24 +08:00
|
|
|
isMoving,
|
2023-01-12 03:32:03 +08:00
|
|
|
currentTool,
|
2023-03-01 10:03:33 +08:00
|
|
|
disabledPan,
|
2022-11-05 03:29:30 +08:00
|
|
|
} = props;
|
|
|
|
|
2023-03-01 10:03:33 +08:00
|
|
|
const [panGrabbing, setPanGrabbing] = React.useState(false);
|
|
|
|
|
2022-11-05 03:29:30 +08:00
|
|
|
const start = () => setActive(true);
|
2023-03-01 10:03:33 +08:00
|
|
|
const handleGrabbing = () => setPanGrabbing(true);
|
|
|
|
const handleReleaseGrab = () => setPanGrabbing(false);
|
2022-11-05 03:29:30 +08:00
|
|
|
|
2023-03-09 00:39:53 +08:00
|
|
|
const multiUserAccess = hasMultiUserAccess(whiteboardId, currentUser?.userId);
|
2022-11-05 03:29:30 +08:00
|
|
|
const end = () => {
|
2023-03-09 00:39:53 +08:00
|
|
|
if (whiteboardId && (multiUserAccess || currentUser?.presenter)) {
|
2022-11-05 03:29:30 +08:00
|
|
|
publishCursorUpdate({
|
|
|
|
xPercent: -1.0,
|
|
|
|
yPercent: -1.0,
|
|
|
|
whiteboardId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setActive(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const moved = (event) => {
|
|
|
|
const { type, x, y } = event;
|
|
|
|
const nav = document.getElementById('Navbar');
|
|
|
|
const getSibling = (el) => {
|
|
|
|
if (el?.previousSibling && !el?.previousSibling?.hasAttribute('data-test')) {
|
|
|
|
return el?.previousSibling;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
const panel = getSibling(nav);
|
|
|
|
const webcams = document.getElementById('cameraDock');
|
|
|
|
const subPanel = panel && getSibling(panel);
|
|
|
|
const camPosition = document.getElementById('layout')?.getAttribute('data-cam-position') || null;
|
|
|
|
const sl = document.getElementById('layout')?.getAttribute('data-layout');
|
|
|
|
const presentationContainer = document.querySelector('[data-test="presentationContainer"]');
|
|
|
|
const presentation = document.getElementById('currentSlideText')?.parentElement;
|
|
|
|
const banners = document.querySelectorAll('[data-test="notificationBannerBar"]');
|
|
|
|
let yOffset = 0;
|
|
|
|
let xOffset = 0;
|
|
|
|
const calcPresOffset = () => {
|
|
|
|
yOffset
|
|
|
|
+= (parseFloat(presentationContainer?.style?.height)
|
|
|
|
- (parseFloat(presentation?.style?.height)
|
2023-03-10 19:30:46 +08:00
|
|
|
+ (currentUser.presenter ? PRES_TOOLBAR_HEIGHT : 0))
|
2022-11-05 03:29:30 +08:00
|
|
|
) / 2;
|
|
|
|
xOffset
|
|
|
|
+= (parseFloat(presentationContainer?.style?.width)
|
|
|
|
- parseFloat(presentation?.style?.width)
|
|
|
|
) / 2;
|
|
|
|
};
|
|
|
|
// If the presentation container is the full screen element we don't
|
|
|
|
// need any offsets
|
|
|
|
const { webkitFullscreenElement, fullscreenElement } = document;
|
|
|
|
const fsEl = webkitFullscreenElement || fullscreenElement;
|
|
|
|
if (fsEl?.getAttribute('data-test') === 'presentationContainer') {
|
|
|
|
calcPresOffset();
|
|
|
|
return setPos({ x: x - xOffset, y: y - yOffset });
|
|
|
|
}
|
|
|
|
if (nav) yOffset += parseFloat(nav?.style?.height);
|
|
|
|
if (panel) xOffset += parseFloat(panel?.style?.width);
|
|
|
|
if (subPanel) xOffset += parseFloat(subPanel?.style?.width);
|
|
|
|
|
2022-12-31 04:09:49 +08:00
|
|
|
// offset native tldraw eraser animation container
|
|
|
|
const overlay = document.getElementsByClassName('tl-overlay')[0];
|
|
|
|
if (overlay) overlay.style.left = '0px';
|
2022-11-05 03:29:30 +08:00
|
|
|
|
|
|
|
if (type === 'touchmove') {
|
|
|
|
calcPresOffset();
|
|
|
|
if (!active) {
|
|
|
|
setActive(true);
|
|
|
|
}
|
|
|
|
const newX = event?.changedTouches[0]?.clientX - xOffset;
|
|
|
|
const newY = event?.changedTouches[0]?.clientY - yOffset;
|
|
|
|
return setPos({ x: newX, y: newY });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document?.documentElement?.dir === 'rtl') {
|
|
|
|
xOffset = 0;
|
|
|
|
if (presentationContainer && presentation) {
|
|
|
|
calcPresOffset();
|
|
|
|
}
|
|
|
|
if (sl.includes('custom')) {
|
|
|
|
if (webcams) {
|
|
|
|
if (camPosition === 'contentTop' || !camPosition) {
|
|
|
|
yOffset += (parseFloat(webcams?.style?.height || 0) + BOTTOM_CAM_HANDLE_HEIGHT);
|
|
|
|
}
|
|
|
|
if (camPosition === 'contentBottom') {
|
|
|
|
yOffset -= BOTTOM_CAM_HANDLE_HEIGHT;
|
|
|
|
}
|
|
|
|
if (camPosition === 'contentRight') {
|
|
|
|
xOffset += (parseFloat(webcams?.style?.width || 0) + SMALL_OFFSET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sl?.includes('smart')) {
|
|
|
|
if (panel || subPanel) {
|
|
|
|
const dockPos = webcams?.getAttribute('data-position');
|
|
|
|
if (dockPos === 'contentTop') {
|
|
|
|
yOffset += (parseFloat(webcams?.style?.height || 0) + SMALL_OFFSET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (webcams && sl?.includes('videoFocus')) {
|
|
|
|
xOffset += parseFloat(nav?.style?.width);
|
|
|
|
yOffset += (parseFloat(panel?.style?.height || 0) - XL_OFFSET);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sl.includes('custom')) {
|
|
|
|
if (webcams) {
|
|
|
|
if (camPosition === 'contentTop' || !camPosition) {
|
|
|
|
yOffset += (parseFloat(webcams?.style?.height) || 0) + XS_OFFSET;
|
|
|
|
}
|
|
|
|
if (camPosition === 'contentBottom') {
|
|
|
|
yOffset -= BOTTOM_CAM_HANDLE_HEIGHT;
|
|
|
|
}
|
|
|
|
if (camPosition === 'contentLeft') {
|
|
|
|
xOffset += (parseFloat(webcams?.style?.width) || 0) + SMALL_OFFSET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sl.includes('smart')) {
|
|
|
|
if (panel || subPanel) {
|
|
|
|
const dockPos = webcams?.getAttribute('data-position');
|
|
|
|
if (dockPos === 'contentLeft') {
|
|
|
|
xOffset += (parseFloat(webcams?.style?.width || 0) + SMALL_OFFSET);
|
|
|
|
}
|
|
|
|
if (dockPos === 'contentTop') {
|
|
|
|
yOffset += (parseFloat(webcams?.style?.height || 0) + SMALL_OFFSET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!panel && !subPanel) {
|
|
|
|
if (webcams) {
|
|
|
|
xOffset = parseFloat(webcams?.style?.width || 0) + SMALL_OFFSET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sl?.includes('videoFocus')) {
|
|
|
|
if (webcams) {
|
|
|
|
xOffset = parseFloat(subPanel?.style?.width);
|
|
|
|
yOffset = parseFloat(panel?.style?.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (presentationContainer && presentation) {
|
|
|
|
calcPresOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (banners) {
|
|
|
|
banners.forEach((el) => {
|
|
|
|
yOffset += parseFloat(window.getComputedStyle(el).height);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return setPos({ x: event.x - xOffset, y: event.y - yOffset });
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2023-02-07 20:52:24 +08:00
|
|
|
const currentCursor = cursorWrapper?.current;
|
|
|
|
currentCursor?.addEventListener('mouseenter', start);
|
|
|
|
currentCursor?.addEventListener('mouseleave', end);
|
2023-03-10 19:30:46 +08:00
|
|
|
currentCursor?.addEventListener('mousedown', handleGrabbing);
|
|
|
|
currentCursor?.addEventListener('mouseup', handleReleaseGrab);
|
2023-02-07 20:52:24 +08:00
|
|
|
currentCursor?.addEventListener('touchend', end);
|
|
|
|
currentCursor?.addEventListener('mousemove', moved);
|
|
|
|
currentCursor?.addEventListener('touchmove', moved);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
currentCursor?.removeEventListener('mouseenter', start);
|
|
|
|
currentCursor?.removeEventListener('mouseleave', end);
|
2023-03-10 19:30:46 +08:00
|
|
|
currentCursor?.removeEventListener('mousedown', handleGrabbing);
|
|
|
|
currentCursor?.removeEventListener('mouseup', handleReleaseGrab);
|
2023-02-07 20:52:24 +08:00
|
|
|
currentCursor?.removeEventListener('touchend', end);
|
|
|
|
currentCursor?.removeEventListener('mousemove', moved);
|
|
|
|
currentCursor?.removeEventListener('touchmove', moved);
|
|
|
|
};
|
|
|
|
}, [cursorWrapper, whiteboardId, currentUser.presenter]);
|
2022-11-05 03:29:30 +08:00
|
|
|
|
2023-01-12 03:32:03 +08:00
|
|
|
let cursorType = multiUserAccess || currentUser?.presenter ? TOOL_CURSORS[currentTool] || 'none' : 'default';
|
2023-03-01 10:03:33 +08:00
|
|
|
if (isPanning && !disabledPan) {
|
|
|
|
if (panGrabbing) {
|
|
|
|
cursorType = TOOL_CURSORS.grabbing;
|
|
|
|
} else {
|
|
|
|
cursorType = TOOL_CURSORS.pan;
|
|
|
|
}
|
|
|
|
}
|
2023-02-11 02:30:24 +08:00
|
|
|
if (isMoving) cursorType = TOOL_CURSORS.moving;
|
2022-11-05 03:29:30 +08:00
|
|
|
|
|
|
|
return (
|
2023-02-07 20:52:24 +08:00
|
|
|
<span key={`cursor-wrapper-${whiteboardId}`} ref={cursorWrapper}>
|
2022-11-05 03:29:30 +08:00
|
|
|
<div style={{ height: '100%', cursor: cursorType }}>
|
|
|
|
{((active && multiUserAccess) || (active && currentUser?.presenter)) && (
|
|
|
|
<PositionLabel
|
|
|
|
pos={pos}
|
|
|
|
otherCursors={otherCursors}
|
|
|
|
currentUser={currentUser}
|
2023-02-14 03:33:06 +08:00
|
|
|
currentPoint={currentPoint}
|
|
|
|
tldrawCamera={tldrawCamera}
|
2022-11-05 03:29:30 +08:00
|
|
|
publishCursorUpdate={publishCursorUpdate}
|
|
|
|
whiteboardId={whiteboardId}
|
|
|
|
isMultiUserActive={isMultiUserActive}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
{otherCursors
|
|
|
|
.filter((c) => c?.xPercent && c.xPercent !== -1.0 && c?.yPercent && c.yPercent !== -1.0)
|
|
|
|
.filter((c) => {
|
|
|
|
if ((isViewersCursorLocked && c?.role !== 'VIEWER')
|
|
|
|
|| !isViewersCursorLocked
|
|
|
|
|| currentUser?.presenter
|
|
|
|
) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.map((c) => {
|
|
|
|
if (c && currentUser.userId !== c?.userId) {
|
|
|
|
if (c.presenter) {
|
|
|
|
return (
|
|
|
|
<Cursor
|
|
|
|
key={`${c?.userId}`}
|
|
|
|
name={c?.userName}
|
|
|
|
color="#C70039"
|
|
|
|
x={c?.xPercent}
|
|
|
|
y={c?.yPercent}
|
2023-02-14 03:33:06 +08:00
|
|
|
tldrawCamera={tldrawCamera}
|
2022-11-05 03:29:30 +08:00
|
|
|
isMultiUserActive={isMultiUserActive(whiteboardId)}
|
|
|
|
owner
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasMultiUserAccess(whiteboardId, c?.userId)
|
|
|
|
&& (
|
2023-03-10 19:30:46 +08:00
|
|
|
<Cursor
|
|
|
|
key={`${c?.userId}`}
|
|
|
|
name={c?.userName}
|
|
|
|
color="#AFE1AF"
|
|
|
|
x={c?.xPercent}
|
|
|
|
y={c?.yPercent}
|
|
|
|
tldrawCamera={tldrawCamera}
|
|
|
|
isMultiUserActive={isMultiUserActive(whiteboardId)}
|
|
|
|
owner
|
|
|
|
/>
|
2022-11-05 03:29:30 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})}
|
|
|
|
</span>
|
|
|
|
);
|
2023-03-10 19:30:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Cursors.propTypes = {
|
|
|
|
whiteboardId: PropTypes.string,
|
|
|
|
otherCursors: PropTypes.arrayOf(PropTypes.shape).isRequired,
|
|
|
|
currentUser: PropTypes.shape({
|
|
|
|
userId: PropTypes.string.isRequired,
|
|
|
|
presenter: PropTypes.bool.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
currentPoint: PropTypes.arrayOf(PropTypes.number),
|
|
|
|
tldrawCamera: PropTypes.shape({
|
|
|
|
point: PropTypes.arrayOf(PropTypes.number).isRequired,
|
|
|
|
zoom: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
publishCursorUpdate: PropTypes.func.isRequired,
|
|
|
|
children: PropTypes.arrayOf(PropTypes.element).isRequired,
|
|
|
|
isViewersCursorLocked: PropTypes.bool.isRequired,
|
|
|
|
hasMultiUserAccess: PropTypes.func.isRequired,
|
|
|
|
isMultiUserActive: PropTypes.func.isRequired,
|
|
|
|
isPanning: PropTypes.bool.isRequired,
|
|
|
|
isMoving: PropTypes.bool.isRequired,
|
|
|
|
currentTool: PropTypes.string,
|
|
|
|
disabledPan: PropTypes.bool.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
Cursors.defaultProps = {
|
|
|
|
whiteboardId: undefined,
|
|
|
|
currentPoint: undefined,
|
|
|
|
tldrawCamera: undefined,
|
|
|
|
currentTool: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Cursors;
|