2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2020-2021 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';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LeafBlot, DeltaOperation } from 'quill';
|
|
|
|
import type Op from 'quill-delta/dist/Op';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2022-11-10 04:59:36 +00:00
|
|
|
import type { DraftBodyRangeType, DraftBodyRangesType } from '../types/Util';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MentionBlot } from './mentions/blot';
|
2020-11-05 21:18:42 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type MentionBlotValue = {
|
2020-11-05 21:18:42 +00:00
|
|
|
uuid: string;
|
|
|
|
title: string;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-05 21:18:42 +00:00
|
|
|
|
|
|
|
export const isMentionBlot = (blot: LeafBlot): blot is MentionBlot =>
|
|
|
|
blot.value() && blot.value().mention;
|
|
|
|
|
|
|
|
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>;
|
|
|
|
export type InsertEmojiOp = InsertOp<'emoji', string>;
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
return acc + op.insert.emoji;
|
|
|
|
}
|
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
|
|
|
|
2020-11-04 02:04:22 +00:00
|
|
|
export const getTextAndMentionsFromOps = (
|
2020-11-05 21:18:42 +00:00
|
|
|
ops: Array<Op>
|
2022-11-10 04:59:36 +00:00
|
|
|
): [string, DraftBodyRangesType] => {
|
|
|
|
const mentions: Array<DraftBodyRangeType> = [];
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
const text = ops
|
2022-03-09 19:27:49 +00:00
|
|
|
.reduce((acc, op, index) => {
|
2020-11-13 19:54:11 +00:00
|
|
|
if (typeof op.insert === 'string') {
|
2022-03-09 19:27:49 +00:00
|
|
|
const toAdd = index === 0 ? op.insert.trimStart() : op.insert;
|
|
|
|
return acc + toAdd;
|
2020-11-04 02:04:22 +00:00
|
|
|
}
|
2020-11-11 22:56:20 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
if (isInsertEmojiOp(op)) {
|
|
|
|
return acc + op.insert.emoji;
|
|
|
|
}
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
if (isInsertMentionOp(op)) {
|
|
|
|
mentions.push({
|
|
|
|
length: 1, // The length of `\uFFFC`
|
|
|
|
mentionUuid: op.insert.mention.uuid,
|
|
|
|
replacementText: op.insert.mention.title,
|
|
|
|
start: acc.length,
|
|
|
|
});
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
return `${acc}\uFFFC`;
|
|
|
|
}
|
2020-11-04 02:04:22 +00:00
|
|
|
|
2020-11-13 19:54:11 +00:00
|
|
|
return acc;
|
|
|
|
}, '')
|
2022-03-09 19:27:49 +00:00
|
|
|
.trimEnd(); // Trimming the start of this string will mess up mention indices
|
2020-11-04 02:04:22 +00:00
|
|
|
|
|
|
|
return [text, mentions];
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2020-11-03 01:19:52 +00:00
|
|
|
export const getDeltaToRemoveStaleMentions = (
|
2020-11-05 21:18:42 +00:00
|
|
|
ops: Array<Op>,
|
2020-11-03 01:19:52 +00:00
|
|
|
memberUuids: Array<string>
|
|
|
|
): Delta => {
|
|
|
|
const newOps = ops.reduce((memo, op) => {
|
|
|
|
if (op.insert) {
|
2020-11-05 21:18:42 +00:00
|
|
|
if (
|
|
|
|
isInsertMentionOp(op) &&
|
|
|
|
!memberUuids.includes(op.insert.mention.uuid)
|
|
|
|
) {
|
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
|
|
|
|
|
|
|
export const insertMentionOps = (
|
|
|
|
incomingOps: Array<Op>,
|
2022-11-10 04:59:36 +00:00
|
|
|
bodyRanges: DraftBodyRangesType
|
2020-11-12 01:01:45 +00:00
|
|
|
): Array<Op> => {
|
|
|
|
const ops = [...incomingOps];
|
|
|
|
|
2022-11-10 04:59:36 +00:00
|
|
|
const sortableBodyRanges: Array<DraftBodyRangeType> = bodyRanges.slice();
|
|
|
|
|
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)
|
|
|
|
.forEach(({ start, length, mentionUuid, replacementText }) => {
|
|
|
|
const op = ops.shift();
|
|
|
|
|
|
|
|
if (op) {
|
|
|
|
const { insert } = op;
|
|
|
|
|
|
|
|
if (typeof insert === 'string') {
|
|
|
|
const left = insert.slice(0, start);
|
|
|
|
const right = insert.slice(start + length);
|
|
|
|
|
|
|
|
const mention = {
|
|
|
|
uuid: mentionUuid,
|
|
|
|
title: replacementText,
|
|
|
|
};
|
|
|
|
|
|
|
|
ops.unshift({ insert: right });
|
|
|
|
ops.unshift({ insert: { mention } });
|
|
|
|
ops.unshift({ insert: left });
|
|
|
|
} else {
|
|
|
|
ops.unshift(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const insertEmojiOps = (incomingOps: Array<Op>): Array<Op> => {
|
|
|
|
return incomingOps.reduce((ops, op) => {
|
|
|
|
if (typeof op.insert === 'string') {
|
|
|
|
const text = op.insert;
|
|
|
|
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;
|
|
|
|
ops.push({ insert: text.slice(index, match.index) });
|
|
|
|
ops.push({ insert: { emoji } });
|
|
|
|
index = match.index + emoji.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops.push({ insert: text.slice(index, text.length) });
|
|
|
|
} else {
|
|
|
|
ops.push(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ops;
|
|
|
|
}, [] as Array<Op>);
|
|
|
|
};
|