Files
ThreadNet-Web/packages/element-web-module-api/src/api/custom-components.ts
T

147 lines
4.6 KiB
TypeScript
Raw Normal View History

2025-05-20 12:14:20 +01:00
/*
Copyright 2025 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
2025-05-19 09:16:27 +01:00
import type { JSX } from "react";
2025-06-17 13:16:01 +01:00
import type { MatrixEvent } from "../models/event";
2025-05-16 15:49:07 +01:00
/**
2025-05-30 14:51:28 +01:00
* Properties for all message components.
* @alpha Subject to change.
2025-05-16 15:49:07 +01:00
*/
2025-05-30 14:51:28 +01:00
export type CustomMessageComponentProps = {
2025-05-16 15:49:07 +01:00
/**
2025-05-30 14:51:28 +01:00
* The Matrix event for this textual body.
2025-06-09 15:41:53 +01:00
* @alpha
2025-05-16 15:49:07 +01:00
*/
2025-05-30 14:51:28 +01:00
mxEvent: MatrixEvent;
};
2025-05-16 15:49:07 +01:00
2025-06-05 09:58:51 +01:00
/**
* Properties to alter the render function of the original component.
* @alpha Subject to change.
*/
export type OriginalMessageComponentProps = {
2025-06-05 09:58:51 +01:00
/**
* Should previews be shown for this event.
2025-06-05 10:01:40 +01:00
* This may be overriden by user preferences.
2025-06-05 09:58:51 +01:00
*/
showUrlPreview?: boolean;
};
2025-06-09 15:41:53 +01:00
/**
* Hints to specify to Element when rendering events.
* @alpha Subject to change.
*/
export type CustomMessageRenderHints = {
/**
* Should the event be allowed to be edited in the client. This should
* be set to false if you override the render function, as the module
* API has no way to display message editing at the moment.
* Default is true.
*/
allowEditingEvent?: boolean;
/**
* If an event contains media, this function will be called to check
* if the media can be prompted to be downloaded as a file.
2025-07-03 14:08:10 +01:00
* If this function is not supplied, media downloads are allowed.
*/
allowDownloadingMedia?: (mxEvent: MatrixEvent) => Promise<boolean>;
2025-06-09 15:41:53 +01:00
};
2025-05-16 15:49:07 +01:00
/**
2025-05-30 14:51:28 +01:00
* Function used to render a message component.
* @alpha Subject to change.
2025-05-16 15:49:07 +01:00
*/
2025-05-30 14:51:28 +01:00
export type CustomMessageRenderFunction = (
2025-05-19 09:16:27 +01:00
/**
2025-05-30 14:51:28 +01:00
* Properties for the message to be renderered.
2025-05-19 09:16:27 +01:00
*/
2025-05-30 14:51:28 +01:00
props: CustomMessageComponentProps,
2025-05-19 09:16:27 +01:00
/**
2025-05-30 14:51:28 +01:00
* Render function for the original component. This may be omitted if the message would not normally be rendered.
2025-05-19 09:16:27 +01:00
*/
originalComponent?: (props?: OriginalMessageComponentProps) => React.JSX.Element,
) => JSX.Element;
/**
* Properties for all message components.
* @alpha Subject to change.
*/
export type CustomRoomPreviewBarComponentProps = {
roomId?: string;
roomAlias?: string;
};
/**
* Function used to render a room preview bar component.
* @alpha Unlikely to change
*/
export type CustomRoomPreviewBarRenderFunction = (
/**
* Properties for the room preview bar to be rendered.
*/
props: CustomRoomPreviewBarComponentProps,
/**
* Render function for the original component.
*/
originalComponent: (props: CustomRoomPreviewBarComponentProps) => JSX.Element,
2025-06-09 15:41:53 +01:00
) => JSX.Element;
2025-05-16 15:49:07 +01:00
/**
2025-05-19 09:16:27 +01:00
* API for inserting custom components into Element.
* @alpha Subject to change.
2025-05-16 15:49:07 +01:00
*/
export interface CustomComponentsApi {
/**
2025-05-30 14:51:28 +01:00
* Register a renderer for a message type in the timeline.
*
2025-06-09 15:41:53 +01:00
* The render function should return a rendered component.
2025-05-19 09:16:27 +01:00
*
2025-06-09 15:41:53 +01:00
* Multiple render function may be registered for a single event type, however the first matching
* result will be used. If no events match or are registered then the originalComponent is rendered.
2025-05-19 09:16:27 +01:00
*
2025-06-09 15:41:53 +01:00
* @param eventTypeOrFilter - The event type this renderer is for. Use a function for more complex filtering.
2025-05-30 14:51:28 +01:00
* @param renderer - The render function.
2025-06-09 15:41:53 +01:00
* @param hints - Hints that alter the way the tile is handled.
2025-06-05 16:10:35 +01:00
* @example
* ```
* customComponents.registerMessageRenderer("m.room.message", (props, originalComponent) => {
* return <YourCustomComponent mxEvent={props.mxEvent} />;
* });
2025-06-09 15:41:53 +01:00
* customComponents.registerMessageRenderer(
* (mxEvent) => mxEvent.getType().matches(/m\.room\.(topic|name)/) && mxEvent.isState(),
* (props, originalComponent) => {
* return <YourCustomStateRenderer mxEvent={props.mxEvent} />;
* }
* );
2025-06-05 16:10:35 +01:00
* ```
2025-05-16 15:49:07 +01:00
*/
2025-06-09 15:41:53 +01:00
registerMessageRenderer(
eventTypeOrFilter: string | ((mxEvent: MatrixEvent) => boolean),
renderer: CustomMessageRenderFunction,
hints?: CustomMessageRenderHints,
): void;
/**
* Register a renderer for the room preview bar.
*
* The render function should return a rendered component.
*
* @param renderer - The render function for the room preview bar.
* @example
* ```
* customComponents.registerRoomPreviewBar((props, OriginalComponent) => {
* if (props.roomId === "!some_special_room_id:server") {
* return <YourCustomRoomPreviewBarComponent {...props} />;
* }
* return <YourCustomComponent mxEvent={props.mxEvent} />;
* });
* ```
*/
registerRoomPreviewBar(renderer: CustomRoomPreviewBarRenderFunction): void;
2025-05-16 15:49:07 +01:00
}