Skip to content

Commit

Permalink
Fix 265959: extra empty line generated when get plain text (#2566)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Valverde U <bvalverde@microsoft.com>
  • Loading branch information
JiuqingSong and BryanValverdeU committed Apr 5, 2024
1 parent 86cc784 commit a4457db
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
22 changes: 19 additions & 3 deletions demo/scripts/controlsV2/demoButtons/exportContentButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import type { RibbonButton } from '../roosterjsReact/ribbon';
/**
* Key of localized strings of Zoom button
*/
export type ExportButtonStringKey = 'buttonNameExport';
export type ExportButtonStringKey =
| 'buttonNameExport'
| 'menuNameExportHTML'
| 'menuNameExportText';

/**
* "Export content" button on the format ribbon
Expand All @@ -14,9 +17,22 @@ export const exportContentButton: RibbonButton<ExportButtonStringKey> = {
unlocalizedText: 'Export',
iconName: 'Export',
flipWhenRtl: true,
onClick: editor => {
dropDownMenu: {
items: {
menuNameExportHTML: 'as HTML',
menuNameExportText: 'as Plain Text',
},
},
onClick: (editor, key) => {
const win = editor.getDocument().defaultView.open();
const html = exportContent(editor);
let html = '';

if (key == 'menuNameExportHTML') {
html = exportContent(editor);
} else if (key == 'menuNameExportText') {
html = `<pre>${exportContent(editor, 'PlainText')}</pre>`;
}

win.document.write(editor.getTrustedHTMLHandler()(html));
},
commandBarProperties: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ContentModelBlockGroup, ContentModelDocument } from 'roosterjs-content-model-types';

const TextForHR = '________________________________________';

/**
* Convert Content Model to plain text
* @param model The source Content Model
Expand Down Expand Up @@ -46,10 +48,16 @@ function contentModelToTextArray(group: ContentModelBlockGroup, textArray: strin
break;
}
});
textArray.push(text);

if (text) {
textArray.push(text);
}

break;

case 'Divider':
textArray.push(block.tagName == 'hr' ? TextForHR : '');
break;
case 'Entity':
textArray.push('');
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ describe('modelToText', () => {
expect(text).toBe('text1\r\ntext2');
});

it('model with empty lines (BR only in paragraph)', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
const para2 = createParagraph();

para1.segments.push(createBr());
para2.segments.push(createBr());

model.blocks.push(para1, para2);

const text = contentModelToText(model);

expect(text).toBe('\r\n');
});

it('model with paragraph and image', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
Expand All @@ -76,7 +91,7 @@ describe('modelToText', () => {
expect(text).toBe('text1 text2');
});

it('model with divider', () => {
it('model with divider (DIV)', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
const para2 = createParagraph();
Expand All @@ -91,6 +106,21 @@ describe('modelToText', () => {
expect(text).toBe('text1\r\n\r\ntext2');
});

it('model with divider (HR)', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
const para2 = createParagraph();

para1.segments.push(createText('text1'));
para2.segments.push(createText('text2'));

model.blocks.push(para1, createDivider('hr'), para2);

const text = contentModelToText(model);

expect(text).toBe('text1\r\n________________________________________\r\ntext2');
});

it('model with list', () => {
const model = createContentModelDocument();
const para1 = createParagraph();
Expand Down

0 comments on commit a4457db

Please sign in to comment.