diff options
| author | Ryan Mehri <[email protected]> | 2020-05-23 09:53:02 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-23 09:53:02 -0600 |
| commit | 128ed6ef43d960becfeaef96615aa06e16932195 (patch) | |
| tree | d81289bc118b006ddec2e9f7162f188e0d8f8ec8 /frontend/src/components/renderers | |
| parent | Merge pull request #29 from jackyzha0/order-langs (diff) | |
| parent | fix weird padding (diff) | |
| download | ctrl-v-128ed6ef43d960becfeaef96615aa06e16932195.tar.xz ctrl-v-128ed6ef43d960becfeaef96615aa06e16932195.zip | |
Merge pull request #30 from jackyzha0/latex-renderer
latex renderer live preview
Diffstat (limited to 'frontend/src/components/renderers')
| -rw-r--r-- | frontend/src/components/renderers/Code.js | 23 | ||||
| -rw-r--r-- | frontend/src/components/renderers/Latex.js | 38 | ||||
| -rw-r--r-- | frontend/src/components/renderers/Raw.js | 21 |
3 files changed, 69 insertions, 13 deletions
diff --git a/frontend/src/components/renderers/Code.js b/frontend/src/components/renderers/Code.js index 02c1cc6..9e7521b 100644 --- a/frontend/src/components/renderers/Code.js +++ b/frontend/src/components/renderers/Code.js @@ -1,6 +1,7 @@ import React from 'react'; import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'; import { atomOneLight, ascetic, atomOneDark, dracula, ocean } from 'react-syntax-highlighter/dist/esm/styles/hljs'; +import styled from 'styled-components' export const THEMES = Object.freeze({ 'atom': atomOneLight, @@ -34,16 +35,32 @@ export const LANGS = Object.freeze({ 'yaml': 'yaml' }) +const StyledPre = styled.pre` + padding: calc(0.8em - 1px) !important; + margin: 0; +` + +const CodeBlock = styled.div` + width: 100%; + font-size: 0.8em; + min-height: 1.2em; + border-radius: 3px; + border: 1px solid #565656; + outline: none; + margin: 1.7em 0; + padding-right: calc(1.6em - 2px); +` + const CodeRenderer = React.forwardRef((props, ref) => { const Pre = (props) => { return ( - <pre {...props} ref={ref} /> + <StyledPre {...props} ref={ref} /> ); } return ( - <div className="lt-shadow codeBlock"> + <CodeBlock className="lt-shadow"> <SyntaxHighlighter ref={ref} language={props.lang} @@ -52,7 +69,7 @@ const CodeRenderer = React.forwardRef((props, ref) => { PreTag={Pre}> {props.content} </SyntaxHighlighter> - </div> + </CodeBlock> ); }); diff --git a/frontend/src/components/renderers/Latex.js b/frontend/src/components/renderers/Latex.js new file mode 100644 index 0000000..c5dd57c --- /dev/null +++ b/frontend/src/components/renderers/Latex.js @@ -0,0 +1,38 @@ +import React from 'react'; +import { BlockMath, InlineMath } from 'react-katex'; +import 'katex/dist/katex.min.css'; +import styled from 'styled-components' + +const StyledInlineLatex = styled.div` + display: block; + margin-bottom: 0.5em; +` + +class Latex extends React.Component { + render() { + // split by line break chars (\\, \newline, \break) + const els = this.props.content.split(/\\\\|\\newline|\\break/) + + // if <=1 lines, just render block + if (els.length <= 1) { + return ( + <BlockMath> + {this.props.content} + </BlockMath> + ); + } else { + // new inline block for every line + const blocks = els.map(line => + <StyledInlineLatex> + <InlineMath> + {line} + </InlineMath> + </StyledInlineLatex> + ) + + return blocks; + } + } +} + +export default Latex
\ No newline at end of file diff --git a/frontend/src/components/renderers/Raw.js b/frontend/src/components/renderers/Raw.js index a8d0e31..7f8e7c1 100644 --- a/frontend/src/components/renderers/Raw.js +++ b/frontend/src/components/renderers/Raw.js @@ -1,6 +1,15 @@ import React from 'react'; +import styled from 'styled-components' import { FetchPaste } from '../../helpers/httpHelper' +const RawText = styled.pre` + word-wrap: break-word; + white-space: pre-wrap; + line-height: initial; + font-size: 0.8em; + padding: 0 1em; +` + class Raw extends React.Component { constructor(props) { @@ -11,18 +20,10 @@ class Raw extends React.Component { } render() { - const styles = { - wordWrap: "break-word", - whiteSpace: "pre-wrap", - lineHeight: "initial", - fontSize: "0.8em", - padding: "0 1em" - } - return ( - <pre style={styles}> + <RawText> {this.state.content} - </pre> + </RawText> ); } |