-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #2601 allow customization when convert from content model to plai…
…n text (#2605) * Fix #2601 allow customization when convert from content model to plain text * Improve * fix test * fix build
- Loading branch information
1 parent
73e62ec
commit a3a14a4
Showing
8 changed files
with
279 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 78 additions & 50 deletions
128
packages/roosterjs-content-model-dom/lib/modelToText/contentModelToText.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,106 @@ | ||
import type { ContentModelBlockGroup, ContentModelDocument } from 'roosterjs-content-model-types'; | ||
import type { | ||
ContentModelBlockGroup, | ||
ContentModelDocument, | ||
ModelToTextCallbacks, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
const TextForHR = '________________________________________'; | ||
const defaultCallbacks: Required<ModelToTextCallbacks> = { | ||
onDivider: divider => (divider.tagName == 'hr' ? TextForHR : ''), | ||
onEntityBlock: () => '', | ||
onEntitySegment: entity => entity.wrapper.textContent ?? '', | ||
onGeneralSegment: segment => segment.element.textContent ?? '', | ||
onImage: () => ' ', | ||
onText: text => text.text, | ||
onParagraph: () => true, | ||
onTable: () => true, | ||
onBlockGroup: () => true, | ||
}; | ||
|
||
/** | ||
* Convert Content Model to plain text | ||
* @param model The source Content Model | ||
* @param [separator='\r\n'] The separator string used for connect lines | ||
* @param callbacks Callbacks to customize the behavior of contentModelToText function | ||
*/ | ||
export function contentModelToText( | ||
model: ContentModelDocument, | ||
separator: string = '\r\n' | ||
separator: string = '\r\n', | ||
callbacks?: ModelToTextCallbacks | ||
): string { | ||
const textArray: string[] = []; | ||
const fullCallbacks = Object.assign({}, defaultCallbacks, callbacks); | ||
|
||
contentModelToTextArray(model, textArray); | ||
contentModelToTextArray(model, textArray, fullCallbacks); | ||
|
||
return textArray.join(separator); | ||
} | ||
|
||
function contentModelToTextArray(group: ContentModelBlockGroup, textArray: string[]) { | ||
group.blocks.forEach(block => { | ||
switch (block.blockType) { | ||
case 'Paragraph': | ||
let text = ''; | ||
function contentModelToTextArray( | ||
group: ContentModelBlockGroup, | ||
textArray: string[], | ||
callbacks: Required<ModelToTextCallbacks> | ||
) { | ||
if (callbacks.onBlockGroup(group)) { | ||
group.blocks.forEach(block => { | ||
switch (block.blockType) { | ||
case 'Paragraph': | ||
if (callbacks.onParagraph(block)) { | ||
let text = ''; | ||
|
||
block.segments.forEach(segment => { | ||
switch (segment.segmentType) { | ||
case 'Br': | ||
textArray.push(text); | ||
text = ''; | ||
break; | ||
block.segments.forEach(segment => { | ||
switch (segment.segmentType) { | ||
case 'Br': | ||
textArray.push(text); | ||
text = ''; | ||
break; | ||
|
||
case 'Entity': | ||
text += segment.wrapper.textContent || ''; | ||
break; | ||
case 'Entity': | ||
text += callbacks.onEntitySegment(segment); | ||
break; | ||
|
||
case 'General': | ||
text += segment.element.textContent || ''; | ||
break; | ||
case 'General': | ||
text += callbacks.onGeneralSegment(segment); | ||
break; | ||
|
||
case 'Text': | ||
text += segment.text; | ||
break; | ||
case 'Text': | ||
text += callbacks.onText(segment); | ||
break; | ||
|
||
case 'Image': | ||
text += ' '; | ||
break; | ||
} | ||
}); | ||
case 'Image': | ||
text += callbacks.onImage(segment); | ||
break; | ||
} | ||
}); | ||
|
||
if (text) { | ||
textArray.push(text); | ||
} | ||
if (text) { | ||
textArray.push(text); | ||
} | ||
} | ||
|
||
break; | ||
break; | ||
|
||
case 'Divider': | ||
textArray.push(block.tagName == 'hr' ? TextForHR : ''); | ||
break; | ||
case 'Entity': | ||
textArray.push(''); | ||
break; | ||
case 'Divider': | ||
textArray.push(callbacks.onDivider(block)); | ||
break; | ||
case 'Entity': | ||
textArray.push(callbacks.onEntityBlock(block)); | ||
break; | ||
|
||
case 'Table': | ||
block.rows.forEach(row => | ||
row.cells.forEach(cell => { | ||
contentModelToTextArray(cell, textArray); | ||
}) | ||
); | ||
break; | ||
case 'Table': | ||
if (callbacks.onTable(block)) { | ||
block.rows.forEach(row => | ||
row.cells.forEach(cell => { | ||
contentModelToTextArray(cell, textArray, callbacks); | ||
}) | ||
); | ||
} | ||
break; | ||
|
||
case 'BlockGroup': | ||
contentModelToTextArray(block, textArray); | ||
break; | ||
} | ||
}); | ||
case 'BlockGroup': | ||
contentModelToTextArray(block, textArray, callbacks); | ||
break; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.