Giter Site home page Giter Site logo

kb1rd / matrix-widget-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from matrix-org/matrix-widget-api

0.0 0.0 0.0 200 KB

JavaScript/TypeScript API for widgets & web clients to communicate

License: Apache License 2.0

JavaScript 0.87% TypeScript 99.13%

matrix-widget-api's Introduction

matrix-widget-api

npm

JavaScript/TypeScript SDK for widgets & clients to communicate.

For help and support, visit #matrix-dev: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/[email protected]/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; // if you know the widget ID, supply it.
const api = new WidgetApi(widgetId);

// Before doing anything else, request capabilities:
api.requestCapability(MatrixCapabilities.Screenshots);
api.requestCapabilities(StickerpickerCapabilities);

// Add custom action handlers (if needed)
api.on(`action:${WidgetApiToWidgetAction.UpdateVisibility}`, (ev: CustomEvent<IVisibilityActionRequest>) => {
    ev.preventDefault(); // we're handling it, so stop the widget API from doing something.
    console.log(ev.detail); // custom handling here
    api.transport.reply(ev.detail, <IWidgetApiRequestEmptyData>{});
});
api.on("action:com.example.my_action", (ev: CustomEvent<ICustomActionRequest>) => {
    ev.preventDefault(); // we're handling it, so stop the widget API from doing something.
    console.log(ev.detail); // custom handling here
    api.transport.reply(ev.detail, {custom: "reply"});
});

// Start the messaging
api.start();

// If waitForIframeLoad is false, tell the client that we're good to go
api.sendContentLoaded();

// Later, do something else (if needed)
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(); // an implementation of WidgetDriver
const api = new ClientWidgetApi(widget, iframe, driver);

// The API is automatically started, so we just have to wait for a ready before doing something
api.on("ready", () => {
    api.updateVisibility(true).then(() => console.log("Widget knows it is visible now"));
    api.transport.send("com.example.my_action", {isExample: true});
});

// Eventually, stop the API handling
api.stop();

matrix-widget-api's People

Contributors

jryans avatar t3chguy avatar turt2live avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.