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';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { RefObject } from 'react';
|
2023-08-04 16:29:47 +00:00
|
|
|
import type { Matcher, AttributeMap } from 'quill';
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
import { assertDev } from '../../util/assert';
|
|
|
|
import { isAciString } from '../../types/ServiceId';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MemberRepository } from '../memberRepository';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
export const matchMention: (
|
|
|
|
memberRepositoryRef: RefObject<MemberRepository>
|
|
|
|
) => Matcher =
|
2021-11-11 22:43:05 +00:00
|
|
|
(memberRepositoryRef: RefObject<MemberRepository>) =>
|
2023-08-04 16:29:47 +00:00
|
|
|
(node: HTMLElement, delta: Delta, attributes: AttributeMap): Delta => {
|
2021-11-11 22:43:05 +00:00
|
|
|
const memberRepository = memberRepositoryRef.current;
|
|
|
|
|
|
|
|
if (memberRepository) {
|
|
|
|
const { title } = node.dataset;
|
|
|
|
|
2023-05-23 22:00:05 +00:00
|
|
|
if (node.classList.contains('MessageBody__at-mention')) {
|
2021-11-11 22:43:05 +00:00
|
|
|
const { id } = node.dataset;
|
2023-08-17 18:48:42 +00:00
|
|
|
const member = memberRepository.getMemberById(id);
|
2021-11-11 22:43:05 +00:00
|
|
|
|
2023-08-17 18:48:42 +00:00
|
|
|
if (member && member.aci) {
|
|
|
|
const { aci } = member;
|
2023-08-04 16:29:47 +00:00
|
|
|
return new Delta().insert(
|
|
|
|
{
|
|
|
|
mention: {
|
|
|
|
title,
|
2023-08-16 20:54:39 +00:00
|
|
|
aci,
|
2023-08-04 16:29:47 +00:00
|
|
|
},
|
2021-11-11 22:43:05 +00:00
|
|
|
},
|
2023-08-04 16:29:47 +00:00
|
|
|
attributes
|
|
|
|
);
|
2021-11-11 22:43:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
return new Delta().insert(`@${title}`, attributes);
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
if (node.classList.contains('mention-blot')) {
|
2023-08-16 20:54:39 +00:00
|
|
|
const { aci } = node.dataset;
|
|
|
|
assertDev(isAciString(aci), 'Mentioned blot has invalid ACI');
|
2023-08-17 18:48:42 +00:00
|
|
|
const member = memberRepository.getMemberByAci(aci);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2023-08-17 18:48:42 +00:00
|
|
|
if (member && member.aci) {
|
|
|
|
assertDev(member.aci === aci, 'Mentioned member has no ACI');
|
2023-08-04 16:29:47 +00:00
|
|
|
return new Delta().insert(
|
|
|
|
{
|
|
|
|
mention: {
|
2023-08-17 18:48:42 +00:00
|
|
|
title: title || member.title,
|
2023-08-16 20:54:39 +00:00
|
|
|
aci,
|
2023-08-04 16:29:47 +00:00
|
|
|
},
|
2021-11-11 22:43:05 +00:00
|
|
|
},
|
2023-08-04 16:29:47 +00:00
|
|
|
attributes
|
|
|
|
);
|
2021-11-11 22:43:05 +00:00
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2023-08-04 16:29:47 +00:00
|
|
|
return new Delta().insert(`@${title}`, attributes);
|
2021-11-11 22:43:05 +00:00
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
return delta;
|
|
|
|
};
|