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.

wip text editor

Oleg Isonen5a76692d941c
diff --git a/app/shared/text-editor/config.ts b/app/shared/text-editor/config.ts
index 5cf4d2581f..37b71b2e3e 100644
--- a/app/shared/text-editor/config.ts
+++ b/app/shared/text-editor/config.ts
@@ -1,4 +1,4 @@
-import { EmojiNode } from "./nodes/emoji-node";
+import { InstanceNode } from "./nodes/instance";
import { darkTheme } from "./themes/dark";
export const config = {
@@ -6,5 +6,5 @@ export const config = {
onError(error) {
throw error;
},
- nodes: [EmojiNode],
+ nodes: [InstanceNode],
};
diff --git a/app/shared/text-editor/nodes/emoji-node.js b/app/shared/text-editor/nodes/emoji-node.js
deleted file mode 100644
index 465e95f26e..0000000000
--- a/app/shared/text-editor/nodes/emoji-node.js
+++ /dev/null
@@ -1,42 +0,0 @@
-import { TextNode } from "lexical";
-
-export class EmojiNode extends TextNode {
- static getType() {
- return "emoji";
- }
-
- static clone(node) {
- return new EmojiNode(node.__className, node.__text, node.__key);
- }
-
- constructor(className, text, key) {
- super(text, key);
- this.__className = className;
- }
-
- createDOM(config) {
- const dom = document.createElement("span");
- const inner = super.createDOM(config);
- dom.className = this.__className;
- inner.className = "emoji-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 $isEmojiNode(node) {
- return node instanceof EmojiNode;
-}
-
-export function $createEmojiNode(className, emojiText) {
- return new EmojiNode(className, emojiText).setMode("token");
-}
diff --git a/app/shared/text-editor/nodes/instance.js b/app/shared/text-editor/nodes/instance.js
new file mode 100644
index 0000000000..a072be41a1
--- /dev/null
+++ b/app/shared/text-editor/nodes/instance.js
@@ -0,0 +1,41 @@
+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/plugins/instance.tsx b/app/shared/text-editor/plugins/instance.tsx
new file mode 100644
index 0000000000..469d59b66e
--- /dev/null
+++ b/app/shared/text-editor/plugins/instance.tsx
@@ -0,0 +1,46 @@
+import { $createInstanceNode, InstanceNode } from "../nodes/instance";
+import { useEffect } from "react";
+import {
+ TextNode,
+ createCommand,
+ $isRangeSelection,
+ $getSelection,
+ COMMAND_PRIORITY_EDITOR,
+ type LexicalCommand,
+} from "lexical";
+import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
+import { useSubscribe } from "@webstudio-is/sdk";
+
+export const INSERT_INSTANCE_COMMAND: LexicalCommand<void> = createCommand();
+
+export const InstancePlugin = () => {
+ const [editor] = useLexicalComposerContext();
+ useEffect(() => {
+ if (editor.hasNodes([InstanceNode]) === false) {
+ throw new Error("InstancePlugin: InstanceNode not registered on editor");
+ }
+
+ return editor.registerCommand(
+ INSERT_INSTANCE_COMMAND,
+ (type) => {
+ const selection = $getSelection();
+ if ($isRangeSelection(selection)) {
+ const instanceNode = $createInstanceNode({
+ type,
+ text: selection.getTextContent(),
+ });
+ selection.insertNodes([instanceNode]);
+ }
+
+ return true;
+ },
+ COMMAND_PRIORITY_EDITOR
+ );
+ }, [editor]);
+
+ useSubscribe("insertInlineInstance", (type) => {
+ editor.dispatchCommand(INSERT_INSTANCE_COMMAND, type);
+ });
+
+ return null;
+};
diff --git a/app/shared/text-editor/plugins/test.js b/app/shared/text-editor/plugins/test.js
deleted file mode 100644
index e1edb2c667..0000000000
--- a/app/shared/text-editor/plugins/test.js
+++ /dev/null
@@ -1,30 +0,0 @@
-import { $createEmojiNode } from "../nodes/emoji-node";
-import { useEffect } from "react";
-import { TextNode } from "lexical";
-import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
-
-function emoticonTransform(node) {
- const textContent = node.getTextContent();
- // When you type :), we will replace it with an emoji node
- if (textContent === ":)") {
- node.replace($createEmojiNode("emoji happysmile", "🙂"));
- }
-}
-
-function useEmoticons(editor) {
- useEffect(() => {
- const removeTransform = editor.registerNodeTransform(
- TextNode,
- emoticonTransform
- );
- return () => {
- removeTransform();
- };
- }, [editor]);
-}
-
-export default function EmoticonPlugin() {
- const [editor] = useLexicalComposerContext();
- useEmoticons(editor);
- return null;
-}
diff --git a/app/shared/text-editor/styles.css b/app/shared/text-editor/styles.css
index fc9325fc91..ea26c14b2b 100644
--- a/app/shared/text-editor/styles.css
+++ b/app/shared/text-editor/styles.css
@@ -153,7 +153,7 @@ pre::-webkit-scrollbar-thumb {
text-decoration: underline;
}
-.emoji {
+.instance {
color: transparent;
background-size: 16px 16px;
background-position: center;
@@ -162,22 +162,23 @@ pre::-webkit-scrollbar-thumb {
margin: 0 -1px;
}
-.emoji-inner {
+.instance-inner {
padding: 0 0.15em;
}
-.emoji-inner::selection {
+.instance-inner::selection {
color: transparent;
background-color: rgba(150, 150, 150, 0.4);
}
-.emoji-inner::moz-selection {
+.instance-inner::moz-selection {
color: transparent;
background-color: rgba(150, 150, 150, 0.4);
}
-.emoji.happysmile {
+.instance.happysmile {
+ background: green;
/*
- background-image: url(./images/emoji/1F642.png);
+ 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
index 81089ffaea..b50ffbd5b1 100644
--- a/app/shared/text-editor/text-editor.js
+++ b/app/shared/text-editor/text-editor.js
@@ -1,23 +1,24 @@
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 EmoticonPlugin from "./plugins/test";
+import { InstancePlugin } from "./plugins/instance";
import { config } from "./config";
-export const TextEditor = () => {
+export const TextEditor = ({ onChange }) => {
return (
<LexicalComposer initialConfig={config}>
<div className="editor-container">
- <PlainTextPlugin
+ <RichTextPlugin
contentEditable={<ContentEditable className="editor-input" />}
placeholder={<Placeholder />}
/>
<OnChangePlugin onChange={onChange} />
<HistoryPlugin />
- <EmoticonPlugin />
+ <InstancePlugin />
</div>
</LexicalComposer>
);
@@ -26,15 +27,3 @@ export const TextEditor = () => {
function Placeholder() {
return <div className="editor-placeholder">Enter some plain text...</div>;
}
-
-// When the editor changes, you can get notified via the
-// LexicalOnChangePlugin!
-function onChange(editorState) {
- editorState.read(() => {
- // Read the contents of the EditorState here.
- const root = $getRoot();
- const selection = $getSelection();
-
- console.log(root, selection);
- });
-}
diff --git a/app/shared/text-editor/text-editor.stories.tsx b/app/shared/text-editor/text-editor.stories.tsx
index 5934f680d7..1672f19f28 100644
--- a/app/shared/text-editor/text-editor.stories.tsx
+++ b/app/shared/text-editor/text-editor.stories.tsx
@@ -2,15 +2,42 @@ import React from "react";
import type { ComponentStory, ComponentMeta } from "@storybook/react";
import { TextEditor as TextEditorPrimitive } from "./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>;
-const Template: ComponentStory<typeof TextEditorPrimitive> = (args) => (
- <TextEditorPrimitive {...args} />
-);
+const Template: ComponentStory<typeof TextEditorPrimitive> = (args) => {
+ return (
+ <>
+ <button
+ onClick={() => {
+ publish({ type: "insertInlineInstance", payload: "bold" });
+ }}
+ >
+ Bold
+ </button>
+ <TextEditorPrimitive {...args} />
+ </>
+ );
+};
export const TextEditor = Template.bind({});
-TextEditor.args = {};
+TextEditor.args = {
+ onChange,
+};
+
+// When the editor changes, you can get notified via the
+// LexicalOnChangePlugin!
+function onChange(editorState: EditorState) {
+ editorState.read(() => {
+ // Read the contents of the EditorState here.
+ const root = $getRoot();
+ const selection = $getSelection();
+
+ console.log(root, selection);
+ });
+}