Skip to content

Commit

Permalink
Remove style "white-space" from empty paragraph (#2820)
Browse files Browse the repository at this point in the history
* Remove white-space style for empty paragraph

* improve
  • Loading branch information
JiuqingSong authored Oct 5, 2024
1 parent eb4127c commit 2d90d5c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function normalizeParagraph(paragraph: ReadonlyContentModelParagraph) {
mutateBlock(paragraph).segments.pop();
}
}

normalizeParagraphStyle(paragraph);
}

if (!isWhiteSpacePreserved(paragraph.format.whiteSpace)) {
Expand All @@ -51,6 +53,18 @@ export function normalizeParagraph(paragraph: ReadonlyContentModelParagraph) {
moveUpSegmentFormat(paragraph);
}

function normalizeParagraphStyle(paragraph: ReadonlyContentModelParagraph) {
// New paragraph should not have white-space style
if (
paragraph.format.whiteSpace &&
paragraph.segments.every(
seg => seg.segmentType == 'Br' || seg.segmentType == 'SelectionMarker'
)
) {
delete mutateBlock(paragraph).format.whiteSpace;
}
}

function removeEmptySegments(block: ReadonlyContentModelParagraph) {
for (let j = block.segments.length - 1; j >= 0; j--) {
if (isSegmentEmpty(block.segments[j])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,34 @@ describe('Move up format', () => {
cachedElement: mockedCache,
});
});

it('Empty paragraph has white-space style', () => {
const para = createParagraph(false, { whiteSpace: 'pre-wrap' });
const br = createBr();

para.segments.push(br);

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [br],
format: {},
});
});

it('Paragraph has content and white-space style', () => {
const para = createParagraph(false, { whiteSpace: 'pre-wrap' });
const text = createText('test');

para.segments.push(text);

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [text],
format: { whiteSpace: 'pre-wrap' },
});
});
});

0 comments on commit 2d90d5c

Please sign in to comment.