slate/components/system/CodeBlock.js

87 lines
2.0 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as Constants from "~/common/constants";
import { css } from "@emotion/react";
// TODO:
// Refactor to https://github.com/FormidableLabs/prism-react-renderer
import Prism from "prismjs";
2020-07-09 05:57:21 +03:00
const STYLES_CODE_BLOCK = css`
box-sizing: border-box;
font-family: ${Constants.font.code};
background-color: ${Constants.system.pitchBlack};
2020-07-09 06:17:25 +03:00
color: ${Constants.system.white};
border-color: ${Constants.system.yellow};
font-size: 12px;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2);
border-radius: 4px;
padding: 24px;
* {
white-space: pre-wrap;
overflow-wrap: break-word;
::-webkit-scrollbar {
-webkit-appearance: none;
width: 0;
height: 0;
}
}
`;
const STYLES_LINE = css`
box-sizing: border-box;
2020-07-09 09:05:42 +03:00
display: flex;
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`
box-sizing: border-box;
color: #666;
font-family: ${Constants.font.code};
2020-07-09 08:30:38 +03:00
flex-shrink: 0;
min-width: 32px;
user-select: none;
`;
2020-07-09 05:57:21 +03:00
const STYLES_CODE = css`
box-sizing: border-box;
background-color: ${Constants.system.pitchBlack};
font-family: ${Constants.font.code};
color: ${Constants.system.gray};
2020-07-09 08:30:38 +03:00
width: 100%;
padding-left: 16px;
`;
// TODO:
// Refactor to https://github.com/FormidableLabs/prism-react-renderer
class CodeBlock extends React.Component {
componentDidMount() {
Prism.highlightAll();
}
render() {
const codeBlockContent = this.props.children + "";
const codeBlockToken = codeBlockContent.split("\n");
const textMap = codeBlockToken;
return (
<div css={STYLES_CODE_BLOCK} className="language-javascript">
{textMap.map((element, index) => {
return (
<div css={STYLES_LINE} key={`${element}-${index}`}>
<div css={STYLES_PRE}>{index}</div>
<pre css={STYLES_CODE}>
<code>{element}</code>
</pre>
</div>
);
})}
</div>
);
}
}
export default CodeBlock;