2021-03-27 11:49:46 +00:00
|
|
|
import styled from 'styled-components';
|
2021-03-26 22:00:12 +00:00
|
|
|
import ReactEditor from 'react-simple-code-editor';
|
2021-03-27 11:49:46 +00:00
|
|
|
import EditorPrismStyle from './EditorPrismStyle';
|
2021-03-26 22:00:12 +00:00
|
|
|
import { getHighlighter } from '../highlighting';
|
|
|
|
|
|
|
|
|
|
export default function EditorTextArea({ code, setCode, language }) {
|
|
|
|
|
const highlight = getHighlighter(language);
|
|
|
|
|
|
|
|
|
|
function highlightWithLineNumbers(input, grammar) {
|
|
|
|
|
return highlight(input, grammar)
|
|
|
|
|
.split('\n')
|
|
|
|
|
.map((line, i) => `<span class='editorLineNumber'>${i + 1}</span>${line}`)
|
|
|
|
|
.join('\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2021-03-27 11:49:46 +00:00
|
|
|
<EditorPrismStyle>
|
|
|
|
|
<StyledReactEditor
|
2021-03-26 22:00:12 +00:00
|
|
|
value={code}
|
|
|
|
|
onValueChange={setCode}
|
|
|
|
|
highlight={highlightWithLineNumbers}
|
|
|
|
|
placeholder={'Type some code...'}
|
|
|
|
|
padding={10}
|
|
|
|
|
textareaId='code-area'
|
|
|
|
|
/>
|
2021-03-27 11:49:46 +00:00
|
|
|
</EditorPrismStyle>
|
2021-03-26 22:00:12 +00:00
|
|
|
)
|
|
|
|
|
}
|
2021-03-27 11:49:46 +00:00
|
|
|
|
|
|
|
|
const StyledReactEditor = styled(ReactEditor)`
|
|
|
|
|
counter-reset: line;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
outline: 0;
|
|
|
|
|
min-height: calc(100vh - 2em);
|
|
|
|
|
|
|
|
|
|
#code-area {
|
|
|
|
|
outline: none;
|
|
|
|
|
padding-left: 60px !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pre {
|
|
|
|
|
padding-left: 60px !important;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.editorLineNumber {
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: 0px;
|
|
|
|
|
color: ${props => props.theme.editor.lineNumber};
|
|
|
|
|
text-align: right;
|
|
|
|
|
width: 40px;
|
|
|
|
|
font-weight: 100;
|
|
|
|
|
}
|
|
|
|
|
`;
|