@pie-lib/math-input-mathlive 1.1.5-beta.1 → 1.1.5-beta.11

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 (55) hide show
  1. package/lib/__tests__/composites.test.js +255 -0
  2. package/lib/__tests__/drop-in-compat.test.js +133 -0
  3. package/lib/__tests__/embedded-fonts.test.js +121 -0
  4. package/lib/__tests__/index.test.js +118 -0
  5. package/lib/__tests__/input-imperative.test.js +155 -0
  6. package/lib/__tests__/keypad-label.test.js +231 -0
  7. package/lib/__tests__/keypad-render.test.js +206 -0
  8. package/lib/__tests__/latex-bridge.test.js +386 -0
  9. package/lib/__tests__/mathlive-instance.test.js +419 -0
  10. package/lib/__tests__/mount-race.test.js +218 -0
  11. package/lib/__tests__/static-prompts.test.js +405 -0
  12. package/lib/__tests__/tmp-markup.test.js +26 -0
  13. package/lib/keypad/index.js +36 -12
  14. package/lib/keypad/index.js.map +1 -1
  15. package/lib/latex-bridge.js +159 -4
  16. package/lib/latex-bridge.js.map +1 -1
  17. package/lib/mathlive-instance.js +21 -28
  18. package/lib/mathlive-instance.js.map +1 -1
  19. package/lib/mf/common-styles.js +17 -2
  20. package/lib/mf/common-styles.js.map +1 -1
  21. package/lib/mf/index.js +6 -0
  22. package/lib/mf/index.js.map +1 -1
  23. package/lib/mf/input.js +13 -0
  24. package/lib/mf/input.js.map +1 -1
  25. package/lib/mf/static.js +4 -2
  26. package/lib/mf/static.js.map +1 -1
  27. package/lib/selection.js +148 -0
  28. package/lib/selection.js.map +1 -0
  29. package/package.json +1 -1
  30. package/src/__tests__/composites.test.jsx +220 -0
  31. package/src/__tests__/drop-in-compat.test.jsx +112 -0
  32. package/src/__tests__/index.test.jsx +109 -0
  33. package/src/__tests__/input-imperative.test.jsx +144 -0
  34. package/src/__tests__/input-selection.test.jsx +64 -0
  35. package/src/__tests__/keypad-label.test.jsx +284 -0
  36. package/src/__tests__/keypad-render.test.jsx +123 -0
  37. package/src/__tests__/latex-bridge.test.js +389 -0
  38. package/src/__tests__/mathlive-instance.test.js +418 -0
  39. package/src/__tests__/mount-race.test.jsx +142 -0
  40. package/src/__tests__/selection.test.js +212 -0
  41. package/src/__tests__/static-prompts.test.jsx +364 -0
  42. package/src/keypad/index.jsx +18 -3
  43. package/src/latex-bridge.js +171 -4
  44. package/src/mathlive-instance.js +27 -29
  45. package/src/mf/common-styles.js +19 -0
  46. package/src/mf/index.js +2 -0
  47. package/src/mf/input.jsx +14 -0
  48. package/src/mf/static.jsx +4 -1
  49. package/src/selection.js +146 -0
  50. package/lib/fonts/embedded.js +0 -120
  51. package/lib/fonts/index.js +0 -81
  52. package/lib/fonts/index.js.map +0 -1
  53. package/scripts/generate-embedded-fonts.js +0 -94
  54. package/src/fonts/embedded.js +0 -116
  55. package/src/fonts/index.js +0 -71
@@ -0,0 +1,112 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import { styled } from '@mui/material/styles';
4
+
5
+ import { mq } from '../index';
6
+
7
+ /**
8
+ * Exercises the exact call patterns real consumers use against
9
+ * @pie-lib/math-input, to catch drop-in incompatibilities.
10
+ *
11
+ * Sources:
12
+ * - packages/math-toolbar/src/editor-and-pad.jsx:16,185
13
+ * - packages/math-toolbar/src/math-preview.jsx:8,156
14
+ * - packages/editable-html/src/plugins/respArea/math-templated/index.jsx:92
15
+ * - packages/editable-html-tip-tap/src/components/respArea/MathTemplated.jsx:111
16
+ */
17
+ describe('drop-in compatibility with @pie-lib/math-input consumers', () => {
18
+ describe('mq.CommonMqStyles destructuring', () => {
19
+ it('math-preview.jsx: { commonMqFontStyles, longdivStyles, supsubStyles }', () => {
20
+ const { commonMqFontStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
21
+
22
+ expect(commonMqFontStyles).toBeDefined();
23
+ expect(longdivStyles).toBeDefined();
24
+ expect(supsubStyles).toBeDefined();
25
+ });
26
+
27
+ it('editor-and-pad.jsx: also { commonMqKeyboardStyles }', () => {
28
+ const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
29
+
30
+ expect(commonMqKeyboardStyles).toBeDefined();
31
+ // All four must be spreadable into an emotion style object.
32
+ expect(() => ({
33
+ ...commonMqFontStyles,
34
+ ...commonMqKeyboardStyles,
35
+ ...longdivStyles,
36
+ ...supsubStyles,
37
+ })).not.toThrow();
38
+ });
39
+
40
+ it('the destructured groups survive being spread into a styled component', () => {
41
+ const { commonMqFontStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
42
+ const Styled = styled('div')({ ...commonMqFontStyles, ...longdivStyles, ...supsubStyles });
43
+
44
+ expect(() => render(<Styled />)).not.toThrow();
45
+ });
46
+ });
47
+
48
+ describe('mq.Input', () => {
49
+ it('editor-and-pad.jsx: styled(mq.Input) renders', () => {
50
+ const MathEditor = styled(mq.Input)(() => ({ width: '100%' }));
51
+
52
+ expect(() => render(<MathEditor latex="\\frac{1}{2}" onChange={() => {}} />)).not.toThrow();
53
+ });
54
+
55
+ it('accepts the className that styled() injects', () => {
56
+ const Styled = styled(mq.Input)({ padding: 4 });
57
+ const { container } = render(<Styled latex="x" onChange={() => {}} />);
58
+
59
+ expect(container.firstChild).toBeTruthy();
60
+ expect(container.firstChild.className).toBeTruthy();
61
+ });
62
+ });
63
+
64
+ describe('mq.Static', () => {
65
+ it('math-preview.jsx: <mq.Static latex onFocus onBlur />', () => {
66
+ expect(() => render(<mq.Static latex={'\\frac{1}{2}'} onFocus={() => {}} onBlur={() => {}} />)).not.toThrow();
67
+ });
68
+
69
+ it('math-templated respArea: <mq.Static latex={value} />', () => {
70
+ expect(() => render(<mq.Static latex={'x=1'} />)).not.toThrow();
71
+ });
72
+
73
+ it('renders answer-block latex (the templated-math case) without throwing', () => {
74
+ expect(() =>
75
+ render(<mq.Static latex={'x=\\MathQuillMathField[r1]{}'} onSubFieldChange={() => {}} />),
76
+ ).not.toThrow();
77
+ });
78
+
79
+ it('is styleable, as keypad labels were in math-input', () => {
80
+ const Styled = styled(mq.Static)({ pointerEvents: 'none' });
81
+
82
+ expect(() => render(<Styled latex={'\\pi'} />)).not.toThrow();
83
+ });
84
+ });
85
+
86
+ describe('surface parity', () => {
87
+ it('exposes exactly the names math-input exported', () => {
88
+ // eslint-disable-next-line global-require
89
+ const pkg = require('../index');
90
+
91
+ [
92
+ 'keysForGrade',
93
+ 'addBrackets',
94
+ 'removeBrackets',
95
+ 'keys',
96
+ 'HorizontalKeypad',
97
+ 'mq',
98
+ 'updateSpans',
99
+ 'registerEmbed',
100
+ 'applyStaticMath',
101
+ ].forEach((name) => {
102
+ expect(pkg[name]).toBeDefined();
103
+ });
104
+ });
105
+
106
+ it('mq exposes Input, Static and CommonMqStyles', () => {
107
+ ['Input', 'Static', 'CommonMqStyles'].forEach((name) => {
108
+ expect(mq[name]).toBeDefined();
109
+ });
110
+ });
111
+ });
112
+ });
@@ -0,0 +1,109 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+
4
+ import {
5
+ keys,
6
+ keysForGrade,
7
+ addBrackets,
8
+ removeBrackets,
9
+ registerEmbed,
10
+ applyStaticMath,
11
+ updateSpans,
12
+ mf,
13
+ mq,
14
+ MathInput,
15
+ HorizontalKeypad,
16
+ KeyPad,
17
+ } from '../index';
18
+
19
+ describe('math-input-mathlive public API', () => {
20
+ it('mirrors the @pie-lib/math-input export surface', () => {
21
+ expect(typeof keysForGrade).toBe('function');
22
+ expect(typeof addBrackets).toBe('function');
23
+ expect(typeof removeBrackets).toBe('function');
24
+ expect(typeof registerEmbed).toBe('function');
25
+ expect(typeof applyStaticMath).toBe('function');
26
+ expect(typeof updateSpans).toBe('function');
27
+ expect(keys).toBeDefined();
28
+ expect(keys.baseSet).toBeDefined();
29
+ });
30
+
31
+ it('exports the field wrappers under both mf and mq', () => {
32
+ expect(mf.Input).toBeDefined();
33
+ expect(mf.Static).toBeDefined();
34
+ // `mq` alias lets a consumer swap packages by changing only the import.
35
+ expect(mq).toBe(mf);
36
+ });
37
+
38
+ it('updateSpans is a safe no-op', () => {
39
+ expect(() => updateSpans()).not.toThrow();
40
+ });
41
+
42
+ it('CommonMqStyles keeps the shape consumers destructure', () => {
43
+ // math-toolbar does:
44
+ // const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles }
45
+ // = mq.CommonMqStyles;
46
+ const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles } = mf.CommonMqStyles;
47
+
48
+ [commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles].forEach((s) => {
49
+ expect(s).toBeDefined();
50
+ expect(typeof s).toBe('object');
51
+ });
52
+ });
53
+
54
+ it('bracket helpers round-trip', () => {
55
+ expect(addBrackets('x')).toEqual('\\(x\\)');
56
+ expect(removeBrackets('\\(x\\)')).toEqual('x');
57
+ expect(removeBrackets(addBrackets('\\frac{1}{2}'))).toEqual('\\frac{1}{2}');
58
+ });
59
+
60
+ it('keysForGrade returns a 5-row keyset', () => {
61
+ const set = keysForGrade('geometry');
62
+
63
+ expect(Array.isArray(set)).toBe(true);
64
+ expect(set.length).toBe(5);
65
+ });
66
+ });
67
+
68
+ describe('rendering', () => {
69
+ // MathLive is a browser-only custom element; loadMathLive() resolves async and
70
+ // is a no-op-ish under jsdom. These assert the components mount without
71
+ // throwing, which is what regressed most often in the MathQuill version.
72
+ it('KeyPad renders without a live MathLive instance', () => {
73
+ const { container } = render(<KeyPad onPress={() => {}} />);
74
+
75
+ expect(container.querySelectorAll('button').length).toBeGreaterThan(0);
76
+ });
77
+
78
+ it('KeyPad falls back to raw latex for labels before MathLive loads', () => {
79
+ const { container } = render(
80
+ <KeyPad onPress={() => {}} baseSet={[[{ name: 'pi', latex: '\\pi', category: 'constants' }]]} />,
81
+ );
82
+
83
+ expect(container.textContent).toContain('\\pi');
84
+ });
85
+
86
+ it('KeyPad fires onPress with the key definition', () => {
87
+ const onPress = jest.fn();
88
+ const key = { name: 'x', label: 'x', write: 'x', category: 'vars' };
89
+ const { container } = render(<KeyPad onPress={onPress} baseSet={[[key]]} />);
90
+
91
+ container.querySelector('button').click();
92
+
93
+ expect(onPress).toHaveBeenCalledWith(key);
94
+ });
95
+
96
+ it('MathInput mounts', () => {
97
+ expect(() => render(<MathInput latex="\\frac{1}{2}" onChange={() => {}} />)).not.toThrow();
98
+ });
99
+
100
+ it('HorizontalKeypad mounts and reports the legacy {value,type} model', () => {
101
+ const onClick = jest.fn();
102
+
103
+ expect(() => render(<HorizontalKeypad onClick={onClick} />)).not.toThrow();
104
+ });
105
+
106
+ it('Static renders display-mode latex without a mathfield', () => {
107
+ expect(() => render(<mf.Static latex="\\frac{1}{2}" />)).not.toThrow();
108
+ });
109
+ });
@@ -0,0 +1,144 @@
1
+ import { KEYSTROKES, keyToAction } from '../latex-bridge';
2
+ import Input from '../mf/input';
3
+
4
+ /**
5
+ * The imperative surface that @pie-lib/math-toolbar drives
6
+ * (editor-and-pad.jsx: clear/command/keystroke/write/latex).
7
+ *
8
+ * These call the methods against a stub mathfield, because a real
9
+ * <math-field> needs a browser. What matters here is the *translation*:
10
+ * MathQuill-era arguments must become valid MathLive operations.
11
+ */
12
+ const stubField = () => {
13
+ const calls = { insert: [], executeCommand: [], focus: 0 };
14
+
15
+ return {
16
+ calls,
17
+ value: '',
18
+ getValue: () => '',
19
+ insert(v) {
20
+ calls.insert.push(v);
21
+ },
22
+ executeCommand(v) {
23
+ calls.executeCommand.push(v);
24
+ },
25
+ focus() {
26
+ calls.focus++;
27
+ },
28
+ blur() {},
29
+ };
30
+ };
31
+
32
+ const withField = () => {
33
+ const input = new Input({});
34
+ const f = stubField();
35
+
36
+ input.mathField = f;
37
+
38
+ return { input, f };
39
+ };
40
+
41
+ describe('Input imperative API (drives math-toolbar)', () => {
42
+ describe('keystroke', () => {
43
+ it('maps MathQuill keystroke names to MathLive selectors', () => {
44
+ const { input, f } = withField();
45
+
46
+ input.keystroke('Left');
47
+ input.keystroke('Right');
48
+ input.keystroke('Backspace');
49
+
50
+ expect(f.calls.executeCommand).toEqual(['moveToPreviousChar', 'moveToNextChar', 'deleteBackward']);
51
+ });
52
+
53
+ it('passes an unknown value through as a selector', () => {
54
+ const { input, f } = withField();
55
+
56
+ input.keystroke('deleteForward');
57
+
58
+ expect(f.calls.executeCommand).toEqual(['deleteForward']);
59
+ });
60
+
61
+ it('does not throw when the engine rejects the selector', () => {
62
+ const { input } = withField();
63
+
64
+ input.mathField.executeCommand = () => {
65
+ throw new Error('unknown command');
66
+ };
67
+
68
+ expect(() => input.keystroke('Nonsense')).not.toThrow();
69
+ });
70
+
71
+ it('covers every keystroke the keypad emits', () => {
72
+ // navigation.js / edit.js only produce these three
73
+ ['Left', 'Right', 'Backspace'].forEach((k) => expect(KEYSTROKES[k]).toBeDefined());
74
+ });
75
+ });
76
+
77
+ describe('command', () => {
78
+ it('expands argument-taking commands into placeholders', () => {
79
+ const { input, f } = withField();
80
+
81
+ input.command('\\frac');
82
+
83
+ // a bare \frac would give no editable slots
84
+ expect(f.calls.insert).toEqual(['\\frac{#?}{#?}']);
85
+ });
86
+
87
+ it('handles array commands', () => {
88
+ const { input, f } = withField();
89
+
90
+ input.command(['\\log', '_']);
91
+
92
+ expect(f.calls.insert).toEqual(['\\log_{#?}']);
93
+ });
94
+
95
+ it('passes argument-less commands through', () => {
96
+ const { input, f } = withField();
97
+
98
+ input.command('\\pi');
99
+
100
+ expect(f.calls.insert).toEqual(['\\pi']);
101
+ });
102
+
103
+ it('agrees with keyToAction', () => {
104
+ expect(keyToAction({ command: '\\sqrt' }).value).toEqual('\\sqrt{#?}');
105
+ });
106
+ });
107
+
108
+ describe('safety before MathLive loads', () => {
109
+ it('every method is a no-op rather than a crash', () => {
110
+ const input = new Input({});
111
+
112
+ // mathField is undefined until loadMathLive() resolves
113
+ expect(input.latex()).toEqual('');
114
+ expect(input.clear()).toEqual('');
115
+ expect(input.command('\\frac')).toEqual('');
116
+ expect(input.keystroke('Left')).toEqual('');
117
+ expect(input.write('x')).toEqual('');
118
+ expect(() => input.focus()).not.toThrow();
119
+ expect(() => input.blur()).not.toThrow();
120
+ expect(() => input.setLatex('x')).not.toThrow();
121
+ expect(input.element).toBeUndefined();
122
+ });
123
+ });
124
+
125
+ describe('mqCompat facade', () => {
126
+ // math-toolbar used to call this.input.mathField.latex(); external
127
+ // consumers may still do the same.
128
+ it('exposes latex() as a getter', () => {
129
+ const { input } = withField();
130
+
131
+ input.mathField.getValue = () => '\\frac{1}{2}';
132
+
133
+ expect(input.mqCompat.latex()).toEqual('\\frac{1}{2}');
134
+ });
135
+
136
+ it('exposes latex(v) as a setter and el()', () => {
137
+ const { input, f } = withField();
138
+
139
+ input.mqCompat.latex('x+1');
140
+ expect(f.value).toEqual('x+1');
141
+ expect(input.mqCompat.el()).toBe(f);
142
+ });
143
+ });
144
+ });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The editable Input keeps its selection snapped to whole atoms (see
3
+ * selection.js). This checks the wiring: attached on mount, removed on unmount.
4
+ */
5
+ jest.mock('../mathlive-instance', () => {
6
+ class FakeMathfieldElement {
7
+ constructor() {
8
+ const el = global.document.createElement('math-field');
9
+
10
+ el.value = '';
11
+ el.getValue = () => el.value;
12
+
13
+ return el;
14
+ }
15
+ }
16
+
17
+ return {
18
+ loadMathLive: () => Promise.resolve({ MathfieldElement: FakeMathfieldElement }),
19
+ getMacros: () => ({}),
20
+ MATH_MODE_SPACE: '\\ ',
21
+ applyShadowStyles: () => {},
22
+ trackPlaceholderCaret: () => () => {},
23
+ latexToMarkup: (l) => `<span>${l}</span>`,
24
+ };
25
+ });
26
+
27
+ const mockUntrack = jest.fn();
28
+
29
+ jest.mock('../selection', () => ({
30
+ trackWholeAtomSelection: jest.fn(() => mockUntrack),
31
+ }));
32
+
33
+ // eslint-disable-next-line import/first
34
+ import Input from '../mf/input';
35
+ // eslint-disable-next-line import/first
36
+ import { trackWholeAtomSelection } from '../selection';
37
+
38
+ describe('Input: whole-atom selection', () => {
39
+ beforeEach(() => {
40
+ trackWholeAtomSelection.mockClear();
41
+ mockUntrack.mockClear();
42
+ });
43
+
44
+ it('attaches the tracker to the mathfield on mount', async () => {
45
+ const input = new Input({});
46
+
47
+ input.holderRef = { current: document.createElement('span') };
48
+ await input.componentDidMount();
49
+
50
+ expect(trackWholeAtomSelection).toHaveBeenCalledTimes(1);
51
+ expect(trackWholeAtomSelection).toHaveBeenCalledWith(input.mathField);
52
+ });
53
+
54
+ it('removes it on unmount', async () => {
55
+ const input = new Input({});
56
+
57
+ input.holderRef = { current: document.createElement('span') };
58
+ await input.componentDidMount();
59
+ input.componentWillUnmount();
60
+
61
+ expect(mockUntrack).toHaveBeenCalledTimes(1);
62
+ expect(input.untrackSelection).toBeUndefined();
63
+ });
64
+ });