Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

improve typing perf by debouncing serialization and submition

Oleg Isonen3e7303f26781
diff --git a/app/canvas/features/wrapper-component/text-editor/plugins/plugin-on-change.tsx b/app/canvas/features/wrapper-component/text-editor/plugins/plugin-on-change.tsx
index 7f3b616928..84bbe25c86 100644
--- a/app/canvas/features/wrapper-component/text-editor/plugins/plugin-on-change.tsx
+++ b/app/canvas/features/wrapper-component/text-editor/plugins/plugin-on-change.tsx
@@ -1,4 +1,6 @@
import { type ChildrenUpdates } from "@webstudio-is/sdk";
+import { useState } from "react";
+import { useDebounce } from "react-use";
import {
OnChangePlugin as LexicalOnChangePlugin,
type EditorState,
@@ -10,12 +12,19 @@ export const OnChangePlugin = ({
}: {
onChange: (updates: ChildrenUpdates) => void;
}) => {
- const handleChange = (state: EditorState) => {
- state.read(() => {
- const updates = toUpdates(state.toJSON().root);
- onChange(updates);
- });
- };
+ const [editorState, setEditorState] = useState<EditorState>();
- return <LexicalOnChangePlugin onChange={handleChange} />;
+ useDebounce(
+ () => {
+ if (editorState === undefined) return;
+ editorState.read(() => {
+ const updates = toUpdates(editorState.toJSON().root);
+ onChange(updates);
+ });
+ },
+ 500,
+ [editorState]
+ );
+
+ return <LexicalOnChangePlugin onChange={setEditorState} />;
};