@pie-lib/charting 5.7.3-next.17 → 5.7.3-next.21

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/lib/mark-label.js CHANGED
@@ -29,6 +29,8 @@ var _styles2 = require("./common/styles");
29
29
 
30
30
  var _renderUi = require("@pie-lib/render-ui");
31
31
 
32
+ var _mathRendering = require("@pie-lib/math-rendering");
33
+
32
34
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
33
35
 
34
36
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -53,11 +55,60 @@ var styles = function styles(theme) {
53
55
  '&.disabled': _objectSpread(_objectSpread({}, (0, _styles2.disabled)('color')), {}, {
54
56
  backgroundColor: 'transparent !important'
55
57
  })
56
- }
58
+ },
59
+ mathInput: {
60
+ pointerEvents: 'auto',
61
+ textAlign: 'center',
62
+ fontSize: theme.typography.fontSize,
63
+ fontFamily: theme.typography.fontFamily,
64
+ color: _renderUi.color.primaryDark()
65
+ },
66
+ disabled: _objectSpread(_objectSpread({}, (0, _styles2.disabled)('color')), {}, {
67
+ backgroundColor: 'transparent !important'
68
+ }),
69
+ error: {
70
+ border: "2px solid ".concat(theme.palette.error.main)
71
+ },
72
+ correct: _objectSpread({}, (0, _styles2.correct)('color')),
73
+ incorrect: _objectSpread({}, (0, _styles2.incorrect)('color'))
57
74
  };
58
75
  };
59
76
 
77
+ function isFractionFormat(label) {
78
+ var trimmedLabel = (label === null || label === void 0 ? void 0 : label.trim()) || '';
79
+ var fracRegex = new RegExp(/^[1-9]*[0-9]*\s?[1-9][0-9]*\/[1-9][0-9]*$/);
80
+ return fracRegex.test(trimmedLabel);
81
+ }
82
+
83
+ function getLabelMathFormat(label) {
84
+ var trimmedLabel = (label === null || label === void 0 ? void 0 : label.trim()) || '';
85
+ var fraction;
86
+ var mixedNr = '';
87
+ var improperFraction = trimmedLabel.split(' ');
88
+
89
+ if (improperFraction[1] && improperFraction[1].includes('/')) {
90
+ fraction = improperFraction[1].split('/') || '';
91
+ } else {
92
+ fraction = (trimmedLabel === null || trimmedLabel === void 0 ? void 0 : trimmedLabel.split('/')) || '';
93
+ }
94
+
95
+ var formattedLLabel;
96
+
97
+ if (isFractionFormat(label)) {
98
+ if (improperFraction[0] && improperFraction[1]) {
99
+ mixedNr = improperFraction[0];
100
+ }
101
+
102
+ formattedLLabel = "\\(".concat(mixedNr, "\\frac{").concat(fraction[0], "}{").concat(fraction[1], "}\\)");
103
+ return formattedLLabel;
104
+ }
105
+
106
+ return undefined;
107
+ }
108
+
60
109
  var MarkLabel = function MarkLabel(props) {
110
+ var _classNames;
111
+
61
112
  // eslint-disable-next-line no-unused-vars
62
113
  var _useState = (0, _react.useState)(null),
63
114
  _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
@@ -83,12 +134,30 @@ var MarkLabel = function MarkLabel(props) {
83
134
  label = _useState4[0],
84
135
  setLabel = _useState4[1];
85
136
 
137
+ var _useState5 = (0, _react.useState)(getLabelMathFormat(mark.label)),
138
+ _useState6 = (0, _slicedToArray2["default"])(_useState5, 2),
139
+ mathLabel = _useState6[0],
140
+ setMathLabel = _useState6[1];
141
+
142
+ var _useState7 = (0, _react.useState)(false),
143
+ _useState8 = (0, _slicedToArray2["default"])(_useState7, 2),
144
+ isEditing = _useState8[0],
145
+ setIsEditing = _useState8[1];
146
+
147
+ var root = (0, _react.useRef)(null);
148
+
86
149
  var onChange = function onChange(e) {
87
- return setLabel(e.target.value);
150
+ setLabel(e.target.value);
151
+ };
152
+
153
+ var isMathRendering = function isMathRendering() {
154
+ return isEditing === false && mathLabel !== undefined;
88
155
  };
89
156
 
90
157
  var onChangeProp = function onChangeProp(e) {
91
- return props.onChange(e.target.value);
158
+ setMathLabel(getLabelMathFormat(mark.label));
159
+ setIsEditing(false);
160
+ props.onChange(e.target.value);
92
161
  };
93
162
 
94
163
  var extraStyle = {};
@@ -104,13 +173,30 @@ var MarkLabel = function MarkLabel(props) {
104
173
  (0, _react.useEffect)(function () {
105
174
  setLabel(mark.label);
106
175
  }, [mark.label]);
107
- return /*#__PURE__*/_react["default"].createElement(_reactInputAutosize["default"], {
108
- autoFocus: autoFocus,
176
+ (0, _react.useEffect)(function () {
177
+ (0, _mathRendering.renderMath)(root);
178
+ }, []);
179
+ return isMathRendering() ? /*#__PURE__*/_react["default"].createElement("div", {
180
+ ref: function ref(r) {
181
+ return root = r;
182
+ },
183
+ dangerouslySetInnerHTML: {
184
+ __html: getLabelMathFormat(label)
185
+ },
186
+ className: (0, _classnames["default"])(classes.mathInput, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, classes.disabled, disabled), (0, _defineProperty2["default"])(_classNames, classes.error, error), (0, _defineProperty2["default"])(_classNames, classes.correct, correctness && correctness.label === 'correct'), (0, _defineProperty2["default"])(_classNames, classes.incorrect, correctness && correctness.label === 'incorrect'), _classNames)),
187
+ onClick: function onClick() {
188
+ return setIsEditing(true);
189
+ },
190
+ style: {
191
+ minWidth: barWidth
192
+ }
193
+ }) : /*#__PURE__*/_react["default"].createElement(_reactInputAutosize["default"], {
109
194
  inputRef: function inputRef(r) {
110
195
  _ref(r);
111
196
 
112
197
  externalInputRef(r);
113
198
  },
199
+ autoFocus: isEditing || autoFocus,
114
200
  disabled: disabled,
115
201
  inputClassName: (0, _classnames["default"])(classes.input, correctness && correctness.label, disabled && 'disabled', error && 'error'),
116
202
  inputStyle: _objectSpread({
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mark-label.jsx"],"names":["styles","theme","input","fontFamily","typography","fontSize","border","color","primaryDark","palette","error","main","backgroundColor","MarkLabel","props","setInput","_ref","node","mark","classes","disabled","externalInputRef","inputRef","barWidth","rotate","correctness","autoFocus","label","setLabel","onChange","e","target","value","onChangeProp","extraStyle","width","textAlign","r","minWidth","background","boxSizing","paddingLeft","paddingRight","position","pointerEvents","top","left","transformOrigin","transform","propTypes","PropTypes","bool","any","func","graphProps","types","GraphPropsType","object","number","shape","string"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;AAEA,IAAMA,MAAM,GAAG,SAATA,MAAS,CAACC,KAAD;AAAA,SAAY;AACzBC,IAAAA,KAAK,EAAE;AACL,eAAO,OADF;AAELC,MAAAA,UAAU,EAAEF,KAAK,CAACG,UAAN,CAAiBD,UAFxB;AAGLE,MAAAA,QAAQ,EAAEJ,KAAK,CAACG,UAAN,CAAiBC,QAHtB;AAILC,MAAAA,MAAM,EAAE,MAJH;AAKLC,MAAAA,KAAK,EAAEA,gBAAMC,WAAN,EALF;AAML,iBAAW;AAAEF,QAAAA,MAAM,sBAAeL,KAAK,CAACQ,OAAN,CAAcC,KAAd,CAAoBC,IAAnC;AAAR,OANN;AAOL,mBAAa,sBAAQ,OAAR,CAPR;AAQL,qBAAe,wBAAU,OAAV,CARV;AASL,oDACK,uBAAS,OAAT,CADL;AAEEC,QAAAA,eAAe,EAAE;AAFnB;AATK;AADkB,GAAZ;AAAA,CAAf;;AAiBO,IAAMC,SAAS,GAAG,SAAZA,SAAY,CAACC,KAAD,EAAW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AAAA,QAAQ,EAAEqB,sBAAUG,IAJA;AAKpBC,EAAAA,UAAU,EAAEC,YAAMC,cALE;AAMpBrC,EAAAA,OAAO,EAAE+B,sBAAUO,MANC;AAOpBnC,EAAAA,QAAQ,EAAE4B,sBAAUG,IAPA;AAQpBnC,EAAAA,IAAI,EAAEgC,sBAAUO,MARI;AASpBlC,EAAAA,QAAQ,EAAE2B,sBAAUQ,MATA;AAUpBlC,EAAAA,MAAM,EAAE0B,sBAAUQ,MAVE;AAWpBjC,EAAAA,WAAW,EAAEyB,sBAAUS,KAAV,CAAgB;AAC3B3B,IAAAA,KAAK,EAAEkB,sBAAUU,MADU;AAE3BjC,IAAAA,KAAK,EAAEuB,sBAAUU;AAFU,GAAhB;AAXO,CAAtB;;eAiBe,wBAAW5D,MAAX,EAAmBa,SAAnB,C","sourcesContent":["import React, { useState, useCallback, useEffect } from 'react';\nimport cn from 'classnames';\nimport { withStyles } from '@material-ui/core/styles';\nimport AutosizeInput from 'react-input-autosize';\nimport PropTypes from 'prop-types';\nimport { types } from '@pie-lib/plot';\nimport { correct, incorrect, disabled } from './common/styles';\nimport { color } from '@pie-lib/render-ui';\n\nconst styles = (theme) => ({\n input: {\n float: 'right',\n fontFamily: theme.typography.fontFamily,\n fontSize: theme.typography.fontSize,\n border: 'none',\n color: color.primaryDark(),\n '&.error': { border: `2px solid ${theme.palette.error.main}` },\n '&.correct': correct('color'),\n '&.incorrect': incorrect('color'),\n '&.disabled': {\n ...disabled('color'),\n backgroundColor: 'transparent !important',\n },\n },\n});\n\nexport const MarkLabel = (props) => {\n // eslint-disable-next-line no-unused-vars\n const [input, setInput] = useState(null);\n const _ref = useCallback((node) => setInput(node), null);\n\n const {\n mark,\n classes,\n disabled,\n inputRef: externalInputRef,\n barWidth,\n rotate,\n correctness,\n autoFocus,\n error,\n } = props;\n const [label, setLabel] = useState(mark.label);\n const onChange = (e) => setLabel(e.target.value);\n const onChangeProp = (e) => props.onChange(e.target.value);\n let extraStyle = {};\n\n if (rotate) {\n extraStyle = {\n width: 'unset',\n textAlign: 'left',\n };\n }\n\n // useState only sets the value once, to synch props to state need useEffect\n useEffect(() => {\n setLabel(mark.label);\n }, [mark.label]);\n\n return (\n <AutosizeInput\n autoFocus={autoFocus}\n inputRef={(r) => {\n _ref(r);\n externalInputRef(r);\n }}\n disabled={disabled}\n inputClassName={cn(classes.input, correctness && correctness.label, disabled && 'disabled', error && 'error')}\n inputStyle={{\n minWidth: barWidth,\n textAlign: 'center',\n background: 'transparent',\n boxSizing: 'border-box',\n paddingLeft: 0,\n paddingRight: 0,\n ...extraStyle,\n }}\n value={label}\n style={{\n position: 'fixed',\n pointerEvents: 'auto',\n top: 0,\n left: 0,\n minWidth: barWidth,\n transformOrigin: 'left',\n transform: `rotate(${rotate}deg)`,\n }}\n onChange={onChange}\n onBlur={onChangeProp}\n />\n );\n};\n\nMarkLabel.propTypes = {\n autoFocus: PropTypes.bool,\n disabled: PropTypes.bool,\n error: PropTypes.any,\n onChange: PropTypes.func,\n graphProps: types.GraphPropsType,\n classes: PropTypes.object,\n inputRef: PropTypes.func,\n mark: PropTypes.object,\n barWidth: PropTypes.number,\n rotate: PropTypes.number,\n correctness: PropTypes.shape({\n value: PropTypes.string,\n label: PropTypes.string,\n }),\n};\n\nexport default withStyles(styles)(MarkLabel);\n"],"file":"mark-label.js"}
1
+ {"version":3,"sources":["../src/mark-label.jsx"],"names":["styles","theme","input","fontFamily","typography","fontSize","border","color","primaryDark","palette","error","main","backgroundColor","mathInput","pointerEvents","textAlign","disabled","correct","incorrect","isFractionFormat","label","trimmedLabel","trim","fracRegex","RegExp","test","getLabelMathFormat","fraction","mixedNr","improperFraction","split","includes","formattedLLabel","undefined","MarkLabel","props","setInput","_ref","node","mark","classes","externalInputRef","inputRef","barWidth","rotate","correctness","autoFocus","setLabel","mathLabel","setMathLabel","isEditing","setIsEditing","root","onChange","e","target","value","isMathRendering","onChangeProp","extraStyle","width","r","__html","minWidth","background","boxSizing","paddingLeft","paddingRight","position","top","left","transformOrigin","transform","propTypes","PropTypes","bool","any","func","graphProps","types","GraphPropsType","object","number","shape","string"],"mappings":";;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;AAEA,IAAMA,MAAM,GAAG,SAATA,MAAS,CAACC,KAAD;AAAA,SAAY;AACzBC,IAAAA,KAAK,EAAE;AACL,eAAO,OADF;AAELC,MAAAA,UAAU,EAAEF,KAAK,CAACG,UAAN,CAAiBD,UAFxB;AAGLE,MAAAA,QAAQ,EAAEJ,KAAK,CAACG,UAAN,CAAiBC,QAHtB;AAILC,MAAAA,MAAM,EAAE,MAJH;AAKLC,MAAAA,KAAK,EAAEA,gBAAMC,WAAN,EALF;AAML,iBAAW;AAAEF,QAAAA,MAAM,sBAAeL,KAAK,CAACQ,OAAN,CAAcC,KAAd,CAAoBC,IAAnC;AAAR,OANN;AAOL,mBAAa,sBAAQ,OAAR,CAPR;AAQL,qBAAe,wBAAU,OAAV,CARV;AASL,oDACK,uBAAS,OAAT,CADL;AAEEC,QAAAA,eAAe,EAAE;AAFnB;AATK,KADkB;AAezBC,IAAAA,SAAS,EAAE;AACTC,MAAAA,aAAa,EAAE,MADN;AAETC,MAAAA,SAAS,EAAE,QAFF;AAGTV,MAAAA,QAAQ,EAAEJ,KAAK,CAACG,UAAN,CAAiBC,QAHlB;AAITF,MAAAA,UAAU,EAAEF,KAAK,CAACG,UAAN,CAAiBD,UAJpB;AAKTI,MAAAA,KAAK,EAAEA,gBAAMC,WAAN;AALE,KAfc;AAsBzBQ,IAAAA,QAAQ,kCACH,uBAAS,OAAT,CADG;AAENJ,MAAAA,eAAe,EAAE;AAFX,MAtBiB;AA0BzBF,IAAAA,KAAK,EAAE;AACLJ,MAAAA,MAAM,sBAAeL,KAAK,CAACQ,OAAN,CAAcC,KAAd,CAAoBC,IAAnC;AADD,KA1BkB;AA6BzBM,IAAAA,OAAO,oBACF,sBAAQ,OAAR,CADE,CA7B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M;;;AAyGPxB,SAAS,CAACuC,SAAV,GAAsB;AACpB3B,EAAAA,SAAS,EAAE4B,sBAAUC,IADD;AAEpB3D,EAAAA,QAAQ,EAAE0D,sBAAUC,IAFA;AAGpBjE,EAAAA,KAAK,EAAEgE,sBAAUE,GAHG;AAIpBvB,EAAAA,QAAQ,EAAEqB,sBAAUG,IAJA;AAKpBC,EAAAA,UAAU,EAAEC,YAAMC,cALE;AAMpBxC,EAAAA,OAAO,EAAEkC,sBAAUO,MANC;AAOpBvC,EAAAA,QAAQ,EAAEgC,sBAAUG,IAPA;AAQpBtC,EAAAA,IAAI,EAAEmC,sBAAUO,MARI;AASpBtC,EAAAA,QAAQ,EAAE+B,sBAAUQ,MATA;AAUpBtC,EAAAA,MAAM,EAAE8B,sBAAUQ,MAVE;AAWpBrC,EAAAA,WAAW,EAAE6B,sBAAUS,KAAV,CAAgB;AAC3B3B,IAAAA,KAAK,EAAEkB,sBAAUU,MADU;AAE3BhE,IAAAA,KAAK,EAAEsD,sBAAUU;AAFU,GAAhB;AAXO,CAAtB;;eAiBe,wBAAWpF,MAAX,EAAmBkC,SAAnB,C","sourcesContent":["import React, { useState, useCallback, useEffect, useRef } from 'react';\nimport classNames from 'classnames';\nimport { withStyles } from '@material-ui/core/styles';\nimport AutosizeInput from 'react-input-autosize';\nimport PropTypes from 'prop-types';\nimport { types } from '@pie-lib/plot';\nimport { correct, incorrect, disabled } from './common/styles';\nimport { color } from '@pie-lib/render-ui';\nimport { renderMath } from '@pie-lib/math-rendering';\n\nconst styles = (theme) => ({\n input: {\n float: 'right',\n fontFamily: theme.typography.fontFamily,\n fontSize: theme.typography.fontSize,\n border: 'none',\n color: color.primaryDark(),\n '&.error': { border: `2px solid ${theme.palette.error.main}` },\n '&.correct': correct('color'),\n '&.incorrect': incorrect('color'),\n '&.disabled': {\n ...disabled('color'),\n backgroundColor: 'transparent !important',\n },\n },\n mathInput: {\n pointerEvents: 'auto',\n textAlign: 'center',\n fontSize: theme.typography.fontSize,\n fontFamily: theme.typography.fontFamily,\n color: color.primaryDark(),\n },\n disabled: {\n ...disabled('color'),\n backgroundColor: 'transparent !important',\n },\n error: {\n border: `2px solid ${theme.palette.error.main}`,\n },\n correct: {\n ...correct('color'),\n },\n incorrect: {\n ...incorrect('color'),\n },\n});\n\nfunction isFractionFormat(label) {\n const trimmedLabel = label?.trim() || '';\n const fracRegex = new RegExp(/^[1-9]*[0-9]*\\s?[1-9][0-9]*\\/[1-9][0-9]*$/);\n return fracRegex.test(trimmedLabel);\n}\n\nfunction getLabelMathFormat(label) {\n const trimmedLabel = label?.trim() || '';\n let fraction;\n let mixedNr = '';\n let improperFraction = trimmedLabel.split(' ');\n if (improperFraction[1] && improperFraction[1].includes('/')) {\n fraction = improperFraction[1].split('/') || '';\n } else {\n fraction = trimmedLabel?.split('/') || '';\n }\n\n let formattedLLabel;\n if (isFractionFormat(label)) {\n if (improperFraction[0] && improperFraction[1]) {\n mixedNr = improperFraction[0];\n }\n formattedLLabel = `\\\\(${mixedNr}\\\\frac{${fraction[0]}}{${fraction[1]}}\\\\)`;\n return formattedLLabel;\n }\n return undefined;\n}\n\nexport const MarkLabel = (props) => {\n // eslint-disable-next-line no-unused-vars\n const [input, setInput] = useState(null);\n const _ref = useCallback((node) => setInput(node), null);\n\n const {\n mark,\n classes,\n disabled,\n inputRef: externalInputRef,\n barWidth,\n rotate,\n correctness,\n autoFocus,\n error,\n } = props;\n\n const [label, setLabel] = useState(mark.label);\n const [mathLabel, setMathLabel] = useState(getLabelMathFormat(mark.label));\n const [isEditing, setIsEditing] = useState(false);\n let root = useRef(null);\n\n const onChange = (e) => {\n setLabel(e.target.value);\n };\n\n const isMathRendering = () => {\n return isEditing === false && mathLabel !== undefined;\n };\n\n const onChangeProp = (e) => {\n setMathLabel(getLabelMathFormat(mark.label));\n setIsEditing(false);\n props.onChange(e.target.value);\n };\n let extraStyle = {};\n\n if (rotate) {\n extraStyle = {\n width: 'unset',\n textAlign: 'left',\n };\n }\n\n // useState only sets the value once, to synch props to state need useEffect\n useEffect(() => {\n setLabel(mark.label);\n }, [mark.label]);\n\n useEffect(() => {\n renderMath(root);\n }, []);\n\n return isMathRendering() ? (\n <div\n ref={(r) => (root = r)}\n dangerouslySetInnerHTML={{ __html: getLabelMathFormat(label) }}\n className={classNames(classes.mathInput, {\n [classes.disabled]: disabled,\n [classes.error]: error,\n [classes.correct]: correctness && correctness.label === 'correct',\n [classes.incorrect]: correctness && correctness.label === 'incorrect',\n })}\n onClick={() => setIsEditing(true)}\n style={{ minWidth: barWidth }}\n ></div>\n ) : (\n <AutosizeInput\n inputRef={(r) => {\n _ref(r);\n externalInputRef(r);\n }}\n autoFocus={isEditing || autoFocus}\n disabled={disabled}\n inputClassName={classNames(\n classes.input,\n correctness && correctness.label,\n disabled && 'disabled',\n error && 'error',\n )}\n inputStyle={{\n minWidth: barWidth,\n textAlign: 'center',\n background: 'transparent',\n boxSizing: 'border-box',\n paddingLeft: 0,\n paddingRight: 0,\n ...extraStyle,\n }}\n value={label}\n style={{\n position: 'fixed',\n pointerEvents: 'auto',\n top: 0,\n left: 0,\n minWidth: barWidth,\n transformOrigin: 'left',\n transform: `rotate(${rotate}deg)`,\n }}\n onChange={onChange}\n onBlur={onChangeProp}\n />\n );\n};\n\nMarkLabel.propTypes = {\n autoFocus: PropTypes.bool,\n disabled: PropTypes.bool,\n error: PropTypes.any,\n onChange: PropTypes.func,\n graphProps: types.GraphPropsType,\n classes: PropTypes.object,\n inputRef: PropTypes.func,\n mark: PropTypes.object,\n barWidth: PropTypes.number,\n rotate: PropTypes.number,\n correctness: PropTypes.shape({\n value: PropTypes.string,\n label: PropTypes.string,\n }),\n};\n\nexport default withStyles(styles)(MarkLabel);\n"],"file":"mark-label.js"}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "5.7.3-next.17+6704cda1",
6
+ "version": "5.7.3-next.21+663190b6",
7
7
  "description": "charting",
8
8
  "keywords": [
9
9
  "react",
@@ -43,5 +43,5 @@
43
43
  "peerDependencies": {
44
44
  "react": "^16.8.1"
45
45
  },
46
- "gitHead": "6704cda1fabe2c1348a875a2dc08e2b28d77ccf3"
46
+ "gitHead": "663190b6cf7f70836adf673e99a69317a5520511"
47
47
  }
@@ -1,11 +1,12 @@
1
- import React, { useState, useCallback, useEffect } from 'react';
2
- import cn from 'classnames';
1
+ import React, { useState, useCallback, useEffect, useRef } from 'react';
2
+ import classNames from 'classnames';
3
3
  import { withStyles } from '@material-ui/core/styles';
4
4
  import AutosizeInput from 'react-input-autosize';
5
5
  import PropTypes from 'prop-types';
6
6
  import { types } from '@pie-lib/plot';
7
7
  import { correct, incorrect, disabled } from './common/styles';
8
8
  import { color } from '@pie-lib/render-ui';
9
+ import { renderMath } from '@pie-lib/math-rendering';
9
10
 
10
11
  const styles = (theme) => ({
11
12
  input: {
@@ -22,8 +23,56 @@ const styles = (theme) => ({
22
23
  backgroundColor: 'transparent !important',
23
24
  },
24
25
  },
26
+ mathInput: {
27
+ pointerEvents: 'auto',
28
+ textAlign: 'center',
29
+ fontSize: theme.typography.fontSize,
30
+ fontFamily: theme.typography.fontFamily,
31
+ color: color.primaryDark(),
32
+ },
33
+ disabled: {
34
+ ...disabled('color'),
35
+ backgroundColor: 'transparent !important',
36
+ },
37
+ error: {
38
+ border: `2px solid ${theme.palette.error.main}`,
39
+ },
40
+ correct: {
41
+ ...correct('color'),
42
+ },
43
+ incorrect: {
44
+ ...incorrect('color'),
45
+ },
25
46
  });
26
47
 
48
+ function isFractionFormat(label) {
49
+ const trimmedLabel = label?.trim() || '';
50
+ const fracRegex = new RegExp(/^[1-9]*[0-9]*\s?[1-9][0-9]*\/[1-9][0-9]*$/);
51
+ return fracRegex.test(trimmedLabel);
52
+ }
53
+
54
+ function getLabelMathFormat(label) {
55
+ const trimmedLabel = label?.trim() || '';
56
+ let fraction;
57
+ let mixedNr = '';
58
+ let improperFraction = trimmedLabel.split(' ');
59
+ if (improperFraction[1] && improperFraction[1].includes('/')) {
60
+ fraction = improperFraction[1].split('/') || '';
61
+ } else {
62
+ fraction = trimmedLabel?.split('/') || '';
63
+ }
64
+
65
+ let formattedLLabel;
66
+ if (isFractionFormat(label)) {
67
+ if (improperFraction[0] && improperFraction[1]) {
68
+ mixedNr = improperFraction[0];
69
+ }
70
+ formattedLLabel = `\\(${mixedNr}\\frac{${fraction[0]}}{${fraction[1]}}\\)`;
71
+ return formattedLLabel;
72
+ }
73
+ return undefined;
74
+ }
75
+
27
76
  export const MarkLabel = (props) => {
28
77
  // eslint-disable-next-line no-unused-vars
29
78
  const [input, setInput] = useState(null);
@@ -40,9 +89,25 @@ export const MarkLabel = (props) => {
40
89
  autoFocus,
41
90
  error,
42
91
  } = props;
92
+
43
93
  const [label, setLabel] = useState(mark.label);
44
- const onChange = (e) => setLabel(e.target.value);
45
- const onChangeProp = (e) => props.onChange(e.target.value);
94
+ const [mathLabel, setMathLabel] = useState(getLabelMathFormat(mark.label));
95
+ const [isEditing, setIsEditing] = useState(false);
96
+ let root = useRef(null);
97
+
98
+ const onChange = (e) => {
99
+ setLabel(e.target.value);
100
+ };
101
+
102
+ const isMathRendering = () => {
103
+ return isEditing === false && mathLabel !== undefined;
104
+ };
105
+
106
+ const onChangeProp = (e) => {
107
+ setMathLabel(getLabelMathFormat(mark.label));
108
+ setIsEditing(false);
109
+ props.onChange(e.target.value);
110
+ };
46
111
  let extraStyle = {};
47
112
 
48
113
  if (rotate) {
@@ -57,15 +122,37 @@ export const MarkLabel = (props) => {
57
122
  setLabel(mark.label);
58
123
  }, [mark.label]);
59
124
 
60
- return (
125
+ useEffect(() => {
126
+ renderMath(root);
127
+ }, []);
128
+
129
+ return isMathRendering() ? (
130
+ <div
131
+ ref={(r) => (root = r)}
132
+ dangerouslySetInnerHTML={{ __html: getLabelMathFormat(label) }}
133
+ className={classNames(classes.mathInput, {
134
+ [classes.disabled]: disabled,
135
+ [classes.error]: error,
136
+ [classes.correct]: correctness && correctness.label === 'correct',
137
+ [classes.incorrect]: correctness && correctness.label === 'incorrect',
138
+ })}
139
+ onClick={() => setIsEditing(true)}
140
+ style={{ minWidth: barWidth }}
141
+ ></div>
142
+ ) : (
61
143
  <AutosizeInput
62
- autoFocus={autoFocus}
63
144
  inputRef={(r) => {
64
145
  _ref(r);
65
146
  externalInputRef(r);
66
147
  }}
148
+ autoFocus={isEditing || autoFocus}
67
149
  disabled={disabled}
68
- inputClassName={cn(classes.input, correctness && correctness.label, disabled && 'disabled', error && 'error')}
150
+ inputClassName={classNames(
151
+ classes.input,
152
+ correctness && correctness.label,
153
+ disabled && 'disabled',
154
+ error && 'error',
155
+ )}
69
156
  inputStyle={{
70
157
  minWidth: barWidth,
71
158
  textAlign: 'center',