2020-10-30 15:34:04 -05:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-10-21 09:53:32 -07:00
|
|
|
import Delta from 'quill-delta';
|
2023-08-04 09:29:47 -07:00
|
|
|
import type { Matcher, AttributeMap } from 'quill';
|
|
|
|
|
2020-11-11 17:01:45 -08:00
|
|
|
import { insertEmojiOps } from '../util';
|
2020-10-21 09:53:32 -07:00
|
|
|
|
2023-08-04 09:29:47 -07:00
|
|
|
export const matchEmojiImage: Matcher = (
|
|
|
|
node: Element,
|
|
|
|
delta: Delta,
|
|
|
|
attributes: AttributeMap
|
|
|
|
): Delta => {
|
2023-05-09 17:40:19 -07:00
|
|
|
if (
|
2023-05-23 15:00:05 -07:00
|
|
|
node.classList.contains('emoji') ||
|
|
|
|
node.classList.contains('module-emoji__image--16px')
|
2023-05-09 17:40:19 -07:00
|
|
|
) {
|
2023-12-18 15:22:46 -08:00
|
|
|
const value = node.getAttribute('aria-label');
|
|
|
|
return new Delta().insert({ emoji: { value } }, attributes);
|
2020-11-02 17:19:52 -08:00
|
|
|
}
|
2023-05-09 17:40:19 -07:00
|
|
|
return delta;
|
2020-11-02 17:19:52 -08:00
|
|
|
};
|
|
|
|
|
2023-08-04 09:29:47 -07:00
|
|
|
export const matchEmojiBlot: Matcher = (
|
|
|
|
node: HTMLElement,
|
|
|
|
delta: Delta,
|
|
|
|
attributes: AttributeMap
|
|
|
|
): Delta => {
|
2020-10-21 09:53:32 -07:00
|
|
|
if (node.classList.contains('emoji-blot')) {
|
2023-12-18 15:22:46 -08:00
|
|
|
const { emoji: value, source } = node.dataset;
|
|
|
|
return new Delta().insert({ emoji: { value, source } }, attributes);
|
2020-10-21 09:53:32 -07:00
|
|
|
}
|
|
|
|
return delta;
|
|
|
|
};
|
|
|
|
|
2023-08-04 09:29:47 -07:00
|
|
|
export const matchEmojiText: Matcher = (
|
|
|
|
node: HTMLElement,
|
|
|
|
_delta: Delta,
|
|
|
|
attributes: AttributeMap
|
|
|
|
): Delta => {
|
|
|
|
if (!('data' in node)) {
|
|
|
|
return new Delta();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { data } = node;
|
|
|
|
if (!data || typeof data !== 'string') {
|
|
|
|
return new Delta();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.replace(/(\n|\r\n)/g, '') === '') {
|
2023-05-11 12:48:34 -07:00
|
|
|
return new Delta();
|
|
|
|
}
|
|
|
|
|
2023-08-04 09:29:47 -07:00
|
|
|
const nodeAsInsert = { insert: data, attributes };
|
2020-11-11 17:01:45 -08:00
|
|
|
|
2023-08-04 09:29:47 -07:00
|
|
|
return new Delta(insertEmojiOps([nodeAsInsert], attributes));
|
2020-11-11 17:01:45 -08:00
|
|
|
};
|