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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
imported emoji example
Oleg Isonen296158ebe59f
diff --git a/app/shared/text-editor/config.ts b/app/shared/text-editor/config.tsnew file mode 100644index 0000000000..5cf4d2581f--- /dev/null+++ b/app/shared/text-editor/config.ts@@ -0,0 +1,10 @@+import { EmojiNode } from "./nodes/emoji-node";+import { darkTheme } from "./themes/dark";++export const config = {+ theme: darkTheme,+ onError(error) {+ throw error;+ },+ nodes: [EmojiNode],+};diff --git a/app/shared/text-editor/index.ts b/app/shared/text-editor/index.tsnew file mode 100644index 0000000000..151dd45f42--- /dev/null+++ b/app/shared/text-editor/index.ts@@ -0,0 +1 @@+export * from "./text-editor";diff --git a/app/shared/text-editor/nodes/emoji-node.js b/app/shared/text-editor/nodes/emoji-node.jsnew file mode 100644index 0000000000..465e95f26e--- /dev/null+++ b/app/shared/text-editor/nodes/emoji-node.js@@ -0,0 +1,42 @@+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/plugins/test.js b/app/shared/text-editor/plugins/test.jsnew file mode 100644index 0000000000..e1edb2c667--- /dev/null+++ b/app/shared/text-editor/plugins/test.js@@ -0,0 +1,30 @@+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.cssnew file mode 100644index 0000000000..fc9325fc91--- /dev/null+++ b/app/shared/text-editor/styles.css@@ -0,0 +1,183 @@+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 {+ font-size: 24px;+ color: #333;+}++.ltr {+ text-align: left;+}++.rtl {+ text-align: right;+}++.editor-container {+ background: #fff;+ margin: 20px auto 20px auto;+ border-radius: 2px;+ max-width: 600px;+ color: #000;+ position: relative;+ line-height: 20px;+ font-weight: 400;+ text-align: left;+ border-top-left-radius: 10px;+ border-top-right-radius: 10px;+}++.editor-input {+ min-height: 150px;+ resize: none;+ font-size: 15px;+ caret-color: rgb(5, 5, 5);+ position: relative;+ tab-size: 1;+ outline: 0;+ padding: 15px 10px;+ caret-color: #444;+}++.editor-placeholder {+ color: #999;+ overflow: hidden;+ position: absolute;+ text-overflow: ellipsis;+ top: 15px;+ left: 10px;+ font-size: 15px;+ user-select: none;+ display: inline-block;+ pointer-events: none;+}++.editor-paragraph {+ margin: 0 0 15px 0;+ position: relative;+}++.tree-view-output {+ display: block;+ background: #222;+ color: #fff;+ padding: 5px;+ font-size: 12px;+ white-space: pre-wrap;+ margin: 1px auto 10px auto;+ max-height: 250px;+ position: relative;+ border-bottom-left-radius: 10px;+ border-bottom-right-radius: 10px;+ overflow: hidden;+ 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;+ margin: auto;+ display: flex;+}++.debug-timetravel-panel-slider {+ padding: 0;+ flex: 8;+}++.debug-timetravel-panel-button {+ padding: 0;+ border: 0;+ background: none;+ flex: 1;+ color: #fff;+ font-size: 12px;+}++.debug-timetravel-panel-button:hover {+ text-decoration: underline;+}++.debug-timetravel-button {+ border: 0;+ padding: 0;+ font-size: 12px;+ top: 10px;+ right: 15px;+ position: absolute;+ background: none;+ color: #fff;+}++.debug-timetravel-button:hover {+ text-decoration: underline;+}++.emoji {+ color: transparent;+ background-size: 16px 16px;+ background-position: center;+ background-repeat: no-repeat;+ vertical-align: middle;+ margin: 0 -1px;+}++.emoji-inner {+ padding: 0 0.15em;+}++.emoji-inner::selection {+ color: transparent;+ background-color: rgba(150, 150, 150, 0.4);+}++.emoji-inner::moz-selection {+ color: transparent;+ background-color: rgba(150, 150, 150, 0.4);+}++.emoji.happysmile {+ /*+ background-image: url(./images/emoji/1F642.png);+ */+}diff --git a/app/shared/text-editor/text-editor.js b/app/shared/text-editor/text-editor.jsnew file mode 100644index 0000000000..81089ffaea--- /dev/null+++ b/app/shared/text-editor/text-editor.js@@ -0,0 +1,40 @@+import { $getRoot, $getSelection } from "lexical";+import { LexicalComposer } from "@lexical/react/LexicalComposer";+import { PlainTextPlugin } from "@lexical/react/LexicalPlainTextPlugin";+import { ContentEditable } from "@lexical/react/LexicalContentEditable";+import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";+import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin";+import EmoticonPlugin from "./plugins/test";+import { config } from "./config";++export const TextEditor = () => {+ return (+ <LexicalComposer initialConfig={config}>+ <div className="editor-container">+ <PlainTextPlugin+ contentEditable={<ContentEditable className="editor-input" />}+ placeholder={<Placeholder />}+ />+ <OnChangePlugin onChange={onChange} />+ <HistoryPlugin />+ <EmoticonPlugin />+ </div>+ </LexicalComposer>+ );+};++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.tsxnew file mode 100644index 0000000000..5934f680d7--- /dev/null+++ b/app/shared/text-editor/text-editor.stories.tsx@@ -0,0 +1,16 @@+import React from "react";+import type { ComponentStory, ComponentMeta } from "@storybook/react";+import { TextEditor as TextEditorPrimitive } from "./text-editor";+import "./styles.css";++export default {+ title: "Text Editor",+ component: TextEditorPrimitive,+} as ComponentMeta<typeof TextEditorPrimitive>;++const Template: ComponentStory<typeof TextEditorPrimitive> = (args) => (+ <TextEditorPrimitive {...args} />+);++export const TextEditor = Template.bind({});+TextEditor.args = {};diff --git a/app/shared/text-editor/themes/dark.ts b/app/shared/text-editor/themes/dark.tsnew file mode 100644index 0000000000..5d2276a381--- /dev/null+++ b/app/shared/text-editor/themes/dark.ts@@ -0,0 +1,6 @@+export const darkTheme = {+ ltr: "ltr",+ rtl: "rtl",+ placeholder: "editor-placeholder",+ paragraph: "editor-paragraph",+};diff --git a/package.json b/package.jsonindex 6e9c69c40f..fcbc34b471 100644--- a/package.json+++ b/package.json@@ -19,6 +19,7 @@"storybook:build": "NODE_OPTIONS=--openssl-legacy-provider build-storybook"},"dependencies": {+ "@lexical/react": "^0.3.5","@prisma/client": "^3.14.0","@radix-ui/colors": "^0.1.8","@radix-ui/react-accessible-icon": "^0.1.4",@@ -56,6 +57,7 @@"hyphenate-style-name": "^1.0.4","immer": "^9.0.12","immerhin": "^0.1.2",+ "lexical": "^0.3.5","lodash.capitalize": "^4.2.1","lodash.debounce": "^4.0.8","lodash.memoize": "^4.1.2",diff --git a/tsconfig.json b/tsconfig.jsonindex 749e4b959e..b1cd4c5f0d 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -1,5 +1,11 @@{- "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],+ "include": [+ "remix.env.d.ts",+ "**/*.ts",+ "**/*.tsx",+ "app/shared/text-editor/text-editor.js",+ "app/shared/text-editor/plugins/test.js"+ ],"compilerOptions": {"lib": ["DOM", "DOM.Iterable", "ES2019"],"isolatedModules": true,diff --git a/yarn.lock b/yarn.lockindex a5857080fb..24f6632369 100644--- a/yarn.lock+++ b/yarn.lock@@ -1717,6 +1717,155 @@"@jridgewell/resolve-uri" "^3.0.3""@jridgewell/sourcemap-codec" "^1.4.10"+"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/clipboard/-/clipboard-0.3.5.tgz#3751dfdd582447ca35a36db83c53cc9ab12ada8d"+ integrity sha512-S6qhUgGAqSqeZIcJUJ6xzF0L6BU27kNbPPRMkr+LxS4AL1dMWhBQlS4bU5jzFditJNjhAcpoUsfAiwLvcGbGog==+ dependencies:+ "@lexical/html" "0.3.5"+ "@lexical/selection" "0.3.5"+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/code/-/code-0.3.5.tgz#73bf605f73fb6240876aa7e643aa48015e06d64b"+ integrity sha512-fz8FxHhkLZT18dvVxTw4z5ke44F+8OTDq6tA2Iqbe/BWtexIXFuZvTr72GfobhMVsa4a9s/cRWDG+yQF8fCFUg==+ dependencies:+ "@lexical/utils" "0.3.5"+ prismjs "^1.27.0"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/dragon/-/dragon-0.3.5.tgz#5cced736f959ebdab11ba2f0b8ad4b4faa4fb27a"+ integrity sha512-tPa4K0PYSUMdW014ed7Byr+kj8YHCfWLq/oosmG0P+LzcmsCl9gnQrv7jyFG3Y+adhhS7Ds0lHdXyT93pcJgBw==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/hashtag/-/hashtag-0.3.5.tgz#30d0fea307aa8f3446aa92f497d52dae70db155c"+ integrity sha512-Bk2N0K5WuTzciKJF7f44g3uYCwdMoektPpemG4czMab1GuBHkf1e5HJ7Y/I8rwAchK0WP9N91yAZZfLKGiT9wQ==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/history/-/history-0.3.5.tgz#efa4a274ec17a705681ed6d785cfd30ea3be45b8"+ integrity sha512-G9IH6ZPvJjAcpyjag1A6vQfknUIBRjbforswT6E8OOpxWK6EI2QRmrdMie9v5OgUAagcTJw7mkyXDPYc1t7PBw==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/html/-/html-0.3.5.tgz#932ce67c859b920d737126630dbe505ce208e4a5"+ integrity sha512-ROutiMr3p6tLxLQ3OXWqawt00RsGleT/5p+Yo0M5aD8OHtszqvriyT3nO7GiCED90S3Vb+V+xYjAcUhb+XcdgA==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/link/-/link-0.3.5.tgz#9136f73040e4049e583e63d9b8834c62406e5ca7"+ integrity sha512-ERmc8244Bpy0RGaQ2kHal90BMBikWMW4T8InJKrHh2dqVte/HUL7kZDMSOaix3OjdHRuRi9yBZCGpzoul8a9Hg==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/list/-/list-0.3.5.tgz#ca82ad306a9fbcddd77c9534294cba22c1f48c56"+ integrity sha512-RU6UV9pSi9Mhh1QwUNkiBqYIihCOcJ92iBKry6LqkB8C4ANGadUHy0/kRnVK9+AIXN6egDUoONFnr7o8/Q/Lvw==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/mark/-/mark-0.3.5.tgz#a75f754f7f6cf3adc0d801d5a98004024d9b700c"+ integrity sha512-/SAuHi4768/IHc/ReJW2sLm0L1V08GU3P8EEdlxkdSXNqXTBBVtihX9jnVW0/X7OyrD6wxzyK+CeWL4gHeDukw==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/markdown/-/markdown-0.3.5.tgz#60a61e5f4a1c434e38c8518a05e5c2e45e458523"+ integrity sha512-8bzkUuB2jH6/rqZeguH9AW1LNRZlqr7myUkGpDBrPhYJ+q+DdN/FkeeTiJeXRKzjmf4dqUOnqP2ZVjkA+JcaqQ==+ dependencies:+ "@lexical/code" "0.3.5"+ "@lexical/link" "0.3.5"+ "@lexical/list" "0.3.5"+ "@lexical/rich-text" "0.3.5"+ "@lexical/text" "0.3.5"+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/offset/-/offset-0.3.5.tgz#ccd9261ce409649cfb3a804d4540be8be6dbc1d3"+ integrity sha512-mFYplAOy/GvzEJb0yyyqvcdK6J042fc589A8FMsal9R7hsH+7btAilVCy3xK7qbDo8QhbLqQjCZeYJ4OqC8cbQ==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/overflow/-/overflow-0.3.5.tgz#f964e42616777190cb4b7826ff4f25bffb6804f2"+ integrity sha512-waim/rxPRL37R+6pdPgGv6otgNMfmyZewFHa198AShQrOksKBlWVSQ66ikQbZbgzSXqYI4LCrt4A1meo8E4m4Q==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/plain-text/-/plain-text-0.3.5.tgz#afd69ac94b113d12a300c16a4a99780619d9dbb6"+ integrity sha512-y6ePx8NkDzL6E1O5tqrgTWe1KVzIbtQNJSJvtoN+9jOFKZegR3bLAwPIt88xV9WyQrtaGMtdmeB4lrN+jviczA==++"@lexical/react@^0.3.5":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/react/-/react-0.3.5.tgz#c01dee8f5f363f61a4044048c8044bcb61f773b3"+ integrity sha512-o1TXcPdhuGaIXMe9dPtOLTucfryJwqC+xpJ/QSgzkTy8qo+jva7fq9QjperZ+rVmWM06Neqj3VdOVi+j1qHfrg==+ dependencies:+ "@lexical/clipboard" "0.3.5"+ "@lexical/code" "0.3.5"+ "@lexical/dragon" "0.3.5"+ "@lexical/hashtag" "0.3.5"+ "@lexical/history" "0.3.5"+ "@lexical/link" "0.3.5"+ "@lexical/list" "0.3.5"+ "@lexical/mark" "0.3.5"+ "@lexical/markdown" "0.3.5"+ "@lexical/overflow" "0.3.5"+ "@lexical/plain-text" "0.3.5"+ "@lexical/rich-text" "0.3.5"+ "@lexical/selection" "0.3.5"+ "@lexical/table" "0.3.5"+ "@lexical/text" "0.3.5"+ "@lexical/utils" "0.3.5"+ "@lexical/yjs" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/rich-text/-/rich-text-0.3.5.tgz#35f7e9b10c77a90ea15b0971237dab20e5956732"+ integrity sha512-+rQauexXzrDKrlRwY8WIvcAuhjz94UYd7+0oc3YwXeYkzh8GdnHlkzEQJktUOoDA8F7mVBhrbfUvaso03Ap4Cg==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/selection/-/selection-0.3.5.tgz#cd371eaf5b6542e490d22af5b81ee613f5225c50"+ integrity sha512-kp/AYc1PRV7QWNSiA44wlJAJVxyIRX5YjjGFlA+KzlKpZ7x18owkHyIir1CaO3JozfPONwyIKan3bvxkF4lgFw==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/table/-/table-0.3.5.tgz#67f225385220e51ef346f3f6abefef9e7715d030"+ integrity sha512-ly/xHwDZjspIy9AC1ypC/vMpYmqQ+2mL3jMYgAMZN19SZUQcuqGGM0zyJXy6quPUWvI5VdqwIO/Jra3dsI0ikQ==+ dependencies:+ "@lexical/utils" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/text/-/text-0.3.5.tgz#71a17852fa59c9d2296935c5b6c7d8034c9da5fe"+ integrity sha512-q2QBchZKmS0INJYZrG7+G63aRYRE13gxcyur/sOzF0eHfYqfhJdwmnHpBkMKNeXoQoneKY2R5eLN34qYT2yFYw==++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/utils/-/utils-0.3.5.tgz#fdc60d86829531fb48cac27a6d7c7760f219e98e"+ integrity sha512-3wX7Y/aTzLlb0r2xTf8oJuouWMCbA1YmUUfDeMcSLH4wlOM+TQv4j7fbNj1Xm+8DWtfB96Ehx3cGW5zDWh6ytQ==+ dependencies:+ "@lexical/list" "0.3.5"+ "@lexical/table" "0.3.5"++"@lexical/[email protected]":+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/@lexical/yjs/-/yjs-0.3.5.tgz#87f16a9b22a2bc2adf5b56480cdd080768b52769"+ integrity sha512-GXQeb1REH8vwXdBgjE+VQPOHg5TwbMZwVhuX9wPaZzpXl3B0EpPDY/oRFPCM/y2vQBqxlGiBj4/QxqutgNoe2A==+ dependencies:+ "@lexical/offset" "0.3.5"+"@mdx-js/mdx@^1.6.22":version "1.6.22"resolved "https://registry.yarnpkg.com/@mdx-js/mdx/-/mdx-1.6.22.tgz#8a723157bf90e78f17dc0f27995398e6c731f1ba"@@ -9911,6 +10060,11 @@ levn@~0.3.0:prelude-ls "~1.1.2"type-check "~0.3.2"+lexical@^0.3.5:+ version "0.3.5"+ resolved "https://registry.yarnpkg.com/lexical/-/lexical-0.3.5.tgz#429ff5e455a625b5102eba5f325f0465740174cd"+ integrity sha512-zi4OvEH7BFOl8NX27cjqbwJSm9qKCgtPw4JJrUfHgXVNCW5yMDV3rXSUW4KnCjeiXHKEi2E+dDk+awfKntroNg==+lines-and-columns@^1.1.6:version "1.2.4"resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"