Files
lucko-paste/src/components/EditorTextArea.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

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';
2021-03-27 14:17:28 +00:00
export default function EditorTextArea({ code, setCode, language, fontSize }) {
2021-03-26 22:00:12 +00:00
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}
2021-03-27 20:05:15 +00:00
placeholder={'Paste (or type) some code...'}
2021-03-26 22:00:12 +00:00
padding={10}
2021-03-27 14:17:28 +00:00
size={fontSize}
2021-03-26 22:00:12 +00:00
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;
2021-03-27 14:17:28 +00:00
font-size: ${props => props.size}px;
2021-03-27 11:49:46 +00:00
outline: 0;
2021-03-27 14:17:28 +00:00
min-height: calc(100vh - 2rem);
2021-03-27 11:49:46 +00:00
#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;
}
`;