Apply existing formatting to pasted content, preserve whitespace

This commit is contained in:
Scott Nonnenberg 2023-08-04 09:29:47 -07:00 committed by GitHub
parent f597f15faf
commit e3ffc70389
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 191 additions and 53 deletions

View file

@ -3,7 +3,7 @@
import emojiRegex from 'emoji-regex';
import Delta from 'quill-delta';
import type { LeafBlot, DeltaOperation } from 'quill';
import type { LeafBlot, DeltaOperation, AttributeMap } from 'quill';
import type Op from 'quill-delta/dist/Op';
import type {
@ -387,7 +387,10 @@ export const insertMentionOps = (
return ops;
};
export const insertEmojiOps = (incomingOps: ReadonlyArray<Op>): Array<Op> => {
export const insertEmojiOps = (
incomingOps: ReadonlyArray<Op>,
existingAttributes: AttributeMap
): Array<Op> => {
return incomingOps.reduce((ops, op) => {
if (typeof op.insert === 'string') {
const text = op.insert;
@ -400,7 +403,10 @@ export const insertEmojiOps = (incomingOps: ReadonlyArray<Op>): Array<Op> => {
while ((match = re.exec(text))) {
const [emoji] = match;
ops.push({ insert: text.slice(index, match.index), attributes });
ops.push({ insert: { emoji }, attributes });
ops.push({
insert: { emoji },
attributes: { ...existingAttributes, ...attributes },
});
index = match.index + emoji.length;
}