-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a ShrinkSql component (#1058)
- Loading branch information
1 parent
544b3f3
commit 62c7111
Showing
2 changed files
with
40 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
caravel/assets/javascripts/SqlLab/components/SqlShrink.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import SyntaxHighlighter from 'react-syntax-highlighter'; | ||
import { github } from 'react-syntax-highlighter/dist/styles'; | ||
|
||
const SqlShrink = (props) => { | ||
let lines = props.sql.split('\n'); | ||
if (lines.length >= props.maxLines) { | ||
lines = lines.slice(0, props.maxLines); | ||
lines.push('{...}'); | ||
} | ||
const shrunk = lines.map((line) => { | ||
if (line.length > props.maxWidth) { | ||
return line.slice(0, props.maxWidth) + '{...}'; | ||
} | ||
return line; | ||
}) | ||
.join('\n'); | ||
return ( | ||
<div> | ||
<SyntaxHighlighter language="sql" style={github}> | ||
{shrunk} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
}; | ||
|
||
SqlShrink.defaultProps = { | ||
maxWidth: 60, | ||
maxLines: 6, | ||
}; | ||
|
||
SqlShrink.propTypes = { | ||
sql: React.PropTypes.string, | ||
maxWidth: React.PropTypes.integer, | ||
maxLines: React.PropTypes.integer, | ||
}; | ||
|
||
export default SqlShrink; |