1
1
import React , { useRef , useState } from 'react'
2
2
3
- const Editor = ( ) => {
4
- const [ inputText , setInputText ] = useState ( '' ) ;
5
- const [ outputText , setOutputText ] = useState ( '' ) ;
3
+ const Editor = ( { dataValue, OutputTest } ) => {
6
4
const textareaRef = useRef ( null ) ;
7
5
8
- const handlePreview = ( ) => {
9
- setOutputText ( inputText ) ;
10
- } ;
11
-
12
6
// headle the bold
13
7
const handleBold = ( ) => {
14
8
const textarea = textareaRef . current ; // get the current selected area in textarea
@@ -17,14 +11,15 @@ const Editor = () => {
17
11
18
12
if ( start === end ) return ; // No text is selected
19
13
20
- const before = inputText . substring ( 0 , start ) ; // get the before string in selection start
21
- const selected = inputText . substring ( start , end ) ; // get the selected text
22
- const after = inputText . substring ( end ) ; // last sting of the selection
14
+ const before = dataValue . substring ( 0 , start ) ; // get the before string in selection start
15
+ const selected = dataValue . substring ( start , end ) ; // get the selected text
16
+ const after = dataValue . substring ( end ) ; // last sting of the selection
23
17
24
18
const newText = `${ before } **${ selected } **${ after } ` ; // ** use this for bold text
25
- setInputText ( newText ) ;
19
+ OutputTest ( newText ) ;
26
20
} ;
27
21
22
+
28
23
// headle the handleItalic
29
24
const handleItalic = ( ) => {
30
25
const textarea = textareaRef . current ; // get the current selected area in textarea
@@ -33,12 +28,12 @@ const Editor = () => {
33
28
34
29
if ( start === end ) return ; // No text is selected
35
30
36
- const before = inputText . substring ( 0 , start ) ; // get the before string in selection start
37
- const selected = inputText . substring ( start , end ) ; // get the selected text
38
- const after = inputText . substring ( end ) ; // last sting of the selection
31
+ const before = dataValue . substring ( 0 , start ) ; // get the before string in selection start
32
+ const selected = dataValue . substring ( start , end ) ; // get the selected text
33
+ const after = dataValue . substring ( end ) ; // last sting of the selection
39
34
40
35
const newText = `${ before } <i>${ selected } </i>${ after } ` ; // ** use this for bold text
41
- setInputText ( newText ) ;
36
+ OutputTest ( newText ) ;
42
37
} ;
43
38
44
39
@@ -50,31 +45,31 @@ const Editor = () => {
50
45
51
46
if ( start === end ) return ; // No text is selected
52
47
53
- const before = inputText . substring ( 0 , start ) ; // get the before string in selection start
54
- const selected = inputText . substring ( start , end ) ; // get the selected text
55
- const after = inputText . substring ( end ) ; // last sting of the selection
48
+ const before = dataValue . substring ( 0 , start ) ; // get the before string in selection start
49
+ const selected = dataValue . substring ( start , end ) ; // get the selected text
50
+ const after = dataValue . substring ( end ) ; // last sting of the selection
56
51
57
52
const newText = `${ before } <u>${ selected } </u>${ after } ` ; // ** use this for bold text
58
- setInputText ( newText ) ;
53
+ OutputTest ( newText ) ;
59
54
} ;
60
55
61
56
62
57
return (
63
- < div className = "min-h-screen bg-gray-100 p-4" >
58
+ < div className = "h-auto bg-gray-100 p-4" >
64
59
< div className = "flex mb-4" >
65
- < button
60
+ < button type = 'button'
66
61
className = "p-2 bg-green-500 text-white font-semibold rounded-md shadow-sm hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-500"
67
62
onClick = { handleBold }
68
63
>
69
64
Bold
70
65
</ button >
71
- < button
66
+ < button type = 'button'
72
67
className = "mx-4 p-2 bg-green-500 text-white font-semibold rounded-md shadow-sm hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-500"
73
68
onClick = { handleItalic }
74
69
>
75
70
Italic
76
71
</ button >
77
- < button
72
+ < button type = 'button'
78
73
className = "mx-4 p-2 bg-green-500 text-white font-semibold rounded-md shadow-sm hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-500"
79
74
onClick = { handleUnderLine }
80
75
>
@@ -86,18 +81,9 @@ const Editor = () => {
86
81
className = "w-full p-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 mb-2"
87
82
rows = "20"
88
83
placeholder = "Type something here..."
89
- value = { inputText }
90
- onChange = { ( e ) => setInputText ( e . target . value ) }
84
+ value = { dataValue }
85
+ onChange = { ( e ) => OutputTest ( e . target . value ) }
91
86
> </ textarea >
92
- < button
93
- className = "p-2 mb-4 bg-blue-500 text-white font-semibold rounded-md shadow-sm hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 mr-2"
94
- onClick = { handlePreview }
95
- >
96
- Preview
97
- </ button >
98
- < div className = "w-full p-2 bg-white border border-gray-300 rounded-md shadow-sm" >
99
- < div className = "whitespace-pre-wrap" dangerouslySetInnerHTML = { { __html : outputText . replace ( / \* \* ( .* ?) \* \* / g, '<b>$1</b>' ) } } > </ div >
100
- </ div >
101
87
</ div >
102
88
)
103
89
}
0 commit comments