Files
ThreadNet-Web/apps/web/src/editor/autocomplete.ts
T

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

112 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-07-15 09:45:45 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2019-2024 New Vector Ltd.
2020-07-15 09:45:45 +01:00
Copyright 2019 The Matrix.org Foundation C.I.C.
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.
2020-07-15 09:45:45 +01:00
*/
2025-02-05 13:25:06 +00:00
import type React from "react";
import { type Part, type CommandPartCreator, type PartCreator } from "./parts";
import type DocumentPosition from "./position";
import { type ICompletion, type ISelectionRange } from "../autocomplete/Autocompleter";
import type Autocomplete from "../components/views/rooms/Autocomplete";
2020-07-15 09:45:45 +01:00
export interface ICallback {
2020-07-20 16:33:53 +01:00
replaceParts?: Part[];
range?: ISelectionRange;
2020-07-15 09:45:45 +01:00
close?: boolean;
}
export type UpdateCallback = (data: ICallback) => void;
export type GetAutocompleterComponent = () => Autocomplete | null;
2020-07-15 09:45:45 +01:00
export type UpdateQuery = (test: string) => Promise<void>;
export default class AutocompleteWrapperModel {
private partIndex?: number;
2020-07-15 09:45:45 +01:00
public constructor(
2020-07-15 09:45:45 +01:00
private updateCallback: UpdateCallback,
private getAutocompleterComponent: GetAutocompleterComponent,
private updateQuery: UpdateQuery,
private partCreator: PartCreator | CommandPartCreator,
) {}
public onEscape(e: KeyboardEvent | React.KeyboardEvent): void {
this.getAutocompleterComponent()?.onEscape(e);
2020-07-15 09:45:45 +01:00
}
2021-07-12 13:26:34 +01:00
public close(): void {
2021-06-29 13:11:58 +01:00
this.updateCallback({ close: true });
2020-07-15 09:45:45 +01:00
}
2021-07-12 13:26:34 +01:00
public hasSelection(): boolean {
return !!this.getAutocompleterComponent()?.hasSelection();
2020-07-15 09:45:45 +01:00
}
2021-07-12 13:26:34 +01:00
public hasCompletions(): boolean {
2020-07-15 09:45:45 +01:00
const ac = this.getAutocompleterComponent();
return !!ac && ac.countCompletions() > 0;
2020-07-15 09:45:45 +01:00
}
public confirmCompletion(): void {
this.getAutocompleterComponent()?.onConfirmCompletion();
2021-06-29 13:11:58 +01:00
this.updateCallback({ close: true });
2020-07-15 09:45:45 +01:00
}
/**
* If there is no current autocompletion, start one and move to the first selection.
*/
2021-07-12 13:26:34 +01:00
public async startSelection(): Promise<void> {
2020-07-15 09:45:45 +01:00
const acComponent = this.getAutocompleterComponent();
if (acComponent && acComponent.countCompletions() === 0) {
2020-07-15 09:45:45 +01:00
// Force completions to show for the text currently entered
await acComponent.forceComplete();
}
}
2021-07-12 13:26:34 +01:00
public selectPreviousSelection(): void {
this.getAutocompleterComponent()?.moveSelection(-1);
2020-07-15 09:45:45 +01:00
}
2021-07-12 13:26:34 +01:00
public selectNextSelection(): void {
this.getAutocompleterComponent()?.moveSelection(+1);
2020-07-15 09:45:45 +01:00
}
2021-07-12 13:26:34 +01:00
public onPartUpdate(part: Part, pos: DocumentPosition): Promise<void> {
2020-07-15 09:45:45 +01:00
this.partIndex = pos.index;
return this.updateQuery(part.text);
}
2021-07-12 13:26:34 +01:00
public onComponentConfirm(completion: ICompletion): void {
2020-07-15 09:45:45 +01:00
this.updateCallback({
replaceParts: this.partForCompletion(completion),
close: true,
range: completion.range,
2020-07-15 09:45:45 +01:00
});
}
2021-07-12 13:26:34 +01:00
private partForCompletion(completion: ICompletion): Part[] {
2021-06-29 13:11:58 +01:00
const { completionId } = completion;
2020-07-15 09:45:45 +01:00
const text = completion.completion;
switch (completion.type) {
case "room":
return [this.partCreator.roomPill(text, completionId), this.partCreator.plain(completion.suffix || "")];
2020-07-15 09:45:45 +01:00
case "at-room":
return [
this.partCreator.atRoomPill(completionId || ""),
this.partCreator.plain(completion.suffix || ""),
];
2020-07-15 09:45:45 +01:00
case "user":
// Insert suffix only if the pill is the part with index 0 - we are at the start of the composer
return this.partCreator.createMentionParts(this.partIndex === 0, text, completionId || "");
2020-07-15 09:45:45 +01:00
case "command":
// command needs special handling for auto complete, but also renders as plain texts
return [(this.partCreator as CommandPartCreator).command(text)];
default:
// used for emoji and other plain text completion replacement
2022-01-24 07:53:05 -05:00
return this.partCreator.plainWithEmoji(text);
2020-07-15 09:45:45 +01:00
}
}
}