2023-12-29 04:10:08 +00:00
|
|
|
import {Box, IconButton, ThemeProvider} from "@mui/material";
|
2023-10-03 16:05:20 +00:00
|
|
|
import { toggleQuizPreview, useQuizPreviewStore } from "@root/quizPreview";
|
2023-10-05 11:41:54 +00:00
|
|
|
import { useLayoutEffect, useRef } from "react";
|
2023-10-03 16:05:20 +00:00
|
|
|
import { Rnd } from "react-rnd";
|
2023-10-17 14:17:02 +00:00
|
|
|
import { useWindowSize } from "../../utils/hooks/useWindowSize";
|
2023-10-09 12:33:45 +00:00
|
|
|
import QuizPreviewLayout from "./QuizPreviewLayout";
|
2023-11-01 13:31:15 +00:00
|
|
|
import ResizeIcon from "@icons/ResizeIcon";
|
2023-12-29 04:10:08 +00:00
|
|
|
import {themesPublication} from "@utils/themes/Publication/themePublication";
|
|
|
|
import {useCurrentQuiz} from "@root/quizes/hooks";
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
const DRAG_PARENT_MARGIN = 0;
|
|
|
|
const NAVBAR_HEIGHT = 0;
|
|
|
|
const DRAG_PARENT_BOTTOM_MARGIN = 0;
|
2023-10-03 16:05:20 +00:00
|
|
|
|
|
|
|
interface RndPositionAndSize {
|
2023-10-17 13:24:37 +00:00
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
width: string;
|
|
|
|
height: string;
|
2023-10-03 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function QuizPreview() {
|
2023-10-17 13:24:37 +00:00
|
|
|
const isPreviewShown = useQuizPreviewStore((state) => state.isPreviewShown);
|
|
|
|
const rndParentRef = useRef<HTMLDivElement>(null);
|
2023-12-29 04:10:08 +00:00
|
|
|
const quiz = useCurrentQuiz();
|
2023-10-17 13:24:37 +00:00
|
|
|
const rndRef = useRef<Rnd | null>(null);
|
|
|
|
const rndPositionAndSizeRef = useRef<RndPositionAndSize>({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: "340",
|
|
|
|
height: "480",
|
|
|
|
});
|
|
|
|
const isFirstShowRef = useRef<boolean>(true);
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
useLayoutEffect(
|
|
|
|
function stickPreviewToBottomRight() {
|
|
|
|
const rnd = rndRef.current;
|
|
|
|
const rndSelfElement = rnd?.getSelfElement();
|
2023-12-27 15:16:23 +00:00
|
|
|
if (!rnd || !rndSelfElement || !rndParentRef.current || !isFirstShowRef.current) return;
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
const rndParentRect = rndParentRef.current.getBoundingClientRect();
|
|
|
|
const rndRect = rndSelfElement.getBoundingClientRect();
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
const x = rndParentRect.width - rndRect.width;
|
|
|
|
const y = rndParentRect.height - rndRect.height;
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
rnd.updatePosition({ x, y });
|
|
|
|
rndPositionAndSizeRef.current.x = x;
|
|
|
|
rndPositionAndSizeRef.current.y = y;
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
isFirstShowRef.current = false;
|
|
|
|
},
|
|
|
|
[isPreviewShown]
|
|
|
|
);
|
2023-10-03 16:05:20 +00:00
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
return (
|
2023-12-29 04:10:08 +00:00
|
|
|
<ThemeProvider theme={themesPublication?.[quiz?.config.theme]}>
|
|
|
|
<Box
|
|
|
|
ref={rndParentRef}
|
|
|
|
data-cy="quiz-preview-container"
|
|
|
|
sx={{
|
|
|
|
position: "fixed",
|
|
|
|
top: NAVBAR_HEIGHT + DRAG_PARENT_MARGIN,
|
|
|
|
left: DRAG_PARENT_MARGIN,
|
|
|
|
bottom: DRAG_PARENT_BOTTOM_MARGIN,
|
|
|
|
right: DRAG_PARENT_MARGIN,
|
|
|
|
// backgroundColor: "rgba(0, 100, 0, 0.2)",
|
|
|
|
pointerEvents: "none",
|
|
|
|
zIndex: 100,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isPreviewShown && (
|
|
|
|
<Rnd
|
|
|
|
minHeight={20}
|
|
|
|
minWidth={20}
|
|
|
|
bounds="parent"
|
|
|
|
ref={rndRef}
|
|
|
|
dragHandleClassName="quiz-preview-draghandle"
|
|
|
|
default={{
|
|
|
|
x: rndPositionAndSizeRef.current.x,
|
|
|
|
y: rndPositionAndSizeRef.current.y,
|
|
|
|
width: rndPositionAndSizeRef.current.width,
|
|
|
|
height: rndPositionAndSizeRef.current.height,
|
|
|
|
}}
|
|
|
|
onResizeStop={(e, direction, ref, delta, position) => {
|
|
|
|
rndPositionAndSizeRef.current.x = position.x;
|
|
|
|
rndPositionAndSizeRef.current.y = position.y;
|
|
|
|
rndPositionAndSizeRef.current.width = ref.style.width;
|
|
|
|
rndPositionAndSizeRef.current.height = ref.style.height;
|
|
|
|
}}
|
|
|
|
onDragStop={(e, d) => {
|
|
|
|
rndPositionAndSizeRef.current.x = d.x;
|
|
|
|
rndPositionAndSizeRef.current.y = d.y;
|
|
|
|
}}
|
|
|
|
onDragStart={(e, d) => {
|
|
|
|
e.preventDefault();
|
|
|
|
}}
|
|
|
|
enableResizing={{
|
|
|
|
topLeft: isPreviewShown,
|
|
|
|
}}
|
|
|
|
resizeHandleComponent={{
|
|
|
|
topLeft: <ResizeIcon />,
|
|
|
|
}}
|
|
|
|
resizeHandleStyles={{
|
|
|
|
topLeft: {
|
|
|
|
top: "-1px",
|
|
|
|
left: "-1px",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
style={{
|
|
|
|
overflow: "hidden",
|
|
|
|
pointerEvents: "auto",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<QuizPreviewLayout />
|
|
|
|
</Rnd>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</ThemeProvider>
|
|
|
|
|
2023-10-17 13:24:37 +00:00
|
|
|
);
|
2023-10-03 16:05:20 +00:00
|
|
|
}
|