1- import { useMemo , useRef , useState } from 'react' ;
1+ import { useEffect , useId , useMemo , useRef , useState } from 'react' ;
22
33import { ActionButton , Button , Input , Tabs , TabsContent , TabsList , TabsTrigger } from '@/components' ;
44import { Dialog , DialogContent , DialogTitle , DialogTrigger } from '@/components/ui/dialog' ;
5- import { useLocale } from '@/locales' ;
6- import { actionDialogVideo , useDialogVideo } from '@/extensions/Video/store' ;
75import { Video } from '@/extensions/Video/Video' ;
6+ import { useLocale } from '@/locales' ;
7+ import { listenEvent } from '@/utils/customEvents/customEvents' ;
8+ import { eventName } from '@/utils/customEvents/events.constant' ;
89
910function checkIsVideo ( url : string ) {
1011 return / \. (?: m p 4 | w e b m | o g g | m o v ) $ / i. test ( url ) ;
@@ -16,8 +17,23 @@ function ActionVideoButton(props: any) {
1617 const [ link , setLink ] = useState < string > ( '' ) ;
1718 const fileInput = useRef < HTMLInputElement > ( null ) ;
1819
19- const dialogVideo = useDialogVideo ( ) ;
2020 const [ error , setError ] = useState < string > ( '' ) ;
21+ const id = useId ( ) ;
22+ const [ open , setOpen ] = useState ( false ) ;
23+
24+ const handleUploadVideo = ( evt : any ) => {
25+ setOpen ( evt . detail ) ;
26+ } ;
27+
28+ useEffect ( ( ) => {
29+ eventName . setEventNameUploadVideo ( id ) ;
30+
31+ const rm1 = listenEvent ( eventName . getEventNameUploadVideo ( ) , handleUploadVideo ) ;
32+
33+ return ( ) => {
34+ rm1 ( ) ;
35+ } ;
36+ } , [ ] ) ;
2137
2238 const uploadOptions = useMemo ( ( ) => {
2339 const uploadOptions = props . editor . extensionManager . extensions . find (
@@ -43,13 +59,13 @@ function ActionVideoButton(props: any) {
4359
4460 props . editor
4561 . chain ( )
62+ . focus ( )
4663 . setVideo ( {
4764 src,
4865 width : '100%' ,
4966 } )
50- . focus ( )
5167 . run ( ) ;
52- actionDialogVideo . setOpen ( false ) ;
68+ setOpen ( false ) ;
5369 }
5470 function handleLink ( e : any ) {
5571 e . preventDefault ( ) ;
@@ -61,13 +77,13 @@ function ActionVideoButton(props: any) {
6177
6278 props . editor
6379 . chain ( )
80+ . focus ( )
6481 . setVideo ( {
6582 src : link ,
6683 width : '100%' ,
6784 } )
68- . focus ( )
6985 . run ( ) ;
70- actionDialogVideo . setOpen ( false ) ;
86+ setOpen ( false ) ;
7187 }
7288
7389 function handleClick ( e : any ) {
@@ -76,11 +92,13 @@ function ActionVideoButton(props: any) {
7692 }
7793
7894 return (
79- < Dialog open = { dialogVideo } onOpenChange = { actionDialogVideo . setOpen } >
95+ < Dialog onOpenChange = { setOpen }
96+ open = { open }
97+ >
8098 < DialogTrigger asChild >
8199 < ActionButton
100+ action = { ( ) => setOpen ( true ) }
82101 icon = { props . icon }
83- action = { ( ) => actionDialogVideo . setOpen ( true ) }
84102 tooltip = { props . tooltip }
85103 />
86104 </ DialogTrigger >
@@ -91,17 +109,18 @@ function ActionVideoButton(props: any) {
91109 </ DialogTitle >
92110
93111 < Tabs
112+ activationMode = "manual"
94113 defaultValue = {
95114 ( uploadOptions ?. resourceVideo === 'both' || uploadOptions ?. resourceVideo === 'upload' ) ? 'upload' : 'link'
96115 }
97- activationMode = "manual"
98116 >
99117 < TabsList className = "richtext-grid richtext-w-full richtext-grid-cols-2" >
100118 { ( uploadOptions ?. resourceVideo === 'both' || uploadOptions ?. resourceVideo === 'upload' ) && (
101119 < TabsTrigger value = "upload" >
102120 { t ( 'editor.video.dialog.tab.upload' ) }
103121 </ TabsTrigger >
104122 ) }
123+
105124 { ( uploadOptions ?. resourceVideo === 'both' || uploadOptions ?. resourceVideo === 'link' ) && (
106125 < TabsTrigger value = "link" >
107126 { t ( 'editor.video.dialog.link' ) }
@@ -111,27 +130,34 @@ function ActionVideoButton(props: any) {
111130
112131 < TabsContent value = "upload" >
113132 < div className = "richtext-flex richtext-items-center richtext-gap-[10px]" >
114- < Button className = "richtext-w-full richtext-mt-1" size = "sm" onClick = { handleClick } >
133+ < Button className = "richtext-mt-1 richtext-w-full"
134+ onClick = { handleClick }
135+ size = "sm"
136+ >
115137 { t ( 'editor.video.dialog.tab.upload' ) }
116138 </ Button >
117139 </ div >
140+
118141 < input
119- type = "file"
120142 accept = "video/*"
121- ref = { fileInput }
122143 multiple
144+ onChange = { handleFile }
145+ ref = { fileInput }
146+ type = "file"
123147 style = { {
124148 display : 'none' ,
125149 } }
126- onChange = { handleFile }
127150 />
128151 </ TabsContent >
152+
129153 < TabsContent value = "link" >
130154 < form onSubmit = { handleLink } >
131155 < div className = "richtext-flex richtext-items-center richtext-gap-2" >
132156 < Input
133- type = "url"
134157 autoFocus
158+ placeholder = { t ( 'editor.video.dialog.placeholder' ) }
159+ required
160+ type = "url"
135161 value = { link }
136162 onChange = { ( e ) => {
137163 const url = e . target . value ;
@@ -146,16 +172,15 @@ function ActionVideoButton(props: any) {
146172 setError ( '' ) ;
147173 setLink ( url ) ;
148174 } }
149- required
150- placeholder = { t ( 'editor.video.dialog.placeholder' ) }
151175 />
152176
153177 < Button type = "submit" >
154178 { t ( 'editor.video.dialog.button.apply' ) }
155179 </ Button >
156180 </ div >
157181 </ form >
158- { error && < div className = "richtext-text-red-500 richtext-my-[5px]" >
182+
183+ { error && < div className = "richtext-my-[5px] richtext-text-red-500" >
159184 { error }
160185 </ div > }
161186 </ TabsContent >
0 commit comments