Improve trailing spurious breaks + tests
This commit is contained in:
parent
9a530a72f6
commit
832da062cc
3 changed files with 63 additions and 22 deletions
|
@ -250,7 +250,7 @@ function parseHtmlMessage(html, partCreator, isQuotedMessage) {
|
|||
}
|
||||
|
||||
export function parsePlainTextMessage(body, partCreator, isQuotedMessage) {
|
||||
const lines = body.split(/\r\n|\r|\n/g); // split on any new-line combination not just \n
|
||||
const lines = body.split(/\r\n|\r|\n/g); // split on any new-line combination not just \n, collapses \r\n
|
||||
const parts = lines.reduce((parts, line, i) => {
|
||||
if (isQuotedMessage) {
|
||||
parts.push(partCreator.plain(QUOTE_LINE_PREFIX));
|
||||
|
|
|
@ -100,6 +100,10 @@ export function formatRangeAsCode(range) {
|
|||
replaceRangeAndExpandSelection(range, parts);
|
||||
}
|
||||
|
||||
// parts helper methods
|
||||
const isBlank = part => !part.text || !/\S/.test(part.text);
|
||||
const isNL = part => part.type === "newline";
|
||||
|
||||
export function toggleInlineFormat(range, prefix, suffix = prefix) {
|
||||
const {model, parts} = range;
|
||||
const {partCreator} = model;
|
||||
|
@ -113,14 +117,12 @@ export function toggleInlineFormat(range, prefix, suffix = prefix) {
|
|||
// - 2 newline parts in sequence
|
||||
// - newline part, plain(<empty or just spaces>), newline part
|
||||
|
||||
const isBlank = part => !part.text || !/\S/.test(part.text);
|
||||
const isNL = part => part.type === "newline";
|
||||
|
||||
// bump startIndex onto the first non-blank after the paragraph ending
|
||||
if (isBlank(parts[i - 2]) && isNL(parts[i - 1]) && !isNL(parts[i]) && !isBlank(parts[i])) {
|
||||
startIndex = i;
|
||||
}
|
||||
|
||||
// if at a paragraph break, store the indexes of the paragraph
|
||||
if (isNL(parts[i - 1]) && isNL(parts[i])) {
|
||||
paragraphIndexes.push([startIndex, i - 1]);
|
||||
startIndex = i + 1;
|
||||
|
@ -129,9 +131,11 @@ export function toggleInlineFormat(range, prefix, suffix = prefix) {
|
|||
startIndex = i + 1;
|
||||
}
|
||||
}
|
||||
if (startIndex < parts.length) {
|
||||
// TODO don't use parts.length here to clean up any trailing cruft
|
||||
paragraphIndexes.push([startIndex, parts.length]);
|
||||
|
||||
const lastNonEmptyPart = parts.map(isBlank).lastIndexOf(false);
|
||||
// If we have not yet included the final paragraph then add it now
|
||||
if (startIndex <= lastNonEmptyPart) {
|
||||
paragraphIndexes.push([startIndex, lastNonEmptyPart + 1]);
|
||||
}
|
||||
|
||||
// keep track of how many things we have inserted as an offset:=0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue