@pie-lib/math-input 8.1.1-next.2 → 8.1.1-next.57
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.
- package/CHANGELOG.json +17 -0
- package/CHANGELOG.md +1172 -0
- package/LICENSE.md +5 -0
- package/lib/horizontal-keypad.js +96 -0
- package/lib/horizontal-keypad.js.map +1 -0
- package/lib/index.js +69 -0
- package/lib/index.js.map +1 -0
- package/lib/keypad/index.js +413 -0
- package/lib/keypad/index.js.map +1 -0
- package/lib/keypad/keys-layout.js +22 -0
- package/lib/keypad/keys-layout.js.map +1 -0
- package/lib/keys/basic-operators.js +33 -0
- package/lib/keys/basic-operators.js.map +1 -0
- package/lib/keys/chars.js +12 -0
- package/lib/keys/chars.js.map +1 -0
- package/lib/keys/comparison.js +39 -0
- package/lib/keys/comparison.js.map +1 -0
- package/lib/keys/constants.js +37 -0
- package/lib/keys/constants.js.map +1 -0
- package/lib/keys/digits.js +46 -0
- package/lib/keys/digits.js.map +1 -0
- package/lib/keys/edit.js +14 -0
- package/lib/keys/edit.js.map +1 -0
- package/lib/keys/exponent.js +30 -0
- package/lib/keys/exponent.js.map +1 -0
- package/lib/keys/fractions.js +29 -0
- package/lib/keys/fractions.js.map +1 -0
- package/lib/keys/geometry.js +140 -0
- package/lib/keys/geometry.js.map +1 -0
- package/lib/keys/grades.js +259 -0
- package/lib/keys/grades.js.map +1 -0
- package/lib/keys/index.js +35 -0
- package/lib/keys/index.js.map +1 -0
- package/lib/keys/log.js +27 -0
- package/lib/keys/log.js.map +1 -0
- package/lib/keys/logic.js +19 -0
- package/lib/keys/logic.js.map +1 -0
- package/lib/keys/matrices.js +19 -0
- package/lib/keys/matrices.js.map +1 -0
- package/lib/keys/misc.js +62 -0
- package/lib/keys/misc.js.map +1 -0
- package/lib/keys/navigation.js +20 -0
- package/lib/keys/navigation.js.map +1 -0
- package/lib/keys/operators.js +15 -0
- package/lib/keys/operators.js.map +1 -0
- package/lib/keys/statistics.js +40 -0
- package/lib/keys/statistics.js.map +1 -0
- package/lib/keys/sub-sup.js +19 -0
- package/lib/keys/sub-sup.js.map +1 -0
- package/lib/keys/trigonometry.js +45 -0
- package/lib/keys/trigonometry.js.map +1 -0
- package/lib/keys/utils.js +87 -0
- package/lib/keys/utils.js.map +1 -0
- package/lib/keys/vars.js +24 -0
- package/lib/keys/vars.js.map +1 -0
- package/lib/math-input.js +141 -0
- package/lib/math-input.js.map +1 -0
- package/lib/mq/common-mq-styles.js +102 -0
- package/lib/mq/common-mq-styles.js.map +1 -0
- package/lib/mq/custom-elements.js +20 -0
- package/lib/mq/custom-elements.js.map +1 -0
- package/lib/mq/index.js +28 -0
- package/lib/mq/index.js.map +1 -0
- package/lib/mq/input.js +186 -0
- package/lib/mq/input.js.map +1 -0
- package/lib/mq/mathquill-instance.js +52 -0
- package/lib/mq/mathquill-instance.js.map +1 -0
- package/lib/mq/static.js +301 -0
- package/lib/mq/static.js.map +1 -0
- package/lib/updateSpans.js +19 -0
- package/lib/updateSpans.js.map +1 -0
- package/package.json +18 -33
- package/src/__tests__/horizontal-keypad.test.jsx +463 -0
- package/src/__tests__/index.test.js +247 -0
- package/src/__tests__/math-input-test.jsx +45 -0
- package/src/__tests__/updateSpans.test.js +297 -0
- package/src/horizontal-keypad.jsx +69 -0
- package/src/index.jsx +28 -0
- package/src/keypad/__tests__/index.test.jsx +25 -0
- package/src/keypad/__tests__/keys-layout.test.js +14 -0
- package/src/keypad/index.jsx +439 -0
- package/src/keypad/keys-layout.js +16 -0
- package/src/keys/__tests__/utils.test.js +57 -0
- package/src/keys/basic-operators.js +32 -0
- package/src/keys/chars.js +5 -0
- package/src/keys/comparison.js +28 -0
- package/src/keys/constants.js +35 -0
- package/src/keys/digits.js +40 -0
- package/src/keys/edit.js +3 -0
- package/src/keys/exponent.js +28 -0
- package/src/keys/fractions.js +26 -0
- package/src/keys/geometry.js +144 -0
- package/src/keys/grades.js +367 -0
- package/src/keys/index.js +20 -0
- package/src/keys/log.js +22 -0
- package/src/keys/logic.js +15 -0
- package/src/keys/matrices.js +15 -0
- package/src/keys/misc.js +65 -0
- package/src/keys/navigation.js +8 -0
- package/src/keys/operators.js +10 -0
- package/src/keys/statistics.js +38 -0
- package/src/keys/sub-sup.js +15 -0
- package/src/keys/trigonometry.js +15 -0
- package/src/keys/utils.js +66 -0
- package/src/keys/vars.js +19 -0
- package/src/math-input.jsx +119 -0
- package/src/mq/__tests__/custom-elements.test.js +342 -0
- package/src/mq/__tests__/input.test.jsx +40 -0
- package/src/mq/__tests__/mathquill-instance.test.js +67 -0
- package/src/mq/__tests__/static.test.jsx +33 -0
- package/src/mq/common-mq-styles.js +109 -0
- package/src/mq/custom-elements.js +11 -0
- package/src/mq/index.js +5 -0
- package/src/mq/input.jsx +166 -0
- package/src/mq/mathquill-instance.js +45 -0
- package/src/mq/static.jsx +290 -0
- package/src/updateSpans.js +15 -0
- package/dist/_virtual/_rolldown/runtime.js +0 -11
- package/dist/horizontal-keypad.d.ts +0 -31
- package/dist/horizontal-keypad.js +0 -57
- package/dist/index.d.ts +0 -18
- package/dist/index.js +0 -19
- package/dist/keypad/accessible-keypad.d.ts +0 -37
- package/dist/keypad/accessible-keypad.js +0 -614
- package/dist/keypad/index.d.ts +0 -2
- package/dist/keypad/keys-layout.d.ts +0 -15
- package/dist/keypad/keys-layout.js +0 -5
- package/dist/keypad/model.d.ts +0 -28
- package/dist/keypad/model.js +0 -4
- package/dist/keys/basic-operators.d.ts +0 -13
- package/dist/keys/basic-operators.js +0 -30
- package/dist/keys/chars.d.ts +0 -13
- package/dist/keys/comparison.d.ts +0 -12
- package/dist/keys/comparison.js +0 -32
- package/dist/keys/constants.d.ts +0 -12
- package/dist/keys/constants.js +0 -35
- package/dist/keys/digits.d.ts +0 -23
- package/dist/keys/digits.js +0 -34
- package/dist/keys/edit.d.ts +0 -14
- package/dist/keys/edit.js +0 -9
- package/dist/keys/exponent.d.ts +0 -12
- package/dist/keys/exponent.js +0 -28
- package/dist/keys/fractions.d.ts +0 -11
- package/dist/keys/fractions.js +0 -27
- package/dist/keys/geometry.d.ts +0 -31
- package/dist/keys/geometry.js +0 -127
- package/dist/keys/grades.d.ts +0 -17
- package/dist/keys/grades.js +0 -414
- package/dist/keys/index.d.ts +0 -14
- package/dist/keys/index.js +0 -50
- package/dist/keys/log.d.ts +0 -11
- package/dist/keys/log.js +0 -25
- package/dist/keys/logic.d.ts +0 -10
- package/dist/keys/logic.js +0 -13
- package/dist/keys/matrices.d.ts +0 -10
- package/dist/keys/matrices.js +0 -17
- package/dist/keys/misc.d.ts +0 -18
- package/dist/keys/misc.js +0 -60
- package/dist/keys/navigation.d.ts +0 -10
- package/dist/keys/navigation.js +0 -13
- package/dist/keys/operators.d.ts +0 -9
- package/dist/keys/operators.js +0 -11
- package/dist/keys/statistics.d.ts +0 -13
- package/dist/keys/statistics.js +0 -38
- package/dist/keys/sub-sup.d.ts +0 -10
- package/dist/keys/sub-sup.js +0 -17
- package/dist/keys/trigonometry.d.ts +0 -14
- package/dist/keys/trigonometry.js +0 -43
- package/dist/keys/utils.d.ts +0 -13
- package/dist/keys/utils.js +0 -24
- package/dist/keys/vars.d.ts +0 -11
- package/dist/keys/vars.js +0 -22
- package/dist/math-input.d.ts +0 -30
- package/dist/mq/common-mq-styles.d.ts +0 -225
- package/dist/mq/common-mq-styles.js +0 -54
- package/dist/mq/custom-elements.d.ts +0 -10
- package/dist/mq/custom-elements.js +0 -10
- package/dist/mq/index.d.ts +0 -12
- package/dist/mq/index.js +0 -12
- package/dist/mq/input.d.ts +0 -35
- package/dist/mq/input.js +0 -83
- package/dist/mq/static.d.ts +0 -43
- package/dist/mq/static.js +0 -142
- package/dist/updateSpans.d.ts +0 -10
- package/dist/updateSpans.js +0 -9
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const mockRegisterEmbed = jest.fn();
|
|
2
|
+
const mockStaticMath = jest.fn().mockReturnValue({ mockInstance: true });
|
|
3
|
+
|
|
4
|
+
jest.mock('@pie-framework/mathquill', () => ({
|
|
5
|
+
__esModule: true,
|
|
6
|
+
default: {
|
|
7
|
+
getInterface: jest.fn(() => ({
|
|
8
|
+
registerEmbed: mockRegisterEmbed,
|
|
9
|
+
StaticMath: mockStaticMath,
|
|
10
|
+
})),
|
|
11
|
+
},
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
jest.mock('../custom-elements', () => ({
|
|
15
|
+
registerLineBreak: jest.fn(),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
describe('mathquill-instance', () => {
|
|
19
|
+
let registerEmbed;
|
|
20
|
+
let applyStaticMath;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.resetModules();
|
|
24
|
+
mockRegisterEmbed.mockClear();
|
|
25
|
+
mockStaticMath.mockClear();
|
|
26
|
+
({ registerEmbed, applyStaticMath } = require('../mathquill-instance'));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('registerEmbed', () => {
|
|
30
|
+
it('delegates to MQ.registerEmbed when available', () => {
|
|
31
|
+
const factory = () => ({});
|
|
32
|
+
registerEmbed('answerBlock', factory);
|
|
33
|
+
|
|
34
|
+
expect(mockRegisterEmbed).toHaveBeenCalledTimes(1);
|
|
35
|
+
expect(mockRegisterEmbed).toHaveBeenCalledWith('answerBlock', factory);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('applyStaticMath', () => {
|
|
40
|
+
it('returns undefined when element is missing', () => {
|
|
41
|
+
expect(applyStaticMath(null, 'x')).toBeUndefined();
|
|
42
|
+
expect(mockStaticMath).not.toHaveBeenCalled();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('sets textContent when latex is provided and calls StaticMath', () => {
|
|
46
|
+
const el = document.createElement('span');
|
|
47
|
+
const handlers = { edit: jest.fn() };
|
|
48
|
+
|
|
49
|
+
const result = applyStaticMath(el, '\\frac{1}{2}', { handlers });
|
|
50
|
+
|
|
51
|
+
expect(el.textContent).toBe('\\frac{1}{2}');
|
|
52
|
+
expect(mockStaticMath).toHaveBeenCalledTimes(1);
|
|
53
|
+
expect(mockStaticMath).toHaveBeenCalledWith(el, { handlers });
|
|
54
|
+
expect(result).toEqual({ mockInstance: true });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('omits textContent when latex is undefined but still calls StaticMath', () => {
|
|
58
|
+
const el = document.createElement('span');
|
|
59
|
+
el.textContent = 'preset';
|
|
60
|
+
|
|
61
|
+
applyStaticMath(el, undefined);
|
|
62
|
+
|
|
63
|
+
expect(el.textContent).toBe('preset');
|
|
64
|
+
expect(mockStaticMath).toHaveBeenCalledWith(el, undefined);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import Static from '../static';
|
|
4
|
+
|
|
5
|
+
const Mathquill = require('@pie-framework/mathquill');
|
|
6
|
+
|
|
7
|
+
jest.mock('@pie-framework/mathquill', () => ({
|
|
8
|
+
StaticMath: jest.fn().mockReturnValue({
|
|
9
|
+
latex: jest.fn(),
|
|
10
|
+
parseLatex: jest.fn(),
|
|
11
|
+
}),
|
|
12
|
+
getInterface: jest.fn().mockReturnThis(),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
describe('static', () => {
|
|
16
|
+
describe('rendering', () => {
|
|
17
|
+
it('renders with latex prop', () => {
|
|
18
|
+
const { container } = render(<Static latex="foo" />);
|
|
19
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('renders with empty latex', () => {
|
|
23
|
+
const { container } = render(<Static latex="" />);
|
|
24
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Note: Tests for internal methods like componentDidMount, shouldComponentUpdate
|
|
29
|
+
// are implementation details and cannot be directly tested with RTL.
|
|
30
|
+
// These components are wrappers around MathQuill library and the original tests
|
|
31
|
+
// focused on testing internal implementation rather than user-facing behavior.
|
|
32
|
+
// The actual MathQuill integration is tested through integration/e2e tests.
|
|
33
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export const commonMqFontStyles = {
|
|
2
|
+
fontFamily: 'MJXZERO, MJXTEX !important',
|
|
3
|
+
'-webkit-font-smoothing': 'antialiased !important',
|
|
4
|
+
|
|
5
|
+
'& .mq-math-mode > span > var': {
|
|
6
|
+
fontFamily: 'MJXZERO, MJXTEX-I !important',
|
|
7
|
+
},
|
|
8
|
+
'& .mq-math-mode span var': {
|
|
9
|
+
fontFamily: 'MJXZERO, MJXTEX-I !important',
|
|
10
|
+
},
|
|
11
|
+
'& .mq-math-mode .mq-nonSymbola': {
|
|
12
|
+
fontFamily: 'MJXZERO, MJXTEX-I !important',
|
|
13
|
+
},
|
|
14
|
+
'& .mq-math-mode > span > var.mq-operator-name': {
|
|
15
|
+
fontFamily: 'MJXZERO, MJXTEX !important',
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const longdivStyles = {
|
|
20
|
+
'& .mq-longdiv-inner': {
|
|
21
|
+
marginTop: '-1px',
|
|
22
|
+
marginLeft: '5px !important;',
|
|
23
|
+
|
|
24
|
+
'& > .mq-empty': {
|
|
25
|
+
padding: '0 !important',
|
|
26
|
+
marginLeft: '0px !important',
|
|
27
|
+
marginTop: '2px',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
'& .mq-math-mode .mq-longdiv': {
|
|
32
|
+
display: 'inline-flex !important',
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const supsubStyles = {
|
|
37
|
+
'& .mq-math-mode sup.mq-nthroot': {
|
|
38
|
+
fontSize: '70% !important',
|
|
39
|
+
verticalAlign: '0.5em !important',
|
|
40
|
+
paddingRight: '0.15em',
|
|
41
|
+
},
|
|
42
|
+
'& .mq-math-mode .mq-supsub': {
|
|
43
|
+
fontSize: '70.7% !important',
|
|
44
|
+
},
|
|
45
|
+
'& .mq-supsub ': {
|
|
46
|
+
fontSize: '70.7%',
|
|
47
|
+
},
|
|
48
|
+
'& .mq-math-mode .mq-supsub.mq-sup-only': {
|
|
49
|
+
verticalAlign: '-0.1em !important',
|
|
50
|
+
|
|
51
|
+
'& .mq-sup': {
|
|
52
|
+
marginBottom: '0px !important',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
/* But when the base is a fraction, move it higher */
|
|
56
|
+
'& .mq-math-mode .mq-fraction + .mq-supsub.mq-sup-only': {
|
|
57
|
+
verticalAlign: '0.4em !important',
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
'& .mq-math-mode .mq-supsub.mq-sup-only.mq-after-fraction-group': {
|
|
61
|
+
verticalAlign: '0.4em !important',
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const commonMqKeyboardStyles = {
|
|
66
|
+
'& *': {
|
|
67
|
+
...commonMqFontStyles,
|
|
68
|
+
...longdivStyles,
|
|
69
|
+
'& .mq-math-mode .mq-sqrt-prefix': {
|
|
70
|
+
top: '0 !important',
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
'& .mq-math-mode .mq-empty': {
|
|
74
|
+
padding: '9px 1px !important',
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
'& .mq-math-mode .mq-longdiv-inner .mq-empty': {
|
|
78
|
+
padding: '0 !important',
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
'& .mq-math-mode .mq-supsub': {
|
|
82
|
+
fontSize: '70.7% !important',
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
'& .mq-math-mode .mq-sqrt-stem': {
|
|
86
|
+
marginTop: '-5px',
|
|
87
|
+
paddingTop: '4px',
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
'& .mq-math-mode .mq-paren': {
|
|
91
|
+
verticalAlign: 'middle !important',
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
'& .mq-math-mode .mq-overarrow .mq-overarrow-inner .mq-empty': {
|
|
95
|
+
padding: '0 !important',
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
'& .mq-math-mode .mq-overline .mq-overline-inner .mq-empty ': {
|
|
99
|
+
padding: '0 !important',
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export default {
|
|
105
|
+
commonMqFontStyles,
|
|
106
|
+
longdivStyles,
|
|
107
|
+
supsubStyles,
|
|
108
|
+
commonMqKeyboardStyles,
|
|
109
|
+
};
|
package/src/mq/index.js
ADDED
package/src/mq/input.jsx
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
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 { MQ } from './mathquill-instance';
|
|
6
|
+
|
|
7
|
+
const log = debug('math-input:mq:input');
|
|
8
|
+
|
|
9
|
+
const StyledSpan = styled('span')({
|
|
10
|
+
// No specific styles needed, but component is available for future styling
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Wrapper for MathQuill MQ.MathField.
|
|
15
|
+
*/
|
|
16
|
+
export class Input extends React.Component {
|
|
17
|
+
static propTypes = {
|
|
18
|
+
className: PropTypes.string,
|
|
19
|
+
onClick: PropTypes.func,
|
|
20
|
+
onChange: PropTypes.func,
|
|
21
|
+
latex: PropTypes.string,
|
|
22
|
+
onFocus: PropTypes.func,
|
|
23
|
+
onBlur: PropTypes.func,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
componentDidMount() {
|
|
27
|
+
if (!MQ) {
|
|
28
|
+
throw new Error('MQ is not defined - but component has mounted?');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.mathField = MQ.MathField(this.input, {
|
|
32
|
+
handlers: {
|
|
33
|
+
edit: this.onInputEdit.bind(this),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
this.updateLatex();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
componentDidUpdate() {
|
|
41
|
+
this.updateLatex();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
updateLatex() {
|
|
45
|
+
if (!this.mathField) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { latex } = this.props;
|
|
50
|
+
|
|
51
|
+
if (latex !== undefined && latex !== null) {
|
|
52
|
+
this.mathField.latex(latex);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
clear() {
|
|
57
|
+
this.mathField.latex('');
|
|
58
|
+
|
|
59
|
+
return '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
blur() {
|
|
63
|
+
log('blur mathfield');
|
|
64
|
+
this.mathField.blur();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
focus() {
|
|
68
|
+
log('focus mathfield...');
|
|
69
|
+
this.mathField.focus();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
command(v) {
|
|
73
|
+
log('command: ', v);
|
|
74
|
+
if (Array.isArray(v)) {
|
|
75
|
+
v.forEach((vv) => {
|
|
76
|
+
this.mathField.cmd(vv);
|
|
77
|
+
});
|
|
78
|
+
} else {
|
|
79
|
+
this.mathField.cmd(v);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.mathField.focus();
|
|
83
|
+
|
|
84
|
+
return this.mathField.latex();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
keystroke(v) {
|
|
88
|
+
this.mathField.keystroke(v);
|
|
89
|
+
this.mathField.focus();
|
|
90
|
+
|
|
91
|
+
return this.mathField.latex();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
write(v) {
|
|
95
|
+
log('write: ', v);
|
|
96
|
+
this.mathField.write(v);
|
|
97
|
+
this.mathField.focus();
|
|
98
|
+
|
|
99
|
+
return this.mathField.latex();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
onInputEdit = () => {
|
|
103
|
+
log('[onInputEdit] ...');
|
|
104
|
+
const { onChange } = this.props;
|
|
105
|
+
|
|
106
|
+
if (!this.mathField) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (onChange) {
|
|
111
|
+
onChange(this.mathField.latex());
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
refresh = () => {
|
|
116
|
+
this.blur();
|
|
117
|
+
this.focus();
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
onKeyPress = (event) => {
|
|
121
|
+
const keys = Object.keys(this.mathField.__controller.options);
|
|
122
|
+
|
|
123
|
+
if (keys.indexOf('ignoreNextMousedown') < 0) {
|
|
124
|
+
// It seems like the controller has the above handler as an option
|
|
125
|
+
// when all the right events are set and everything works fine
|
|
126
|
+
// this seems to work in all cases
|
|
127
|
+
this.refresh();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (event.charCode === 13) {
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
onClick = (event) => {
|
|
137
|
+
const { onClick } = this.props;
|
|
138
|
+
|
|
139
|
+
this.refresh();
|
|
140
|
+
onClick && onClick(event);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
shouldComponentUpdate(nextProps) {
|
|
144
|
+
log('next: ', nextProps.latex);
|
|
145
|
+
log('current: ', this.mathField.latex());
|
|
146
|
+
|
|
147
|
+
return nextProps.latex !== this.mathField.latex();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
render() {
|
|
151
|
+
const { onFocus, onBlur, className } = this.props;
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
<StyledSpan
|
|
155
|
+
className={className}
|
|
156
|
+
onKeyDown={this.onKeyPress}
|
|
157
|
+
onClick={this.onClick}
|
|
158
|
+
onFocus={onFocus}
|
|
159
|
+
onBlur={onBlur}
|
|
160
|
+
ref={(r) => (this.input = r)}
|
|
161
|
+
/>
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export default Input;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import MathQuill from '@pie-framework/mathquill';
|
|
2
|
+
import { registerLineBreak } from './custom-elements';
|
|
3
|
+
|
|
4
|
+
let MQ;
|
|
5
|
+
if (typeof window !== 'undefined') {
|
|
6
|
+
MQ = MathQuill.getInterface(3);
|
|
7
|
+
if (MQ && MQ.registerEmbed) {
|
|
8
|
+
registerLineBreak(MQ);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Register a MathQuill embed (e.g. answer blocks in pie-elements).
|
|
14
|
+
* No-op in SSR or if the interface is unavailable.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} name
|
|
17
|
+
* @param {Function} factory
|
|
18
|
+
*/
|
|
19
|
+
export function registerEmbed(name, factory) {
|
|
20
|
+
if (!MQ || !MQ.registerEmbed) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
MQ.registerEmbed(name, factory);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Apply MathQuill static math to a DOM element.
|
|
28
|
+
* Optionally sets element text to `latex` before enhancing (typical pie-elements usage).
|
|
29
|
+
*
|
|
30
|
+
* @param {HTMLElement} element
|
|
31
|
+
* @param {string} [latex] If provided, assigned to element.textContent before StaticMath runs.
|
|
32
|
+
* @param {object} [options] Optional MathQuill StaticMath options (e.g. handlers).
|
|
33
|
+
* @returns {object|undefined} MathQuill static math instance, if any.
|
|
34
|
+
*/
|
|
35
|
+
export function applyStaticMath(element, latex, options) {
|
|
36
|
+
if (!MQ || !element) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
if (latex !== undefined && latex !== null) {
|
|
40
|
+
element.textContent = latex;
|
|
41
|
+
}
|
|
42
|
+
return MQ.StaticMath(element, options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { MQ };
|