2022-04-05 22:49:13 +08:00
|
|
|
import * as React from "react";
|
|
|
|
import ReactCursorPosition from "react-cursor-position";
|
2022-04-11 00:31:12 +08:00
|
|
|
import Vec from "@tldraw/vec";
|
|
|
|
import { _ } from "lodash";
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2022-04-11 00:31:12 +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;
|
2022-04-11 00:31:12 +08:00
|
|
|
let _x = null;
|
|
|
|
let _y = null;
|
2022-04-05 22:49:13 +08:00
|
|
|
|
2022-04-11 00:31:12 +08:00
|
|
|
if (!currentPoint) {
|
2022-05-30 00:24:44 +08:00
|
|
|
_x = (x + pageState?.camera?.point[0]) * pageState?.camera?.zoom;
|
|
|
|
_y = (y + pageState?.camera?.point[1]) * pageState?.camera?.zoom;
|
2022-04-11 00:31:12 +08:00
|
|
|
}
|
2022-04-05 22:49:13 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
key={`${name}-${color}-${x}-${y}`}
|
|
|
|
style={{
|
|
|
|
zIndex: z,
|
|
|
|
position: "absolute",
|
2022-04-11 00:31:12 +08:00
|
|
|
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",
|
2022-04-11 00:31:12 +08:00
|
|
|
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,
|
2022-04-11 00:31:12 +08:00
|
|
|
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;
|
2022-04-11 00:31:12 +08:00
|
|
|
const prevCurrentPoint = usePrevious(currentPoint);
|
2022-04-05 22:49:13 +08:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-04-11 00:31:12 +08:00
|
|
|
try {
|
|
|
|
const point = _.isEqual(currentPoint, prevCurrentPoint)
|
|
|
|
? [x, y]
|
|
|
|
: currentPoint;
|
2022-05-07 00:37:43 +08:00
|
|
|
publishCursorUpdate({
|
2022-05-30 00:24:44 +08:00
|
|
|
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
|
|
|
});
|
2022-04-11 00:31:12 +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%" }}>
|
2022-04-11 00:31:12 +08:00
|
|
|
{renderCursor(name, color, x, y, currentPoint, props.pageState)}
|
2022-04-05 22:49:13 +08:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function Cursors(props) {
|
2022-05-30 00:24:44 +08:00
|
|
|
let cursorWrapper = React.useRef(null);
|
|
|
|
const [active, setActive] = React.useState(false);
|
2022-05-30 22:51:08 +08:00
|
|
|
const {
|
|
|
|
whiteboardId,
|
|
|
|
otherCursors,
|
|
|
|
currentUser,
|
|
|
|
tldrawAPI,
|
|
|
|
publishCursorUpdate,
|
|
|
|
children,
|
|
|
|
} = props;
|
2022-05-30 00:24:44 +08:00
|
|
|
React.useEffect(() => {
|
|
|
|
!cursorWrapper.hasOwnProperty("mouseenter") &&
|
|
|
|
cursorWrapper?.addEventListener("mouseenter", (event) => {
|
|
|
|
setActive(true);
|
|
|
|
});
|
|
|
|
!cursorWrapper.hasOwnProperty("mouseleave") &&
|
|
|
|
cursorWrapper?.addEventListener("mouseleave", (event) => {
|
2022-05-30 22:51:08 +08:00
|
|
|
publishCursorUpdate({
|
2022-05-30 00:24:44 +08:00
|
|
|
xPercent: null,
|
|
|
|
yPercent: null,
|
2022-05-30 22:51:08 +08:00
|
|
|
whiteboardId: whiteboardId,
|
2022-05-30 00:24:44 +08:00
|
|
|
});
|
|
|
|
setActive(false);
|
|
|
|
});
|
|
|
|
}, [cursorWrapper]);
|
|
|
|
|
2022-04-05 22:49:13 +08:00
|
|
|
return (
|
2022-05-30 00:24:44 +08:00
|
|
|
<span disabled={true} ref={(r) => (cursorWrapper = r)}>
|
2022-04-05 22:49:13 +08:00
|
|
|
<ReactCursorPosition style={{ height: "100%", cursor: "none" }}>
|
2022-05-30 00:24:44 +08:00
|
|
|
{active && (
|
|
|
|
<PositionLabel
|
2022-05-30 22:51:08 +08:00
|
|
|
otherCursors={otherCursors}
|
|
|
|
currentUser={currentUser}
|
|
|
|
currentPoint={tldrawAPI?.currentPoint}
|
|
|
|
pageState={tldrawAPI?.getPageState()}
|
|
|
|
publishCursorUpdate={publishCursorUpdate}
|
|
|
|
whiteboardId={whiteboardId}
|
2022-05-30 00:24:44 +08:00
|
|
|
/>
|
|
|
|
)}
|
2022-05-30 22:51:08 +08:00
|
|
|
{children}
|
2022-04-05 22:49:13 +08:00
|
|
|
</ReactCursorPosition>
|
2022-05-30 22:51:08 +08:00
|
|
|
{otherCursors
|
2022-05-30 00:24:44 +08:00
|
|
|
.filter((c) => c?.xPercent && c?.yPercent)
|
|
|
|
.map((c) => {
|
|
|
|
return (
|
|
|
|
c &&
|
2022-05-30 22:51:08 +08:00
|
|
|
currentUser.userId !== c?.userId &&
|
2022-05-30 00:24:44 +08:00
|
|
|
renderCursor(
|
|
|
|
c?.userName,
|
|
|
|
c?.presenter ? "#C70039" : "#AFE1AF",
|
|
|
|
c?.xPercent,
|
|
|
|
c?.yPercent,
|
|
|
|
null,
|
2022-05-30 22:51:08 +08:00
|
|
|
tldrawAPI?.getPageState(),
|
2022-05-30 00:24:44 +08:00
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</span>
|
2022-04-05 22:49:13 +08:00
|
|
|
);
|
|
|
|
}
|