@pie-lib/math-toolbar 3.1.0-beta.9 → 4.0.0-next.42

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.
@@ -1,36 +0,0 @@
1
- import React from 'react';
2
-
3
- import IconButton from '@mui/material/IconButton';
4
- import Check from '@mui/icons-material/Check';
5
- import { styled } from '@mui/material/styles';
6
- import PropTypes from 'prop-types';
7
-
8
- const StyledIconButton = styled(IconButton)(({ theme, hideBackground }) => ({
9
- verticalAlign: 'top',
10
- width: '28px',
11
- height: '28px',
12
- color: '#00bb00',
13
- ...(hideBackground && {
14
- backgroundColor: theme.palette.common.white,
15
- '&:hover': {
16
- backgroundColor: theme.palette.grey[200],
17
- },
18
- }),
19
- '& .MuiIconButton-label': {
20
- position: 'absolute',
21
- top: '2px',
22
- },
23
- }));
24
-
25
- export const RawDoneButton = ({ onClick, hideBackground }) => (
26
- <StyledIconButton aria-label="Done" onClick={onClick} hideBackground={hideBackground} size="large">
27
- <Check />
28
- </StyledIconButton>
29
- );
30
-
31
- RawDoneButton.propTypes = {
32
- onClick: PropTypes.func,
33
- hideBackground: PropTypes.bool,
34
- };
35
-
36
- export const DoneButton = RawDoneButton;
@@ -1,505 +0,0 @@
1
- import React from 'react';
2
- import debug from 'debug';
3
- import PropTypes from 'prop-types';
4
- import Button from '@mui/material/Button';
5
- import { styled } from '@mui/material/styles';
6
- import MenuItem from '@mui/material/MenuItem';
7
- import Select from '@mui/material/Select';
8
- import { isEqual } from 'lodash-es';
9
- import FormControl from '@mui/material/FormControl';
10
- import InputLabel from '@mui/material/InputLabel';
11
-
12
- import { HorizontalKeypad, mq, updateSpans } from '@pie-lib/math-input-mathlive';
13
- import { color } from '@pie-lib/render-ui';
14
- import { markFractionBaseSuperscripts } from './utils';
15
-
16
- const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
17
- const log = debug('@pie-lib:math-toolbar:editor-and-pad');
18
-
19
- const decimalRegex = /\.|,/g;
20
-
21
- const MathToolbarContainer = styled('div')(({ theme }) => ({
22
- zIndex: 9,
23
- position: 'relative',
24
- textAlign: 'center',
25
- width: 'auto',
26
- '& > .mq-math-mode': {
27
- border: 'solid 1px lightgrey',
28
- },
29
- '& > .mq-focused': {
30
- outline: 'none',
31
- boxShadow: 'none',
32
- border: `dotted 1px ${theme.palette.primary.main}`,
33
- borderRadius: '0px',
34
- },
35
- '& .mq-overarrow-inner': {
36
- border: 'none !important',
37
- paddingTop: '0 !important',
38
- },
39
- '& .mq-overarrow-inner-right': {
40
- display: 'none !important',
41
- },
42
- '& .mq-overarrow-inner-left': {
43
- display: 'none !important',
44
- },
45
- '& .mq-longdiv-inner': {
46
- borderTop: '1px solid !important',
47
- paddingTop: '1.5px !important',
48
- },
49
- '& .mq-overarrow.mq-arrow-both': {
50
- top: '7.8px',
51
- marginTop: '0px',
52
- minWidth: '1.23em',
53
- },
54
- '& .mq-parallelogram': {
55
- lineHeight: 0.85,
56
- },
57
- }));
58
-
59
- const InputAndTypeContainer = styled('div')(({ theme, hide }) => ({
60
- display: hide ? 'none' : 'flex',
61
- alignItems: 'center',
62
- '& .mq-editable-field .mq-cursor': {
63
- top: '-4px',
64
- },
65
- '& .mq-math-mode .mq-selection, .mq-editable-field .mq-selection': {
66
- paddingTop: '18px',
67
- },
68
- '& .mq-math-mode .mq-overarrow': {
69
- fontFamily: 'Roboto, Helvetica, Arial, sans-serif !important',
70
- },
71
- '& .mq-math-mode .mq-overline .mq-overline-inner': {
72
- paddingTop: '0.4em !important',
73
- },
74
- '& .mq-overarrow.mq-arrow-both': {
75
- minWidth: '1.23em',
76
- '& *': {
77
- lineHeight: '1 !important',
78
- },
79
- '&:before': {
80
- top: '-0.45em',
81
- left: '-1px',
82
- },
83
- '&:after': {
84
- position: 'absolute !important',
85
- top: '0px !important',
86
- right: '-2px',
87
- },
88
- '&.mq-empty:after': {
89
- top: '-0.45em',
90
- },
91
- },
92
- '& .mq-overarrow.mq-arrow-right': {
93
- '&:before': {
94
- top: '-0.4em',
95
- right: '-1px',
96
- },
97
- },
98
- '& *': {
99
- ...commonMqFontStyles,
100
- ...supsubStyles,
101
- ...longdivStyles,
102
- '& .mq-math-mode .mq-sqrt-prefix': {
103
- verticalAlign: 'baseline !important',
104
- top: '1px !important',
105
- left: '-0.1em !important',
106
- },
107
- '& .mq-math-mode .mq-overarc ': {
108
- paddingTop: '0.45em !important',
109
- },
110
- '& .mq-math-mode .mq-empty': {
111
- padding: '9px 1px !important',
112
- },
113
- '& .mq-math-mode .mq-root-block': {
114
- paddingTop: '10px',
115
- },
116
- '& .mq-scaled .mq-sqrt-prefix': {
117
- top: '0 !important',
118
- },
119
- '& .mq-math-mode .mq-longdiv .mq-longdiv-inner': {
120
- marginLeft: '4px !important',
121
- paddingTop: '6px !important',
122
- paddingLeft: '6px !important',
123
- },
124
- '& .mq-math-mode .mq-paren': {
125
- verticalAlign: 'top !important',
126
- padding: '1px 0.1em !important',
127
- },
128
- '& .mq-math-mode .mq-sqrt-stem': {
129
- borderTop: '0.07em solid',
130
- marginLeft: '-1.5px',
131
- marginTop: '-2px !important',
132
- paddingTop: '5px !important',
133
- },
134
- '& .mq-math-mode .mq-denominator': {
135
- marginTop: '-5px !important',
136
- padding: '0.5em 0.1em 0.1em !important',
137
- },
138
- '& .mq-math-mode .mq-numerator, .mq-math-mode .mq-over': {
139
- padding: '0 0.1em !important',
140
- paddingBottom: '0 !important',
141
- marginBottom: '-2px',
142
- },
143
- },
144
- '& span[data-prime="true"]': {
145
- fontFamily: 'Roboto, Helvetica, Arial, sans-serif !important',
146
- },
147
- }));
148
-
149
- const StyledFormControl = styled(FormControl)({
150
- flex: 'initial',
151
- width: '25%',
152
- minWidth: '100px',
153
- marginLeft: '15px',
154
- marginTop: '5px',
155
- marginBottom: '5px',
156
- marginRight: '5px',
157
- '& label': {
158
- fontFamily: 'Roboto, Helvetica, Arial, sans-serif !important',
159
- },
160
- '& div': {
161
- fontFamily: 'Roboto, Helvetica, Arial, sans-serif !important',
162
- },
163
- });
164
-
165
- const StyledInputLabel = styled(InputLabel)(() => ({
166
- backgroundColor: 'transparent',
167
- }));
168
-
169
- const InputContainerDiv = styled('div')(({ theme, error }) => ({
170
- minWidth: '500px',
171
- maxWidth: '900px',
172
- minHeight: '30px',
173
- width: '100%',
174
- display: 'flex',
175
- marginTop: theme.spacing(1),
176
- marginBottom: theme.spacing(1),
177
- ...(error && {
178
- border: '2px solid red',
179
- }),
180
- '& .mq-sqrt-prefix .mq-scaled': {
181
- verticalAlign: 'middle !important',
182
- },
183
- }));
184
-
185
- const MathEditor = styled(mq.Input)(({ controlledKeypadMode }) => ({
186
- maxWidth: controlledKeypadMode ? '400px' : '500px',
187
- color: color.text(),
188
- backgroundColor: color.background(),
189
- padding: '2px',
190
- }));
191
-
192
- const AddAnswerBlockButton = styled(Button)({
193
- position: 'absolute',
194
- right: '12px',
195
- border: '1px solid lightgrey',
196
- color: color.text(),
197
- });
198
-
199
- const StyledHr = styled('hr')(({ theme }) => ({
200
- padding: 0,
201
- margin: 0,
202
- height: '1px',
203
- border: 'none',
204
- borderBottom: `solid 1px ${theme.palette.primary.main}`,
205
- }));
206
-
207
- const KeyboardContainer = styled(HorizontalKeypad)(({ mode }) => ({
208
- ...commonMqKeyboardStyles,
209
- ...(mode === 'language' && {
210
- '& *': {
211
- fontFamily: 'Roboto, Helvetica, Arial, sans-serif !important',
212
- },
213
- }),
214
- }));
215
-
216
- const toNodeData = (data) => {
217
- if (!data) {
218
- return;
219
- }
220
-
221
- const { type, value } = data;
222
-
223
- if (type === 'command' || type === 'cursor') {
224
- return data;
225
- } else if (type === 'answer') {
226
- return { type: 'answer', ...data };
227
- } else if (value === 'clear') {
228
- return { type: 'clear' };
229
- } else {
230
- return { type: 'write', value };
231
- }
232
- };
233
-
234
- export class EditorAndPad extends React.Component {
235
- static propTypes = {
236
- classNames: PropTypes.object,
237
- keypadMode: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
238
- autoFocus: PropTypes.bool,
239
- allowAnswerBlock: PropTypes.bool,
240
- showKeypad: PropTypes.bool,
241
- controlledKeypad: PropTypes.bool,
242
- controlledKeypadMode: PropTypes.bool,
243
- error: PropTypes.string,
244
- noDecimal: PropTypes.bool,
245
- hideInput: PropTypes.bool,
246
- noLatexHandling: PropTypes.bool,
247
- layoutForKeyPad: PropTypes.object,
248
- maxResponseAreas: PropTypes.number,
249
- additionalKeys: PropTypes.array,
250
- latex: PropTypes.string.isRequired,
251
- onAnswerBlockAdd: PropTypes.func,
252
- onFocus: PropTypes.func,
253
- onBlur: PropTypes.func,
254
- onChange: PropTypes.func.isRequired,
255
- setKeypadInteraction: PropTypes.func,
256
- };
257
-
258
- constructor(props) {
259
- super(props);
260
-
261
- this.state = { equationEditor: 'item-authoring', addDisabled: false };
262
- }
263
-
264
- componentDidMount() {
265
- if (this.input && this.props.autoFocus) {
266
- // adding a timeout to wait for other stuff related to focus to be finished
267
- setTimeout(() => this.input.focus(), 0);
268
- }
269
- }
270
-
271
- onClick = (data) => {
272
- const { noDecimal, noLatexHandling, onChange } = this.props;
273
- const c = toNodeData(data);
274
- log('mathChange: ', c);
275
-
276
- if (noLatexHandling) {
277
- onChange(c.value);
278
- return;
279
- }
280
-
281
- // if decimals are not allowed for this response, we discard the input
282
- if (noDecimal && (c.value === '.' || c.value === ',')) {
283
- return;
284
- }
285
-
286
- if (!c) {
287
- return;
288
- }
289
-
290
- if (c.type === 'clear') {
291
- log('call clear...');
292
- this.input.clear();
293
- } else if (c.type === 'command') {
294
- this.input.command(c.value);
295
- } else if (c.type === 'cursor') {
296
- this.input.keystroke(c.value);
297
- } else if (c.type === 'answer') {
298
- this.input.write('%response%');
299
- } else {
300
- this.input.write(c.value);
301
- }
302
- };
303
-
304
- updateDisable = (isEdit) => {
305
- const { maxResponseAreas } = this.props;
306
-
307
- if (maxResponseAreas) {
308
- const shouldDisable = this.checkResponseAreasNumber(maxResponseAreas, isEdit);
309
-
310
- this.setState({ addDisabled: shouldDisable });
311
- }
312
- };
313
-
314
- onAnswerBlockClick = () => {
315
- this.props.onAnswerBlockAdd();
316
- this.onClick({
317
- type: 'answer',
318
- });
319
-
320
- this.updateDisable(true);
321
- };
322
-
323
- onEditorChange = (latex) => {
324
- const { onChange, noDecimal } = this.props;
325
-
326
- updateSpans();
327
- markFractionBaseSuperscripts();
328
-
329
- this.updateDisable(true);
330
-
331
- // if no decimals are allowed and the last change is a decimal dot, discard the change
332
- if (noDecimal && (latex.indexOf('.') !== -1 || latex.indexOf(',') !== -1) && this.input) {
333
- this.input.clear();
334
- this.input.write(latex.replace(decimalRegex, ''));
335
- return;
336
- }
337
-
338
- // eslint-disable-next-line no-useless-escape
339
- const regexMatch = latex.match(/[0-9]\\ \\frac\{[^\{]*\}\{ \}/);
340
-
341
- if (this.input && regexMatch && regexMatch?.length) {
342
- try {
343
- // Undo the empty fraction the editor just created after a digit.
344
- // MathQuill needed cursor surgery via `__controller`; MathLive has no
345
- // such internals, so use the public delete command. `keystroke` accepts
346
- // the MathQuill name and maps it to the engine's selector.
347
- this.input.keystroke('Backspace');
348
- } catch (e) {
349
- // eslint-disable-next-line no-console
350
- console.error(e.toString());
351
- }
352
-
353
- return;
354
- }
355
-
356
- onChange(latex);
357
- };
358
-
359
- /** Only render if the editor's latex is different
360
- * or the keypad state changed from one state to the other (shown / hidden) */
361
- shouldComponentUpdate(nextProps, nextState) {
362
- // `this.input` is set via a ref, and the underlying math engine loads
363
- // asynchronously, so neither is guaranteed to exist on early renders.
364
- // Treat "not ready" as different, so the first real render is not skipped.
365
- const inputIsDifferent = !this.input || this.input.latex() !== nextProps.latex;
366
- log('[shouldComponentUpdate] ', 'inputIsDifferent: ', inputIsDifferent);
367
-
368
- if (!isEqual(this.props.error, nextProps.error)) {
369
- return true;
370
- }
371
-
372
- if (!inputIsDifferent && this.props.keypadMode !== nextProps.keypadMode) {
373
- return true;
374
- }
375
-
376
- if (!inputIsDifferent && this.props.noDecimal !== nextProps.noDecimal) {
377
- return true;
378
- }
379
-
380
- if (!inputIsDifferent && this.state.equationEditor !== nextState.equationEditor) {
381
- return true;
382
- }
383
-
384
- if (!inputIsDifferent && this.props.controlledKeypad) {
385
- return this.props.showKeypad !== nextProps.showKeypad;
386
- }
387
-
388
- return inputIsDifferent;
389
- }
390
-
391
- onEditorTypeChange = (evt) => {
392
- this.setState({ equationEditor: evt.target.value });
393
- };
394
-
395
- checkResponseAreasNumber = (maxResponseAreas, isEdit) => {
396
- const { latex } = (this.input && this.input.props) || {};
397
-
398
- if (latex) {
399
- const count = (latex.match(/answerBlock/g) || []).length;
400
-
401
- return isEdit ? count === maxResponseAreas - 1 : count === maxResponseAreas;
402
- }
403
-
404
- return false;
405
- };
406
-
407
- render() {
408
- const {
409
- classNames,
410
- keypadMode,
411
- allowAnswerBlock,
412
- additionalKeys,
413
- controlledKeypad,
414
- controlledKeypadMode,
415
- showKeypad,
416
- setKeypadInteraction,
417
- noDecimal,
418
- hideInput,
419
- layoutForKeyPad,
420
- latex,
421
- onFocus,
422
- onBlur,
423
- error,
424
- } = this.props;
425
- const shouldShowKeypad = !controlledKeypad || (controlledKeypad && showKeypad);
426
- const { addDisabled } = this.state;
427
-
428
- log('[render]', latex);
429
-
430
- return (
431
- <MathToolbarContainer className={classNames.mathToolbar}>
432
- <InputAndTypeContainer hide={hideInput}>
433
- {controlledKeypadMode && (
434
- <StyledFormControl variant={'standard'}>
435
- <StyledInputLabel id="equation-editor-label">{'Equation Editor'}</StyledInputLabel>
436
- <Select
437
- labelId="equation-editor-label"
438
- id="equation-editor-select"
439
- name="equationEditor"
440
- label={'Equation Editor'}
441
- onChange={this.onEditorTypeChange}
442
- value={this.state.equationEditor}
443
- MenuProps={{ transitionDuration: { enter: 225, exit: 195 } }}
444
- >
445
- <MenuItem value="non-negative-integers">Numeric - Non-Negative Integers</MenuItem>
446
- <MenuItem value="integers">Numeric - Integers</MenuItem>
447
- <MenuItem value="decimals">Numeric - Decimals</MenuItem>
448
- <MenuItem value="fractions">Numeric - Fractions</MenuItem>
449
- <MenuItem value={1}>Grade 1 - 2</MenuItem>
450
- <MenuItem value={3}>Grade 3 - 5</MenuItem>
451
- <MenuItem value={6}>Grade 6 - 7</MenuItem>
452
- <MenuItem value={8}>Grade 8 - HS</MenuItem>
453
- <MenuItem value={'geometry'}>Geometry</MenuItem>
454
- <MenuItem value={'advanced-algebra'}>Advanced Algebra</MenuItem>
455
- <MenuItem value={'statistics'}>Statistics</MenuItem>
456
- <MenuItem value={'item-authoring'}>Item Authoring</MenuItem>
457
- </Select>
458
- </StyledFormControl>
459
- )}
460
- <InputContainerDiv error={error}>
461
- <MathEditor
462
- onFocus={() => {
463
- onFocus && onFocus();
464
- this.updateDisable(false);
465
- }}
466
- onBlur={(event) => {
467
- this.updateDisable(false);
468
- onBlur && onBlur(event);
469
- }}
470
- className={(classNames && classNames.editor) || ''}
471
- controlledKeypadMode={controlledKeypadMode}
472
- ref={(r) => (this.input = r)}
473
- latex={latex}
474
- onChange={this.onEditorChange}
475
- />
476
- </InputContainerDiv>
477
- </InputAndTypeContainer>
478
- {allowAnswerBlock && (
479
- <AddAnswerBlockButton
480
- type="primary"
481
- style={{ bottom: shouldShowKeypad ? '320px' : '20px' }}
482
- onClick={this.onAnswerBlockClick}
483
- disabled={addDisabled}
484
- >
485
- + Response Area
486
- </AddAnswerBlockButton>
487
- )}
488
- <StyledHr />
489
- {shouldShowKeypad && (
490
- <KeyboardContainer
491
- mode={controlledKeypadMode ? this.state.equationEditor : keypadMode}
492
- controlledKeypadMode={controlledKeypadMode}
493
- layoutForKeyPad={layoutForKeyPad}
494
- additionalKeys={additionalKeys}
495
- onClick={this.onClick}
496
- noDecimal={noDecimal}
497
- setKeypadInteraction={setKeypadInteraction}
498
- />
499
- )}
500
- </MathToolbarContainer>
501
- );
502
- }
503
- }
504
-
505
- export default EditorAndPad;