Commit 631e2ec4 authored by hewenhao's avatar hewenhao

feat: 竖式加减法错题

parent 593eb9e7
......@@ -21,5 +21,10 @@
&.is_dot {
width: 24px;
height: 112px;
&.is_small {
height: 80px;
width: 17px;
}
}
}
......@@ -2,7 +2,6 @@
display: flex;
flex-direction: column;
align-items: center;
padding-right: 60px;
min-height: 100%;
}
......
......@@ -8,9 +8,11 @@ import {
isBlankQuestion,
isSelectionQuestion,
isVerticalDivisionQuestion,
isFractionAddMinusQuestion
isFractionAddMinusQuestion,
isVerticalAddMinusQuestion
} from '../../../play/types'
import QuestionFractionAddMinus from './question-fraction-add-minus'
import QuestionVerticalAddMinus from './question-vertical-add-minus'
type QuestionProps = {
question: QuestionType
......@@ -43,6 +45,10 @@ const Question = (props: QuestionProps) => {
)
}
if (isVerticalAddMinusQuestion(question)) {
return <QuestionVerticalAddMinus question={question} />
}
return (
<View>not supported question type</View>
)
......
.content {
display: flex;
flex-direction: column;
align-items: center;
min-height: 100%;
}
.gap {
flex: 2;
}
.gap_2 {
flex: 3;
}
.question_content {
display: flex;
align-items: center;
justify-content: center;
}
.item_cell {
min-width: 96px;
height: 80px;
display: flex;
align-items: center;
justify-content: center;
font-size: 50px;
font-weight: 500;
color: #465164;
line-height: 80px;
margin: 0 -8px;
}
.sign_cell {
display: flex;
align-items: center;
justify-content: center;
height: 80px;
min-width: 96px;
margin: 0 -8px;
}
.left {
display: flex;
justify-content: flex-end;
gap: 10px;
}
.right {
display: flex;
justify-content: flex-start;
gap: 10px;
}
.split {
height: 8px;
background: rgba(23, 134, 247, 0.9);
margin: 6px 0;
}
.column_wrap {
display: flex;
flex-direction: column;
gap: 10px;
}
.place_holder {
width: 24px;
height: 80px;
}
/** 填空题 */
import { View } from '@tarojs/components'
import { useMemo } from 'react'
import classNames from 'classnames'
import htmlParser, { Node } from 'html-parse-stringify'
import type { VerticalAddMinusQuestion } from '../../../play/types'
import styles from './question-vertical-add-minus.module.scss'
import { renderRichText } from './render-tichtext'
import MathSymbol from '../../../play/components/math-symbol'
type QuestionVerticalAddMinusProps = {
question: VerticalAddMinusQuestion
}
const genNewNode = (src: string): Node[] => {
const tree = htmlParser.parse(src)
return tree.reduce<Node[]>((ret, item) => {
if (item.type === 'text') {
const sub: Node[] = item.content.split('').filter(Boolean).map(text => ({
content: text,
type: 'text'
}))
ret = [...ret, ...sub]
} else {
ret.push(item)
}
return ret
}, [])
}
const isDotTextNode = (node: Node) => node.type === 'text' && node.content === '.'
function computeFinalData (top: Node[], center: Node[], bottom: Node[]) {
const topDotIndex = top.findIndex(isDotTextNode)
const centerDotIndex = center.findIndex(isDotTextNode)
const bottomDotIndex = bottom.findIndex(isDotTextNode)
const hasTopDot = topDotIndex !== -1
const hasCenterDot = centerDotIndex !== -1
const hasBottomDot = bottomDotIndex !== -1
return {
left: [
{ key: 1, nodes: hasTopDot ? top.slice(0, topDotIndex) : top },
{ key: 2, nodes: hasCenterDot ? center.slice(0, centerDotIndex) : center, hasSign: true },
{ key: 3, nodes: [], isSplit: true },
{ key: 4, nodes: hasBottomDot ? bottom.slice(0, bottomDotIndex) : bottom },
],
center: [
{ key: 1, hasDot: hasTopDot },
{ key: 2, hasDot: hasCenterDot },
{ key: 3, hasDot: false, isSplit: true },
{ key: 4, hasDot: hasBottomDot },
],
right: [
{ key: 1, nodes: hasTopDot ? top.slice(topDotIndex + 1, top.length) : [] },
{ key: 2, nodes: hasCenterDot ? center.slice(centerDotIndex + 1, center.length) : [] },
{ key: 3, nodes: [], isSplit: true },
{ key: 4, nodes: hasBottomDot ? bottom.slice(bottomDotIndex + 1, bottom.length) : [] },
]
}
}
type FormatContentRet = {
sign: 'ADD' | 'SUB',
top: Node[];
center: Node[];
bottom: Node[];
}
const formatContent = (str: string): FormatContentRet => {
if (!str) {
return {
sign: 'ADD',
top: [],
center: [],
bottom: []
}
}
const list = genNewNode(str)
let signIndex = list.findIndex(c => c.type === 'text' && c.content === '+')
let signal: FormatContentRet['sign'] = 'ADD'
if (signIndex === -1) {
signIndex = list.findIndex(c => c.type === 'text' && c.content === '-')
signal = 'SUB'
}
if (signIndex === -1) {
return {
sign: signal,
top: [],
center: [],
bottom: []
}
}
const top = list.slice(0, signIndex)
const rightAndCenter = list.slice(signIndex + 1, list.length)
const equalIndex = rightAndCenter.findIndex(c => c.type === 'text' && c.content === '=')
if (equalIndex === -1) {
return {
sign: signal,
top: [],
center: [],
bottom: []
}
}
const center = rightAndCenter.slice(0, equalIndex)
const bottom = rightAndCenter.slice(equalIndex + 1, rightAndCenter.length)
return {
sign: signal,
top,
center,
bottom
}
}
const QuestionVerticalAddMinus = (props: QuestionVerticalAddMinusProps) => {
const { question } = props
const preGen = useMemo(() => {
return formatContent(question.contentText)
}, [question.contentText])
const contentTreeInfo = useMemo(() => {
return computeFinalData(preGen.top, preGen.center, preGen.bottom)
}, [preGen])
return (
<View className={classNames(styles.content)}>
<View className={styles.gap}></View>
<View className={styles.question_content}>
<View className={styles.column_wrap}>
{contentTreeInfo.left.map(item => {
if (item.isSplit) {
return (
<View key={item.key} className={styles.split}></View>
)
}
return (
<View key={item.key} className={styles.left}>
{item.hasSign && (
<View className={styles.sign_cell}>
<MathSymbol value={preGen.sign}></MathSymbol>
</View>
)}
{item.nodes.map((node, index) => (
<View key={index} className={styles.item_cell}>{renderRichText(node, question)}</View>
))}
</View>
)
})}
</View>
<View className={styles.column_wrap}>
{contentTreeInfo.center.map(item => {
if (item.isSplit) {
return (
<View key={item.key} className={styles.split}></View>
)
}
if (item.hasDot) {
return (
<MathSymbol value="." key={item.key} size="small"></MathSymbol>
)
}
return (
<View className={styles.place_holder} key={item.key}></View>
)
})}
</View>
<View className={styles.column_wrap}>
{contentTreeInfo.right.map(item => {
if (item.isSplit) {
return (
<View key={item.key} className={styles.split}></View>
)
}
return (
<View key={item.key} className={styles.right}>
{item.nodes.map((node, index) => (
<View key={index} className={styles.item_cell}>{renderRichText(node, question)}</View>
))}
</View>
)
})}
</View>
</View>
<View className={styles.gap_2}></View>
</View>
)
}
export default QuestionVerticalAddMinus
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment