Skip to content

Commit

Permalink
Trim the whole of a message's text
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Svenningsen <chris@carbonfive.com>
  • Loading branch information
sidke and crsven committed Nov 13, 2020
1 parent e9f37ec commit 34be074
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 61 deletions.
93 changes: 32 additions & 61 deletions ts/quill/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,82 +41,53 @@ export const isInsertMentionOp = (op: Op): op is InsertMentionOp =>
isSpecificInsertOp(op, 'mention');

export const getTextFromOps = (ops: Array<DeltaOperation>): string =>
ops.reduce((acc, { insert }, index) => {
if (typeof insert === 'string') {
let textToAdd;
switch (index) {
case 0: {
textToAdd = insert.trimLeft();
break;
}
case ops.length - 1: {
textToAdd = insert.trimRight();
break;
}
default: {
textToAdd = insert;
break;
}
ops
.reduce((acc, op) => {
if (typeof op.insert === 'string') {
return acc + op.insert;
}
const textWithoutNewlines = textToAdd.replace(/\n+$/, '');
return acc + textWithoutNewlines;
}

if (insert.emoji) {
return acc + insert.emoji;
}
if (isInsertEmojiOp(op)) {
return acc + op.insert.emoji;
}

if (insert.mention) {
return `${acc}@${insert.mention.title}`;
}
if (isInsertMentionOp(op)) {
return `${acc}@${op.insert.mention.title}`;
}

return acc;
}, '');
return acc;
}, '')
.trim();

export const getTextAndMentionsFromOps = (
ops: Array<Op>
): [string, Array<BodyRangeType>] => {
const mentions: Array<BodyRangeType> = [];

const text = ops.reduce((acc, op, index) => {
if (typeof op.insert === 'string') {
let textToAdd;
switch (index) {
case 0: {
textToAdd = op.insert.trimLeft();
break;
}
case ops.length - 1: {
textToAdd = op.insert.trimRight();
break;
}
default: {
textToAdd = op.insert;
break;
}
const text = ops
.reduce((acc, op) => {
if (typeof op.insert === 'string') {
return acc + op.insert;
}

const textWithoutNewlines = textToAdd.replace(/\n+$/, '');
return acc + textWithoutNewlines;
}

if (isInsertEmojiOp(op)) {
return acc + op.insert.emoji;
}
if (isInsertEmojiOp(op)) {
return acc + op.insert.emoji;
}

if (isInsertMentionOp(op)) {
mentions.push({
length: 1, // The length of `\uFFFC`
mentionUuid: op.insert.mention.uuid,
replacementText: op.insert.mention.title,
start: acc.length,
});
if (isInsertMentionOp(op)) {
mentions.push({
length: 1, // The length of `\uFFFC`
mentionUuid: op.insert.mention.uuid,
replacementText: op.insert.mention.title,
start: acc.length,
});

return `${acc}\uFFFC`;
}
return `${acc}\uFFFC`;
}

return acc;
}, '');
return acc;
}, '')
.trim();

return [text, mentions];
};
Expand Down
25 changes: 25 additions & 0 deletions ts/test/quill/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ describe('getTextAndMentionsFromOps', () => {
},
]);
});

it('does not trim newlines padding mentions', () => {
const ops = [
{ insert: 'test \n' },
{
insert: {
mention: {
uuid: 'abcdef',
title: '@fred',
},
},
},
{ insert: '\n test' },
];
const [resultText, resultMentions] = getTextAndMentionsFromOps(ops);
assert.equal(resultText, 'test \n\uFFFC\n test');
assert.deepEqual(resultMentions, [
{
length: 1,
mentionUuid: 'abcdef',
replacementText: '@fred',
start: 6,
},
]);
});
});
});

Expand Down

0 comments on commit 34be074

Please sign in to comment.