Skip to content

Commit d95df27

Browse files
committed
add integrate ob module demo
1 parent 7bfe203 commit d95df27

9 files changed

+284
-36
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
},
1111
"dependencies": {
1212
"history": "^5.3.0",
13-
"openblocks-sdk": "^0.0.17",
13+
"less": "^4.1.3",
14+
"openblocks-sdk": "^0.0.20",
1415
"react": "17",
1516
"react-dom": "17",
1617
"react-router": "^6.4.3",

src/App.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { OpenblocksAppView } from "openblocks-sdk";
21
import { useEffect } from "react";
32
import {
43
createHashRouter,
@@ -9,6 +8,8 @@ import {
98
useLocation,
109
useNavigate,
1110
} from "react-router-dom";
11+
import { AppDemo } from "./AppDemo";
12+
import { ModuleDemo } from "./ModuleDemo";
1213

1314
const demos = [
1415
{
@@ -23,6 +24,13 @@ const demos = [
2324
title: "Pagination with temporary state demo",
2425
appId: "6379cdd9f02c6e6ecc1d37ff",
2526
},
27+
{
28+
title: "Use module as ui component",
29+
appId: "637dc058a899fe1ffcb1589a",
30+
isModule: true,
31+
initialMethodName: "clearForm",
32+
initialInputs: { formTitle: "Student info" },
33+
},
2634
];
2735

2836
function titleToPath(title: string) {
@@ -55,17 +63,21 @@ function Root() {
5563
);
5664
}
5765

58-
function AppDemo(props: { appId: string }) {
59-
return <OpenblocksAppView className="ob-app" appId={props.appId} />;
60-
}
61-
6266
const routes: RouteObject[] = [
6367
{
6468
path: "/",
6569
element: <Root />,
6670
children: demos.map((i) => ({
6771
path: titleToPath(i.title),
68-
element: <AppDemo appId={i.appId} />,
72+
element: i.isModule ? (
73+
<ModuleDemo
74+
appId={i.appId}
75+
initialModuleInputs={i.initialInputs}
76+
initialMethodName={i.initialMethodName}
77+
/>
78+
) : (
79+
<AppDemo appId={i.appId} />
80+
),
6981
})),
7082
},
7183
];

src/AppDemo.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { OpenblocksAppView } from "openblocks-sdk";
2+
3+
export function AppDemo(props: { appId: string }) {
4+
return <OpenblocksAppView className="ob-app" appId={props.appId} />;
5+
}

src/ModuleDemo.tsx

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { AppViewInstance, OpenblocksAppView } from "openblocks-sdk";
2+
import { useRef, useState } from "react";
3+
4+
interface IProps {
5+
appId: string;
6+
initialModuleInputs: any;
7+
initialMethodName: string;
8+
}
9+
10+
export function ModuleDemo(props: IProps) {
11+
const inputRef = useRef<HTMLTextAreaElement>(null);
12+
const methodInputRef = useRef<HTMLInputElement>(null);
13+
const instanceRef = useRef<AppViewInstance<any, any>>(null);
14+
const [inputs, setInputs] = useState<any>(props.initialModuleInputs);
15+
const [outputs, setOutputs] = useState<any>({});
16+
const [eventLogs, setEventLog] = useState<string[]>([]);
17+
18+
const handleEventTriggered = (eventName: string) => {
19+
setEventLog((logs) =>
20+
logs.concat(`${new Date().toISOString()} event ${eventName} triggered`)
21+
);
22+
};
23+
24+
const handleSetInputs = () => {
25+
const inputDataJson = inputRef.current?.value || JSON.stringify({});
26+
setInputs(JSON.parse(inputDataJson));
27+
};
28+
29+
const handleInvokeMethod = () => {
30+
const methodName = methodInputRef.current?.value;
31+
if (!methodName) {
32+
return;
33+
}
34+
instanceRef.current?.invokeMethod(methodName);
35+
};
36+
37+
return (
38+
<div className="module-demo">
39+
<div>
40+
<h2>Host page</h2>
41+
<div className="module-controls">
42+
<div>
43+
<strong>inputs</strong>
44+
<textarea
45+
ref={inputRef}
46+
rows={5}
47+
defaultValue={JSON.stringify(inputs)}
48+
placeholder="input initial module input data in json format"
49+
></textarea>
50+
<button onClick={handleSetInputs}>set inputs</button>
51+
</div>
52+
<div>
53+
<strong>outputs</strong>
54+
<textarea
55+
readOnly
56+
rows={10}
57+
value={JSON.stringify(outputs, null, 2)}
58+
></textarea>
59+
</div>
60+
<div>
61+
<strong>invoke method</strong>
62+
<input
63+
ref={methodInputRef}
64+
defaultValue={props.initialMethodName}
65+
placeholder="input module method name"
66+
/>
67+
<button onClick={handleInvokeMethod}>invoke</button>
68+
</div>
69+
<div>
70+
<strong>events trigger log</strong>
71+
<textarea
72+
rows={5}
73+
readOnly
74+
value={eventLogs.reverse().join("\n")}
75+
></textarea>
76+
</div>
77+
</div>
78+
</div>
79+
<div className="module-demo-panel">
80+
<h2>Openblocks module</h2>
81+
<div>
82+
<OpenblocksAppView
83+
onModuleOutputChange={setOutputs}
84+
onModuleEventTriggered={handleEventTriggered}
85+
moduleInputs={inputs}
86+
appId={props.appId}
87+
ref={instanceRef}
88+
/>
89+
</div>
90+
</div>
91+
</div>
92+
);
93+
}

src/index.css renamed to src/main.less

+33-21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ a {
1616
color: #646cff;
1717
text-decoration: inherit;
1818
}
19+
1920
a:hover {
2021
color: #535bf2;
2122
}
@@ -32,27 +33,6 @@ h1 {
3233
line-height: 1.1;
3334
}
3435

35-
button {
36-
border-radius: 8px;
37-
border: 1px solid transparent;
38-
padding: 0.6em 1.2em;
39-
font-size: 1em;
40-
font-weight: 500;
41-
font-family: inherit;
42-
background-color: #1a1a1a;
43-
cursor: pointer;
44-
transition: border-color 0.25s;
45-
}
46-
47-
button:hover {
48-
border-color: #646cff;
49-
}
50-
51-
button:focus,
52-
button:focus-visible {
53-
outline: 4px auto -webkit-focus-ring-color;
54-
}
55-
5636
.ob-app {
5737
width: 100%;
5838
}
@@ -73,3 +53,35 @@ button:focus-visible {
7353
.nav a {
7454
margin-right: 18px;
7555
}
56+
57+
.module-demo {
58+
display: flex;
59+
flex-direction: row;
60+
border: 1px solid #ddd;
61+
62+
& > div {
63+
flex: 1;
64+
padding: 10px;
65+
& > h2 {
66+
padding-bottom: 10px;
67+
}
68+
&:first-child {
69+
border-right: 1px solid #ddd;
70+
}
71+
}
72+
73+
.module-controls {
74+
button {
75+
margin-top: 8px;
76+
}
77+
& > div {
78+
display: flex;
79+
flex-direction: column;
80+
margin-bottom: 18px;
81+
}
82+
}
83+
}
84+
85+
button {
86+
cursor: pointer;
87+
}

src/main.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactDOM from "react-dom";
33
import App from "./App";
44

55
import "openblocks-sdk/dist/style.css";
6-
import "./index.css";
6+
import "./main.less";
77

88
ReactDOM.render(
99
<React.StrictMode>

src/vite-env.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
/// <reference types="vite/client" />
2-
3-
declare module "openblocks-sdk";

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,
17-
"jsx": "react-jsx",
17+
"jsx": "react-jsx"
1818
},
1919
"include": ["src"],
2020
"references": [{ "path": "./tsconfig.node.json" }]

0 commit comments

Comments
 (0)