2021-03-26 22:00:12 +00:00
|
|
|
import { useState, useEffect } from 'react';
|
2021-12-12 20:02:44 +00:00
|
|
|
import { ThemeProvider, createGlobalStyle } from 'styled-components';
|
2021-03-27 11:49:46 +00:00
|
|
|
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
|
|
|
|
2022-01-08 19:10:27 +00:00
|
|
|
export default function Editor({
|
|
|
|
|
forcedContent,
|
|
|
|
|
setForcedContent,
|
|
|
|
|
actualContent,
|
|
|
|
|
setActualContent,
|
|
|
|
|
contentType,
|
|
|
|
|
}) {
|
2021-03-26 22:00:12 +00:00
|
|
|
const [language, setLanguage] = useState('plain');
|
2022-01-08 19:10:27 +00:00
|
|
|
|
2021-04-02 13:05:15 +01:00
|
|
|
const [theme, setTheme] = usePreference(
|
|
|
|
|
'theme',
|
|
|
|
|
'dark',
|
|
|
|
|
pref => !!themes[pref]
|
|
|
|
|
);
|
|
|
|
|
const [fontSize, setFontSize, fontSizeCheck] = usePreference(
|
|
|
|
|
'fontsize',
|
|
|
|
|
16,
|
|
|
|
|
pref => pref >= 10 && pref <= 22
|
|
|
|
|
);
|
2021-03-27 11:49:46 +00:00
|
|
|
|
2021-03-26 22:00:12 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (contentType) {
|
|
|
|
|
setLanguage(contentType);
|
|
|
|
|
}
|
|
|
|
|
}, [contentType]);
|
|
|
|
|
|
2021-03-27 14:17:28 +00:00
|
|
|
function zoom(delta) {
|
2021-04-02 13:05:15 +01:00
|
|
|
const newFontSize = fontSize + delta;
|
|
|
|
|
if (fontSizeCheck(newFontSize)) {
|
|
|
|
|
setFontSize(newFontSize);
|
2021-03-27 14:17:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 13:05:15 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ThemeProvider theme={themes[theme]}>
|
2021-12-12 20:02:44 +00:00
|
|
|
<EditorGlobalStyle />
|
2021-04-02 13:05:15 +01:00
|
|
|
<EditorControls
|
2022-01-08 19:10:27 +00:00
|
|
|
actualContent={actualContent}
|
|
|
|
|
setForcedContent={setForcedContent}
|
2021-04-02 13:05:15 +01:00
|
|
|
language={language}
|
|
|
|
|
setLanguage={setLanguage}
|
|
|
|
|
theme={theme}
|
|
|
|
|
setTheme={setTheme}
|
|
|
|
|
zoom={zoom}
|
|
|
|
|
/>
|
|
|
|
|
<EditorTextArea
|
2022-01-08 19:10:27 +00:00
|
|
|
forcedContent={forcedContent}
|
|
|
|
|
setActualContent={setActualContent}
|
|
|
|
|
theme={themes[theme]}
|
2021-04-02 13:05:15 +01:00
|
|
|
language={language}
|
|
|
|
|
fontSize={fontSize}
|
|
|
|
|
/>
|
|
|
|
|
</ThemeProvider>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 20:02:44 +00:00
|
|
|
const EditorGlobalStyle = createGlobalStyle`
|
|
|
|
|
html, body {
|
|
|
|
|
color-scheme: ${props => props.theme.lightOrDark};
|
|
|
|
|
scrollbar-color: ${props => props.theme.lightOrDark};
|
|
|
|
|
background-color: ${props => props.theme.editor.background};
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2021-04-02 13:05:15 +01:00
|
|
|
// hook used to load "preference" settings from local storage, or fall back to a default value.
|
|
|
|
|
function usePreference(id, defaultValue, valid) {
|
|
|
|
|
const [value, setValue] = useState(() => {
|
|
|
|
|
const pref = ls.get(id);
|
|
|
|
|
if (pref && valid(pref)) {
|
|
|
|
|
return pref;
|
|
|
|
|
} else {
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (value === defaultValue) {
|
|
|
|
|
ls.remove(id);
|
|
|
|
|
} else {
|
|
|
|
|
ls.set(id, value);
|
|
|
|
|
}
|
|
|
|
|
}, [value, id, defaultValue]);
|
|
|
|
|
|
|
|
|
|
return [value, setValue, valid];
|
2021-03-26 22:00:12 +00:00
|
|
|
}
|