51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
|
// Copyright 2020 Signal Messenger, LLC
|
||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
||
|
import Delta from 'quill-delta';
|
||
|
import { RefObject } from 'react';
|
||
|
import { MemberRepository } from '../util';
|
||
|
|
||
|
export const matchMention = (
|
||
|
memberRepositoryRef: RefObject<MemberRepository>
|
||
|
) => (node: HTMLElement, delta: Delta): Delta => {
|
||
|
const memberRepository = memberRepositoryRef.current;
|
||
|
|
||
|
if (memberRepository) {
|
||
|
const { title } = node.dataset;
|
||
|
|
||
|
if (node.classList.contains('module-message-body__at-mention')) {
|
||
|
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;
|
||
|
};
|