signal-desktop/ts/quill/mentions/matchers.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-11-03 01:19:52 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import Delta from 'quill-delta';
import { RefObject } from 'react';
2020-11-04 22:04:48 +00:00
import { MemberRepository } from '../memberRepository';
2020-11-03 01:19:52 +00:00
export const matchMention = (
memberRepositoryRef: RefObject<MemberRepository>
) => (node: HTMLElement, delta: Delta): Delta => {
const memberRepository = memberRepositoryRef.current;
if (memberRepository) {
const { title } = node.dataset;
2021-10-20 20:46:42 +00:00
if (node.classList.contains('MessageBody__at-mention')) {
2020-11-03 01:19:52 +00:00
const { id } = node.dataset;
const conversation = memberRepository.getMemberById(id);
if (conversation && conversation.uuid) {
return new Delta().insert({
mention: {
title,
uuid: conversation.uuid,
},
});
}
return new Delta().insert(`@${title}`);
}
if (node.classList.contains('mention-blot')) {
const { uuid } = node.dataset;
const conversation = memberRepository.getMemberByUuid(uuid);
if (conversation && conversation.uuid) {
return new Delta().insert({
mention: {
title: title || conversation.title,
uuid: conversation.uuid,
},
});
}
return new Delta().insert(`@${title}`);
}
}
return delta;
};