@khanacademy/math-input 0.0.0-PR443-20230328215601

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/.eslintrc.js +12 -0
  2. package/CHANGELOG.md +104 -0
  3. package/README.md +39 -0
  4. package/dist/es/index.css +587 -0
  5. package/dist/es/index.js +7291 -0
  6. package/dist/es/index.js.map +1 -0
  7. package/dist/index.css +587 -0
  8. package/dist/index.js +7333 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/strings +71 -0
  11. package/index.html +20 -0
  12. package/less/echo.less +56 -0
  13. package/less/main.less +5 -0
  14. package/less/overrides.less +129 -0
  15. package/less/popover.less +22 -0
  16. package/less/tabbar.less +6 -0
  17. package/package.json +64 -0
  18. package/src/actions/index.js +57 -0
  19. package/src/components/__tests__/gesture-state-machine_test.js +437 -0
  20. package/src/components/__tests__/node-manager_test.js +89 -0
  21. package/src/components/__tests__/two-page-keypad_test.js +42 -0
  22. package/src/components/app.js +73 -0
  23. package/src/components/common-style.ts +46 -0
  24. package/src/components/compute-layout-parameters.js +157 -0
  25. package/src/components/corner-decal.js +56 -0
  26. package/src/components/echo-manager.js +160 -0
  27. package/src/components/empty-keypad-button.js +49 -0
  28. package/src/components/expression-keypad.js +323 -0
  29. package/src/components/fraction-keypad.js +176 -0
  30. package/src/components/gesture-manager.js +226 -0
  31. package/src/components/gesture-state-machine.js +283 -0
  32. package/src/components/icon.js +74 -0
  33. package/src/components/iconography/arrow.js +22 -0
  34. package/src/components/iconography/backspace.js +29 -0
  35. package/src/components/iconography/cdot.js +29 -0
  36. package/src/components/iconography/cos.js +30 -0
  37. package/src/components/iconography/cube-root.js +36 -0
  38. package/src/components/iconography/dismiss.js +25 -0
  39. package/src/components/iconography/divide.js +34 -0
  40. package/src/components/iconography/down.js +16 -0
  41. package/src/components/iconography/equal.js +33 -0
  42. package/src/components/iconography/exp-2.js +29 -0
  43. package/src/components/iconography/exp-3.js +29 -0
  44. package/src/components/iconography/exp.js +29 -0
  45. package/src/components/iconography/frac.js +44 -0
  46. package/src/components/iconography/geq.js +33 -0
  47. package/src/components/iconography/gt.js +33 -0
  48. package/src/components/iconography/index.js +45 -0
  49. package/src/components/iconography/jump-into-numerator.js +41 -0
  50. package/src/components/iconography/jump-out-base.js +30 -0
  51. package/src/components/iconography/jump-out-denominator.js +41 -0
  52. package/src/components/iconography/jump-out-exponent.js +30 -0
  53. package/src/components/iconography/jump-out-numerator.js +41 -0
  54. package/src/components/iconography/jump-out-parentheses.js +33 -0
  55. package/src/components/iconography/left-paren.js +33 -0
  56. package/src/components/iconography/left.js +16 -0
  57. package/src/components/iconography/leq.js +33 -0
  58. package/src/components/iconography/ln.js +29 -0
  59. package/src/components/iconography/log-n.js +29 -0
  60. package/src/components/iconography/log.js +29 -0
  61. package/src/components/iconography/lt.js +33 -0
  62. package/src/components/iconography/minus.js +32 -0
  63. package/src/components/iconography/neq.js +33 -0
  64. package/src/components/iconography/parens.js +33 -0
  65. package/src/components/iconography/percent.js +49 -0
  66. package/src/components/iconography/period.js +26 -0
  67. package/src/components/iconography/plus.js +32 -0
  68. package/src/components/iconography/radical.js +36 -0
  69. package/src/components/iconography/right-paren.js +33 -0
  70. package/src/components/iconography/right.js +16 -0
  71. package/src/components/iconography/sin.js +30 -0
  72. package/src/components/iconography/sqrt.js +32 -0
  73. package/src/components/iconography/tan.js +30 -0
  74. package/src/components/iconography/times.js +33 -0
  75. package/src/components/iconography/up.js +16 -0
  76. package/src/components/input/__tests__/context-tracking_test.js +177 -0
  77. package/src/components/input/__tests__/mathquill_test.js +747 -0
  78. package/src/components/input/__tests__/test-math-wrapper.jsx +33 -0
  79. package/src/components/input/cursor-contexts.ts +44 -0
  80. package/src/components/input/cursor-handle.tsx +137 -0
  81. package/src/components/input/drag-listener.js +75 -0
  82. package/src/components/input/math-input.tsx +993 -0
  83. package/src/components/input/math-wrapper.js +959 -0
  84. package/src/components/input/scroll-into-view.js +72 -0
  85. package/src/components/keypad/button-assets.tsx +493 -0
  86. package/src/components/keypad/button.stories.js +29 -0
  87. package/src/components/keypad/button.tsx +106 -0
  88. package/src/components/keypad/index.tsx +65 -0
  89. package/src/components/keypad/keypad-page-items.tsx +91 -0
  90. package/src/components/keypad/keypad-pages.stories.tsx +30 -0
  91. package/src/components/keypad/keypad.stories.tsx +33 -0
  92. package/src/components/keypad/numeric-input-page.tsx +99 -0
  93. package/src/components/keypad/pre-algebra-page.tsx +97 -0
  94. package/src/components/keypad/trigonometry-page.tsx +89 -0
  95. package/src/components/keypad-button.js +362 -0
  96. package/src/components/keypad-container.js +299 -0
  97. package/src/components/keypad.js +154 -0
  98. package/src/components/many-keypad-button.js +44 -0
  99. package/src/components/math-icon.js +65 -0
  100. package/src/components/multi-symbol-grid.js +182 -0
  101. package/src/components/multi-symbol-popover.js +59 -0
  102. package/src/components/navigation-pad.js +139 -0
  103. package/src/components/node-manager.js +129 -0
  104. package/src/components/popover-manager.js +76 -0
  105. package/src/components/popover-state-machine.js +173 -0
  106. package/src/components/prop-types.js +82 -0
  107. package/src/components/provided-keypad.tsx +105 -0
  108. package/src/components/styles.js +38 -0
  109. package/src/components/svg-icon.js +25 -0
  110. package/src/components/tabbar/__tests__/tabbar_test.js +65 -0
  111. package/src/components/tabbar/icons.tsx +64 -0
  112. package/src/components/tabbar/item.tsx +136 -0
  113. package/src/components/tabbar/tabbar.stories.js +60 -0
  114. package/src/components/tabbar/tabbar.tsx +59 -0
  115. package/src/components/tabbar/types.ts +1 -0
  116. package/src/components/text-icon.js +52 -0
  117. package/src/components/touchable-keypad-button.js +146 -0
  118. package/src/components/two-page-keypad.js +99 -0
  119. package/src/components/velocity-tracker.js +76 -0
  120. package/src/components/z-indexes.ts +8 -0
  121. package/src/consts.ts +91 -0
  122. package/src/data/key-configs.ts +349 -0
  123. package/src/data/keys.ts +124 -0
  124. package/src/demo.js +9 -0
  125. package/src/fake-react-native-web/index.ts +11 -0
  126. package/src/fake-react-native-web/text.tsx +54 -0
  127. package/src/fake-react-native-web/view.tsx +89 -0
  128. package/src/index.ts +22 -0
  129. package/src/native-app.js +85 -0
  130. package/src/store/index.ts +534 -0
  131. package/src/utils.ts +14 -0
  132. package/tools/svg-to-react/convert.py +111 -0
  133. package/tools/svg-to-react/icons/math-keypad-icon-0.svg +32 -0
  134. package/tools/svg-to-react/icons/math-keypad-icon-1.svg +32 -0
  135. package/tools/svg-to-react/icons/math-keypad-icon-2.svg +32 -0
  136. package/tools/svg-to-react/icons/math-keypad-icon-3.svg +32 -0
  137. package/tools/svg-to-react/icons/math-keypad-icon-4.svg +32 -0
  138. package/tools/svg-to-react/icons/math-keypad-icon-5.svg +32 -0
  139. package/tools/svg-to-react/icons/math-keypad-icon-6.svg +32 -0
  140. package/tools/svg-to-react/icons/math-keypad-icon-7.svg +32 -0
  141. package/tools/svg-to-react/icons/math-keypad-icon-8.svg +32 -0
  142. package/tools/svg-to-react/icons/math-keypad-icon-9.svg +32 -0
  143. package/tools/svg-to-react/icons/math-keypad-icon-addition.svg +34 -0
  144. package/tools/svg-to-react/icons/math-keypad-icon-cos.svg +38 -0
  145. package/tools/svg-to-react/icons/math-keypad-icon-delete.svg +36 -0
  146. package/tools/svg-to-react/icons/math-keypad-icon-dismiss.svg +36 -0
  147. package/tools/svg-to-react/icons/math-keypad-icon-division.svg +36 -0
  148. package/tools/svg-to-react/icons/math-keypad-icon-equals-not.svg +50 -0
  149. package/tools/svg-to-react/icons/math-keypad-icon-equals.svg +48 -0
  150. package/tools/svg-to-react/icons/math-keypad-icon-exponent-2.svg +38 -0
  151. package/tools/svg-to-react/icons/math-keypad-icon-exponent-3.svg +38 -0
  152. package/tools/svg-to-react/icons/math-keypad-icon-exponent.svg +38 -0
  153. package/tools/svg-to-react/icons/math-keypad-icon-fraction.svg +42 -0
  154. package/tools/svg-to-react/icons/math-keypad-icon-greater-than.svg +46 -0
  155. package/tools/svg-to-react/icons/math-keypad-icon-jump-out-base.svg +44 -0
  156. package/tools/svg-to-react/icons/math-keypad-icon-jump-out-denominator.svg +48 -0
  157. package/tools/svg-to-react/icons/math-keypad-icon-jump-out-exponent.svg +44 -0
  158. package/tools/svg-to-react/icons/math-keypad-icon-jump-out-parentheses.svg +44 -0
  159. package/tools/svg-to-react/icons/math-keypad-icon-less-than.svg +46 -0
  160. package/tools/svg-to-react/icons/math-keypad-icon-log-10.svg +36 -0
  161. package/tools/svg-to-react/icons/math-keypad-icon-log-e.svg +36 -0
  162. package/tools/svg-to-react/icons/math-keypad-icon-log.svg +38 -0
  163. package/tools/svg-to-react/icons/math-keypad-icon-multiplication-cross.svg +40 -0
  164. package/tools/svg-to-react/icons/math-keypad-icon-multiplication-dot.svg +38 -0
  165. package/tools/svg-to-react/icons/math-keypad-icon-percent.svg +42 -0
  166. package/tools/svg-to-react/icons/math-keypad-icon-radical-2.svg +36 -0
  167. package/tools/svg-to-react/icons/math-keypad-icon-radical-3.svg +38 -0
  168. package/tools/svg-to-react/icons/math-keypad-icon-radical.svg +38 -0
  169. package/tools/svg-to-react/icons/math-keypad-icon-radix-character.svg +32 -0
  170. package/tools/svg-to-react/icons/math-keypad-icon-sin.svg +38 -0
  171. package/tools/svg-to-react/icons/math-keypad-icon-subtraction.svg +32 -0
  172. package/tools/svg-to-react/icons/math-keypad-icon-tan.svg +38 -0
  173. package/tools/svg-to-react/symbol_map.py +41 -0
  174. package/tsconfig.json +9 -0
@@ -0,0 +1,993 @@
1
+ import Color from "@khanacademy/wonder-blocks-color";
2
+ import * as i18n from "@khanacademy/wonder-blocks-i18n";
3
+ import {entries} from "@khanacademy/wonder-stuff-core";
4
+ import {StyleSheet} from "aphrodite";
5
+ import * as React from "react";
6
+ import ReactDOM from "react-dom";
7
+
8
+ import Keys from "../../data/keys";
9
+ import {View} from "../../fake-react-native-web/index";
10
+ import {
11
+ cursorHandleRadiusPx,
12
+ cursorHandleDistanceMultiplier,
13
+ wonderBlocksBlue,
14
+ offBlack,
15
+ } from "../common-style";
16
+
17
+ import CursorHandle from "./cursor-handle";
18
+ import DragListener from "./drag-listener";
19
+ import MathWrapper from "./math-wrapper";
20
+ import {scrollIntoView} from "./scroll-into-view";
21
+
22
+ const constrainingFrictionFactor = 0.8;
23
+
24
+ type Props = {
25
+ keypadElement: any;
26
+ onBlur: () => void;
27
+ onChange: any;
28
+ onFocus: () => void;
29
+ style: any;
30
+ value: string;
31
+ };
32
+
33
+ type DefaultProps = {
34
+ style: Props["style"];
35
+ value: Props["value"];
36
+ };
37
+
38
+ type HandleState = {
39
+ animateIntoPosition?: boolean | null | undefined;
40
+ visible: boolean;
41
+ x?: number;
42
+ y?: number;
43
+ };
44
+
45
+ type State = {
46
+ focused: boolean;
47
+ handle: HandleState;
48
+ };
49
+
50
+ // eslint-disable-next-line react/no-unsafe
51
+ class MathInput extends React.Component<Props, State> {
52
+ didTouchOutside: boolean | null | undefined;
53
+ didScroll: boolean | null | undefined;
54
+ mathField: any;
55
+ // @ts-expect-error [FEI-5003] - TS2564 - Property 'recordTouchStartOutside' has no initializer and is not definitely assigned in the constructor.
56
+ recordTouchStartOutside: (arg1: any) => void;
57
+ // @ts-expect-error [FEI-5003] - TS2564 - Property 'blurOnTouchEndOutside' has no initializer and is not definitely assigned in the constructor.
58
+ blurOnTouchEndOutside: (arg1: any) => void;
59
+ dragListener: any;
60
+ inputRef: HTMLDivElement | null | undefined;
61
+ _isMounted: boolean | null | undefined;
62
+ _mathContainer: any;
63
+ // @ts-expect-error [FEI-5003] - TS2564 - Property '_container' has no initializer and is not definitely assigned in the constructor.
64
+ _container: HTMLDivElement;
65
+ _root: any;
66
+ // @ts-expect-error [FEI-5003] - TS2564 - Property '_containerBounds' has no initializer and is not definitely assigned in the constructor.
67
+ _containerBounds: ClientRect;
68
+ _keypadBounds: ClientRect | null | undefined;
69
+
70
+ static defaultProps: DefaultProps = {
71
+ style: {},
72
+ value: "",
73
+ };
74
+
75
+ state: State = {
76
+ focused: false,
77
+ handle: {
78
+ animateIntoPosition: false,
79
+ visible: false,
80
+ x: 0,
81
+ y: 0,
82
+ },
83
+ };
84
+
85
+ componentDidMount() {
86
+ this._isMounted = true;
87
+
88
+ this.mathField = new MathWrapper(
89
+ this._mathContainer,
90
+ {},
91
+ {
92
+ onCursorMove: (cursor) => {
93
+ // TODO(charlie): It's not great that there is so much coupling
94
+ // between this keypad and the input behavior. We should wrap
95
+ // this `MathInput` component in an intermediary component
96
+ // that translates accesses on the keypad into vanilla props,
97
+ // to make this input keypad-agnostic.
98
+ this.props.keypadElement &&
99
+ this.props.keypadElement.setCursor(cursor);
100
+ },
101
+ },
102
+ );
103
+
104
+ // NOTE(charlie): MathQuill binds this handler to manage its
105
+ // drag-to-select behavior. For reasons that I can't explain, the event
106
+ // itself gets triggered even if you tap slightly outside of the
107
+ // bound container (maybe 5px outside of any boundary). As a result, the
108
+ // cursor appears when tapping at those locations, even though the input
109
+ // itself doesn't receive any touch start or mouse down event and, as
110
+ // such, doesn't focus itself. This makes for a confusing UX, as the
111
+ // cursor appears, but the keypad does not and the input otherwise
112
+ // treats itself as unfocused. Thankfully, we don't need this behavior--
113
+ // we manage all of the cursor interactions ourselves--so we can safely
114
+ // unbind the handler.
115
+ this.mathField.mathField.__controller.container.unbind(
116
+ "mousedown.mathquill",
117
+ );
118
+
119
+ this.mathField.setContent(this.props.value);
120
+
121
+ this._updateInputPadding();
122
+
123
+ this._container = ReactDOM.findDOMNode(this) as HTMLDivElement;
124
+ this._root = this._container.querySelector(".mq-root-block");
125
+ this._root.addEventListener("scroll", this._handleScroll);
126
+
127
+ // Record the initial scroll displacement on touch start. This allows
128
+ // us to detect whether a touch event was a scroll and only blur the
129
+ // input on non-scrolls--blurring the input on scroll makes for a
130
+ // frustrating user experience.
131
+ this.recordTouchStartOutside = (evt: any) => {
132
+ if (this.state.focused) {
133
+ // Only blur if the touch is both outside of the input, and
134
+ // above or to the left or right of the keypad (if it has been
135
+ // provided). The reasoning for not blurring when touches occur
136
+ // below the keypad is that the keypad may be anchored above
137
+ // the 'Check answer' bottom bar, in which case, we don't want
138
+ // to dismiss the keypad on check.
139
+ // TODO(charlie): Inject this logic.
140
+ if (!this._container.contains(evt.target)) {
141
+ let touchDidStartInOrBelowKeypad = false;
142
+ if (
143
+ this.props.keypadElement &&
144
+ this.props.keypadElement.getDOMNode()
145
+ ) {
146
+ const bounds = this._getKeypadBounds();
147
+ for (let i = 0; i < evt.changedTouches.length; i++) {
148
+ const [x, y] = [
149
+ evt.changedTouches[i].clientX,
150
+ evt.changedTouches[i].clientY,
151
+ ];
152
+ if (
153
+ (bounds.left <= x &&
154
+ bounds.right >= x &&
155
+ bounds.top <= y &&
156
+ bounds.bottom >= y) ||
157
+ bounds.bottom < y
158
+ ) {
159
+ touchDidStartInOrBelowKeypad = true;
160
+ break;
161
+ }
162
+ }
163
+ }
164
+
165
+ if (!touchDidStartInOrBelowKeypad) {
166
+ this.didTouchOutside = true;
167
+
168
+ if (this.dragListener) {
169
+ this.dragListener.detach();
170
+ }
171
+
172
+ this.dragListener = new DragListener(() => {
173
+ this.didScroll = true;
174
+ this.dragListener.detach();
175
+ }, evt);
176
+ this.dragListener.attach();
177
+ }
178
+ }
179
+ }
180
+ };
181
+
182
+ this.blurOnTouchEndOutside = (evt) => {
183
+ // If the user didn't scroll, blur the input.
184
+ // TODO(charlie): Verify that the touch that ended actually started
185
+ // outside the keypad. Right now, you can touch down on the keypad,
186
+ // touch elsewhere, release the finger on the keypad, and trigger a
187
+ // dismissal. This code needs to be generalized to handle
188
+ // multi-touch.
189
+ if (this.state.focused && this.didTouchOutside && !this.didScroll) {
190
+ this.blur();
191
+ }
192
+
193
+ this.didTouchOutside = false;
194
+ this.didScroll = false;
195
+
196
+ if (this.dragListener) {
197
+ this.dragListener.detach();
198
+ }
199
+ };
200
+
201
+ window.addEventListener("touchstart", this.recordTouchStartOutside);
202
+ window.addEventListener("touchend", this.blurOnTouchEndOutside);
203
+ window.addEventListener("touchcancel", this.blurOnTouchEndOutside);
204
+
205
+ // HACK(benkomalo): if the window resizes, the keypad bounds can
206
+ // change. That's a bit peeking into the internals of the keypad
207
+ // itself, since we know bounds can change only when the viewport
208
+ // changes, but seems like a rare enough thing to get wrong that it's
209
+ // not worth wiring up extra things for the technical "purity" of
210
+ // having the keypad notify of changes to us.
211
+ window.addEventListener("resize", this._clearKeypadBoundsCache);
212
+ window.addEventListener(
213
+ "orientationchange",
214
+ this._clearKeypadBoundsCache,
215
+ );
216
+ }
217
+
218
+ UNSAFE_componentWillReceiveProps(props: Props) {
219
+ if (this.props.keypadElement !== props.keypadElement) {
220
+ this._clearKeypadBoundsCache();
221
+ }
222
+ }
223
+
224
+ componentDidUpdate(prevProps: Props, prevState: State) {
225
+ if (this.mathField.getContent() !== this.props.value) {
226
+ this.mathField.setContent(this.props.value);
227
+ }
228
+
229
+ if (prevState.focused !== this.state.focused) {
230
+ this._updateInputPadding();
231
+ }
232
+ }
233
+
234
+ componentWillUnmount() {
235
+ this._isMounted = false;
236
+
237
+ window.removeEventListener("touchstart", this.recordTouchStartOutside);
238
+ window.removeEventListener("touchend", this.blurOnTouchEndOutside);
239
+ window.removeEventListener("touchcancel", this.blurOnTouchEndOutside);
240
+ // @ts-expect-error [FEI-5003] - TS2769 - No overload matches this call.
241
+ window.removeEventListener("resize", this._clearKeypadBoundsCache());
242
+ window.removeEventListener(
243
+ "orientationchange",
244
+ // @ts-expect-error [FEI-5003] - TS2769 - No overload matches this call.
245
+ this._clearKeypadBoundsCache(),
246
+ );
247
+ }
248
+
249
+ _clearKeypadBoundsCache: () => void = () => {
250
+ this._keypadBounds = null;
251
+ };
252
+
253
+ _cacheKeypadBounds: (arg1: any) => void = (keypadNode) => {
254
+ this._keypadBounds = keypadNode.getBoundingClientRect();
255
+ };
256
+
257
+ _updateInputPadding: () => void = () => {
258
+ this._container = ReactDOM.findDOMNode(this) as HTMLDivElement;
259
+ this._root = this._container.querySelector(".mq-root-block");
260
+
261
+ const padding = this.getInputInnerPadding();
262
+ // NOTE(diedra): This overrides the default 2px padding from Mathquil.
263
+ this._root.style.padding =
264
+ `${padding.paddingTop}px ${padding.paddingRight}px` +
265
+ ` ${padding.paddingBottom}px ${padding.paddingLeft}px`;
266
+ this._root.style.fontSize = `${fontSizePt}pt`;
267
+ };
268
+
269
+ /** Gets and cache they bounds of the keypadElement */
270
+ _getKeypadBounds: () => any = () => {
271
+ if (!this._keypadBounds) {
272
+ const node = this.props.keypadElement.getDOMNode();
273
+ this._cacheKeypadBounds(node);
274
+ }
275
+ return this._keypadBounds;
276
+ };
277
+
278
+ _updateCursorHandle: (arg1?: boolean | null | undefined) => void = (
279
+ animateIntoPosition,
280
+ ) => {
281
+ const containerBounds = this._container.getBoundingClientRect();
282
+ const cursor: any = this._container.querySelector(".mq-cursor");
283
+ const cursorBounds = cursor.getBoundingClientRect();
284
+
285
+ const cursorWidth = cursorBounds.width;
286
+ const gapBelowCursor = 2;
287
+
288
+ const inputInnerPadding = this.getInputInnerPadding();
289
+
290
+ // The cursor should never be further right or left than the edge of the
291
+ // container's values.
292
+ const furthestRightCursorBound =
293
+ containerBounds.right -
294
+ cursorWidth -
295
+ inputInnerPadding.paddingRight;
296
+ const furthestLeftCursorBound =
297
+ containerBounds.left + cursorWidth + inputInnerPadding.paddingLeft;
298
+
299
+ let cursorBoundsLeft = cursorBounds.left;
300
+ if (cursorBounds.left > furthestRightCursorBound) {
301
+ cursorBoundsLeft = furthestRightCursorBound;
302
+ } else if (cursorBounds.left < furthestLeftCursorBound) {
303
+ cursorBoundsLeft = furthestLeftCursorBound;
304
+ }
305
+
306
+ this.setState({
307
+ handle: {
308
+ visible: true,
309
+ animateIntoPosition,
310
+ // We subtract containerBounds' left/top to correct for the
311
+ // position of the container within the page.
312
+ x: cursorBoundsLeft + cursorWidth / 2 - containerBounds.left,
313
+ y: cursorBounds.bottom + gapBelowCursor - containerBounds.top,
314
+ },
315
+ });
316
+ };
317
+
318
+ _hideCursorHandle: () => void = () => {
319
+ this.setState({
320
+ handle: {
321
+ visible: false,
322
+ x: 0,
323
+ y: 0,
324
+ },
325
+ });
326
+ };
327
+
328
+ _handleScroll: () => void = () => {
329
+ // If animateIntoPosition is false, the user is currently manually positioning
330
+ // the cursor. This is important because the user can scroll the input field
331
+ // with the curor handle, and we don't want to override that ability.
332
+ // But we do want to hide the handle is the user is just scrolling the input field
333
+ // normally, because the handle will not move with the scroll.
334
+ if (this.state.handle.animateIntoPosition !== false) {
335
+ this._hideCursorHandle();
336
+ }
337
+ };
338
+
339
+ blur: () => void = () => {
340
+ this.mathField.blur();
341
+ this.props.onBlur && this.props.onBlur();
342
+ this.setState({focused: false, handle: {visible: false}});
343
+ };
344
+
345
+ focus: () => void = () => {
346
+ // Pass this component's handleKey method to the keypad so it can call
347
+ // it whenever it needs to trigger a keypress action.
348
+ this.props.keypadElement.setKeyHandler((key) => {
349
+ const cursor = this.mathField.pressKey(key);
350
+
351
+ // Trigger an `onChange` if the value in the input changed, and hide
352
+ // the cursor handle whenever the user types a key. If the value
353
+ // changed as a result of a keypress, we need to be careful not to
354
+ // call `setState` until after `onChange` has resolved.
355
+ const hideCursor = () => {
356
+ this.setState({
357
+ handle: {
358
+ visible: false,
359
+ },
360
+ });
361
+ };
362
+ const value = this.mathField.getContent();
363
+ if (this.props.value !== value) {
364
+ this.props.onChange(value, hideCursor);
365
+ } else {
366
+ hideCursor();
367
+ }
368
+
369
+ return cursor;
370
+ });
371
+
372
+ this.mathField.focus();
373
+ this.props.onFocus && this.props.onFocus();
374
+ this.setState({focused: true}, () => {
375
+ // NOTE(charlie): We use `setTimeout` to allow for a layout pass to
376
+ // occur. Otherwise, the keypad is measured incorrectly. Ideally,
377
+ // we'd use requestAnimationFrame here, but it's unsupported on
378
+ // Android Browser 4.3.
379
+ setTimeout(() => {
380
+ if (this._isMounted) {
381
+ // TODO(benkomalo): the keypad is animating at this point,
382
+ // so we can't call _cacheKeypadBounds(), even though
383
+ // it'd be nice to do so. It should probably be the case
384
+ // that the higher level controller tells us when the
385
+ // keypad is settled (then scrollIntoView wouldn't have
386
+ // to make assumptions about that either).
387
+ const maybeKeypadNode =
388
+ this.props.keypadElement &&
389
+ this.props.keypadElement.getDOMNode();
390
+ scrollIntoView(this._container, maybeKeypadNode);
391
+ }
392
+ });
393
+ });
394
+ };
395
+
396
+ /**
397
+ * Tries to determine which DOM node to place the cursor next to based on
398
+ * where the user drags the cursor handle. If it finds a node it will
399
+ * place the cursor next to it, update the handle to be under the cursor,
400
+ * and return true. If it doesn't find a node, it returns false.
401
+ *
402
+ * It searches for nodes by doing it tests at the following points:
403
+ *
404
+ * (x - dx, y), (x, y), (x + dx, y)
405
+ *
406
+ * If it doesn't find any nodes from the rendered math it will update y
407
+ * by adding dy.
408
+ *
409
+ * The algorithm ends its search when y goes outside the bounds of
410
+ * containerBounds.
411
+ *
412
+ * @param {DOMRect} containerBounds - bounds of the container node
413
+ * @param {number} x - the initial x coordinate in the viewport
414
+ * @param {number} y - the initial y coordinate in the viewport
415
+ * @param {number} dx - horizontal spacing between elementFromPoint calls
416
+ * @param {number} dy - vertical spacing between elementFromPoint calls,
417
+ * sign determines direction.
418
+ * @returns {boolean} - true if a node was hit, false otherwise.
419
+ */
420
+ _findHitNode: (
421
+ arg1: ClientRect,
422
+ arg2: number,
423
+ arg3: number,
424
+ arg4: number,
425
+ arg5: number,
426
+ ) => boolean = (containerBounds, x, y, dx, dy) => {
427
+ while (y >= containerBounds.top && y <= containerBounds.bottom) {
428
+ y += dy;
429
+
430
+ const points = [
431
+ [x - dx, y],
432
+ [x, y],
433
+ [x + dx, y],
434
+ ];
435
+
436
+ const elements = points
437
+ // @ts-expect-error [FEI-5003] - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter.
438
+ .map((point) => document.elementFromPoint(...point))
439
+ // We exclude the root container itself and any nodes marked
440
+ // as non-leaf which are fractions, parens, and roots. The
441
+ // children of those nodes are included in the list because
442
+ // those are the items we care about placing the cursor next
443
+ // to.
444
+ //
445
+ // MathQuill's mq-non-leaf is not applied to all non-leaf nodes
446
+ // so the naming is a bit confusing. Although fractions are
447
+ // included, neither mq-numerator nor mq-denominator nodes are
448
+ // and neither are subscripts or superscripts.
449
+ .filter(
450
+ (element) =>
451
+ element &&
452
+ this._root.contains(element) &&
453
+ ((!element.classList.contains("mq-root-block") &&
454
+ !element.classList.contains("mq-non-leaf")) ||
455
+ element.classList.contains("mq-empty") ||
456
+ element.classList.contains("mq-hasCursor")),
457
+ );
458
+
459
+ let hitNode = null;
460
+
461
+ // Contains only DOMNodes without child elements. These should
462
+ // contain some amount of text though.
463
+ const leafElements: Array<null | HTMLElement> = [];
464
+
465
+ // Contains only DOMNodes with child elements.
466
+ const nonLeafElements: Array<null | HTMLElement> = [];
467
+
468
+ let max = 0;
469
+ const counts: {
470
+ [key: string]: number;
471
+ } = {};
472
+ const elementsById: Record<string, any> = {};
473
+
474
+ for (const element of elements) {
475
+ // @ts-expect-error [FEI-5003] - TS2531 - Object is possibly 'null'.
476
+ const id = element.getAttribute("mathquill-command-id");
477
+ if (id != null) {
478
+ // @ts-expect-error [FEI-5003] - TS2345 - Argument of type 'Element | null' is not assignable to parameter of type 'HTMLElement | null'.
479
+ leafElements.push(element);
480
+
481
+ counts[id] = (counts[id] || 0) + 1;
482
+ elementsById[id] = element;
483
+ } else {
484
+ // @ts-expect-error [FEI-5003] - TS2345 - Argument of type 'Element | null' is not assignable to parameter of type 'HTMLElement | null'.
485
+ nonLeafElements.push(element);
486
+ }
487
+ }
488
+
489
+ // When determining which DOMNode to place the cursor beside, we
490
+ // prefer leaf nodes. Hitting a leaf node is a good sign that the
491
+ // cursor is really close to some piece of math that has been
492
+ // rendered because leaf nodes contain text. Non-leaf nodes may
493
+ // contain a lot of whitespace so the cursor may be further away
494
+ // from actual text within the expression.
495
+ //
496
+ // Since we're doing three hit tests per loop it's possible that
497
+ // we hit multiple leaf nodes at the same time. In this case we
498
+ // we prefer the DOMNode with the most hits.
499
+ // TODO(kevinb) consider preferring nodes hit by [x, y].
500
+ for (const [id, count] of entries(counts)) {
501
+ if (count > max) {
502
+ max = count;
503
+ hitNode = elementsById[id];
504
+ }
505
+ }
506
+
507
+ // It's possible that two non-leaf nodes are right beside each
508
+ // other. We don't bother counting the number of hits for each,
509
+ // b/c this seems like an unlikely situation. Also, ignoring the
510
+ // hit count in the situation should not have serious effects on
511
+ // the overall accuracy of the algorithm.
512
+ if (hitNode == null && nonLeafElements.length > 0) {
513
+ // @ts-expect-error [FEI-5003] - TS2322 - Type 'HTMLElement | null' is not assignable to type 'null'.
514
+ hitNode = nonLeafElements[0];
515
+ }
516
+
517
+ if (hitNode !== null) {
518
+ this.mathField.setCursorPosition(x, y, hitNode);
519
+ return true;
520
+ }
521
+ }
522
+
523
+ return false;
524
+ };
525
+
526
+ /**
527
+ * Inserts the cursor at the DOM node closest to the given coordinates,
528
+ * based on hit-tests conducted using #_findHitNode.
529
+ *
530
+ * @param {number} x - the x coordinate in the viewport
531
+ * @param {number} y - the y coordinate in the viewport
532
+ */
533
+ _insertCursorAtClosestNode: (arg1: number, arg2: number) => void = (
534
+ x,
535
+ y,
536
+ ) => {
537
+ const cursor = this.mathField.getCursor();
538
+
539
+ // Pre-emptively check if the input has any child nodes; if not, the
540
+ // input is empty, so we throw the cursor at the start.
541
+ if (!this._root.hasChildNodes()) {
542
+ cursor.insAtLeftEnd(this.mathField.mathField.__controller.root);
543
+ return;
544
+ }
545
+
546
+ // NOTE(diedra): The adding and subtracting of 10 or 15 pixels here accounts
547
+ // for the padding that surrounds the input values.
548
+ if (y > this._containerBounds.bottom) {
549
+ y = this._containerBounds.bottom - 10;
550
+ } else if (y < this._containerBounds.top) {
551
+ y = this._containerBounds.top + 10;
552
+ }
553
+ if (x > this._containerBounds.right) {
554
+ x = this._containerBounds.right - 15;
555
+ } else if (x < this._containerBounds.left) {
556
+ x = this._containerBounds.left + 15;
557
+ }
558
+
559
+ let dy;
560
+
561
+ // Vertical spacing between hit tests
562
+ // dy is negative because we're moving upwards.
563
+ dy = -8;
564
+
565
+ // Horizontal spacing between hit tests
566
+ // Note: This value depends on the font size. If the gap is too small
567
+ // we end up placing the cursor at the end of the expression when we
568
+ // shouldn't.
569
+ const dx = 5;
570
+
571
+ if (this._findHitNode(this._containerBounds, x, y, dx, dy)) {
572
+ return;
573
+ }
574
+
575
+ // If we haven't found anything start from the top.
576
+ y = this._containerBounds.top;
577
+
578
+ // dy is positive b/c we're going downwards.
579
+ dy = 8;
580
+
581
+ if (this._findHitNode(this._containerBounds, x, y, dx, dy)) {
582
+ return;
583
+ }
584
+
585
+ const firstChildBounds = this._root.firstChild.getBoundingClientRect();
586
+ const lastChildBounds = this._root.lastChild.getBoundingClientRect();
587
+
588
+ const left = firstChildBounds.left;
589
+ const right = lastChildBounds.right;
590
+
591
+ // We've exhausted all of the options. We're likely either to the right
592
+ // or left of all of the math, so we place the cursor at the end to
593
+ // which it's closest.
594
+ if (Math.abs(x - right) < Math.abs(x - left)) {
595
+ cursor.insAtRightEnd(this.mathField.mathField.__controller.root);
596
+ } else {
597
+ cursor.insAtLeftEnd(this.mathField.mathField.__controller.root);
598
+ }
599
+ // In that event, we need to update the cursor context ourselves.
600
+ this.props.keypadElement &&
601
+ this.props.keypadElement.setCursor({
602
+ context: this.mathField.contextForCursor(cursor),
603
+ });
604
+ };
605
+
606
+ handleTouchStart: (arg1: React.TouchEvent<HTMLDivElement>) => void = (
607
+ e,
608
+ ) => {
609
+ e.stopPropagation();
610
+
611
+ // Hide the cursor handle on touch start, if the handle itself isn't
612
+ // handling the touch event.
613
+ this._hideCursorHandle();
614
+
615
+ // Cache the container bounds, so as to avoid re-computing. If we don't
616
+ // have any content, then it's not necessary, since the cursor can't be
617
+ // moved anyway.
618
+ if (this.mathField.getContent() !== "") {
619
+ this._containerBounds = this._container.getBoundingClientRect();
620
+
621
+ // Make the cursor visible and set the handle-less cursor's
622
+ // location.
623
+ const touch = e.changedTouches[0];
624
+ this._insertCursorAtClosestNode(touch.clientX, touch.clientY);
625
+ }
626
+
627
+ // Trigger a focus event, if we're not already focused.
628
+ if (!this.state.focused) {
629
+ this.focus();
630
+ }
631
+ };
632
+
633
+ handleTouchMove: (arg1: React.TouchEvent<HTMLDivElement>) => void = (e) => {
634
+ e.stopPropagation();
635
+
636
+ // Update the handle-less cursor's location on move, if there's any
637
+ // content in the box. Note that if the user touched outside the keypad
638
+ // (e.g., with a different finger) during this touch interaction, we
639
+ // may have blurred, in which case we should ignore the touch (since
640
+ // the cursor is no longer visible and the input is no longer
641
+ // highlighted).
642
+ if (this.mathField.getContent() !== "" && this.state.focused) {
643
+ const touch = e.changedTouches[0];
644
+ this._insertCursorAtClosestNode(touch.clientX, touch.clientY);
645
+ }
646
+ };
647
+
648
+ handleTouchEnd: (arg1: React.TouchEvent<HTMLDivElement>) => void = (e) => {
649
+ e.stopPropagation();
650
+
651
+ // And on touch-end, reveal the cursor, unless the input is empty. Note
652
+ // that if the user touched outside the keypad (e.g., with a different
653
+ // finger) during this touch interaction, we may have blurred, in which
654
+ // case we should ignore the touch (since the cursor is no longer
655
+ // visible and the input is no longer highlighted).
656
+ if (this.mathField.getContent() !== "" && this.state.focused) {
657
+ this._updateCursorHandle();
658
+ }
659
+ };
660
+
661
+ /**
662
+ * When a touch starts in the cursor handle, we track it so as to avoid
663
+ * handling any touch events ourself.
664
+ *
665
+ * @param {TouchEvent} e - the raw touch event from the browser
666
+ */
667
+ onCursorHandleTouchStart: (
668
+ arg1: React.TouchEvent<HTMLSpanElement>,
669
+ ) => void = (e) => {
670
+ // NOTE(charlie): The cursor handle is a child of this view, so whenever
671
+ // it receives a touch event, that event would also typically be bubbled
672
+ // up to our own handlers. However, we want the cursor to handle its own
673
+ // touch events, and for this view to only handle touch events that
674
+ // don't affect the cursor. As such, we `stopPropagation` on any touch
675
+ // events that are being handled by the cursor, so as to avoid handling
676
+ // them in our own touch handlers.
677
+ e.stopPropagation();
678
+
679
+ e.preventDefault();
680
+
681
+ // Cache the container bounds, so as to avoid re-computing.
682
+ this._containerBounds = this._container.getBoundingClientRect();
683
+ };
684
+
685
+ _constrainToBound: (
686
+ arg1: number,
687
+ arg2: number,
688
+ arg3: number,
689
+ arg4: number,
690
+ ) => number = (value, min, max, friction) => {
691
+ if (value < min) {
692
+ return min + (value - min) * friction;
693
+ } else if (value > max) {
694
+ return max + (value - max) * friction;
695
+ } else {
696
+ return value;
697
+ }
698
+ };
699
+
700
+ /**
701
+ * When the user moves the cursor handle update the position of the cursor
702
+ * and the handle.
703
+ *
704
+ * @param {TouchEvent} e - the raw touch event from the browser
705
+ */
706
+ onCursorHandleTouchMove: (arg1: React.TouchEvent<HTMLSpanElement>) => void =
707
+ (e) => {
708
+ e.stopPropagation();
709
+
710
+ const x = e.changedTouches[0].clientX;
711
+ const y = e.changedTouches[0].clientY;
712
+
713
+ const relativeX = x - this._containerBounds.left;
714
+ const relativeY =
715
+ y -
716
+ 2 * cursorHandleRadiusPx * cursorHandleDistanceMultiplier -
717
+ this._containerBounds.top;
718
+
719
+ // We subtract the containerBounds left/top to correct for the
720
+ // MathInput's position on the page. On top of that, we subtract an
721
+ // additional 2 x {height of the cursor} so that the bottom of the
722
+ // cursor tracks the user's finger, to make it visible under their
723
+ // touch.
724
+ this.setState({
725
+ handle: {
726
+ animateIntoPosition: false,
727
+ visible: true,
728
+ // TODO(charlie): Use clientX and clientY to avoid the need for
729
+ // scroll offsets. This likely also means that the cursor
730
+ // detection doesn't work when scrolled, since we're not
731
+ // offsetting those values.
732
+ x: this._constrainToBound(
733
+ relativeX,
734
+ 0,
735
+ this._containerBounds.width,
736
+ constrainingFrictionFactor,
737
+ ),
738
+ y: this._constrainToBound(
739
+ relativeY,
740
+ 0,
741
+ this._containerBounds.height,
742
+ constrainingFrictionFactor,
743
+ ),
744
+ },
745
+ });
746
+
747
+ // Use a y-coordinate that's just above where the user is actually
748
+ // touching because they're dragging the handle which is a little
749
+ // below where the cursor actually is.
750
+ const distanceAboveFingerToTrySelecting = 22;
751
+ const adjustedY = y - distanceAboveFingerToTrySelecting;
752
+
753
+ this._insertCursorAtClosestNode(x, adjustedY);
754
+ };
755
+
756
+ /**
757
+ * When the user releases the cursor handle, animate it back into place.
758
+ *
759
+ * @param {TouchEvent} e - the raw touch event from the browser
760
+ */
761
+ onCursorHandleTouchEnd: (arg1: React.TouchEvent<HTMLSpanElement>) => void =
762
+ (e) => {
763
+ e.stopPropagation();
764
+
765
+ this._updateCursorHandle(true);
766
+ };
767
+
768
+ /**
769
+ * If the gesture is cancelled mid-drag, simply hide it.
770
+ *
771
+ * @param {TouchEvent} e - the raw touch event from the browser
772
+ */
773
+ onCursorHandleTouchCancel: (
774
+ arg1: React.TouchEvent<HTMLSpanElement>,
775
+ ) => void = (e) => {
776
+ e.stopPropagation();
777
+
778
+ this._updateCursorHandle(true);
779
+ };
780
+
781
+ domKeyToMathQuillKey: (arg1: string) => string | null | undefined = (
782
+ key,
783
+ ) => {
784
+ const keyMap = {
785
+ "+": Keys.PLUS,
786
+ "-": Keys.MINUS,
787
+ "*": Keys.TIMES,
788
+ "/": Keys.DIVIDE,
789
+ ".": Keys.DECIMAL,
790
+ "%": Keys.PERCENT,
791
+ "=": Keys.EQUAL,
792
+ ">": Keys.GT,
793
+ "<": Keys.LT,
794
+ "^": Keys.EXP,
795
+ } as const;
796
+
797
+ // Numbers
798
+ if (["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].includes(key)) {
799
+ return `NUM_${key}`;
800
+ }
801
+
802
+ // Movement keys
803
+ else if (key === "Backspace") {
804
+ return Keys.BACKSPACE;
805
+ }
806
+
807
+ // Operators
808
+ else if (key in keyMap) {
809
+ return keyMap[key];
810
+ }
811
+
812
+ // The key pressed doesn't map to any of the math input operators
813
+ return null;
814
+ };
815
+
816
+ handleKeyUp: (arg1: React.KeyboardEvent<HTMLDivElement>) => void = (
817
+ event,
818
+ ) => {
819
+ const mathQuillKey = this.domKeyToMathQuillKey(event.key);
820
+
821
+ if (mathQuillKey) {
822
+ this.mathField.pressKey(mathQuillKey);
823
+
824
+ // TODO(diedra): If the new value being added is off-screen to the right
825
+ // due to the max-width of the text box, scroll the box to show the newest
826
+ // value
827
+ const value = this.mathField.getContent();
828
+ if (this.props.value !== value) {
829
+ this.mathField.setContent(this.props.value);
830
+ this.props.onChange(value, false);
831
+ this._hideCursorHandle();
832
+ }
833
+ }
834
+ };
835
+
836
+ getBorderWidthPx: () => number = () => {
837
+ // TODO(diedra): Move these to the common style package.
838
+ const normalBorderWidthPx = 1;
839
+ const focusedBorderWidthPx = 2;
840
+
841
+ return this.state.focused ? focusedBorderWidthPx : normalBorderWidthPx;
842
+ };
843
+
844
+ // Calculate the appropriate padding based on the border width (which is
845
+ // considered 'padding', since we're using 'border-box') and the fact
846
+ // that MathQuill automatically applies 2px of padding to the inner
847
+ // input.
848
+ getInputInnerPadding: () => {
849
+ paddingTop: number;
850
+ paddingRight: number;
851
+ paddingBottom: number;
852
+ paddingLeft: number;
853
+ } = () => {
854
+ const paddingInset = totalDesiredPadding - this.getBorderWidthPx();
855
+
856
+ // Now, translate that to the appropriate padding for each direction.
857
+ // The complication here is that we want numerals to be centered within
858
+ // the input. However, Symbola (MathQuill's font of choice) renders
859
+ // numerals with approximately 3px of padding below and 1px of padding
860
+ // above (to make room for ascenders and descenders). So we ignore those
861
+ // padding values for the vertical directions.
862
+ const symbolaPaddingBottom = 3;
863
+ const symbolaPaddingTop = 1;
864
+ const padding = {
865
+ paddingTop: paddingInset - symbolaPaddingTop,
866
+ paddingRight: paddingInset,
867
+ paddingBottom: paddingInset - symbolaPaddingBottom,
868
+ paddingLeft: paddingInset,
869
+ } as const;
870
+
871
+ return padding;
872
+ };
873
+
874
+ render(): React.ReactElement {
875
+ const {focused, handle} = this.state;
876
+ const {style} = this.props;
877
+
878
+ const innerStyle = {
879
+ ...inlineStyles.innerContainer,
880
+ borderWidth: this.getBorderWidthPx(),
881
+ ...(focused
882
+ ? {
883
+ borderColor: wonderBlocksBlue,
884
+ }
885
+ : {}),
886
+ ...style,
887
+ } as const;
888
+
889
+ // NOTE(diedra): This label explicitly refers to tapping because this field
890
+ // is currently only seen if the user is using a mobile device.
891
+ // We added the tapping instructions because there is currently a bug where
892
+ // Android users need to use two fingers to tap the input field to make the
893
+ // keyboard appear. It should only require one finger, which is how iOS works.
894
+ // TODO(diedra): Fix the bug that is causing Android to require a two finger tap
895
+ // to the open the keyboard, and then remove the second half of this label.
896
+ const ariaLabel =
897
+ i18n._("Math input box") +
898
+ " " +
899
+ i18n._("Tap with one or two fingers to open keyboard");
900
+
901
+ return (
902
+ <View
903
+ style={styles.input}
904
+ onTouchStart={this.handleTouchStart}
905
+ onTouchMove={this.handleTouchMove}
906
+ onTouchEnd={this.handleTouchEnd}
907
+ onClick={(e) => e.stopPropagation()}
908
+ role={"textbox"}
909
+ ariaLabel={ariaLabel}
910
+ >
911
+ {/* NOTE(charlie): This is used purely to namespace the styles in
912
+ overrides.css. */}
913
+ <div
914
+ className="keypad-input"
915
+ // @ts-expect-error [FEI-5003] - TS2322 - Type 'string' is not assignable to type 'number | undefined'.
916
+ tabIndex={"0"}
917
+ ref={(node) => {
918
+ this.inputRef = node;
919
+ }}
920
+ onKeyUp={this.handleKeyUp}
921
+ >
922
+ {/* NOTE(charlie): This element must be styled with inline
923
+ styles rather than with Aphrodite classes, as MathQuill
924
+ modifies the class names on the DOM node. */}
925
+ <div
926
+ ref={(node) => {
927
+ this._mathContainer = ReactDOM.findDOMNode(node);
928
+ }}
929
+ style={innerStyle}
930
+ />
931
+ </div>
932
+ {focused && handle.visible && (
933
+ <CursorHandle
934
+ {...handle}
935
+ onTouchStart={this.onCursorHandleTouchStart}
936
+ onTouchMove={this.onCursorHandleTouchMove}
937
+ onTouchEnd={this.onCursorHandleTouchEnd}
938
+ onTouchCancel={this.onCursorHandleTouchCancel}
939
+ />
940
+ )}
941
+ </View>
942
+ );
943
+ }
944
+ }
945
+
946
+ const fontSizePt = 18;
947
+ const inputMaxWidth = 128;
948
+
949
+ // The height of numerals in Symbola (rendered at 18pt) is about 20px (though
950
+ // they render at 24px due to padding for ascenders and descenders). We want our
951
+ // box to be laid out such that there's 12px of padding between a numeral and the
952
+ // edge of the input, so we use this 20px number as our 'base height' and
953
+ // account for the ascender and descender padding when computing the additional
954
+ // padding in our `render` method.
955
+ const numeralHeightPx = 20;
956
+ const totalDesiredPadding = 12;
957
+ const minHeightPx = numeralHeightPx + totalDesiredPadding * 2;
958
+ const minWidthPx = 64;
959
+
960
+ const styles = StyleSheet.create({
961
+ input: {
962
+ position: "relative",
963
+ display: "inline-block",
964
+ verticalAlign: "middle",
965
+ maxWidth: inputMaxWidth,
966
+ },
967
+ });
968
+
969
+ const inlineStyles = {
970
+ // Styles for the inner, MathQuill-ified input element. It's important that
971
+ // these are done with regular inline styles rather than Aphrodite classes
972
+ // as MathQuill adds CSS class names to the element outside of the typical
973
+ // React flow; assigning a class to the element can thus disrupt MathQuill
974
+ // behavior. For example, if the client provided new styles to be applied
975
+ // on focus and the styles here were applied with Aphrodite, then Aphrodite
976
+ // would merge the provided styles with the base styles here, producing a
977
+ // new CSS class name that we would apply to the element, clobbering any CSS
978
+ // class names that MathQuill had applied itself.
979
+ innerContainer: {
980
+ backgroundColor: "white",
981
+ minHeight: minHeightPx,
982
+ minWidth: minWidthPx,
983
+ maxWidth: inputMaxWidth,
984
+ boxSizing: "border-box",
985
+ position: "relative",
986
+ borderStyle: "solid",
987
+ borderColor: Color.offBlack50,
988
+ borderRadius: 4,
989
+ color: offBlack,
990
+ },
991
+ } as const;
992
+
993
+ export default MathInput;