From ca7664996042b0600098a2e4eecd725eb6976c9d Mon Sep 17 00:00:00 2001 From: letranloc Date: Sat, 9 Sep 2017 11:02:26 +0700 Subject: [PATCH 01/13] integrate with Khan/math-input --- .npmignore | 2 +- README.md | 11 +- package.json | 7 +- postcss.config.js | 7 + src/components/InsertKatexButton.js | 46 +- src/components/KatexOutput.js | 70 +- src/components/TeXBlock.js | 281 +++--- src/components/math-input/actions/index.js | 58 ++ src/components/math-input/app.js | 6 + src/components/math-input/components/app.js | 66 ++ .../math-input/components/common-style.js | 49 + .../components/compute-layout-parameters.js | 157 +++ .../math-input/components/corner-decal.js | 52 + .../math-input/components/echo-manager.js | 143 +++ .../components/empty-keypad-button.js | 43 + .../components/expression-keypad.js | 317 ++++++ .../math-input/components/fraction-keypad.js | 172 ++++ .../math-input/components/gesture-manager.js | 220 +++++ .../components/gesture-state-machine.js | 283 ++++++ src/components/math-input/components/icon.js | 77 ++ .../components/iconography/arrow.js | 15 + .../components/iconography/backspace.js | 21 + .../math-input/components/iconography/cdot.js | 26 + .../math-input/components/iconography/cos.js | 26 + .../components/iconography/cube-root.js | 27 + .../components/iconography/dismiss.js | 17 + .../components/iconography/divide.js | 26 + .../math-input/components/iconography/down.js | 14 + .../components/iconography/equal.js | 25 + .../components/iconography/exp-2.js | 25 + .../components/iconography/exp-3.js | 25 + .../math-input/components/iconography/exp.js | 25 + .../components/iconography/frac-exclusive.js | 32 + .../components/iconography/frac-inclusive.js | 32 + .../math-input/components/iconography/geq.js | 25 + .../math-input/components/iconography/gt.js | 25 + .../components/iconography/index.js | 48 + .../iconography/jump-into-numerator.js | 26 + .../components/iconography/jump-out-base.js | 22 + .../iconography/jump-out-denominator.js | 26 + .../iconography/jump-out-exponent.js | 22 + .../iconography/jump-out-numerator.js | 26 + .../iconography/jump-out-parentheses.js | 21 + .../components/iconography/left-paren.js | 25 + .../math-input/components/iconography/left.js | 12 + .../math-input/components/iconography/leq.js | 25 + .../math-input/components/iconography/ln.js | 25 + .../components/iconography/log-n.js | 25 + .../math-input/components/iconography/log.js | 25 + .../math-input/components/iconography/lt.js | 25 + .../components/iconography/minus.js | 24 + .../math-input/components/iconography/neq.js | 25 + .../components/iconography/parens.js | 25 + .../components/iconography/percent.js | 29 + .../components/iconography/period.js | 23 + .../math-input/components/iconography/plus.js | 24 + .../components/iconography/radical.js | 27 + .../components/iconography/right-paren.js | 25 + .../components/iconography/right.js | 14 + .../math-input/components/iconography/sin.js | 26 + .../math-input/components/iconography/sqrt.js | 24 + .../math-input/components/iconography/tan.js | 26 + .../components/iconography/times.js | 25 + .../math-input/components/iconography/up.js | 14 + .../components/input/cursor-contexts.js | 31 + .../components/input/cursor-handle.js | 108 +++ .../components/input/drag-listener.js | 79 ++ .../math-input/components/input/math-input.js | 825 ++++++++++++++++ .../components/input/math-wrapper.js | 917 ++++++++++++++++++ .../components/input/scroll-into-view.js | 59 ++ .../math-input/components/keypad-button.js | 351 +++++++ .../math-input/components/keypad-container.js | 298 ++++++ .../math-input/components/keypad.js | 145 +++ .../components/many-keypad-button.js | 43 + .../math-input/components/math-icon.js | 61 ++ .../components/multi-symbol-grid.js | 155 +++ .../components/multi-symbol-popover.js | 52 + .../math-input/components/navigation-pad.js | 132 +++ .../math-input/components/node-manager.js | 122 +++ .../math-input/components/pager-indicator.js | 94 ++ .../math-input/components/popover-manager.js | 67 ++ .../components/popover-state-machine.js | 173 ++++ .../math-input/components/prop-types.js | 96 ++ .../math-input/components/provided-keypad.js | 77 ++ .../math-input/components/styles.js | 38 + .../math-input/components/svg-icon.js | 43 + .../math-input/components/text-icon.js | 48 + .../components/touchable-keypad-button.js | 124 +++ .../math-input/components/two-page-keypad.js | 83 ++ .../math-input/components/velocity-tracker.js | 76 ++ .../math-input/components/view-pager.js | 124 +++ .../math-input/components/z-indexes.js | 10 + src/components/math-input/consts.js | 73 ++ src/components/math-input/data/key-configs.js | 305 ++++++ src/components/math-input/data/keys.js | 70 ++ src/components/math-input/echo.css | 72 ++ .../math-input/fake-react-native-web/index.js | 13 + .../math-input/fake-react-native-web/text.js | 53 + .../math-input/fake-react-native-web/view.js | 122 +++ src/components/math-input/index.js | 27 + src/components/math-input/lib/i18n.js | 411 ++++++++ src/components/math-input/lib/icu-slim.js | 99 ++ src/components/math-input/overrides.css | 108 +++ src/components/math-input/popover.css | 23 + src/components/math-input/store/index.js | 493 ++++++++++ .../math-input/tools/svg-to-react/convert.py | 110 +++ .../svg-to-react/icons/math-keypad-icon-0.svg | 37 + .../svg-to-react/icons/math-keypad-icon-1.svg | 35 + .../svg-to-react/icons/math-keypad-icon-2.svg | 37 + .../svg-to-react/icons/math-keypad-icon-3.svg | 37 + .../svg-to-react/icons/math-keypad-icon-4.svg | 37 + .../svg-to-react/icons/math-keypad-icon-5.svg | 37 + .../svg-to-react/icons/math-keypad-icon-6.svg | 37 + .../svg-to-react/icons/math-keypad-icon-7.svg | 35 + .../svg-to-react/icons/math-keypad-icon-8.svg | 37 + .../svg-to-react/icons/math-keypad-icon-9.svg | 37 + .../icons/math-keypad-icon-addition.svg | 42 + .../icons/math-keypad-icon-cos.svg | 43 + .../icons/math-keypad-icon-delete.svg | 50 + .../icons/math-keypad-icon-dismiss.svg | 41 + .../icons/math-keypad-icon-division.svg | 44 + .../icons/math-keypad-icon-equals-not.svg | 62 ++ .../icons/math-keypad-icon-equals.svg | 57 ++ .../icons/math-keypad-icon-exponent-2.svg | 47 + .../icons/math-keypad-icon-exponent-3.svg | 47 + .../icons/math-keypad-icon-exponent.svg | 47 + .../icons/math-keypad-icon-fraction.svg | 51 + .../icons/math-keypad-icon-greater-than.svg | 52 + .../icons/math-keypad-icon-jump-out-base.svg | 56 ++ .../math-keypad-icon-jump-out-denominator.svg | 66 ++ .../math-keypad-icon-jump-out-exponent.svg | 56 ++ .../math-keypad-icon-jump-out-parentheses.svg | 57 ++ .../icons/math-keypad-icon-less-than.svg | 52 + .../icons/math-keypad-icon-log-10.svg | 41 + .../icons/math-keypad-icon-log-e.svg | 41 + .../icons/math-keypad-icon-log.svg | 47 + .../math-keypad-icon-multiplication-cross.svg | 47 + .../math-keypad-icon-multiplication-dot.svg | 39 + .../icons/math-keypad-icon-percent.svg | 45 + .../icons/math-keypad-icon-radical-2.svg | 40 + .../icons/math-keypad-icon-radical-3.svg | 46 + .../icons/math-keypad-icon-radical.svg | 46 + .../math-keypad-icon-radix-character.svg | 33 + .../icons/math-keypad-icon-sin.svg | 43 + .../icons/math-keypad-icon-subtraction.svg | 38 + .../icons/math-keypad-icon-tan.svg | 43 + .../tools/svg-to-react/symbol_map.py | 41 + src/components/math-input/utils.js | 17 + src/index.js | 102 +- src/modifiers/insertTeXBlock.js | 45 +- src/modifiers/removeTeXBlock.js | 90 +- src/styles.css | 121 +-- yarn.lock | 288 +++--- 153 files changed, 11494 insertions(+), 542 deletions(-) create mode 100644 postcss.config.js create mode 100644 src/components/math-input/actions/index.js create mode 100644 src/components/math-input/app.js create mode 100644 src/components/math-input/components/app.js create mode 100644 src/components/math-input/components/common-style.js create mode 100644 src/components/math-input/components/compute-layout-parameters.js create mode 100644 src/components/math-input/components/corner-decal.js create mode 100644 src/components/math-input/components/echo-manager.js create mode 100644 src/components/math-input/components/empty-keypad-button.js create mode 100644 src/components/math-input/components/expression-keypad.js create mode 100644 src/components/math-input/components/fraction-keypad.js create mode 100644 src/components/math-input/components/gesture-manager.js create mode 100644 src/components/math-input/components/gesture-state-machine.js create mode 100644 src/components/math-input/components/icon.js create mode 100644 src/components/math-input/components/iconography/arrow.js create mode 100644 src/components/math-input/components/iconography/backspace.js create mode 100644 src/components/math-input/components/iconography/cdot.js create mode 100644 src/components/math-input/components/iconography/cos.js create mode 100644 src/components/math-input/components/iconography/cube-root.js create mode 100644 src/components/math-input/components/iconography/dismiss.js create mode 100644 src/components/math-input/components/iconography/divide.js create mode 100644 src/components/math-input/components/iconography/down.js create mode 100644 src/components/math-input/components/iconography/equal.js create mode 100644 src/components/math-input/components/iconography/exp-2.js create mode 100644 src/components/math-input/components/iconography/exp-3.js create mode 100644 src/components/math-input/components/iconography/exp.js create mode 100644 src/components/math-input/components/iconography/frac-exclusive.js create mode 100644 src/components/math-input/components/iconography/frac-inclusive.js create mode 100644 src/components/math-input/components/iconography/geq.js create mode 100644 src/components/math-input/components/iconography/gt.js create mode 100644 src/components/math-input/components/iconography/index.js create mode 100644 src/components/math-input/components/iconography/jump-into-numerator.js create mode 100644 src/components/math-input/components/iconography/jump-out-base.js create mode 100644 src/components/math-input/components/iconography/jump-out-denominator.js create mode 100644 src/components/math-input/components/iconography/jump-out-exponent.js create mode 100644 src/components/math-input/components/iconography/jump-out-numerator.js create mode 100644 src/components/math-input/components/iconography/jump-out-parentheses.js create mode 100644 src/components/math-input/components/iconography/left-paren.js create mode 100644 src/components/math-input/components/iconography/left.js create mode 100644 src/components/math-input/components/iconography/leq.js create mode 100644 src/components/math-input/components/iconography/ln.js create mode 100644 src/components/math-input/components/iconography/log-n.js create mode 100644 src/components/math-input/components/iconography/log.js create mode 100644 src/components/math-input/components/iconography/lt.js create mode 100644 src/components/math-input/components/iconography/minus.js create mode 100644 src/components/math-input/components/iconography/neq.js create mode 100644 src/components/math-input/components/iconography/parens.js create mode 100644 src/components/math-input/components/iconography/percent.js create mode 100644 src/components/math-input/components/iconography/period.js create mode 100644 src/components/math-input/components/iconography/plus.js create mode 100644 src/components/math-input/components/iconography/radical.js create mode 100644 src/components/math-input/components/iconography/right-paren.js create mode 100644 src/components/math-input/components/iconography/right.js create mode 100644 src/components/math-input/components/iconography/sin.js create mode 100644 src/components/math-input/components/iconography/sqrt.js create mode 100644 src/components/math-input/components/iconography/tan.js create mode 100644 src/components/math-input/components/iconography/times.js create mode 100644 src/components/math-input/components/iconography/up.js create mode 100644 src/components/math-input/components/input/cursor-contexts.js create mode 100644 src/components/math-input/components/input/cursor-handle.js create mode 100644 src/components/math-input/components/input/drag-listener.js create mode 100644 src/components/math-input/components/input/math-input.js create mode 100644 src/components/math-input/components/input/math-wrapper.js create mode 100644 src/components/math-input/components/input/scroll-into-view.js create mode 100644 src/components/math-input/components/keypad-button.js create mode 100644 src/components/math-input/components/keypad-container.js create mode 100644 src/components/math-input/components/keypad.js create mode 100644 src/components/math-input/components/many-keypad-button.js create mode 100644 src/components/math-input/components/math-icon.js create mode 100644 src/components/math-input/components/multi-symbol-grid.js create mode 100644 src/components/math-input/components/multi-symbol-popover.js create mode 100644 src/components/math-input/components/navigation-pad.js create mode 100644 src/components/math-input/components/node-manager.js create mode 100644 src/components/math-input/components/pager-indicator.js create mode 100644 src/components/math-input/components/popover-manager.js create mode 100644 src/components/math-input/components/popover-state-machine.js create mode 100644 src/components/math-input/components/prop-types.js create mode 100644 src/components/math-input/components/provided-keypad.js create mode 100644 src/components/math-input/components/styles.js create mode 100644 src/components/math-input/components/svg-icon.js create mode 100644 src/components/math-input/components/text-icon.js create mode 100644 src/components/math-input/components/touchable-keypad-button.js create mode 100644 src/components/math-input/components/two-page-keypad.js create mode 100644 src/components/math-input/components/velocity-tracker.js create mode 100644 src/components/math-input/components/view-pager.js create mode 100644 src/components/math-input/components/z-indexes.js create mode 100644 src/components/math-input/consts.js create mode 100644 src/components/math-input/data/key-configs.js create mode 100644 src/components/math-input/data/keys.js create mode 100644 src/components/math-input/echo.css create mode 100644 src/components/math-input/fake-react-native-web/index.js create mode 100644 src/components/math-input/fake-react-native-web/text.js create mode 100644 src/components/math-input/fake-react-native-web/view.js create mode 100644 src/components/math-input/index.js create mode 100644 src/components/math-input/lib/i18n.js create mode 100644 src/components/math-input/lib/icu-slim.js create mode 100644 src/components/math-input/overrides.css create mode 100644 src/components/math-input/popover.css create mode 100644 src/components/math-input/store/index.js create mode 100644 src/components/math-input/tools/svg-to-react/convert.py create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-0.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-1.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-2.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-3.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-4.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-5.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-6.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-7.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-8.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-9.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-addition.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-cos.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-delete.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-dismiss.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-division.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-equals-not.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-equals.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-exponent-2.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-exponent-3.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-exponent.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-fraction.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-greater-than.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-jump-out-base.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-jump-out-denominator.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-jump-out-exponent.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-jump-out-parentheses.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-less-than.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-log-10.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-log-e.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-log.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-multiplication-cross.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-multiplication-dot.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-percent.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-radical-2.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-radical-3.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-radical.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-radix-character.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-sin.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-subtraction.svg create mode 100644 src/components/math-input/tools/svg-to-react/icons/math-keypad-icon-tan.svg create mode 100644 src/components/math-input/tools/svg-to-react/symbol_map.py create mode 100644 src/components/math-input/utils.js diff --git a/.npmignore b/.npmignore index 7bc73bc..4e0df9a 100644 --- a/.npmignore +++ b/.npmignore @@ -4,7 +4,7 @@ node_modules # sources -/src/ +/draft-js-katex-plugin/src.bak/ webpack.config.js .babelrc .gitignore diff --git a/README.md b/README.md index e0d26e5..bef80c2 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,14 @@ This plugin insert and render LaTeX using [KaTeX](https://github.com/Khan/KaTeX), modified from [TeX example](https://github.com/facebook/draft-js/tree/master/examples/draft-0-10-0/tex). +- Integrated with [Khan/math-input](https://github.com/Khan/math-input). + ## Usage -Add KaTeX style +Add MathQuill libs ```html - + + ``` Add plugin @@ -19,7 +22,3 @@ const kaTeXPlugin = createKaTeXPlugin(); const { InsertButton } = kaTeXPlugin; ``` - -## TODO - -- [ ] Integrate with [Khan/math-input](https://github.com/Khan/math-input). diff --git a/package.json b/package.json index 5173cdf..a447f76 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,15 @@ "url": "https://github.com/letranloc/draft-js-katex-plugin/issues" }, "dependencies": { + "aphrodite": "^1.2.3", "decorate-component-with-props": "^1.0.2", "draft-js": ">=0.9.1", - "katex": "^0.7.1", + "jquery": "^3.2.1", + "katex": "^0.8.3", "react": "^15.4.2", + "react-addons-css-transition-group": "^15.6.0", + "react-redux": "^5.0.6", + "redux": "^3.7.2", "union-class-names": "^1.0.0" }, "devDependencies": { diff --git a/postcss.config.js b/postcss.config.js new file mode 100644 index 0000000..b58cda9 --- /dev/null +++ b/postcss.config.js @@ -0,0 +1,7 @@ +/* eslint-disable global-require */ + +module.exports = { + plugins: [ + require('autoprefixer')({ browsers: ['> 1%'] }) + ] +}; diff --git a/src/components/InsertKatexButton.js b/src/components/InsertKatexButton.js index 1fddb0b..61abb18 100644 --- a/src/components/InsertKatexButton.js +++ b/src/components/InsertKatexButton.js @@ -3,32 +3,28 @@ import unionClassNames from 'union-class-names'; import insertTeXBlock from '../modifiers/insertTeXBlock'; export default class InsertKatexButton extends Component { - static propTypes = { - children: PropTypes.node.isRequired, - theme: PropTypes.any, - }; + static propTypes = { + children: PropTypes.node.isRequired, + theme: PropTypes.any, + }; - static defaultProps = { - theme: {} - }; + onClick = () => { + const { store } = this.props; + const editorState = store.getEditorState(); + store.setEditorState(insertTeXBlock(editorState)); + }; - onClick = () => { - const { store } = this.props; - const editorState = store.getEditorState(); - store.setEditorState(insertTeXBlock(editorState)); - }; + render() { + const { theme = {}, className, children } = this.props; + const combinedClassName = unionClassNames(theme.insertButton, className); - render() { - const { theme, className, children } = this.props; - const combinedClassName = unionClassNames(theme.insertButton, className); - - return ( - - ); - } + return ( + + ); + } } diff --git a/src/components/KatexOutput.js b/src/components/KatexOutput.js index ff503ae..ae82b7b 100644 --- a/src/components/KatexOutput.js +++ b/src/components/KatexOutput.js @@ -2,46 +2,46 @@ import katex from 'katex'; import React from 'react'; export default class KatexOutput extends React.Component { - constructor(props) { - super(props); - this.timer = null; - } - - componentDidMount() { - this.update(); - } + constructor(props) { + super(props); + this.timer = null; + } - componentWillReceiveProps({ content }) { - if (content !== this.props.content) { - this.update(); + componentDidMount() { + this.update(); } - } - componentWillUnmount() { - clearTimeout(this.timer); - this.timer = null; - } + componentWillReceiveProps({ content }) { + if (content !== this.props.content) { + this.update(); + } + } - update() { - if (this.timer) { - clearTimeout(this.timer); + componentWillUnmount() { + clearTimeout(this.timer); + this.timer = null; } - this.timer = setTimeout(() => { - katex.render( - this.props.content, - this.container, - { displayMode: true } - ); - }, 0); - } + update() { + if (this.timer) { + clearTimeout(this.timer); + } - render() { - return ( -
{ this.container = container; }} - onClick={this.props.onClick} - /> - ); - } + this.timer = setTimeout(() => { + katex.render( + this.props.content, + this.container, + { displayMode: true } + ); + }, 0); + } + + render() { + return ( +
{ this.container = container; }} + onClick={this.props.onClick} + /> + ); + } } diff --git a/src/components/TeXBlock.js b/src/components/TeXBlock.js index 2bd38c6..a99e778 100644 --- a/src/components/TeXBlock.js +++ b/src/components/TeXBlock.js @@ -3,144 +3,161 @@ import katex from 'katex'; import { Entity } from 'draft-js'; import unionClassNames from 'union-class-names'; import KatexOutput from './KatexOutput'; +import MathInput from './math-input/components/app'; export default class TeXBlock extends Component { - constructor(props) { - super(props); - this.state = { editMode: false }; - } - - onClick = () => { - if (this.state.editMode) { - return; + constructor(props) { + super(props); + this.state = { editMode: false } } - this.setState({ - editMode: true, - texValue: this.getValue(), - }, () => { - this.startEdit(); - }); - }; - - onValueChange = (evt) => { - const value = evt.target.value; - let invalid = false; - try { - katex.__parse(value); // eslint-disable-line no-underscore-dangle - } catch (e) { - invalid = true; - } finally { - this.setState({ - invalidTeX: invalid, - texValue: value, - }); - } - }; - - getValue = () => { - const entityKey = this.props.block.getEntityAt(0); - const entityData = Entity.get(entityKey).getData(); - return entityData.content; - }; - - startEdit = () => { - const { block, blockProps } = this.props; - blockProps.onStartEdit(block.getKey()); - }; - - finishEdit = (newContentState) => { - const { block, blockProps } = this.props; - blockProps.onFinishEdit(block.getKey(), newContentState); - }; - - remove = () => { - const { block, blockProps } = this.props; - blockProps.onRemove(block.getKey()); - }; - - save = () => { - const { block, store } = this.props; - - const entityKey = block.getEntityAt(0); - const editorState = store.getEditorState(); - - Entity.mergeData(entityKey, { content: this.state.texValue }); - - this.setState({ - invalidTeX: false, - editMode: false, - texValue: null, - }, this.finishEdit.bind(this, editorState)); - }; - - render() { - const { theme, doneContent, removeContent } = this.props; - - let texContent = null; - if (this.state.editMode) { - if (this.state.invalidTeX) { - texContent = ''; - } else { - texContent = this.state.texValue; - } - } else { - texContent = this.getValue(); - } - - let className = theme.tex; - if (this.state.editMode) { - className = unionClassNames(className, theme.activeTeX); - } - - let editPanel = null; - if (this.state.editMode) { - let buttonClass = theme.saveButton; - if (this.state.invalidTeX) { - buttonClass = unionClassNames(buttonClass, theme.invalidButton); - } - - editPanel = ( -
+ callbacks = {}; + + onClick = () => { + if (this.state.editMode || this.props.store.getReadOnly()) { + return; + } + + this.setState({ + editMode: true, + texValue: this.getValue(), + }, () => { + this.startEdit(); + }); + }; + + onValueChange = (evt) => { + const value = evt.target.value; + this.onMathInputChange(value); + }; + + onFocus = () => { + if (this.callbacks.blur) { + this.callbacks.blur(); + } + }; + + onMathInputChange = (value) => { + let invalid = false; + try { + katex.__parse(value); // eslint-disable-line no-underscore-dangle + } catch (e) { + invalid = true; + } finally { + this.setState({ + invalidTeX: invalid, + texValue: value, + }); + } + }; + + getValue = () => { + const entityKey = this.props.block.getEntityAt(0); + const entityData = Entity.get(entityKey).getData(); + return entityData.content; + }; + + startEdit = () => { + const { block, blockProps } = this.props; + blockProps.onStartEdit(block.getKey()); + }; + + finishEdit = (newContentState) => { + const { block, blockProps } = this.props; + blockProps.onFinishEdit(block.getKey(), newContentState); + }; + + remove = () => { + const { block, blockProps } = this.props; + blockProps.onRemove(block.getKey()); + }; + + save = () => { + const { block, store } = this.props; + + const entityKey = block.getEntityAt(0); + const editorState = store.getEditorState(); + + Entity.mergeData(entityKey, { content: this.state.texValue }); + + this.setState({ + invalidTeX: false, + editMode: false, + texValue: null, + }, this.finishEdit.bind(this, editorState)); + }; + + render() { + const { theme, doneContent, removeContent } = this.props; + + let texContent = null; + if (this.state.editMode) { + if (this.state.invalidTeX) { + texContent = ''; + } else { + texContent = this.state.texValue; + } + } else { + texContent = this.getValue(); + } + + let className = theme.tex; + if (this.state.editMode) { + className = unionClassNames(className, theme.activeTeX); + } + + let editPanel = null; + if (this.state.editMode) { + let buttonClass = theme.saveButton; + if (this.state.invalidTeX) { + buttonClass = unionClassNames(buttonClass, theme.invalidButton); + } + + editPanel = ( +