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]}>
|
|
|
|
|
<EditorControls code={content} language={language} setLanguage={setLanguage} theme={theme} setTheme={setTheme} />
|
|
|
|
|
<EditorTextArea code={content} setCode={setContent} language={language} />
|
|
|
|
|
</ThemeProvider>
|
2021-03-26 22:00:12 +00:00
|
|
|
</>
|
|
|
|
|
}
|