Eliminate extra newlines when pasting composer text back into composer

This commit is contained in:
Scott Nonnenberg
2023-08-10 12:54:51 -07:00
committed by Jamie Kyle
parent 366b875fd2
commit c2ad69951f
2 changed files with 38 additions and 11 deletions

View File

@@ -79,7 +79,13 @@ function getStringFromNode(
) {
return element.ariaLabel || '';
}
if (nextSibling && element.nodeName === 'BR') {
// Sometimes we need to add multiple newlines to represent nested divs, and other times
// we only want to add a newline if we know there's another node after this.
const shouldAddNewline =
parent && (nextSibling || parent.childNodes.length === 1);
if (shouldAddNewline && element.nodeName === 'BR') {
return '\n';
}
const childCount = element.childNodes.length;
@@ -94,13 +100,12 @@ function getStringFromNode(
}
if (
parent &&
parent.childNodes.length > 1 &&
shouldAddNewline &&
(element.nodeName === 'P' ||
element.nodeName === 'DIV' ||
element.nodeName === 'TIME')
) {
if (result.length > 0 && !result.endsWith('\n\n')) {
if (result.length > 0 && result !== '\n' && !result.endsWith('\n\n')) {
result += '\n';
}
}