Skip to content

[lexical][lexical-clipboard][lexical-playground] Feature: ImageNode caption support for exportDOM and importDOM#7900

Merged
etrepum merged 6 commits intofacebook:mainfrom
Kepron:export-import-dom-caption
Oct 6, 2025
Merged

[lexical][lexical-clipboard][lexical-playground] Feature: ImageNode caption support for exportDOM and importDOM#7900
etrepum merged 6 commits intofacebook:mainfrom
Kepron:export-import-dom-caption

Conversation

@Kepron
Copy link
Copy Markdown
Contributor

@Kepron Kepron commented Oct 3, 2025

Description

These changes will allow ImageNode's exportDOM and importDOM to use the caption feature. Thanks to @etrepum on the Discord channel for guiding me on this.

Also includes a bug fix to the LexicalNode constructor so $cloneWithProperties can be used in read-only editors, and a small performance improvement to lexical/clipboard functions that use it to slice TextNode based on the current selection.

Closes #5025

Test plan

Before

  • Users could only add images without caption on html export.

After

  • Now users can use caption for html export.

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Oct 3, 2025

Hi @Kepron!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@vercel
Copy link
Copy Markdown

vercel bot commented Oct 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lexical Ready Ready Preview Comment Oct 6, 2025 1:14am
lexical-playground Ready Ready Preview Comment Oct 6, 2025 1:14am

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Oct 3, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 3, 2025
@Kepron Kepron closed this Oct 4, 2025
@Kepron
Copy link
Copy Markdown
Contributor Author

Kepron commented Oct 5, 2025

I discovered that every time an image with a caption is imported, an extra p element with a br child is added above and below it. I’m turning the PR into a draft until I fix this bug.

Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an e2e test, made it import/export html instead of just plain text, and fixed some bugs here

Comment on lines +323 to +327
if (selection !== null && $isTextNode(target)) {
target = $sliceSelectedTextNodeContent(
selection,
$cloneWithProperties(target),
);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a performance improvement, it was doing some unnecessary work by cloning every node when there is a selection instead of just nodes it intends to modify

<div className="image-caption-container">
<LexicalNestedComposer initialEditor={caption}>
<AutoFocusPlugin />
<DisableCaptionOnBlur setShowCaption={setShowCaption} />
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a UI improvement, if you remove the caption content and the caption editor loses focus then it will revert to the same state as it had before clicking the add caption button

{showCaption && (
<div className="image-caption-container">
<LexicalNestedComposer initialEditor={caption}>
<AutoFocusPlugin />
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't make sense to autofocus in all cases (e.g. when an image with caption was pasted), so I removed this plugin

Comment on lines +421 to +427
if (show) {
node.__caption.update(() => {
if (!$getSelection()) {
$getRoot().selectEnd();
}
});
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the equivalent to the AutofocusPlugin so when you click the add caption button it focuses the caption editor

function $convertImageElement(domNode: Node): null | DOMConversionOutput {
const img = domNode as HTMLImageElement;
if (img.src.startsWith('file:///') || isGoogleDocCheckboxImg(img)) {
const src = img.getAttribute('src');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using getAttribute prevents it from adding the origin when the original src was a relative path

return {
img: (node: Node) => ({
figcaption: () => ({
conversion: () => ({node: null}),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

figcaption is imported directly from the figure importer, we ignore it here to save some work


if (__DEV__) {
if (this.__type !== 'root') {
errorOnReadOnly();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug fix where $cloneWithProperties would throw when it shouldn't. The invariant this is checking is already checked appropriately above in $setNodeKey

figure: () => ({
conversion: (node) => {
return {
after: (childNodes) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect to parse an img node from the children, and if so then we find the associated caption and generate the caption editor's content from its DOM

@etrepum etrepum changed the title [lexical-playground] Feature: Caption has been added to exportDOM and importDOM [lexical][lexical-clipboard][lexical-playground] Feature: ImageNode caption support for exportDOM and importDOM Oct 6, 2025
@etrepum etrepum marked this pull request as ready for review October 6, 2025 05:15
@etrepum etrepum added this pull request to the merge queue Oct 6, 2025
Merged via the queue into facebook:main with commit 81446cd Oct 6, 2025
42 checks passed
@Kepron Kepron deleted the export-import-dom-caption branch October 6, 2025 13:57
@Kepron Kepron restored the export-import-dom-caption branch October 10, 2025 10:38
@etrepum etrepum mentioned this pull request Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

image caption conversion

2 participants