@@ -6,12 +6,14 @@ import { HTMLChangePlugin } from './plugins/toolbar/helper/HTMLChangePlugin';
6
6
import { isHtmlStringValueSafeForLexical } from './plugins/toolbar/helper/util' ;
7
7
import { useState } from 'react' ;
8
8
9
+ const isValueSafeForLexical = ( value : ValueSegment [ ] ) => {
10
+ const blankNodeMap = new Map < string , ValueSegment > ( ) ;
11
+ const initialValueString = convertSegmentsToString ( value , blankNodeMap ) ;
12
+ return isHtmlStringValueSafeForLexical ( initialValueString , blankNodeMap ) ;
13
+ } ;
14
+
9
15
export const HTMLEditor = ( { initialValue, onChange, ...baseEditorProps } : BaseEditorProps ) : JSX . Element => {
10
- const [ isValuePlaintext , setIsValuePlaintext ] = useState ( ( ) => {
11
- const blankNodeMap = new Map < string , ValueSegment > ( ) ;
12
- const initialValueString = convertSegmentsToString ( initialValue , blankNodeMap ) ;
13
- return ! isHtmlStringValueSafeForLexical ( initialValueString , blankNodeMap ) ;
14
- } ) ;
16
+ const [ isValuePlaintext , setIsValuePlaintext ] = useState ( ( ) => ! isValueSafeForLexical ( initialValue ) ) ;
15
17
const [ isSwitchFromPlaintextBlocked , setIsSwitchFromPlaintextBlocked ] = useState ( ( ) => isValuePlaintext ) ;
16
18
const [ value , setValue ] = useState < ValueSegment [ ] > ( initialValue ) ;
17
19
@@ -23,6 +25,11 @@ export const HTMLEditor = ({ initialValue, onChange, ...baseEditorProps }: BaseE
23
25
onChange ?.( { value : value } ) ;
24
26
} ;
25
27
28
+ const handleSetIsValuePlaintext = ( newIsPlaintext : boolean ) => {
29
+ setIsValuePlaintext ( newIsPlaintext ) ;
30
+ setIsSwitchFromPlaintextBlocked ( ! isValueSafeForLexical ( value ) ) ;
31
+ } ;
32
+
26
33
return (
27
34
< EditorWrapper
28
35
{ ...baseEditorProps }
@@ -39,12 +46,11 @@ export const HTMLEditor = ({ initialValue, onChange, ...baseEditorProps }: BaseE
39
46
} }
40
47
isSwitchFromPlaintextBlocked = { isSwitchFromPlaintextBlocked }
41
48
onBlur = { handleBlur }
42
- setIsValuePlaintext = { setIsValuePlaintext }
49
+ setIsValuePlaintext = { handleSetIsValuePlaintext }
43
50
>
44
51
< HTMLChangePlugin
45
52
isValuePlaintext = { isValuePlaintext }
46
53
setIsSwitchFromPlaintextBlocked = { setIsSwitchFromPlaintextBlocked }
47
- setIsValuePlaintext = { setIsValuePlaintext }
48
54
setValue = { onValueChange }
49
55
/>
50
56
</ EditorWrapper >
0 commit comments