Files
superset2/superset/assets/javascripts/SqlLab/components/HighlightedSql.jsx
Maxime Beauchemin 366ecefbaa Bumping the JS libs to fix the build (#2616)
* bumping the js libs

* New linting rules

* More linting

* More

* Done linting

* npm >=4.5.0

* Bumping node

* Tweaking the build

* Fixing the damn build

* Fixing the apps
2017-04-13 15:04:09 -07:00

88 lines
2.1 KiB
JavaScript

import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { github } from 'react-syntax-highlighter/dist/styles';
import ModalTrigger from '../../components/ModalTrigger';
const defaultProps = {
maxWidth: 50,
maxLines: 5,
shrink: false,
};
const propTypes = {
sql: React.PropTypes.string.isRequired,
rawSql: React.PropTypes.string,
maxWidth: React.PropTypes.number,
maxLines: React.PropTypes.number,
shrink: React.PropTypes.bool,
};
class HighlightedSql extends React.Component {
constructor(props) {
super(props);
this.state = {
modalBody: null,
};
}
shrinkSql() {
const sql = this.props.sql || '';
let lines = sql.split('\n');
if (lines.length >= this.props.maxLines) {
lines = lines.slice(0, this.props.maxLines);
lines.push('{...}');
}
return lines.map((line) => {
if (line.length > this.props.maxWidth) {
return line.slice(0, this.props.maxWidth) + '{...}';
}
return line;
})
.join('\n');
}
triggerNode() {
const shownSql = this.props.shrink ? this.shrinkSql(this.props.sql) : this.props.sql;
return (
<SyntaxHighlighter language="sql" style={github}>
{shownSql}
</SyntaxHighlighter>);
}
generateModal() {
let rawSql;
if (this.props.rawSql && this.props.rawSql !== this.props.sql) {
rawSql = (
<div>
<h4>Raw SQL</h4>
<SyntaxHighlighter language="sql" style={github}>
{this.props.rawSql}
</SyntaxHighlighter>
</div>
);
}
this.setState({
modalBody: (
<div>
<h4>Source SQL</h4>
<SyntaxHighlighter language="sql" style={github}>
{this.props.sql}
</SyntaxHighlighter>
{rawSql}
</div>
),
});
}
render() {
return (
<ModalTrigger
modalTitle="SQL"
triggerNode={this.triggerNode()}
modalBody={this.state.modalBody}
beforeOpen={this.generateModal.bind(this)}
/>
);
}
}
HighlightedSql.propTypes = propTypes;
HighlightedSql.defaultProps = defaultProps;
export default HighlightedSql;