Migrating To Extensions
Migrating to Lexical Builder is designed to be seamless! Lexical Builder is a higher-level API on top of the existing functionality you are already using.
Generally speaking, the only thing that needs to change is how you create the editor. Everything else can be migrated (or not) at your own leisure, but the result will be simpler and more composable if you do!
React App
- Before
- After
import {ContentEditable} from '@lexical/react/LexicalContentEditable';
import {AutoFocusPlugin} from '@lexical/react/LexicalAutoFocusPlugin';
import {LexicalComposer} from '@lexical/react/LexicalComposer';
import {LexicalErrorBoundary} from '@lexical/react/LexicalErrorBoundary';
import {HistoryPlugin} from '@lexical/react/LexicalHistoryPlugin';
import {RichTextPlugin} from '@lexical/react/LexicalRichTextPlugin';
import ExampleTheme from './ExampleTheme';
import ToolbarPlugin from './plugins/ToolbarPlugin';
import TreeViewPlugin from './plugins/TreeViewPlugin';
const placeholderText = 'Enter some rich text...';
const contentEditable = (
<ContentEditable
className="editor-input"
aria-placeholder={placeholderText}
placeholder={<div className="editor-placeholder">{placeholderText}</div>}
/>
);
const editorConfig = {
namespace: 'React.js Demo',
nodes: [],
// Handling of errors during update
onError(error: Error) {
throw error;
},
// The editor theme
theme: ExampleTheme,
};
export default function App() {
return (
<LexicalComposer initialConfig={editorConfig}>
<div className="editor-container">
<ToolbarPlugin />
<div className="editor-inner">
<RichTextPlugin
contentEditable={contentEditable}
ErrorBoundary={LexicalErrorBoundary}
/>
<HistoryPlugin />
<AutoFocusPlugin />
<TreeViewPlugin />
</div>
</div>
</LexicalComposer>
);
}
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { HistoryExtension } from "@lexical/history";
import { RichTextExtension } from "@lexical/rich-text";
import { AutoFocusExtension } from "@lexical/extension";
import { LexicalExtensionComposer } from "@lexical/react/LexicalExtensionComposer";
import ExampleTheme from "./ExampleTheme";
import ToolbarPlugin from "./plugins/ToolbarPlugin";
import TreeViewPlugin from "./plugins/TreeViewPlugin";
const placeholderText = "Enter some rich text...";
const contentEditable = (
<ContentEditable
className="editor-input"
aria-placeholder={placeholderText}
placeholder={<div className="editor-placeholder">{placeholderText}</div>}
/>
);
const editorExtension = defineExtension({
name: "[root]",
namespace: "React.js Extension Demo",
dependencies: [
AutoFocusExtension,
RichTextExtension,
HistoryExtension,
configExtension(ReactExtension, { contentEditable: null }),
],
// The editor theme
theme: ExampleTheme,
});
export default function App() {
return (
{/* We specify our own layout for the editor's children */}
<LexicalExtensionComposer extension={editorExtension} contentEditable={null}>
<div className="editor-container">
<ToolbarPlugin />
<div className="editor-inner">
{contentEditable}
<TreeViewPlugin />
</div>
</div>
</LexicalExtensionComposer>
);
}
React Plug-in (without UI)
- Before
- After (minimal)
- After (all-in)
/**
* USAGE:
* 1. Add KeywordNode to your initialConfig nodes Array.
* If you forget this, you will get an error.
* 2. Add the <KeywordPlugin /> as a child of your LexicalComposer.
* If you forget this, it will silently not work.
* 3. Add CSS somewhere for '.keyword'.
* If you don't like that selector, too bad.
*/
import type {EditorConfig, LexicalNode, SerializedTextNode} from 'lexical';
import {$create, TextNode} from 'lexical';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalTextEntity} from '@lexical/react/useLexicalTextEntity';
import {useCallback, useEffect} from 'react';
export class KeywordNode extends TextNode {
$config() {
return this.config('keyword', {extends: TextNode});
}
createDOM(config: EditorConfig): HTMLElement {
const dom = super.createDOM(config);
dom.style.cursor = 'default';
dom.className = 'keyword';
return dom;
}
canInsertTextBefore(): boolean {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
isTextEntity(): true {
return true;
}
}
export function $createKeywordNode(keyword: string): KeywordNode {
return $create(KeywordNode).setTextContent(keyword);
}
export function $isKeywordNode(node: LexicalNode | null | undefined): boolean {
return node instanceof KeywordNode;
}
const KEYWORDS_REGEX =
/(^|[^A-Za-z])(congrats|congratulations|mazel tov|mazal tov)($|[^A-Za-z])/i;
export function KeywordsPlugin(): JSX.Element | null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (!editor.hasNodes([KeywordNode])) {
throw new Error('KeywordsPlugin: KeywordNode not registered on editor');
}
}, [editor]);
const $convertToKeywordNode = useCallback(
(textNode: TextNode): KeywordNode => {
return $createKeywordNode(textNode.getTextContent());
},
[],
);
const getKeywordMatch = useCallback((text: string) => {
const matchArr = KEYWORDS_REGEX.exec(text);
if (matchArr === null) {
return null;
}
const hashtagLength = matchArr[2].length;
const startOffset = matchArr.index + matchArr[1].length;
const endOffset = startOffset + hashtagLength;
return {
end: endOffset,
start: startOffset,
};
}, []);
useLexicalTextEntity<KeywordNode>(
getKeywordMatch,
KeywordNode,
$convertToKeywordNode,
);
return null;
}
// Use this strategy for a minimal changes and to expose a backwards
// compatible interface to support editors not using Lexical Builder
/**
* USAGE:
* 1. Add KeywordsExtension as a dependency to your LexicalExtensionComposer root extension
* 2. Add CSS somewhere for '.keyword'.
* If you don't like that selector, too bad.
*/
import type {EditorConfig, LexicalNode, SerializedTextNode} from 'lexical';
import {$create, TextNode, defineExtension, configExtension} from 'lexical';
import {ReactExtension} from '@lexical/react/ReactExtension';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalTextEntity} from '@lexical/react/useLexicalTextEntity';
import {useCallback, useEffect} from 'react';
export class KeywordNode extends TextNode {
$config() {
return this.config('keyword', {extends: TextNode});
}
createDOM(config: EditorConfig): HTMLElement {
const dom = super.createDOM(config);
dom.style.cursor = 'default';
dom.className = 'keyword';
return dom;
}
canInsertTextBefore(): boolean {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
isTextEntity(): true {
return true;
}
}
export function $createKeywordNode(keyword: string): KeywordNode {
return $create(KeywordNode).setTextContent(keyword);
}
export function $isKeywordNode(node: LexicalNode | null | undefined): boolean {
return node instanceof KeywordNode;
}
const KEYWORDS_REGEX =
/(^|[^A-Za-z])(congrats|congratulations|mazel tov|mazal tov)($|[^A-Za-z])/i;
export function KeywordsPlugin(): JSX.Element | null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (!editor.hasNodes([KeywordNode])) {
throw new Error('KeywordsPlugin: KeywordNode not registered on editor');
}
}, [editor]);
const $convertToKeywordNode = useCallback(
(textNode: TextNode): KeywordNode => {
return $createKeywordNode(textNode.getTextContent());
},
[],
);
const getKeywordMatch = useCallback((text: string) => {
const matchArr = KEYWORDS_REGEX.exec(text);
if (matchArr === null) {
return null;
}
const hashtagLength = matchArr[2].length;
const startOffset = matchArr.index + matchArr[1].length;
const endOffset = startOffset + hashtagLength;
return {
end: endOffset,
start: startOffset,
};
}, []);
useLexicalTextEntity<KeywordNode>(
getKeywordMatch,
KeywordNode,
$convertToKeywordNode,
);
return null;
}
// We only need to add metadata so that you can easily use this extension!
export const KeywordsExtension = defineExtension({
name: '@lexical/example/LexicalKeywords',
nodes: () => [KeywordNode],
dependencies: [
configExtension(ReactExtension, {decorators: [<KeywordsPlugin />]}),
],
});
// Use this strategy if you are dropping legacy support
/**
* USAGE:
* 1. Add KeywordsExtension as a dependency to your LexicalExtensionComposer root extension
* OR use it in a Vanilla JS project because it never really needed React!
* 2. Add CSS somewhere for '.keyword', or change it!
* If you don't like that selector, use
* `configExtension(KeywordsExtension, {className: 'something-else'})`
*/
import type {
EditorConfig,
LexicalNode,
SerializedTextNode,
} from "lexical";
import {
$create,
TextNode,
configExtension
defineExtension,
safeCast,
} from "lexical";
import {
$getExtensionDependency,
} from "@lexical/extension";
import { registerLexicalTextEntity } from "@lexical/text";
import { useCallback, useEffect } from "react";
// Provide any type-safe configuration, that a Node can use in its
// implementation, without trying to shoehorn it into the theme!
export interface KeywordsConfig {
/** The className to use for KeywordNode, default is "keyword" */
className: string;
}
export class KeywordNode extends TextNode {
$config() {
return this.config('keyword', {extends: TextNode});
}
createDOM(config: EditorConfig): HTMLElement {
const dom = super.createDOM(config);
dom.style.cursor = "default";
// This lets us configure the class name!
dom.className = $getExtensionDependency(KeywordsExtension).config.className;
return dom;
}
canInsertTextBefore(): boolean {
return false;
}
canInsertTextAfter(): boolean {
return false;
}
isTextEntity(): true {
return true;
}
}
export function $createKeywordNode(keyword: string): KeywordNode {
return new KeywordNode(keyword);
}
export function $isKeywordNode(node: LexicalNode | null | undefined): boolean {
return node instanceof KeywordNode;
}
const KEYWORDS_REGEX =
/(^|[^A-Za-z])(congrats|congratulations|mazel tov|mazal tov)($|[^A-Za-z])/i;
function $convertToKeywordNode(textNode: TextNode) {
return $createKeywordNode(textNode.getTextContent());
}
// Oh wait, we don't even have a React dependency anymore!
export const KeywordsExtension = defineExtension({
name: "@lexical/example/LexicalKeywords",
nodes: () => [KeywordNode],
config: safeCast<KeywordsConfig>({ className: "keyword" }),
register(editor) {
return mergeRegister(
...registerLexicalTextEntity(
editor,
getKeywordMatch,
KeywordNode,
$convertToKeywordNode,
)
);
},
});