'use strict'; import './public-path'; import React, {Component} from 'react'; import ReactDOM from 'react-dom'; import { I18nextProvider, translate, } from 'react-i18next'; import i18n from './i18n'; import { parentRPC, UntrustedContentRoot } from './untrusted'; import PropTypes from "prop-types"; import styles from "./sandboxed-codeeditor.scss"; import { getPublicUrl, getSandboxUrl, getTrustedUrl } from "./urls"; import { base, unbase } from "../../../shared/templates"; import brace from 'brace'; import ACEEditorRaw from 'react-ace'; import 'brace/theme/github'; import 'brace/ext/searchbox'; import 'brace/mode/html'; import {CodeEditorSourceType} from "./sandboxed-codeeditor-shared"; import mjml2html from "mjml4-in-browser"; import juice from "juice"; @translate(null, { withRef: true }) class CodeEditorSandbox extends Component { constructor(props) { super(props); let defaultSource; if (props.sourceType === CodeEditorSourceType.MJML) { defaultSource = '\n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ''; } else if (props.sourceType === CodeEditorSourceType.HTML) { defaultSource = '\n' + '\n' + '\n' + ' \n' + ' Title of the document\n' + '\n' + '\n' + ' Content of the document......\n' + '\n' + ''; } const trustedUrlBase = getTrustedUrl(); const sandboxUrlBase = getSandboxUrl(); const publicUrlBase = getPublicUrl(); const source = this.props.initialSource ? base(this.props.initialSource, trustedUrlBase, sandboxUrlBase, publicUrlBase) : defaultSource; this.state = { source, preview: props.initialPreview }; } static propTypes = { entityTypeId: PropTypes.string, entityId: PropTypes.number, initialSource: PropTypes.string, sourceType: PropTypes.string, initialPreview: PropTypes.bool } async exportState(method, params) { const trustedUrlBase = getTrustedUrl(); const sandboxUrlBase = getSandboxUrl(); const publicUrlBase = getPublicUrl(); return { source: unbase(this.state.source, trustedUrlBase, sandboxUrlBase, publicUrlBase, true) }; } async setPreview(method, preview) { this.setState({ preview }); } componentDidMount() { parentRPC.setMethodHandler('exportState', ::this.exportState); parentRPC.setMethodHandler('setPreview', ::this.setPreview); } render() { let previewContents; if (this.props.sourceType === CodeEditorSourceType.MJML) { const res = mjml2html(this.state.source); previewContents = res.html; } else if (this.props.sourceType === CodeEditorSourceType.HTML) { previewContents = juice(this.state.source); } return (
this.setState({source: data})} fontSize={12} showPrintMargin={false} value={this.state.source} tabSize={2} setOptions={{useWorker: false}} // This disables syntax check because it does not always work well (e.g. in case of JS code in report templates) />
{ this.state.preview &&
}
); } } export default function() { parentRPC.init(); ReactDOM.render( } /> , document.getElementById('root') ); };