Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 6x 6x | import React, { useEffect, useRef, useState } from "react";
import { Box, Paper } from "@mui/material";
import { useTheme } from "@mui/material/styles";
import { sanitizeRichText } from "@/utils/helpers/sanitizeHtml";
import {
STUDENT_QUESTION_FONT_SIZE,
dropZoneColors,
neutralContentBg,
questionContentBg,
} from "./studentQuestionSurfaces";
import "./StudentQuestion.scss";
const unwrapBlock = (html) => {
const el = document.createElement("div");
el.innerHTML = html;
if (el.children.length === 1 && el.children[0].tagName === "P") {
return el.children[0].innerHTML;
}
return html;
};
/**
* A Single-Stimulus "drag-and-drop" group's own sentence-with-blanks
* interaction — the group-scoped counterpart of the standalone take-assignment
* DragAndDrop.jsx, reporting through `value`/`onChange` (like
* GroupDropdownAnswer.jsx / GroupGraphAnswer.jsx) instead of dispatching
* straight to Redux, since the parent (SingleStimulus.jsx) owns the
* combined per-group answer object.
*
* `content` is the group's own text with `__blank__` markers, `choices` is
* that group's own flat `[{id, text}]` draggable pool (ONE pool shared by
* every blank, unlike a "drop-down" group's per-blank options), and
* `value`/`onChange` carry that group's answer as `[{id, answer}, ...]`, one
* entry per blank, id === position (same convention the standalone type
* uses). Blank count is derived from `content` itself, not from `choices`,
* so `value` never needs to arrive pre-seeded — same pattern the standalone
* student-side DragAndDrop.jsx uses when rebuilding blanks per question.
*/
const GroupDragAndDropAnswer = ({ content, choices, value, onChange }) => {
const theme = useTheme();
const [dragOverId, setDragOverId] = useState(null);
const containerRef = useRef(null);
const safeChoices = Array.isArray(choices) ? choices : [];
const blankCount = Math.max((content || "").split("__blank__").length - 1, 0);
const blanks = Array.from({ length: blankCount }, (_, i) => ({
id: i,
answer: (Array.isArray(value) ? value : []).find((b) => b?.id === i)?.answer ?? "",
}));
const handleDragStart = (e, text) => {
e.dataTransfer.setData("text/plain", text);
};
const buildDropZoneHTML = (index) => {
const blank = blanks[index];
const hasAnswer = !!blank?.answer;
const answerContent = hasAnswer ? unwrapBlock(blank.answer) : "Drop here";
const isDragOver = dragOverId === index;
const {
bg: bgColor,
border: borderColor,
text: textColor,
} = dropZoneColors(theme, { dragOver: isDragOver, filled: hasAnswer });
const borderStyle = hasAnswer ? "solid" : "dashed";
const clearBtn = hasAnswer
? `<span data-clear-index="${index}" style="position:absolute;top:-10px;right:-10px;` +
`display:inline-flex;align-items:center;justify-content:center;` +
`width:20px;height:20px;border-radius:50%;background-color:#d32f2f;` +
`color:white;font-size:12px;cursor:pointer;line-height:1;z-index:1;">✕</span>`
: "";
return (
`<span data-drop-index="${index}" style="position:relative;display:inline-block;` +
`vertical-align:middle;padding:1px 3px;margin:4px 8px;` +
`border:2px ${borderStyle} ${borderColor};border-radius:3px;background-color:${bgColor};` +
`white-space:nowrap;line-height:normal;font-size:inherit;` +
`font-weight:${hasAnswer ? 500 : 400};color:${textColor};cursor:pointer;transition:all 0.2s ease;">` +
`${answerContent}${clearBtn}` +
`</span>`
);
};
const buildContent = () => {
let blankIndex = 0;
return (content || "").replace(/__blank__/g, () => buildDropZoneHTML(blankIndex++));
};
// Attach drag/drop DOM listeners after every render so they stay in sync
// with the latest blanks, same pattern as the standalone DragAndDrop.jsx.
useEffect(() => {
const container = containerRef.current;
if (!container) return undefined;
const cleanup = [];
container.querySelectorAll("span[data-drop-index]").forEach((zone) => {
const index = parseInt(zone.getAttribute("data-drop-index"), 10);
const onDragOver = (e) => {
e.preventDefault();
setDragOverId(index);
};
const onDragLeave = () => setDragOverId(null);
const onDrop = (e) => {
e.preventDefault();
const text = e.dataTransfer.getData("text/plain");
onChange(blanks.map((b) => (b.id === index ? { ...b, answer: text } : b)));
setDragOverId(null);
};
zone.addEventListener("dragover", onDragOver);
zone.addEventListener("dragleave", onDragLeave);
zone.addEventListener("drop", onDrop);
cleanup.push(() => {
zone.removeEventListener("dragover", onDragOver);
zone.removeEventListener("dragleave", onDragLeave);
zone.removeEventListener("drop", onDrop);
});
});
container.querySelectorAll("span[data-clear-index]").forEach((btn) => {
const index = parseInt(btn.getAttribute("data-clear-index"), 10);
const onClick = (e) => {
e.stopPropagation();
onChange(blanks.map((b) => (b.id === index ? { ...b, answer: "" } : b)));
};
btn.addEventListener("click", onClick);
cleanup.push(() => btn.removeEventListener("click", onClick));
});
return () => cleanup.forEach((fn) => fn());
});
return (
<Box className="student-drag-drop">
<Paper
ref={containerRef}
variant="outlined"
className="student-question-content student-drag-drop-question"
dangerouslySetInnerHTML={{ __html: sanitizeRichText(buildContent()) }}
sx={{
display: "block !important",
bgcolor: questionContentBg(theme),
fontSize: STUDENT_QUESTION_FONT_SIZE,
lineHeight: 2,
fontWeight: "normal",
"& p": { margin: 0, padding: 0 },
"& u": { textDecoration: "underline" },
"& s, & del": { textDecoration: "line-through" },
"& strong, & b": { fontWeight: "bold" },
"& em, & i": { fontStyle: "italic" },
"& sub": { verticalAlign: "sub", fontSize: "smaller" },
"& sup": { verticalAlign: "super", fontSize: "smaller" },
"& ul": { listStyle: "disc", paddingLeft: "2em" },
"& ol": { listStyle: "decimal", paddingLeft: "2em" },
"& li": { display: "list-item" },
"& img": { maxWidth: "100%", height: "auto", verticalAlign: "middle" },
}}
/>
<Paper
elevation={0}
className="student-drag-choices"
sx={{ bgcolor: neutralContentBg(theme), fontSize: STUDENT_QUESTION_FONT_SIZE }}
>
{safeChoices.map((choiceObj, index) => (
<Box
key={choiceObj.id ?? index}
className="student-drag-choice"
draggable
onDragStart={(e) => handleDragStart(e, choiceObj.text)}
>
<Box
dangerouslySetInnerHTML={{ __html: sanitizeRichText(unwrapBlock(choiceObj.text)) }}
sx={{
"& p": { margin: 0, padding: 0 },
"& u": { textDecoration: "underline" },
"& s, & del": { textDecoration: "line-through" },
"& strong, & b": { fontWeight: "bold" },
"& em, & i": { fontStyle: "italic" },
}}
/>
</Box>
))}
</Paper>
</Box>
);
};
export default GroupDragAndDropAnswer;
|