From 34be0744d21b69bb197adc2e0fb147b97cb6c224 Mon Sep 17 00:00:00 2001 From: Sidney Keese Date: Fri, 13 Nov 2020 11:54:11 -0800 Subject: [PATCH] Trim the whole of a message's text Co-authored-by: Chris Svenningsen --- ts/quill/util.ts | 93 +++++++++++++------------------------- ts/test/quill/util_test.ts | 25 ++++++++++ 2 files changed, 57 insertions(+), 61 deletions(-) diff --git a/ts/quill/util.ts b/ts/quill/util.ts index fa4626ef5c..dbf73d1d8d 100644 --- a/ts/quill/util.ts +++ b/ts/quill/util.ts @@ -41,82 +41,53 @@ export const isInsertMentionOp = (op: Op): op is InsertMentionOp => isSpecificInsertOp(op, 'mention'); export const getTextFromOps = (ops: Array): 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 ): [string, Array] => { const mentions: Array = []; - 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]; }; diff --git a/ts/test/quill/util_test.ts b/ts/test/quill/util_test.ts index 64f6a71527..17326a8214 100644 --- a/ts/test/quill/util_test.ts +++ b/ts/test/quill/util_test.ts @@ -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, + }, + ]); + }); }); });