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

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-05-19 09:16:27 +01:00
import type { JSX } from "react";
import type { RoomEvent } from "matrix-js-sdk";
2025-05-16 15:49:07 +01:00
/**
2025-05-19 09:16:27 +01:00
* Targets in Element for custom components.
2025-05-16 15:49:07 +01:00
* @public
*/
export enum CustomComponentTarget {
/**
2025-05-19 09:16:27 +01:00
* Component that renders "m.room.message" events in the room timeline.
2025-05-16 15:49:07 +01:00
*/
TextualBody = "TextualBody",
2025-05-19 09:16:27 +01:00
/**
* "Options" Context menu for a timeline event.
* Use `buildContextMenuBlock` to build a section to be used by this component.
* @see buildContextMenuBlock
*/
2025-05-16 15:49:07 +01:00
MessageContextMenu = "MessageContextMenu",
}
export interface ContextMenuItem {
2025-05-19 09:16:27 +01:00
/**
* The human readable label for an event.
* TODO: Should this be i18n-d
*/
2025-05-16 15:49:07 +01:00
label: string;
2025-05-19 09:16:27 +01:00
/**
* The icon to use for this item.
* https://github.com/vector-im/riot-web/blob/efc6149a8b3362c01b93f52e76e5c4ae8cbcb65c/res/css/views/context_menus/_MessageContextMenu.pcss#L10
*/
2025-05-16 15:49:07 +01:00
iconClassName: string;
2025-05-19 09:16:27 +01:00
/**
* Handler for click events on the context menu item.
* Does NOT close the menu after execution.
*/
2025-05-16 15:49:07 +01:00
onClick: (
e: React.MouseEvent<Element> | React.KeyboardEvent<Element> | React.FormEvent<Element>,
) => void | Promise<void>;
}
/**
* Properties for the render component.
* @public
*/
export type CustomComponentProps = {
[CustomComponentTarget.TextualBody]: {
/**
2025-05-19 09:16:27 +01:00
* The Matrix event for this textual body.
2025-05-16 15:49:07 +01:00
*/
2025-05-19 09:16:27 +01:00
mxEvent: RoomEvent;
2025-05-16 15:49:07 +01:00
/**
2025-05-19 09:16:27 +01:00
* Words to highlight on (e.g. from search results).\
* May be undefined if the client does not need to highlight
2025-05-16 15:49:07 +01:00
*/
highlights?: string[];
/**
* Should previews be shown for this event
*/
showUrlPreview?: boolean;
/**
* Is this event being rendered to a static export
*/
forExport?: boolean;
};
[CustomComponentTarget.MessageContextMenu]: {
/**
2025-05-19 09:16:27 +01:00
* The Matrix event which this context menu targets.
2025-05-16 15:49:07 +01:00
*/
2025-05-19 09:16:27 +01:00
mxEvent: RoomEvent;
2025-05-16 15:49:07 +01:00
/**
2025-05-19 09:16:27 +01:00
* Function that will close the menu.
2025-05-16 15:49:07 +01:00
*/
closeMenu: () => void;
};
};
/**
* Render function. Returning null skips this function and passes it onto the next registered renderer.
* @public
*/
export type CustomComponentRenderFunction<T extends CustomComponentTarget> = (
2025-05-19 09:16:27 +01:00
/**
* Properties from the given target to be used for rendering.
*/
2025-05-16 15:49:07 +01:00
props: CustomComponentProps[T],
2025-05-19 09:16:27 +01:00
/**
* The original component.
*/
2025-05-16 15:49:07 +01:00
originalComponent: JSX.Element,
) => JSX.Element | null;
/**
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 {
/**
* Register a renderer for a component type.
2025-05-19 09:16:27 +01:00
* The render function should either return a rendered component, or `null` if the
* component should not be overidden.
*
* Multiple render function may be registered for a single target, however the first
* non-null result will be used. If all results are null, or no registrations exist
* for a target then the original component is used.
*
* @param target - The target location for the component.
2025-05-16 15:49:07 +01:00
* @param renderer - The render method.
*/
register<T extends CustomComponentTarget>(target: T, renderer: CustomComponentRenderFunction<T>): void;
/**
2025-05-19 09:16:27 +01:00
* Generate a context menu section for a given set of items.
2025-05-16 15:49:07 +01:00
* @param items - A set of items to render.
*/
buildContextMenuBlock(items: ContextMenuItem[]): JSX.Element;
}