Skip to content

Commit

Permalink
Create line break even if only child
Browse files Browse the repository at this point in the history
  • Loading branch information
Janna Wieneke committed Jul 7, 2024
1 parent bbeba94 commit 8db4d50
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions packages/lexical/src/nodes/LexicalLineBreakNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
SerializedLexicalNode,
} from '../LexicalNode';

import {DOM_TEXT_TYPE} from '../LexicalConstants';
import {LexicalNode} from '../LexicalNode';
import {$applyNodeReplacement} from '../LexicalUtils';

Expand Down Expand Up @@ -49,10 +48,7 @@ export class LineBreakNode extends LexicalNode {

static importDOM(): DOMConversionMap | null {
return {
br: (node: Node) => {
if (isOnlyChild(node)) {
return null;
}
br: () => {
return {
conversion: $convertLineBreakElement,
priority: 0,
Expand All @@ -75,7 +71,7 @@ export class LineBreakNode extends LexicalNode {
}
}

function $convertLineBreakElement(node: Node): DOMConversionOutput {
function $convertLineBreakElement(): DOMConversionOutput {
return {node: $createLineBreakNode()};
}

Expand All @@ -88,31 +84,3 @@ export function $isLineBreakNode(
): node is LineBreakNode {
return node instanceof LineBreakNode;
}

function isOnlyChild(node: Node): boolean {
const parentElement = node.parentElement;
if (parentElement !== null) {
const firstChild = parentElement.firstChild!;
if (
firstChild === node ||
(firstChild.nextSibling === node && isWhitespaceDomTextNode(firstChild))
) {
const lastChild = parentElement.lastChild!;
if (
lastChild === node ||
(lastChild.previousSibling === node &&
isWhitespaceDomTextNode(lastChild))
) {
return true;
}
}
}
return false;
}

function isWhitespaceDomTextNode(node: Node): boolean {
return (
node.nodeType === DOM_TEXT_TYPE &&
/^( |\t|\r?\n)+$/.test(node.textContent || '')
);
}

0 comments on commit 8db4d50

Please sign in to comment.