You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to mimic Facebook's behaviour where typing 'enter' doesn't start a new paragraph but introduces a linebreak instead.
However, If they press enter twice, it should start a new paragraph.
How could I implement this?
I've tried writing a plugin that merges a new paragraph with the previous paragraph as soon as it contains any text:
export default function LineBreaksNotParagraphsPlugin() {
const [editor] = useLexicalComposerContext();
useEffect(() => {
// Use linebreaks instead of paragraphs (except when two linebreaks after each other)
editor.registerNodeTransform(CustomParagraphNode, (paragraph) => {
const previousSibling = paragraph.getPreviousSibling();
const mergeWithPreviousSibling = () => {
previousSibling.append(new LineBreakNode());
previousSibling.append(...paragraph.getChildren());
paragraph.remove();
};
if (previousSibling?.getType() === 'paragraph' && !paragraph.isEmpty()) {
mergeWithPreviousSibling();
}
});
});
return null;
}
This needs to be extended to prevent merging if the previous paragraph is empty (= start a new paragraph when entering two linebreaks), but then I'd need to make sure this paragraph doesn't get merged with the one before subsequently. I tried making a CustomParagraphNode with a flag that indicates if the paragraph can be merged (= it's a linebreak) or not (= it's a paragraph), but without success thus far.
Just keeping the above implementation, which simply replaces paragraphs by <br /><br />, would be acceptable too if it weren't for the fact that you can't set headings, list items etc. anymore, because the whole text is a single paragraph.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'd like to mimic Facebook's behaviour where typing 'enter' doesn't start a new paragraph but introduces a linebreak instead.
However, If they press enter twice, it should start a new paragraph.
How could I implement this?
I've tried writing a plugin that merges a new paragraph with the previous paragraph as soon as it contains any text:
This needs to be extended to prevent merging if the previous paragraph is empty (= start a new paragraph when entering two linebreaks), but then I'd need to make sure this paragraph doesn't get merged with the one before subsequently. I tried making a CustomParagraphNode with a flag that indicates if the paragraph can be merged (= it's a linebreak) or not (= it's a paragraph), but without success thus far.
Just keeping the above implementation, which simply replaces paragraphs by
<br /><br />
, would be acceptable too if it weren't for the fact that you can't set headings, list items etc. anymore, because the whole text is a single paragraph.Does anyone have recommendations?
Beta Was this translation helpful? Give feedback.
All reactions