Skip to content

Commit 8cba6e1

Browse files
authored
Merge pull request #27 from lowcoding/feat/v1.8.3
✨ feat: 优化交互
2 parents 9c9a479 + cfdccac commit 8cba6e1

File tree

11 files changed

+251
-210
lines changed

11 files changed

+251
-210
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
低代码工具,支持 ChatGPT 和其它 LLM
44

5-
[详细文档](https://lowcoding.gitee.io/)
5+
[详细文档](https://lowcoding.github.io/)
66

77
> 文档不经常更新,新功能使用方法可查看 [releases](https://github.com/lowcoding/lowcode-vscode/releases)
88

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT and other LLM",
55
"author": "wjkang <ruoxieme@gmail.com>",
6-
"version": "1.8.2",
6+
"version": "1.8.3",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",
@@ -222,7 +222,7 @@
222222
"viewsWelcome": [
223223
{
224224
"view": "lowcode.welcome",
225-
"contents": "[打开文件夹](command:lowcode.openFolderForceNewWindow)\n[创建应用](command:lowcode.openScaffold)\n[ChatGPT](command:lowcode.showChatGPTView)\n[设置](command:lowcode.openConfig)\n查看[文档](https://lowcoding.gitee.io)以学习如何在 VS Code 中如何使用 lowcode。"
225+
"contents": "[打开文件夹](command:lowcode.openFolderForceNewWindow)\n[创建应用](command:lowcode.openScaffold)\n[ChatGPT](command:lowcode.showChatGPTView)\n[设置](command:lowcode.openConfig)\n查看[文档](https://lowcoding.github.io/)以学习如何在 VS Code 中如何使用 lowcode。"
226226
}
227227
],
228228
"configuration": {

src/webview/controllers/script.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export const runScript = async (
2323
if (fs.existsSync(scriptFile)) {
2424
delete eval('require').cache[eval('require').resolve(scriptFile)];
2525
const script = eval('require')(scriptFile);
26-
if (script[message.data.script]) {
26+
if (script[message.data.script] || script.runScript) {
2727
const context = {
2828
model: message.data.model,
29+
method: message.data.script,
30+
script: message.data.script,
2931
params: message.data.params,
3032
clipboardImage: message.data.clipboardImage,
3133
vscode,
@@ -39,7 +41,9 @@ export const runScript = async (
3941
materialPath: message.data.materialPath,
4042
activeTextEditor: getLastActiveTextEditor(),
4143
};
42-
const extendModel = await script[message.data.script](context);
44+
const extendModel = await (
45+
script[message.data.script] || script.runScript
46+
)(context);
4347
return extendModel;
4448
}
4549
throw new Error(`方法: ${message.data.script} 不存在`);

webview-react/src/components/AmisComponent/index.tsx

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import React, { useEffect, useRef } from 'react';
2-
import { Button, Space, message } from 'antd';
1+
import React, {
2+
forwardRef,
3+
useEffect,
4+
useImperativeHandle,
5+
useRef,
6+
} from 'react';
7+
import { message } from 'antd';
38
import { MessageType } from 'antd/lib/message';
49
import { render } from 'amis';
510
import './cxd.css';
@@ -14,7 +19,6 @@ import {
1419
RendererAction,
1520
RendererEvent,
1621
} from 'amis-core';
17-
import RunScript from '../RunScript';
1822
import { useState } from '@/hooks/useImmer';
1923
import { runScript } from '@/webview/service';
2024

@@ -25,14 +29,7 @@ const request: { count: number; hideLoading?: MessageType } = {
2529

2630
interface IProps {
2731
schema: object;
28-
scripts?: [
29-
{
30-
method: string;
31-
remark: string;
32-
},
33-
];
3432
path: string;
35-
onFormChange: (values: object) => void;
3633
}
3734

3835
// 动作定义
@@ -74,8 +71,7 @@ export class RunScriptAction implements RendererAction {
7471
// @ts-ignore
7572
registerAction('runScript', new RunScriptAction());
7673

77-
export default (props: IProps) => {
78-
const [scriptModalVisible, setScriptModalVisible] = useState(false);
74+
export default forwardRef((props: IProps, ref) => {
7975
const [model, setModel] = useState({} as object);
8076
const amisScoped = useRef<any>();
8177
const env = {
@@ -187,65 +183,41 @@ export default (props: IProps) => {
187183
},
188184
};
189185

186+
useImperativeHandle(ref, () => ({
187+
getValues: () => {
188+
const component = amisScoped.current.getComponentByName('page.form');
189+
if (component) {
190+
setModel(component.getValues());
191+
return component.getValues();
192+
}
193+
return {};
194+
},
195+
setValues: (values: object) => {
196+
const component = amisScoped.current.getComponentByName('page.form');
197+
if (component) {
198+
component.setValues(values);
199+
}
200+
setModel(values);
201+
},
202+
}));
203+
190204
useEffect(() => {
191205
componentData.model = model;
192206
componentData.materialPath = props.path;
193207
}, [model, props.path]);
194208

195-
const handleOpenRunScriptModal = () => {
196-
const values = amisScoped.current
197-
.getComponentByName('page.form')
198-
.getValues();
199-
setModel(values);
200-
setScriptModalVisible(true);
201-
};
202-
203-
const handleRunScriptResult = (result: object) => {
204-
amisScoped.current.getComponentByName('page.form').setValues(result);
205-
props.onFormChange(result);
206-
setScriptModalVisible(false);
207-
};
208-
209209
return (
210210
<>
211211
{render(
212212
props.schema as any,
213213
{
214-
scopeRef: (ref: any) => {
215-
amisScoped.current = ref;
214+
scopeRef: (scopeRef: any) => {
215+
amisScoped.current = scopeRef;
216216
},
217217
useMobileUI: false,
218218
},
219219
env,
220220
)}
221-
<br></br>
222-
<Space>
223-
<Button type="primary" size="small" onClick={handleOpenRunScriptModal}>
224-
执行脚本
225-
</Button>
226-
<Button
227-
type="primary"
228-
size="small"
229-
onClick={() => {
230-
const values = amisScoped.current
231-
.getComponentByName('page.form')
232-
.getValues();
233-
props.onFormChange(values);
234-
}}
235-
>
236-
生成模板数据
237-
</Button>
238-
</Space>
239-
<RunScript
240-
visible={scriptModalVisible}
241-
materialPath={props.path}
242-
model={model}
243-
scripts={props.scripts}
244-
onCancel={() => {
245-
setScriptModalVisible(false);
246-
}}
247-
onOk={handleRunScriptResult}
248-
/>
249221
</>
250222
);
251-
};
223+
});

webview-react/src/components/FormilyComponent/index.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import React, { useEffect, useMemo } from 'react';
1+
import React, {
2+
forwardRef,
3+
useEffect,
4+
useImperativeHandle,
5+
useMemo,
6+
} from 'react';
27
import { createForm } from '@formily/core';
38
import { createSchemaField } from '@formily/react';
49
import {
@@ -89,17 +94,11 @@ interface IProps {
8994
schema: object;
9095
};
9196
initialValues: object;
92-
scripts?: [
93-
{
94-
method: string;
95-
remark: string;
96-
},
97-
];
9897
path: string;
9998
onFormChange: (values: object) => void;
10099
}
101100

102-
export default (props: IProps) => {
101+
export default forwardRef((props: IProps, ref) => {
103102
const [init, setInit] = useState(false);
104103
const [scriptModalVisible, setScriptModalVisible] = useState(false);
105104
const [model, setModel] = useState({} as object);
@@ -116,6 +115,13 @@ export default (props: IProps) => {
116115
[],
117116
);
118117

118+
useImperativeHandle(ref, () => ({
119+
getValues: () => form.values,
120+
setValues: (values: object) => {
121+
form.setValues(values);
122+
},
123+
}));
124+
119125
useEffect(() => {
120126
if (
121127
props.initialValues &&
@@ -140,7 +146,7 @@ export default (props: IProps) => {
140146
<Form {...props.schema.form} form={form}>
141147
<SchemaField schema={props.schema.schema} />
142148
</Form>
143-
<Space>
149+
{/* <Space>
144150
<Button
145151
type="primary"
146152
size="small"
@@ -159,8 +165,8 @@ export default (props: IProps) => {
159165
>
160166
重新生成模板数据
161167
</Button>
162-
</Space>
163-
<RunScript
168+
</Space> */}
169+
{/* <RunScript
164170
visible={scriptModalVisible}
165171
materialPath={props.path}
166172
model={model}
@@ -169,7 +175,7 @@ export default (props: IProps) => {
169175
setScriptModalVisible(false);
170176
}}
171177
onOk={handleRunScriptResult}
172-
/>
178+
/> */}
173179
</div>
174180
);
175-
};
181+
});

webview-react/src/components/RunScript/index.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ interface IProps {
1111
scripts?: [{ method: string; remark: string; readClipboardImage?: boolean }];
1212
privateMaterials?: boolean;
1313
onCancel: () => void;
14-
onOk: (model: object) => void;
14+
onOk: (result: {
15+
/** 立即更新 model */
16+
updateModelImmediately: boolean;
17+
/** 仅更新参数 */
18+
onlyUpdateParams: boolean;
19+
/** 要更新的参数 */
20+
params?: string;
21+
model: object;
22+
}) => void;
1523
}
1624

1725
const RunScript: React.FC<IProps> = (props) => {
@@ -21,8 +29,6 @@ const RunScript: React.FC<IProps> = (props) => {
2129

2230
useEffect(() => {
2331
if (props.visible) {
24-
setScript('');
25-
setParams('');
2632
setLoading(false);
2733
}
2834
}, [props.visible]);
@@ -40,7 +46,19 @@ const RunScript: React.FC<IProps> = (props) => {
4046
createBlockPath: localStorage.getItem('selectedFolder') || undefined,
4147
})
4248
.then((result) => {
43-
props.onOk(result);
49+
if (result.model) {
50+
if (result.onlyUpdateParams) {
51+
setParams(result.params || '');
52+
} else {
53+
props.onOk(result);
54+
}
55+
} else {
56+
props.onOk({
57+
updateModelImmediately: true,
58+
onlyUpdateParams: false,
59+
model: result, // 旧版本只返回 model
60+
});
61+
}
4462
})
4563
.finally(() => {
4664
setLoading(false);
@@ -55,17 +73,17 @@ const RunScript: React.FC<IProps> = (props) => {
5573
maskClosable={false}
5674
onCancel={props.onCancel}
5775
onOk={handleOk}
76+
okText="确定"
77+
cancelText="取消"
5878
>
5979
<Form layout="vertical">
6080
<Form.Item label="方法">
6181
<Select
62-
mode="tags"
6382
placeholder="请输入或选择"
64-
value={script ? [script] : undefined}
83+
value={script || undefined}
6584
onChange={(value) => {
66-
setScript(value && value.length ? value[value.length - 1] : '');
85+
setScript(value);
6786
}}
68-
notFoundContent={null}
6987
>
7088
{props.scripts?.map((item) => (
7189
<Select.Option value={item.method} key={item.method}>
@@ -77,6 +95,7 @@ const RunScript: React.FC<IProps> = (props) => {
7795
<Form.Item label="参数">
7896
<Input.TextArea
7997
value={params}
98+
rows={6}
8099
onChange={(e) => {
81100
const value = e.target.value;
82101
setParams(value);

webview-react/src/components/SelectDirectory/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const SelectDirectory: React.FC<Iprops> = ({
6868
title="选择模块生成目录"
6969
visible={visible}
7070
closable={false}
71+
maskClosable={false}
7172
okText="确定"
7273
cancelText="关闭"
7374
onCancel={() => {

0 commit comments

Comments
 (0)