Skip to content

Commit 7ded280

Browse files
authored
fix(designer-ui): Remove problematic call to setIsValuePlaintext (#4979)
* Remove problematic call to `setIsValuePlaintext` * Clean up an unused variable * Ensure 'safe for lexical' is checked when switching view
1 parent 43070e2 commit 7ded280

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

libs/designer-ui/src/lib/html/index.tsx

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { HTMLChangePlugin } from './plugins/toolbar/helper/HTMLChangePlugin';
66
import { isHtmlStringValueSafeForLexical } from './plugins/toolbar/helper/util';
77
import { useState } from 'react';
88

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+
915
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));
1517
const [isSwitchFromPlaintextBlocked, setIsSwitchFromPlaintextBlocked] = useState(() => isValuePlaintext);
1618
const [value, setValue] = useState<ValueSegment[]>(initialValue);
1719

@@ -23,6 +25,11 @@ export const HTMLEditor = ({ initialValue, onChange, ...baseEditorProps }: BaseE
2325
onChange?.({ value: value });
2426
};
2527

28+
const handleSetIsValuePlaintext = (newIsPlaintext: boolean) => {
29+
setIsValuePlaintext(newIsPlaintext);
30+
setIsSwitchFromPlaintextBlocked(!isValueSafeForLexical(value));
31+
};
32+
2633
return (
2734
<EditorWrapper
2835
{...baseEditorProps}
@@ -39,12 +46,11 @@ export const HTMLEditor = ({ initialValue, onChange, ...baseEditorProps }: BaseE
3946
}}
4047
isSwitchFromPlaintextBlocked={isSwitchFromPlaintextBlocked}
4148
onBlur={handleBlur}
42-
setIsValuePlaintext={setIsValuePlaintext}
49+
setIsValuePlaintext={handleSetIsValuePlaintext}
4350
>
4451
<HTMLChangePlugin
4552
isValuePlaintext={isValuePlaintext}
4653
setIsSwitchFromPlaintextBlocked={setIsSwitchFromPlaintextBlocked}
47-
setIsValuePlaintext={setIsValuePlaintext}
4854
setValue={onValueChange}
4955
/>
5056
</EditorWrapper>

libs/designer-ui/src/lib/html/plugins/toolbar/helper/HTMLChangePlugin.tsx

+2-13
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,20 @@ import { $getRoot } from 'lexical';
2222
export interface HTMLChangePluginProps {
2323
isValuePlaintext: boolean;
2424
setIsSwitchFromPlaintextBlocked: (value: boolean) => void;
25-
setIsValuePlaintext: (isValuePlaintext: boolean) => void;
2625
setValue: (newVal: ValueSegment[]) => void;
2726
}
2827

29-
export const HTMLChangePlugin = ({
30-
isValuePlaintext,
31-
setIsSwitchFromPlaintextBlocked,
32-
setIsValuePlaintext,
33-
setValue,
34-
}: HTMLChangePluginProps) => {
28+
export const HTMLChangePlugin = ({ isValuePlaintext, setIsSwitchFromPlaintextBlocked, setValue }: HTMLChangePluginProps) => {
3529
const onChange = (editorState: EditorState, editor: LexicalEditor) => {
3630
const nodeMap = new Map<string, ValueSegment>();
37-
let isNewValuePlaintext = isValuePlaintext;
3831

3932
editorState.read(() => {
4033
const editorString = getChildrenNodes($getRoot(), nodeMap);
4134
const isSafeForLexical = isHtmlStringValueSafeForLexical(editorString, nodeMap);
4235

4336
setIsSwitchFromPlaintextBlocked(!isSafeForLexical);
44-
if (!isSafeForLexical) {
45-
isNewValuePlaintext = true;
46-
setIsValuePlaintext(isNewValuePlaintext);
47-
}
4837

49-
convertEditorState(editor, nodeMap, { isValuePlaintext: isNewValuePlaintext }).then(setValue);
38+
convertEditorState(editor, nodeMap, { isValuePlaintext }).then(setValue);
5039
});
5140
};
5241
return <OnChangePlugin ignoreSelectionChange onChange={onChange} />;

0 commit comments

Comments
 (0)