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

93 lines
2.3 KiB
TypeScript
Raw Normal View History

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';
2021-03-26 22:00:12 +00:00
2022-11-06 22:46:13 +00:00
import usePreference from '../hooks/usePreference';
2025-06-20 08:35:23 +01:00
import { Themes } from '../style/themes.ts';
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;
2025-06-20 08:35:23 +01:00
theme: keyof Themes;
setTheme: (value: keyof Themes) => void;
setShowAbout: (value: boolean) => void;
2022-11-06 22:46:13 +00:00
}
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,
pasteId,
2025-06-20 08:35:23 +01:00
theme,
setTheme,
setShowAbout,
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 [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
2025-03-15 13:36:29 +00:00
const [wordWrap, setWordWrap] = usePreference<boolean>(
'wordwrap',
true,
() => true
);
2021-03-26 22:00:12 +00:00
useEffect(() => {
if (contentType) {
2026-02-23 20:34:19 +00:00
// eslint-disable-next-line react-hooks/set-state-in-effect
2021-03-26 22:00:12 +00:00
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 (
<>
2025-06-20 08:35:23 +01:00
<EditorGlobalStyle />
<EditorControls
actualContent={actualContent}
resetFunction={resetFunction}
language={language}
setLanguage={setLanguage}
readOnly={readOnly}
setReadOnly={setReadOnly}
theme={theme}
setTheme={setTheme}
wordWrap={wordWrap}
setWordWrap={setWordWrap}
zoom={zoom}
setShowAbout={setShowAbout}
/>
<EditorTextArea
forcedContent={forcedContent}
actualContent={actualContent}
setActualContent={setActualContent}
language={language}
fontSize={fontSize}
readOnly={readOnly}
wordWrap={wordWrap}
2026-02-23 20:34:19 +00:00
resetFunctionRef={resetFunction}
2025-06-20 08:35:23 +01:00
/>
2021-04-02 13:05:15 +01:00
</>
);
}