Lexical keeps empty paragraph when everything is removed from the editor #10103
revnelson
started this conversation in
Feature Requests & Ideas
Replies: 1 comment
-
I made a util to remove the empty node and stuck it in a // https://stackoverflow.com/questions/75493899/how-to-check-if-the-editor-is-empty
export const isEmpty = (node?: { root: SerializedRootNode }) => {
if (!node || !node?.root) {
return true
}
const { root } = node
const isRoot = root.type === 'root'
const hasSingleChild = root.children.length < 2
const hasNestedChild = root.children.some((childNode) => {
// @ts-expect-error @TODO why is `children` not in the typedef?
if (childNode?.children) {
// @ts-expect-error saa
return childNode.children.length > 0
}
return false
})
return isRoot && hasSingleChild && !hasNestedChild
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When you remove all content from a lexical editor that previously had content, an empty paragraph is returned from the API.
This creates problems with our frontend serializer adding unwanted whitespace and causing us to have to do ugly checks like:
if (root?.children?.length === 1 && root.children.at(0).type === 'paragraph' && root.children.at(0).children?.length === 0) return
Can we get some sort of option to fully remove the
root
object when all content is removed from a lexical editor?Beta Was this translation helpful? Give feedback.
All reactions