2017-05-18 02:36:31 +00:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
2017-08-03 05:10:58 +00:00
|
|
|
import styled from 'styled-components';
|
2017-12-03 19:13:35 +00:00
|
|
|
import type { SlateNodeProps } from '../types';
|
2017-08-03 05:10:58 +00:00
|
|
|
import CopyButton from './CopyButton';
|
2017-10-27 05:42:08 +00:00
|
|
|
import { color } from 'shared/styles/constants';
|
2017-05-18 02:36:31 +00:00
|
|
|
|
2018-02-04 20:30:22 +00:00
|
|
|
function getCopyText(node) {
|
|
|
|
return node.nodes.reduce((memo, line) => `${memo}${line.text}\n`, '');
|
|
|
|
}
|
|
|
|
|
2017-12-03 19:13:35 +00:00
|
|
|
export default function Code({
|
|
|
|
children,
|
|
|
|
node,
|
|
|
|
readOnly,
|
|
|
|
attributes,
|
|
|
|
}: SlateNodeProps) {
|
2018-02-04 20:30:22 +00:00
|
|
|
// TODO: There is a currently a bug in slate-prism that prevents code elements
|
|
|
|
// with a language class name from formatting correctly on first load.
|
|
|
|
// const language = node.data.get('language') || 'javascript';
|
2017-10-22 03:30:28 +00:00
|
|
|
|
2017-05-18 02:36:31 +00:00
|
|
|
return (
|
2017-11-27 04:34:45 +00:00
|
|
|
<Container {...attributes} spellCheck={false}>
|
2018-02-04 20:30:22 +00:00
|
|
|
{readOnly && <CopyButton text={getCopyText(node)} />}
|
|
|
|
<code>{children}</code>
|
2017-08-03 05:10:58 +00:00
|
|
|
</Container>
|
2017-05-18 02:36:31 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-03 05:10:58 +00:00
|
|
|
|
2018-02-04 20:30:22 +00:00
|
|
|
const Container = styled.div`
|
|
|
|
position: relative;
|
2017-10-22 03:30:28 +00:00
|
|
|
background: ${color.smokeLight};
|
2017-08-03 05:10:58 +00:00
|
|
|
border-radius: 4px;
|
|
|
|
border: 1px solid ${color.smokeDark};
|
|
|
|
|
|
|
|
code {
|
2018-02-04 20:30:22 +00:00
|
|
|
display: block;
|
2018-02-05 07:43:57 +00:00
|
|
|
overflow-x: scroll;
|
|
|
|
padding: 0.5em 1em;
|
2018-02-04 20:30:22 +00:00
|
|
|
line-height: 1.4em;
|
2017-08-03 05:10:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 20:30:22 +00:00
|
|
|
pre {
|
|
|
|
margin: 0;
|
|
|
|
}
|
2017-08-03 05:10:58 +00:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
> span {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|