Render the widget fully from room state
This commit is contained in:
parent
0c6c0f32dd
commit
bdd34120f0
3 changed files with 60 additions and 18 deletions
|
@ -15,30 +15,31 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import AppTile from "matrix-react-sdk/src/components/views/elements/AppTile";
|
||||||
|
import { IWidget } from "matrix-widget-api";
|
||||||
|
import MatrixClientContext from "matrix-react-sdk/src/contexts/MatrixClientContext";
|
||||||
|
import { MatrixClientPeg } from "matrix-react-sdk/src/MatrixClientPeg";
|
||||||
|
|
||||||
// add React and ReactPerf to the global namespace, to make them easier to access via the console
|
// add React and ReactPerf to the global namespace, to make them easier to access via the console
|
||||||
// this incidentally means we can forget our React imports in JSX files without penalty.
|
// this incidentally means we can forget our React imports in JSX files without penalty.
|
||||||
window.React = React;
|
window.React = React;
|
||||||
|
|
||||||
import AppTile from "matrix-react-sdk/src/components/views/elements/AppTile";
|
|
||||||
|
|
||||||
export interface IStartOpts {
|
export interface IStartOpts {
|
||||||
accessToken: string;
|
|
||||||
widgetId: string;
|
widgetId: string;
|
||||||
roomId?: string;
|
roomId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadApp(opts: IStartOpts) {
|
export async function loadApp(widget: IWidget) {
|
||||||
// TODO: Actually use `opts` to populate the widget
|
return (
|
||||||
return <AppTile
|
<MatrixClientContext.Provider value={MatrixClientPeg.get()}>
|
||||||
app={{
|
<div id="mx_ThinWrapper_container">
|
||||||
id: "test1234",
|
<AppTile
|
||||||
url: "http://localhost:8082/index.html#/?widgetId=$matrix_widget_id",
|
app={widget}
|
||||||
name: "Test Widget",
|
fullWidth={true}
|
||||||
type: "m.custom",
|
userId={MatrixClientPeg.get().getUserId()}
|
||||||
data: {},
|
userWidget={false}
|
||||||
}}
|
/>
|
||||||
fullWidth={true}
|
</div>
|
||||||
userId={"@test:example.org"}
|
</MatrixClientContext.Provider>
|
||||||
userWidget={true}
|
);
|
||||||
/>;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,3 +42,17 @@ body, html {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#mx_ThinWrapper_container {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
right: 0;
|
||||||
|
left: 0;
|
||||||
|
|
||||||
|
.mx_AppTileFullWidth {
|
||||||
|
width: unset !important;
|
||||||
|
height: calc(100% - 10px); // 5px top + bottom borders on the AppTile
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -19,6 +19,9 @@ require("./index.scss");
|
||||||
import * as qs from 'querystring';
|
import * as qs from 'querystring';
|
||||||
import { settled } from "../promise_utils";
|
import { settled } from "../promise_utils";
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
|
import { StopGapWidgetDriver, WidgetRenderMode } from "matrix-react-sdk/src/stores/widgets/StopGapWidgetDriver";
|
||||||
|
import WidgetUtils from "matrix-react-sdk/src/utils/WidgetUtils";
|
||||||
|
import { MatrixClientPeg } from "matrix-react-sdk/src/MatrixClientPeg";
|
||||||
|
|
||||||
// The widget's options are encoded into the fragment to avoid leaking info to the server. The widget
|
// The widget's options are encoded into the fragment to avoid leaking info to the server. The widget
|
||||||
// spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
|
// spec on the other hand requires the widgetId and parentUrl to show up in the regular query string.
|
||||||
|
@ -31,13 +34,18 @@ const qsParam = (name: string, optional = false): string => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const accessToken = qsParam("accessToken");
|
const accessToken = qsParam("accessToken");
|
||||||
|
const homeserverUrl = qsParam("hsUrl");
|
||||||
const roomId = qsParam("roomId", true);
|
const roomId = qsParam("roomId", true);
|
||||||
const widgetId = qsParam("widgetId"); // state_key or account data key
|
const widgetId = qsParam("widgetId"); // state_key or account data key
|
||||||
|
|
||||||
|
// TODO: clear href so people don't accidentally copy/paste it
|
||||||
|
//window.location.hash = '';
|
||||||
|
|
||||||
(async function() {
|
(async function() {
|
||||||
const {
|
const {
|
||||||
rageshakePromise,
|
rageshakePromise,
|
||||||
preparePlatform,
|
preparePlatform,
|
||||||
|
loadSkin,
|
||||||
loadOlm, // to handle timelines
|
loadOlm, // to handle timelines
|
||||||
loadLanguage,
|
loadLanguage,
|
||||||
loadTheme,
|
loadTheme,
|
||||||
|
@ -54,18 +62,37 @@ const widgetId = qsParam("widgetId"); // state_key or account data key
|
||||||
await settled(rageshakePromise);
|
await settled(rageshakePromise);
|
||||||
|
|
||||||
console.log("Running startup...");
|
console.log("Running startup...");
|
||||||
|
StopGapWidgetDriver.RENDER_MODE = WidgetRenderMode.ThinWrapper;
|
||||||
|
await loadSkin();
|
||||||
await loadOlm();
|
await loadOlm();
|
||||||
preparePlatform();
|
preparePlatform();
|
||||||
|
await MatrixClientPeg.shim(homeserverUrl, accessToken);
|
||||||
await loadTheme();
|
await loadTheme();
|
||||||
await loadLanguage();
|
await loadLanguage();
|
||||||
|
|
||||||
|
console.log("Locating widget...");
|
||||||
|
const stateEvent = await MatrixClientPeg.get()._http.authedRequest(
|
||||||
|
undefined, "GET",
|
||||||
|
`/rooms/${encodeURIComponent(roomId)}/state/im.vector.modular.widgets/${encodeURIComponent(widgetId)}`,
|
||||||
|
undefined, undefined, {},
|
||||||
|
);
|
||||||
|
if (!stateEvent?.url) {
|
||||||
|
throw new Error("Invalid widget");
|
||||||
|
}
|
||||||
|
const app = WidgetUtils.makeAppConfig(
|
||||||
|
widgetId,
|
||||||
|
stateEvent,
|
||||||
|
MatrixClientPeg.get().getUserId(), // assume we are the sender
|
||||||
|
roomId,
|
||||||
|
widgetId);
|
||||||
|
|
||||||
// Now we can start our custom code
|
// Now we can start our custom code
|
||||||
console.log("Loading app...");
|
console.log("Loading app...");
|
||||||
const module = await import(
|
const module = await import(
|
||||||
/* webpackChunkName: "thin-wrapper-app" */
|
/* webpackChunkName: "thin-wrapper-app" */
|
||||||
/* webpackPreload: true */
|
/* webpackPreload: true */
|
||||||
"./app");
|
"./app");
|
||||||
window.matrixChat = ReactDOM.render(await module.loadApp({accessToken, roomId, widgetId}),
|
window.matrixChat = ReactDOM.render(await module.loadApp(app),
|
||||||
document.getElementById('matrixchat'));
|
document.getElementById('matrixchat'));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
Loading…
Add table
Reference in a new issue