Commit 35c9e17b authored by hewenhao's avatar hewenhao

feat: 竖式加减法

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