Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support content editable in FF and Safari #4683

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 46 additions & 27 deletions apps/builder/app/canvas/features/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,25 @@ const CaretColorPlugin = () => {
const OnChangeOnBlurPlugin = ({
onChange,
}: {
onChange: (editorState: EditorState) => void;
onChange: (editorState: EditorState, reason: "blur" | "unmount") => void;
}) => {
const [editor] = useLexicalComposerContext();
const handleChange = useEffectEvent(onChange);
useEffect(
() => () => {
// Safari and FF support as no blur event is triggered in some cases
editor.read(() => {
handleChange(editor.getEditorState(), "unmount");
});
},
[editor, handleChange]
);

useEffect(() => {
const handleBlur = () => {
// force read to get the latest state
editor.read(() => {
handleChange(editor.getEditorState());
handleChange(editor.getEditorState(), "blur");
});
};

Expand Down Expand Up @@ -1286,6 +1295,10 @@ const RichTextContentPluginInternal = ({
);

const closeMenuWithUpdate = () => {
if (menuState === "closed") {
return;
}

editor.update(() => {
closeMenu();
});
Expand Down Expand Up @@ -1354,6 +1367,8 @@ const RichTextContentPluginInternal = ({
unsubscribeUpdateListener();
unsubscibeSelectionChange();
unsubscribeBlurListener();
// Safari and FF support as no blur event is triggered in some cases
closeMenuWithUpdate();
};
}, [
editor,
Expand Down Expand Up @@ -1466,35 +1481,39 @@ export const TextEditor = ({
const lastSavedStateJsonRef = useRef<SerializedEditorState | null>(null);
const [newLinkKeyToInstanceId] = useState(() => new Map());

const handleChange = useEffectEvent((editorState: EditorState) => {
editorState.read(() => {
const treeRootInstance = instances.get(rootInstanceSelector[0]);
if (treeRootInstance) {
const jsonState = editorState.toJSON();
if (deepEqual(jsonState, lastSavedStateJsonRef.current)) {
setDataCollapsed(rootInstanceSelector[0], false);
return;
}
const handleChange = useEffectEvent(
(editorState: EditorState, reason: "blur" | "unmount" | "next") => {
editorState.read(() => {
const treeRootInstance = instances.get(rootInstanceSelector[0]);
if (treeRootInstance) {
const jsonState = editorState.toJSON();
if (deepEqual(jsonState, lastSavedStateJsonRef.current)) {
setDataCollapsed(rootInstanceSelector[0], false);
return;
}

onChange(
$convertToUpdates(treeRootInstance, refs, newLinkKeyToInstanceId)
);
newLinkKeyToInstanceId.clear();
lastSavedStateJsonRef.current = jsonState;
}
onChange(
$convertToUpdates(treeRootInstance, refs, newLinkKeyToInstanceId)
);
newLinkKeyToInstanceId.clear();
lastSavedStateJsonRef.current = jsonState;
}

setDataCollapsed(rootInstanceSelector[0], false);
});
setDataCollapsed(rootInstanceSelector[0], false);
});

const textEditingSelector = $textEditingInstanceSelector.get()?.selector;
if (textEditingSelector === undefined) {
return;
}
const textEditingSelector = $textEditingInstanceSelector.get()?.selector;
if (textEditingSelector === undefined) {
return;
}

if (shallowEqual(textEditingSelector, rootInstanceSelector)) {
$textEditingInstanceSelector.set(undefined);
if (reason === "blur") {
if (shallowEqual(textEditingSelector, rootInstanceSelector)) {
$textEditingInstanceSelector.set(undefined);
}
}
}
});
);

useLayoutEffect(() => {
const sheet = createRegularStyleSheet({ name: "text-editor" });
Expand Down Expand Up @@ -1625,7 +1644,7 @@ export const TextEditor = ({
}
}

handleChange(state);
handleChange(state, "next");

$textEditingInstanceSelector.set({
selector: nextSelector,
Expand Down
Loading