@khanacademy/math-input 17.0.3 → 17.0.5

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 (84) hide show
  1. package/dist/es/index.js +2 -2
  2. package/dist/es/index.js.map +1 -1
  3. package/dist/index.js +2 -2
  4. package/dist/index.js.map +1 -1
  5. package/package.json +5 -2
  6. package/.eslintrc.js +0 -18
  7. package/CHANGELOG.md +0 -654
  8. package/less/main.less +0 -2
  9. package/less/overrides.less +0 -122
  10. package/src/components/__tests__/integration.test.tsx +0 -300
  11. package/src/components/aphrodite-css-transition-group/index.tsx +0 -78
  12. package/src/components/aphrodite-css-transition-group/transition-child.tsx +0 -192
  13. package/src/components/aphrodite-css-transition-group/types.ts +0 -20
  14. package/src/components/aphrodite-css-transition-group/util.ts +0 -97
  15. package/src/components/input/__tests__/context-tracking.test.ts +0 -176
  16. package/src/components/input/__tests__/mathquill-helpers.test.ts +0 -105
  17. package/src/components/input/__tests__/mathquill.test.ts +0 -747
  18. package/src/components/input/__tests__/test-math-wrapper.ts +0 -29
  19. package/src/components/input/cursor-contexts.ts +0 -37
  20. package/src/components/input/cursor-handle.tsx +0 -137
  21. package/src/components/input/cursor-styles.ts +0 -10
  22. package/src/components/input/drag-listener.ts +0 -79
  23. package/src/components/input/math-input.tsx +0 -1036
  24. package/src/components/input/math-wrapper.ts +0 -189
  25. package/src/components/input/mathquill-helpers.ts +0 -262
  26. package/src/components/input/mathquill-instance.ts +0 -106
  27. package/src/components/input/mathquill-types.ts +0 -32
  28. package/src/components/input/scroll-into-view.ts +0 -65
  29. package/src/components/key-handlers/__tests__/handle-jump-out.test.ts +0 -94
  30. package/src/components/key-handlers/handle-arrow.ts +0 -70
  31. package/src/components/key-handlers/handle-backspace.ts +0 -277
  32. package/src/components/key-handlers/handle-exponent.ts +0 -53
  33. package/src/components/key-handlers/handle-jump-out.ts +0 -107
  34. package/src/components/key-handlers/key-translator.ts +0 -222
  35. package/src/components/keypad/__tests__/__snapshots__/keypad.test.tsx.snap +0 -1913
  36. package/src/components/keypad/__tests__/__snapshots__/mobile-keypad.test.tsx.snap +0 -600
  37. package/src/components/keypad/__tests__/keypad-button.test.tsx +0 -84
  38. package/src/components/keypad/__tests__/keypad-v2-mathquill.test.tsx +0 -304
  39. package/src/components/keypad/__tests__/keypad-v2.cypress.ts +0 -16
  40. package/src/components/keypad/__tests__/keypad.test.tsx +0 -321
  41. package/src/components/keypad/__tests__/mobile-keypad.test.tsx +0 -115
  42. package/src/components/keypad/__tests__/test-data-tabs.ts +0 -21
  43. package/src/components/keypad/button-assets.tsx +0 -1880
  44. package/src/components/keypad/index.tsx +0 -2
  45. package/src/components/keypad/keypad-button.stories.tsx +0 -81
  46. package/src/components/keypad/keypad-button.tsx +0 -124
  47. package/src/components/keypad/keypad-mathquill.stories.tsx +0 -109
  48. package/src/components/keypad/keypad-pages/extras-page.tsx +0 -35
  49. package/src/components/keypad/keypad-pages/fractions-page.tsx +0 -125
  50. package/src/components/keypad/keypad-pages/geometry-page.tsx +0 -34
  51. package/src/components/keypad/keypad-pages/keypad-pages.stories.tsx +0 -37
  52. package/src/components/keypad/keypad-pages/numbers-page.tsx +0 -94
  53. package/src/components/keypad/keypad-pages/operators-page.tsx +0 -117
  54. package/src/components/keypad/keypad.tsx +0 -233
  55. package/src/components/keypad/mobile-keypad-internals.tsx +0 -240
  56. package/src/components/keypad/mobile-keypad.tsx +0 -24
  57. package/src/components/keypad/navigation-button.tsx +0 -127
  58. package/src/components/keypad/navigation-pad.stories.tsx +0 -26
  59. package/src/components/keypad/navigation-pad.tsx +0 -67
  60. package/src/components/keypad/shared-keys.tsx +0 -109
  61. package/src/components/keypad/utils.ts +0 -34
  62. package/src/components/keypad-context.tsx +0 -70
  63. package/src/components/prop-types.ts +0 -16
  64. package/src/components/tabbar/__tests__/tabbar.test.tsx +0 -105
  65. package/src/components/tabbar/icons.tsx +0 -122
  66. package/src/components/tabbar/index.ts +0 -1
  67. package/src/components/tabbar/item.tsx +0 -146
  68. package/src/components/tabbar/tabbar.stories.tsx +0 -83
  69. package/src/components/tabbar/tabbar.tsx +0 -65
  70. package/src/data/key-configs.ts +0 -770
  71. package/src/data/keys.ts +0 -123
  72. package/src/enums.ts +0 -27
  73. package/src/fake-react-native-web/index.ts +0 -11
  74. package/src/fake-react-native-web/text.tsx +0 -55
  75. package/src/fake-react-native-web/view.tsx +0 -91
  76. package/src/full-keypad.stories.tsx +0 -142
  77. package/src/full-mobile-input.stories.tsx +0 -115
  78. package/src/index.ts +0 -52
  79. package/src/types.ts +0 -70
  80. package/src/utils.test.ts +0 -33
  81. package/src/utils.ts +0 -61
  82. package/src/version.ts +0 -10
  83. package/tsconfig-build.json +0 -11
  84. package/tsconfig-build.tsbuildinfo +0 -1
@@ -1,94 +0,0 @@
1
- import {createMathField} from "../../input/mathquill-instance";
2
- import handleJumpOut from "../handle-jump-out";
3
-
4
- describe("handleJumpOut", () => {
5
- it("jumps out of parenthesis", () => {
6
- // Arrange
7
- const mount = document.createElement("div");
8
- const mathField = createMathField(mount);
9
-
10
- // Act
11
- mathField.typedText("(4");
12
- handleJumpOut(mathField, "JUMP_OUT_PARENTHESES");
13
- mathField.typedText("6");
14
-
15
- // Assert
16
- expect(mathField.latex()).toBe("\\left(4\\right)6");
17
- });
18
-
19
- it("jumps into numerator", () => {
20
- // Arrange
21
- const mount = document.createElement("div");
22
- const mathField = createMathField(mount);
23
-
24
- // Act
25
- mathField.typedText("4");
26
- mathField.cmd("frac");
27
- mathField.keystroke("Left");
28
- handleJumpOut(mathField, "JUMP_INTO_NUMERATOR");
29
- mathField.typedText("6");
30
-
31
- // Assert
32
- expect(mathField.latex()).toBe("4\\frac{6}{ }");
33
- });
34
-
35
- it("jumps out of numerator", () => {
36
- // Arrange
37
- const mount = document.createElement("div");
38
- const mathField = createMathField(mount);
39
-
40
- // Act
41
- mathField.cmd("frac");
42
- mathField.typedText("4");
43
- handleJumpOut(mathField, "JUMP_OUT_NUMERATOR");
44
- mathField.typedText("6");
45
-
46
- // Assert
47
- expect(mathField.latex()).toBe("\\frac{4}{6}");
48
- });
49
-
50
- it("jumps out of denominator", () => {
51
- // Arrange
52
- const mount = document.createElement("div");
53
- const mathField = createMathField(mount);
54
-
55
- // Act
56
- mathField.cmd("frac");
57
- mathField.typedText("4");
58
- handleJumpOut(mathField, "JUMP_OUT_NUMERATOR");
59
- mathField.typedText("6");
60
- handleJumpOut(mathField, "JUMP_OUT_DENOMINATOR");
61
- mathField.typedText("2");
62
-
63
- // Assert
64
- expect(mathField.latex()).toBe("\\frac{4}{6}2");
65
- });
66
-
67
- it("jumps out of superscript", () => {
68
- // Arrange
69
- const mount = document.createElement("div");
70
- const mathField = createMathField(mount);
71
-
72
- // Act
73
- mathField.typedText("4^22");
74
- handleJumpOut(mathField, "JUMP_OUT_EXPONENT");
75
- mathField.typedText("6");
76
-
77
- // Assert
78
- expect(mathField.latex()).toBe("4^{22}6");
79
- });
80
-
81
- it("jumps out of subscript", () => {
82
- // Arrange
83
- const mount = document.createElement("div");
84
- const mathField = createMathField(mount);
85
-
86
- // Act
87
- mathField.typedText("4_22");
88
- handleJumpOut(mathField, "JUMP_OUT_BASE");
89
- mathField.typedText("6");
90
-
91
- // Assert
92
- expect(mathField.latex()).toBe("4_{22}6");
93
- });
94
- });
@@ -1,70 +0,0 @@
1
- import {
2
- maybeFindCommand,
3
- maybeFindCommandBeforeParens,
4
- } from "../input/mathquill-helpers";
5
- import {mathQuillInstance} from "../input/mathquill-instance";
6
- import {MathFieldActionType} from "../input/mathquill-types";
7
-
8
- import type Key from "../../data/keys";
9
- import type {
10
- MathFieldInterface,
11
- MathFieldCursor,
12
- } from "../input/mathquill-types";
13
-
14
- function handleLeftArrow(
15
- mathField: MathFieldInterface,
16
- cursor: MathFieldCursor,
17
- ) {
18
- // If we're inside a function, and just after the left parentheses, we
19
- // need to skip the entire function name, rather than move the cursor
20
- // inside of it. For example, when hitting left from within the
21
- // parentheses in `cos()`, we want to place the cursor to the left of
22
- // the entire expression, rather than between the `s` and the left
23
- // parenthesis.
24
- // From the cursor's perspective, this requires that our left node is
25
- // the ActionType.MQ_END node, that our grandparent is the left parenthesis, and
26
- // the nodes to the left of our grandparent comprise a valid function
27
- // name.
28
- if (cursor[mathQuillInstance.L] === MathFieldActionType.MQ_END) {
29
- const parent = cursor.parent;
30
- const grandparent = parent.parent;
31
- if (grandparent.ctrlSeq === "\\left(") {
32
- const command = maybeFindCommandBeforeParens(grandparent);
33
- if (command) {
34
- cursor.insLeftOf(command.startNode);
35
- return;
36
- }
37
- }
38
- }
39
-
40
- // Otherwise, we default to the standard MathQull left behavior.
41
- mathField.keystroke("Left");
42
- }
43
-
44
- function handleRightArrow(
45
- mathField: MathFieldInterface,
46
- cursor: MathFieldCursor,
47
- ) {
48
- const command = maybeFindCommand(cursor[mathQuillInstance.R]);
49
- if (command) {
50
- // Similarly, if a function is to our right, then we need to place
51
- // the cursor at the start of its parenthetical content, which is
52
- // done by putting it to the left of ites parentheses and then
53
- // moving right once.
54
- cursor.insLeftOf(command.endNode);
55
- mathField.keystroke("Right");
56
- } else {
57
- // Otherwise, we default to the standard MathQull right behavior.
58
- mathField.keystroke("Right");
59
- }
60
- }
61
-
62
- export default function handleArrow(mathField: MathFieldInterface, key: Key) {
63
- const cursor = mathField.cursor();
64
-
65
- if (key === "LEFT") {
66
- handleLeftArrow(mathField, cursor);
67
- } else if (key === "RIGHT") {
68
- handleRightArrow(mathField, cursor);
69
- }
70
- }
@@ -1,277 +0,0 @@
1
- import {
2
- isFraction,
3
- isSquareRoot,
4
- isNthRoot,
5
- isNthRootIndex,
6
- isInsideLogIndex,
7
- isInsideEmptyNode,
8
- selectNode,
9
- maybeFindCommandBeforeParens,
10
- } from "../input/mathquill-helpers";
11
- import {mathQuillInstance} from "../input/mathquill-instance";
12
- import {MathFieldActionType} from "../input/mathquill-types";
13
-
14
- import type {
15
- MathFieldInterface,
16
- MathFieldCursor,
17
- } from "../input/mathquill-types";
18
-
19
- function handleBackspaceInNthRoot(
20
- mathField: MathFieldInterface,
21
- cursor: MathFieldCursor,
22
- ) {
23
- const isAtLeftEnd =
24
- cursor[mathQuillInstance.L] === MathFieldActionType.MQ_END;
25
-
26
- const isRootEmpty = isInsideEmptyNode(cursor.parent.parent.blocks[0].ends);
27
-
28
- if (isAtLeftEnd) {
29
- selectNode(cursor.parent.parent, cursor);
30
-
31
- if (isRootEmpty) {
32
- mathField.keystroke("Backspace");
33
- }
34
- } else {
35
- mathField.keystroke("Backspace");
36
- }
37
- }
38
-
39
- function handleBackspaceInRootIndex(
40
- mathField: MathFieldInterface,
41
- cursor: MathFieldCursor,
42
- ) {
43
- if (isInsideEmptyNode(cursor)) {
44
- // When deleting the index in a nthroot, we change from the nthroot
45
- // to a sqrt, e.g. \sqrt[|]{35x-5} => |\sqrt{35x-5}. If there's no
46
- // content under the root, then we delete the whole thing.
47
-
48
- const grandparent = cursor.parent.parent;
49
- const latex = grandparent.latex();
50
- const reinsertionPoint = grandparent[mathQuillInstance.L];
51
-
52
- selectNode(grandparent, cursor);
53
-
54
- const rootIsEmpty =
55
- (grandparent.blocks[1]._el as HTMLElement).textContent === "";
56
-
57
- if (rootIsEmpty) {
58
- // If there is not content under the root then simply delete
59
- // the whole thing.
60
- mathField.keystroke("Backspace");
61
- } else {
62
- // Replace the nthroot with a sqrt if there was content under
63
- // the root.
64
-
65
- // Start by deleting the selection.
66
- mathField.keystroke("Backspace");
67
-
68
- // Replace the nth-root with a sqrt.
69
- mathField.write(latex.replace(/^\\sqrt\[\]/, "\\sqrt"));
70
-
71
- // Adjust the cursor to be to the left the sqrt.
72
- if (reinsertionPoint === MathFieldActionType.MQ_END) {
73
- mathField.moveToLeftEnd();
74
- } else {
75
- cursor.insRightOf(reinsertionPoint);
76
- }
77
- }
78
- } else {
79
- if (cursor[mathQuillInstance.L] !== MathFieldActionType.MQ_END) {
80
- // If the cursor is not at the leftmost position inside the
81
- // root's index, delete a character.
82
- mathField.keystroke("Backspace");
83
- } else {
84
- // TODO(kevinb) verify that we want this behavior after testing
85
- // Do nothing because we haven't completely deleted the
86
- // index of the radical.
87
- }
88
- }
89
- }
90
-
91
- function handleBackspaceInLogIndex(
92
- mathField: MathFieldInterface,
93
- cursor: MathFieldCursor,
94
- ) {
95
- if (isInsideEmptyNode(cursor)) {
96
- const grandparent = cursor.parent.parent;
97
- const command = maybeFindCommandBeforeParens(grandparent);
98
-
99
- cursor.insLeftOf(command?.startNode);
100
- cursor.startSelection();
101
-
102
- if (grandparent[mathQuillInstance.R] !== MathFieldActionType.MQ_END) {
103
- cursor.insRightOf(grandparent[mathQuillInstance.R]);
104
- } else {
105
- cursor.insRightOf(grandparent);
106
- }
107
-
108
- cursor.select();
109
- cursor.endSelection();
110
- const isLogBodyEmpty =
111
- grandparent[mathQuillInstance.R]._el.textContent === "";
112
-
113
- if (isLogBodyEmpty) {
114
- // If there's no content inside the log's parens then delete the
115
- // whole thing.
116
- mathField.keystroke("Backspace");
117
- }
118
- } else {
119
- mathField.keystroke("Backspace");
120
- }
121
- }
122
-
123
- function handleBackspaceOutsideParens(cursor: MathFieldCursor) {
124
- // In this case the node with '\\left(' for its ctrlSeq
125
- // is the parent of the expression contained within the
126
- // parentheses.
127
- //
128
- // Handle selecting an expression before deleting:
129
- // (x+1)| => |(x+1)|
130
- // \log(x+1)| => |\log(x+1)|
131
-
132
- const leftNode = cursor[mathQuillInstance.L];
133
- const rightNode = cursor[mathQuillInstance.R];
134
- const command = maybeFindCommandBeforeParens(leftNode);
135
-
136
- if (command && command.startNode) {
137
- // There's a command before the parens so we select it as well as
138
- // the parens.
139
- cursor.insLeftOf(command.startNode);
140
- cursor.startSelection();
141
- if (rightNode === MathFieldActionType.MQ_END) {
142
- cursor.insAtRightEnd(cursor.parent);
143
- } else {
144
- cursor.insLeftOf(rightNode);
145
- }
146
- cursor.select();
147
- cursor.endSelection();
148
- } else {
149
- cursor.startSelection();
150
- cursor.insLeftOf(leftNode); // left of \\left(
151
- cursor.select();
152
- cursor.endSelection();
153
- }
154
- }
155
-
156
- function handleBackspaceInsideParens(
157
- mathField: MathFieldInterface,
158
- cursor: MathFieldCursor,
159
- ) {
160
- // Handle situations when the cursor is inside parens or a
161
- // command that uses parens, e.g. \log() or \tan()
162
- //
163
- // MathQuill represents log(x+1) in roughly the following way
164
- // [l, o, g, \\left[parent:[x, +, 1]]]
165
- //
166
- // If the cursor is inside the parentheses it's next to one of:
167
- // x, +, or 1. This makes sub_sub_expr its parent and sub_expr
168
- // it's parent.
169
- //
170
- // Interestingly parent doesn't have any nodes to the left or
171
- // right of it (even though the corresponding DOM node has
172
- // ( and ) characters on either side.
173
- //
174
- // The grandparent's ctrlSeq is `\\left(`. The `\\right)` isn't
175
- // stored anywhere. NOTE(kevinb): I believe this is because
176
- // MathQuill knows what the close paren should be and does the
177
- // right thing at render time.
178
- //
179
- // This conditional branch handles the following cases:
180
- // - \log(x+1|) => \log(x+|)
181
- // - \log(|x+1) => |\log(x+1)|
182
- // - \log(|) => |
183
-
184
- if (cursor[mathQuillInstance.L] !== MathFieldActionType.MQ_END) {
185
- // This command contains math and there's some math to
186
- // the left of the cursor that we should delete normally
187
- // before doing anything special.
188
- mathField.keystroke("Backspace");
189
- return;
190
- }
191
-
192
- const grandparent = cursor.parent.parent;
193
-
194
- // If the cursors is inside the parens at the start but the command
195
- // has a subscript as is the case in log_n then move the cursor into
196
- // the subscript, e.g. \log_{5}(|x+1) => \log_{5|}(x+1)
197
-
198
- if (grandparent[mathQuillInstance.L].sub) {
199
- // if there is a subscript
200
- if (grandparent[mathQuillInstance.L].sub._el.textContent) {
201
- // and it contains text
202
- // move the cursor to the right end of the subscript
203
- cursor.insAtRightEnd(grandparent[mathQuillInstance.L].sub);
204
- return;
205
- }
206
- }
207
-
208
- // Determine if the parens are empty before we modify the
209
- // cursor's position.
210
- const isEmpty = isInsideEmptyNode(cursor);
211
-
212
- // Insert the cursor to the left of the command if there is one
213
- // or before the '\\left(` if there isn't
214
- const command = maybeFindCommandBeforeParens(grandparent);
215
-
216
- cursor.insLeftOf((command && command.startNode) || grandparent);
217
- cursor.startSelection();
218
- cursor.insRightOf(grandparent);
219
- cursor.select();
220
- cursor.endSelection();
221
-
222
- // Delete the selection, but only if the parens were empty to
223
- // begin with.
224
- if (isEmpty) {
225
- mathField.keystroke("Backspace");
226
- }
227
- }
228
-
229
- function handleBackspaceAfterLigaturedSymbol(mathField: MathFieldInterface) {
230
- mathField.keystroke("Backspace");
231
- mathField.keystroke("Backspace");
232
- }
233
-
234
- /**
235
- * Selects and deletes part of the expression based on the cursor location.
236
- * See inline comments for precise behavior of different cases.
237
- */
238
- function handleBackspace(mathField: MathFieldInterface) {
239
- const cursor = mathField.cursor();
240
- if (!cursor.selection) {
241
- const parent = cursor.parent;
242
- const grandparent = parent.parent;
243
- const leftNode = cursor[mathQuillInstance.L];
244
-
245
- if (isFraction(leftNode)) {
246
- selectNode(leftNode, cursor);
247
- } else if (isSquareRoot(leftNode)) {
248
- selectNode(leftNode, cursor);
249
- } else if (isNthRoot(leftNode)) {
250
- selectNode(leftNode, cursor);
251
- } else if (isNthRootIndex(parent)) {
252
- handleBackspaceInRootIndex(mathField, cursor);
253
- } else if (leftNode.ctrlSeq === "\\left(") {
254
- handleBackspaceOutsideParens(cursor);
255
- } else if (grandparent.ctrlSeq === "\\left(") {
256
- handleBackspaceInsideParens(mathField, cursor);
257
- } else if (isInsideLogIndex(cursor)) {
258
- handleBackspaceInLogIndex(mathField, cursor);
259
- } else if (
260
- leftNode.ctrlSeq === "\\ge " ||
261
- leftNode.ctrlSeq === "\\le "
262
- ) {
263
- handleBackspaceAfterLigaturedSymbol(mathField);
264
- } else if (
265
- isNthRoot(grandparent) &&
266
- leftNode === MathFieldActionType.MQ_END
267
- ) {
268
- handleBackspaceInNthRoot(mathField, cursor);
269
- } else {
270
- mathField.keystroke("Backspace");
271
- }
272
- } else {
273
- mathField.keystroke("Backspace");
274
- }
275
- }
276
-
277
- export default handleBackspace;
@@ -1,53 +0,0 @@
1
- import {mathQuillInstance} from "../input/mathquill-instance";
2
- import {MathFieldActionType} from "../input/mathquill-types";
3
-
4
- import type Key from "../../data/keys";
5
- import type {MathFieldInterface} from "../input/mathquill-types";
6
-
7
- const ArithmeticOperators = ["+", "-", "\\cdot", "\\times", "\\div"];
8
- const EqualityOperators = ["=", "\\neq", "<", "\\leq", ">", "\\geq"];
9
-
10
- export default function handleExponent(
11
- mathField: MathFieldInterface,
12
- key: Key,
13
- ) {
14
- const cursor = mathField.cursor();
15
- // If there's an invalid operator preceding the cursor (anything that
16
- // knowingly cannot be raised to a power), add an empty set of
17
- // parentheses and apply the exponent to that.
18
- const invalidPrefixes = [...ArithmeticOperators, ...EqualityOperators];
19
-
20
- const precedingNode = cursor[mathQuillInstance.L];
21
- const shouldPrefixWithParens =
22
- precedingNode === MathFieldActionType.MQ_END ||
23
- invalidPrefixes.includes(precedingNode.ctrlSeq.trim());
24
- if (shouldPrefixWithParens) {
25
- mathField.write("\\left(\\right)");
26
- }
27
-
28
- // Insert the appropriate exponent operator.
29
- switch (key) {
30
- case "EXP":
31
- mathField.cmd("^");
32
- break;
33
-
34
- case "EXP_2":
35
- case "EXP_3":
36
- mathField.write(`^${key === "EXP_2" ? 2 : 3}`);
37
-
38
- // If we enter a square or a cube, we should leave the cursor
39
- // within the newly inserted parens, if they exist. This takes
40
- // exactly four left strokes, since the cursor by default would
41
- // end up to the right of the exponent.
42
- if (shouldPrefixWithParens) {
43
- mathField.keystroke("Left");
44
- mathField.keystroke("Left");
45
- mathField.keystroke("Left");
46
- mathField.keystroke("Left");
47
- }
48
- break;
49
-
50
- default:
51
- throw new Error(`Invalid exponent key: ${key}`);
52
- }
53
- }
@@ -1,107 +0,0 @@
1
- import {CursorContext} from "../input/cursor-contexts";
2
- import {
3
- isFraction,
4
- isParens,
5
- getCursorContext,
6
- } from "../input/mathquill-helpers";
7
- import {mathQuillInstance} from "../input/mathquill-instance";
8
- import {MathFieldActionType} from "../input/mathquill-types";
9
-
10
- import type Key from "../../data/keys";
11
- import type {MathFieldInterface} from "../input/mathquill-types";
12
-
13
- const KeysForJumpContext = {
14
- [CursorContext.IN_PARENS]: "JUMP_OUT_PARENTHESES",
15
- [CursorContext.IN_SUPER_SCRIPT]: "JUMP_OUT_EXPONENT",
16
- [CursorContext.IN_SUB_SCRIPT]: "JUMP_OUT_BASE",
17
- [CursorContext.BEFORE_FRACTION]: "JUMP_INTO_NUMERATOR",
18
- [CursorContext.IN_NUMERATOR]: "JUMP_OUT_NUMERATOR",
19
- [CursorContext.IN_DENOMINATOR]: "JUMP_OUT_DENOMINATOR",
20
- };
21
-
22
- /**
23
- * Advances the cursor to the next logical position.
24
- */
25
- function handleJumpOut(mathField: MathFieldInterface, key: Key): void {
26
- const cursor = mathField.cursor();
27
- const context = getCursorContext(mathField);
28
-
29
- // Validate that the current cursor context matches the key's intent.
30
- if (KeysForJumpContext[context] !== key) {
31
- // If we don't have a valid cursor context, yet the user was able
32
- // to trigger a jump-out key, that's a broken invariant. Rather
33
- // than throw an error (which would kick the user out of the
34
- // exercise), we do nothing, as a fallback strategy. The user can
35
- // still move the cursor manually.
36
- return;
37
- }
38
-
39
- switch (context) {
40
- case CursorContext.IN_PARENS:
41
- // Insert at the end of the parentheses, and then navigate right
42
- // once more to get 'beyond' the parentheses.
43
- cursor.insRightOf(cursor.parent.parent);
44
- mathField.keystroke("Right");
45
- break;
46
-
47
- case CursorContext.BEFORE_FRACTION:
48
- // Find the nearest fraction to the right of the cursor.
49
- let fractionNode;
50
- let visitor = cursor;
51
- while (
52
- visitor[mathQuillInstance.R] !== MathFieldActionType.MQ_END
53
- ) {
54
- if (isFraction(visitor[mathQuillInstance.R])) {
55
- fractionNode = visitor[mathQuillInstance.R];
56
- }
57
- visitor = visitor[mathQuillInstance.R];
58
- }
59
-
60
- // Jump into it!
61
- cursor.insLeftOf(fractionNode);
62
- mathField.keystroke("Right");
63
- break;
64
-
65
- case CursorContext.IN_NUMERATOR:
66
- // HACK(charlie): I can't find a better way to do this. The goal
67
- // is to place the cursor at the start of the matching
68
- // denominator. So, we identify the appropriate node, and
69
- // continue rightwards until we find ourselves inside of it.
70
- // It's possible that there are cases in which we don't reach
71
- // the denominator, though I can't think of any.
72
- const siblingDenominator = cursor.parent.parent.blocks[1];
73
- while (cursor.parent !== siblingDenominator) {
74
- mathField.keystroke("Right");
75
- }
76
- break;
77
-
78
- case CursorContext.IN_DENOMINATOR:
79
- cursor.insRightOf(cursor.parent.parent);
80
- break;
81
-
82
- case CursorContext.IN_SUB_SCRIPT:
83
- // Insert just beyond the superscript.
84
- cursor.insRightOf(cursor.parent.parent);
85
-
86
- // Navigate right once more, if we're right before parens. This
87
- // is to handle the standard case in which the subscript is the
88
- // base of a custom log.
89
- if (isParens(cursor[mathQuillInstance.R])) {
90
- mathField.keystroke("Right");
91
- }
92
- break;
93
-
94
- case CursorContext.IN_SUPER_SCRIPT:
95
- // Insert just beyond the superscript.
96
- cursor.insRightOf(cursor.parent.parent);
97
- break;
98
-
99
- default:
100
- throw new Error(
101
- `Attempted to 'Jump Out' from node, but found no ` +
102
- `appropriate cursor context: ${context}`,
103
- );
104
- }
105
- }
106
-
107
- export default handleJumpOut;