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: Slow performance on a large site during text editing #3351

Merged
merged 1 commit into from
May 14, 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
30 changes: 27 additions & 3 deletions apps/builder/app/canvas/features/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
$createLineBreakNode,
$getSelection,
$isRangeSelection,
type EditorState,
} from "lexical";
import { LinkNode } from "@lexical/link";
import { LexicalComposer } from "@lexical/react/LexicalComposer";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
import { nanoid } from "nanoid";
import { createRegularStyleSheet } from "@webstudio-is/css-engine";
Expand All @@ -30,6 +30,7 @@ import {
$convertTextToLexical,
} from "./interop";
import { colord } from "colord";
import { useEffectEvent } from "~/shared/hook-utils/effect-event";

const BindInstanceToNodePlugin = ({ refs }: { refs: Refs }) => {
const [editor] = useLexicalComposerContext();
Expand Down Expand Up @@ -113,6 +114,29 @@ const CaretColorPlugin = () => {
return null;
};

const OnChangeOnBlurPlugin = ({
onChange,
}: {
onChange: (editorState: EditorState) => void;
}) => {
const [editor] = useLexicalComposerContext();
const handleChange = useEffectEvent(onChange);

useEffect(() => {
const handleBlur = () => {
handleChange(editor.getEditorState());
};

// https://github.com/facebook/lexical/blob/867d449b2a6497ff9b1fbdbd70724c74a1044d8b/packages/lexical-react/src/LexicalNodeEventPlugin.ts#L59C12-L67C8
return editor.registerRootListener((rootElement, prevRootElement) => {
rootElement?.addEventListener("blur", handleBlur);
prevRootElement?.removeEventListener("blur", handleBlur);
});
}, [editor, handleChange]);

return null;
istarkov marked this conversation as resolved.
Show resolved Hide resolved
};

const RemoveParagaphsPlugin = () => {
const [editor] = useLexicalComposerContext();

Expand Down Expand Up @@ -266,8 +290,8 @@ export const TextEditor = ({
/>
<LinkPlugin />
<HistoryPlugin />
<OnChangePlugin
ignoreSelectionChange={true}

<OnChangeOnBlurPlugin
onChange={(editorState) => {
editorState.read(() => {
const treeRootInstance = instances.get(rootInstanceSelector[0]);
Expand Down
Loading