2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-11-03 01:19:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
import emojiRegex from 'emoji-regex';
|
2020-11-03 01:19:52 +00:00
|
|
|
import Delta from 'quill-delta';
|
2023-08-04 16:29:47 +00:00
|
|
|
import type { LeafBlot, DeltaOperation, AttributeMap } from 'quill';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type Op from 'quill-delta/dist/Op';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
import type {
|
|
|
|
DisplayNode,
|
|
|
|
DraftBodyRange,
|
|
|
|
DraftBodyRanges,
|
|
|
|
} from '../types/BodyRange';
|
2023-04-10 16:31:45 +00:00
|
|
|
import { BodyRange } from '../types/BodyRange';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MentionBlot } from './mentions/blot';
|
2023-12-18 23:22:46 +00:00
|
|
|
import type { EmojiBlot } from './emoji/blot';
|
2023-06-08 21:50:44 +00:00
|
|
|
import { isNewlineOnlyOp, QuillFormattingStyle } from './formatting/menu';
|
2023-05-10 01:23:56 +00:00
|
|
|
import { isNotNil } from '../util/isNotNil';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { AciString } from '../types/ServiceId';
|
2023-08-28 16:31:24 +00:00
|
|
|
import { emojiToData } from '../components/emoji/lib';
|
2020-11-05 21:18:42 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type MentionBlotValue = {
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: AciString;
|
2020-11-05 21:18:42 +00:00
|
|
|
title: string;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-05 21:18:42 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
export type FormattingBlotValue = {
|
|
|
|
style: BodyRange.Style;
|
|
|
|
};
|
|
|
|
|
2023-12-18 23:22:46 +00:00
|
|
|
export const isEmojiBlot = (blot: LeafBlot): blot is EmojiBlot =>
|
|
|
|
blot.value() && blot.value().emoji;
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
export const isMentionBlot = (blot: LeafBlot): blot is MentionBlot =>
|
|
|
|
blot.value() && blot.value().mention;
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
export const isFormatting = (blot: LeafBlot): blot is MentionBlot =>
|
|
|
|
blot.value() && blot.value().style;
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
export type RetainOp = Op & { retain: number };
|
|
|
|
export type InsertOp<K extends string, T> = Op & { insert: { [V in K]: T } };
|
|
|
|
|
|
|
|
export type InsertMentionOp = InsertOp<'mention', MentionBlotValue>;
|
2023-12-18 23:22:46 +00:00
|
|
|
export type InsertEmojiOp = InsertOp<
|
|
|
|
'emoji',
|
|
|
|
{ value: string; source?: string }
|
|
|
|
>;
|
2020-11-05 21:18:42 +00:00
|
|
|
|
|
|
|
export const isRetainOp = (op?: Op): op is RetainOp =>
|
|
|
|
op !== undefined && op.retain !== undefined;
|
|
|
|
|
|
|
|
export const isSpecificInsertOp = (op: Op, type: string): boolean => {
|
|
|
|
return (
|
|
|
|
op.insert !== undefined &&
|
|
|
|
typeof op.insert === 'object' &&
|
|
|
|
Object.hasOwnProperty.call(op.insert, type)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isInsertEmojiOp = (op: Op): op is InsertEmojiOp =>
|
|
|
|
isSpecificInsertOp(op, 'emoji');
|
|
|
|
|
|
|
|
export const isInsertMentionOp = (op: Op): op is InsertMentionOp =>
|
|
|
|
isSpecificInsertOp(op, 'mention');
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-06 20:11:18 +00:00
|
|
|
export const getTextFromOps = (ops: Array<DeltaOperation>): string =>
|
2020-11-13 19:54:11 +00:00
|
|
|
ops
|
|
|
|
.reduce((acc, op) => {
|
|
|
|
if (typeof op.insert === 'string') {
|
|
|
|
return acc + op.insert;
|
2020-11-06 20:11:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
if (isInsertEmojiOp(op)) {
|
2023-12-18 23:22:46 +00:00
|
|
|
return acc + op.insert.emoji.value;
|
2020-11-13 19:54:11 +00:00
|
|
|
}
|
2020-11-06 20:11:18 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
if (isInsertMentionOp(op)) {
|
|
|
|
return `${acc}@${op.insert.mention.title}`;
|
|
|
|
}
|
2020-11-06 20:11:18 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
return acc;
|
|
|
|
}, '')
|
|
|
|
.trim();
|
2020-11-06 20:11:18 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
const { BOLD, ITALIC, MONOSPACE, SPOILER, STRIKETHROUGH, NONE } =
|
|
|
|
BodyRange.Style;
|
|
|
|
|
|
|
|
function extractFormatRange({
|
|
|
|
bodyRanges,
|
|
|
|
index,
|
|
|
|
previousData,
|
|
|
|
hasStyle,
|
|
|
|
style,
|
|
|
|
}: {
|
|
|
|
bodyRanges: Array<DraftBodyRange>;
|
|
|
|
index: number;
|
|
|
|
previousData: { start: number } | undefined;
|
|
|
|
hasStyle: boolean;
|
|
|
|
style: BodyRange.Style;
|
|
|
|
}) {
|
|
|
|
if (hasStyle && !previousData) {
|
|
|
|
return { start: index };
|
|
|
|
}
|
|
|
|
if (!hasStyle && previousData) {
|
|
|
|
const { start } = previousData;
|
|
|
|
bodyRanges.push({
|
|
|
|
length: index - start,
|
|
|
|
start,
|
|
|
|
style,
|
|
|
|
});
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return previousData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractAllFormats(
|
|
|
|
bodyRanges: Array<DraftBodyRange>,
|
|
|
|
formats: Record<BodyRange.Style, { start: number } | undefined>,
|
|
|
|
index: number,
|
|
|
|
op?: Op
|
|
|
|
): Record<BodyRange.Style, { start: number } | undefined> {
|
|
|
|
const result = { ...formats };
|
|
|
|
const params = {
|
|
|
|
bodyRanges,
|
|
|
|
index,
|
|
|
|
};
|
|
|
|
|
|
|
|
result[BOLD] = extractFormatRange({
|
|
|
|
...params,
|
|
|
|
style: BOLD,
|
|
|
|
previousData: result[BOLD],
|
|
|
|
hasStyle: op?.attributes?.[QuillFormattingStyle.bold],
|
|
|
|
});
|
|
|
|
result[ITALIC] = extractFormatRange({
|
|
|
|
...params,
|
|
|
|
style: ITALIC,
|
|
|
|
previousData: result[ITALIC],
|
|
|
|
hasStyle: op?.attributes?.[QuillFormattingStyle.italic],
|
|
|
|
});
|
|
|
|
result[MONOSPACE] = extractFormatRange({
|
|
|
|
...params,
|
|
|
|
style: MONOSPACE,
|
|
|
|
previousData: result[MONOSPACE],
|
|
|
|
hasStyle: op?.attributes?.[QuillFormattingStyle.monospace],
|
|
|
|
});
|
|
|
|
result[SPOILER] = extractFormatRange({
|
|
|
|
...params,
|
|
|
|
style: SPOILER,
|
|
|
|
previousData: result[SPOILER],
|
|
|
|
hasStyle: op?.attributes?.[QuillFormattingStyle.spoiler],
|
|
|
|
});
|
|
|
|
result[STRIKETHROUGH] = extractFormatRange({
|
|
|
|
...params,
|
|
|
|
style: STRIKETHROUGH,
|
|
|
|
previousData: result[STRIKETHROUGH],
|
|
|
|
hasStyle: op?.attributes?.[QuillFormattingStyle.strike],
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getTextAndRangesFromOps = (
|
2020-11-05 21:18:42 +00:00
|
|
|
ops: Array<Op>
|
2023-04-14 18:16:28 +00:00
|
|
|
): { text: string; bodyRanges: DraftBodyRanges } => {
|
2023-05-10 01:23:56 +00:00
|
|
|
const startingBodyRanges: Array<DraftBodyRange> = [];
|
2023-11-10 23:10:39 +00:00
|
|
|
let earliestMonospaceIndex = Number.MAX_SAFE_INTEGER;
|
2023-04-14 18:16:28 +00:00
|
|
|
let formats: Record<BodyRange.Style, { start: number } | undefined> = {
|
|
|
|
[BOLD]: undefined,
|
|
|
|
[ITALIC]: undefined,
|
|
|
|
[MONOSPACE]: undefined,
|
|
|
|
[SPOILER]: undefined,
|
|
|
|
[STRIKETHROUGH]: undefined,
|
|
|
|
[NONE]: undefined,
|
|
|
|
};
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
const preTrimText = ops.reduce((acc, op) => {
|
2023-06-08 21:50:44 +00:00
|
|
|
// We special-case all-newline ops because Quill doesn't apply styles to them
|
|
|
|
if (isNewlineOnlyOp(op)) {
|
2023-05-12 20:48:14 +00:00
|
|
|
return acc + op.insert;
|
|
|
|
}
|
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
// Start or finish format sections as needed
|
|
|
|
formats = extractAllFormats(startingBodyRanges, formats, acc.length, op);
|
2023-04-14 18:16:28 +00:00
|
|
|
|
2023-11-10 23:10:39 +00:00
|
|
|
const newMonospaceStart =
|
|
|
|
formats[MONOSPACE]?.start ?? earliestMonospaceIndex;
|
|
|
|
if (newMonospaceStart < earliestMonospaceIndex) {
|
|
|
|
earliestMonospaceIndex = newMonospaceStart;
|
|
|
|
}
|
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
if (typeof op.insert === 'string') {
|
|
|
|
return acc + op.insert;
|
|
|
|
}
|
2020-11-11 22:56:20 +00:00
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
if (isInsertEmojiOp(op)) {
|
2023-12-18 23:22:46 +00:00
|
|
|
return acc + op.insert.emoji.value;
|
2023-05-10 01:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isInsertMentionOp(op)) {
|
|
|
|
startingBodyRanges.push({
|
|
|
|
length: 1, // The length of `\uFFFC`
|
2023-08-16 20:54:39 +00:00
|
|
|
mentionAci: op.insert.mention.aci,
|
2023-05-10 01:23:56 +00:00
|
|
|
replacementText: op.insert.mention.title,
|
|
|
|
start: acc.length,
|
|
|
|
});
|
|
|
|
|
|
|
|
return `${acc}\uFFFC`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, '');
|
|
|
|
|
|
|
|
// Close off any pending formats
|
|
|
|
extractAllFormats(startingBodyRanges, formats, preTrimText.length);
|
|
|
|
|
|
|
|
// Now repair bodyRanges after trimming
|
2023-11-10 23:10:39 +00:00
|
|
|
let trimStart = preTrimText.trimStart();
|
|
|
|
let trimmedFromStart = preTrimText.length - trimStart.length;
|
|
|
|
|
|
|
|
// We don't want to trim leading monospace text
|
|
|
|
if (earliestMonospaceIndex < trimmedFromStart) {
|
|
|
|
trimStart = preTrimText.slice(earliestMonospaceIndex);
|
|
|
|
trimmedFromStart = earliestMonospaceIndex;
|
|
|
|
}
|
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
const text = trimStart.trimEnd();
|
|
|
|
const textLength = text.length;
|
|
|
|
|
|
|
|
const bodyRanges = startingBodyRanges
|
|
|
|
.map(startingRange => {
|
|
|
|
let range = {
|
|
|
|
...startingRange,
|
|
|
|
start: startingRange.start - trimmedFromStart,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (range.start >= text.length) {
|
|
|
|
return null;
|
2020-11-13 19:54:11 +00:00
|
|
|
}
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
const underStartBy = -range.start;
|
|
|
|
if (underStartBy > 0) {
|
|
|
|
const length = range.length - underStartBy;
|
|
|
|
if (length <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
range = {
|
|
|
|
...range,
|
|
|
|
start: 0,
|
|
|
|
length,
|
|
|
|
};
|
2020-11-13 19:54:11 +00:00
|
|
|
}
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
const end = range.start + range.length;
|
|
|
|
const overEndBy = end - textLength;
|
|
|
|
if (overEndBy > 0) {
|
|
|
|
range = {
|
|
|
|
...range,
|
|
|
|
length: range.length - overEndBy,
|
|
|
|
};
|
|
|
|
}
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2023-05-10 01:23:56 +00:00
|
|
|
return range;
|
|
|
|
})
|
|
|
|
.filter(isNotNil);
|
2023-04-14 18:16:28 +00:00
|
|
|
|
|
|
|
return { text, bodyRanges };
|
2020-11-04 02:04:22 +00:00
|
|
|
};
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
export const getBlotTextPartitions = (
|
2021-09-17 16:21:33 +00:00
|
|
|
blotText: string | undefined,
|
2020-11-05 21:18:42 +00:00
|
|
|
index: number
|
|
|
|
): [string, string] => {
|
2021-09-17 16:21:33 +00:00
|
|
|
const lowerCaseBlotText = (blotText || '').toLowerCase();
|
|
|
|
const leftLeafText = lowerCaseBlotText.substr(0, index);
|
|
|
|
const rightLeafText = lowerCaseBlotText.substr(index);
|
2020-11-05 21:18:42 +00:00
|
|
|
|
2021-09-17 16:21:33 +00:00
|
|
|
return [leftLeafText, rightLeafText];
|
2020-11-05 21:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const matchBlotTextPartitions = (
|
|
|
|
blot: LeafBlot,
|
|
|
|
index: number,
|
|
|
|
leftRegExp: RegExp,
|
|
|
|
rightRegExp?: RegExp
|
|
|
|
): Array<RegExpMatchArray | null> => {
|
2021-09-17 16:21:33 +00:00
|
|
|
const [leftText, rightText] = getBlotTextPartitions(blot.text, index);
|
2020-11-05 21:18:42 +00:00
|
|
|
|
|
|
|
const leftMatch = leftRegExp.exec(leftText);
|
|
|
|
let rightMatch = null;
|
|
|
|
|
|
|
|
if (rightRegExp) {
|
|
|
|
rightMatch = rightRegExp.exec(rightText);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [leftMatch, rightMatch];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getDeltaToRestartMention = (ops: Array<Op>): Delta => {
|
|
|
|
const changes = ops.reduce((acc, op): Array<Op> => {
|
|
|
|
if (op.insert && typeof op.insert === 'string') {
|
|
|
|
acc.push({ retain: op.insert.length });
|
|
|
|
} else {
|
|
|
|
acc.push({ retain: 1 });
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, Array<Op>());
|
|
|
|
changes.push({ delete: 1 });
|
|
|
|
changes.push({ insert: '@' });
|
|
|
|
return new Delta(changes);
|
|
|
|
};
|
|
|
|
|
2023-12-18 23:22:46 +00:00
|
|
|
export const getDeltaToRestartEmoji = (ops: Array<Op>): Delta => {
|
|
|
|
const changes = new Array<Op>();
|
|
|
|
for (const op of ops.slice(0, -1)) {
|
|
|
|
if (op.insert && typeof op.insert === 'string') {
|
|
|
|
changes.push({ retain: op.insert.length });
|
|
|
|
} else {
|
|
|
|
changes.push({ retain: 1 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const last = ops.at(-1);
|
|
|
|
if (!last || !last.insert) {
|
|
|
|
throw new Error('No emoji to delete');
|
|
|
|
}
|
|
|
|
|
|
|
|
changes.push({ delete: 1 });
|
|
|
|
if ((last as InsertEmojiOp).insert.emoji?.source) {
|
|
|
|
changes.push({ insert: (last as InsertEmojiOp).insert.emoji?.source });
|
|
|
|
}
|
|
|
|
return new Delta(changes);
|
|
|
|
};
|
|
|
|
|
2020-11-03 01:19:52 +00:00
|
|
|
export const getDeltaToRemoveStaleMentions = (
|
2020-11-05 21:18:42 +00:00
|
|
|
ops: Array<Op>,
|
2023-08-16 20:54:39 +00:00
|
|
|
memberAcis: Array<AciString>
|
2020-11-03 01:19:52 +00:00
|
|
|
): Delta => {
|
|
|
|
const newOps = ops.reduce((memo, op) => {
|
|
|
|
if (op.insert) {
|
2020-11-05 21:18:42 +00:00
|
|
|
if (
|
|
|
|
isInsertMentionOp(op) &&
|
2023-08-16 20:54:39 +00:00
|
|
|
!memberAcis.includes(op.insert.mention.aci)
|
2020-11-05 21:18:42 +00:00
|
|
|
) {
|
2020-11-03 01:19:52 +00:00
|
|
|
const deleteOp = { delete: 1 };
|
|
|
|
const textOp = { insert: `@${op.insert.mention.title}` };
|
|
|
|
return [...memo, deleteOp, textOp];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof op.insert === 'string') {
|
|
|
|
const retainStringOp = { retain: op.insert.length };
|
|
|
|
return [...memo, retainStringOp];
|
|
|
|
}
|
|
|
|
|
|
|
|
const retainEmbedOp = { retain: 1 };
|
|
|
|
return [...memo, retainEmbedOp];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...memo, op];
|
2020-11-05 21:18:42 +00:00
|
|
|
}, Array<Op>());
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
return new Delta(newOps);
|
|
|
|
};
|
2020-11-12 01:01:45 +00:00
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
export const insertFormattingAndMentionsOps = (
|
|
|
|
nodes: ReadonlyArray<DisplayNode>
|
|
|
|
): ReadonlyArray<Op> => {
|
|
|
|
let ops: Array<Op> = [];
|
|
|
|
|
|
|
|
nodes.forEach(node => {
|
|
|
|
const startingOp: Op = {
|
|
|
|
insert: node.text,
|
|
|
|
attributes: {
|
|
|
|
[QuillFormattingStyle.bold]: node.isBold,
|
|
|
|
[QuillFormattingStyle.italic]: node.isItalic,
|
|
|
|
[QuillFormattingStyle.monospace]: node.isMonospace,
|
|
|
|
[QuillFormattingStyle.spoiler]: node.isSpoiler,
|
|
|
|
[QuillFormattingStyle.strike]: node.isStrikethrough,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ops = ops.concat(insertMentionOps([startingOp], node.mentions));
|
|
|
|
});
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
};
|
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
export const insertMentionOps = (
|
|
|
|
incomingOps: Array<Op>,
|
2023-04-14 18:16:28 +00:00
|
|
|
bodyRanges: DraftBodyRanges
|
2020-11-12 01:01:45 +00:00
|
|
|
): Array<Op> => {
|
|
|
|
const ops = [...incomingOps];
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
const sortableBodyRanges: Array<DraftBodyRange> = bodyRanges.slice();
|
2022-11-10 04:59:36 +00:00
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
// Working backwards through bodyRanges (to avoid offsetting later mentions),
|
|
|
|
// Shift off the op with the text to the left of the last mention,
|
|
|
|
// Insert a mention based on the current bodyRange,
|
|
|
|
// Unshift the mention and surrounding text to leave the ops ready for the next range
|
2022-11-10 04:59:36 +00:00
|
|
|
sortableBodyRanges
|
2020-11-12 01:01:45 +00:00
|
|
|
.sort((a, b) => b.start - a.start)
|
2023-04-10 16:31:45 +00:00
|
|
|
.forEach(bodyRange => {
|
|
|
|
if (!BodyRange.isMention(bodyRange)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const { start, length, mentionAci, replacementText } = bodyRange;
|
2023-04-10 16:31:45 +00:00
|
|
|
|
2020-11-12 01:01:45 +00:00
|
|
|
const op = ops.shift();
|
|
|
|
|
|
|
|
if (op) {
|
2023-04-14 18:16:28 +00:00
|
|
|
const { insert, attributes } = op;
|
2020-11-12 01:01:45 +00:00
|
|
|
|
|
|
|
if (typeof insert === 'string') {
|
|
|
|
const left = insert.slice(0, start);
|
|
|
|
const right = insert.slice(start + length);
|
|
|
|
|
|
|
|
const mention = {
|
2023-08-16 20:54:39 +00:00
|
|
|
aci: mentionAci,
|
2020-11-12 01:01:45 +00:00
|
|
|
title: replacementText,
|
|
|
|
};
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
ops.unshift({ insert: right, attributes });
|
|
|
|
ops.unshift({ insert: { mention }, attributes });
|
|
|
|
ops.unshift({ insert: left, attributes });
|
2020-11-12 01:01:45 +00:00
|
|
|
} else {
|
|
|
|
ops.unshift(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
};
|
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
export const insertEmojiOps = (
|
|
|
|
incomingOps: ReadonlyArray<Op>,
|
|
|
|
existingAttributes: AttributeMap
|
|
|
|
): Array<Op> => {
|
2020-11-12 01:01:45 +00:00
|
|
|
return incomingOps.reduce((ops, op) => {
|
|
|
|
if (typeof op.insert === 'string') {
|
|
|
|
const text = op.insert;
|
2023-04-14 18:16:28 +00:00
|
|
|
const { attributes } = op;
|
2020-11-12 01:01:45 +00:00
|
|
|
const re = emojiRegex();
|
|
|
|
let index = 0;
|
|
|
|
let match: RegExpExecArray | null;
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-cond-assign
|
|
|
|
while ((match = re.exec(text))) {
|
|
|
|
const [emoji] = match;
|
2023-08-28 16:31:24 +00:00
|
|
|
const emojiData = emojiToData(emoji);
|
|
|
|
if (emojiData) {
|
|
|
|
ops.push({ insert: text.slice(index, match.index), attributes });
|
|
|
|
ops.push({
|
2023-12-18 23:22:46 +00:00
|
|
|
insert: { emoji: { value: emoji } },
|
2023-08-28 16:31:24 +00:00
|
|
|
attributes: { ...existingAttributes, ...attributes },
|
|
|
|
});
|
|
|
|
index = match.index + emoji.length;
|
|
|
|
}
|
2020-11-12 01:01:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 18:16:28 +00:00
|
|
|
ops.push({ insert: text.slice(index, text.length), attributes });
|
2020-11-12 01:01:45 +00:00
|
|
|
} else {
|
|
|
|
ops.push(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
}, [] as Array<Op>);
|
|
|
|
};
|