@pie-lib/math-input-mathlive 1.1.5-beta.6 → 1.1.5-beta.7

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.
@@ -0,0 +1,364 @@
1
+ import Static from '../mf/static';
2
+
3
+ /**
4
+ * The answer-block contract: `\MathQuillMathField[id]{}` in stored latex becomes
5
+ * a MathLive prompt, and sub-field values/focus are surfaced through the same
6
+ * callbacks @pie-lib/math-input exposed (`onSubFieldChange`, `onSubFieldFocus`,
7
+ * `getFieldName`, `setInput`).
8
+ *
9
+ * Driven against a stub mathfield: a real <math-field> needs a browser, and what
10
+ * matters here is the mapping, not MathLive's rendering.
11
+ */
12
+ const stubField = (values = {}, selection = null) => ({
13
+ values: { ...values },
14
+ getPrompts() {
15
+ return Object.keys(this.values);
16
+ },
17
+ getPromptValue(id) {
18
+ return this.values[id];
19
+ },
20
+ setPromptValue(id, v) {
21
+ this.values[id] = v;
22
+ },
23
+ getPromptRange(id) {
24
+ const i = Object.keys(this.values).indexOf(id);
25
+
26
+ return [i * 10, i * 10 + 5];
27
+ },
28
+ selection,
29
+ // Caret bookkeeping: MathLive exposes the caret offset as `position`, and any
30
+ // executeCommand triggers the render that reveals the caret.
31
+ position: undefined,
32
+ commands: [],
33
+ executeCommand(name) {
34
+ this.commands.push(name);
35
+ },
36
+ addEventListener() {},
37
+ removeEventListener() {},
38
+ focus() {
39
+ this.focused = true;
40
+ },
41
+ blur() {
42
+ this.blurred = true;
43
+ },
44
+ remove() {},
45
+ });
46
+
47
+ const make = (props = {}) => new Static({ latex: 'x=\\MathQuillMathField[r1]{}', getFieldName: () => {}, ...props });
48
+
49
+ describe('Static: answer blocks / prompts', () => {
50
+ describe('isInteractive', () => {
51
+ it('is interactive when the latex has answer blocks', () => {
52
+ expect(make({ latex: 'x=\\MathQuillMathField[r1]{}' }).isInteractive()).toBe(true);
53
+ expect(make({ latex: 'x=\\placeholder[r1]{}' }).isInteractive()).toBe(true);
54
+ });
55
+
56
+ it('is display-only for plain latex', () => {
57
+ expect(make({ latex: '\\frac{1}{2}' }).isInteractive()).toBe(false);
58
+ expect(make({ latex: '' }).isInteractive()).toBe(false);
59
+ });
60
+ });
61
+
62
+ describe('values', () => {
63
+ it('returns {} before the field exists', () => {
64
+ expect(make().values()).toEqual({});
65
+ });
66
+
67
+ it('maps each prompt id to its stored-form latex', () => {
68
+ const s = make();
69
+
70
+ s.mathField = stubField({ r1: '7', r2: '\\placeholder{}' });
71
+
72
+ // \placeholder must not leak into stored values
73
+ expect(s.values()).toEqual({ r1: '7', r2: '' });
74
+ });
75
+ });
76
+
77
+ describe('promptHandle / innerFields', () => {
78
+ it('exposes a MathQuill-field-like handle with get/set latex', () => {
79
+ const s = make();
80
+
81
+ s.mathField = stubField({ r1: '7' });
82
+
83
+ const h = s.promptHandle('r1');
84
+
85
+ expect(h.id).toEqual('r1');
86
+ expect(h.latex()).toEqual('7');
87
+
88
+ h.latex('9');
89
+ expect(s.mathField.values.r1).toEqual('9');
90
+ });
91
+
92
+ it('handle latex() is safe with no field', () => {
93
+ expect(make().promptHandle('r1').latex()).toEqual('');
94
+ });
95
+
96
+ it('innerFields returns one handle per prompt', () => {
97
+ const s = make();
98
+
99
+ s.mathField = stubField({ r1: '1', r2: '2' });
100
+
101
+ expect(s.innerFields.map((f) => f.id)).toEqual(['r1', 'r2']);
102
+ expect(make().innerFields).toEqual([]);
103
+ });
104
+ });
105
+
106
+ describe('onSubFieldChange', () => {
107
+ it('fires only for prompts whose value changed', () => {
108
+ const onSubFieldChange = jest.fn();
109
+ const s = make({ onSubFieldChange });
110
+
111
+ s.mathField = stubField({ r1: '1', r2: '2' });
112
+ s.promptValues = { r1: '1', r2: '2' };
113
+
114
+ s.mathField.values.r2 = '5';
115
+ s.onPromptInput();
116
+
117
+ expect(onSubFieldChange).toHaveBeenCalledTimes(1);
118
+ expect(onSubFieldChange).toHaveBeenCalledWith('r2', '5');
119
+ });
120
+
121
+ it('uses getFieldName when it returns a name', () => {
122
+ const onSubFieldChange = jest.fn();
123
+ const s = make({ onSubFieldChange, getFieldName: (handle) => 'named-' + handle.id });
124
+
125
+ s.mathField = stubField({ r1: '1' });
126
+ s.promptValues = {};
127
+
128
+ s.onPromptInput();
129
+
130
+ expect(onSubFieldChange).toHaveBeenCalledWith('named-r1', '1');
131
+ });
132
+
133
+ it('still tracks values when no callback is supplied', () => {
134
+ const s = make();
135
+
136
+ s.mathField = stubField({ r1: '3' });
137
+ s.onPromptInput();
138
+
139
+ expect(s.promptValues).toEqual({ r1: '3' });
140
+ });
141
+ });
142
+
143
+ describe('onSubFieldFocus', () => {
144
+ it('reports the prompt containing the selection, plus setInput', () => {
145
+ const onSubFieldFocus = jest.fn();
146
+ const setInput = jest.fn();
147
+ const s = make({ onSubFieldFocus, setInput });
148
+
149
+ // r2 occupies offsets 10..15
150
+ s.mathField = stubField({ r1: '', r2: '' }, { ranges: [[11, 11]] });
151
+ s.onPromptFocus();
152
+
153
+ expect(onSubFieldFocus).toHaveBeenCalledTimes(1);
154
+ expect(onSubFieldFocus.mock.calls[0][0]).toEqual('r2');
155
+ expect(onSubFieldFocus.mock.calls[0][1].id).toEqual('r2');
156
+ expect(setInput).toHaveBeenCalled();
157
+ });
158
+
159
+ it('does nothing without a selection', () => {
160
+ const onSubFieldFocus = jest.fn();
161
+ const s = make({ onSubFieldFocus });
162
+
163
+ s.mathField = stubField({ r1: '' }, null);
164
+ s.onPromptFocus();
165
+
166
+ expect(onSubFieldFocus).not.toHaveBeenCalled();
167
+ });
168
+
169
+ it('does not throw when the field is missing', () => {
170
+ const s = make({ onSubFieldFocus: jest.fn() });
171
+
172
+ expect(() => s.onPromptFocus()).not.toThrow();
173
+ });
174
+ });
175
+
176
+ describe('accessibility announcements', () => {
177
+ afterEach(() => {
178
+ document.body.innerHTML = '';
179
+ });
180
+
181
+ it('creates a polite off-screen live region', () => {
182
+ const s = make();
183
+
184
+ s.createLiveRegion();
185
+
186
+ expect(s.liveRegion.getAttribute('aria-live')).toEqual('polite');
187
+ expect(s.liveRegion.getAttribute('aria-atomic')).toEqual('true');
188
+ expect(document.body.contains(s.liveRegion)).toBe(true);
189
+
190
+ s.removeLiveRegion();
191
+ expect(s.liveRegion).toBeUndefined();
192
+ });
193
+
194
+ it('does not create a second region', () => {
195
+ const s = make();
196
+
197
+ s.createLiveRegion();
198
+ const first = s.liveRegion;
199
+
200
+ s.createLiveRegion();
201
+ expect(s.liveRegion).toBe(first);
202
+ s.removeLiveRegion();
203
+ });
204
+
205
+ it('announces when typed input expands into a command', () => {
206
+ const s = make();
207
+
208
+ s.createLiveRegion();
209
+ s.promptValues = { r1: 'x' };
210
+ // grew by more than one char AND gained a backslash => a conversion
211
+ s.announceConversion({ r1: 'x\\frac{}{}' });
212
+
213
+ expect(s.liveRegion.textContent).toEqual('Converted to math symbol');
214
+ s.removeLiveRegion();
215
+ });
216
+
217
+ it('does not announce ordinary typing', () => {
218
+ const s = make();
219
+
220
+ s.createLiveRegion();
221
+ s.promptValues = { r1: 'x' };
222
+ s.announceConversion({ r1: 'xy' });
223
+
224
+ expect(s.liveRegion.textContent).toEqual('');
225
+ s.removeLiveRegion();
226
+ });
227
+
228
+ it('does not announce a deletion', () => {
229
+ const s = make();
230
+
231
+ s.createLiveRegion();
232
+ s.promptValues = { r1: 'x\\frac{}{}' };
233
+ s.onKeyDown({ key: 'Backspace' });
234
+ s.announceConversion({ r1: 'x' });
235
+
236
+ expect(s.liveRegion.textContent).toEqual('');
237
+ s.removeLiveRegion();
238
+ });
239
+ });
240
+
241
+ describe('focus / blur', () => {
242
+ it('delegates to the field and is safe without one', () => {
243
+ const s = make();
244
+
245
+ expect(() => {
246
+ s.focus();
247
+ s.blur();
248
+ }).not.toThrow();
249
+
250
+ s.mathField = stubField();
251
+ s.focus();
252
+ s.blur();
253
+
254
+ expect(s.mathField.focused).toBe(true);
255
+ expect(s.mathField.blurred).toBe(true);
256
+ });
257
+ });
258
+
259
+ /**
260
+ * MathLive shows the caret only while the field carries `ML__focused`, which
261
+ * it adds during a render gated on `isSelectionEditable && hasFocus()`. In a
262
+ * read-only field only prompt interiors are editable, so after a bare
263
+ * `focus()` - whose selection sits outside every prompt - the answer block
264
+ * looked focused but had no blinking cursor. Verified in Chromium: the caret
265
+ * span goes from visibility:hidden to visible once the selection is inside a
266
+ * prompt AND a render has run.
267
+ */
268
+ describe('caret placement', () => {
269
+ it('puts a collapsed caret in the first prompt on focus', () => {
270
+ const s = make();
271
+
272
+ s.mathField = stubField({ r1: '', r2: '' });
273
+ s.focus();
274
+
275
+ // stub ranges are [0,5] for r1, [10,15] for r2
276
+ expect(s.mathField.selection).toEqual({ ranges: [[0, 0]] });
277
+ });
278
+
279
+ it('forces the render that makes the caret visible', () => {
280
+ const s = make();
281
+
282
+ s.mathField = stubField({ r1: '' });
283
+ s.focus();
284
+
285
+ // assigning `selection` alone updates the model without re-rendering, so
286
+ // ML__focused would never be added
287
+ expect(s.mathField.commands).toEqual(['scrollIntoView']);
288
+ });
289
+
290
+ it('does not move a caret that is already inside a prompt', () => {
291
+ const s = make();
292
+
293
+ s.mathField = stubField({ r1: '', r2: '' });
294
+ s.mathField.position = 12; // inside r2
295
+ s.focus();
296
+
297
+ expect(s.mathField.selection).toBe(null);
298
+ expect(s.mathField.commands).toEqual([]);
299
+ });
300
+
301
+ it('targets a specific prompt', () => {
302
+ const s = make();
303
+
304
+ s.mathField = stubField({ r1: '', r2: '' });
305
+ s.focusPrompt('r2');
306
+
307
+ expect(s.mathField.selection).toEqual({ ranges: [[10, 10]] });
308
+ });
309
+
310
+ it('falls back to the first prompt for an unknown id', () => {
311
+ const s = make();
312
+
313
+ s.mathField = stubField({ r1: '', r2: '' });
314
+ s.focusPrompt('nope');
315
+
316
+ expect(s.mathField.selection).toEqual({ ranges: [[0, 0]] });
317
+ });
318
+
319
+ it('is a no-op when there are no prompts', () => {
320
+ const s = make();
321
+
322
+ s.mathField = stubField({});
323
+
324
+ expect(() => s.focusPrompt()).not.toThrow();
325
+ expect(s.mathField.selection).toBe(null);
326
+ });
327
+
328
+ it('is safe without a field', () => {
329
+ expect(() => make().focusPrompt('r1')).not.toThrow();
330
+ });
331
+
332
+ describe('promptContaining', () => {
333
+ it('finds the prompt whose range holds the offset, inclusive', () => {
334
+ const s = make();
335
+
336
+ s.mathField = stubField({ r1: '', r2: '' });
337
+
338
+ expect(s.promptContaining(0)).toEqual('r1');
339
+ expect(s.promptContaining(5)).toEqual('r1');
340
+ expect(s.promptContaining(10)).toEqual('r2');
341
+ expect(s.promptContaining(7)).toBeUndefined();
342
+ });
343
+
344
+ it('treats a missing position as outside', () => {
345
+ const s = make();
346
+
347
+ s.mathField = stubField({ r1: '' });
348
+
349
+ expect(s.promptContaining(undefined)).toBeUndefined();
350
+ });
351
+ });
352
+
353
+ // MathQuill's inner fields could be focused directly.
354
+ it('promptHandle exposes focus() for its own prompt', () => {
355
+ const s = make();
356
+
357
+ s.mathField = stubField({ r1: '', r2: '' });
358
+ s.promptHandle('r2').focus();
359
+
360
+ expect(s.mathField.focused).toBe(true);
361
+ expect(s.mathField.selection).toEqual({ ranges: [[10, 10]] });
362
+ });
363
+ });
364
+ });
@@ -72,11 +72,20 @@ const EMPTY_GROUP_REGEX = /\{\s*\}/g;
72
72
  // Empty delimiter pairs: `\abs{}` is converted to `\left|\right|`, and the
73
73
  // parenthesis / bracket keys are `\left(\right)` / `\left[\right]`. None has a
74
74
  // `{}` group for the rule above to catch, so they would render as bare fences.
75
- const EMPTY_FENCE_REGEX = /\\left([(\[|])\s*\\right([)\]|])/g;
75
+ const EMPTY_FENCE_REGEX = /\\left([([|])\s*\\right([)\]|])/g;
76
76
  // The same muted fill as the other slots (`pie-empty`), but a tall rectangle
77
77
  // rather than a square. It also sets the fences' height: `\left...\right` stretch
78
78
  // to their content, so a taller slot gives taller parentheses.
79
- const EMPTY_FENCE_SLOT = '\\htmlData{pie-empty=1}{\\rule{0.55em}{1em}}';
79
+ //
80
+ // A `\rule` stands on the baseline, but the fences are centred on the math axis
81
+ // (0.25em above it), so an un-lifted box sat high between them. `[-0.25em]`
82
+ // lowers it by (axis - height / 2) = 0.25 - 1 / 2, centring it on the axis.
83
+ //
84
+ // Lowering it alone also shrinks the fences, which size themselves to their
85
+ // content (MathLive switches to short bars). The zero-width `\rule{0em}{1em}`
86
+ // strut keeps the content as tall as before, so the bars keep their height and
87
+ // only the box moves.
88
+ const EMPTY_FENCE_SLOT = '\\rule{0em}{1em}\\htmlData{pie-empty=1}{\\rule[-0.25em]{0.55em}{1em}}';
80
89
 
81
90
  /**
82
91
  * Make empty argument slots visible, for static rendering.
package/src/mf/input.jsx CHANGED
@@ -81,7 +81,7 @@ export class Input extends React.Component {
81
81
  // defaults to an empty string.
82
82
  this.mathField.mathModeSpace = MATH_MODE_SPACE;
83
83
  // After the exponent key the cursor must stay in the exponent until it is moved
84
- // explicitly, as in the legacy editor. MathLive's `smartSuperscript` (on by
84
+ // MathLive's `smartSuperscript` (on by
85
85
  // default) jumps out of an empty superscript after a single digit, which breaks
86
86
  // typing x^23 or 10^6 and is inconsistent with non-digit exponents.
87
87
  this.mathField.smartSuperscript = false;