@pie-lib/math-input-mathlive 1.1.5-beta.9 → 2.0.1-next.20261109072207

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 (195) hide show
  1. package/README.md +84 -109
  2. package/dist/_virtual/_rolldown/runtime.js +11 -0
  3. package/dist/horizontal-keypad.d.ts +22 -0
  4. package/dist/horizontal-keypad.js +54 -0
  5. package/dist/index.d.ts +20 -0
  6. package/dist/index.js +12 -0
  7. package/dist/keypad/index.d.ts +40 -0
  8. package/dist/keypad/index.js +245 -0
  9. package/dist/keypad/keys-layout.d.ts +7 -0
  10. package/dist/keypad/keys-layout.js +5 -0
  11. package/dist/keys/basic-operators.d.ts +5 -0
  12. package/dist/keys/basic-operators.js +30 -0
  13. package/dist/keys/chars.d.ts +5 -0
  14. package/dist/keys/comparison.d.ts +4 -0
  15. package/dist/keys/comparison.js +32 -0
  16. package/dist/keys/constants.d.ts +4 -0
  17. package/dist/keys/constants.js +35 -0
  18. package/dist/keys/digits.d.ts +15 -0
  19. package/dist/keys/digits.js +34 -0
  20. package/dist/keys/edit.d.ts +6 -0
  21. package/dist/keys/edit.js +9 -0
  22. package/dist/keys/exponent.d.ts +4 -0
  23. package/dist/keys/exponent.js +28 -0
  24. package/dist/keys/fractions.d.ts +3 -0
  25. package/dist/keys/fractions.js +27 -0
  26. package/dist/keys/geometry.d.ts +23 -0
  27. package/dist/keys/geometry.js +127 -0
  28. package/dist/keys/grades.d.ts +9 -0
  29. package/dist/keys/grades.js +414 -0
  30. package/dist/keys/index.d.ts +6 -0
  31. package/dist/keys/index.js +50 -0
  32. package/dist/keys/log.d.ts +3 -0
  33. package/dist/keys/log.js +25 -0
  34. package/dist/keys/logic.d.ts +2 -0
  35. package/dist/keys/logic.js +13 -0
  36. package/dist/keys/matrices.d.ts +2 -0
  37. package/dist/keys/matrices.js +17 -0
  38. package/dist/keys/misc.d.ts +10 -0
  39. package/dist/keys/misc.js +60 -0
  40. package/dist/keys/navigation.d.ts +2 -0
  41. package/dist/keys/navigation.js +13 -0
  42. package/dist/keys/operators.d.ts +1 -0
  43. package/dist/keys/operators.js +11 -0
  44. package/dist/keys/statistics.d.ts +5 -0
  45. package/dist/keys/statistics.js +38 -0
  46. package/dist/keys/sub-sup.d.ts +2 -0
  47. package/dist/keys/sub-sup.js +17 -0
  48. package/dist/keys/trigonometry.d.ts +6 -0
  49. package/dist/keys/trigonometry.js +43 -0
  50. package/dist/keys/utils.d.ts +5 -0
  51. package/dist/keys/utils.js +24 -0
  52. package/dist/keys/vars.d.ts +3 -0
  53. package/dist/keys/vars.js +22 -0
  54. package/dist/latex-bridge.d.ts +197 -0
  55. package/dist/latex-bridge.js +210 -0
  56. package/dist/longdiv.d.ts +24 -0
  57. package/dist/longdiv.js +43 -0
  58. package/dist/math-input.d.ts +26 -0
  59. package/dist/math-input.js +74 -0
  60. package/dist/mathlive-instance.d.ts +221 -0
  61. package/dist/mathlive-instance.js +159 -0
  62. package/dist/mf/common-styles.d.ts +594 -0
  63. package/dist/mf/common-styles.js +80 -0
  64. package/dist/mf/field-label.d.ts +16 -0
  65. package/dist/mf/field-label.js +13 -0
  66. package/dist/mf/index.d.ts +4 -0
  67. package/dist/mf/index.js +21 -0
  68. package/dist/mf/input.d.ts +94 -0
  69. package/dist/mf/input.js +127 -0
  70. package/dist/mf/static.d.ts +143 -0
  71. package/dist/mf/static.js +230 -0
  72. package/dist/selection.d.ts +43 -0
  73. package/dist/selection.js +39 -0
  74. package/package.json +32 -18
  75. package/lib/__tests__/composites.test.js +0 -255
  76. package/lib/__tests__/drop-in-compat.test.js +0 -133
  77. package/lib/__tests__/embedded-fonts.test.js +0 -121
  78. package/lib/__tests__/index.test.js +0 -118
  79. package/lib/__tests__/input-imperative.test.js +0 -155
  80. package/lib/__tests__/keypad-label.test.js +0 -231
  81. package/lib/__tests__/keypad-render.test.js +0 -206
  82. package/lib/__tests__/latex-bridge.test.js +0 -386
  83. package/lib/__tests__/mathlive-instance.test.js +0 -419
  84. package/lib/__tests__/mount-race.test.js +0 -218
  85. package/lib/__tests__/static-prompts.test.js +0 -405
  86. package/lib/__tests__/tmp-markup.test.js +0 -26
  87. package/lib/horizontal-keypad.js +0 -101
  88. package/lib/horizontal-keypad.js.map +0 -1
  89. package/lib/index.js +0 -143
  90. package/lib/index.js.map +0 -1
  91. package/lib/keypad/index.js +0 -431
  92. package/lib/keypad/index.js.map +0 -1
  93. package/lib/keypad/keys-layout.js +0 -22
  94. package/lib/keypad/keys-layout.js.map +0 -1
  95. package/lib/keys/basic-operators.js +0 -33
  96. package/lib/keys/basic-operators.js.map +0 -1
  97. package/lib/keys/chars.js +0 -12
  98. package/lib/keys/chars.js.map +0 -1
  99. package/lib/keys/comparison.js +0 -39
  100. package/lib/keys/comparison.js.map +0 -1
  101. package/lib/keys/constants.js +0 -37
  102. package/lib/keys/constants.js.map +0 -1
  103. package/lib/keys/digits.js +0 -46
  104. package/lib/keys/digits.js.map +0 -1
  105. package/lib/keys/edit.js +0 -14
  106. package/lib/keys/edit.js.map +0 -1
  107. package/lib/keys/exponent.js +0 -30
  108. package/lib/keys/exponent.js.map +0 -1
  109. package/lib/keys/fractions.js +0 -29
  110. package/lib/keys/fractions.js.map +0 -1
  111. package/lib/keys/geometry.js +0 -143
  112. package/lib/keys/geometry.js.map +0 -1
  113. package/lib/keys/grades.js +0 -259
  114. package/lib/keys/grades.js.map +0 -1
  115. package/lib/keys/index.js +0 -35
  116. package/lib/keys/index.js.map +0 -1
  117. package/lib/keys/log.js +0 -27
  118. package/lib/keys/log.js.map +0 -1
  119. package/lib/keys/logic.js +0 -19
  120. package/lib/keys/logic.js.map +0 -1
  121. package/lib/keys/matrices.js +0 -19
  122. package/lib/keys/matrices.js.map +0 -1
  123. package/lib/keys/misc.js +0 -69
  124. package/lib/keys/misc.js.map +0 -1
  125. package/lib/keys/navigation.js +0 -20
  126. package/lib/keys/navigation.js.map +0 -1
  127. package/lib/keys/operators.js +0 -15
  128. package/lib/keys/operators.js.map +0 -1
  129. package/lib/keys/statistics.js +0 -40
  130. package/lib/keys/statistics.js.map +0 -1
  131. package/lib/keys/sub-sup.js +0 -19
  132. package/lib/keys/sub-sup.js.map +0 -1
  133. package/lib/keys/trigonometry.js +0 -45
  134. package/lib/keys/trigonometry.js.map +0 -1
  135. package/lib/keys/utils.js +0 -87
  136. package/lib/keys/utils.js.map +0 -1
  137. package/lib/keys/vars.js +0 -24
  138. package/lib/keys/vars.js.map +0 -1
  139. package/lib/latex-bridge.js +0 -508
  140. package/lib/latex-bridge.js.map +0 -1
  141. package/lib/math-input.js +0 -143
  142. package/lib/math-input.js.map +0 -1
  143. package/lib/mathlive-instance.js +0 -585
  144. package/lib/mathlive-instance.js.map +0 -1
  145. package/lib/mf/common-styles.js +0 -203
  146. package/lib/mf/common-styles.js.map +0 -1
  147. package/lib/mf/index.js +0 -84
  148. package/lib/mf/index.js.map +0 -1
  149. package/lib/mf/input.js +0 -364
  150. package/lib/mf/input.js.map +0 -1
  151. package/lib/mf/static.js +0 -500
  152. package/lib/mf/static.js.map +0 -1
  153. package/src/__tests__/composites.test.jsx +0 -220
  154. package/src/__tests__/drop-in-compat.test.jsx +0 -112
  155. package/src/__tests__/index.test.jsx +0 -109
  156. package/src/__tests__/input-imperative.test.jsx +0 -144
  157. package/src/__tests__/keypad-label.test.jsx +0 -283
  158. package/src/__tests__/keypad-render.test.jsx +0 -123
  159. package/src/__tests__/latex-bridge.test.js +0 -389
  160. package/src/__tests__/mathlive-instance.test.js +0 -418
  161. package/src/__tests__/mount-race.test.jsx +0 -142
  162. package/src/__tests__/static-prompts.test.jsx +0 -364
  163. package/src/horizontal-keypad.jsx +0 -73
  164. package/src/index.jsx +0 -63
  165. package/src/keypad/index.jsx +0 -386
  166. package/src/keypad/keys-layout.js +0 -16
  167. package/src/keys/basic-operators.js +0 -32
  168. package/src/keys/chars.js +0 -5
  169. package/src/keys/comparison.js +0 -28
  170. package/src/keys/constants.js +0 -35
  171. package/src/keys/digits.js +0 -40
  172. package/src/keys/edit.js +0 -3
  173. package/src/keys/exponent.js +0 -28
  174. package/src/keys/fractions.js +0 -26
  175. package/src/keys/geometry.js +0 -149
  176. package/src/keys/grades.js +0 -367
  177. package/src/keys/index.js +0 -20
  178. package/src/keys/log.js +0 -22
  179. package/src/keys/logic.js +0 -15
  180. package/src/keys/matrices.js +0 -15
  181. package/src/keys/misc.js +0 -71
  182. package/src/keys/navigation.js +0 -8
  183. package/src/keys/operators.js +0 -10
  184. package/src/keys/statistics.js +0 -38
  185. package/src/keys/sub-sup.js +0 -15
  186. package/src/keys/trigonometry.js +0 -15
  187. package/src/keys/utils.js +0 -66
  188. package/src/keys/vars.js +0 -19
  189. package/src/latex-bridge.js +0 -524
  190. package/src/math-input.jsx +0 -115
  191. package/src/mathlive-instance.js +0 -582
  192. package/src/mf/common-styles.js +0 -210
  193. package/src/mf/index.js +0 -30
  194. package/src/mf/input.jsx +0 -319
  195. package/src/mf/static.jsx +0 -479
@@ -1,210 +0,0 @@
1
- /**
2
- * Shared MathLive styling.
3
- *
4
- * Replaces @pie-lib/math-input's `common-mq-styles.js`. That file was built
5
- * around MathQuill's `.mq-*` class names, none of which exist in MathLive
6
- * output, so the selectors here target MathLive's own structure instead.
7
- *
8
- * The four named groups are kept because consumers destructure them:
9
- *
10
- * const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles }
11
- * = mq.CommonMqStyles; // math-toolbar/src/editor-and-pad.jsx, math-preview.jsx
12
- *
13
- * Most are intentionally near-empty: MathLive sizes scripts, radicals and
14
- * fences correctly on its own, so the MathQuill-era corrections are not needed.
15
- * They stay as documented extension points rather than being deleted, so the
16
- * destructuring above keeps working. The exception is the radical index, which
17
- * MathLive sizes to TeX metrics that read too small here - see
18
- * `rootIndexStyles`.
19
- */
20
-
21
- /**
22
- * Font styling. MathQuill needed explicit MathJax font families to match the
23
- * static renderer; MathLive ships the KaTeX fonts it renders with, so this only
24
- * smooths rendering and lets the host override the family.
25
- */
26
- export const commonMqFontStyles = {
27
- '-webkit-font-smoothing': 'antialiased',
28
- '& math-field': {
29
- fontFamily: 'inherit',
30
- },
31
- };
32
-
33
- /**
34
- * Long division. The MathQuill fork drew this with nested `.mq-longdiv-*`
35
- * elements that needed manual offsets; here it comes from the `\longdiv` macro
36
- * (see `PIE_MACROS`), tagged with `data-pie-macro="longdiv"`.
37
- */
38
- export const longdivStyles = {
39
- '& [data-pie-macro="longdiv"]': {
40
- display: 'inline-flex',
41
- },
42
- };
43
-
44
- /**
45
- * How the index of a radical is sized.
46
- *
47
- * MathLive follows TeX and sets the index of `\sqrt[n]{x}` in scriptscriptstyle,
48
- * which it emits as an inline `font-size: 50%` on the span wrapping the index's
49
- * glyphs. At authoring sizes that leaves the index - on the nth-root key, and in
50
- * any radical the author types - too small to read next to the radicand, so it is
51
- * scaled up to 80% - 1.6x what MathLive asks for.
52
- *
53
- * Only that innermost span is resized. `.ML__sqrt-index` itself keeps its font
54
- * size on purpose: the vlist inside it tucks the index into the radical's crook
55
- * with em-based `top`/`height` offsets, and scaling those lifts the index clear
56
- * of the radical instead of just enlarging it.
57
- *
58
- * The child chain is spelled out so that nothing nested deeper inside the index
59
- * (a fraction's numerator, another radical) is caught by the same rule, and
60
- * `!important` is required because the 50% is an INLINE style.
61
- */
62
- const ROOT_INDEX_SELECTOR = '.ML__sqrt-index > .ML__vlist-t > .ML__vlist-r > .ML__vlist > span > [style*="font-size"]';
63
- const ROOT_INDEX_FONT_SIZE = '80%';
64
-
65
- export const rootIndexStyles = {
66
- [`& ${ROOT_INDEX_SELECTOR}`]: {
67
- fontSize: `${ROOT_INDEX_FONT_SIZE} !important`,
68
- },
69
- };
70
-
71
- /**
72
- * The same rule as a CSS string, for a live mathfield's shadow root - see
73
- * `SHADOW_CSS` in mathlive-instance.js. Static markup is styled by
74
- * {@link rootIndexStyles}.
75
- */
76
- export const ROOT_INDEX_CSS = `${ROOT_INDEX_SELECTOR}{font-size:${ROOT_INDEX_FONT_SIZE} !important}`;
77
-
78
- /**
79
- * Size of the parallel sign (`\parallel`, tagged `data-pie-macro="parallel"` by
80
- * `PIE_MACROS`). The legacy editor enlarged it (`updateSpans` set 32px) because
81
- * the bars read too small next to the letters of `AB ∥ CD`; MathLive draws it at
82
- * the surrounding size, so it is scaled up here. Not applied to keypad labels.
83
- */
84
- const PARALLEL_SELECTOR = '[data-pie-macro="parallel"]';
85
- const PARALLEL_FONT_SIZE = '1.4em';
86
-
87
- export const parallelStyles = {
88
- [`& ${PARALLEL_SELECTOR}`]: {
89
- fontSize: PARALLEL_FONT_SIZE,
90
- },
91
- };
92
-
93
- /** The same rule as a CSS string, for a live mathfield's shadow root. */
94
- export const PARALLEL_CSS = `${PARALLEL_SELECTOR}{font-size:${PARALLEL_FONT_SIZE}}`;
95
-
96
- /**
97
- * Superscript/subscript sizing. MathLive follows TeX metrics for scripts, so the
98
- * MathQuill-era corrections are gone; the radical index is the one place where a
99
- * TeX-correct size reads too small in the authoring UI.
100
- */
101
- export const supsubStyles = {
102
- ...rootIndexStyles,
103
- ...parallelStyles,
104
- };
105
-
106
- /** Empty placeholder boxes (answer blocks / open slots). */
107
- export const placeholderStyles = {
108
- '& .ML__placeholder': {
109
- borderBottom: '1px solid currentColor',
110
- minWidth: '1em',
111
- },
112
- // Stand-in glyph for an empty argument slot in STATIC math (see
113
- // `withVisibleEmptySlots`). Muted so it reads as "a slot goes here" rather
114
- // than as part of the symbol, matching how MathQuill shaded `.mq-empty`.
115
- '& [data-pie-empty]': {
116
- opacity: 0.5,
117
- },
118
- // Stretchy accents (\overarc, \overleftrightarrow, \overrightarrow) render an
119
- // <svg width="100%" preserveAspectRatio="none"> inside a `.ML__stretchy`. That
120
- // span is absolutely positioned, so its immediate wrapper computes to 0px and
121
- // the width has to resolve against a *positioned* ancestor. Pin that to
122
- // `.ML__base`, whose width already equals the accent's content (measured:
123
- // 9.85px for a 0.56em slot), so the arc spans exactly its content.
124
- //
125
- // Do NOT write a bare `& svg` rule instead: MathLive's own rule is
126
- // descendant-scoped (`.ML__latex .ML__stretchy svg`), and a global one
127
- // re-parents every svg in the subtree.
128
- '& .ML__base': {
129
- position: 'relative',
130
- },
131
- '& .ML__stretchy': {
132
- position: 'absolute',
133
- left: 0,
134
- width: '100%',
135
- },
136
- // MathLive centres an accent by shifting it right half the content width -
137
- // `margin-left: 0.28em` for a 0.56em slot - which assumes a glyph with its own
138
- // side bearings. Over a synthetic empty slot it just pushes the arc right.
139
- //
140
- // `!important` is required: MathLive writes this as an INLINE style
141
- // (`style="top:-3.4em;margin-left:0.28em"`), which a class cannot override.
142
- //
143
- // It also fixes the width. `.ML__center` is the stretchy's containing block
144
- // (MathLive gives `.ML__vlist > span` position:relative), and its width is the
145
- // base minus that margin - measured 9.85px - 4.93px = 4.93px, exactly half,
146
- // which is why the arc came out half-width AND offset right.
147
- //
148
- // Scoped with `:has(.ML__stretchy)`: only stretchy accents are affected.
149
- // A single-glyph accent (`\hat{x}`, `\vec{x}`) genuinely needs its centring
150
- // offset, and must keep it.
151
- '& .ML__center:has(.ML__stretchy)': {
152
- marginLeft: '0 !important',
153
- },
154
- };
155
-
156
- /**
157
- * Styling for the on-screen keyboard and any container rendering math labels.
158
- * Also hides MathLive's own affordances, since pie supplies its own keypad.
159
- */
160
- export const commonMqKeyboardStyles = {
161
- ...commonMqFontStyles,
162
- ...longdivStyles,
163
- ...rootIndexStyles,
164
- ...placeholderStyles,
165
- touchAction: 'manipulation',
166
- '& math-field': {
167
- fontSize: 'inherit',
168
- outline: 'none',
169
- },
170
- '& math-field::part(virtual-keyboard-toggle)': {
171
- display: 'none',
172
- },
173
- '& math-field::part(menu-toggle)': {
174
- display: 'none',
175
- },
176
- };
177
-
178
- /** Everything combined - the common case for a container of math. */
179
- export const commonMathLiveStyles = {
180
- ...commonMqFontStyles,
181
- ...longdivStyles,
182
- ...supsubStyles,
183
- ...placeholderStyles,
184
- '& math-field': {
185
- fontSize: 'inherit',
186
- outline: 'none',
187
- },
188
- '& math-field::part(virtual-keyboard-toggle)': {
189
- display: 'none',
190
- },
191
- '& math-field::part(menu-toggle)': {
192
- display: 'none',
193
- },
194
- };
195
-
196
- export const commonKeyboardStyles = commonMqKeyboardStyles;
197
-
198
- /**
199
- * Default export mirrors @pie-lib/math-input's `CommonMqStyles` object so
200
- * existing destructuring keeps working.
201
- */
202
- export default {
203
- commonMqFontStyles,
204
- longdivStyles,
205
- rootIndexStyles,
206
- supsubStyles,
207
- commonMqKeyboardStyles,
208
- placeholderStyles,
209
- commonMathLiveStyles,
210
- };
package/src/mf/index.js DELETED
@@ -1,30 +0,0 @@
1
- import Input from './input';
2
- import Static from './static';
3
- import CommonMqStyles, {
4
- commonMathLiveStyles,
5
- commonKeyboardStyles,
6
- commonMqFontStyles,
7
- commonMqKeyboardStyles,
8
- longdivStyles,
9
- rootIndexStyles,
10
- parallelStyles,
11
- supsubStyles,
12
- placeholderStyles,
13
- } from './common-styles';
14
-
15
- export {
16
- Input,
17
- Static,
18
- // Same object shape as @pie-lib/math-input's `mq.CommonMqStyles`, so
19
- // `const { commonMqFontStyles, ... } = mq.CommonMqStyles` keeps working.
20
- CommonMqStyles,
21
- commonMathLiveStyles,
22
- commonKeyboardStyles,
23
- commonMqFontStyles,
24
- commonMqKeyboardStyles,
25
- longdivStyles,
26
- rootIndexStyles,
27
- parallelStyles,
28
- supsubStyles,
29
- placeholderStyles,
30
- };
package/src/mf/input.jsx DELETED
@@ -1,319 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { styled } from '@mui/material/styles';
4
- import debug from 'debug';
5
- import {
6
- loadMathLive,
7
- getMacros,
8
- MATH_MODE_SPACE,
9
- applyShadowStyles,
10
- trackPlaceholderCaret,
11
- } from '../mathlive-instance';
12
- import { toMathLive, fromMathLive, keyToAction, KEYSTROKES } from '../latex-bridge';
13
-
14
- const log = debug('pie-lib:math-input-mathlive:input');
15
-
16
- const Holder = styled('span')({
17
- display: 'inline-block',
18
- '& math-field': {
19
- display: 'block',
20
- width: '100%',
21
- },
22
- // MathLive ships its own virtual keyboard toggle; pie supplies its own keypad.
23
- '& math-field::part(virtual-keyboard-toggle)': {
24
- display: 'none',
25
- },
26
- '& math-field::part(menu-toggle)': {
27
- display: 'none',
28
- },
29
- });
30
-
31
- /**
32
- * Editable math field. Drop-in replacement for @pie-lib/math-input's `mq.Input`.
33
- *
34
- * Keeps the same imperative surface used by MathInput's keypad
35
- * (`write`, `command`, `keystroke`, `clear`, `focus`, `blur`) so callers do not
36
- * change, and the same `latex` / `onChange` props.
37
- */
38
- export class Input extends React.Component {
39
- static propTypes = {
40
- className: PropTypes.string,
41
- latex: PropTypes.string,
42
- onChange: PropTypes.func,
43
- onClick: PropTypes.func,
44
- onFocus: PropTypes.func,
45
- onBlur: PropTypes.func,
46
- readOnly: PropTypes.bool,
47
- innerRef: PropTypes.func,
48
- };
49
-
50
- constructor(props) {
51
- super(props);
52
- this.holderRef = React.createRef();
53
- // Tracks the last latex we emitted, so prop echoes don't reset the cursor.
54
- this.lastEmitted = undefined;
55
- }
56
-
57
- async componentDidMount() {
58
- // MathLive loads asynchronously, so this method resumes after an await and
59
- // has to know whether it is still the current mount. React StrictMode calls
60
- // mount -> unmount -> mount on the SAME instance in dev, so a boolean
61
- // "unmounted" flag would latch and block the remount from ever creating a
62
- // field. A generation counter identifies the live mount instead: only the
63
- // newest one proceeds, and a real unmount invalidates all pending ones.
64
- const generation = (this.mountGeneration = (this.mountGeneration || 0) + 1);
65
- const ml = await loadMathLive();
66
-
67
- if (generation !== this.mountGeneration || !ml || !this.holderRef.current || this.mathField) {
68
- return;
69
- }
70
-
71
- this.mathField = new ml.MathfieldElement({ macros: getMacros() });
72
- this.mathField.value = toMathLive(this.props.latex || '');
73
-
74
- if (this.props.readOnly) {
75
- this.mathField.readOnly = true;
76
- }
77
-
78
- // pie drives input through its own keypad, so suppress MathLive's.
79
- this.mathField.mathVirtualKeyboardPolicy = 'manual';
80
- // Without this the spacebar does nothing: MathLive's mathModeSpace
81
- // defaults to an empty string.
82
- this.mathField.mathModeSpace = MATH_MODE_SPACE;
83
- // After the exponent key the cursor must stay in the exponent until it is moved
84
- // MathLive's `smartSuperscript` (on by
85
- // default) jumps out of an empty superscript after a single digit, which breaks
86
- // typing x^23 or 10^6 and is inconsistent with non-digit exponents.
87
- this.mathField.smartSuperscript = false;
88
-
89
- // The mathfield renders in shadow DOM, so page CSS cannot reach it - the
90
- // stretchy-accent fix has to be injected there directly.
91
- applyShadowStyles(this.mathField);
92
-
93
- // A selected `\placeholder{}` gets no MathLive caret; draw one.
94
- this.untrackCaret = trackPlaceholderCaret(this.mathField);
95
-
96
- this.mathField.addEventListener('input', this.onInputEdit);
97
-
98
- // Belt and braces: drop anything a previous mount may have left behind.
99
- this.holderRef.current.replaceChildren(this.mathField);
100
-
101
- if (this.props.innerRef) {
102
- this.props.innerRef(this);
103
- }
104
- }
105
-
106
- componentDidUpdate(prevProps) {
107
- if (!this.mathField) {
108
- return;
109
- }
110
-
111
- const { latex, readOnly } = this.props;
112
-
113
- if (readOnly !== prevProps.readOnly) {
114
- this.mathField.readOnly = !!readOnly;
115
- }
116
-
117
- // Only push the prop back into the field when it genuinely differs from
118
- // what the field already holds. This is the analogue of the old
119
- // shouldComponentUpdate latex-equality guard and prevents cursor thrash.
120
- if (latex !== undefined && latex !== null && latex !== this.lastEmitted) {
121
- const next = toMathLive(latex);
122
-
123
- if (next !== this.mathField.value) {
124
- this.mathField.value = next;
125
- }
126
- }
127
- }
128
-
129
- componentWillUnmount() {
130
- // Invalidate any mount still awaiting the MathLive load, so it does not
131
- // append a field into a holder that is going away.
132
- this.mountGeneration = (this.mountGeneration || 0) + 1;
133
-
134
- if (this.untrackCaret) {
135
- this.untrackCaret();
136
- this.untrackCaret = undefined;
137
- }
138
-
139
- if (this.mathField) {
140
- this.mathField.removeEventListener('input', this.onInputEdit);
141
- this.mathField.remove();
142
- this.mathField = undefined;
143
- }
144
-
145
- if (this.props.innerRef) {
146
- this.props.innerRef(null);
147
- }
148
- }
149
-
150
- onInputEdit = () => {
151
- const { onChange } = this.props;
152
-
153
- if (!this.mathField) {
154
- return;
155
- }
156
-
157
- const latex = fromMathLive(this.mathField.getValue('latex'));
158
-
159
- this.lastEmitted = latex;
160
-
161
- if (onChange) {
162
- onChange(latex);
163
- }
164
- };
165
-
166
- /** Current latex, in stored (MathQuill-compatible) form. */
167
- latex() {
168
- return this.mathField ? fromMathLive(this.mathField.getValue('latex')) : '';
169
- }
170
-
171
- /**
172
- * The underlying `<math-field>` element, or undefined until MathLive has
173
- * finished loading. Exposed for callers that need the real element.
174
- *
175
- * NOTE: this is NOT a MathQuill field. Code written against
176
- * `@pie-lib/math-input` often did `input.mathField.latex()`; that shape is
177
- * provided by `mqCompat` below, and `latex()` on this component is the
178
- * supported replacement.
179
- */
180
- get element() {
181
- return this.mathField;
182
- }
183
-
184
- /**
185
- * MathQuill-shaped facade, so existing `input.mathField.latex()` /
186
- * `.el()` call sites keep working instead of throwing. Only the members that
187
- * consumers actually used are provided; MathQuill internals such as
188
- * `__controller` have no MathLive equivalent and are deliberately absent.
189
- */
190
- get mqCompat() {
191
- return {
192
- latex: (v) => (v === undefined ? this.latex() : (this.setLatex(v), v)),
193
- el: () => this.mathField,
194
- focus: () => this.focus(),
195
- blur: () => this.blur(),
196
- };
197
- }
198
-
199
- /** Programmatically replace the content. */
200
- setLatex(v) {
201
- if (this.mathField) {
202
- this.mathField.value = toMathLive(v || '');
203
- }
204
- }
205
-
206
- clear() {
207
- if (this.mathField) {
208
- this.mathField.value = '';
209
- }
210
-
211
- return '';
212
- }
213
-
214
- blur() {
215
- log('blur mathfield');
216
- this.mathField && this.mathField.blur();
217
- }
218
-
219
- focus() {
220
- log('focus mathfield...');
221
- this.mathField && this.mathField.focus();
222
- }
223
-
224
- /**
225
- * MathQuill `cmd()` equivalent.
226
- *
227
- * Callers pass MathQuill command names (`'\\frac'`, `'\\sqrt'`, `'^'`, or an
228
- * array of them). Those must be expanded into latex with `#?` placeholders,
229
- * otherwise inserting a bare `\frac` produces no editable slots.
230
- */
231
- command(v) {
232
- log('command: ', v);
233
-
234
- if (!this.mathField) {
235
- return '';
236
- }
237
-
238
- const action = keyToAction({ command: v });
239
-
240
- if (action) {
241
- this.mathField.insert(action.value, { focus: true });
242
- }
243
-
244
- this.mathField.focus();
245
-
246
- return this.latex();
247
- }
248
-
249
- /**
250
- * MathQuill `keystroke()` equivalent.
251
- *
252
- * Accepts MathQuill keystroke names (`'Left'`, `'Right'`, `'Backspace'`) as
253
- * the keypad emits them, and passes anything else through as a MathLive
254
- * selector.
255
- */
256
- keystroke(v) {
257
- log('keystroke: ', v);
258
-
259
- if (!this.mathField) {
260
- return '';
261
- }
262
-
263
- const selector = KEYSTROKES[v] || v;
264
-
265
- try {
266
- this.mathField.executeCommand(selector);
267
- } catch (e) {
268
- log('unknown keystroke/selector "%s": %s', v, e && e.message);
269
- }
270
-
271
- this.mathField.focus();
272
-
273
- return this.latex();
274
- }
275
-
276
- /** MathQuill `write()` equivalent. Accepts an array, as `cmd()` did. */
277
- write(v) {
278
- log('write: ', v);
279
-
280
- if (!this.mathField) {
281
- return '';
282
- }
283
-
284
- const values = Array.isArray(v) ? v : [v];
285
-
286
- values.forEach((vv) => this.mathField.insert(vv, { focus: true }));
287
- this.mathField.focus();
288
-
289
- return this.latex();
290
- }
291
-
292
- /**
293
- * Enter must not insert a line break: math input is single-expression, and
294
- * the MathQuill version swallowed charCode 13 for the same reason. Newlines
295
- * are authored explicitly through the newLine embed, not the Enter key.
296
- */
297
- onKeyDown = (event) => {
298
- if (event.key === 'Enter') {
299
- event.preventDefault();
300
- }
301
- };
302
-
303
- render() {
304
- const { className, onFocus, onBlur, onClick } = this.props;
305
-
306
- return (
307
- <Holder
308
- className={className}
309
- onFocus={onFocus}
310
- onBlur={onBlur}
311
- onClick={onClick}
312
- onKeyDown={this.onKeyDown}
313
- ref={this.holderRef}
314
- />
315
- );
316
- }
317
- }
318
-
319
- export default Input;