bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/whiteboard/cursors/component.jsx

165 lines
4.2 KiB
React
Raw Normal View History

2022-04-05 22:49:13 +08:00
import * as React from "react";
import ReactCursorPosition from "react-cursor-position";
import Vec from "@tldraw/vec";
import { _ } from "lodash";
2022-04-05 22:49:13 +08:00
function usePrevious(value) {
const ref = React.useRef();
React.useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}
const renderCursor = (
name,
color,
x,
y,
currentPoint,
pageState,
owner = false
) => {
2022-04-05 22:49:13 +08:00
const z = !owner ? 2 : 1;
let _x = null;
let _y = null;
2022-04-05 22:49:13 +08:00
if (!currentPoint) {
_x = (x + pageState?.camera?.point[0]) * pageState?.camera?.zoom;
_y = (y + pageState?.camera?.point[1]) * pageState?.camera?.zoom;
}
2022-04-05 22:49:13 +08:00
return (
<>
<div
key={`${name}-${color}-${x}-${y}`}
style={{
zIndex: z,
position: "absolute",
left: (_x || x) - 2.5,
top: (_y || y) - 2.5,
2022-04-05 22:49:13 +08:00
width: 5,
height: 5,
borderRadius: "50%",
background: `${color}`,
pointerEvents: "none",
}}
/>
<div
style={{
zIndex: z,
position: "absolute",
pointerEvents: "none",
left: (_x || x) + 3.75,
top: (_y || y) + 3,
2022-04-05 22:49:13 +08:00
paddingLeft: ".25rem",
paddingRight: ".25rem",
paddingBottom: ".1rem",
lineHeight: "1rem",
borderRadius: "2px",
color: "#FFF",
backgroundColor: color,
border: `1px solid ${color}`,
}}
>
{name}
</div>
</>
);
};
const PositionLabel = (props) => {
const {
position: { x = 0, y = 0 } = {},
currentUser,
currentPoint,
pageState,
2022-04-05 22:49:13 +08:00
publishCursorUpdate,
2022-05-07 00:37:43 +08:00
whiteboardId,
2022-04-05 22:49:13 +08:00
} = props;
const { name, color, userId, presenter } = currentUser;
const prevCurrentPoint = usePrevious(currentPoint);
2022-04-05 22:49:13 +08:00
React.useEffect(() => {
try {
const point = _.isEqual(currentPoint, prevCurrentPoint)
? [x, y]
: currentPoint;
2022-05-07 00:37:43 +08:00
publishCursorUpdate({
xPercent:
point[0] / pageState?.camera?.zoom - pageState?.camera?.point[0],
yPercent:
point[1] / pageState?.camera?.zoom - pageState?.camera?.point[1],
whiteboardId,
2022-05-07 00:37:43 +08:00
});
} catch (e) {
console.log(e);
}
2022-04-05 22:49:13 +08:00
}, [x, y]);
return (
<>
<div style={{ position: "absolute", height: "100%", width: "100%" }}>
{renderCursor(name, color, x, y, currentPoint, props.pageState)}
2022-04-05 22:49:13 +08:00
</div>
</>
);
};
export default function Cursors(props) {
let cursorWrapper = React.useRef(null);
const [active, setActive] = React.useState(false);
React.useEffect(() => {
!cursorWrapper.hasOwnProperty("mouseenter") &&
cursorWrapper?.addEventListener("mouseenter", (event) => {
setActive(true);
});
!cursorWrapper.hasOwnProperty("mouseleave") &&
cursorWrapper?.addEventListener("mouseleave", (event) => {
props?.publishCursorUpdate({
xPercent: null,
yPercent: null,
whiteboardId: props?.whiteboardId,
});
setActive(false);
});
}, [cursorWrapper]);
2022-04-05 22:49:13 +08:00
return (
<span disabled={true} ref={(r) => (cursorWrapper = r)}>
2022-04-05 22:49:13 +08:00
<ReactCursorPosition style={{ height: "100%", cursor: "none" }}>
{active && (
<PositionLabel
otherCursors={props.otherCursors}
currentUser={props.currentUser}
currentPoint={props.tldrawAPI?.currentPoint}
pageState={props.tldrawAPI?.getPageState()}
publishCursorUpdate={props.publishCursorUpdate}
whiteboardId={props.whiteboardId}
/>
)}
2022-04-05 22:49:13 +08:00
{props.children}
</ReactCursorPosition>
{props.otherCursors
.filter((c) => c?.xPercent && c?.yPercent)
.map((c) => {
return (
c &&
props.currentUser.userId !== c?.userId &&
renderCursor(
c?.userName,
c?.presenter ? "#C70039" : "#AFE1AF",
c?.xPercent,
c?.yPercent,
null,
props.tldrawAPI?.getPageState(),
true
)
);
})}
</span>
2022-04-05 22:49:13 +08:00
);
}