2020-06-29 04:46:34 +03:00
|
|
|
import * as React from "react";
|
|
|
|
import * as Constants from "~/common/constants";
|
2020-06-24 12:35:22 +03:00
|
|
|
|
2020-06-29 04:46:34 +03:00
|
|
|
import { css } from "@emotion/react";
|
2020-07-20 10:07:21 +03:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Refactor to https://github.com/FormidableLabs/prism-react-renderer
|
2020-07-15 06:54:46 +03:00
|
|
|
import Prism from "prismjs";
|
2020-06-24 12:35:22 +03:00
|
|
|
|
2020-07-09 05:57:21 +03:00
|
|
|
const STYLES_CODE_BLOCK = css`
|
2020-07-10 08:03:37 +03:00
|
|
|
box-sizing: border-box;
|
2020-07-15 06:54:46 +03:00
|
|
|
font-family: ${Constants.font.code};
|
2020-07-08 11:43:37 +03:00
|
|
|
background-color: ${Constants.system.pitchBlack};
|
2020-07-09 06:17:25 +03:00
|
|
|
color: ${Constants.system.white};
|
2020-07-08 11:43:37 +03:00
|
|
|
border-color: ${Constants.system.yellow};
|
2020-07-09 10:05:51 +03:00
|
|
|
font-size: 12px;
|
|
|
|
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2);
|
|
|
|
border-radius: 4px;
|
|
|
|
padding: 24px;
|
2020-07-20 09:36:32 +03:00
|
|
|
|
|
|
|
* {
|
|
|
|
white-space: pre-wrap;
|
|
|
|
overflow-wrap: break-word;
|
|
|
|
}
|
2020-06-24 12:35:22 +03:00
|
|
|
`;
|
|
|
|
|
2020-07-20 09:36:32 +03:00
|
|
|
const STYLES_LINE = css`
|
2020-07-10 08:03:37 +03:00
|
|
|
box-sizing: border-box;
|
2020-07-09 09:05:42 +03:00
|
|
|
display: flex;
|
2020-07-08 11:43:37 +03:00
|
|
|
align-items: flex-start;
|
|
|
|
justify-content: space-between;
|
|
|
|
`;
|
2020-07-09 02:55:46 +03:00
|
|
|
|
2020-07-09 05:57:21 +03:00
|
|
|
const STYLES_PRE = css`
|
2020-07-10 08:03:37 +03:00
|
|
|
box-sizing: border-box;
|
2020-07-09 10:05:51 +03:00
|
|
|
color: #666;
|
2020-07-15 06:54:46 +03:00
|
|
|
font-family: ${Constants.font.code};
|
2020-07-09 08:30:38 +03:00
|
|
|
flex-shrink: 0;
|
2020-07-09 10:05:51 +03:00
|
|
|
min-width: 32px;
|
|
|
|
user-select: none;
|
2020-07-08 11:43:37 +03:00
|
|
|
`;
|
|
|
|
|
2020-07-09 05:57:21 +03:00
|
|
|
const STYLES_CODE = css`
|
2020-07-10 08:03:37 +03:00
|
|
|
box-sizing: border-box;
|
2020-07-08 11:43:37 +03:00
|
|
|
background-color: ${Constants.system.pitchBlack};
|
2020-07-15 06:54:46 +03:00
|
|
|
font-family: ${Constants.font.code};
|
2020-07-08 11:43:37 +03:00
|
|
|
color: ${Constants.system.gray};
|
2020-07-09 08:30:38 +03:00
|
|
|
width: 100%;
|
2020-07-09 10:05:51 +03:00
|
|
|
padding-left: 16px;
|
2020-07-08 11:43:37 +03:00
|
|
|
`;
|
|
|
|
|
2020-07-16 21:03:52 +03:00
|
|
|
// TODO:
|
|
|
|
// Refactor to https://github.com/FormidableLabs/prism-react-renderer
|
2020-07-21 03:56:14 +03:00
|
|
|
class CodeBlock extends React.Component {
|
2020-07-15 06:54:46 +03:00
|
|
|
componentDidMount() {
|
|
|
|
Prism.highlightAll();
|
|
|
|
}
|
2020-07-16 21:03:52 +03:00
|
|
|
|
2020-07-08 11:43:37 +03:00
|
|
|
render() {
|
2020-07-09 10:05:51 +03:00
|
|
|
const codeBlockContent = this.props.children + "";
|
|
|
|
const codeBlockToken = codeBlockContent.split("\n");
|
2020-07-08 11:43:37 +03:00
|
|
|
const textMap = codeBlockToken;
|
|
|
|
|
|
|
|
return (
|
2020-07-16 21:03:52 +03:00
|
|
|
<div css={STYLES_CODE_BLOCK} className="language-javascript">
|
2020-07-08 11:43:37 +03:00
|
|
|
{textMap.map((element, index) => {
|
|
|
|
return (
|
2020-07-20 09:36:32 +03:00
|
|
|
<div css={STYLES_LINE} key={`${element}-${index}`}>
|
2020-07-09 10:05:51 +03:00
|
|
|
<div css={STYLES_PRE}>{index}</div>
|
2020-07-16 21:03:52 +03:00
|
|
|
<pre css={STYLES_CODE}>
|
2020-07-16 06:44:58 +03:00
|
|
|
<code>{element}</code>
|
|
|
|
</pre>
|
2020-07-09 10:05:51 +03:00
|
|
|
</div>
|
2020-07-08 11:43:37 +03:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 03:56:14 +03:00
|
|
|
|
|
|
|
export default CodeBlock;
|