-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yxh01132861
committed
Nov 28, 2023
1 parent
6a4f2ea
commit 032033f
Showing
5 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
packages/li-editor/src/widgets/FetchDataset/FetchDataset.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.li-fetch-dataset { | ||
width: 800px; | ||
min-height: 600px; | ||
overflow-y: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/li-editor/src/widgets/FetchDataset/ProcessingFunction/helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { FuncItem } from './index'; | ||
|
||
export const getValue = (funList: FuncItem[]) => { | ||
const funs: Record<string, any> = {}; | ||
|
||
funList.forEach((item) => { | ||
if (item.key === 'success-func') { | ||
funs.onComplete = { | ||
value: item.value.toString(), | ||
type: 'JSFunction', | ||
}; | ||
} else { | ||
funs.onError = { | ||
value: item.value.toString(), | ||
type: 'JSFunction', | ||
}; | ||
} | ||
}); | ||
|
||
return funs; | ||
}; |
26 changes: 26 additions & 0 deletions
26
packages/li-editor/src/widgets/FetchDataset/ProcessingFunction/index.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@import '../../../theme/index.less'; | ||
|
||
.li-provessing-function { | ||
&__success-and-err { | ||
&__content { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
|
||
&-json { | ||
height: 80px; | ||
background-color: '#1e1e1e'; | ||
flex: 1; | ||
} | ||
|
||
&-icon { | ||
width: 30px; | ||
text-align: center; | ||
|
||
&:hover { | ||
cursor: pointer; | ||
} | ||
} | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
packages/li-editor/src/widgets/FetchDataset/ProcessingFunction/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { DownOutlined, MinusCircleOutlined } from '@ant-design/icons'; | ||
import MonacoEditor from '@monaco-editor/react'; | ||
import { Button, Dropdown } from 'antd'; | ||
import React, { useRef, useState } from 'react'; | ||
import { getValue } from './helper'; | ||
import './index.less'; | ||
|
||
type ProcessingFunctionProps = { | ||
onChange?: (funs: { | ||
onComplete?: { type: string; value: string }; | ||
onError?: { type: string; value: string }; | ||
}) => void; | ||
}; | ||
|
||
export type FuncItem = { name: string; value: any; key: string }; | ||
|
||
const ItemList = [ | ||
{ | ||
key: 'success-func', | ||
label: '请求成功对结果的处理函数', | ||
}, | ||
{ | ||
key: 'error-func', | ||
label: '请求失败对异常的处理函数', | ||
}, | ||
]; | ||
|
||
const ProcessingFunction = (props: ProcessingFunctionProps) => { | ||
const { onChange } = props; | ||
const [menus, setMenus] = useState(ItemList); | ||
const functionListRef = useRef<FuncItem[]>([]); | ||
|
||
const onFunctionListChange = (list: FuncItem[]) => { | ||
functionListRef.current = [...list]; | ||
|
||
const funs = getValue(list); | ||
if (onChange) { | ||
onChange(funs); | ||
} | ||
|
||
const selectedKeys: string[] = list.map((item) => item.key); | ||
const _items = ItemList.filter( | ||
(item) => item && item.key && typeof item.key === 'string' && !selectedKeys.includes(item.key), | ||
); | ||
setMenus(_items); | ||
}; | ||
|
||
const onAddFunc = (val: { key: string; label: string }) => { | ||
const _item = | ||
val.key === 'success-func' | ||
? { | ||
name: val.label, | ||
value: 'function(res) { return res.data }', | ||
key: val.key, | ||
} | ||
: { | ||
name: val.label, | ||
value: 'function(err) {}', | ||
key: val.key, | ||
}; | ||
|
||
const _funcList = [...functionListRef.current, _item]; | ||
onFunctionListChange(_funcList); | ||
}; | ||
|
||
const onDel = (key: string) => { | ||
const _funcList = functionListRef.current.filter((item) => item.key !== key); | ||
onFunctionListChange(_funcList); | ||
}; | ||
|
||
const handleEditorChange = (fun: any, key: string) => { | ||
const _functionList = functionListRef.current.map((item) => (item.key === key ? { ...item, value: fun } : item)); | ||
const funs = getValue(_functionList); | ||
if (onChange) { | ||
onChange(funs); | ||
} | ||
functionListRef.current = _functionList; | ||
}; | ||
|
||
return ( | ||
<div className="li-provessing-function"> | ||
<div className="li-provessing-function__btn"> | ||
<Dropdown menu={{ items: menus.map((item) => ({ ...item, onClick: () => onAddFunc(item) })) }}> | ||
<Button> | ||
选择添加 <DownOutlined /> | ||
</Button> | ||
</Dropdown> | ||
</div> | ||
|
||
{functionListRef.current.map((item: FuncItem) => { | ||
return ( | ||
<div className="li-provessing-function__success-and-err" key={item.key}> | ||
<p>{item.name}:</p> | ||
<div className="li-provessing-function__success-and-err__content"> | ||
<div className="li-provessing-function__success-and-err__content-json"> | ||
<MonacoEditor | ||
language="json" | ||
options={{ | ||
readOnly: false, | ||
minimap: { enabled: false }, | ||
lineNumbers: 'off', | ||
overviewRulerBorder: false, | ||
}} | ||
onChange={(fun) => handleEditorChange(fun, item.key)} | ||
theme="vs-dark" | ||
value={item.value} | ||
/> | ||
</div> | ||
<div className="li-provessing-function__success-and-err__content-icon"> | ||
<MinusCircleOutlined | ||
onClick={() => { | ||
onDel(item.key); | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ProcessingFunction; |