matrix-widget-api
JavaScript/TypeScript SDK for widgets & clients to communicate.
For help and support, visit #matrix-widgets:matrix.org on Matrix.
Not yet ready for usage
This is currently not validated and thus should not be relied upon until this notice goes away. Installation
instructions will take this notice's place.
Using the API without a bundler
If you're looking to drop the widget-api into a web browser without the use of a bundler, add a script
tag similar to the following:
<script src="https://unpkg.com/matrix-widget-api@0.1.0/dist/api.min.js"></script>
Note that the version number may need changing to match the current release.
Once included, the widget-api will be available under mxwidgets
. For example, new mxwidgets.WidgetApi(...)
to instantiate the WidgetApi
class.
Usage for widgets
The general usage for this would be:
const widgetId = null;
const api = new WidgetApi(widgetId);
api.requestCapability(MatrixCapabilities.Screenshots);
api.requestCapabilities(StickerpickerCapabilities);
api.on(`action:${WidgetApiToWidgetAction.UpdateVisibility}`, (ev: CustomEvent<IVisibilityActionRequest>) => {
ev.preventDefault();
console.log(ev.detail);
api.transport.reply(ev.detail, <IWidgetApiRequestEmptyData>{});
});
api.on("action:com.example.my_action", (ev: CustomEvent<ICustomActionRequest>) => {
ev.preventDefault();
console.log(ev.detail);
api.transport.reply(ev.detail, {custom: "reply"});
});
api.start();
api.sendContentLoaded();
api.setAlwaysOnScreen(true);
api.transport.send("com.example.my_action", {isExample: true});
For a more complete example, see the examples
directory of this repo.
Usage for web clients
This SDK is meant for use in browser-based applications. The concepts may be transferable to other platforms,
though currently this SDK is intended to only be used by browsers. In the future it may be possible for this
SDK to provide an interface for other platforms.
TODO: Improve this
const driver = new CustomDriver();
const api = new ClientWidgetApi(widget, iframe, driver);
api.on("ready", () => {
api.updateVisibility(true).then(() => console.log("Widget knows it is visible now"));
api.transport.send("com.example.my_action", {isExample: true});
});
api.stop();