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

102 lines
2.5 KiB
JavaScript
Raw Normal View History

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';
2022-01-27 23:26:16 +00:00
import { isMobile } from 'react-device-detect';
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,
}) {
2022-01-27 23:26:16 +00:00
const [language, setLanguage] = useState(isMobile);
const [readOnly, setReadOnly] = useState(true);
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}
2022-01-27 23:26:16 +00:00
readOnly={readOnly}
setReadOnly={setReadOnly}
2021-04-02 13:05:15 +01:00
theme={theme}
setTheme={setTheme}
zoom={zoom}
/>
<EditorTextArea
2022-01-08 19:10:27 +00:00
forcedContent={forcedContent}
2022-01-08 19:19:16 +00:00
actualContent={actualContent}
2022-01-08 19:10:27 +00:00
setActualContent={setActualContent}
theme={themes[theme]}
2021-04-02 13:05:15 +01:00
language={language}
fontSize={fontSize}
2022-01-27 23:26:16 +00:00
readOnly={readOnly}
2021-04-02 13:05:15 +01:00
/>
</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
}