2020-10-30 15:34:04 -05:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-11-09 20:59:36 -08:00
|
|
|
import type { DraftBodyRangeType, DraftBodyRangesType } from '../types/Util';
|
2020-09-18 17:43:57 -04:00
|
|
|
|
|
|
|
export function getTextWithMentions(
|
2022-11-09 20:59:36 -08:00
|
|
|
bodyRanges: DraftBodyRangesType,
|
2020-09-18 17:43:57 -04:00
|
|
|
text: string
|
|
|
|
): string {
|
2022-11-09 20:59:36 -08:00
|
|
|
const sortableBodyRanges: Array<DraftBodyRangeType> = bodyRanges.slice();
|
|
|
|
return sortableBodyRanges
|
2020-11-02 17:19:52 -08:00
|
|
|
.sort((a, b) => b.start - a.start)
|
|
|
|
.reduce((acc, { start, length, replacementText }) => {
|
|
|
|
const left = acc.slice(0, start);
|
|
|
|
const right = acc.slice(start + length);
|
|
|
|
return `${left}@${replacementText}${right}`;
|
|
|
|
}, text);
|
2020-09-18 17:43:57 -04:00
|
|
|
}
|