@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,534 @@
1
+ import * as Redux from "redux";
2
+
3
+ import {tabletCutoffPx} from "../components/common-style";
4
+ import {computeLayoutParameters} from "../components/compute-layout-parameters";
5
+ import ExpressionKeypad from "../components/expression-keypad";
6
+ import FractionKeypad from "../components/fraction-keypad";
7
+ import GestureManager from "../components/gesture-manager";
8
+ import * as CursorContexts from "../components/input/cursor-contexts";
9
+ import VelocityTracker from "../components/velocity-tracker";
10
+ import {
11
+ DeviceOrientations,
12
+ DeviceTypes,
13
+ EchoAnimationTypes,
14
+ KeyTypes,
15
+ KeypadTypes,
16
+ LayoutModes,
17
+ } from "../consts";
18
+ import KeyConfigs from "../data/key-configs";
19
+ import Keys from "../data/keys";
20
+
21
+ const keypadForType = {
22
+ [KeypadTypes.FRACTION]: FractionKeypad,
23
+ [KeypadTypes.EXPRESSION]: ExpressionKeypad,
24
+ } as const;
25
+
26
+ export const createStore = (): any => {
27
+ const initialInputState: {
28
+ keyHandler: any;
29
+ cursor: any;
30
+ } = {
31
+ keyHandler: null,
32
+ cursor: {
33
+ context: CursorContexts.NONE,
34
+ },
35
+ };
36
+
37
+ const inputReducer = function (state = initialInputState, action: any) {
38
+ switch (action.type) {
39
+ case "SetKeyHandler":
40
+ return {
41
+ ...state,
42
+ keyHandler: action.keyHandler,
43
+ };
44
+
45
+ case "PressKey":
46
+ const keyConfig = KeyConfigs[action.key];
47
+ if (keyConfig.type !== KeyTypes.KEYPAD_NAVIGATION) {
48
+ // This is probably an anti-pattern but it works for the
49
+ // case where we don't actually control the state but we
50
+ // still want to communicate with the other object
51
+ return {
52
+ ...state,
53
+ cursor: state.keyHandler(keyConfig.id),
54
+ };
55
+ }
56
+
57
+ // TODO(kevinb) get state from MathQuill and store it?
58
+ return state;
59
+
60
+ case "SetCursor":
61
+ return {
62
+ ...state,
63
+ cursor: action.cursor,
64
+ };
65
+
66
+ default:
67
+ return state;
68
+ }
69
+ };
70
+
71
+ const defaultKeypadType = KeypadTypes.EXPRESSION;
72
+
73
+ const initialKeypadState = {
74
+ extraKeys: ["x", "y", Keys.THETA, Keys.PI],
75
+ keypadType: defaultKeypadType,
76
+ active: false,
77
+ } as const;
78
+
79
+ const keypadReducer = function (
80
+ state = initialKeypadState,
81
+ action: {
82
+ type: string;
83
+ },
84
+ ) {
85
+ switch (action.type) {
86
+ case "DismissKeypad":
87
+ return {
88
+ ...state,
89
+ active: false,
90
+ };
91
+
92
+ case "ActivateKeypad":
93
+ return {
94
+ ...state,
95
+ active: true,
96
+ };
97
+
98
+ case "ConfigureKeypad":
99
+ return {
100
+ ...state,
101
+ // Default `extraKeys` to the empty array.
102
+ extraKeys: [],
103
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'configuration' does not exist on type '{ type: string; }'.
104
+ ...action.configuration,
105
+ };
106
+
107
+ case "PressKey":
108
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'key' does not exist on type '{ type: string; }'.
109
+ const keyConfig = KeyConfigs[action.key];
110
+ // NOTE(charlie): Our keypad system operates by triggering key
111
+ // presses with key IDs in a dumb manner, such that the keys
112
+ // don't know what they can do--instead, the store is
113
+ // responsible for interpreting key presses and triggering the
114
+ // right actions when they occur. Hence, we figure off a
115
+ // dismissal here rather than dispatching a dismiss action in
116
+ // the first place.
117
+ if (keyConfig.id === Keys.DISMISS) {
118
+ return keypadReducer(state, {type: "DismissKeypad"});
119
+ }
120
+ return state;
121
+
122
+ default:
123
+ return state;
124
+ }
125
+ };
126
+
127
+ // We default to the right-most page. This is done so-as to enforce a
128
+ // consistent orientation between the view pager layout and the flattened
129
+ // layout, where our default page appears on the far right.
130
+ const getDefaultPage = (numPages) => numPages - 1;
131
+
132
+ const initialPagerState = {
133
+ animateToPosition: false,
134
+ currentPage: getDefaultPage(keypadForType[defaultKeypadType].numPages),
135
+ // The cumulative differential in the horizontal direction for the
136
+ // current swipe.
137
+ dx: 0,
138
+ numPages: keypadForType[defaultKeypadType].numPages,
139
+ pageWidthPx: 0,
140
+ velocityTracker: new VelocityTracker(),
141
+ } as const;
142
+
143
+ const pagerReducer = function (
144
+ state = initialPagerState,
145
+ action: {
146
+ type: string;
147
+ },
148
+ ) {
149
+ switch (action.type) {
150
+ case "ConfigureKeypad":
151
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'configuration' does not exist on type '{ type: string; }'.
152
+ const {keypadType} = action.configuration;
153
+ const {numPages} = keypadForType[keypadType];
154
+ return {
155
+ ...state,
156
+ numPages,
157
+ animateToPosition: false,
158
+ currentPage: getDefaultPage(numPages),
159
+ dx: 0,
160
+ };
161
+
162
+ case "SetPageSize":
163
+ return {
164
+ ...state,
165
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'pageWidthPx' does not exist on type '{ type: string; }'.
166
+ pageWidthPx: action.pageWidthPx,
167
+ };
168
+
169
+ case "PressKey":
170
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'key' does not exist on type '{ type: string; }'.
171
+ const keyConfig = KeyConfigs[action.key];
172
+
173
+ // Reset the keypad page if the user performs a math operation.
174
+ if (
175
+ keyConfig.type === KeyTypes.VALUE ||
176
+ keyConfig.type === KeyTypes.OPERATOR
177
+ ) {
178
+ return pagerReducer(state, {type: "ResetKeypadPage"});
179
+ }
180
+ return state;
181
+
182
+ case "ResetKeypadPage":
183
+ return {
184
+ ...state,
185
+ animateToPosition: true,
186
+ // We start at the right-most page.
187
+ currentPage: getDefaultPage(state.numPages),
188
+ dx: 0,
189
+ };
190
+
191
+ case "PageKeypadRight":
192
+ const nextPage = Math.min(
193
+ state.currentPage + 1,
194
+ state.numPages - 1,
195
+ );
196
+ return {
197
+ ...state,
198
+ animateToPosition: true,
199
+ currentPage: nextPage,
200
+ dx: 0,
201
+ };
202
+
203
+ case "PageKeypadLeft":
204
+ const prevPage = Math.max(state.currentPage - 1, 0);
205
+ return {
206
+ ...state,
207
+ animateToPosition: true,
208
+ currentPage: prevPage,
209
+ dx: 0,
210
+ };
211
+
212
+ case "OnSwipeChange":
213
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'dx' does not exist on type '{ type: string; }'.
214
+ state.velocityTracker.push(action.dx);
215
+
216
+ return {
217
+ ...state,
218
+ animateToPosition: false,
219
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'dx' does not exist on type '{ type: string; }'.
220
+ dx: action.dx,
221
+ };
222
+
223
+ case "OnSwipeEnd":
224
+ const {pageWidthPx, velocityTracker} = state;
225
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'dx' does not exist on type '{ type: string; }'.
226
+ const {dx} = action;
227
+ const velocity = velocityTracker.getVelocity();
228
+
229
+ // NOTE(charlie): These will need refinement. The velocity comes
230
+ // from Framer.
231
+ const minFlingVelocity = 0.1;
232
+ const minFlingDistance = 10;
233
+
234
+ const shouldPageRight =
235
+ dx < -pageWidthPx / 2 ||
236
+ (velocity < -minFlingVelocity && dx < -minFlingDistance);
237
+
238
+ const shouldPageLeft =
239
+ dx > pageWidthPx / 2 ||
240
+ (velocity > minFlingVelocity && dx > minFlingDistance);
241
+
242
+ if (shouldPageRight) {
243
+ return pagerReducer(state, {type: "PageKeypadRight"});
244
+ } else if (shouldPageLeft) {
245
+ return pagerReducer(state, {type: "PageKeypadLeft"});
246
+ }
247
+
248
+ return {
249
+ ...state,
250
+ animateToPosition: true,
251
+ dx: 0,
252
+ };
253
+
254
+ default:
255
+ return state;
256
+ }
257
+ };
258
+
259
+ const createGestureManager = (swipeEnabled: boolean) => {
260
+ return new GestureManager(
261
+ {
262
+ swipeEnabled,
263
+ },
264
+ {
265
+ onSwipeChange: (dx) => {
266
+ store.dispatch({
267
+ type: "OnSwipeChange",
268
+ dx,
269
+ });
270
+ },
271
+ onSwipeEnd: (dx) => {
272
+ store.dispatch({
273
+ type: "OnSwipeEnd",
274
+ dx,
275
+ });
276
+ },
277
+ onActiveNodesChanged: (activeNodes) => {
278
+ store.dispatch({
279
+ type: "SetActiveNodes",
280
+ activeNodes,
281
+ });
282
+ },
283
+ onClick: (key, layoutProps, inPopover) => {
284
+ store.dispatch({
285
+ type: "PressKey",
286
+ key,
287
+ ...layoutProps,
288
+ inPopover,
289
+ });
290
+ },
291
+ },
292
+ [],
293
+ [Keys.BACKSPACE, Keys.UP, Keys.RIGHT, Keys.DOWN, Keys.LEFT],
294
+ );
295
+ };
296
+
297
+ const initialGestureState = {
298
+ popover: null,
299
+ focus: null,
300
+ gestureManager: createGestureManager(
301
+ keypadForType[defaultKeypadType].numPages > 1,
302
+ ),
303
+ } as const;
304
+
305
+ const gestureReducer = function (state = initialGestureState, action: any) {
306
+ switch (action.type) {
307
+ case "DismissKeypad":
308
+ // NOTE(charlie): In the past, we enforced the "gesture manager
309
+ // will not receive any events when the keypad is hidden"
310
+ // assumption by assuming that the keypad would be hidden when
311
+ // dismissed and, as such, that none of its managed DOM nodes
312
+ // would be able to receive touch events. However, on mobile
313
+ // Safari, we're seeing that some of the keys receive touch
314
+ // events even when off-screen, inexplicably. So, to guard
315
+ // against that bug and make the contract explicit, we enable
316
+ // and disable event tracking on activation and dismissal.
317
+ state.gestureManager.disableEventTracking();
318
+ return state;
319
+
320
+ case "ActivateKeypad":
321
+ state.gestureManager.enableEventTracking();
322
+ return state;
323
+
324
+ case "SetActiveNodes":
325
+ return {
326
+ ...state,
327
+ ...action.activeNodes,
328
+ };
329
+
330
+ case "ConfigureKeypad":
331
+ const {keypadType} = action.configuration;
332
+ const {numPages} = keypadForType[keypadType];
333
+ const swipeEnabled = numPages > 1;
334
+ return {
335
+ popover: null,
336
+ focus: null,
337
+ gestureManager: createGestureManager(swipeEnabled),
338
+ };
339
+
340
+ default:
341
+ return state;
342
+ }
343
+ };
344
+
345
+ // Used to generate unique animation IDs for the echo animations. The actual
346
+ // values are irrelevant as long as they are unique.
347
+ let _lastAnimationId = 0;
348
+
349
+ const initialEchoState = {
350
+ echoes: [],
351
+ } as const;
352
+
353
+ const echoReducer = function (state = initialEchoState, action) {
354
+ switch (action.type) {
355
+ case "PressKey":
356
+ const keyConfig = KeyConfigs[action.key];
357
+
358
+ // Add in the echo animation if the user performs a math
359
+ // operation.
360
+ if (
361
+ keyConfig.type === KeyTypes.VALUE ||
362
+ keyConfig.type === KeyTypes.OPERATOR
363
+ ) {
364
+ return {
365
+ ...state,
366
+ echoes: [
367
+ ...state.echoes,
368
+ {
369
+ animationId: "" + _lastAnimationId++,
370
+ animationType: action.inPopover
371
+ ? EchoAnimationTypes.LONG_FADE_ONLY
372
+ : EchoAnimationTypes.FADE_ONLY,
373
+ borders: action.borders,
374
+ id: keyConfig.id,
375
+ initialBounds: action.initialBounds,
376
+ },
377
+ ],
378
+ };
379
+ }
380
+ return state;
381
+
382
+ case "RemoveEcho":
383
+ const remainingEchoes = state.echoes.filter((echo) => {
384
+ // @ts-expect-error [FEI-5003] - TS2339 - Property 'animationId' does not exist on type 'never'.
385
+ return echo.animationId !== action.animationId;
386
+ });
387
+ return {
388
+ ...state,
389
+ echoes: remainingEchoes,
390
+ };
391
+
392
+ default:
393
+ return state;
394
+ }
395
+ };
396
+
397
+ const initialLayoutState = {
398
+ gridDimensions: {
399
+ numRows: keypadForType[defaultKeypadType].rows,
400
+ numColumns: keypadForType[defaultKeypadType].columns,
401
+ numMaxVisibleRows: keypadForType[defaultKeypadType].maxVisibleRows,
402
+ numPages: keypadForType[defaultKeypadType].numPages,
403
+ },
404
+ buttonDimensions: {
405
+ widthPx: 48,
406
+ heightPx: 48,
407
+ },
408
+ pageDimensions: {
409
+ pageWidthPx: 0,
410
+ pageHeightPx: 0,
411
+ },
412
+ layoutMode: LayoutModes.FULLSCREEN,
413
+ paginationEnabled: false,
414
+ navigationPadEnabled: false,
415
+ } as const;
416
+
417
+ /**
418
+ * Compute the additional layout state based on the provided page and grid
419
+ * dimensions.
420
+ */
421
+ const layoutParametersForDimensions = (
422
+ pageDimensions:
423
+ | {
424
+ pageHeightPx: never;
425
+ pageWidthPx: never;
426
+ }
427
+ | {
428
+ pageHeightPx: number;
429
+ pageWidthPx: number;
430
+ },
431
+ gridDimensions,
432
+ ) => {
433
+ const {pageWidthPx, pageHeightPx} = pageDimensions;
434
+
435
+ // Determine the device type and orientation.
436
+ const deviceOrientation =
437
+ pageWidthPx > pageHeightPx
438
+ ? DeviceOrientations.LANDSCAPE
439
+ : DeviceOrientations.PORTRAIT;
440
+ const deviceType =
441
+ Math.min(pageWidthPx, pageHeightPx) > tabletCutoffPx
442
+ ? DeviceTypes.TABLET
443
+ : DeviceTypes.PHONE;
444
+
445
+ // Using that information, make some decisions (or assumptions)
446
+ // about the resulting layout.
447
+ const navigationPadEnabled = deviceType === DeviceTypes.TABLET;
448
+ const paginationEnabled =
449
+ deviceType === DeviceTypes.PHONE &&
450
+ deviceOrientation === DeviceOrientations.PORTRAIT;
451
+
452
+ const deviceInfo = {deviceOrientation, deviceType} as const;
453
+ const layoutOptions = {
454
+ navigationPadEnabled,
455
+ paginationEnabled,
456
+ // HACK(charlie): It's not great that we're making assumptions about
457
+ // the toolbar (which is rendered by webapp, and should always be
458
+ // visible and anchored to the bottom of the page for phone and
459
+ // tablet exercises). But this is primarily a heuristic (the goal is
460
+ // to preserve a 'good' amount of space between the top of the
461
+ // keypad and the top of the page) so we afford to have some margin
462
+ // of error.
463
+ toolbarEnabled: true,
464
+ } as const;
465
+
466
+ return {
467
+ ...computeLayoutParameters(
468
+ gridDimensions,
469
+ pageDimensions,
470
+ deviceInfo,
471
+ layoutOptions,
472
+ ),
473
+ // Pass along some of the layout information, so that other
474
+ // components in the heirarchy can adapt appropriately.
475
+ navigationPadEnabled,
476
+ paginationEnabled,
477
+ };
478
+ };
479
+
480
+ const layoutReducer = function (state = initialLayoutState, action: any) {
481
+ switch (action.type) {
482
+ case "ConfigureKeypad":
483
+ const {keypadType} = action.configuration;
484
+ const gridDimensions = {
485
+ numRows: keypadForType[keypadType].rows,
486
+ numColumns: keypadForType[keypadType].columns,
487
+ numMaxVisibleRows: keypadForType[keypadType].maxVisibleRows,
488
+ numPages: keypadForType[keypadType].numPages,
489
+ } as const;
490
+
491
+ return {
492
+ ...state,
493
+ ...layoutParametersForDimensions(
494
+ state.pageDimensions,
495
+ gridDimensions,
496
+ ),
497
+ gridDimensions,
498
+ };
499
+
500
+ case "SetPageSize":
501
+ const {pageWidthPx, pageHeightPx} = action;
502
+ const pageDimensions = {pageWidthPx, pageHeightPx} as const;
503
+
504
+ return {
505
+ ...state,
506
+ ...layoutParametersForDimensions(
507
+ pageDimensions,
508
+ state.gridDimensions,
509
+ ),
510
+ pageDimensions,
511
+ };
512
+
513
+ default:
514
+ return state;
515
+ }
516
+ };
517
+
518
+ const reducer = Redux.combineReducers({
519
+ input: inputReducer,
520
+ keypad: keypadReducer,
521
+ pager: pagerReducer,
522
+ gestures: gestureReducer,
523
+ echoes: echoReducer,
524
+ layout: layoutReducer,
525
+ });
526
+
527
+ // TODO(charlie): This non-inlined return is necessary so as to allow the
528
+ // gesture manager to dispatch actions on the store in its callbacks. We
529
+ // should come up with a better pattern to remove the two-way dependency.
530
+ // eslint-disable-next-line import/no-deprecated
531
+ const store = Redux.createStore(reducer);
532
+
533
+ return store;
534
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,14 @@
1
+ import {getDecimalSeparator} from "@khanacademy/wonder-blocks-i18n";
2
+
3
+ import {DecimalSeparators} from "./consts";
4
+
5
+ // NOTES(kevinb):
6
+ // - In order to get the correct decimal separator for the current locale,
7
+ // the locale must bet set using `setLocale(kaLocale)` which can be
8
+ // imported from wonder-blocks-i18n.
9
+ // - Some languages/locales use different decimal separators than the ones
10
+ // listed here. Much of the Arab world uses U+066C.
11
+ export const decimalSeparator: string =
12
+ getDecimalSeparator() === ","
13
+ ? DecimalSeparators.COMMA
14
+ : DecimalSeparators.PERIOD;
@@ -0,0 +1,111 @@
1
+ import os
2
+ import re
3
+ import sys
4
+ import symbol_map
5
+
6
+ color = '#3B3E40'
7
+
8
+ header = '''/**
9
+ * An autogenerated component that renders the %s iconograpy in SVG.
10
+ *
11
+ * Generated with: https://gist.github.com/crm416/3c7abc88e520eaed72347af240b32590.
12
+ */
13
+ const React = require('react');
14
+ const PropTypes = require('prop-types');
15
+ '''
16
+
17
+ # A simple template with no PropTypes, for the case in which there are no
18
+ # colors in the component.
19
+ simple_template = header + '''
20
+ const %s = () => {
21
+ return %s;
22
+ };
23
+
24
+ module.exports = %s;
25
+ '''
26
+ complex_template = header + '''
27
+ const %s = React.createClass({
28
+ propTypes: {
29
+ color: PropTypes.string.isRequired,
30
+ },
31
+
32
+ render() {
33
+ return %s;
34
+ },
35
+ });
36
+
37
+ module.exports = %s;
38
+ '''
39
+
40
+ index_template = '''/**
41
+ * A directory of autogenerated icon components.
42
+ */
43
+
44
+ module.exports = {
45
+ %s
46
+ };
47
+ '''
48
+
49
+ if __name__ == '__main__':
50
+ input_dir_name = sys.argv[1]
51
+ output_dir_name = sys.argv[2]
52
+
53
+ filename_map = {}
54
+
55
+ for filename in os.listdir(input_dir_name):
56
+ use_simple = True
57
+
58
+ svg_filename = filename.split('/')[-1].split('.')[0]
59
+
60
+ prefix = 'math-keypad-icon-'
61
+ if svg_filename[:len(prefix)] == prefix:
62
+ svg_filename = svg_filename[len(prefix):]
63
+ else:
64
+ continue
65
+
66
+ try:
67
+ symbol_name = symbol_map.filename_to_symbol[svg_filename]
68
+ except:
69
+ print 'Skipping: ' + svg_filename
70
+ continue
71
+
72
+ component_name = symbol_name.title().replace('_', '')
73
+ js_filename = symbol_name.lower().replace('_', '-')
74
+
75
+ with open(input_dir_name + '/' + filename, 'r') as f:
76
+ contents = f.read()
77
+
78
+ # Strip out the namespace tag
79
+ namespace = 'xmlns="http://www.w3.org/2000/svg"'
80
+ namespace_index = contents.index(namespace)
81
+ contents = contents[:namespace_index - 1] + contents[namespace_index + len(namespace):]
82
+
83
+ # Replace any colors
84
+ before = contents
85
+ contents = re.sub('"' + color + '"', '{this.props.color}', contents)
86
+ if before != contents:
87
+ use_simple = False
88
+
89
+ # Replace the xlink:href tag (special case)
90
+ contents = contents.replace('xlink:href', 'xlinkHref')
91
+ # Replace any other tags
92
+ tags = re.findall(r' (\S+)=', contents)
93
+ for tag in tags:
94
+ pieces = tag.split('-')
95
+ jsx_version = pieces[0] + ''.join(map(lambda x: x.title(), pieces[1:]))
96
+ contents = contents.replace(tag, jsx_version)
97
+
98
+ with open(output_dir_name + '/' + js_filename + '.js', 'w') as f:
99
+ template = simple_template if use_simple else complex_template
100
+ f.write(template % (symbol_name, component_name, contents, component_name))
101
+
102
+ filename_map[symbol_name] = js_filename
103
+
104
+ index_contents = ''
105
+ for symbol_name in filename_map:
106
+ if index_contents:
107
+ index_contents += '\n'
108
+ index_contents += ' %s: require(\'./%s\'),' % (symbol_name, filename_map[symbol_name])
109
+
110
+ with open(output_dir_name + '/index.js', 'w') as f:
111
+ f.write(index_template % index_contents)
@@ -0,0 +1,32 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+ <svg width="48px" height="48px" viewBox="0 0 48 48" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
+ <!-- Generator: sketchtool 39.1 (31720) - http://www.bohemiancoding.com/sketch -->
4
+ <title>64B30C56-8FCE-4ECE-BD13-525F6207F288</title>
5
+ <desc>Created with sketchtool.</desc>
6
+ <defs>
7
+ <rect id="path-1" x="0" y="0" width="600" height="956"></rect>
8
+ </defs>
9
+ <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
10
+ <g id="Math-Input" transform="translate(-634.000000, -1516.000000)"></g>
11
+ <g id="Asset-Sheet" transform="translate(-434.000000, -420.000000)">
12
+ <g id="math_keypad_icon_0" transform="translate(434.000000, 420.000000)">
13
+ <rect id="bounds" fill-opacity="0" fill="#FF0088" x="0" y="0" width="48" height="48"></rect>
14
+ <path d="M24,32.3 C28.55,32.3 30.375,27.725 30.375,23.675 C30.375,19.6 28.55,15.075 24,15.075 C19.425,15.075 17.625,19.6 17.625,23.675 C17.625,27.725 19.425,32.3 24,32.3 L24,32.3 Z M24,30.45 C20.825,30.45 19.75,27 19.75,23.675 C19.75,20.35 20.825,16.925 24,16.925 C27.175,16.925 28.25,20.35 28.25,23.675 C28.25,27 27.175,30.45 24,30.45 L24,30.45 Z" id="0" fill="#3B3E40"></path>
15
+ </g>
16
+ </g>
17
+ <g id="Math-Input" transform="translate(-634.000000, -1516.000000)">
18
+ <g id="Asset-Sheet" transform="translate(200.000000, 1096.000000)">
19
+ <mask id="mask-2" fill="white">
20
+ <use xlink:href="#path-1"></use>
21
+ </mask>
22
+ <use id="Mask" fill="#FAFAFA" xlink:href="#path-1"></use>
23
+ <g id="math_keypad_icon_0" mask="url(#mask-2)">
24
+ <g transform="translate(434.000000, 420.000000)">
25
+ <rect id="bounds" fill-opacity="0" fill="#FF0088" x="0" y="0" width="48" height="48"></rect>
26
+ <path d="M24,32.3 C28.55,32.3 30.375,27.725 30.375,23.675 C30.375,19.6 28.55,15.075 24,15.075 C19.425,15.075 17.625,19.6 17.625,23.675 C17.625,27.725 19.425,32.3 24,32.3 L24,32.3 Z M24,30.45 C20.825,30.45 19.75,27 19.75,23.675 C19.75,20.35 20.825,16.925 24,16.925 C27.175,16.925 28.25,20.35 28.25,23.675 C28.25,27 27.175,30.45 24,30.45 L24,30.45 Z" id="0" fill="#3B3E40"></path>
27
+ </g>
28
+ </g>
29
+ </g>
30
+ </g>
31
+ </g>
32
+ </svg>