Skip to content

Commit 879dcca

Browse files
authored
Merge pull request #11 from lowcoding/v1.7.0
✨ feat: 实现 amis copy,注册 runScript 动作,优化交互
2 parents 67cc97b + 37ca12f commit 879dcca

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.6.0",
6+
"version": "1.7.0",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import { Button, Space, message } from 'antd';
33
import { MessageType } from 'antd/lib/message';
44
import { render } from 'amis';
@@ -7,8 +7,16 @@ import './helper.css';
77
// import 'amis/lib/helper.css';
88
import 'amis/sdk/iconfont.css';
99
import axios from 'axios';
10+
import {
11+
ListenerAction,
12+
ListenerContext,
13+
registerAction,
14+
RendererAction,
15+
RendererEvent,
16+
} from 'amis-core';
1017
import RunScript from '../RunScript';
1118
import { useState } from '@/hooks/useImmer';
19+
import { runScript } from '@/webview/service';
1220

1321
const request: { count: number; hideLoading?: MessageType } = {
1422
count: 0,
@@ -27,6 +35,44 @@ interface IProps {
2735
onFormChange: (values: object) => void;
2836
}
2937

38+
// 动作定义
39+
interface IRunScriptAction extends ListenerAction {
40+
actionType: 'runScript';
41+
args: {
42+
method: string; // 动作参数1
43+
params: string; // 动作参数2
44+
};
45+
}
46+
47+
const componentData = {
48+
model: {},
49+
materialPath: '',
50+
privateMaterials: false,
51+
};
52+
export class RunScriptAction implements RendererAction {
53+
// @ts-ignore
54+
run(
55+
action: IRunScriptAction,
56+
renderer: ListenerContext,
57+
event: RendererEvent<any>,
58+
) {
59+
const props = renderer.props;
60+
const { method, params } = action.args;
61+
runScript({
62+
script: method,
63+
params,
64+
model: componentData.model,
65+
materialPath: componentData.materialPath,
66+
privateMaterials: componentData.privateMaterials,
67+
createBlockPath: localStorage.getItem('selectedFolder') || undefined,
68+
});
69+
}
70+
}
71+
72+
// 注册自定义动作
73+
// @ts-ignore
74+
registerAction('runScript', new RunScriptAction());
75+
3076
export default (props: IProps) => {
3177
const [scriptModalVisible, setScriptModalVisible] = useState(false);
3278
const [model, setModel] = useState({} as object);
@@ -134,8 +180,17 @@ export default (props: IProps) => {
134180
},
135181
isCancel: (value: any) => axios.isCancel(value),
136182
useMobileUI: false,
183+
copy: (contents: string, options?: any) => {
184+
navigator.clipboard.writeText(contents);
185+
message.success('已复制到剪贴板');
186+
},
137187
};
138188

189+
useEffect(() => {
190+
componentData.model = model;
191+
componentData.materialPath = props.path;
192+
}, [model, props.path]);
193+
139194
const handleOpenRunScriptModal = () => {
140195
const values = amisScoped.current
141196
.getComponentByName('page.form')

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const RunScript: React.FC<IProps> = (props) => {
3434
model: props.model,
3535
materialPath: props.materialPath,
3636
privateMaterials: props.privateMaterials,
37-
createBlockPath: localStorage.getItem('selectedFolder'),
37+
createBlockPath: localStorage.getItem('selectedFolder') || undefined,
3838
})
3939
.then((result) => {
4040
props.onOk(result);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { callVscode } from '@/webview';
44

55
interface Iprops {
66
visible: boolean;
7+
loading?: boolean;
78
onCancel: () => void;
89
onOk: (path: string, createPath: string[]) => void;
910
}
@@ -34,7 +35,12 @@ const formatTreeData = (node: OriDirectoryTreeNode) => {
3435
return formatedNode;
3536
};
3637

37-
const SelectDirectory: React.FC<Iprops> = ({ visible, onOk, onCancel }) => {
38+
const SelectDirectory: React.FC<Iprops> = ({
39+
visible,
40+
onOk,
41+
onCancel,
42+
loading,
43+
}) => {
3844
const [tree, setTree] = useState<DirectoryTreeNode[]>([]);
3945
const [formData, setFormData] = useState<{
4046
path: string;
@@ -65,8 +71,12 @@ const SelectDirectory: React.FC<Iprops> = ({ visible, onOk, onCancel }) => {
6571
okText="确定"
6672
cancelText="关闭"
6773
onCancel={() => {
74+
if (loading) {
75+
return;
76+
}
6877
onCancel();
6978
}}
79+
okButtonProps={{ loading }}
7080
onOk={() => {
7181
if (!formData.path) {
7282
message.error('未选择目录');

webview-react/src/pages/blocks/Detail/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,21 @@ export default () => {
155155
/>
156156
<SelectDirectory
157157
visible={model.directoryModalVsible}
158+
loading={model.loading}
158159
onCancel={() => {
159160
model.setDirectoryModalVsible(false);
160161
}}
161162
onOk={(path, createPath = []) => {
162-
model.setDirectoryModalVsible(false);
163+
model.setLoding(true);
163164
genCodeByBlockMaterial({
164165
material: model.selectedMaterial.name,
165166
model: model.selectedMaterial.model,
166167
path,
167168
createPath,
168169
privateMaterials: model.selectedMaterial.privateMaterials,
169170
}).then(() => {
171+
model.setDirectoryModalVsible(false);
172+
model.setLoding(false);
170173
message.success('生成成功');
171174
});
172175
}}

webview-react/src/pages/blocks/Detail/model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const useModel = () => {
2323
const [directoryModalVsible, setDirectoryModalVsible] = useState(false);
2424
const [jsonToTsModalVisble, setJsonToTsModalVisble] = useState(false);
2525
const [scriptModalVisible, setScriptModalVisible] = useState(false);
26+
const [loading, setLoding] = useState(false);
2627

2728
return {
2829
selectedMaterial,
@@ -39,6 +40,8 @@ export const useModel = () => {
3940
setJsonToTsModalVisble,
4041
scriptModalVisible,
4142
setScriptModalVisible,
43+
loading,
44+
setLoding,
4245
};
4346
};
4447

0 commit comments

Comments
 (0)