From 1519c913241669b532d13351331e7a8d512e1438 Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Fri, 17 Jul 2020 22:29:38 -0700 Subject: refactor renderers --- frontend/src/components/renderers/Latex.js | 56 +++++++++++----------- frontend/src/components/renderers/Raw.js | 75 +++++++++++++----------------- 2 files changed, 59 insertions(+), 72 deletions(-) (limited to 'frontend/src/components/renderers') diff --git a/frontend/src/components/renderers/Latex.js b/frontend/src/components/renderers/Latex.js index dcb9ea3..fd3d4e2 100644 --- a/frontend/src/components/renderers/Latex.js +++ b/frontend/src/components/renderers/Latex.js @@ -8,37 +8,35 @@ const StyledInlineLatex = styled.div` margin-bottom: 1em; ` -class Latex extends React.Component { - render() { - // split by \begin{...} and \end{...} flags - const els = this.props.content.split(/(\\begin\{.*\}[\s\S]*?\\end\{.*\})/gm).map(line => { - // line doesnt start with \begin{...}, safe to split on \\ - if (!line.match(/^(\\begin\{.*\})/)) { - return line.split("\\\\") - } else { - return line - } - }).flat() - - // if <=1 lines, just render block - if (els.length <= 1) { - return ( - - {this.props.content} - - ); +const Latex = (props) => { + // split by \begin{...} and \end{...} flags + const els = props.content.split(/(\\begin\{.*\}[\s\S]*?\\end\{.*\})/gm).map(line => { + // line doesnt start with \begin{...}, safe to split on \\ + if (!line.match(/^(\\begin\{.*\})/)) { + return line.split("\\\\") } else { - // new inline block for every line - const blocks = els.map(line => - - - {line} - - - ) - - return blocks; + return line } + }).flat() + + // if <=1 lines, just render block + if (els.length <= 1) { + return ( + + {props.content} + + ); + } else { + // new inline block for every line + const blocks = els.map(line => + + + {line} + + + ) + + return blocks; } } diff --git a/frontend/src/components/renderers/Raw.js b/frontend/src/components/renderers/Raw.js index 7f8e7c1..d4dc830 100644 --- a/frontend/src/components/renderers/Raw.js +++ b/frontend/src/components/renderers/Raw.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import styled from 'styled-components' import { FetchPaste } from '../../helpers/httpHelper' @@ -10,48 +10,37 @@ const RawText = styled.pre` padding: 0 1em; ` -class Raw extends React.Component { - - constructor(props) { - super(props); - this.state = { - content: '', - }; - } - - render() { - return ( - - {this.state.content} - - ); - } - - componentDidMount() { - FetchPaste(this.props.hash) - .then((response) => { - const data = response.data - this.setState({ content: data.content }) - }).catch((error) => { - const resp = error.response - - // catch 401 unauth (password protected) - if (resp.status === 401) { - this.setState({ content: 'err: password protected' }) - return - } - - // some weird err - if (resp !== undefined) { - const errTxt = `${resp.statusText}: ${resp.data}` - this.setState({ content: errTxt }) - return - } - - // some weird err (e.g. network) - this.setState({ content: error }) - }) - } +const Raw = ({hash}) => { + const [content, setContent] = useState(''); + + useEffect(() => { + FetchPaste(hash) + .then((response) => { + const data = response.data + setContent(data.content) + }).catch((error) => { + const resp = error.response + + // catch 401 unauth (password protected) + if (resp.status === 401) { + setContent('err: password protected') + return + } + + // some weird err + if (resp !== undefined) { + const errTxt = `${resp.statusText}: ${resp.data}` + setContent(errTxt) + return + } + + // some weird err (e.g. network) + setContent(error) + })}, [hash]) + + return ( + {content} + ); } export default Raw \ No newline at end of file -- cgit v1.2.3