-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
2d7faba
commit f110613
Showing
9 changed files
with
129 additions
and
39 deletions.
There are no files selected for viewing
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
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
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
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
71 changes: 71 additions & 0 deletions
71
web/apps/web/src/components/app/nodes/state-node/utils.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,71 @@ | ||
import { FieldValues } from '@shellagent/ui'; | ||
import { uuid } from '@shellagent/flow-engine'; | ||
import { generateUUID } from '@/utils/common-helper'; | ||
import { produce } from 'immer'; | ||
import { IButtonType } from '@/components/app/node-form/widgets/button-editor'; | ||
|
||
export const initData = (data: FieldValues) => { | ||
const newData = produce(data, draft => { | ||
// 更新 render.buttons 下的 id 和 on_click | ||
if (draft?.render?.buttons) { | ||
draft.render.buttons = draft?.render?.buttons?.map( | ||
(button: IButtonType) => ({ | ||
...button, | ||
id: generateUUID(), | ||
on_click: { | ||
event: '', | ||
payload: {}, | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
// 更新 input 和 output 中的 key | ||
const updateKeys = (obj: { [key: string]: FieldValues }) => { | ||
const keyMap: { [oldKey: string]: string } = {}; | ||
|
||
Object.entries(obj).forEach(([key, value], index) => { | ||
const newKey = uuid(index); | ||
keyMap[key] = newKey; | ||
delete obj[key]; | ||
obj[newKey] = value; | ||
}); | ||
|
||
return keyMap; | ||
}; | ||
|
||
const replaceKeyInData = ( | ||
data: any, | ||
keyMap: { [oldKey: string]: string }, | ||
): any => { | ||
if (typeof data === 'string') { | ||
for (const oldKey in keyMap) { | ||
data = data.replace( | ||
new RegExp(`{{.*(${oldKey})(.*)}}`, 'g'), | ||
`{{${keyMap[oldKey]}}}`, | ||
); | ||
} | ||
return data; | ||
} else if (Array.isArray(data)) { | ||
return data.map(item => replaceKeyInData(item, keyMap)); | ||
} else if (typeof data === 'object' && data !== null) { | ||
for (const k in data) { | ||
data[k] = replaceKeyInData(data[k], keyMap); | ||
} | ||
} | ||
return data; | ||
}; | ||
|
||
const inputKeyMap = updateKeys(draft?.input || {}); | ||
const outputKeyMap = updateKeys(draft?.output || {}); | ||
|
||
const combinedKeyMap = { ...inputKeyMap, ...outputKeyMap }; | ||
|
||
draft.input = replaceKeyInData(draft?.input || {}, combinedKeyMap); | ||
draft.output = replaceKeyInData(draft?.output || {}, combinedKeyMap); | ||
draft.render = replaceKeyInData(draft?.render || {}, combinedKeyMap); | ||
draft.blocks = replaceKeyInData(draft?.blocks || [], combinedKeyMap); | ||
}); | ||
|
||
return newData; | ||
}; |
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
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
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
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