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

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-03-26 22:00:12 +00:00
import { useState, useEffect } from 'react';
2021-03-27 11:49:46 +00:00
import { ThemeProvider } from 'styled-components';
import ls from 'local-storage';
2021-03-26 22:00:12 +00:00
import EditorControls from './EditorControls';
import EditorTextArea from './EditorTextArea';
2021-03-27 11:49:46 +00:00
import themes from '../style/themes';
2021-03-26 22:00:12 +00:00
export default function Editor({ content, setContent, contentType }) {
const [language, setLanguage] = useState('plain');
2021-03-27 11:49:46 +00:00
const [theme, setTheme] = useState(() => {
const preference = ls.get('theme');
if (preference && themes[preference]) {
return preference;
} else {
return 'dark';
}
});
useEffect(() => {
if (theme === 'dark') {
ls.remove('theme');
} else {
ls.set('theme', theme);
}
}, [theme])
2021-03-26 22:00:12 +00:00
useEffect(() => {
if (contentType) {
setLanguage(contentType);
}
}, [contentType]);
return <>
2021-03-27 11:49:46 +00:00
<ThemeProvider theme={themes[theme]}>
2021-03-27 11:59:56 +00:00
<EditorControls code={content} setCode={setContent} language={language} setLanguage={setLanguage} theme={theme} setTheme={setTheme} />
2021-03-27 11:49:46 +00:00
<EditorTextArea code={content} setCode={setContent} language={language} />
</ThemeProvider>
2021-03-26 22:00:12 +00:00
</>
}