-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-renderers.tsx
More file actions
174 lines (157 loc) · 4.23 KB
/
Copy pathfunction-renderers.tsx
File metadata and controls
174 lines (157 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Structured MathML renderers for special value-tree functions.
*
* Most functions can render as binary operators or plain function calls. The
* helpers in this file cover the functions whose notation needs real MathML
* structure: fractions, powers, square roots, unary minus, and condition tables.
*/
import type { ReactElement, ReactNode } from 'react';
import { MATHML_FONT_SIZE } from './constants.ts';
interface SpecialValueFunctionRenderProps {
readonly functionId: string;
readonly argReactNodes: readonly ReactNode[];
}
interface ConditionalFunctionMathmlProps {
readonly argCnd: ReactNode;
readonly argTrue: ReactNode;
readonly argFalse: ReactNode;
}
const baseStyle = { fontSize: MATHML_FONT_SIZE, textAlign: 'left' as const };
const fontSizeStyle = { fontSize: MATHML_FONT_SIZE };
const ConditionalFunctionMathml = ({
argCnd,
argTrue,
argFalse,
}: ConditionalFunctionMathmlProps): ReactElement => {
return (
<mrow style={baseStyle}>
<mo style={fontSizeStyle}>{'{'}</mo>
<mtable columnalign={'left'} style={baseStyle}>
<mtr style={fontSizeStyle}>
<mtd style={{ ...baseStyle, paddingRight: '0.5em' }}>{argTrue}</mtd>
<mtd style={baseStyle}>
<mtext style={fontSizeStyle}>{'if '}</mtext>
<mspace width={'5px'} />
{argCnd}
</mtd>
</mtr>
<mtr style={fontSizeStyle}>
<mtd style={{ ...baseStyle, paddingRight: '0.5em' }}>{argFalse}</mtd>
<mtd style={baseStyle}>
<mtext style={fontSizeStyle}>otherwise.</mtext>
</mtd>
</mtr>
</mtable>
</mrow>
);
};
const FractionFunctionMathml = ({
numerator,
denominator,
}: {
readonly numerator: ReactNode;
readonly denominator: ReactNode;
}): ReactElement => {
return (
<mfrac>
<mstyle style={{ fontSize: MATHML_FONT_SIZE }}>{numerator}</mstyle>
<mstyle style={{ fontSize: MATHML_FONT_SIZE }}>{denominator}</mstyle>
</mfrac>
);
};
const PowerFunctionMathml = ({
base,
exponent,
}: {
readonly base: ReactNode;
readonly exponent: ReactNode;
}): ReactElement => {
return (
<msup>
<mstyle style={{ fontSize: MATHML_FONT_SIZE }}>{base}</mstyle>
<mstyle style={{ scale: '0.8' }}>{exponent}</mstyle>
</msup>
);
};
const SquareRootFunctionMathml = ({
value,
}: {
readonly value: ReactNode;
}): ReactElement => {
return (
<msqrt>
<mstyle style={{ padding: '3px', fontSize: MATHML_FONT_SIZE }}>
{value}
</mstyle>
</msqrt>
);
};
const AbsoluteValueFunctionMathml = ({
value,
}: {
readonly value: ReactNode;
}): ReactElement => {
return (
<mrow style={fontSizeStyle}>
<mo fence="true" stretchy="true">
|
</mo>
{value}
<mo fence="true" stretchy="true">
|
</mo>
</mrow>
);
};
const UnaryMinusFunctionMathml = ({
value,
}: {
readonly value: ReactNode;
}): ReactElement => {
return (
<mrow style={{ fontSize: MATHML_FONT_SIZE }}>
<mo style={{ fontSize: MATHML_FONT_SIZE }}>{'-'}</mo>
{value}
</mrow>
);
};
export const renderSpecialValueFunction = ({
functionId,
argReactNodes,
}: SpecialValueFunctionRenderProps): ReactElement | undefined => {
if (functionId === 'fg.abs' && argReactNodes.length === 1) {
return <AbsoluteValueFunctionMathml value={argReactNodes[0]} />;
}
if (functionId === 'fg.sqrt' && argReactNodes.length === 1) {
return <SquareRootFunctionMathml value={argReactNodes[0]} />;
}
if (functionId === 'fg.uminus' && argReactNodes.length === 1) {
return <UnaryMinusFunctionMathml value={argReactNodes[0]} />;
}
if (functionId === 'fg.cnd' && argReactNodes.length === 3) {
return (
<ConditionalFunctionMathml
argCnd={argReactNodes[0]}
argTrue={argReactNodes[1]}
argFalse={argReactNodes[2]}
/>
);
}
if (functionId === 'fg.divide' && argReactNodes.length === 2) {
return (
<FractionFunctionMathml
numerator={argReactNodes[0]}
denominator={argReactNodes[1]}
/>
);
}
if (functionId === 'fg.pow' && argReactNodes.length === 2) {
return (
<PowerFunctionMathml
base={argReactNodes[0]}
exponent={argReactNodes[1]}
/>
);
}
return undefined;
};