Allow using room pills in slash commands (#7513)

This commit is contained in:
Michael Telatynski 2022-01-12 09:40:18 +00:00 committed by GitHub
parent 31247a50ca
commit b835588331
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 193 additions and 249 deletions

View file

@ -24,10 +24,11 @@ import { EventType } from "matrix-js-sdk/src/@types/event";
import * as ContentHelpers from 'matrix-js-sdk/src/content-helpers';
import { parseFragment as parseHtml, Element as ChildElement } from "parse5";
import { logger } from "matrix-js-sdk/src/logger";
import { IContent } from 'matrix-js-sdk/src/models/event';
import { MatrixClientPeg } from './MatrixClientPeg';
import dis from './dispatcher/dispatcher';
import { _t, _td, newTranslatableError } from './languageHandler';
import { _t, _td, newTranslatableError, ITranslatableError } from './languageHandler';
import Modal from './Modal';
import MultiInviter from './utils/MultiInviter';
import { linkifyAndSanitizeHtml } from './HtmlUtils';
@ -60,6 +61,7 @@ import SlashCommandHelpDialog from "./components/views/dialogs/SlashCommandHelpD
import { shouldShowComponent } from "./customisations/helpers/UIComponents";
import { TimelineRenderingType } from './contexts/RoomContext';
import RoomViewStore from "./stores/RoomViewStore";
import { XOR } from "./@types/common";
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
interface HTMLInputEvent extends Event {
@ -94,7 +96,9 @@ export const CommandCategories = {
"other": _td("Other"),
};
type RunFn = ((roomId: string, args: string, cmd: string) => {error: any} | {promise: Promise<any>});
export type RunResult = XOR<{ error: Error | ITranslatableError }, { promise: Promise<IContent | undefined> }>;
type RunFn = ((roomId: string, args: string, cmd: string) => RunResult);
interface ICommandOpts {
command: string;
@ -109,15 +113,15 @@ interface ICommandOpts {
}
export class Command {
command: string;
aliases: string[];
args: undefined | string;
description: string;
runFn: undefined | RunFn;
category: string;
hideCompletionAfterSpace: boolean;
private _isEnabled?: () => boolean;
public renderingTypes?: TimelineRenderingType[];
public readonly command: string;
public readonly aliases: string[];
public readonly args: undefined | string;
public readonly description: string;
public readonly runFn: undefined | RunFn;
public readonly category: string;
public readonly hideCompletionAfterSpace: boolean;
public readonly renderingTypes?: TimelineRenderingType[];
private readonly _isEnabled?: () => boolean;
constructor(opts: ICommandOpts) {
this.command = opts.command;
@ -131,15 +135,15 @@ export class Command {
this.renderingTypes = opts.renderingTypes;
}
getCommand() {
public getCommand() {
return `/${this.command}`;
}
getCommandWithArgs() {
public getCommandWithArgs() {
return this.getCommand() + " " + this.args;
}
run(roomId: string, threadId: string, args: string) {
public run(roomId: string, threadId: string, args: string): RunResult {
// if it has no runFn then its an ignored/nop command (autocomplete only) e.g `/me`
if (!this.runFn) {
reject(
@ -166,11 +170,11 @@ export class Command {
return this.runFn.bind(this)(roomId, args);
}
getUsage() {
public getUsage() {
return _t('Usage') + ': ' + this.getCommandWithArgs();
}
isEnabled(): boolean {
public isEnabled(): boolean {
return this._isEnabled ? this._isEnabled() : true;
}
}
@ -1289,7 +1293,6 @@ interface ICmd {
/**
* Process the given text for /commands and return a bound method to perform them.
* @param {string} roomId The room in which the command was performed.
* @param {string} input The raw text input by the user.
* @return {null|function(): Object} Function returning an object with the property 'error' if there was an error
* processing the command, or 'promise' if a request was sent out.