Composition area: Only paste HTML that originated in Signal
Co-authored-by: Chris Svenningsen <chris@carbonfive.com>
This commit is contained in:
parent
98da8746e8
commit
7af2284c6b
6 changed files with 148 additions and 77 deletions
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2020 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import emojiRegex from 'emoji-regex';
|
||||
import Delta from 'quill-delta';
|
||||
import { LeafBlot, DeltaOperation } from 'quill';
|
||||
import Op from 'quill-delta/dist/Op';
|
||||
|
@ -195,3 +196,67 @@ export const getDeltaToRemoveStaleMentions = (
|
|||
|
||||
return new Delta(newOps);
|
||||
};
|
||||
|
||||
export const insertMentionOps = (
|
||||
incomingOps: Array<Op>,
|
||||
bodyRanges: Array<BodyRangeType>
|
||||
): Array<Op> => {
|
||||
const ops = [...incomingOps];
|
||||
|
||||
// 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
|
||||
bodyRanges
|
||||
.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>);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue