Skip to content

Commit 750a4ae

Browse files
committed
可以导入、导出
1 parent e3ab7a1 commit 750a4ae

File tree

9 files changed

+143
-9
lines changed

9 files changed

+143
-9
lines changed

src/example/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
如何使用审批流样例,根据这个目录的演示,集成到目标项目

src/example/WorkflowEditor/WorkFlowEditorInner.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { Avatar, Button, Dropdown, MenuProps, Space } from "antd"
33
import { memo, useCallback, useMemo, useState } from "react"
44
import { styled } from "styled-components"
55
import classNames from "classnames"
6-
import { NavTabs, Toolbar, WorkflowDiagram } from "../../workflow-editor"
6+
import { NavTabs, Toolbar, WorkflowDiagram, useImport } from "../../workflow-editor"
77
import { useTranslate } from "../../workflow-editor/react-locales"
8+
import { useExport } from "../../workflow-editor"
89

910
const Container = styled.div`
1011
flex:1;
@@ -27,18 +28,22 @@ export const WorkFlowEditorInner = memo((props: {
2728
const { className, ...other } = props
2829
const [selectedTab, setSelectedTab] = useState<TabType>(TabType.flowDesign)
2930
const t = useTranslate()
31+
const exportjson = useExport()
32+
const importJson = useImport()
3033
const items: MenuProps['items'] = useMemo(() => [
3134
{
3235
label: t("import"),
3336
key: 'import',
34-
icon: <ImportOutlined />
37+
icon: <ImportOutlined />,
38+
onClick: importJson,
3539
},
3640
{
3741
label: t("export"),
3842
key: 'export',
39-
icon: <ExportOutlined />
43+
icon: <ExportOutlined />,
44+
onClick: exportjson,
4045
},
41-
], [t]);
46+
], [exportjson, importJson, t]);
4247

4348
const handleNavChange = useCallback((key?: string) => {
4449
setSelectedTab((key || TabType.flowDesign) as TabType)

src/workflow-editor/classes/EditorStore.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ export class EditorStore {
114114
this.dispatch(setStartNodeAction)
115115
}
116116

117+
setStartNode(node: IWorkFlowNode) {
118+
this.backup()
119+
const setStartNodeAction: SetStartNodeAction = {
120+
type: ActionType.SET_START_NODE,
121+
payload: {
122+
node
123+
}
124+
}
125+
126+
this.dispatch(setStartNodeAction)
127+
}
128+
117129
changeNode(node: IWorkFlowNode) {
118130
this.backup()
119131
const changeNodeAction: ChangeNodeAction = {

src/workflow-editor/hooks/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export * from "./useEditorStore"
1+
export * from "./useEditorStore"
2+
export * from "./useExport"
3+
export * from "./useImport"
4+
export * from "./useSelectedNode"
5+
export * from "./useStartNode"
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1-
export function useExport(){
2-
1+
import { message } from "antd";
2+
import { useCallback } from "react";
3+
import { saveFile } from "../utils/saveFile";
4+
import { useTranslate } from "../react-locales";
5+
import { useStartNode } from "./useStartNode";
6+
7+
export function useExport() {
8+
const t = useTranslate();
9+
const startNode = useStartNode();
10+
const doExport = useCallback(() => {
11+
12+
saveFile(`approvalflow`, JSON.stringify({ startNode }, null, 2)).then(
13+
(savedName) => {
14+
if (savedName) {
15+
message.success(t("operateSuccess"))
16+
}
17+
}
18+
).catch(err => {
19+
console.error(err)
20+
});
21+
}, [startNode, t]);
22+
23+
return doExport
324
}
Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1-
export function useImport(){
2-
1+
import { message } from "antd";
2+
import { useCallback } from "react";
3+
import { getTheFiles } from "../utils/getFIles";
4+
import { IWorkFlowNode } from "../interfaces";
5+
import { useEditorStore } from "./useEditorStore";
6+
import { useTranslate } from "../react-locales";
7+
8+
export interface IFlowJson {
9+
startNode?: IWorkFlowNode
10+
}
11+
12+
export function useImport() {
13+
const edtorStore = useEditorStore()
14+
const t = useTranslate()
15+
16+
const doImport = useCallback(() => {
17+
getTheFiles(".json").then((fileHandles) => {
18+
fileHandles?.[0]?.getFile().then((file: any) => {
19+
file.text().then((fileData: any) => {
20+
try {
21+
const flowJson: IFlowJson = JSON.parse(fileData);
22+
if (flowJson.startNode) {
23+
edtorStore?.setStartNode(flowJson.startNode)
24+
} else {
25+
message.error(t("fileIllegal"));
26+
}
27+
} catch (error: any) {
28+
console.error(error);
29+
message.error(t("fileIllegal"));
30+
}
31+
});
32+
});
33+
}).catch(err => {
34+
console.error(err)
35+
});
36+
}, [edtorStore, t]);
37+
38+
return doImport
339
}

src/workflow-editor/locales.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const defalutLocales: ILocales = {
4444
add: "添加",
4545
departmentsAndMembersVisable: "以下部门和人员可看到审批模板",
4646
search: "搜索",
47+
operateSuccess: "操作成功",
48+
fileIllegal: "文件不合法",
4749
},
4850
'en-US': {
4951
baseSettings: "Base Settings",
@@ -88,5 +90,7 @@ export const defalutLocales: ILocales = {
8890
add: "Add",
8991
departmentsAndMembersVisable: "Departments and Members",
9092
search: "Search",
93+
operateSuccess: "Operate success",
94+
fileIllegal: "File Illegal",
9195
}
9296
}

src/workflow-editor/utils/getFIles.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export async function getTheFiles(accept: string, multiple?: boolean) {
2+
// open file picker
3+
const fileHandles = await (window as any).showOpenFilePicker({
4+
types: [{
5+
accept: {
6+
"file/*": accept?.split(",")
7+
},
8+
}],
9+
excludeAcceptAllOption: false,
10+
multiple: multiple,
11+
});
12+
13+
return fileHandles;
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const pickerTypes = [
2+
{
3+
accept: {
4+
"text/json": [".json"],
5+
},
6+
},
7+
];
8+
9+
10+
export async function saveFile(name: string, content: string) {
11+
//const handle = getHandle();
12+
// create a new handle
13+
try{
14+
const newHandle = await (window as any).showSaveFilePicker({
15+
suggestedName: name + ".json",
16+
types: pickerTypes,
17+
});
18+
19+
// create a FileSystemWritableFileStream to write to
20+
const writableStream = await newHandle.createWritable();
21+
22+
// write our file
23+
await writableStream.write(content);
24+
25+
// close the file and write the contents to disk.
26+
await writableStream.close();
27+
28+
//setHandle(newHandle);
29+
30+
return newHandle.name;
31+
}
32+
catch(error){
33+
console.error(error);
34+
return false;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)