feat: 处理数据导入,ID更新的可控功能

This commit is contained in:
奔跑的面条 2022-08-17 16:56:58 +08:00
parent 1ee76efeb8
commit e348caaa0c
2 changed files with 30 additions and 12 deletions

View File

@ -34,13 +34,13 @@ export const useFile = () => {
// 新增 // 新增
onPositiveCallback: async () => { onPositiveCallback: async () => {
fileData = JSON.parse(fileData) fileData = JSON.parse(fileData)
await updateComponent(fileData) await updateComponent(fileData, false, true)
window['$message'].success('导入成功!') window['$message'].success('导入成功!')
}, },
// 覆盖 // 覆盖
onNegativeCallback: async () => { onNegativeCallback: async () => {
fileData = JSON.parse(fileData) fileData = JSON.parse(fileData)
await updateComponent(fileData, true) await updateComponent(fileData, true, true)
window['$message'].success('导入成功!') window['$message'].success('导入成功!')
} }
}) })

View File

@ -14,11 +14,11 @@ export const useSync = () => {
/** /**
* * * *
* @param projectData * @param projectData
* @param isSplace * @param isReplace
* @returns * @returns
*/ */
const updateComponent = async (projectData: ChartEditStorage, isSplace = false) => { const updateComponent = async (projectData: ChartEditStorage, isReplace = false, changeId = false) => {
if (isSplace) { if (isReplace) {
// 清除列表 // 清除列表
chartEditStore.componentList = [] chartEditStore.componentList = []
// 清除历史记录 // 清除历史记录
@ -47,22 +47,40 @@ export const useSync = () => {
// 组件 // 组件
if (key === ChartEditStoreEnum.COMPONENT_LIST) { if (key === ChartEditStoreEnum.COMPONENT_LIST) {
for (const comItem of projectData[key]) { for (const comItem of projectData[key]) {
// 重新创建是为了处理类种方法消失的问题 // 重新创建是为了处理类种方法消失的问题
const create = async (e: CreateComponentType, callBack?: (e: CreateComponentType) => void) => { const create = async (
_componentInstance: CreateComponentType,
callBack?: (componentInstance: CreateComponentType) => void
) => {
// 补充 class 上的方法 // 补充 class 上的方法
let newComponent: CreateComponentType = await createComponent(e.chartConfig) let newComponent: CreateComponentType = await createComponent(_componentInstance.chartConfig)
if (callBack) { if (callBack) {
callBack(Object.assign(newComponent, { ...e, id: getUUID() })) if (changeId) {
callBack(Object.assign(newComponent, { ..._componentInstance, id: getUUID() }))
} else {
callBack(Object.assign(newComponent))
}
} else { } else {
chartEditStore.addComponentList(Object.assign(newComponent, { ...e, id: getUUID() }), false, true) if (changeId) {
chartEditStore.addComponentList(
Object.assign(newComponent, { ..._componentInstance, id: getUUID() }),
false,
true
)
} else {
chartEditStore.addComponentList(Object.assign(newComponent), false, true)
}
} }
} }
if (comItem.isGroup) { if (comItem.isGroup) {
// 创建分组 // 创建分组
let groupClass = new PublicGroupConfigClass() let groupClass = new PublicGroupConfigClass()
groupClass = Object.assign(groupClass, comItem) if (changeId) {
groupClass = Object.assign(groupClass, { ...comItem, id: getUUID() })
} else {
groupClass = Object.assign(groupClass, { ...comItem })
}
// 注册子应用 // 注册子应用
const targetList: CreateComponentType[] = [] const targetList: CreateComponentType[] = []
@ -72,7 +90,7 @@ export const useSync = () => {
}) })
}) })
groupClass.groupList = targetList groupClass.groupList = targetList
// 分组插入到列表 // 分组插入到列表
chartEditStore.addComponentList(groupClass, false, true) chartEditStore.addComponentList(groupClass, false, true)
} else { } else {