1- import { FC as FunctionComponent , useState , useEffect , useMemo } from ' react' ;
1+ import { FC as FunctionComponent , useState , useEffect , useMemo } from " react" ;
22
3- import { Box , Flex , Typography } from ' @strapi/design-system' ;
4- import type { Schema } from ' @strapi/types' ;
5- import MDEditor , { commands , ICommand } from ' @uiw/react-md-editor' ;
6- import { useIntl } from ' react-intl' ;
7- import { styled } from ' styled-components' ;
3+ import { Box , Flex , Typography } from " @strapi/design-system" ;
4+ import type { Schema } from " @strapi/types" ;
5+ import MDEditor , { commands , ICommand } from " @uiw/react-md-editor" ;
6+ import { useIntl } from " react-intl" ;
7+ import { styled } from " styled-components" ;
88
9- import '@uiw/react-markdown-preview/markdown.css' ;
10-
11- import pluginId from '../pluginId' ;
12- import { MediaLib } from './MediaLib' ;
9+ import "@uiw/react-markdown-preview/markdown.css" ;
1310
11+ import pluginId from "../pluginId" ;
12+ import { MediaLib } from "./MediaLib" ;
13+ import { useField } from "@strapi/strapi/admin" ;
1414
1515const Wrapper = styled . div `
1616 > div:nth-child(2) {
@@ -58,80 +58,76 @@ const Wrapper = styled.div`
5858` ;
5959
6060interface EditorProps {
61- name : string ,
62- onChange : ( e : { target : { name : string ; value : string } } ) => void ,
63- value : string ,
61+ name : string ;
62+ onChange : ( e : { target : { name : string ; value : string } } ) => void ;
63+ value : string ;
6464 intlLabel : {
65- id : string ,
66- defaultMessage : string ,
67- } ,
68- disabled ?: boolean ,
69- error ?: string ,
65+ id : string ;
66+ defaultMessage : string ;
67+ } ;
68+ disabled ?: boolean ;
69+ error ?: string ;
7070 description ?: {
71- id : string ,
72- defaultMessage : string ,
73- } ,
74- required ?: boolean ,
71+ id : string ;
72+ defaultMessage : string ;
73+ } ;
74+ required ?: boolean ;
7575}
7676
7777const Editor : FunctionComponent < EditorProps > = ( {
7878 name,
79- onChange,
80- value,
8179 intlLabel,
8280 disabled,
8381 error,
8482 description,
8583 required,
8684} ) => {
87- const { formatMessage} = useIntl ( ) ;
85+ // const { formatMessage } = useIntl();
86+ const { onChange, value } : any = useField ( name ) ;
87+ const formatMessage = ( message : { id : string ; defaultMessage : string } ) =>
88+ message ?. defaultMessage ?? "" ;
8889 const [ mediaLibVisible , setMediaLibVisible ] = useState ( false ) ;
8990 const [ mediaLibSelection , setMediaLibSelection ] = useState ( - 1 ) ;
9091
9192 const handleToggleMediaLib = ( ) => setMediaLibVisible ( ( prev ) => ! prev ) ;
9293
9394 const handleChangeAssets = ( assets : Schema . Attribute . MediaValue < true > ) => {
94- let newValue = value ? value : '' ;
95+ let newValue = value ? value : "" ;
9596 assets . map ( ( asset ) => {
96- if ( asset . mime . includes ( ' image' ) ) {
97+ if ( asset . mime . includes ( " image" ) ) {
9798 const imgTag = `` ;
9899 if ( mediaLibSelection > - 1 ) {
99- const preValue = value ?. substring ( 0 , mediaLibSelection ) ?? '' ;
100- const postValue = value ?. substring ( mediaLibSelection ) ?? '' ;
100+ const preValue = value ?. substring ( 0 , mediaLibSelection ) ?? "" ;
101+ const postValue = value ?. substring ( mediaLibSelection ) ?? "" ;
101102 newValue = `${
102- preValue && ! preValue . endsWith ( ' ' ) ? preValue + ' ' : preValue
103+ preValue && ! preValue . endsWith ( " " ) ? preValue + " " : preValue
103104 } ${ imgTag } ${
104- postValue && ! postValue . startsWith ( ' ' ) ?
105- ' ' + postValue :
106- postValue
105+ postValue && ! postValue . startsWith ( " " )
106+ ? " " + postValue
107+ : postValue
107108 } `;
108109 } else {
109110 newValue = `${ newValue } ${ imgTag } ` ;
110111 }
111112 }
112113 // Handle videos and other type of files by adding some code
113114 } ) ;
114- onChange ( { target : { name,
115- value : newValue ?? '' } } ) ;
115+ onChange ( { target : { name, value : newValue ?? "" } } ) ;
116116 handleToggleMediaLib ( ) ;
117117 } ;
118118
119- const [ configs , setConfigs ] = useState < { toolbarCommands ?: string [ ] } > ( { } ) ;
119+ const [ configs , setConfigs ] = useState < { toolbarCommands ?: string [ ] } > ( { } ) ;
120120
121121 const toolbarCommands = useMemo ( ( ) => {
122122 const strapiMediaLibrary : ICommand = {
123- name : ' image' ,
124- keyCommand : ' image' ,
125- buttonProps : { ' aria-label' : ' Insert title3' } ,
123+ name : " image" ,
124+ keyCommand : " image" ,
125+ buttonProps : { " aria-label" : " Insert title3" } ,
126126 icon : (
127- < svg
128- width = '12'
129- height = '12'
130- viewBox = '0 0 20 20'
131- >
127+ < svg width = "12" height = "12" viewBox = "0 0 20 20" >
132128 < path
133- fill = ' currentColor'
134- d = ' M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z'
129+ fill = " currentColor"
130+ d = " M15 9c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm4-7H1c-.55 0-1 .45-1 1v14c0 .55.45 1 1 1h18c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 13l-6-5-2 2-4-5-4 8V4h16v11z"
135131 > </ path >
136132 </ svg >
137133 ) ,
@@ -164,15 +160,19 @@ const Editor: FunctionComponent<EditorProps> = ({
164160 commands . checkedListCommand ,
165161 ] as ICommand [ ] ;
166162 }
167- const customCommands = configs ?. toolbarCommands ?. map ( ( config ) => {
168- if ( config === 'strapiMediaLibrary' ) return strapiMediaLibrary ;
169- if (
170- config in commands &&
171- commands [ config as unknown as keyof typeof commands ]
172- ) {
173- return commands [ config as unknown as keyof typeof commands ] as ICommand ;
174- } ;
175- } ) . filter ( ( command ) : command is ICommand => command !== undefined ) ;
163+ const customCommands = configs ?. toolbarCommands
164+ ?. map ( ( config ) => {
165+ if ( config === "strapiMediaLibrary" ) return strapiMediaLibrary ;
166+ if (
167+ config in commands &&
168+ commands [ config as unknown as keyof typeof commands ]
169+ ) {
170+ return commands [
171+ config as unknown as keyof typeof commands
172+ ] as ICommand ;
173+ }
174+ } )
175+ . filter ( ( command ) : command is ICommand => command !== undefined ) ;
176176
177177 return customCommands ;
178178 } , [ JSON . stringify ( configs ) ] ) ;
@@ -188,18 +188,11 @@ const Editor: FunctionComponent<EditorProps> = ({
188188 return (
189189 < Flex gap = { 4 } >
190190 < Box >
191- < Typography
192- variant = 'pi'
193- fontWeight = 'bold'
194- >
191+ < Typography variant = "pi" fontWeight = "bold" >
195192 { formatMessage ( intlLabel ) }
196193 </ Typography >
197194 { required && (
198- < Typography
199- variant = 'pi'
200- fontWeight = 'bold'
201- textColor = 'danger600'
202- >
195+ < Typography variant = "pi" fontWeight = "bold" textColor = "danger600" >
203196 *
204197 </ Typography >
205198 ) }
@@ -208,33 +201,28 @@ const Editor: FunctionComponent<EditorProps> = ({
208201 < MDEditor
209202 hidden = { disabled }
210203 commands = { toolbarCommands }
211- value = { value || '' }
204+ value = { value || "" }
212205 onChange = { ( newValue ) => {
213- onChange ( { target : { name,
214- value : newValue || '' } } ) ;
206+ onChange ( { target : { name, value : newValue || "" } } ) ;
215207 } }
216208 />
217- < div style = { { padding : ' 50px 0 0 0' } } />
218- < MediaLib
209+ < div style = { { padding : " 50px 0 0 0" } } />
210+ { /* <MediaLib
219211 isOpen={mediaLibVisible}
220212 onChange={handleChangeAssets}
221213 onToggle={handleToggleMediaLib}
222- />
214+ /> */ }
223215 </ Wrapper >
224216 { error && (
225- < Typography
226- variant = 'pi'
227- textColor = 'danger600'
228- >
229- { formatMessage ( { id : error ,
230- defaultMessage : error } ) }
217+ < Typography variant = "pi" textColor = "danger600" >
218+ { formatMessage ( { id : error , defaultMessage : error } ) }
231219 </ Typography >
232220 ) }
233221 { description && (
234- < Typography variant = 'pi' > { formatMessage ( description ) } </ Typography >
222+ < Typography variant = "pi" > { formatMessage ( description ) } </ Typography >
235223 ) }
236224 </ Flex >
237225 ) ;
238226} ;
239227
240- export { Editor } ;
228+ export { Editor } ;
0 commit comments