@tinyweb_dev/oe-exam-sdk 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/components/exams/take/components/question-renderers/ChooseThenAnswerGroupRenderer.js +1 -1
  2. package/dist/components/questions/_shared/types/answer-the-question-group.type.d.ts +3 -0
  3. package/dist/components/questions/_shared/types/choose-the-correct-answer-group.type.d.ts +2 -2
  4. package/dist/components/questions/_shared/types/choose-the-correct-answer-group.type.js +5 -1
  5. package/dist/components/questions/_shared/types/choose-then-answer-group.type.d.ts +17 -0
  6. package/dist/components/questions/_shared/types/cross-out-word-group.type.d.ts +1 -0
  7. package/dist/components/questions/_shared/types/true-false-correct-group.type.d.ts +1 -0
  8. package/dist/components/questions/_shared/types/true-false-group.type.d.ts +1 -0
  9. package/dist/components/questions/types/answer-the-question-group/AnswerTheQuestionGroupClient.d.ts +1 -1
  10. package/dist/components/questions/types/answer-the-question-group/AnswerTheQuestionGroupClient.js +13 -15
  11. package/dist/components/questions/types/answer-the-question-group/AnswerTheQuestionGroupCreator.js +2 -0
  12. package/dist/components/questions/types/answer-the-question-group/map-answer-the-question-group-data.js +2 -0
  13. package/dist/components/questions/types/answer-the-question-group/transform.js +12 -5
  14. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseAnswerGroupImageUpload.d.ts +7 -0
  15. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseAnswerGroupImageUpload.js +4 -1
  16. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseTheCorrectAnswerGroupClient.js +24 -22
  17. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseTheCorrectAnswerGroupCreator.js +45 -58
  18. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseTheCorrectAnswerGroupItemEditor.d.ts +6 -0
  19. package/dist/components/questions/types/choose-the-correct-answer-group/ChooseTheCorrectAnswerGroupItemEditor.js +145 -0
  20. package/dist/components/questions/types/choose-the-correct-answer-group/map-choose-the-correct-answer-group-data.js +6 -15
  21. package/dist/components/questions/types/choose-the-correct-answer-group/transform.js +5 -4
  22. package/dist/components/questions/types/choose-then-answer-group/ChooseThenAnswerGroupClient.d.ts +1 -1
  23. package/dist/components/questions/types/choose-then-answer-group/ChooseThenAnswerGroupClient.js +75 -11
  24. package/dist/components/questions/types/choose-then-answer-group/ChooseThenAnswerGroupCreator.js +54 -6
  25. package/dist/components/questions/types/choose-then-answer-group/map-choose-then-answer-group-data.d.ts +4 -1
  26. package/dist/components/questions/types/choose-then-answer-group/map-choose-then-answer-group-data.js +39 -2
  27. package/dist/components/questions/types/choose-then-answer-group/transform.js +27 -14
  28. package/dist/components/questions/types/cross-out-word-group/CrossOutWordGroupClient.d.ts +1 -1
  29. package/dist/components/questions/types/cross-out-word-group/CrossOutWordGroupClient.js +54 -3
  30. package/dist/components/questions/types/crossword-puzzle/CrosswordPuzzleClient.d.ts +1 -1
  31. package/dist/components/questions/types/crossword-puzzle/CrosswordPuzzleClient.js +15 -5
  32. package/dist/components/questions/types/find-words-in-matrix/FindWordsInMatrixClient.d.ts +12 -2
  33. package/dist/components/questions/types/find-words-in-matrix/FindWordsInMatrixClient.js +144 -45
  34. package/dist/components/questions/types/spontaneous-qa-group/SpontaneousQaGroupClient.d.ts +1 -1
  35. package/dist/components/questions/types/spontaneous-qa-group/SpontaneousQaGroupClient.js +79 -3
  36. package/dist/components/questions/types/spontaneous-qa-group/index.d.ts +1 -0
  37. package/dist/components/questions/types/spontaneous-qa-group/index.js +1 -0
  38. package/dist/components/questions/types/spontaneous-qa-group/map-spontaneous-qa-group-data.d.ts +2 -0
  39. package/dist/components/questions/types/spontaneous-qa-group/map-spontaneous-qa-group-data.js +74 -0
  40. package/dist/components/questions/types/true-false-correct-group/TrueFalseCorrectGroupClient.d.ts +1 -1
  41. package/dist/components/questions/types/true-false-correct-group/TrueFalseCorrectGroupClient.js +26 -4
  42. package/dist/components/questions/types/true-false-group/TrueFalseGroupClient.d.ts +1 -1
  43. package/dist/components/questions/types/true-false-group/TrueFalseGroupClient.js +13 -3
  44. package/dist/components/questions/viewer/AnswerTheQuestionGroupViewer.d.ts +3 -2
  45. package/dist/components/questions/viewer/AnswerTheQuestionGroupViewer.js +12 -5
  46. package/dist/components/questions/viewer/ChooseThenAnswerGroupViewer.d.ts +3 -2
  47. package/dist/components/questions/viewer/ChooseThenAnswerGroupViewer.js +4 -3
  48. package/dist/components/questions/viewer/CrossOutWordGroupViewer.d.ts +3 -1
  49. package/dist/components/questions/viewer/CrossOutWordGroupViewer.js +15 -2
  50. package/dist/components/questions/viewer/QuestionViewer.d.ts +9 -1
  51. package/dist/components/questions/viewer/QuestionViewer.js +22 -9
  52. package/dist/components/questions/viewer/SpontaneousQaGroupViewer.js +4 -79
  53. package/dist/components/questions/viewer/TrueFalseCorrectGroupViewer.d.ts +3 -2
  54. package/dist/components/questions/viewer/TrueFalseCorrectGroupViewer.js +14 -4
  55. package/dist/components/questions/viewer/TrueFalseGroupViewer.d.ts +5 -2
  56. package/dist/components/questions/viewer/TrueFalseGroupViewer.js +14 -4
  57. package/dist/components/questions/viewer/WordOrderAndMatchGroupViewer.d.ts +6 -2
  58. package/dist/components/questions/viewer/WordOrderAndMatchGroupViewer.js +26 -6
  59. package/dist/components/results/renderers/ReviewAnswerTheQuestionGroupRenderer.js +1 -1
  60. package/dist/shared/lib/utils/question-reverse-transform.js +12 -4
  61. package/dist/shared/types/questions/answer-the-question-group.d.ts +8 -3
  62. package/dist/shared/types/questions/answer-the-question-group.js +5 -0
  63. package/dist/shared/types/questions/choose-the-correct-answer-group.d.ts +1 -1
  64. package/dist/shared/types/questions/choose-then-answer-group.d.ts +3 -0
  65. package/package.json +1 -1
@@ -1,20 +1,58 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { Lightbulb, Star } from 'lucide-react';
3
+ import { Check, Lightbulb, Star, X } from 'lucide-react';
4
4
  import { cn } from '../../../../shared/lib/utils';
5
5
  function emptyAnswer() {
6
6
  return { optionId: '', text: '' };
7
7
  }
8
- export function ChooseThenAnswerGroupClient({ questionData, isReviewMode = false, userAnswers = [], onAnswerChange, }) {
8
+ function formatHalf(points) {
9
+ const half = points * 0.5;
10
+ return Number.isInteger(half) ? String(half) : half.toFixed(1);
11
+ }
12
+ function badgeClass(params) {
13
+ if (params.isExample) {
14
+ return 'bg-amber-500';
15
+ }
16
+ if (!params.isReviewMode || !params.hasAttempt) {
17
+ return 'bg-indigo-600';
18
+ }
19
+ if (params.partialScore != null) {
20
+ if (params.partialScore >= params.points - 1e-9) {
21
+ return 'bg-emerald-500';
22
+ }
23
+ if (params.partialScore > 0) {
24
+ return 'bg-amber-500';
25
+ }
26
+ return 'bg-red-500';
27
+ }
28
+ if (params.mappingCorrect || params.hasText) {
29
+ return 'bg-amber-500';
30
+ }
31
+ return 'bg-red-500';
32
+ }
33
+ export function ChooseThenAnswerGroupClient({ questionData, isReviewMode = false, userAnswers = [], itemResults, onAnswerChange, }) {
9
34
  const canEdit = Boolean(onAnswerChange) && !isReviewMode;
10
35
  const items = questionData.items || [];
11
- return (_jsxs("div", { className: "space-y-4", children: [(questionData.title || questionData.instruction) && (_jsxs("div", { className: "rounded-xl border border-slate-200 bg-slate-50 px-4 py-3", children: [questionData.title && (_jsx("p", { className: "text-xs font-semibold uppercase tracking-wide text-slate-500", children: questionData.title })), questionData.instruction && (_jsx("p", { className: "text-sm font-medium text-slate-800", children: questionData.instruction }))] })), items.map((item, itemIndex) => {
36
+ const hasAttempt = Boolean(itemResults?.length) ||
37
+ userAnswers.some((answer) => Boolean(answer?.optionId || answer?.text?.trim()));
38
+ return (_jsxs("div", { className: "space-y-2", children: [(questionData.title || questionData.instruction) && (_jsxs("div", { className: "rounded-lg border border-indigo-100 bg-indigo-50/80 px-3 py-2", children: [questionData.title && (_jsx("p", { className: "text-[11px] font-semibold text-indigo-500", children: questionData.title })), questionData.instruction && (_jsx("p", { className: "text-sm font-medium text-indigo-900", children: questionData.instruction }))] })), items.map((item, itemIndex) => {
12
39
  const stored = userAnswers[itemIndex] || emptyAnswer();
13
40
  const current = item.isExample
14
41
  ? { optionId: item.optionId, text: stored.text }
15
42
  : stored;
16
43
  const expectedId = item.optionId;
44
+ const hasStudentText = current.text.trim() !== '';
45
+ const result = itemResults?.[itemIndex];
46
+ const mappingCorrect = Boolean(expectedId) && current.optionId === expectedId;
47
+ const points = typeof item.points === 'number' ? item.points : 1;
48
+ const halfLabel = formatHalf(points);
49
+ const sampleAnswers = (item.expectedAnswers || [])
50
+ .map((answer) => answer.trim())
51
+ .filter((answer) => answer !== '');
17
52
  const parts = (item.question || '').split('{0}');
53
+ const showReview = isReviewMode && !item.isExample && Boolean(expectedId);
54
+ const showAnswerKey = showReview && !hasAttempt;
55
+ const showGradedReview = showReview && hasAttempt;
18
56
  const setAnswer = (next) => {
19
57
  if (!canEdit || item.isExample)
20
58
  return;
@@ -23,12 +61,38 @@ export function ChooseThenAnswerGroupClient({ questionData, isReviewMode = false
23
61
  text: next.text ?? current.text,
24
62
  });
25
63
  };
26
- return (_jsxs("div", { className: "overflow-hidden rounded-xl border border-gray-200 bg-white", children: [_jsxs("div", { className: "flex items-center gap-2 border-b border-gray-100 px-3 py-2", children: [_jsx("span", { className: "flex h-6 w-6 items-center justify-center rounded-full bg-slate-800 text-xs font-bold text-white", children: item.questionNumber || itemIndex + 1 }), item.isExample && (_jsxs("span", { className: "inline-flex items-center gap-1 rounded-full bg-amber-100 px-2 py-0.5 text-[10px] font-semibold uppercase text-amber-800", children: [_jsx(Star, { className: "h-3 w-3" }), "Example"] }))] }), _jsxs("div", { className: "space-y-3 p-3", children: [_jsxs("p", { className: "flex flex-wrap items-center gap-1.5 text-[15px] leading-relaxed text-slate-800", children: [_jsx("span", { children: parts[0] || '' }), _jsx("span", { className: "inline-flex items-center gap-1", children: item.options.map((option, optionIndex) => {
27
- const selected = current.optionId === option.id;
28
- const showReview = isReviewMode && !item.isExample && Boolean(expectedId);
29
- const isCorrect = showReview && option.id === expectedId;
30
- const isWrong = showReview && selected && option.id !== expectedId;
31
- return (_jsxs("span", { className: "inline-flex items-center gap-1", children: [optionIndex > 0 && _jsx("span", { className: "text-slate-400", children: "|" }), _jsx("button", { type: "button", disabled: !canEdit || item.isExample, onClick: () => setAnswer({ optionId: option.id }), className: cn('rounded-md border px-2.5 py-1 text-sm transition-colors', selected && !isCorrect && !isWrong && 'border-black bg-black text-white', !selected && !isCorrect && !isWrong && 'border-gray-300 bg-white text-gray-900', isCorrect && 'border-emerald-500 bg-emerald-50 text-emerald-900', isWrong && 'border-red-500 bg-red-50 text-red-900', (!canEdit || item.isExample) && 'cursor-default'), children: option.text || option.id })] }, option.id));
32
- }) }), _jsx("span", { children: parts.slice(1).join('{0}') })] }), _jsx("textarea", { value: current.text, disabled: !canEdit || item.isExample, onChange: (event) => setAnswer({ text: event.target.value }), placeholder: "Answer for you", rows: 3, className: "w-full rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm text-slate-800 outline-none focus:border-slate-400 disabled:bg-slate-50" })] })] }, `${item.questionNumber}-${itemIndex}`));
33
- }), isReviewMode && questionData.explanation && (_jsxs("div", { className: "flex items-start gap-2 rounded-xl border border-amber-100 bg-amber-50 px-4 py-3 text-sm text-amber-800", children: [_jsx(Lightbulb, { className: "mt-0.5 h-4 w-4 flex-shrink-0" }), questionData.explanation] }))] }));
64
+ return (_jsx("div", { className: "rounded-xl border border-slate-200 bg-white p-3", children: _jsxs("div", { className: "flex items-start gap-2", children: [_jsx("span", { className: cn('mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-xs font-bold text-white', badgeClass({
65
+ isExample: item.isExample,
66
+ isReviewMode,
67
+ hasAttempt,
68
+ points,
69
+ partialScore: result?.partialScore,
70
+ mappingCorrect,
71
+ hasText: hasStudentText,
72
+ })), children: item.questionNumber || itemIndex + 1 }), _jsxs("div", { className: "min-w-0 flex-1 space-y-2", children: [item.isExample && (_jsxs("span", { className: "inline-flex items-center gap-1 rounded-full bg-amber-100 px-2 py-0.5 text-[10px] font-semibold uppercase text-amber-800", children: [_jsx(Star, { className: "h-3 w-3" }), "Example"] })), _jsxs("p", { className: "flex flex-wrap items-center gap-x-1.5 gap-y-1.5 text-[15px] leading-7 text-slate-800", children: [_jsx("span", { children: parts[0] || '' }), _jsx("span", { className: "inline-flex items-center gap-1", children: item.options.map((option, optionIndex) => {
73
+ const selected = current.optionId === option.id;
74
+ const isKey = option.id === expectedId;
75
+ const pickedCorrect = showGradedReview && selected && isKey;
76
+ const pickedWrong = showGradedReview && selected && !isKey;
77
+ const keyOnly = showGradedReview && !selected && isKey;
78
+ const officialCorrect = showAnswerKey && isKey;
79
+ return (_jsxs("span", { className: "inline-flex items-center gap-1", children: [optionIndex > 0 && (_jsx("span", { className: "text-sm font-medium text-slate-300", children: "|" })), _jsxs("button", { type: "button", disabled: !canEdit || item.isExample, onClick: () => setAnswer({ optionId: option.id }), className: cn('inline-flex min-w-[3.5rem] items-center justify-center gap-1 rounded-full border px-2.5 py-1 text-sm font-semibold transition-colors', !showReview &&
80
+ selected &&
81
+ 'border-indigo-600 bg-indigo-600 text-white shadow-sm', !showReview &&
82
+ !selected &&
83
+ 'border-slate-200 bg-white text-slate-800 hover:border-indigo-300 hover:bg-indigo-50', showAnswerKey &&
84
+ !isKey &&
85
+ 'border-slate-200 bg-white text-slate-800', officialCorrect &&
86
+ 'border-2 border-emerald-500 bg-white text-slate-800', pickedCorrect && 'border-emerald-600 bg-emerald-600 text-white', pickedWrong && 'border-red-500 bg-red-500 text-white', keyOnly && 'border-emerald-500 bg-white text-emerald-800', showGradedReview &&
87
+ !selected &&
88
+ !isKey &&
89
+ 'border-slate-200 bg-slate-50 text-slate-400', canEdit &&
90
+ !item.isExample &&
91
+ 'cursor-pointer focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-200', (!canEdit || item.isExample) && 'cursor-default'), children: [option.text || option.id, pickedCorrect ? _jsx(Check, { className: "h-3 w-3" }) : null, pickedWrong ? _jsx(X, { className: "h-3 w-3" }) : null] })] }, option.id));
92
+ }) }), _jsx("span", { children: parts.slice(1).join('{0}') })] }), showReview && hasAttempt ? (_jsxs("div", { className: "flex flex-wrap gap-1.5", children: [_jsxs("span", { className: cn('rounded-full px-2 py-0.5 text-[11px] font-semibold', mappingCorrect
93
+ ? 'bg-emerald-50 text-emerald-700'
94
+ : 'bg-red-50 text-red-700'), children: ["Ch\u1ECDn ", mappingCorrect ? halfLabel : '0'] }), result?.aiCorrect !== undefined ? (_jsxs("span", { className: cn('rounded-full px-2 py-0.5 text-[11px] font-semibold', result.aiCorrect
95
+ ? 'bg-emerald-50 text-emerald-700'
96
+ : 'bg-red-50 text-red-700'), children: ["Vi\u1EBFt ", result.aiCorrect ? halfLabel : '0'] })) : null] })) : null, canEdit || hasStudentText || (isReviewMode && hasAttempt) ? (_jsxs("label", { className: "block space-y-1", children: [_jsx("span", { className: "text-xs font-medium text-slate-500", children: "Your answer" }), isReviewMode && !hasStudentText ? (_jsx("p", { className: "rounded-lg border border-dashed border-slate-200 bg-slate-50 px-3 py-2 text-sm italic text-slate-400", children: "Ch\u01B0a vi\u1EBFt" })) : (_jsx("textarea", { value: current.text, disabled: !canEdit || item.isExample, onChange: (event) => setAnswer({ text: event.target.value }), placeholder: "Write your answer here...", rows: 2, className: "min-h-0 w-full resize-none rounded-lg border border-slate-200 bg-white px-3 py-2 text-sm leading-relaxed text-slate-800 outline-none focus:border-indigo-400 focus:ring-2 focus:ring-indigo-100 disabled:bg-slate-50" }))] })) : null, isReviewMode && sampleAnswers.length > 0 ? (_jsxs("p", { className: "text-xs text-slate-500", children: [_jsx("span", { className: "font-semibold", children: "Sample:" }), " ", sampleAnswers.join(' · ')] })) : null] })] }) }, `${item.questionNumber}-${itemIndex}`));
97
+ }), isReviewMode && questionData.explanation && (_jsxs("div", { className: "flex items-start gap-2 rounded-lg border border-amber-100 bg-amber-50 px-3 py-2 text-sm text-amber-800", children: [_jsx(Lightbulb, { className: "mt-0.5 h-4 w-4 flex-shrink-0" }), questionData.explanation] }))] }));
34
98
  }
@@ -8,10 +8,10 @@ import { PointsInput } from '../../../../components/ui/points-input';
8
8
  import { Card, CardContent, CardHeader, CardTitle } from '../../../../components/ui/card';
9
9
  import { Switch } from '../../../../components/ui/switch';
10
10
  import { useDebouncedCallback } from '../../../../shared/lib/hooks';
11
- import { ListChecks, Plus, Star, Trash2 } from 'lucide-react';
11
+ import { ListChecks, PenLine, Plus, Sparkles, Star, Trash2 } from 'lucide-react';
12
12
  import { CompactCreatorHeader, CompactExplanationToggle, CompactFieldCard, CompactPreviewBanner, } from '../../_shared/components/compact';
13
13
  import { ChooseThenAnswerGroupClient } from './ChooseThenAnswerGroupClient';
14
- import { createEmptyItem, createEmptyOption, sumGradablePoints, } from './map-choose-then-answer-group-data';
14
+ import { createEmptyItem, createEmptyOption, normalizeExpectedAnswers, sumGradablePoints, } from './map-choose-then-answer-group-data';
15
15
  import { CHOOSE_THEN_ANSWER_GROUP_DEFAULT_INSTRUCTION } from '../../_shared/types/choose-then-answer-group.type';
16
16
  function normalizeItems(raw) {
17
17
  if (!Array.isArray(raw) || raw.length === 0) {
@@ -29,6 +29,7 @@ function normalizeItems(raw) {
29
29
  question: item.question || '',
30
30
  options,
31
31
  optionId: item.optionId || options[0]?.id || '',
32
+ expectedAnswers: normalizeExpectedAnswers(item.expectedAnswers),
32
33
  isExample: item.isExample === true,
33
34
  points: item.isExample ? 0 : (typeof item.points === 'number' ? item.points : 1),
34
35
  questionNumber: item.questionNumber || index + 1,
@@ -41,6 +42,7 @@ export function ChooseThenAnswerGroupCreator({ initialData, onChange, externalEr
41
42
  const [items, setItems] = useState(() => normalizeItems(initialData?.items));
42
43
  const [explanation, setExplanation] = useState(initialData?.explanation || '');
43
44
  const [isClientMode, setIsClientMode] = useState(false);
45
+ const [previewAnswers, setPreviewAnswers] = useState([]);
44
46
  const [errors, setErrors] = useState([]);
45
47
  const onChangeRef = useRef(onChange);
46
48
  const previousPayloadRef = useRef('');
@@ -156,6 +158,33 @@ export function ChooseThenAnswerGroupCreator({ initialData, onChange, externalEr
156
158
  };
157
159
  }));
158
160
  };
161
+ const updateExpectedAnswer = (itemIndex, answerIndex, value) => {
162
+ setItems((prev) => prev.map((item, i) => {
163
+ if (i !== itemIndex)
164
+ return item;
165
+ const expectedAnswers = [...(item.expectedAnswers || [''])];
166
+ expectedAnswers[answerIndex] = value;
167
+ return { ...item, expectedAnswers };
168
+ }));
169
+ };
170
+ const addExpectedAnswer = (itemIndex) => {
171
+ setItems((prev) => prev.map((item, i) => {
172
+ if (i !== itemIndex)
173
+ return item;
174
+ return { ...item, expectedAnswers: [...(item.expectedAnswers || ['']), ''] };
175
+ }));
176
+ };
177
+ const removeExpectedAnswer = (itemIndex, answerIndex) => {
178
+ setItems((prev) => prev.map((item, i) => {
179
+ const current = item.expectedAnswers || [''];
180
+ if (i !== itemIndex || current.length <= 1)
181
+ return item;
182
+ return {
183
+ ...item,
184
+ expectedAnswers: current.filter((_, j) => j !== answerIndex),
185
+ };
186
+ }));
187
+ };
159
188
  const addItem = () => {
160
189
  setItems((prev) => [...prev, createEmptyItem(prev.length + 1)]);
161
190
  };
@@ -168,10 +197,29 @@ export function ChooseThenAnswerGroupCreator({ initialData, onChange, externalEr
168
197
  })));
169
198
  };
170
199
  const displayErrors = [...errors, ...(externalErrors || [])];
200
+ const openPreview = () => {
201
+ setPreviewAnswers(items.map((item) => ({
202
+ optionId: item.isExample ? item.optionId : '',
203
+ text: '',
204
+ })));
205
+ setIsClientMode(true);
206
+ };
171
207
  if (isClientMode) {
172
- return (_jsxs("div", { className: "space-y-4", children: [_jsx(CompactPreviewBanner, { onBack: () => setIsClientMode(false) }), _jsx(ChooseThenAnswerGroupClient, { questionData: buildPayload(), isReviewMode: true })] }));
208
+ return (_jsxs("div", { className: "space-y-4", children: [_jsx(CompactPreviewBanner, { onBack: () => setIsClientMode(false) }), _jsx(ChooseThenAnswerGroupClient, { questionData: buildPayload(), userAnswers: previewAnswers, onAnswerChange: (itemIndex, next) => {
209
+ setPreviewAnswers((prev) => {
210
+ const answers = items.map((item, index) => prev[index] || {
211
+ optionId: item.isExample ? item.optionId : '',
212
+ text: '',
213
+ });
214
+ answers[itemIndex] = next;
215
+ return answers;
216
+ });
217
+ } })] }));
173
218
  }
174
- return (_jsxs("div", { className: "space-y-3", children: [displayErrors.length > 0 && (_jsx("ul", { className: "list-disc space-y-1 rounded-md border border-red-200 bg-red-50 p-2 pl-6 text-xs text-red-700", children: displayErrors.map((error) => (_jsx("li", { children: error }, error))) })), _jsx(CompactFieldCard, { header: _jsx(CompactCreatorHeader, { icon: _jsx(ListChecks, { className: "h-3.5 w-3.5" }), title: "Ch\u1ECDn r\u1ED3i tr\u1EA3 l\u1EDDi (Nh\u00F3m)", preview: { onClick: () => setIsClientMode(true) } }), children: _jsxs("div", { className: "grid gap-2.5 md:grid-cols-2", children: [_jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { htmlFor: "cta-title", className: "text-sm font-semibold text-gray-800", children: "Ti\u00EAu \u0111\u1EC1 (optional)" }), _jsx(Input, { id: "cta-title", value: title, onChange: (event) => setTitle(event.target.value), placeholder: "GRAMMAR", className: "h-10 border-gray-200 bg-white" })] }), _jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { htmlFor: "cta-instruction", className: "text-sm font-semibold text-gray-800", children: "H\u01B0\u1EDBng d\u1EABn" }), _jsx(Input, { id: "cta-instruction", value: instruction, onChange: (event) => setInstruction(event.target.value), placeholder: CHOOSE_THEN_ANSWER_GROUP_DEFAULT_INSTRUCTION, className: "h-10 border-gray-200 bg-white" })] })] }) }), _jsxs(Card, { className: "overflow-hidden border-0 bg-white shadow-sm", children: [_jsx(CardHeader, { className: "px-3 py-2", children: _jsxs("div", { className: "flex flex-wrap items-center justify-between gap-2", children: [_jsxs(CardTitle, { className: "text-sm font-semibold text-gray-800", children: ["Danh s\u00E1ch c\u00E2u (", items.length, ")"] }), _jsxs(Button, { type: "button", variant: "outline", size: "sm", onClick: addItem, className: "h-8 gap-1.5 px-2.5 text-xs", children: [_jsx(Plus, { className: "h-3.5 w-3.5" }), " Th\u00EAm c\u00E2u"] })] }) }), _jsxs(CardContent, { className: "space-y-2.5 px-3 pb-3", children: [items.map((item, index) => (_jsxs("div", { className: "space-y-2.5 rounded-md border border-gray-200 bg-white px-3 py-2.5", children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsxs("span", { className: "text-sm font-medium text-gray-700", children: ["C\u00E2u ", index + 1] }), items.length > 1 && (_jsx(Button, { type: "button", variant: "ghost", size: "icon", onClick: () => removeItem(index), className: "h-7 w-7 text-red-500 hover:bg-red-50", children: _jsx(Trash2, { className: "h-3.5 w-3.5" }) }))] }), _jsx(Input, { value: item.question, onChange: (event) => updateItem(index, { question: event.target.value }), placeholder: "Did you eat {0} for lunch today?", className: "h-10 border-gray-200 bg-white" }), _jsxs("p", { className: "text-xs text-slate-500", children: ["D\u00F9ng ", '{0}', " cho ch\u1ED7 hi\u1EC7n chip ch\u1ECDn."] }), item.options.map((option, optionIndex) => (_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(Input, { value: option.text, onChange: (event) => updateOption(index, optionIndex, event.target.value), placeholder: `Lựa chọn ${optionIndex + 1}`, className: "h-10 border-gray-200 bg-white" }), item.options.length > 2 && (_jsx(Button, { type: "button", variant: "ghost", size: "icon", onClick: () => removeOption(index, optionIndex), className: "h-7 w-7 text-red-500 hover:bg-red-50", children: _jsx(Trash2, { className: "h-3.5 w-3.5" }) }))] }, option.id))), _jsxs(Button, { type: "button", size: "sm", variant: "outline", onClick: () => addOption(index), className: "h-8 gap-1.5 px-2.5 text-xs", children: [_jsx(Plus, { className: "h-3.5 w-3.5" }), " Th\u00EAm l\u1EF1a ch\u1ECDn"] }), _jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsx("select", { className: "h-10 rounded-md border border-input bg-background px-3 text-sm", value: item.optionId, onChange: (event) => updateItem(index, { optionId: event.target.value }), children: item.options.map((option) => (_jsx("option", { value: option.id, children: option.text || option.id }, option.id))) }), _jsxs("div", { className: "flex items-center gap-1", children: [_jsx(Switch, { id: `cta-example-${index}`, checked: item.isExample === true, onCheckedChange: (checked) => updateItem(index, { isExample: checked }) }), _jsxs(Label, { htmlFor: `cta-example-${index}`, className: "flex cursor-pointer items-center gap-1 text-xs", children: [_jsx(Star, { className: "h-3 w-3 text-amber-500" }), "Example"] })] }), !item.isExample && (_jsx(PointsInput, { value: item.points, allowZero: true, onChange: (event) => updateItem(index, {
175
- points: Number.parseInt(event.target.value, 10) || 1,
176
- }) }))] })] }, `${item.questionNumber}-${index}`))), _jsx(CompactExplanationToggle, { value: explanation, onValueChange: setExplanation, defaultVisible: Boolean(initialData?.explanation) })] })] })] }));
219
+ return (_jsxs("div", { className: "space-y-3", children: [displayErrors.length > 0 && (_jsx("ul", { className: "list-disc space-y-1 rounded-md border border-red-200 bg-red-50 p-2 pl-6 text-xs text-red-700", children: displayErrors.map((error) => (_jsx("li", { children: error }, error))) })), _jsxs(CompactFieldCard, { header: _jsx(CompactCreatorHeader, { icon: _jsx(ListChecks, { className: "h-3.5 w-3.5" }), title: "Ch\u1ECDn r\u1ED3i tr\u1EA3 l\u1EDDi (Nh\u00F3m)", preview: { onClick: openPreview } }), children: [_jsxs("div", { className: "grid gap-2.5 md:grid-cols-2", children: [_jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { htmlFor: "cta-title", className: "text-sm font-semibold text-gray-800", children: "Ti\u00EAu \u0111\u1EC1 (optional)" }), _jsx(Input, { id: "cta-title", value: title, onChange: (event) => setTitle(event.target.value), placeholder: "GRAMMAR", className: "h-10 border-gray-200 bg-white" })] }), _jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { htmlFor: "cta-instruction", className: "text-sm font-semibold text-gray-800", children: "H\u01B0\u1EDBng d\u1EABn" }), _jsx(Input, { id: "cta-instruction", value: instruction, onChange: (event) => setInstruction(event.target.value), placeholder: CHOOSE_THEN_ANSWER_GROUP_DEFAULT_INSTRUCTION, className: "h-10 border-gray-200 bg-white" })] })] }), _jsxs("p", { className: "mt-2.5 rounded-lg border border-indigo-100 bg-indigo-50/70 px-3 py-2 text-xs leading-relaxed text-indigo-800", children: ["M\u1ED7i c\u00E2u c\u00F3 2 ph\u1EA7n: ", _jsx("strong", { children: "ch\u1ECDn t\u1EEB \u0111\u00FAng" }), " trong c\u00E2u (mapping), r\u1ED3i", ' ', _jsx("strong", { children: "vi\u1EBFt c\u00E2u tr\u1EA3 l\u1EDDi" }), " (AI). V\u00ED d\u1EE5: Did you eat many | much for lunch today? \u2192 No, I didn\u2019t eat much for lunch today."] })] }), _jsxs(Card, { className: "overflow-hidden border-0 bg-white shadow-sm", children: [_jsx(CardHeader, { className: "px-3 py-2", children: _jsxs("div", { className: "flex flex-wrap items-center justify-between gap-2", children: [_jsxs(CardTitle, { className: "text-sm font-semibold text-gray-800", children: ["Danh s\u00E1ch c\u00E2u (", items.length, ")"] }), _jsxs(Button, { type: "button", variant: "outline", size: "sm", onClick: addItem, className: "h-8 gap-1.5 px-2.5 text-xs", children: [_jsx(Plus, { className: "h-3.5 w-3.5" }), " Th\u00EAm c\u00E2u"] })] }) }), _jsxs(CardContent, { className: "space-y-2.5 px-3 pb-3", children: [items.map((item, index) => (_jsxs("div", { className: "space-y-2.5 rounded-md border border-gray-200 bg-white px-3 py-2.5", children: [_jsxs("div", { className: "flex flex-wrap items-center justify-between gap-2", children: [_jsxs("span", { className: "text-sm font-medium text-gray-700", children: ["C\u00E2u ", index + 1] }), _jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsxs("div", { className: "flex items-center gap-1", children: [_jsx(Switch, { id: `cta-example-${index}`, checked: item.isExample === true, onCheckedChange: (checked) => updateItem(index, { isExample: checked }) }), _jsxs(Label, { htmlFor: `cta-example-${index}`, className: "flex cursor-pointer items-center gap-1 text-xs", children: [_jsx(Star, { className: "h-3 w-3 text-amber-500" }), "Example"] })] }), !item.isExample && (_jsxs("div", { className: "space-y-0.5", children: [_jsx(PointsInput, { value: item.points, allowZero: true, onChange: (event) => updateItem(index, {
220
+ points: Number.parseInt(event.target.value, 10) || 1,
221
+ }) }), _jsx("p", { className: "text-[10px] leading-tight text-slate-500", children: "C\u1EA3 c\u00E2u: 50% ch\u1ECDn + 50% vi\u1EBFt" })] })), items.length > 1 && (_jsx(Button, { type: "button", variant: "ghost", size: "icon", onClick: () => removeItem(index), className: "h-7 w-7 text-red-500 hover:bg-red-50", children: _jsx(Trash2, { className: "h-3.5 w-3.5" }) }))] })] }), _jsxs("div", { className: "space-y-2 rounded-lg border border-slate-200 bg-slate-50/60 px-3 py-2.5", children: [_jsxs("p", { className: "flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600", children: [_jsx(ListChecks, { className: "h-3.5 w-3.5" }), "1. Ch\u1ECDn t\u1EEB \u0111\u00FAng"] }), _jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { className: "text-sm font-semibold text-gray-800", children: "C\u00E2u h\u1ECFi" }), _jsx(Input, { value: item.question, onChange: (event) => updateItem(index, { question: event.target.value }), placeholder: "Did you eat {0} for lunch today?", className: "h-10 border-gray-200 bg-white" }), _jsxs("p", { className: "text-xs text-slate-500", children: ["D\u00F9ng ", '{0}', " cho ch\u1ED7 hi\u1EC7n chip ch\u1ECDn."] })] }), _jsxs("div", { className: "space-y-1.5", children: [_jsx(Label, { className: "text-sm font-semibold text-gray-800", children: "L\u1EF1a ch\u1ECDn" }), _jsx("p", { className: "text-xs text-slate-500", children: "Ch\u1ECDn radio \u0111\u1EC3 \u0111\u00E1nh d\u1EA5u \u0111\u00E1p \u00E1n \u0111\u00FAng (chip)." }), item.options.map((option, optionIndex) => {
222
+ const isCorrect = item.optionId === option.id;
223
+ return (_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("input", { type: "radio", name: `cta-correct-${index}`, checked: isCorrect, onChange: () => updateItem(index, { optionId: option.id }), "aria-label": `Đáp án đúng: ${option.text || option.id}`, className: "h-4 w-4 shrink-0 accent-indigo-600" }), _jsx(Input, { value: option.text, onChange: (event) => updateOption(index, optionIndex, event.target.value), placeholder: `Lựa chọn ${optionIndex + 1}`, className: "h-10 border-gray-200 bg-white" }), isCorrect && (_jsx("span", { className: "shrink-0 text-[10px] font-semibold uppercase text-indigo-600", children: "\u0110\u00FAng" })), item.options.length > 2 && (_jsx(Button, { type: "button", variant: "ghost", size: "icon", onClick: () => removeOption(index, optionIndex), className: "h-7 w-7 text-red-500 hover:bg-red-50", children: _jsx(Trash2, { className: "h-3.5 w-3.5" }) }))] }, option.id));
224
+ })] }), _jsxs(Button, { type: "button", size: "sm", variant: "outline", onClick: () => addOption(index), className: "h-8 gap-1.5 px-2.5 text-xs", children: [_jsx(Plus, { className: "h-3.5 w-3.5" }), " Th\u00EAm l\u1EF1a ch\u1ECDn"] })] }), _jsxs("div", { className: "space-y-2 rounded-lg border border-violet-100 bg-violet-50/50 px-3 py-2.5", children: [_jsxs("p", { className: "flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-violet-700", children: [_jsx(Sparkles, { className: "h-3.5 w-3.5" }), "2. Tr\u1EA3 l\u1EDDi c\u00E2u h\u1ECFi (AI)"] }), _jsxs("p", { className: "flex items-start gap-1.5 text-xs text-violet-800/80", children: [_jsx(PenLine, { className: "mt-0.5 h-3.5 w-3.5 shrink-0" }), "H\u1ECDc sinh vi\u1EBFt c\u00E2u tr\u1EA3 l\u1EDDi c\u1EE7a m\u00ECnh. \u0110\u00E1p \u00E1n m\u1EABu gi\u00FAp AI ch\u1EA5m theo ngh\u0129a (\u0111\u1EC3 tr\u1ED1ng = ch\u1EA5m \u0111\u00FAng ch\u1EE7 \u0111\u1EC1)."] }), (item.expectedAnswers || ['']).map((answer, answerIndex) => (_jsxs("div", { className: "flex items-start gap-2", children: [_jsx("textarea", { value: answer, onChange: (event) => updateExpectedAnswer(index, answerIndex, event.target.value), placeholder: "No, I didn\u2019t eat much for lunch today.", rows: 2, className: "min-h-[3.25rem] w-full resize-none rounded-md border border-gray-200 bg-white px-3 py-2 text-sm outline-none focus:border-violet-400 focus:ring-2 focus:ring-violet-100" }), (item.expectedAnswers || ['']).length > 1 && (_jsx(Button, { type: "button", variant: "ghost", size: "icon", onClick: () => removeExpectedAnswer(index, answerIndex), className: "h-7 w-7 text-red-500 hover:bg-red-50", children: _jsx(Trash2, { className: "h-3.5 w-3.5" }) }))] }, `cta-expected-${index}-${answerIndex}`))), _jsxs(Button, { type: "button", size: "sm", variant: "outline", onClick: () => addExpectedAnswer(index), className: "h-8 gap-1.5 px-2.5 text-xs", children: [_jsx(Plus, { className: "h-3.5 w-3.5" }), " Th\u00EAm \u0111\u00E1p \u00E1n m\u1EABu"] })] })] }, `${item.questionNumber}-${index}`))), _jsx(CompactExplanationToggle, { value: explanation, onValueChange: setExplanation, defaultVisible: Boolean(initialData?.explanation) })] })] })] }));
177
225
  }
@@ -1,11 +1,14 @@
1
- import type { ChooseThenAnswerGroupData, ChooseThenAnswerGroupItemData, ChooseThenAnswerOption, ChooseThenAnswerStudentItemAnswer } from '../../_shared/types/choose-then-answer-group.type';
1
+ import type { ChooseThenAnswerGradeDetail, ChooseThenAnswerGroupData, ChooseThenAnswerGroupItemData, ChooseThenAnswerItemResult, ChooseThenAnswerOption, ChooseThenAnswerStudentItemAnswer } from '../../_shared/types/choose-then-answer-group.type';
2
2
  export declare function nextPrefixedId(prefix: string, existing: string[]): string;
3
3
  export declare function sumGradablePoints(items: ChooseThenAnswerGroupItemData[]): number;
4
4
  export declare function createEmptyOption(existingIds: string[]): ChooseThenAnswerOption;
5
+ export declare function normalizeExpectedAnswers(value: unknown): string[];
5
6
  export declare function createEmptyItem(questionNumber: number): ChooseThenAnswerGroupItemData;
6
7
  export declare function parseItemAnswer(raw: unknown): {
7
8
  optionId: string;
9
+ expectedAnswers: string[];
8
10
  };
9
11
  export declare function parseStudentAnswer(raw: unknown): ChooseThenAnswerStudentItemAnswer;
10
12
  export declare function mapApiItem(item: unknown, index: number, fallbackAnswer?: unknown): ChooseThenAnswerGroupItemData;
11
13
  export declare function mapQuestionToChooseThenAnswerGroupData(question: Record<string, unknown> | null | undefined): ChooseThenAnswerGroupData;
14
+ export declare function toCtaItemResults(details: ChooseThenAnswerGradeDetail[] | undefined, items: ChooseThenAnswerGroupItemData[], userAnswers: ChooseThenAnswerStudentItemAnswer[]): ChooseThenAnswerItemResult[] | undefined;
@@ -28,6 +28,16 @@ export function createEmptyOption(existingIds) {
28
28
  text: '',
29
29
  };
30
30
  }
31
+ export function normalizeExpectedAnswers(value) {
32
+ if (!Array.isArray(value)) {
33
+ return [''];
34
+ }
35
+ const filled = value
36
+ .filter((item) => typeof item === 'string')
37
+ .map((item) => item.trim())
38
+ .filter((item) => item !== '');
39
+ return filled.length > 0 ? filled : [''];
40
+ }
31
41
  export function createEmptyItem(questionNumber) {
32
42
  const first = createEmptyOption([]);
33
43
  const second = createEmptyOption([first.id]);
@@ -35,6 +45,7 @@ export function createEmptyItem(questionNumber) {
35
45
  question: '',
36
46
  options: [first, second],
37
47
  optionId: first.id,
48
+ expectedAnswers: [''],
38
49
  isExample: false,
39
50
  points: 1,
40
51
  questionNumber,
@@ -42,8 +53,11 @@ export function createEmptyItem(questionNumber) {
42
53
  }
43
54
  export function parseItemAnswer(raw) {
44
55
  if (!isRecord(raw))
45
- return { optionId: '' };
46
- return { optionId: asString(raw.optionId) };
56
+ return { optionId: '', expectedAnswers: [''] };
57
+ return {
58
+ optionId: asString(raw.optionId),
59
+ expectedAnswers: normalizeExpectedAnswers(raw.expectedAnswers),
60
+ };
47
61
  }
48
62
  export function parseStudentAnswer(raw) {
49
63
  if (!isRecord(raw))
@@ -78,6 +92,7 @@ export function mapApiItem(item, index, fallbackAnswer) {
78
92
  question: asString(content.question) || asString(record.question),
79
93
  options,
80
94
  optionId: parsed.optionId || options[0]?.id || '',
95
+ expectedAnswers: parsed.expectedAnswers,
81
96
  isExample,
82
97
  points: isExample ? 0 : (typeof record.points === 'number' ? record.points : 1),
83
98
  questionNumber: typeof record.questionNumber === 'number' ? record.questionNumber : index + 1,
@@ -125,3 +140,25 @@ export function mapQuestionToChooseThenAnswerGroupData(question) {
125
140
  points: 1,
126
141
  };
127
142
  }
143
+ export function toCtaItemResults(details, items, userAnswers) {
144
+ if (!details?.length) {
145
+ return undefined;
146
+ }
147
+ const byId = new Map(details.map((detail) => [detail.itemId, detail]));
148
+ return items.map((item, index) => {
149
+ const detail = byId.get(`item-${index}`);
150
+ const mappingCorrect = Boolean(item.optionId) && userAnswers[index]?.optionId === item.optionId;
151
+ const half = (typeof item.points === 'number' ? item.points : 1) * 0.5;
152
+ let aiCorrect;
153
+ if (detail?.partialScore != null) {
154
+ const mappingHalf = mappingCorrect ? half : 0;
155
+ aiCorrect = detail.partialScore - mappingHalf > 1e-9;
156
+ }
157
+ return {
158
+ mappingCorrect,
159
+ aiCorrect,
160
+ partialScore: detail?.partialScore,
161
+ feedback: detail?.feedback,
162
+ };
163
+ });
164
+ }
@@ -1,5 +1,8 @@
1
1
  import { CHOOSE_THEN_ANSWER_GROUP_DEFAULT_INSTRUCTION, CHOOSE_THEN_ANSWER_ITEM_TYPE, } from '../../_shared/types/choose-then-answer-group.type';
2
2
  import { sumGradablePoints } from './map-choose-then-answer-group-data';
3
+ function filledExpectedAnswers(values) {
4
+ return (values || []).map((value) => value.trim()).filter((value) => value !== '');
5
+ }
3
6
  export const transformChooseThenAnswerGroup = (question) => {
4
7
  const answerData = question.answer;
5
8
  const items = Array.isArray(answerData?.items) ? answerData.items : [];
@@ -7,25 +10,35 @@ export const transformChooseThenAnswerGroup = (question) => {
7
10
  return { apiContent: { items: [] }, apiCorrectAnswer: { answer: [] } };
8
11
  }
9
12
  const title = typeof answerData?.title === 'string' ? answerData.title.trim() : '';
10
- const apiItems = items.map((item, index) => ({
11
- questionType: CHOOSE_THEN_ANSWER_ITEM_TYPE,
12
- ...(item.isExample ? { isExample: true } : {}),
13
- content: {
14
- question: item.question || '',
15
- options: (item.options || []).map((option) => ({
16
- id: option.id || '',
17
- text: option.text || '',
18
- })),
19
- },
20
- correctAnswer: { answer: { optionId: item.optionId || '' } },
21
- points: item.isExample ? 0 : (typeof item.points === 'number' ? item.points : 1),
22
- questionNumber: item.questionNumber || index + 1,
23
- }));
13
+ const apiItems = items.map((item, index) => {
14
+ const expectedAnswers = filledExpectedAnswers(item.expectedAnswers);
15
+ return {
16
+ questionType: CHOOSE_THEN_ANSWER_ITEM_TYPE,
17
+ ...(item.isExample ? { isExample: true } : {}),
18
+ content: {
19
+ question: item.question || '',
20
+ options: (item.options || []).map((option) => ({
21
+ id: option.id || '',
22
+ text: option.text || '',
23
+ })),
24
+ },
25
+ correctAnswer: {
26
+ answer: {
27
+ optionId: item.optionId || '',
28
+ ...(expectedAnswers.length > 0 ? { expectedAnswers } : {}),
29
+ },
30
+ },
31
+ points: item.isExample ? 0 : (typeof item.points === 'number' ? item.points : 1),
32
+ questionNumber: item.questionNumber || index + 1,
33
+ };
34
+ });
24
35
  return {
25
36
  apiContent: {
26
37
  meta: {
27
38
  instruction: answerData?.instruction || CHOOSE_THEN_ANSWER_GROUP_DEFAULT_INSTRUCTION,
28
39
  ...(title ? { title } : {}),
40
+ gradingType: 'AI',
41
+ isAiGraded: true,
29
42
  },
30
43
  items: apiItems,
31
44
  },
@@ -1,2 +1,2 @@
1
1
  import type { CrossOutWordGroupClientProps } from '../../_shared/types/cross-out-word-group.type';
2
- export declare function CrossOutWordGroupClient({ questionData, isReviewMode, userAnswers, }: CrossOutWordGroupClientProps): import("react").JSX.Element;
2
+ export declare function CrossOutWordGroupClient({ questionData, isReviewMode, userAnswers, onAnswerChange, }: CrossOutWordGroupClientProps): import("react").JSX.Element;
@@ -2,6 +2,12 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { CircleHelp, Lightbulb, Star } from 'lucide-react';
4
4
  import { cn } from '../../../../shared/lib/utils';
5
+ function tokenize(sentence) {
6
+ return sentence.trim().split(/\s+/).filter(Boolean);
7
+ }
8
+ function normalizeWord(word) {
9
+ return word.replace(/[^\p{L}\p{N}'-]/gu, '').toLowerCase();
10
+ }
5
11
  function formatAnswer(answer) {
6
12
  if (!answer)
7
13
  return '—';
@@ -15,13 +21,58 @@ function formatAnswer(answer) {
15
21
  return 'câu đúng (không gạch)';
16
22
  return `gạch "${answer.crossOut}"${posInfo}`;
17
23
  }
18
- export function CrossOutWordGroupClient({ questionData, isReviewMode = false, userAnswers = [], }) {
24
+ export function CrossOutWordGroupClient({ questionData, isReviewMode = false, userAnswers = [], onAnswerChange, }) {
19
25
  const items = questionData.items || [];
26
+ const emit = (itemIndex, nextItem) => {
27
+ const item = items[itemIndex];
28
+ if (isReviewMode || item?.isExample || !onAnswerChange) {
29
+ return;
30
+ }
31
+ const next = items.map((_, index) => userAnswers[index] ?? null);
32
+ next[itemIndex] = nextItem;
33
+ onAnswerChange(next);
34
+ };
20
35
  return (_jsxs("div", { className: "space-y-4", children: [questionData.instruction && (_jsx("div", { className: "rounded-xl border border-indigo-100 bg-indigo-50 px-4 py-3 text-sm font-medium text-indigo-800", children: questionData.instruction })), items.map((item, itemIndex) => {
21
- const userValue = userAnswers[itemIndex];
22
36
  const isReplace = item.type === 'CROSS_OUT_AND_REPLACE';
37
+ const userValue = item.isExample ? item.correctAnswer : userAnswers[itemIndex];
38
+ const canSelect = !isReviewMode && !item.isExample && Boolean(onAnswerChange);
39
+ const tokens = tokenize(item.question || '');
40
+ const selectedIndex = userValue?.wordIndex;
41
+ const selectedWord = userValue?.crossOut || '';
42
+ const replaceWith = userValue?.replaceWith || '';
43
+ const correct = item.correctAnswer;
23
44
  return (_jsxs("div", { className: "overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm", children: [_jsxs("div", { className: "flex items-center justify-between border-b border-gray-100 bg-gradient-to-r from-gray-50 to-white px-3 py-2", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("div", { className: "flex h-7 w-7 items-center justify-center rounded-lg bg-gradient-to-br from-blue-500 to-indigo-600 text-white", children: _jsx(CircleHelp, { className: "h-3.5 w-3.5" }) }), _jsxs("span", { className: "text-sm font-medium text-gray-700", children: ["C\u00E2u ", item.questionNumber || itemIndex + 1] }), item.isExample && (_jsxs("span", { className: "inline-flex items-center gap-1 rounded-full bg-amber-100 px-2 py-0.5 text-xs font-semibold text-amber-700", children: [_jsx(Star, { className: "h-3 w-3" }), "Example"] })), _jsx("span", { className: cn('rounded-full px-2 py-0.5 text-xs font-semibold', isReplace
24
45
  ? 'bg-violet-100 text-violet-700'
25
- : 'bg-sky-100 text-sky-700'), children: isReplace ? 'Gạch + thay' : 'Chỉ gạch' })] }), _jsxs("span", { className: "text-xs text-gray-500", children: [item.points || 0, " \u0111i\u1EC3m"] })] }), _jsxs("div", { className: "space-y-3 p-3", children: [_jsx("p", { className: "text-sm font-semibold leading-relaxed text-gray-800", children: item.question || 'Câu hỏi chưa được nhập' }), _jsxs("div", { className: "rounded-lg border border-gray-100 bg-gray-50 px-3 py-2 text-sm text-gray-700", children: [_jsx("span", { className: "font-medium text-gray-500", children: "\u0110\u00E1p \u00E1n: " }), formatAnswer(item.correctAnswer)] }), isReviewMode && (_jsxs("div", { className: "rounded-lg border border-blue-100 bg-blue-50 px-3 py-2 text-sm text-blue-800", children: [_jsx("span", { className: "font-medium", children: "H\u1ECDc sinh: " }), formatAnswer(userValue)] }))] })] }, `${item.questionNumber}-${itemIndex}`));
46
+ : 'bg-sky-100 text-sky-700'), children: isReplace ? 'Gạch + thay' : 'Chỉ gạch' })] }), _jsxs("span", { className: "text-xs text-gray-500", children: [item.points || 0, " \u0111i\u1EC3m"] })] }), _jsxs("div", { className: "space-y-3 p-3", children: [isReplace ? (_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("label", { className: "text-xs font-semibold text-slate-600", children: "T\u1EEB \u0111\u00FAng:" }), _jsx("input", { type: "text", value: replaceWith, disabled: !canSelect, onChange: (event) => emit(itemIndex, {
47
+ type: item.type,
48
+ wordIndex: selectedIndex ?? null,
49
+ crossOut: selectedWord,
50
+ replaceWith: event.target.value,
51
+ }), placeholder: "Nh\u1EADp t\u1EEB thay th\u1EBF...", className: "h-8 min-w-[10rem] rounded-lg border border-slate-200 px-2.5 text-sm outline-none focus:border-blue-500 focus:ring-2 focus:ring-blue-100 disabled:bg-slate-50" })] })) : null, _jsx("div", { className: "flex flex-wrap items-center gap-1.5 text-sm font-medium leading-relaxed text-slate-800", children: tokens.map((token, tokenIndex) => {
52
+ const plain = normalizeWord(token);
53
+ const isSelected = typeof selectedIndex === 'number'
54
+ ? selectedIndex === tokenIndex
55
+ : Boolean(selectedWord) && normalizeWord(selectedWord) === plain && plain !== '';
56
+ const isCorrectTarget = isReviewMode && !item.isExample && correct?.wordIndex === tokenIndex;
57
+ return (_jsx("button", { type: "button", disabled: !canSelect || plain === '', onClick: () => {
58
+ const already = typeof selectedIndex === 'number'
59
+ ? selectedIndex === tokenIndex
60
+ : selectedWord === token;
61
+ emit(itemIndex, {
62
+ type: item.type,
63
+ wordIndex: already ? null : tokenIndex,
64
+ crossOut: already
65
+ ? ''
66
+ : token.replace(/[^\p{L}\p{N}'-]/gu, '') || token,
67
+ ...(isReplace ? { replaceWith } : {}),
68
+ });
69
+ }, className: cn('rounded-lg px-2 py-1 transition-colors', isSelected
70
+ ? 'border border-blue-200 bg-blue-100 font-bold text-blue-700 line-through decoration-blue-600'
71
+ : isCorrectTarget
72
+ ? 'border-2 border-dashed border-emerald-500 bg-emerald-50 font-bold text-emerald-800'
73
+ : canSelect
74
+ ? 'cursor-pointer hover:bg-blue-50 hover:text-blue-700'
75
+ : 'cursor-default'), children: token }, `${item.questionNumber}-token-${tokenIndex}`));
76
+ }) }), isReviewMode ? (_jsxs("div", { className: "rounded-lg border border-emerald-100 bg-emerald-50 px-3 py-2 text-sm text-emerald-800", children: [_jsx("span", { className: "font-medium", children: "\u0110\u00E1p \u00E1n: " }), formatAnswer(item.correctAnswer)] })) : null] })] }, `${item.questionNumber}-${itemIndex}`));
26
77
  }), isReviewMode && questionData.explanation && (_jsxs("div", { className: "flex items-start gap-2 rounded-xl border border-amber-100 bg-amber-50 px-4 py-3 text-sm text-amber-800", children: [_jsx(Lightbulb, { className: "mt-0.5 h-4 w-4 flex-shrink-0" }), questionData.explanation] }))] }));
27
78
  }
@@ -7,5 +7,5 @@ interface CrosswordPuzzleClientProps {
7
7
  userAnswers?: Record<string, string>;
8
8
  onAnswerChange?: (key: string, value: string) => void;
9
9
  }
10
- export declare function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode, userAnswers, }: CrosswordPuzzleClientProps): React.JSX.Element;
10
+ export declare function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode, userAnswers, onAnswerChange, }: CrosswordPuzzleClientProps): React.JSX.Element;
11
11
  export {};
@@ -1,7 +1,7 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { useMemo } from 'react';
4
- export function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode = false, userAnswers = {}, }) {
4
+ export function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode = false, userAnswers = {}, onAnswerChange, }) {
5
5
  const rows = content?.gridSize?.rows || 12;
6
6
  const cols = content?.gridSize?.cols || 12;
7
7
  const acrossClues = useMemo(() => content?.clues?.across || [], [content?.clues?.across]);
@@ -33,11 +33,11 @@ export function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode = f
33
33
  return (_jsxs("div", { className: "flex flex-col gap-3 p-3 bg-white rounded-xl border border-gray-200 shadow-sm", children: [content?.title && (_jsx("h3", { className: "text-lg font-bold text-gray-800 text-center", children: content.title })), _jsxs("div", { className: "grid grid-cols-1 lg:grid-cols-12 gap-6 items-start", children: [_jsxs("div", { className: "lg:col-span-5 flex flex-col gap-4", children: [_jsxs("div", { className: "rounded-lg border border-blue-200 bg-blue-50/50 p-4", children: [_jsxs("h4", { className: "font-bold text-blue-900 mb-2.5 flex items-center gap-2", children: [_jsx("span", { className: "px-2 py-0.5 rounded bg-blue-600 text-white text-xs font-semibold", children: "Across" }), _jsx("span", { children: "H\u00E0ng ngang" })] }), _jsx("div", { className: "flex flex-col gap-2 max-h-60 overflow-y-auto pr-1", children: acrossClues.map((c) => {
34
34
  const answer = correctAnswer?.answers?.[`across-${c.number}`];
35
35
  const userAns = userAnswers[`across-${c.number}`];
36
- return (_jsxs("div", { className: "flex items-start gap-2 text-sm bg-white p-2 rounded border border-blue-100", children: [_jsxs("span", { className: "font-bold text-blue-700 min-w-[20px]", children: [c.number, "."] }), _jsxs("div", { className: "flex-1", children: [_jsx("span", { className: "text-gray-800", children: c.clue }), isReviewMode && answer && (_jsxs("div", { className: "text-xs mt-1", children: [_jsx("span", { className: "text-gray-500", children: "\u0110\u00E1p \u00E1n: " }), _jsx("span", { className: "font-bold text-green-700", children: answer }), userAns && userAns !== answer && (_jsx("span", { className: "ml-2 line-through text-red-500 font-semibold", children: userAns }))] }))] }), _jsxs("span", { className: "text-xs text-gray-400", children: ["(", c.length, " ch\u1EEF)"] })] }, `across-${c.number}`));
36
+ return (_jsxs("div", { className: "flex items-start gap-2 text-sm bg-white p-2 rounded border border-blue-100", children: [_jsxs("span", { className: "font-bold text-blue-700 min-w-[20px]", children: [c.number, "."] }), _jsxs("div", { className: "flex-1", children: [_jsx("span", { className: "text-gray-800", children: c.clue }), !isReviewMode && onAnswerChange ? (_jsx("input", { type: "text", value: userAns || '', maxLength: c.length, onChange: (event) => onAnswerChange(`across-${c.number}`, event.target.value.toUpperCase()), placeholder: `${c.length} chữ`, className: "mt-1.5 w-full rounded border border-blue-200 px-2 py-1 text-sm font-semibold uppercase tracking-wider outline-none focus:border-blue-500" })) : null, isReviewMode && answer && (_jsxs("div", { className: "text-xs mt-1", children: [_jsx("span", { className: "text-gray-500", children: "\u0110\u00E1p \u00E1n: " }), _jsx("span", { className: "font-bold text-green-700", children: answer }), userAns && userAns !== answer && (_jsx("span", { className: "ml-2 line-through text-red-500 font-semibold", children: userAns }))] }))] }), _jsxs("span", { className: "text-xs text-gray-400", children: ["(", c.length, " ch\u1EEF)"] })] }, `across-${c.number}`));
37
37
  }) })] }), _jsxs("div", { className: "rounded-lg border border-purple-200 bg-purple-50/50 p-4", children: [_jsxs("h4", { className: "font-bold text-purple-900 mb-2.5 flex items-center gap-2", children: [_jsx("span", { className: "px-2 py-0.5 rounded bg-purple-600 text-white text-xs font-semibold", children: "Down" }), _jsx("span", { children: "H\u00E0ng d\u1ECDc" })] }), _jsx("div", { className: "flex flex-col gap-2 max-h-60 overflow-y-auto pr-1", children: downClues.map((c) => {
38
38
  const answer = correctAnswer?.answers?.[`down-${c.number}`];
39
39
  const userAns = userAnswers[`down-${c.number}`];
40
- return (_jsxs("div", { className: "flex items-start gap-2 text-sm bg-white p-2 rounded border border-purple-100", children: [_jsxs("span", { className: "font-bold text-purple-700 min-w-[20px]", children: [c.number, "."] }), _jsxs("div", { className: "flex-1", children: [_jsx("span", { className: "text-gray-800", children: c.clue }), isReviewMode && answer && (_jsxs("div", { className: "text-xs mt-1", children: [_jsx("span", { className: "text-gray-500", children: "\u0110\u00E1p \u00E1n: " }), _jsx("span", { className: "font-bold text-green-700", children: answer }), userAns && userAns !== answer && (_jsx("span", { className: "ml-2 line-through text-red-500 font-semibold", children: userAns }))] }))] }), _jsxs("span", { className: "text-xs text-gray-400", children: ["(", c.length, " ch\u1EEF)"] })] }, `down-${c.number}`));
40
+ return (_jsxs("div", { className: "flex items-start gap-2 text-sm bg-white p-2 rounded border border-purple-100", children: [_jsxs("span", { className: "font-bold text-purple-700 min-w-[20px]", children: [c.number, "."] }), _jsxs("div", { className: "flex-1", children: [_jsx("span", { className: "text-gray-800", children: c.clue }), !isReviewMode && onAnswerChange ? (_jsx("input", { type: "text", value: userAns || '', maxLength: c.length, onChange: (event) => onAnswerChange(`down-${c.number}`, event.target.value.toUpperCase()), placeholder: `${c.length} chữ`, className: "mt-1.5 w-full rounded border border-purple-200 px-2 py-1 text-sm font-semibold uppercase tracking-wider outline-none focus:border-purple-500" })) : null, isReviewMode && answer && (_jsxs("div", { className: "text-xs mt-1", children: [_jsx("span", { className: "text-gray-500", children: "\u0110\u00E1p \u00E1n: " }), _jsx("span", { className: "font-bold text-green-700", children: answer }), userAns && userAns !== answer && (_jsx("span", { className: "ml-2 line-through text-red-500 font-semibold", children: userAns }))] }))] }), _jsxs("span", { className: "text-xs text-gray-400", children: ["(", c.length, " ch\u1EEF)"] })] }, `down-${c.number}`));
41
41
  }) })] })] }), _jsx("div", { className: "lg:col-span-7 flex justify-center overflow-x-auto p-4 bg-gray-50 rounded-xl border border-gray-200", children: _jsx("div", { className: "inline-grid gap-1", style: {
42
42
  gridTemplateColumns: `repeat(${cols}, minmax(0, 1fr))`,
43
43
  }, children: Array.from({ length: rows }).map((_, rIdx) => {
@@ -66,8 +66,18 @@ export function CrosswordPuzzleClient({ content, correctAnswer, isReviewMode = f
66
66
  previewLetter = word[offset] || '';
67
67
  }
68
68
  }
69
- else if (userAnswers?.[key]) {
70
- previewLetter = userAnswers[key];
69
+ else {
70
+ if (cellInfo.across) {
71
+ const word = userAnswers[`across-${cellInfo.across.number}`] || '';
72
+ previewLetter = word[c - cellInfo.across.startCol] || '';
73
+ }
74
+ if (!previewLetter && cellInfo.down) {
75
+ const word = userAnswers[`down-${cellInfo.down.number}`] || '';
76
+ previewLetter = word[r - cellInfo.down.startRow] || '';
77
+ }
78
+ if (!previewLetter) {
79
+ previewLetter = userAnswers[key] || '';
80
+ }
71
81
  }
72
82
  const cellBg = isMarked ? 'bg-gray-400 border-gray-400 shadow-sm ring-1 ring-gray-400/50' : 'bg-gray-100 border-gray-300';
73
83
  let textColor = isExample
@@ -1,9 +1,19 @@
1
1
  import React from 'react';
2
- import type { FindWordsInMatrixContent, FindWordsInMatrixCorrectAnswer } from '../../../../shared/types/questions/find-words-in-matrix';
2
+ import type { FindWordsInMatrixContent, FindWordsInMatrixCorrectAnswer, MatrixCellCoord } from '../../../../shared/types/questions/find-words-in-matrix';
3
+ type WordSelection = {
4
+ wordId?: string;
5
+ word?: string;
6
+ categoryId?: string;
7
+ path: MatrixCellCoord[];
8
+ };
3
9
  interface FindWordsInMatrixClientProps {
4
10
  content: FindWordsInMatrixContent;
5
11
  correctAnswer?: FindWordsInMatrixCorrectAnswer;
6
12
  showSolution?: boolean;
13
+ userAnswer?: unknown;
14
+ onAnswerChange?: (payload: {
15
+ words: WordSelection[];
16
+ }) => void;
7
17
  }
8
- export declare function FindWordsInMatrixClient({ content, correctAnswer, showSolution, }: FindWordsInMatrixClientProps): React.JSX.Element;
18
+ export declare function FindWordsInMatrixClient({ content, correctAnswer, showSolution, userAnswer, onAnswerChange, }: FindWordsInMatrixClientProps): React.JSX.Element;
9
19
  export {};