import React, { Component } from 'react'; import './index.less'; import Assets from '@src/components/Assets'; import { formatSecond, formatPercent, formatSeconds } from '@src/services/Tools'; import Icon from '../../../../components/Icon'; import Button from '../../../../components/Button'; import Switch from '../../../../components/Switch'; import Tabs from '../../../../components/Tabs'; import Progress from '../../../../components/Progress'; import HardInput from '../../../../components/HardInput'; import AnswerCheckbox from '../../../../components/AnswerCheckbox'; import { SentenceOption } from '../../../../../Constant'; import { Question } from '../../../../stores/question'; export default class extends Component { constructor(props) { super(props); // 确保可以自身进行答案显示,外部也可以直接显示答案 // 将props转入state this.state = { analysisTab: 'qx', focusKey: 'subject', scene: this.props.scene || 'answer', userQuestion: this.props.userQuestion, question: this.props.question, }; const { question, userQuestion } = this.props; if (this.state.scene === 'answer') { this.state.stem = this.formatStem(question.stem, userQuestion.userAnswer, question.answer); } else { this.state.stem = question.stem; } } showAnswer() { const { userQuestion } = this.state; Question.getDetailById(userQuestion.id).then(result => { const { question } = result; this.setState({ userQuestion: result, question: result.question, scene: 'answer', stem: this.formatStem(question.stem, result.userAnswer, question.answer), }); }); } addTarget(target) { const uuid = target.getAttribute('uuid'); if (!uuid) return; const text = target.innerText; const { focusKey, answer = {}, question } = this.state; if (!answer[focusKey]) answer[focusKey] = []; if (answer[focusKey].filter(row => row.uuid === uuid).length > 0) return; answer[focusKey].push({ text, uuid, }); this.setState({ answer, stem: this.formatStem(question.stem, answer), }); } removeTarget(key, target) { const { answer = {}, question } = this.state; if (!answer[key]) return; answer[key] = answer[key].filter(row => row.uuid !== target.uuid); this.setState({ answer, stem: this.formatStem(question.stem, answer), }); } next() { const { flow } = this.props; const { scene } = this.state; if (scene === 'question') { const { answer } = this.state; flow.submit(answer).then(() => { this.showAnswer(); }); } else if (scene === 'answer') { flow.next(); } } formatStem(stem, userAnswer, answer) { // userAnswer 添加蓝色字, 错误的添加红色背景 // answer 添加绿色背景 const uuidMap = {}; const show = !!answer; answer = answer || {}; userAnswer = userAnswer || {}; Object.keys(userAnswer).forEach(key => { if (key === 'options') return; const u = userAnswer[key]; const a = answer[key] && answer[key].length > 0 ? answer[key][0] : []; const map = {}; a.forEach(row => { if (!uuidMap[row.uuid]) uuidMap[row.uuid] = []; uuidMap[row.uuid].push('true'); map[row.uuid] = row; }); u.forEach(row => { if (!uuidMap[row.uuid]) uuidMap[row.uuid] = []; uuidMap[row.uuid].push('user'); if (show && !map[row.uuid]) uuidMap[row.uuid].push('false'); }); }); Object.keys(uuidMap).forEach(uuid => { stem = stem.replace(`uuid='${uuid}'`, `uuid='${uuid}' class='${uuidMap[uuid].join(' ')}'`); }); return stem; } renderHeader() { const { mode } = this.props; switch (mode) { case 'process': return this.renderProcessHeader(); default: return this.renderQuestionHeader(); } } renderProcessHeader() { const { userQuestion, singleTime, paper, flow } = this.props; return (