Skip to content

Commit

Permalink
chore: compile
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Mar 15, 2024
1 parent c41560f commit 2f0fadc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 52 deletions.
13 changes: 2 additions & 11 deletions dist/other/jupyterlite/JupyterLiteSession.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { JupyterliteMessageSchema } from "@mat3ra/esse/lib/js/types";
import React from "react";
import MessageHandler from "./MessageHandler";
interface JupyterLiteSessionProps {
originURL: string;
defaultNotebookPath?: string;
frameId: string;
handlers: {
type: string;
filter: {
keys: string[];
};
extraParameters: any[];
handler: (args: any) => void | any;
}[];
messageHandler?: MessageHandler;
}
declare class JupyterLiteSession extends React.Component<JupyterLiteSessionProps> {
static defaultProps: Partial<JupyterLiteSessionProps>;
componentDidMount(): void;
componentWillUnmount(): void;
receiveMessage: (event: MessageEvent<JupyterliteMessageSchema>) => void;
sendMessage: (data: any, variableName: string) => void;
render(): React.JSX.Element;
}
export default JupyterLiteSession;
53 changes: 12 additions & 41 deletions dist/other/jupyterlite/JupyterLiteSession.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
import React from "react";
const defaultProps = {
// eslint-disable-next-line react/default-props-match-prop-types
originURL: "https://jupyterlite.mat3ra.com",
// eslint-disable-next-line react/default-props-match-prop-types
frameId: "jupyter-lite-iframe",
};
class JupyterLiteSession extends React.Component {
constructor() {
super(...arguments);
this.receiveMessage = (event) => {
if (event.origin !== new URL(this.props.originURL).origin)
return;
const message = event.data;
const handlerConfig = this.props.handlers.find((handler) => {
return (handler.type === message.type &&
handler.filter.keys.every((key) => message.payload.hasOwnProperty(key)));
});
if (handlerConfig) {
const { handler, filter, extraParameters } = handlerConfig;
handler(message.payload);
// TODO: make more generic
const { requestData, variableName } = message.payload;
if (requestData && variableName) {
const data = handler(variableName)();
this.sendMessage(data, variableName);
}
}
};
this.sendMessage = (data, variableName) => {
const message = {
type: "from-host-to-iframe",
payload: { data, variableName },
};
const iframe = document.getElementById(this.props.frameId);
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(message, this.props.originURL);
}
else {
console.error("JupyterLite iframe not found");
}
};
}
componentDidMount() {
window.addEventListener("message", this.receiveMessage, false);
const { messageHandler, originURL } = this.props;
messageHandler === null || messageHandler === void 0 ? void 0 : messageHandler.init(originURL);
}
componentWillUnmount() {
window.removeEventListener("message", this.receiveMessage, false);
const { messageHandler } = this.props;
messageHandler === null || messageHandler === void 0 ? void 0 : messageHandler.destroy();
}
render() {
const { defaultNotebookPath, originURL, frameId } = this.props;
Expand All @@ -49,8 +22,6 @@ class JupyterLiteSession extends React.Component {
return (React.createElement("iframe", { name: "jupyterlite", title: "JupyterLite", id: frameId, src: src, sandbox: "allow-scripts allow-same-origin allow-popups allow-forms allow-modals allow-top-navigation-by-user-activation allow-downloads", width: "100%", height: "100%" }));
}
}
JupyterLiteSession.defaultProps = {
originURL: "https://jupyterlite.mat3ra.com",
frameId: "jupyter-lite-iframe",
};
// eslint-disable-next-line react/static-property-placement
JupyterLiteSession.defaultProps = defaultProps;
export default JupyterLiteSession;
11 changes: 11 additions & 0 deletions dist/other/jupyterlite/MessageHandler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type HandlerFunction = (data?: any, variableName?: string) => void;
declare class MessageHandler {
private handlers;
private originURL;
init(originURL: string): void;
destroy(): void;
addHandlers(action: string, handlers: HandlerFunction[]): void;
private receiveMessage;
sendData(data: any, variableName?: string): void;
}
export default MessageHandler;
47 changes: 47 additions & 0 deletions dist/other/jupyterlite/MessageHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class MessageHandler {
constructor() {
this.handlers = {};
this.originURL = "*";
this.receiveMessage = (event) => {
if (this.originURL !== "*" && event.origin !== this.originURL) {
return;
}
if (event.data.type === "from-iframe-to-host") {
const { action } = event.data.payload;
// TODO: make action required in ESSE
// @ts-ignore
if (this.handlers[action]) {
// @ts-ignore
this.handlers[action].forEach((handler) => {
handler(event.data.payload.data, event.data.payload.variableName);
});
}
}
};
}
init(originURL) {
window.addEventListener("message", this.receiveMessage);
this.originURL = originURL;
}
destroy() {
window.removeEventListener("message", this.receiveMessage);
}
addHandlers(action, handlers) {
if (!this.handlers[action]) {
this.handlers[action] = [];
}
this.handlers[action].push(...handlers);
}
sendData(data, variableName = "data") {
const message = {
type: "from-host-to-iframe",
payload: {
action: "set-data",
data,
variableName,
},
};
window.parent.postMessage(message, this.originURL);
}
}
export default MessageHandler;

0 comments on commit 2f0fadc

Please sign in to comment.