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
first working version of lexical inside the instance
Oleg Isonendc15cb95c261
diff --git a/app/canvas/canvas.tsx b/app/canvas/canvas.tsxindex 9cba40358c..326db5f666 100644--- a/app/canvas/canvas.tsx+++ b/app/canvas/canvas.tsx@@ -50,6 +50,7 @@ import {import { registerContainers } from "./shared/immerhin";import { useTrackHoveredElement } from "./shared/use-track-hovered-element";import { usePublishScrollState } from "./shared/use-publish-scroll-state";+import { LexicalComposer, config } from "~/shared/text-editor";registerContainers();@@ -127,7 +128,9 @@ const DesignMode = ({ treeId, project }: DesignModeProps) => {return (// Using touch backend becuase html5 drag&drop doesn't fire drag events in our case<DndProvider backend={TouchBackend} options={dndOptions}>- {elements}+ {elements && (+ <LexicalComposer initialConfig={config}>{elements}</LexicalComposer>+ )}</DndProvider>);};diff --git a/app/canvas/features/wrapper-component/editor/editor.tsx b/app/canvas/features/wrapper-component/editor/editor.tsxnew file mode 100644index 0000000000..5448ab5a08--- /dev/null+++ b/app/canvas/features/wrapper-component/editor/editor.tsx@@ -0,0 +1,19 @@+import {+ RichTextPlugin,+ HistoryPlugin,+ InstancePlugin,+} from "~/shared/text-editor";++type EditorProps = {+ children: JSX.Element;+};++export const Editor = ({ children }: EditorProps) => {+ return (+ <>+ <RichTextPlugin contentEditable={children} placeholder="" />+ <HistoryPlugin />+ <InstancePlugin />+ </>+ );+};diff --git a/app/canvas/features/wrapper-component/editor/use-is-editing.ts b/app/canvas/features/wrapper-component/editor/use-is-editing.tsnew file mode 100644index 0000000000..615b4ff1e7--- /dev/null+++ b/app/canvas/features/wrapper-component/editor/use-is-editing.ts@@ -0,0 +1,44 @@+import { type Instance } from "@webstudio-is/sdk";+import { useEffect, useMemo, useState } from "react";+import { useSelectedInstance } from "~/canvas/shared/nano-states";++type EditableProps = {+ onDoubleClick: () => void;+};++export const useIsEditing = (instance: Instance): [boolean, EditableProps] => {+ const [selectedInstance] = useSelectedInstance();+ const [isEditing, setIsEditing] = useState(false);++ useEffect(() => {+ if (selectedInstance?.id !== instance.id) {+ setIsEditing(false);+ }+ }, [selectedInstance, instance, setIsEditing]);++ const props = useMemo(+ () => ({+ onKeyDown(event: KeyboardEvent) {+ if (selectedInstance?.id !== instance.id) return;+ if (event.key === "Enter" && isEditing === false) {+ setIsEditing(true);+ // Prevent inserting a newline when you want to start editing mode+ event.preventDefault();+ return;+ }+ if (event.key === "Escape") {+ setIsEditing(false);+ }+ },+ onDoubleClick() {+ // We only want to do this if the component is the one that was clicked+ // @todo this logic shouldn't be necessary+ if (selectedInstance?.id !== instance.id) return;++ setIsEditing(true);+ },+ }),+ [setIsEditing, selectedInstance, instance, isEditing]+ );+ return [isEditing, props];+};diff --git a/app/canvas/features/wrapper-component/wrapper-component.tsx b/app/canvas/features/wrapper-component/wrapper-component.tsxindex e9f5d203c4..3def6689eb 100644--- a/app/canvas/features/wrapper-component/wrapper-component.tsx+++ b/app/canvas/features/wrapper-component/wrapper-component.tsx@@ -10,6 +10,9 @@ import { primitives } from "~/shared/canvas-components";import { useCss } from "./use-css";import { useDraggable } from "./use-draggable";import { useEnsureFocus } from "./use-ensure-focus";+import { Editor } from "./editor/editor";+import { useIsEditing } from "./editor/use-is-editing";+import { useContentEditable } from "~/shared/text-editor";type WrapperComponentDevProps = {instance: Instance;@@ -26,53 +29,62 @@ export const WrapperComponentDev = ({...rest}: WrapperComponentDevProps) => {const className = useCss({ instance, css });+ const [isEditing, isEditingProps] = useIsEditing(instance);+ const [editableRefCallback, editableProps] = useContentEditable(isEditing);const { dragRefCallback, ...draggableProps } = useDraggable({instance,// We can't drag if we are editing text.- isDisabled: false,+ isDisabled: isEditing === true,});const focusRefCallback = useEnsureFocus();const refCallback = useCallback((element) => {+ if (isEditing) editableRefCallback(element);dragRefCallback(element);focusRefCallback(element);},- [dragRefCallback, focusRefCallback]+ [dragRefCallback, focusRefCallback, editableRefCallback, isEditing]);const userProps = useUserProps(instance.id);- const readonly =+ const readonlyProps =instance.component === "Input" ? { readOnly: true } : undefined;const { Component } = primitives[instance.component];- return (- <>- <Component- {...userProps}- {...rest}- {...draggableProps}- {...readonly}- className={className}- id={instance.id}- tabIndex={0}- data-component={instance.component}- data-id={instance.id}- ref={refCallback}- onClick={(event: MouseEvent) => {- if (instance.component === "Link") {- event.preventDefault();- }- }}- onSubmit={(event: FormEvent) => {- // Prevent submitting the form when clicking a button type submit+ const element = (+ <Component+ {...userProps}+ {...rest}+ {...draggableProps}+ {...readonlyProps}+ {...isEditingProps}+ {...editableProps}+ className={className}+ id={instance.id}+ tabIndex={0}+ data-component={instance.component}+ data-id={instance.id}+ ref={refCallback}+ onClick={(event: MouseEvent) => {+ if (instance.component === "Link") {event.preventDefault();- }}- >- {renderWrapperComponentChildren(children)}- </Component>- </>+ }+ }}+ onSubmit={(event: FormEvent) => {+ // Prevent submitting the form when clicking a button type submit+ event.preventDefault();+ }}+ >+ {isEditing ? undefined : renderWrapperComponentChildren(children)}+ </Component>);++ if (isEditing) {+ return <Editor>{element}</Editor>;+ }++ return element;};diff --git a/app/shared/text-editor/content-editable.tsx b/app/shared/text-editor/content-editable.tsxnew file mode 100644index 0000000000..1c5a59704c--- /dev/null+++ b/app/shared/text-editor/content-editable.tsx@@ -0,0 +1,31 @@+import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";+import { useCallback, useEffect } from "react";++type RefCallback = (rootElement: null | HTMLElement) => void;++const props = {+ //autoCapitalize: false,+ //autoComplete: false,+ contentEditable: true,+};++export const useContentEditable = (+ isEditing: boolean+): [RefCallback, typeof props | undefined] => {+ const [editor] = useLexicalComposerContext();++ const ref: RefCallback = useCallback(+ (rootElement) => {+ editor.setRootElement(rootElement);+ },+ [editor]+ );++ useEffect(() => {+ if (isEditing === true) return;+ // Lets unset the root element when we are not editing+ editor.setRootElement(null);+ }, [editor, isEditing]);++ return [ref, isEditing ? props : undefined];+};diff --git a/app/shared/text-editor/example-text-editor.tsx b/app/shared/text-editor/example-text-editor.tsxindex e013491def..29d938cf4c 100644--- a/app/shared/text-editor/example-text-editor.tsx+++ b/app/shared/text-editor/example-text-editor.tsx@@ -89,7 +89,9 @@ export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {<div className="editor-container"><RichTextPlugincontentEditable={<ContentEditable className="editor-input" />}- placeholder={<Placeholder />}+ placeholder={+ <div className="editor-placeholder">Enter some plain text...</div>+ }/><OnChangePlugin onChange={onChange} /><HistoryPlugin />@@ -100,7 +102,3 @@ export const ExampleTextEditor = ({ onChange }: ExampleTextEditorProps) => {</>);};--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.tsnew file mode 100644index 0000000000..3969201748--- /dev/null+++ b/app/shared/text-editor/index.ts@@ -0,0 +1,9 @@+export {+ LexicalComposer,+ RichTextPlugin,+ ContentEditable,+ HistoryPlugin,+} from "./lexical";+export { config } from "./config";+export { InstancePlugin } from "./plugins/plugin-instance";+export { useContentEditable } from "./content-editable";