@pie-lib/math-input-mathlive 1.0.0 → 1.0.2

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.
@@ -22,7 +22,10 @@ const log = debug('pie-lib:math-input-mathlive:keypad');
22
22
  * `convertLatexToMarkup`, so a 40-key keypad no longer creates 40 live math
23
23
  * instances.
24
24
  */
25
- const LabelHolder = styled('span')({
25
+ // The `color.*()` helpers must be called lazily, inside the styled callback.
26
+ // Evaluating them in a plain object literal runs them at module load, which
27
+ // breaks any consumer that mocks a subset of @pie-lib/render-ui.
28
+ const LabelHolder = styled('span')(() => ({
26
29
  pointerEvents: 'none',
27
30
  textTransform: 'none',
28
31
  color: color.text(),
@@ -32,9 +35,9 @@ const LabelHolder = styled('span')({
32
35
  lineHeight: 1,
33
36
  // Answer-block / empty-slot boxes inside a label.
34
37
  '& .ML__placeholder': {
35
- backgroundColor: color.keypadEmptyPlaceholder(),
38
+ backgroundColor: color.keypadEmptyPlaceholder ? color.keypadEmptyPlaceholder() : undefined,
36
39
  },
37
- });
40
+ }));
38
41
 
39
42
  export class LatexLabel extends React.Component {
40
43
  static propTypes = {
@@ -238,10 +241,7 @@ export class KeyPad extends React.Component {
238
241
  keyIsNotAllowed = (key) => {
239
242
  const { noDecimal } = this.props;
240
243
 
241
- return !!(
242
- ((key.write === '.' && key.label === '.') || (key.write === ',' && key.label === ',')) &&
243
- noDecimal
244
- );
244
+ return !!(((key.write === '.' && key.label === '.') || (key.write === ',' && key.label === ',')) && noDecimal);
245
245
  };
246
246
 
247
247
  render() {
package/src/mf/input.jsx CHANGED
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
3
3
  import { styled } from '@mui/material/styles';
4
4
  import debug from 'debug';
5
5
  import { loadMathLive, getMacros } from '../mathlive-instance';
6
- import { toMathLive, fromMathLive } from '../latex-bridge';
6
+ import { toMathLive, fromMathLive, keyToAction, KEYSTROKES } from '../latex-bridge';
7
7
 
8
8
  const log = debug('pie-lib:math-input-mathlive:input');
9
9
 
@@ -131,6 +131,41 @@ export class Input extends React.Component {
131
131
  return this.mathField ? fromMathLive(this.mathField.getValue('latex')) : '';
132
132
  }
133
133
 
134
+ /**
135
+ * The underlying `<math-field>` element, or undefined until MathLive has
136
+ * finished loading. Exposed for callers that need the real element.
137
+ *
138
+ * NOTE: this is NOT a MathQuill field. Code written against
139
+ * `@pie-lib/math-input` often did `input.mathField.latex()`; that shape is
140
+ * provided by `mqCompat` below, and `latex()` on this component is the
141
+ * supported replacement.
142
+ */
143
+ get element() {
144
+ return this.mathField;
145
+ }
146
+
147
+ /**
148
+ * MathQuill-shaped facade, so existing `input.mathField.latex()` /
149
+ * `.el()` call sites keep working instead of throwing. Only the members that
150
+ * consumers actually used are provided; MathQuill internals such as
151
+ * `__controller` have no MathLive equivalent and are deliberately absent.
152
+ */
153
+ get mqCompat() {
154
+ return {
155
+ latex: (v) => (v === undefined ? this.latex() : (this.setLatex(v), v)),
156
+ el: () => this.mathField,
157
+ focus: () => this.focus(),
158
+ blur: () => this.blur(),
159
+ };
160
+ }
161
+
162
+ /** Programmatically replace the content. */
163
+ setLatex(v) {
164
+ if (this.mathField) {
165
+ this.mathField.value = toMathLive(v || '');
166
+ }
167
+ }
168
+
134
169
  clear() {
135
170
  if (this.mathField) {
136
171
  this.mathField.value = '';
@@ -149,7 +184,13 @@ export class Input extends React.Component {
149
184
  this.mathField && this.mathField.focus();
150
185
  }
151
186
 
152
- /** MathQuill `cmd()` equivalent. */
187
+ /**
188
+ * MathQuill `cmd()` equivalent.
189
+ *
190
+ * Callers pass MathQuill command names (`'\\frac'`, `'\\sqrt'`, `'^'`, or an
191
+ * array of them). Those must be expanded into latex with `#?` placeholders,
192
+ * otherwise inserting a bare `\frac` produces no editable slots.
193
+ */
153
194
  command(v) {
154
195
  log('command: ', v);
155
196
 
@@ -157,21 +198,39 @@ export class Input extends React.Component {
157
198
  return '';
158
199
  }
159
200
 
160
- const values = Array.isArray(v) ? v : [v];
201
+ const action = keyToAction({ command: v });
202
+
203
+ if (action) {
204
+ this.mathField.insert(action.value, { focus: true });
205
+ }
161
206
 
162
- values.forEach((vv) => this.mathField.insert(vv, { focus: true }));
163
207
  this.mathField.focus();
164
208
 
165
209
  return this.latex();
166
210
  }
167
211
 
168
- /** MathQuill `keystroke()` equivalent - takes a MathLive selector. */
169
- keystroke(selector) {
212
+ /**
213
+ * MathQuill `keystroke()` equivalent.
214
+ *
215
+ * Accepts MathQuill keystroke names (`'Left'`, `'Right'`, `'Backspace'`) as
216
+ * the keypad emits them, and passes anything else through as a MathLive
217
+ * selector.
218
+ */
219
+ keystroke(v) {
220
+ log('keystroke: ', v);
221
+
170
222
  if (!this.mathField) {
171
223
  return '';
172
224
  }
173
225
 
174
- this.mathField.executeCommand(selector);
226
+ const selector = KEYSTROKES[v] || v;
227
+
228
+ try {
229
+ this.mathField.executeCommand(selector);
230
+ } catch (e) {
231
+ log('unknown keystroke/selector "%s": %s', v, e && e.message);
232
+ }
233
+
175
234
  this.mathField.focus();
176
235
 
177
236
  return this.latex();