[examples] Feature: Added examples (for the website) #8258
[examples] Feature: Added examples (for the website) #8258etrepum merged 5 commits intofacebook:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
The chat input example would be a great place to demonstrate using multiple editors on the page (a readonly editor per chat message, and a writable editor for the chat input). This way it would be trivial to add support for rich text. The chat messages should also scroll to the bottom after submit. (this is just based on using the examples, not a close reading of the code) |
| const selection = $getSelection(); | ||
| if ($isRangeSelection(selection)) { | ||
| selection.insertNodes([$createTextNode(emoji)]); | ||
| } else { | ||
| // fallback: append to end | ||
| const root = $getRoot(); | ||
| const lastChild = root.getLastChild(); | ||
| if (lastChild) { | ||
| lastChild.selectEnd(); | ||
| const sel = $getSelection(); | ||
| if (sel !== null) { | ||
| sel.insertNodes([$createTextNode(emoji)]); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
| const selection = $getSelection(); | |
| if ($isRangeSelection(selection)) { | |
| selection.insertNodes([$createTextNode(emoji)]); | |
| } else { | |
| // fallback: append to end | |
| const root = $getRoot(); | |
| const lastChild = root.getLastChild(); | |
| if (lastChild) { | |
| lastChild.selectEnd(); | |
| const sel = $getSelection(); | |
| if (sel !== null) { | |
| sel.insertNodes([$createTextNode(emoji)]); | |
| } | |
| } | |
| } | |
| $insertNodes([$createTextNode(emoji)]); |
| .read(() => $getRoot().getTextContent().trim() !== ''); | ||
| if (hasContent) { | ||
| onSubmit(editor.getEditorState()); | ||
| clearEditor(editor); |
There was a problem hiding this comment.
With ClearEditorExtension as a dependency:
| clearEditor(editor); | |
| editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined); |
| namespace: '@lexical/website/chat-message', | ||
| theme: chatMessageTheme, | ||
| }), | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps |
There was a problem hiding this comment.
This exception isn't needed, initialState is a dependency. Both the EditorState and the function references are stable.
| useEffect(() => { | ||
| return editor.registerUpdateListener(({editorState}) => { | ||
| editorState.read(() => { | ||
| setCount($getRoot().getTextContent().length); |
There was a problem hiding this comment.
| setCount($getRoot().getTextContent().length); | |
| setCount($getRoot().getTextContentSize()); |
| editor.registerCommand( | ||
| SELECTION_CHANGE_COMMAND, | ||
| (_payload, _newEditor) => { | ||
| $updateToolbar(); | ||
| return false; | ||
| }, | ||
| COMMAND_PRIORITY_LOW, | ||
| ), |
There was a problem hiding this comment.
This is redundant, the update listener is called when the selection changes
| editor.registerCommand( | |
| SELECTION_CHANGE_COMMAND, | |
| (_payload, _newEditor) => { | |
| $updateToolbar(); | |
| return false; | |
| }, | |
| COMMAND_PRIORITY_LOW, | |
| ), |
etrepum
left a comment
There was a problem hiding this comment.
The clearEditor function is still there
| export function clearEditor(editor: LexicalEditor) { | ||
| editor.update(() => { | ||
| const root = $getRoot(); | ||
| root.clear(); | ||
| const paragraph = $createParagraphNode(); | ||
| paragraph.append($createTextNode('')); | ||
| root.append(paragraph); | ||
| if ($isParagraphNode(paragraph)) { | ||
| paragraph.selectEnd(); | ||
| } | ||
| }); | ||
| } | ||
|
|
There was a problem hiding this comment.
Should be unused now
| export function clearEditor(editor: LexicalEditor) { | |
| editor.update(() => { | |
| const root = $getRoot(); | |
| root.clear(); | |
| const paragraph = $createParagraphNode(); | |
| paragraph.append($createTextNode('')); | |
| root.append(paragraph); | |
| if ($isParagraphNode(paragraph)) { | |
| paragraph.selectEnd(); | |
| } | |
| }); | |
| } |
| .read(() => $getRoot().getTextContent().trim() !== ''); | ||
| if (hasContent) { | ||
| onSubmit(editor.getEditorState()); | ||
| clearEditor(editor); |
There was a problem hiding this comment.
| clearEditor(editor); | |
| editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined); |
There was a problem hiding this comment.
(this change will also need changes to the imports)
|
These nits are the last things I noticed here, so once these are cleaned up it looks good to go! |
Description
Added 4 examples to the /examples directory:
These examples will be useful for the website, since we will be importing the editors. Also, we will link the editors to stackblitz from the site.
Test plan
I tested them on my personal branch in stackblitz. Here are the links:
Notion
https://stackblitz.com/github/m-santanna/lexical/tree/examples/examples/website-notion
Toolbar
https://stackblitz.com/github/m-santanna/lexical/tree/examples/examples/website-toolbar
Chatapp
https://stackblitz.com/github/m-santanna/lexical/tree/examples/examples/website-chat
Rich Input
https://stackblitz.com/github/m-santanna/lexical/tree/examples/examples/website-rich-input