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.

first working version lazy loading the editor

Oleg Isonen5247d43dabb1
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index e58857dc1d..9193e3ad60 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -45,10 +45,7 @@ import { registerContainers } from "./shared/immerhin";
import { useTrackHoveredElement } from "./shared/use-track-hovered-element";
import { usePublishScrollState } from "./shared/use-publish-scroll-state";
import { useDragAndDrop } from "./shared/use-drag-drop";
-import {
- LexicalComposer,
- config,
-} from "~/canvas/features/wrapper-component/text-editor";
+
import { setInstanceChildrenMutable } from "~/shared/tree-utils";
registerContainers();
@@ -133,18 +130,14 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
if (elements === undefined) return null;
- const children = (
- <LexicalComposer initialConfig={config}>{elements}</LexicalComposer>
- );
-
if (isPreviewMode) {
- return children;
+ return elements;
}
return (
<>
<DesignMode treeId={data.tree.id} project={data.project} />
- {children}
+ {elements}
</>
);
};
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/editor.tsx b/apps/designer/app/canvas/features/wrapper-component/text-editor/editor.tsx
index 13b4bee56c..f33724b0e8 100644
--- a/apps/designer/app/canvas/features/wrapper-component/text-editor/editor.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/editor.tsx
@@ -1,9 +1,15 @@
-import { type ChildrenUpdates, type Instance } from "@webstudio-is/react-sdk";
-import { RichTextPlugin, HistoryPlugin } from "./lexical";
+import type { ChildrenUpdates, Instance } from "@webstudio-is/react-sdk";
+import {
+ RichTextPlugin,
+ HistoryPlugin,
+ LexicalComposer,
+ useLexicalComposerContext,
+} from "./lexical";
import { InstancePlugin } from "./plugins/plugin-instance";
import { ToolbarConnectorPlugin } from "./plugins/plugin-toolbar-connector";
import { OnChangePlugin } from "./plugins/plugin-on-change";
-import { memo } from "react";
+import { config } from "./config";
+import { useEffect } from "react";
type EditorProps = {
children?: JSX.Element;
@@ -12,12 +18,19 @@ type EditorProps = {
onChange: (updates: ChildrenUpdates) => void;
};
-export const Editor = ({
+const Editor = ({
+ element,
instance,
editable,
children,
onChange,
}: EditorProps) => {
+ const [editor] = useLexicalComposerContext();
+
+ useEffect(() => {
+ editor.setRootElement(element);
+ }, [element]);
+
return (
<>
<RichTextPlugin contentEditable={editable} placeholder="" />
@@ -30,5 +43,12 @@ export const Editor = ({
);
};
-// Prevent rerender because in editing mode Editor controls the node.
-export const EditorMemoized = memo(Editor, () => true);
+const EditorConfigured = (props) => {
+ return (
+ <LexicalComposer initialConfig={config}>
+ <Editor {...props} />
+ </LexicalComposer>
+ );
+};
+
+export default EditorConfigured;
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/example-text-editor.tsx b/apps/designer/app/canvas/features/wrapper-component/text-editor/example-text-editor.tsx
index 38c55084f3..0e4fe8e079 100644
--- a/apps/designer/app/canvas/features/wrapper-component/text-editor/example-text-editor.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/example-text-editor.tsx
@@ -9,7 +9,7 @@ import { Box, Button } from "@webstudio-is/design-system";
import { LexicalComposer, ContentEditable } from "./lexical";
import { TreeViewPlugin } from "./plugins/tree-view-plugin";
import { config } from "./config";
-import { Editor } from "./editor";
+import Editor from "./editor";
type ExampleTextEditorProps = {
onChange: (state: ChildrenUpdates) => void;
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/hooks/use-content-editable.tsx b/apps/designer/app/canvas/features/wrapper-component/text-editor/hooks/use-content-editable.tsx
index 1c5a59704c..3edd0dae17 100644
--- a/apps/designer/app/canvas/features/wrapper-component/text-editor/hooks/use-content-editable.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/hooks/use-content-editable.tsx
@@ -1,4 +1,4 @@
-import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
+import { useLexicalComposerContext } from "../lexical";
import { useCallback, useEffect } from "react";
type RefCallback = (rootElement: null | HTMLElement) => void;
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/index.ts b/apps/designer/app/canvas/features/wrapper-component/text-editor/index.ts
index 03a81bb600..d442c978f0 100644
--- a/apps/designer/app/canvas/features/wrapper-component/text-editor/index.ts
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/index.ts
@@ -1,4 +1 @@
-export { LexicalComposer } from "./lexical";
-export { config } from "./config";
-export { useContentEditable } from "./hooks/use-content-editable";
-export { EditorMemoized } from "./editor";
+export { Editor } from "./lazy-editor";
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/lazy-editor.tsx b/apps/designer/app/canvas/features/wrapper-component/text-editor/lazy-editor.tsx
new file mode 100644
index 0000000000..655c6ff1fc
--- /dev/null
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/lazy-editor.tsx
@@ -0,0 +1,14 @@
+import { memo, lazy, Suspense } from "react";
+
+const EditorLazy = lazy(() => import("./editor.tsx"));
+
+const EditorWithSuspense = (props) => {
+ return (
+ <Suspense fallback={<div>Loading...</div>}>
+ <EditorLazy {...props} />
+ </Suspense>
+ );
+};
+
+// Prevent rerender because in editing mode Editor controls the node.
+export const Editor = memo(EditorWithSuspense, () => true);
diff --git a/apps/designer/app/canvas/features/wrapper-component/text-editor/plugins/plugin-instance.tsx b/apps/designer/app/canvas/features/wrapper-component/text-editor/plugins/plugin-instance.tsx
index 9b3857f056..3fea264882 100644
--- a/apps/designer/app/canvas/features/wrapper-component/text-editor/plugins/plugin-instance.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/text-editor/plugins/plugin-instance.tsx
@@ -27,7 +27,6 @@ const INSERT_INSTANCE_COMMAND: LexicalCommand<Instance> = createCommand();
type InstancePluginProps = {
children: Instance["children"];
};
-
export const InstancePlugin = ({ children }: InstancePluginProps) => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
diff --git a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
index 9e50e27eff..9a16adb378 100644
--- a/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/apps/designer/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -1,4 +1,4 @@
-import { useCallback, MouseEvent, FormEvent, useMemo } from "react";
+import { useCallback, MouseEvent, FormEvent, useMemo, useRef } from "react";
import {
type Instance,
type CSS,
@@ -11,7 +11,7 @@ import {
import { useBreakpoints, useTextEditingInstanceId } from "~/shared/nano-states";
import { useCss } from "./use-css";
import { useEnsureFocus } from "./use-ensure-focus";
-import { EditorMemoized, useContentEditable } from "./text-editor";
+import { Editor } from "./text-editor";
import noop from "lodash.noop";
import { useSelectedElement } from "~/canvas/shared/nano-states";
@@ -31,24 +31,23 @@ export const WrapperComponentDev = ({
}: WrapperComponentDevProps) => {
const className = useCss({ instance, css });
const [editingInstanceId] = useTextEditingInstanceId();
- const [, setSelectedElement] = useSelectedElement();
+ const [element, setSelectedElement] = useSelectedElement();
const isEditing = editingInstanceId === instance.id;
- const [editableRefCallback, editableProps] = useContentEditable(isEditing);
const focusRefCallback = useEnsureFocus();
const refCallback = useCallback(
(element) => {
- if (isEditing) editableRefCallback(element);
focusRefCallback(element);
// When entering text editing we unmount the instance element, so we need to update the reference, otherwise we have a detached element referenced and bounding box will be wrong.
if (element !== null) setSelectedElement(element);
},
- [focusRefCallback, editableRefCallback, setSelectedElement, isEditing]
+ [focusRefCallback, setSelectedElement, isEditing]
);
const userProps = useUserProps(instance.id);
const readonlyProps =
instance.component === "Input" ? { readOnly: true } : undefined;
+ const editableProps = isEditing ? { contentEditable: true } : undefined;
const { Component } = components[instance.component];
@@ -78,7 +77,8 @@ export const WrapperComponentDev = ({
if (isEditing) {
return (
- <EditorMemoized
+ <Editor
+ element={element}
instance={instance}
editable={<Component {...props} />}
onChange={(updates) => {
diff --git a/packages/react-sdk/tsconfig.json b/packages/react-sdk/tsconfig.json
index e599e806b6..d494fe77bc 100644
--- a/packages/react-sdk/tsconfig.json
+++ b/packages/react-sdk/tsconfig.json
@@ -3,10 +3,10 @@
"extends": "../../node_modules/@webstudio-is/tsconfig/base.json",
"include": ["./src/**/*.ts", "./src/**/*.tsx"],
"compilerOptions": {
- "lib": ["DOM", "DOM.Iterable", "ES2019"],
+ "lib": ["DOM", "DOM.Iterable", "es2020"],
"module": "commonjs",
"jsx": "react-jsx",
- "target": "ES2019",
+ "target": "es2020",
"strict": true,
"baseUrl": ".",
"paths": {