Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e80121

Browse files
committed
feat: add openblocks sdk
1 parent d24980e commit 7e80121

File tree

29 files changed

+229
-117
lines changed

29 files changed

+229
-117
lines changed

client/packages/openblocks-cli/client.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ declare var REACT_APP_BUILD_ID: string;
3535
declare var REACT_APP_LOG_LEVEL: string;
3636
declare var REACT_APP_IMPORT_MAP: string;
3737
declare var REACT_APP_SERVER_IPS: string;
38+
declare var REACT_APP_BUNDLE_TYPE: "sdk" | "app";

client/packages/openblocks-cli/config/vite.config.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ buildVars.forEach(({ name, defaultValue }) => {
1111
});
1212

1313
export default defineConfig({
14-
define,
14+
define: {
15+
...define,
16+
__OPENBLOCKS_DEV__: JSON.stringify({}),
17+
},
1518
assetsInclude: ["**/*.md"],
1619
resolve: {
1720
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
18-
alias: {
19-
"__user-dev-comps__": paths.compsIndexJs,
20-
"__user-pkg-json__": paths.appPackageJson,
21-
},
2221
},
2322
build: {
2423
target: "chrome69",

client/packages/openblocks-cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openblocks-cli",
33
"description": "CLI tool used to start build publish openblocks components",
4-
"version": "0.0.14",
4+
"version": "0.0.15",
55
"bin": "./index.js",
66
"type": "module",
77
"exports": {

client/packages/openblocks-core/lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ var blacklist = new Set([
10061006
"MutationObserver",
10071007
]);
10081008
var proxyTargetIdentity = Symbol("proxy_target_identity");
1009-
var globalVarNames = new Set(["window", "globalThis", "self"]);
1009+
var globalVarNames = new Set(["window", "globalThis", "self", "global"]);
10101010
/**
10111011
* return an immutable object.
10121012
* @remarks
@@ -1087,7 +1087,7 @@ function createBlackHole() {
10871087
});
10881088
}
10891089
function createMockWindow() {
1090-
return new Proxy({}, {
1090+
var win = new Proxy({}, {
10911091
has: function () {
10921092
return true;
10931093
},
@@ -1098,6 +1098,9 @@ function createMockWindow() {
10981098
if (p in target) {
10991099
return Reflect.get(target, p);
11001100
}
1101+
if (globalVarNames.has(p)) {
1102+
return win;
1103+
}
11011104
if (typeof p === "string" && blacklist.has(p)) {
11021105
log.log("[Sandbox] access ".concat(String(p), " on mock window, return mock object"));
11031106
return createBlackHole();
@@ -1113,6 +1116,7 @@ function createMockWindow() {
11131116
return ret;
11141117
},
11151118
});
1119+
return win;
11161120
}
11171121
var mockWindow = createMockWindow();
11181122
function clearMockWindow() {

client/packages/openblocks-core/src/eval/utils/evalScript.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ describe("evalScript", () => {
3838
expect(evalScript("a[0]", { a: [] })).toBe(undefined);
3939

4040
expect(evalScript("abc", {})).toBeUndefined();
41+
expect(evalScript("window.window === window", {})).toBe(true);
42+
expect(evalScript("window.window", {})).not.toBe(window);
43+
expect(evalScript("window.global", {})).not.toBe(window);
4144
expect(evalScript("setTimeout in window", {})).toBe(true);
4245
expect(evalScript("WebSocket in window", {})).toBe(true);
4346
expect(evalScript("anything in window", {})).toBe(true);

client/packages/openblocks-core/src/eval/utils/evalScript.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const blacklist = new Set<PropertyKey>([
2121

2222
const proxyTargetIdentity = Symbol("proxy_target_identity");
2323

24-
const globalVarNames = new Set<PropertyKey>(["window", "globalThis", "self"]);
24+
const globalVarNames = new Set<PropertyKey>(["window", "globalThis", "self", "global"]);
2525

2626
/**
2727
* return an immutable object.
@@ -108,7 +108,7 @@ export function createBlackHole(): any {
108108
}
109109

110110
function createMockWindow() {
111-
return new Proxy(
111+
const win: any = new Proxy(
112112
{},
113113
{
114114
has() {
@@ -121,6 +121,9 @@ function createMockWindow() {
121121
if (p in target) {
122122
return Reflect.get(target, p);
123123
}
124+
if (globalVarNames.has(p)) {
125+
return win;
126+
}
124127
if (typeof p === "string" && blacklist.has(p)) {
125128
log.log(`[Sandbox] access ${String(p)} on mock window, return mock object`);
126129
return createBlackHole();
@@ -137,6 +140,7 @@ function createMockWindow() {
137140
},
138141
}
139142
);
143+
return win;
140144
}
141145

142146
let mockWindow: any = createMockWindow();

client/packages/openblocks-design/src/components/Trees.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const Treediv = styled.div<{ $height?: number }>`
99
display: flex;
1010
flex-direction: column;
1111
gap: 8px;
12-
overflow: hidden;
1312
1413
&::before {
1514
content: "";

client/packages/openblocks-design/src/components/form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export const FormInputItem = (props: FormProps & InputProps) => (
137137
hasFeedback={true}
138138
>
139139
<FormInput
140-
autoFocus={props.autoFocus}
140+
{...props}
141141
autoComplete={"off"}
142142
disabled={props.disabled}
143143
placeholder={props.placeholder}

client/packages/openblocks-dev-utils/buildVars.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ export const buildVars = [
3939
name: "REACT_APP_SERVER_IPS",
4040
defaultValue: "",
4141
},
42+
{
43+
name: "REACT_APP_BUNDLE_TYPE",
44+
defaultValue: "app",
45+
},
4246
];
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from "react";
2+
3+
export type OutputChangeHandler<O> = (output: O) => void;
4+
5+
export type EventTriggerHandler = (eventName: string) => void;
6+
7+
type Off = () => void;
8+
9+
interface EventHandlerMap<O = any> {
10+
moduleOutputChange: OutputChangeHandler<O>;
11+
moduleEventTriggered: EventTriggerHandler;
12+
}
13+
14+
export interface AppViewInstanceOptions<I = any> {
15+
moduleInputs?: I;
16+
}
17+
18+
export interface OpenblocksAppViewProps<I, O> extends AppViewInstanceOptions<I> {
19+
appId: string;
20+
className?: string;
21+
onModuleOutputChange?: OutputChangeHandler<O>;
22+
onModuleEventTriggered?: EventTriggerHandler;
23+
}
24+
25+
export interface AppViewInstance<I, O> {
26+
on<K extends keyof EventHandlerMap<O>>(event: K, handler?: EventHandlerMap<O>[K]): Off;
27+
setModuleInputs(inputs: I): void;
28+
invokeMethod(methodName: string): void;
29+
}
30+
31+
export declare const OpenblocksAppView: React.ForwardRefExoticComponent<
32+
OpenblocksAppViewProps<unknown, unknown> &
33+
React.RefAttributes<AppViewInstance<unknown, unknown> | undefined>
34+
>;
35+
36+
export declare function bootstrapAppAt<I = any, O = any>(
37+
appId: string,
38+
node: Element | null,
39+
options: AppViewInstanceOptions<I>
40+
): Promise<AppViewInstance<I, O>>;

client/packages/openblocks-sdk/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "openblocks-sdk",
3-
"version": "0.0.10",
3+
"version": "0.0.20",
44
"type": "module",
55
"files": [
66
"src",
77
"dist",
8+
"index.d.ts",
89
"tsconfig.json"
910
],
1011
"main": "./dist/openblocks-sdk.js",
11-
"types": "./src/openblocks/index.sdk.ts",
1212
"module": "./dist/openblocks-sdk.js",
1313
"exports": {
1414
".": {
@@ -20,10 +20,8 @@
2020
}
2121
},
2222
"scripts": {
23-
"build": "NODE_ENV=production vite build",
24-
"start": "vite",
25-
"prepack": "cp -r ../openblocks/src ./src/openblocks",
26-
"postpack": "rm -rf ./src/openblocks"
23+
"build": "vite build",
24+
"start": "vite"
2725
},
2826
"devDependencies": {
2927
"@rollup/plugin-commonjs": "^22.0.2",

client/packages/openblocks-sdk/vite.config.mts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ buildVars.forEach(({ name, defaultValue }) => {
1111
define[name] = JSON.stringify(process.env[name] || defaultValue);
1212
});
1313

14+
const apiBaseUrl = "https://api.openblocks.dev";
15+
1416
export const viteConfig: UserConfig = {
15-
define,
17+
define: {
18+
...define,
19+
REACT_APP_API_HOST: JSON.stringify(apiBaseUrl),
20+
REACT_APP_BUNDLE_TYPE: JSON.stringify("sdk"),
21+
},
1622
assetsInclude: ["**/*.md"],
1723
resolve: {
1824
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],

client/packages/openblocks/src/app-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ declare var REACT_APP_BUILD_ID: string;
3838
declare var REACT_APP_LOG_LEVEL: string;
3939
declare var REACT_APP_IMPORT_MAP: string;
4040
declare var REACT_APP_SERVER_IPS: string;
41+
declare var REACT_APP_BUNDLE_TYPE: "sdk" | "app";

client/packages/openblocks/src/comps/comps/appSettingsComp.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ItemSpan = styled.span`
2222
display: inline-flex;
2323
align-items: center;
2424
margin: 0 1px;
25-
max-width: 224px;
25+
max-width: 218px;
2626
`;
2727

2828
const getTagStyle = (theme: ThemeDetail) => {
@@ -66,7 +66,6 @@ export const DefaultSpan = styled.span`
6666
align-items: center;
6767
justify-content: center;
6868
height: 16px;
69-
min-width: 36px;
7069
padding: 0 5px;
7170
`;
7271

client/packages/openblocks/src/comps/comps/jsonComp/jsonEditorComp.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { formDataChildren, FormDataPropertyView } from "../formComp/formDataCons
1616
import { JsonEditorStyle } from "comps/controls/styleControlConstants";
1717
import { styleControl } from "comps/controls/styleControl";
1818
import { migrateOldData, withDefault } from "comps/generators/simpleGenerators";
19+
import { debounce } from "lodash";
20+
import { RecordConstructorToView } from "openblocks-core";
1921

2022
/**
2123
* JsonEditor Comp
@@ -73,12 +75,17 @@ const childrenMap = {
7375
...formDataChildren,
7476
};
7577

78+
const handleChange = (v: string | undefined, props: RecordConstructorToView<typeof childrenMap>) => {
79+
try {
80+
const value = JSON.parse(v === undefined ? "" : v);
81+
props.value.onChange(value);
82+
props.onEvent("change");
83+
} catch (error) {}
84+
};
85+
const handleChangeDebouce = debounce(handleChange, 1000);
86+
7687
let JsonEditorTmpComp = (function () {
7788
return new UICompBuilder(childrenMap, (props) => {
78-
const handleChange = (v: JSONValue) => {
79-
props.value.onChange(v);
80-
props.onEvent("change");
81-
};
8289
return props.label({
8390
style: props.style,
8491
children: (
@@ -88,11 +95,7 @@ let JsonEditorTmpComp = (function () {
8895
defaultLanguage="json"
8996
value={JSON.stringify(props.value.value, null, 2)}
9097
loading=""
91-
onChange={(v: string | undefined) => {
92-
try {
93-
handleChange(JSON.parse(v === undefined ? "" : v));
94-
} catch (error) {}
95-
}}
98+
onChange={(v) => handleChangeDebouce(v, props)}
9699
options={{
97100
contextmenu: false,
98101
hideCursorInOverviewRuler: true,

client/packages/openblocks/src/comps/comps/jsonComp/jsonExplorerComp.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import styled from "styled-components";
77
import { BoolControl } from "comps/controls/boolControl";
88
import { jsonObjectExposingStateControl } from "comps/controls/codeStateControl";
99
import { dropdownControl } from "comps/controls/dropdownControl";
10-
import { NumberControl } from "comps/controls/codeControl";
10+
import { ArrayOrJSONObjectControl, NumberControl } from "comps/controls/codeControl";
1111
import { hiddenPropertyView } from "comps/utils/propertyUtils";
1212
import { trans } from "i18n";
1313

@@ -44,15 +44,15 @@ const JsonExplorerContainer = styled.div<{ $theme: keyof typeof bgColorMap }>`
4444

4545
let JsonExplorerTmpComp = (function () {
4646
const childrenMap = {
47-
value: jsonObjectExposingStateControl("value", defaultData),
47+
value: withDefault(ArrayOrJSONObjectControl, JSON.stringify(defaultData, null, 2)),
4848
indent: withDefault(NumberControl, 4),
4949
expandToggle: BoolControl.DEFAULT_TRUE,
5050
theme: dropdownControl(themeOptions, "shapeshifter:inverted"),
5151
};
5252
return new UICompBuilder(childrenMap, (props) => (
5353
<JsonExplorerContainer $theme={props.theme as keyof typeof bgColorMap}>
5454
<ReactJson
55-
src={props.value.value}
55+
src={props.value}
5656
theme={props.theme as ThemeKeys}
5757
collapsed={!props.expandToggle}
5858
displayDataTypes={false}

client/packages/openblocks/src/comps/controls/actionSelector/executeCompAction.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function executeCompAction(params: ExecuteCompActionOptions) {
189189
/>
190190
</BranchDiv>
191191
)}
192-
{params && <BranchDiv>{this.children.params.propertyView(params)}</BranchDiv>}
192+
{!!params?.length && <BranchDiv>{this.children.params.propertyView(params)}</BranchDiv>}
193193
</>
194194
);
195195
}}

client/packages/openblocks/src/comps/controls/actionSelector/executeQueryAction.tsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,26 @@ export class ExecuteQueryAction extends ExecuteQueryTmpAction {
4646
}
4747

4848
propertyView({ placement }: { placement?: "query" | "table" }) {
49-
const getQueryOptions = (editorState: EditorState) => {
50-
const options: { label: string; value: string }[] = editorState
51-
.queryCompInfoList()
52-
.map((info) => ({
53-
label: info.name,
54-
value: info.name,
55-
}))
56-
.filter(
57-
// Filter out the current query under query
58-
(option) =>
59-
placement !== "query" ||
60-
(placement === "query" &&
61-
editorState.selectedBottomResType === BottomResTypeEnum.Query &&
62-
option.value !== editorState.selectedBottomResName)
63-
);
49+
const getQueryOptions = (editorState?: EditorState) => {
50+
const options: { label: string; value: string }[] =
51+
editorState
52+
?.queryCompInfoList()
53+
.map((info) => ({
54+
label: info.name,
55+
value: info.name,
56+
}))
57+
.filter(
58+
// Filter out the current query under query
59+
(option) =>
60+
placement !== "query" ||
61+
(placement === "query" &&
62+
editorState.selectedBottomResType === BottomResTypeEnum.Query &&
63+
option.value !== editorState.selectedBottomResName)
64+
) || [];
6465

6566
// input queries
6667
editorState
67-
.getModuleLayoutComp()
68+
?.getModuleLayoutComp()
6869
?.getInputs()
6970
.forEach((i) => {
7071
const { name, type } = i.getView();

client/packages/openblocks/src/comps/controls/codeControl.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import _ from "lodash";
2828
import { ReactNode } from "react";
2929
import {
3030
showTransform,
31+
toArrayJSONObject,
3132
toBoolean,
3233
toJSONArray,
3334
toJSONObject,
@@ -358,7 +359,7 @@ export const JSONObjectControl = jsonBaseControl<JSONObject>("JSON", toJSONObjec
358359
export const JSONValueControl = jsonBaseControl<JSONValue>(undefined, toJSONValue);
359360
// the main difference between Object and JSON is that Object's value can be function
360361
export const ObjectControl = jsonBaseControl<Record<string, unknown>>("Object", toObject);
361-
362+
export const ArrayOrJSONObjectControl = jsonBaseControl<JSONObject | Array<JSONValue>>("JSON", toArrayJSONObject);
362363
export const jsonObjectControl = (defaultValue?: JSONObject) =>
363364
defaultValue === undefined
364365
? JSONObjectControl

0 commit comments

Comments
 (0)