-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(richtext-lexical): export hasText helper (#9484)
### What? Extracted `hasText` helper method in `richTextValidateHOC` ### Why? The new exported `hasText` helper method can now also be used during front-end serialization - for example, to check whether a caption element should be rendered when text is optional and therefore possibly empty (which would allow us to prevent rendering an empty caption element).
- Loading branch information
Showing
3 changed files
with
41 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { | ||
SerializedEditorState, | ||
SerializedLexicalNode, | ||
SerializedParagraphNode, | ||
SerializedTextNode, | ||
} from 'lexical' | ||
|
||
export function hasText( | ||
value: null | SerializedEditorState<SerializedLexicalNode> | undefined, | ||
): boolean { | ||
const hasChildren = !!value?.root?.children?.length | ||
|
||
let hasOnlyEmptyParagraph = false | ||
if (value?.root?.children?.length === 1) { | ||
if (value?.root?.children[0]?.type === 'paragraph') { | ||
const paragraphNode = value?.root?.children[0] as SerializedParagraphNode | ||
|
||
if (!paragraphNode?.children || paragraphNode?.children?.length === 0) { | ||
hasOnlyEmptyParagraph = true | ||
} else if (paragraphNode?.children?.length === 1) { | ||
const paragraphNodeChild = paragraphNode?.children[0] | ||
if (paragraphNodeChild.type === 'text') { | ||
if (!(paragraphNodeChild as SerializedTextNode | undefined)?.['text']?.length) { | ||
hasOnlyEmptyParagraph = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!hasChildren || hasOnlyEmptyParagraph) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters