Commit 35c9e17b authored by hewenhao's avatar hewenhao

feat: 竖式加减法

parent a5e4db02
......@@ -23,9 +23,10 @@ const SymbolMap = {
type MathSymbolProps = {
value: '*' | '+' | '-' | '/' | '%' | '>' | '<' | '=' | '=2' | '.' | 'ADD' | 'SUB';
size?: 'small' | 'big';
className?: string;
}
const MathSymbol = ({ value, size = 'big' }: MathSymbolProps) => {
const MathSymbol = ({ value, size = 'big', className }: MathSymbolProps) => {
const url = useMemo(() => {
return SymbolMap[value]
}, [value])
......@@ -37,7 +38,7 @@ const MathSymbol = ({ value, size = 'big' }: MathSymbolProps) => {
return (
<Image
src={url}
className={classNames(styles.symbol, {
className={classNames(styles.symbol, className, {
[styles.is_remaind]: value === '%',
[styles.is_pair]: isPair,
[styles.is_small]: size === 'small',
......
......@@ -21,7 +21,7 @@
}
.item_cell {
min-width: 112px;
min-width: 128px;
height: 112px;
display: flex;
align-items: center;
......@@ -30,6 +30,7 @@
font-weight: 500;
color: #465164;
line-height: 98px;
margin: 0 -8px;
}
.sign_cell {
......@@ -37,6 +38,8 @@
align-items: center;
justify-content: center;
height: 112px;
min-width: 128px;
margin: 0 -8px;
}
.left {
......@@ -53,12 +56,13 @@
.split {
height: 8px;
background: rgba(23, 134, 247, 0.9);
margin: 16px 0;
margin: 6px 0;
}
.center {
.column_wrap {
display: flex;
flex-direction: column;
gap: 10px;
}
.place_holder {
......
......@@ -6,7 +6,6 @@ import htmlParser, { Node } from 'html-parse-stringify'
import type { VerticalAddMinusQuestion } from '../../types'
import styles from './question-vertical-add-minus.module.scss'
import AnswerBlank from '../answer-blank'
import { renderRichText } from './render-tichtext'
import MathSymbol from '../math-symbol'
......@@ -14,19 +13,6 @@ type QuestionVerticalAddMinusProps = {
question: VerticalAddMinusQuestion
}
type NumberLineProps = {
left: string;
}
const NumberLine = (props: NumberLineProps) => {
return (
<View>
<View></View>
<View></View>
</View>
)
}
const genNewNode = (src: string): Node[] => {
const tree = htmlParser.parse(src)
return tree.reduce<Node[]>((ret, item) => {
......@@ -68,29 +54,86 @@ function computeFinalData (top: Node[], center: Node[], bottom: Node[]) {
],
right: [
{ key: 1, nodes: hasTopDot ? top.slice(topDotIndex + 1, top.length) : [] },
{ key: 2, nodes: hasCenterDot ? center.slice(topDotIndex + 1, center.length) : [] },
{ key: 2, nodes: hasCenterDot ? center.slice(centerDotIndex + 1, center.length) : [] },
{ key: 3, nodes: [], isSplit: true },
{ key: 4, nodes: hasBottomDot ? bottom.slice(topDotIndex + 1, bottom.length) : [] },
{ 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 sign = question.questionItems[3].contentText
const preGen = useMemo(() => {
return formatContent(question.contentText)
}, [question.contentText])
const contentTreeInfo = useMemo(() => {
const top = genNewNode(question.questionItems[0].contentText)
const center = genNewNode(question.questionItems[1].contentText)
const bottom = genNewNode(question.questionItems[2].contentText)
return computeFinalData(top, center, bottom)
}, [question.questionItems])
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>
<View className={styles.column_wrap}>
{contentTreeInfo.left.map(item => {
if (item.isSplit) {
return (
......@@ -102,7 +145,7 @@ const QuestionVerticalAddMinus = (props: QuestionVerticalAddMinusProps) => {
<View key={item.key} className={styles.left}>
{item.hasSign && (
<View className={styles.sign_cell}>
<MathSymbol value={sign}></MathSymbol>
<MathSymbol value={preGen.sign}></MathSymbol>
</View>
)}
{item.nodes.map((node, index) => (
......@@ -112,7 +155,7 @@ const QuestionVerticalAddMinus = (props: QuestionVerticalAddMinusProps) => {
)
})}
</View>
<View className={styles.center}>
<View className={styles.column_wrap}>
{contentTreeInfo.center.map(item => {
if (item.isSplit) {
return (
......@@ -131,7 +174,7 @@ const QuestionVerticalAddMinus = (props: QuestionVerticalAddMinusProps) => {
)
})}
</View>
<View>
<View className={styles.column_wrap}>
{contentTreeInfo.right.map(item => {
if (item.isSplit) {
return (
......
......@@ -43,12 +43,7 @@ export interface VerticalDivisionQuestion extends QuestionItem {
export interface VerticalAddMinusQuestion extends QuestionItem {
questionType: '竖式加减法';
questionItems: [
RandomQuestionItem,
RandomQuestionItem,
RandomQuestionItem,
RandomQuestionSign
];
contentText: string;
}
export interface FractionAddMinusQuestion extends QuestionItem {
......
......@@ -145,7 +145,7 @@ const CreatePk = (props: CreatePkProps) => {
)}
>
<Image src={assetsMainNext['pk-wechat-logo.png'].u} className={styles.wechat_logo}></Image>
<View>发给同学好友进行Pk</View>
<View>发给同学好友进行PK</View>
</UserGuideView>
<View className={styles.tips}>会以你的最佳表现与对方答题数据进行PK</View>
......
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