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.

almost ready

Oleg Isonen3a4bfd5518da
diff --git a/app/shared/text-editor/config.ts b/app/shared/text-editor/config.ts
index 37b71b2e3e..6eb55c710c 100644
--- a/app/shared/text-editor/config.ts
+++ b/app/shared/text-editor/config.ts
@@ -1,9 +1,10 @@
-import { InstanceNode } from "./nodes/instance";
+import { InstanceNode } from "./nodes/node-instance";
import { darkTheme } from "./themes/dark";
export const config = {
+ namespace: "example",
theme: darkTheme,
- onError(error) {
+ onError(error: Error) {
throw error;
},
nodes: [InstanceNode],
diff --git a/app/shared/text-editor/text-editor.stories.tsx b/app/shared/text-editor/example-text-editor.stories.tsx
similarity index 56%
rename from app/shared/text-editor/text-editor.stories.tsx
rename to app/shared/text-editor/example-text-editor.stories.tsx
index 1672f19f28..9f6534d067 100644
--- a/app/shared/text-editor/text-editor.stories.tsx
+++ b/app/shared/text-editor/example-text-editor.stories.tsx
@@ -1,28 +1,16 @@
import React from "react";
import type { ComponentStory, ComponentMeta } from "@storybook/react";
-import { TextEditor as TextEditorPrimitive } from "./text-editor";
+import { ExampleTextEditor } from "./example-text-editor";
import "./styles.css";
import { $getRoot, $getSelection, type EditorState } from "lexical";
-import { publish } from "@webstudio-is/sdk";
export default {
title: "Text Editor",
- component: TextEditorPrimitive,
-} as ComponentMeta<typeof TextEditorPrimitive>;
+ component: ExampleTextEditor,
+} as ComponentMeta<typeof ExampleTextEditor>;
-const Template: ComponentStory<typeof TextEditorPrimitive> = (args) => {
- return (
- <>
- <button
- onClick={() => {
- publish({ type: "insertInlineInstance", payload: "bold" });
- }}
- >
- Bold
- </button>
- <TextEditorPrimitive {...args} />
- </>
- );
+const Template: ComponentStory<typeof ExampleTextEditor> = (args) => {
+ return <ExampleTextEditor {...args} />;
};
export const TextEditor = Template.bind({});
diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsx
new file mode 100644
index 0000000000..150df4b571
--- /dev/null
+++ b/app/shared/text-editor/example-text-editor.tsx
@@ -0,0 +1,82 @@
+import { LexicalComposer } from "@lexical/react/LexicalComposer";
+import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
+import { ContentEditable } from "@lexical/react/LexicalContentEditable";
+import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
+import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
+import { InstancePlugin } from "./plugins/plugin-instance";
+import { TreeViewPlugin } from "./plugins/tree-view-plugin";
+import { config } from "./config";
+import { publish } from "@webstudio-is/sdk";
+import { type EditorState } from "lexical";
+
+type ExampleTextEditorProps = {
+ onChange: (state: EditorState) => void;
+};
+
+const Menu = () => (
+ <div className="editor-menu">
+ <button
+ onClick={() => {
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ component: "Bold",
+ props: {},
+ },
+ });
+ }}
+ >
+ Bold
+ </button>
+ <button
+ onClick={() => {
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ component: "Italic",
+ props: {},
+ },
+ });
+ }}
+ >
+ Italic
+ </button>
+ <button
+ onClick={() => {
+ publish({
+ type: "insertInlineInstance",
+ payload: {
+ component: "Link",
+ props: { href: "https://google.com", target: "_blank" },
+ },
+ });
+ }}
+ >
+ Link
+ </button>
+ </div>
+);
+
+export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {
+ return (
+ <>
+ <Menu />
+ <LexicalComposer initialConfig={config}>
+ <div className="editor-container">
+ <RichTextPlugin
+ contentEditable={<ContentEditable className="editor-input" />}
+ placeholder={<Placeholder />}
+ />
+ <OnChangePlugin onChange={onChange} />
+ <HistoryPlugin />
+ <InstancePlugin />
+ <TreeViewPlugin />
+ </div>
+ </LexicalComposer>
+ </>
+ );
+};
+
+const Placeholder = () => {
+ return <div className="editor-placeholder">Enter some plain text...</div>;
+};
diff --git a/app/shared/text-editor/index.ts b/app/shared/text-editor/index.ts
index 151dd45f42..bf7f890243 100644
--- a/app/shared/text-editor/index.ts
+++ b/app/shared/text-editor/index.ts
@@ -1 +1 @@
-export * from "./text-editor";
+export * from "./example-text-editor";
diff --git a/app/shared/text-editor/nodes/instance.js b/app/shared/text-editor/nodes/instance.js
deleted file mode 100644
index a072be41a1..0000000000
--- a/app/shared/text-editor/nodes/instance.js
+++ /dev/null
@@ -1,41 +0,0 @@
-import { TextNode } from "lexical";
-
-const typeElementMap = {
- bold: "b",
-};
-
-export class InstanceNode extends TextNode {
- static getType() {
- return "instance";
- }
-
- static clone(node) {
- return new InstanceNode(node.options);
- }
-
- constructor(options) {
- super(options.text);
- this.options = options;
- }
-
- createDOM(config) {
- const dom = document.createElement(typeElementMap[this.options.type]);
- const inner = super.createDOM(config);
- inner.className = "instance-inner";
- dom.appendChild(inner);
- return dom;
- }
-
- updateDOM(prevNode, dom, config) {
- const inner = dom.firstChild;
- if (inner === null) {
- return true;
- }
- super.updateDOM(prevNode, inner, config);
- return false;
- }
-}
-
-export function $createInstanceNode(options) {
- return new InstanceNode(options).setMode("token");
-}
diff --git a/app/shared/text-editor/nodes/node-instance.tsx b/app/shared/text-editor/nodes/node-instance.tsx
new file mode 100644
index 0000000000..ea71933a68
--- /dev/null
+++ b/app/shared/text-editor/nodes/node-instance.tsx
@@ -0,0 +1,55 @@
+import { type EditorConfig, TextNode } from "lexical";
+import { render } from "react-dom";
+
+type Options = {
+ component: JSX.Element;
+ text: string;
+};
+
+export class InstanceNode extends TextNode {
+ static getType(): "instance" {
+ return "instance";
+ }
+
+ static clone(node: InstanceNode) {
+ return new InstanceNode(node.options);
+ }
+
+ constructor(options: Options) {
+ // This makes sure caret is at the end of the selection after inserting this node instance
+ super(options.text);
+ this.options = options;
+ }
+
+ createDOM(config: EditorConfig) {
+ const container = super.createDOM(config);
+ render(this.options.component, container);
+ return container;
+ }
+
+ updateDOM(prevNode: TextNode, dom: HTMLElement, config: EditorConfig) {
+ const inner = dom.firstChild as HTMLElement;
+ if (inner === null) {
+ return true;
+ }
+ super.updateDOM(prevNode, inner, config);
+
+ return false;
+ }
+
+ isInline(): true {
+ return true;
+ }
+
+ canInsertTextBefore(): false {
+ return false;
+ }
+
+ canInsertTextAfter(): false {
+ return false;
+ }
+}
+
+export function $createInstanceNode(options: Options) {
+ return new InstanceNode(options);
+}
diff --git a/app/shared/text-editor/plugins/instance.tsx b/app/shared/text-editor/plugins/plugin-instance.tsx
similarity index 51%
rename from app/shared/text-editor/plugins/instance.tsx
rename to app/shared/text-editor/plugins/plugin-instance.tsx
index 469d59b66e..d13c3066d7 100644
--- a/app/shared/text-editor/plugins/instance.tsx
+++ b/app/shared/text-editor/plugins/plugin-instance.tsx
@@ -1,7 +1,6 @@
-import { $createInstanceNode, InstanceNode } from "../nodes/instance";
+import { $createInstanceNode, InstanceNode } from "../nodes/node-instance";
import { useEffect } from "react";
import {
- TextNode,
createCommand,
$isRangeSelection,
$getSelection,
@@ -10,8 +9,14 @@ import {
} from "lexical";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { useSubscribe } from "@webstudio-is/sdk";
+import { primitives } from "../../canvas-components";
-export const INSERT_INSTANCE_COMMAND: LexicalCommand<void> = createCommand();
+const INSERT_INSTANCE_COMMAND: LexicalCommand<void> = createCommand();
+
+type Payload = {
+ component: keyof typeof primitives;
+ props: Record<string, unknown>;
+};
export const InstancePlugin = () => {
const [editor] = useLexicalComposerContext();
@@ -20,14 +25,16 @@ export const InstancePlugin = () => {
throw new Error("InstancePlugin: InstanceNode not registered on editor");
}
- return editor.registerCommand(
+ return editor.registerCommand<Payload>(
INSERT_INSTANCE_COMMAND,
- (type) => {
+ (payload) => {
const selection = $getSelection();
- if ($isRangeSelection(selection)) {
+ const text = selection?.getTextContent();
+ if ($isRangeSelection(selection) && text) {
+ const { Component } = primitives[payload.component];
const instanceNode = $createInstanceNode({
- type,
- text: selection.getTextContent(),
+ component: <Component {...payload.props}>{text}</Component>,
+ text,
});
selection.insertNodes([instanceNode]);
}
@@ -38,9 +45,12 @@ export const InstancePlugin = () => {
);
}, [editor]);
- useSubscribe("insertInlineInstance", (type) => {
- editor.dispatchCommand(INSERT_INSTANCE_COMMAND, type);
- });
+ useSubscribe<"insertInlineInstance", Payload>(
+ "insertInlineInstance",
+ (payload) => {
+ editor.dispatchCommand<Payload>(INSERT_INSTANCE_COMMAND, payload);
+ }
+ );
return null;
};
diff --git a/app/shared/text-editor/plugins/tree-view-plugin.tsx b/app/shared/text-editor/plugins/tree-view-plugin.tsx
new file mode 100644
index 0000000000..8b16894757
--- /dev/null
+++ b/app/shared/text-editor/plugins/tree-view-plugin.tsx
@@ -0,0 +1,16 @@
+import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
+import { TreeView } from "@lexical/react/LexicalTreeView";
+
+export const TreeViewPlugin = () => {
+ const [editor] = useLexicalComposerContext();
+ return (
+ <TreeView
+ viewClassName="tree-view-output"
+ timeTravelPanelClassName="debug-timetravel-panel"
+ timeTravelButtonClassName="debug-timetravel-button"
+ timeTravelPanelSliderClassName="debug-timetravel-panel-slider"
+ timeTravelPanelButtonClassName="debug-timetravel-panel-button"
+ editor={editor}
+ />
+ );
+};
diff --git a/app/shared/text-editor/styles.css b/app/shared/text-editor/styles.css
index ea26c14b2b..cdc5831b15 100644
--- a/app/shared/text-editor/styles.css
+++ b/app/shared/text-editor/styles.css
@@ -1,34 +1,5 @@
body {
- margin: 0;
background: #eee;
- font-family: system-ui, -apple-system, BlinkMacSystemFont, ".SFNSText-Regular",
- sans-serif;
- font-weight: 500;
- -webkit-font-smoothing: antialiased;
- -moz-osx-font-smoothing: grayscale;
-}
-
-.App {
- font-family: sans-serif;
- text-align: center;
-}
-
-.other h2 {
- font-size: 18px;
- color: #444;
- margin-bottom: 7px;
-}
-
-.other a {
- color: #777;
- text-decoration: underline;
- font-size: 14px;
-}
-
-.other ul {
- padding: 0;
- margin: 0;
- list-style-type: none;
}
h1 {
@@ -44,9 +15,13 @@ h1 {
text-align: right;
}
+.editor-menu {
+ margin: 10px;
+ display: flex;
+ gap: 10px;
+}
.editor-container {
background: #fff;
- margin: 20px auto 20px auto;
border-radius: 2px;
max-width: 600px;
color: #000;
@@ -104,15 +79,6 @@ h1 {
line-height: 14px;
}
-pre::-webkit-scrollbar {
- background: transparent;
- width: 10px;
-}
-
-pre::-webkit-scrollbar-thumb {
- background: #999;
-}
-
.debug-timetravel-panel {
overflow: hidden;
padding: 0 0 10px 0;
@@ -152,33 +118,3 @@ pre::-webkit-scrollbar-thumb {
.debug-timetravel-button:hover {
text-decoration: underline;
}
-
-.instance {
- color: transparent;
- background-size: 16px 16px;
- background-position: center;
- background-repeat: no-repeat;
- vertical-align: middle;
- margin: 0 -1px;
-}
-
-.instance-inner {
- padding: 0 0.15em;
-}
-
-.instance-inner::selection {
- color: transparent;
- background-color: rgba(150, 150, 150, 0.4);
-}
-
-.instance-inner::moz-selection {
- color: transparent;
- background-color: rgba(150, 150, 150, 0.4);
-}
-
-.instance.happysmile {
- background: green;
- /*
- background-image: url(./images/instance/1F642.png);
- */
-}
diff --git a/app/shared/text-editor/text-editor.js b/app/shared/text-editor/text-editor.js
deleted file mode 100644
index b50ffbd5b1..0000000000
--- a/app/shared/text-editor/text-editor.js
+++ /dev/null
@@ -1,29 +0,0 @@
-import { $getRoot, $getSelection } from "lexical";
-import { LexicalComposer } from "@lexical/react/LexicalComposer";
-import { PlainTextPlugin } from "@lexical/react/LexicalPlainTextPlugin";
-import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
-import { ContentEditable } from "@lexical/react/LexicalContentEditable";
-import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
-import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";
-import { InstancePlugin } from "./plugins/instance";
-import { config } from "./config";
-
-export const TextEditor = ({ onChange }) => {
- return (
- <LexicalComposer initialConfig={config}>
- <div className="editor-container">
- <RichTextPlugin
- contentEditable={<ContentEditable className="editor-input" />}
- placeholder={<Placeholder />}
- />
- <OnChangePlugin onChange={onChange} />
- <HistoryPlugin />
- <InstancePlugin />
- </div>
- </LexicalComposer>
- );
-};
-
-function Placeholder() {
- return <div className="editor-placeholder">Enter some plain text...</div>;
-}