Skip to content

Commit 9c51636

Browse files
committed
chore: MDXEditor에서 불필요한 부분 제거
1 parent cc77285 commit 9c51636

File tree

2 files changed

+17
-76
lines changed

2 files changed

+17
-76
lines changed

apps/pyconkr/src/debug/page/mdi_test.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ import * as Common from "@frontend/common";
77
const HalfWidthStyle: React.CSSProperties = { width: "50%", maxWidth: "50%" };
88

99
export const MdiTestPage: React.FC = () => {
10-
const inputRef = React.useRef<HTMLTextAreaElement>(null);
1110
const [state, setState] = React.useState<{ text: string, resetKey: number }>({ text: "", resetKey: Math.random() });
12-
const setMDXInput = (text: string) => setState({ text, resetKey: Math.random() });
13-
const onLoad = (text: string) => {
14-
setState((prev) => ({ ...prev, text }));
15-
if (inputRef.current) inputRef.current.value = text;
16-
}
11+
const setMDXInput = (input?: string) => setState({ text: input || "", resetKey: Math.random() });
1712

1813
return (
1914
<Stack direction="row" spacing={2} sx={{ width: "100%", height: "100%", minHeight: "100%", maxHeight: "100%", flexGrow: 1, py: 2 }}>
20-
<Box sx={HalfWidthStyle}><Common.Components.MDXEditor inputRef={inputRef} defaultValue={state.text} onLoad={onLoad} onSave={setMDXInput} ctrlSMode="save" /></Box>
15+
<Box sx={HalfWidthStyle}><Common.Components.MDXEditor defaultValue={state.text} onChange={setMDXInput} /></Box>
2116
<Box sx={HalfWidthStyle}><Common.Components.MDXRenderer {...state} /></Box>
2217
</Stack>
2318
);

packages/common/src/components/mdx_editor.tsx

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
import * as React from "react";
22

3-
import { Apps, Save } from "@mui/icons-material";
4-
import { Button, ButtonProps, MenuItem, Select, Stack, Typography } from "@mui/material";
5-
import MDEditor, { GroupOptions, RefMDEditor, commands } from '@uiw/react-md-editor';
6-
// import * as CryptoJS from "crypto-js";
3+
import { Apps } from "@mui/icons-material";
4+
import { Button, MenuItem, Select, Stack, Typography } from "@mui/material";
5+
import MDEditor, { GroupOptions, ICommand, commands } from '@uiw/react-md-editor';
76
import type { MDXComponents } from "mdx/types";
7+
// import * as CryptoJS from "crypto-js";
88

99
import Hooks from "../hooks";
1010

11-
const LOCAL_STORAGE_KEY = "mdx_editor_input_";
12-
1311
type CustomComponentInfoType = {
1412
k: string; // key
1513
n: string; // name
1614
v?: MDXComponents[string]; // value
1715
}
1816

1917
type MDXEditorProps = {
20-
sectionId?: string;
18+
disabled?: boolean;
2119
defaultValue?: string;
22-
inputRef?: React.RefObject<HTMLTextAreaElement | null>;
23-
onLoad?: (value: string) => void;
24-
onSave?: (value: string) => void;
25-
ctrlSMode?: "ignore" | "save";
26-
submitActions?: ButtonProps[];
20+
onChange?: (value?: string) => void;
21+
extraCommands?: ICommand[];
2722
};
2823

2924
const TextEditorStyle: React.CSSProperties = {
@@ -38,8 +33,6 @@ const TextEditorStyle: React.CSSProperties = {
3833
fieldSizing: 'content',
3934
} as React.CSSProperties;
4035

41-
const getDefaultValueFromLocalStorage = (sectionId?: string): string => localStorage.getItem(LOCAL_STORAGE_KEY + (sectionId || "unknown")) ?? "";
42-
4336
// const calculateMD5FromFileBase64 = (fileBase64: string): string => CryptoJS.MD5(CryptoJS.enc.Base64.parse(fileBase64)).toString();
4437

4538
// const onFileInEvent: React.DragEventHandler<HTMLDivElement> = (event) => {
@@ -108,18 +101,9 @@ const getCustomComponentSelector: (registeredComponentList: CustomComponentInfoT
108101
</Stack>;
109102
}
110103

111-
export const MDXEditor: React.FC<MDXEditorProps> = ({ sectionId, defaultValue, inputRef, onLoad, onSave, ctrlSMode, submitActions }) => {
112-
const [value, setValue] = React.useState<string>(defaultValue || getDefaultValueFromLocalStorage(sectionId));
104+
export const MDXEditor: React.FC<MDXEditorProps> = ({ disabled, defaultValue, onChange, extraCommands }) => {
113105
const { mdxComponents } = Hooks.Common.useCommonContext();
114106

115-
if (!inputRef) inputRef = React.useRef<HTMLTextAreaElement | null>(null);
116-
const setRef: React.RefAttributes<RefMDEditor>["ref"] = (n) => {
117-
if (n?.textarea) {
118-
!inputRef.current && onLoad?.(n.textarea.value);
119-
inputRef.current = n.textarea
120-
}
121-
}
122-
123107
const registeredComponentList: CustomComponentInfoType[] = [
124108
{ k: "", n: "", v: undefined },
125109
...Object.entries(mdxComponents ?? {}).map(([k, v]) => {
@@ -129,41 +113,16 @@ export const MDXEditor: React.FC<MDXEditorProps> = ({ sectionId, defaultValue, i
129113
})
130114
];
131115

132-
const onSaveAction = () => {
133-
if (!inputRef.current) return;
134-
135-
setValue(inputRef.current.value);
136-
onSave?.(inputRef.current.value);
137-
localStorage.setItem(LOCAL_STORAGE_KEY + (sectionId || "unknown"), inputRef.current.value);
138-
alert("저장했습니다.");
139-
}
140-
141-
const handleCtrlSAction: (this: GlobalEventHandlers, ev: KeyboardEvent) => any = (event) => {
142-
if (event.key === "s" && (event.ctrlKey || event.metaKey)) {
143-
event.preventDefault();
144-
event.stopPropagation();
145-
console.log(`Ctrl+S pressed, executing ${ctrlSMode} action`);
146-
ctrlSMode === "save" && onSaveAction();
147-
}
148-
}
149-
150-
React.useEffect(
151-
ctrlSMode ? () => {
152-
document.addEventListener("keydown", handleCtrlSAction);
153-
return () => {
154-
console.log("Removing event listener for Ctrl+S action");
155-
document.removeEventListener("keydown", handleCtrlSAction);
156-
}
157-
} : () => { }, [inputRef.current]
158-
)
159-
160116
return <Stack direction="column" spacing={2} sx={{ width: "100%", height: "100%", maxWidth: "100%" }}>
161117
<MDEditor
118+
data-color-mode="light"
119+
textareaProps={{ disabled }}
162120
preview="edit"
163121
highlightEnable={true}
164-
ref={setRef}
165-
value={value}
166-
onChange={(v) => setValue(v || "")}
122+
height="none"
123+
minHeight={0}
124+
value={defaultValue}
125+
onChange={onChange}
167126
commands={[
168127
commands.group(
169128
[
@@ -201,21 +160,8 @@ export const MDXEditor: React.FC<MDXEditorProps> = ({ sectionId, defaultValue, i
201160
buttonProps: { 'aria-label': 'Insert custom component' }
202161
}),
203162
]}
204-
extraCommands={[
205-
commands.group([], {
206-
name: 'save',
207-
groupName: 'save',
208-
icon: <Save style={{ fontSize: 12 }} />,
209-
execute: onSaveAction,
210-
buttonProps: { 'aria-label': 'Save' }
211-
})
212-
]}
163+
extraCommands={extraCommands}
213164
style={TextEditorStyle}
214165
/>
215-
{
216-
submitActions && <Stack direction="row" spacing={2} sx={{ mt: 2 }}>
217-
{submitActions.map((buttonProps, index) => <Button key={index} {...buttonProps} />)}
218-
</Stack>
219-
}
220166
</Stack>;
221167
};

0 commit comments

Comments
 (0)