Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(lab): add getHTML() to RichTextEditor imperative ref (#4550)

Sherry95f28a108e5e
diff --git a/apps/storybook/stories/RichTextEditor.stories.tsx b/apps/storybook/stories/RichTextEditor.stories.tsx
index 27bbacb378..f066d2f37e 100644
--- a/apps/storybook/stories/RichTextEditor.stories.tsx
+++ b/apps/storybook/stories/RichTextEditor.stories.tsx
@@ -191,6 +191,14 @@ export const ImperativeRef = {
}}>
getMarkdown()
</button>
+ <button
+ type="button"
+ onClick={() => {
+ const html = ref.current?.getHTML();
+ setReadout(`getHTML():\n${html}`);
+ }}>
+ getHTML()
+ </button>
<button
type="button"
onClick={() => {
diff --git a/packages/lab/package.json b/packages/lab/package.json
index 04c222742d..dd14c4a0f9 100644
--- a/packages/lab/package.json
+++ b/packages/lab/package.json
@@ -64,6 +64,7 @@
"lexical": "^0.46.0",
"@lexical/react": "^0.46.0",
"@lexical/markdown": "^0.46.0",
+ "@lexical/html": "^0.46.0",
"@lexical/link": "^0.46.0",
"@lexical/list": "^0.46.0",
"@lexical/rich-text": "^0.46.0",
@@ -77,6 +78,7 @@
},
"devDependencies": {
"@lexical/code": "^0.46.0",
+ "@lexical/html": "^0.46.0",
"@lexical/link": "^0.46.0",
"@lexical/list": "^0.46.0",
"@lexical/markdown": "^0.46.0",
@@ -98,6 +100,9 @@
"@lexical/markdown": {
"optional": true
},
+ "@lexical/html": {
+ "optional": true
+ },
"@lexical/link": {
"optional": true
},
diff --git a/packages/lab/src/RichTextEditor/RichTextEditor.doc.mjs b/packages/lab/src/RichTextEditor/RichTextEditor.doc.mjs
index 8abaa445b4..f90244dd0a 100644
--- a/packages/lab/src/RichTextEditor/RichTextEditor.doc.mjs
+++ b/packages/lab/src/RichTextEditor/RichTextEditor.doc.mjs
@@ -153,7 +153,7 @@ export const docs = {
{
guidance: true,
description:
- 'Use a ref (RichTextEditorRef) to imperatively focus(), clear(), read the state via getEditorState(), serialize to Markdown via getMarkdown(), or reach the LexicalEditor via getEditor(). The handle is available after mount. getMarkdown() uses the same transformers prop the editor is configured with. focus() and clear() are no-ops when the editor is read-only or disabled, and clear() resets to a single empty paragraph.',
+ 'Use a ref (RichTextEditorRef) to imperatively focus(), clear(), read the state via getEditorState(), serialize to Markdown via getMarkdown() or HTML via getHTML(), or reach the LexicalEditor via getEditor(). The handle is available after mount. getMarkdown() uses the same transformers prop the editor is configured with. focus() and clear() are no-ops when the editor is read-only or disabled, and clear() resets to a single empty paragraph.',
},
{
guidance: false,
diff --git a/packages/lab/src/RichTextEditor/RichTextEditor.test.tsx b/packages/lab/src/RichTextEditor/RichTextEditor.test.tsx
index b326ed5959..4327c3005c 100644
--- a/packages/lab/src/RichTextEditor/RichTextEditor.test.tsx
+++ b/packages/lab/src/RichTextEditor/RichTextEditor.test.tsx
@@ -342,6 +342,7 @@ describe('RichTextEditor', () => {
expect(typeof ref.current?.clear).toBe('function');
expect(typeof ref.current?.getEditorState).toBe('function');
expect(typeof ref.current?.getMarkdown).toBe('function');
+ expect(typeof ref.current?.getHTML).toBe('function');
expect(typeof ref.current?.getEditor).toBe('function');
});
@@ -426,6 +427,18 @@ describe('RichTextEditor', () => {
await waitFor(() => expect(ref.current?.getMarkdown()).toBe('Title'));
});
+ it('ref.getHTML() serializes content to an HTML string', () => {
+ const ref = createRef<RichTextEditorRef>();
+ render(
+ <RichTextEditor ref={ref} label="Notes" defaultValue={HELLO_STATE} />,
+ );
+ const html = ref.current?.getHTML();
+ expect(typeof html).toBe('string');
+ // A paragraph with the seeded text renders as a <p> containing "Hello world".
+ expect(html).toContain('<p');
+ expect(html).toContain('Hello world');
+ });
+
it('ref.clear() resets the editor to a single empty paragraph', async () => {
const ref = createRef<RichTextEditorRef>();
render(
diff --git a/packages/lab/src/RichTextEditor/RichTextEditor.tsx b/packages/lab/src/RichTextEditor/RichTextEditor.tsx
index 48b9fdb902..59c5ae9221 100644
--- a/packages/lab/src/RichTextEditor/RichTextEditor.tsx
+++ b/packages/lab/src/RichTextEditor/RichTextEditor.tsx
@@ -80,6 +80,7 @@ import {ListNode, ListItemNode} from '@lexical/list';
import {HeadingNode, QuoteNode} from '@lexical/rich-text';
import {LinkNode, AutoLinkNode} from '@lexical/link';
import {CodeNode, CodeHighlightNode} from '@lexical/code';
+import {$generateHtmlFromNodes} from '@lexical/html';
import type {
EditorState,
Klass,
@@ -237,6 +238,12 @@ export interface RichTextEditorRef {
* `$convertToMarkdownString` run in a read context.
*/
getMarkdown: () => string;
+ /**
+ * Serialize the current content to an HTML string via
+ * `$generateHtmlFromNodes`. Requires a DOM (available in the browser and in
+ * jsdom-based tests). Useful for copy/paste, email, or non-Lexical consumers.
+ */
+ getHTML: () => string;
/**
* Access the underlying `LexicalEditor` instance for advanced use cases
* (custom commands, listeners, node transforms).
@@ -633,6 +640,11 @@ function EditorRefBridge({
editor
.getEditorState()
.read(() => $convertToMarkdownString(transformers)),
+ getHTML: () =>
+ // $generateHtmlFromNodes serializes the whole document (null selection)
+ // to HTML; must run in a read context and requires a DOM.
+ // `@lexical/html` is a subpackage (built dist) — safe.
+ editor.getEditorState().read(() => $generateHtmlFromNodes(editor, null)),
getEditor: () => editor,
}),
[editor, editable, transformers],
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3bd3d280f6..a818991f60 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -680,6 +680,9 @@ importers:
'@lexical/code':
specifier: ^0.46.0
version: 0.46.0([email protected])
+ '@lexical/html':
+ specifier: ^0.46.0
+ version: 0.46.0([email protected])
'@lexical/link':
specifier: ^0.46.0
version: 0.46.0([email protected])