|
| 1 | +import React, { PureComponent } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import ReactMarkdown from 'react-markdown'; |
| 5 | +import { neos } from '@neos-project/neos-ui-decorators'; |
| 6 | +import { selectors } from '@neos-project/neos-ui-redux-store'; |
| 7 | + |
| 8 | +const evaluate = (context, _expression) => { |
| 9 | + const { node, parentNode } = context; // jshint ignore:line |
| 10 | + return eval(_expression.replace('ClientEval:', '')); // jshint ignore:line |
| 11 | +} |
| 12 | + |
| 13 | +@neos((globalRegistry) => ({ |
| 14 | + i18nRegistry: globalRegistry.get('i18n'), |
| 15 | +})) |
| 16 | +@connect(state => { |
| 17 | + const focusedNode = selectors.CR.Nodes.focusedSelector(state); |
| 18 | + const parentNode = selectors.CR.Nodes.nodeByContextPath(state)(focusedNode.parent); |
| 19 | + |
| 20 | + return { |
| 21 | + focusedNode, |
| 22 | + parentNode, |
| 23 | + }; |
| 24 | +}) |
| 25 | +export default class MarkdownView extends PureComponent { |
| 26 | + |
| 27 | + static propTypes = { |
| 28 | + options: PropTypes.shape({ |
| 29 | + content: PropTypes.string, |
| 30 | + className: PropTypes.string, |
| 31 | + allowedElements: PropTypes.arrayOf(PropTypes.string), |
| 32 | + disallowedElements: PropTypes.arrayOf(PropTypes.string), |
| 33 | + focusedNode: PropTypes.object, |
| 34 | + parentNode: PropTypes.object, |
| 35 | + }).isRequired, |
| 36 | + i18nRegistry: PropTypes.object.isRequired, |
| 37 | + } |
| 38 | + |
| 39 | + state = { |
| 40 | + content: '', |
| 41 | + } |
| 42 | + |
| 43 | + constructor(props) { |
| 44 | + super(props); |
| 45 | + } |
| 46 | + |
| 47 | + componentDidMount() { |
| 48 | + this.generateContent(); |
| 49 | + } |
| 50 | + |
| 51 | + generateContent() { |
| 52 | + let content = this.props.options.content || ''; |
| 53 | + console.log('transient values', this.props.transientValues); |
| 54 | + |
| 55 | + if (content.startsWith('ClientEval:')) { |
| 56 | + const context = { |
| 57 | + node: this.props.focusedNode, |
| 58 | + parentNode: this.props.parentNode, |
| 59 | + }; |
| 60 | + content = evaluate(context, content.replace('ClientEval:', '')); |
| 61 | + } |
| 62 | + try { |
| 63 | + content = this.props.i18nRegistry.translate(content); |
| 64 | + } catch (e) { |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + if (typeof content === 'object' |
| 69 | + && 'then' in content |
| 70 | + && typeof content.then === 'function' |
| 71 | + ) { |
| 72 | + content.then(content => { |
| 73 | + this.setState({ content }); |
| 74 | + }) |
| 75 | + } else { |
| 76 | + if (typeof content === 'string') { |
| 77 | + content = content |
| 78 | + // replace trailing \\ with double spaces to allow for line breaks lost in yaml |
| 79 | + .replace(/\\\\\n/g, ' \n'); |
| 80 | + } |
| 81 | + this.setState({ content }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + render() { |
| 86 | + const { |
| 87 | + className, |
| 88 | + allowedElements, |
| 89 | + disallowedElements, |
| 90 | + } = this.props.options; |
| 91 | + |
| 92 | + return ( |
| 93 | + <ReactMarkdown |
| 94 | + className={className} |
| 95 | + allowedElements={allowedElements} |
| 96 | + disallowedElements={disallowedElements} |
| 97 | + > |
| 98 | + {this.state.content} |
| 99 | + </ReactMarkdown> |
| 100 | + ); |
| 101 | + } |
| 102 | +}; |
0 commit comments