Files
ThreadNet-Web/src/autocomplete/Autocompleter.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
4.0 KiB
TypeScript
Raw Normal View History

2017-06-01 15:18:06 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2017-2024 New Vector Ltd.
2017-06-01 17:29:40 +01:00
Copyright 2016 Aviral Dasgupta
2017-06-01 15:18:06 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2017-06-01 15:18:06 +01:00
*/
2021-06-18 16:13:55 +01:00
import { ReactElement } from "react";
import { Room } from "matrix-js-sdk/src/matrix";
2021-06-18 16:13:55 +01:00
2016-06-01 16:54:21 +05:30
import CommandProvider from "./CommandProvider";
import RoomProvider from "./RoomProvider";
import UserProvider from "./UserProvider";
import EmojiProvider from "./EmojiProvider";
import NotifProvider from "./NotifProvider";
2021-06-18 16:13:55 +01:00
import { timeout } from "../utils/promise";
2021-06-29 13:11:58 +01:00
import AutocompleteProvider, { ICommand } from "./AutocompleteProvider";
import SpaceProvider from "./SpaceProvider";
import { TimelineRenderingType } from "../contexts/RoomContext";
import { filterBoolean } from "../utils/arrays";
2016-09-13 15:41:52 +05:30
2020-04-20 19:00:54 +01:00
export interface ISelectionRange {
beginning?: boolean; // whether the selection is in the first block of the editor or not
start: number; // byte offset relative to the start anchor of the current editor selection.
end: number; // byte offset relative to the end anchor of the current editor selection.
}
2016-09-13 15:41:52 +05:30
2020-04-20 19:00:54 +01:00
export interface ICompletion {
2022-11-30 11:32:56 +00:00
type?: "at-room" | "command" | "community" | "room" | "user";
2020-06-18 14:32:43 +01:00
completion: string;
2020-04-20 19:00:54 +01:00
completionId?: string;
component: ReactElement;
2020-06-18 14:32:43 +01:00
range: ISelectionRange;
command?: string;
2020-04-20 19:00:54 +01:00
suffix?: string;
2017-07-20 16:49:23 +01:00
// If provided, apply a LINK entity to the completion with the
// data = { url: href }.
2020-06-18 14:32:43 +01:00
href?: string;
2020-04-20 19:00:54 +01:00
}
2016-06-01 16:54:21 +05:30
const PROVIDERS = [UserProvider, RoomProvider, EmojiProvider, NotifProvider, CommandProvider, SpaceProvider];
2016-06-01 16:54:21 +05:30
2016-09-13 15:41:52 +05:30
// Providers will get rejected if they take longer than this.
const PROVIDER_COMPLETION_TIMEOUT = 3000;
2020-04-20 19:00:54 +01:00
export interface IProviderCompletions {
completions: ICompletion[];
provider: AutocompleteProvider;
command: Partial<ICommand>;
2020-04-20 19:00:54 +01:00
}
export default class Autocompleter {
public room: Room;
public providers: AutocompleteProvider[];
2020-04-20 19:00:54 +01:00
public constructor(room: Room, renderingType: TimelineRenderingType = TimelineRenderingType.Room) {
this.room = room;
2019-01-11 13:54:11 +00:00
this.providers = PROVIDERS.map((Prov) => {
return new Prov(room, renderingType);
});
}
2016-09-13 15:41:52 +05:30
public destroy(): void {
this.providers.forEach((p) => {
p.destroy();
});
}
2016-09-13 15:41:52 +05:30
public async getCompletions(
query: string,
selection: ISelectionRange,
force = false,
limit = -1,
): Promise<IProviderCompletions[]> {
2017-11-02 18:11:18 +00:00
/* Note: This intentionally waits for all providers to return,
otherwise, we run into a condition where new completions are displayed
while the user is interacting with the list, which makes it difficult
to predict whether an action will actually do what is intended
*/
2020-04-20 19:00:54 +01:00
// list of results from each provider, each being a list of completions or null if it times out
const completionsList: Array<ICompletion[] | null> = await Promise.all(
this.providers.map(async (provider): Promise<ICompletion[] | null> => {
return timeout(
provider.getCompletions(query, selection, force, limit),
null,
PROVIDER_COMPLETION_TIMEOUT,
);
}),
);
// map then filter to maintain the index for the map-operation, for this.providers to line up
return filterBoolean(
completionsList.map((completions, i) => {
if (!completions || !completions.length) return;
return {
completions,
provider: this.providers[i],
/* the currently matched "command" the completer tried to complete
* we pass this through so that Autocomplete can figure out when to
* re-show itself once hidden.
*/
command: this.providers[i].getCurrentCommand(query, selection, force),
};
}),
);
}
2016-06-01 16:54:21 +05:30
}