2023-01-08 20:33:17 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2022-01-27 23:26:16 +00:00
|
|
|
import { isMobile } from 'react-device-detect';
|
2022-11-06 22:46:13 +00:00
|
|
|
import { ThemeProvider } from 'styled-components';
|
2021-03-26 22:00:12 +00:00
|
|
|
|
2022-11-06 22:46:13 +00:00
|
|
|
import usePreference from '../hooks/usePreference';
|
|
|
|
|
import themes, { Themes } from '../style/themes';
|
2021-03-26 22:00:12 +00:00
|
|
|
import EditorControls from './EditorControls';
|
2022-11-06 22:46:13 +00:00
|
|
|
import EditorGlobalStyle from './EditorGlobalStyle';
|
2021-03-26 22:00:12 +00:00
|
|
|
import EditorTextArea from './EditorTextArea';
|
2022-11-06 22:46:13 +00:00
|
|
|
|
|
|
|
|
export interface EditorProps {
|
|
|
|
|
forcedContent: string;
|
|
|
|
|
actualContent: string;
|
|
|
|
|
setActualContent: (value: string) => void;
|
|
|
|
|
contentType?: string;
|
|
|
|
|
pasteId?: string;
|
|
|
|
|
}
|
2021-03-26 22:00:12 +00:00
|
|
|
|
2023-01-08 20:33:17 +00:00
|
|
|
export type ResetFunction = () => void;
|
|
|
|
|
|
2022-01-08 19:10:27 +00:00
|
|
|
export default function Editor({
|
|
|
|
|
forcedContent,
|
|
|
|
|
actualContent,
|
|
|
|
|
setActualContent,
|
|
|
|
|
contentType,
|
2022-04-03 19:47:01 +01:00
|
|
|
pasteId,
|
2022-11-06 22:46:13 +00:00
|
|
|
}: EditorProps) {
|
|
|
|
|
const [language, setLanguage] = useState<string>('plain');
|
|
|
|
|
const [readOnly, setReadOnly] = useState<boolean>(isMobile && !!pasteId);
|
2024-12-08 22:11:46 +00:00
|
|
|
const resetFunction = useRef<ResetFunction>(null);
|
2022-01-08 19:10:27 +00:00
|
|
|
|
2022-11-06 22:46:13 +00:00
|
|
|
const [theme, setTheme] = usePreference<keyof Themes>(
|
2021-04-02 13:05:15 +01:00
|
|
|
'theme',
|
|
|
|
|
'dark',
|
|
|
|
|
pref => !!themes[pref]
|
|
|
|
|
);
|
2022-11-06 22:46:13 +00:00
|
|
|
const [fontSize, setFontSize, fontSizeCheck] = usePreference<number>(
|
2021-04-02 13:05:15 +01:00
|
|
|
'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]);
|
|
|
|
|
|
2022-11-06 22:46:13 +00:00
|
|
|
function zoom(delta: number) {
|
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}
|
2023-01-08 20:33:17 +00:00
|
|
|
resetFunction={resetFunction}
|
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}
|
2023-01-08 20:33:17 +00:00
|
|
|
resetFunction={resetFunction}
|
2021-04-02 13:05:15 +01:00
|
|
|
/>
|
|
|
|
|
</ThemeProvider>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|