2021-01-14 18:07:05 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
2020-11-03 01:19:52 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
import _ from 'lodash';
|
2020-11-03 01:19:52 +00:00
|
|
|
import Quill from 'quill';
|
|
|
|
import Delta from 'quill-delta';
|
|
|
|
import React, { RefObject } from 'react';
|
|
|
|
|
|
|
|
import { Popper } from 'react-popper';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { createPortal } from 'react-dom';
|
|
|
|
import { ConversationType } from '../../state/ducks/conversations';
|
|
|
|
import { Avatar } from '../../components/Avatar';
|
|
|
|
import { LocalizerType } from '../../types/Util';
|
2020-11-04 22:04:48 +00:00
|
|
|
import { MemberRepository } from '../memberRepository';
|
2020-11-05 21:18:42 +00:00
|
|
|
import { matchBlotTextPartitions } from '../util';
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type MentionCompletionOptions = {
|
2020-11-03 01:19:52 +00:00
|
|
|
i18n: LocalizerType;
|
|
|
|
memberRepositoryRef: RefObject<MemberRepository>;
|
|
|
|
setMentionPickerElement: (element: JSX.Element | null) => void;
|
|
|
|
me?: ConversationType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
declare global {
|
2021-01-14 18:07:05 +00:00
|
|
|
// We want to extend `HTMLElement`'s properties, so we need an interface.
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-11-03 01:19:52 +00:00
|
|
|
interface HTMLElement {
|
|
|
|
// Webkit-specific
|
|
|
|
scrollIntoViewIfNeeded: (bringToCenter: boolean) => void;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MENTION_REGEX = /(?:^|\W)@([-+\w]*)$/;
|
|
|
|
|
|
|
|
export class MentionCompletion {
|
|
|
|
results: Array<ConversationType>;
|
|
|
|
|
|
|
|
index: number;
|
|
|
|
|
|
|
|
root: HTMLDivElement;
|
|
|
|
|
|
|
|
quill: Quill;
|
|
|
|
|
|
|
|
options: MentionCompletionOptions;
|
|
|
|
|
|
|
|
suggestionListRef: RefObject<HTMLDivElement>;
|
|
|
|
|
|
|
|
constructor(quill: Quill, options: MentionCompletionOptions) {
|
|
|
|
this.results = [];
|
|
|
|
this.index = 0;
|
|
|
|
this.options = options;
|
|
|
|
this.root = document.body.appendChild(document.createElement('div'));
|
|
|
|
this.quill = quill;
|
|
|
|
this.suggestionListRef = React.createRef<HTMLDivElement>();
|
|
|
|
|
|
|
|
const clearResults = () => {
|
|
|
|
if (this.results.length) {
|
2020-11-05 21:18:42 +00:00
|
|
|
this.clearResults();
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const changeIndex = (by: number) => (): boolean => {
|
|
|
|
if (this.results.length) {
|
|
|
|
this.changeIndex(by);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.quill.keyboard.addBinding({ key: 37 }, clearResults); // Left Arrow
|
|
|
|
this.quill.keyboard.addBinding({ key: 38 }, changeIndex(-1)); // Up Arrow
|
|
|
|
this.quill.keyboard.addBinding({ key: 39 }, clearResults); // Right Arrow
|
|
|
|
this.quill.keyboard.addBinding({ key: 40 }, changeIndex(1)); // Down Arrow
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
this.quill.on('text-change', _.debounce(this.onTextChange.bind(this), 0));
|
2020-11-03 01:19:52 +00:00
|
|
|
this.quill.on('selection-change', this.onSelectionChange.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
this.root.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
changeIndex(by: number): void {
|
|
|
|
this.index = (this.index + by + this.results.length) % this.results.length;
|
|
|
|
this.render();
|
|
|
|
const suggestionList = this.suggestionListRef.current;
|
|
|
|
if (suggestionList) {
|
|
|
|
const selectedElement = suggestionList.querySelector<HTMLElement>(
|
|
|
|
'[aria-selected="true"]'
|
|
|
|
);
|
|
|
|
if (selectedElement) {
|
|
|
|
selectedElement.scrollIntoViewIfNeeded(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectionChange(): void {
|
|
|
|
// Selection should never change while we're editing a mention
|
2020-11-05 21:18:42 +00:00
|
|
|
this.clearResults();
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
possiblyShowMemberResults(): Array<ConversationType> {
|
2020-11-03 01:19:52 +00:00
|
|
|
const range = this.quill.getSelection();
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
if (range) {
|
|
|
|
const [blot, index] = this.quill.getLeaf(range.index);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const [leftTokenTextMatch] = matchBlotTextPartitions(
|
|
|
|
blot,
|
|
|
|
index,
|
|
|
|
MENTION_REGEX
|
|
|
|
);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
if (leftTokenTextMatch) {
|
|
|
|
const [, leftTokenText] = leftTokenTextMatch;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
let results: Array<ConversationType> = [];
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const memberRepository = this.options.memberRepositoryRef.current;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
if (memberRepository) {
|
|
|
|
if (leftTokenText === '') {
|
|
|
|
results = memberRepository.getMembers(this.options.me);
|
|
|
|
} else {
|
|
|
|
const fullMentionText = leftTokenText;
|
|
|
|
results = memberRepository.search(fullMentionText, this.options.me);
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
return results;
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
return [];
|
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
onTextChange(): void {
|
|
|
|
const showMemberResults = this.possiblyShowMemberResults();
|
|
|
|
|
|
|
|
if (showMemberResults.length > 0) {
|
|
|
|
this.results = showMemberResults;
|
|
|
|
this.index = 0;
|
|
|
|
this.render();
|
|
|
|
} else if (this.results.length !== 0) {
|
|
|
|
this.clearResults();
|
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
completeMention(resultIndexArg?: number): void {
|
|
|
|
const resultIndex = resultIndexArg || this.index;
|
|
|
|
|
2020-11-03 01:19:52 +00:00
|
|
|
const range = this.quill.getSelection();
|
|
|
|
|
|
|
|
if (range === null) return;
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const member = this.results[resultIndex];
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const [blot, index] = this.quill.getLeaf(range.index);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
const [leftTokenTextMatch] = matchBlotTextPartitions(
|
|
|
|
blot,
|
|
|
|
index,
|
|
|
|
MENTION_REGEX
|
|
|
|
);
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
if (leftTokenTextMatch) {
|
|
|
|
const [, leftTokenText] = leftTokenTextMatch;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
this.insertMention(
|
|
|
|
member,
|
|
|
|
range.index - leftTokenText.length - 1,
|
|
|
|
leftTokenText.length + 1,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
insertMention(
|
2020-11-05 21:18:42 +00:00
|
|
|
mention: ConversationType,
|
2020-11-03 01:19:52 +00:00
|
|
|
index: number,
|
|
|
|
range: number,
|
|
|
|
withTrailingSpace = false
|
|
|
|
): void {
|
2020-11-18 15:15:42 +00:00
|
|
|
const delta = new Delta().retain(index).delete(range).insert({ mention });
|
2020-11-03 01:19:52 +00:00
|
|
|
|
|
|
|
if (withTrailingSpace) {
|
|
|
|
this.quill.updateContents(delta.insert(' '), 'user');
|
|
|
|
this.quill.setSelection(index + 2, 0, 'user');
|
|
|
|
} else {
|
|
|
|
this.quill.updateContents(delta, 'user');
|
|
|
|
this.quill.setSelection(index + 1, 0, 'user');
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
this.clearResults();
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
clearResults(): void {
|
|
|
|
this.results = [];
|
|
|
|
this.index = 0;
|
2020-11-03 01:19:52 +00:00
|
|
|
|
2020-11-05 21:18:42 +00:00
|
|
|
this.render();
|
2020-11-03 01:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onUnmount(): void {
|
|
|
|
document.body.removeChild(this.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): void {
|
|
|
|
const { results: memberResults, index: memberResultsIndex } = this;
|
|
|
|
|
|
|
|
if (memberResults.length === 0) {
|
|
|
|
this.options.setMentionPickerElement(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const element = createPortal(
|
|
|
|
<Popper
|
|
|
|
placement="top"
|
|
|
|
modifiers={{
|
|
|
|
width: {
|
|
|
|
enabled: true,
|
|
|
|
fn: oldData => {
|
|
|
|
const data = oldData;
|
|
|
|
const { width, left } = data.offsets.reference;
|
|
|
|
|
|
|
|
data.styles.width = `${width}px`;
|
|
|
|
data.offsets.popper.width = width;
|
|
|
|
data.offsets.popper.left = left;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
order: 840,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{({ ref, style }) => (
|
|
|
|
<div
|
|
|
|
ref={ref}
|
|
|
|
className="module-composition-input__suggestions"
|
|
|
|
style={style}
|
|
|
|
role="listbox"
|
|
|
|
aria-expanded
|
|
|
|
aria-activedescendant={`mention-result--${
|
|
|
|
memberResults.length ? memberResults[memberResultsIndex].name : ''
|
|
|
|
}`}
|
|
|
|
tabIndex={0}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
ref={this.suggestionListRef}
|
|
|
|
className="module-composition-input__suggestions--scroller"
|
|
|
|
>
|
|
|
|
{memberResults.map((member, index) => (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
key={member.uuid}
|
|
|
|
id={`mention-result--${member.name}`}
|
|
|
|
role="option button"
|
|
|
|
aria-selected={memberResultsIndex === index}
|
|
|
|
onClick={() => {
|
2020-11-05 21:18:42 +00:00
|
|
|
this.completeMention(index);
|
2020-11-03 01:19:52 +00:00
|
|
|
}}
|
|
|
|
className={classNames(
|
|
|
|
'module-composition-input__suggestions__row',
|
|
|
|
'module-composition-input__suggestions__row--mention',
|
|
|
|
memberResultsIndex === index
|
|
|
|
? 'module-composition-input__suggestions__row--selected'
|
|
|
|
: null
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Avatar
|
|
|
|
avatarPath={member.avatarPath}
|
|
|
|
conversationType="direct"
|
|
|
|
i18n={this.options.i18n}
|
|
|
|
size={28}
|
|
|
|
title={member.title}
|
|
|
|
/>
|
|
|
|
<div className="module-composition-input__suggestions__title">
|
|
|
|
{member.title}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Popper>,
|
|
|
|
this.root
|
|
|
|
);
|
|
|
|
|
|
|
|
this.options.setMentionPickerElement(element);
|
|
|
|
}
|
|
|
|
}
|