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

99 lines
3.0 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-03 13:41:57 +01:00
import type { MatrixEvent } from "matrix-js-sdk/lib/matrix";
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 OriginalComponentProps = {
/**
* 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;
};
2025-05-16 15:49:07 +01:00
/**
2025-05-30 14:51:28 +01:00
* Function used to render a message component.
2025-06-09 15:41:53 +01:00
* @alpha Unlikely 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
*/
2025-06-05 09:58:51 +01:00
originalComponent?: (props?: OriginalComponentProps) => React.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.
2025-05-16 15:49:07 +01:00
* @public
*/
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;
2025-05-16 15:49:07 +01:00
}