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.

render existing text when clicking into editor

Oleg Isonenc92239f82643
diff --git a/app/canvas/features/wrapper-component/editor/editor.tsx b/app/canvas/features/wrapper-component/editor/editor.tsx
index 5448ab5a08..031ba89eb7 100644
--- a/app/canvas/features/wrapper-component/editor/editor.tsx
+++ b/app/canvas/features/wrapper-component/editor/editor.tsx
@@ -1,3 +1,4 @@
+import { type Instance } from "@webstudio-is/sdk";
import {
RichTextPlugin,
HistoryPlugin,
@@ -5,15 +6,16 @@ import {
} from "~/shared/text-editor";
type EditorProps = {
- children: JSX.Element;
+ editable: JSX.Element;
+ children: Array<Instance | string>;
};
-export const Editor = ({ children }: EditorProps) => {
+export const Editor = ({ children, editable }: EditorProps) => {
return (
<>
- <RichTextPlugin contentEditable={children} placeholder="" />
+ <RichTextPlugin contentEditable={editable} placeholder="" />
<HistoryPlugin />
- <InstancePlugin />
+ <InstancePlugin>{children}</InstancePlugin>
</>
);
};
diff --git a/app/canvas/features/wrapper-component/wrapper-component.tsx b/app/canvas/features/wrapper-component/wrapper-component.tsx
index 958f2f5ea2..6afb68d2a3 100644
--- a/app/canvas/features/wrapper-component/wrapper-component.tsx
+++ b/app/canvas/features/wrapper-component/wrapper-component.tsx
@@ -82,12 +82,12 @@ export const WrapperComponentDev = ({
event.preventDefault();
}}
>
- {isEditing ? undefined : renderWrapperComponentChildren(children)}
+ {isEditing ? null : renderWrapperComponentChildren(children)}
</Component>
);
if (isEditing) {
- return <Editor>{element}</Editor>;
+ return <Editor editable={element}>{instance.children}</Editor>;
}
return element;
diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsx
index 29d938cf4c..da1a105d6c 100644
--- a/app/shared/text-editor/example-text-editor.tsx
+++ b/app/shared/text-editor/example-text-editor.tsx
@@ -89,13 +89,11 @@ export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {
<div className="editor-container">
<RichTextPlugin
contentEditable={<ContentEditable className="editor-input" />}
- placeholder={
- <div className="editor-placeholder">Enter some plain text...</div>
- }
+ placeholder=""
/>
<OnChangePlugin onChange={onChange} />
<HistoryPlugin />
- <InstancePlugin />
+ <InstancePlugin children={["Something"]} />
<TreeViewPlugin />
</div>
</LexicalComposer>
diff --git a/app/shared/text-editor/lexical.ts b/app/shared/text-editor/lexical.ts
index 737218fc3f..b527bf2d75 100644
--- a/app/shared/text-editor/lexical.ts
+++ b/app/shared/text-editor/lexical.ts
@@ -1,12 +1,20 @@
-export type { EditorConfig, EditorState, LexicalCommand } from "lexical";
+export type {
+ EditorConfig,
+ EditorState,
+ LexicalCommand,
+ LexicalEditor,
+} from "lexical";
export {
TextNode,
+ RootNode,
createCommand,
$isRangeSelection,
$getSelection,
$getRoot,
COMMAND_PRIORITY_EDITOR,
$getNodeByKey,
+ $createTextNode,
+ $createParagraphNode,
} from "lexical";
export { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
export { LexicalComposer } from "@lexical/react/LexicalComposer";
diff --git a/app/shared/text-editor/plugins/plugin-instance.tsx b/app/shared/text-editor/plugins/plugin-instance.tsx
index b0fd0812fb..af715bfee4 100644
--- a/app/shared/text-editor/plugins/plugin-instance.tsx
+++ b/app/shared/text-editor/plugins/plugin-instance.tsx
@@ -11,6 +11,9 @@ import {
createCommand,
$isRangeSelection,
$getSelection,
+ $createTextNode,
+ $createParagraphNode,
+ $getRoot,
COMMAND_PRIORITY_EDITOR,
useLexicalComposerContext,
type LexicalCommand,
@@ -18,6 +21,36 @@ import {
import { useCss } from "~/canvas/features/wrapper-component/use-css";
import { useBreakpoints } from "~/shared/nano-states";
+const populateRoot = (children: Instance["children"]) => {
+ const nodes = children.map((child) => {
+ if (typeof child === "string") {
+ const paragraph = $createParagraphNode();
+ paragraph.append($createTextNode(child));
+ return paragraph;
+ }
+ // @todo
+ const props = {};
+ // Inline components should always have a single child string
+ const text = typeof child.children[0] === "string" ? child.children[0] : "";
+
+ return $createInstanceNode({
+ component: (
+ <InlineWrapperComponent {...props} instance={child}>
+ {text}
+ </InlineWrapperComponent>
+ ),
+ text,
+ });
+ });
+
+ const root = $getRoot();
+ root.clear();
+ for (const node of nodes) {
+ root.append(node);
+ }
+ root.selectStart();
+};
+
const INSERT_INSTANCE_COMMAND: LexicalCommand<CreateInstancePayload> =
createCommand();
@@ -26,7 +59,11 @@ type CreateInstancePayload = {
props: Record<string, unknown>;
};
-export const InstancePlugin = () => {
+type InstancePluginProps = {
+ children: Instance["children"];
+};
+
+export const InstancePlugin = ({ children }: InstancePluginProps) => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (editor.hasNodes([InstanceNode]) === false) {
@@ -56,6 +93,12 @@ export const InstancePlugin = () => {
);
}, [editor]);
+ useEffect(() => {
+ editor.update(() => {
+ populateRoot(children);
+ });
+ }, [editor, children]);
+
useSubscribe<"insertInlineInstance", CreateInstancePayload>(
"insertInlineInstance",
(payload) => {
@@ -65,6 +108,7 @@ export const InstancePlugin = () => {
);
}
);
+
return null;
};
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 926f331193..c7a5edb888 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,6 +10,12 @@ datasource db {
url = env("DATABASE_URL")
}
+model Team {
+ id String @id @default(uuid())
+ users User[]
+
+}
+
model User {
id String @id @default(uuid())
email String? @unique
@@ -17,18 +23,20 @@ model User {
image String?
username String?
createdAt DateTime @default(now())
+ Team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
projects Project[]
}
model Project {
id String @id @default(uuid())
- userId String
title String
domain String @unique
prodTreeId String? // exists when published
devTreeId String
prodTreeIdHistory String @default("[]")
User User? @relation(fields: [userId], references: [id])
+ userId String?
}
model Tree {