11import * 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' ;
76import type { MDXComponents } from "mdx/types" ;
7+ // import * as CryptoJS from "crypto-js";
88
99import Hooks from "../hooks" ;
1010
11- const LOCAL_STORAGE_KEY = "mdx_editor_input_" ;
12-
1311type CustomComponentInfoType = {
1412 k : string ; // key
1513 n : string ; // name
1614 v ?: MDXComponents [ string ] ; // value
1715}
1816
1917type 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
2924const 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