diff --git a/packages/renderer/src/event/event-handlers-executor.ts b/packages/renderer/src/event/event-handlers-executor.ts index b2c93fd79cd7d77a3521cefad0ddf4552ba4ec2a..b599b0d7fcb5e465af7023c8a23b4fb2ed091a41 100644 --- a/packages/renderer/src/event/event-handlers-executor.ts +++ b/packages/renderer/src/event/event-handlers-executor.ts @@ -24,15 +24,22 @@ class EventHandlersExecutor { return; } - let lastResult; - for (const eventHandler of eventHandlers) { - const result = await eventHandler.handle(); - if (eventHandler.getType() === EventHandlerType.Command) { - lastResult = result; + try { + let lastResult; + for (const eventHandler of eventHandlers) { + const result = await eventHandler.handle(); + if (eventHandler.getType() === EventHandlerType.Command) { + lastResult = result; + } } - } + return lastResult; + } catch (error) { - return lastResult; + // 内置功能中已经使用了reject()来终止流,此类异常不输出错误 + if (error) { + console.error(error); + } + } } } diff --git a/packages/ui-binding/lib/compositions/use-data-grid-binding.ts b/packages/ui-binding/lib/compositions/use-data-grid-binding.ts index e4496a2cc33153e9a4765f4978219ea2300cad21..ee43d2198b25dfe465e38b0498775202534ed43a 100644 --- a/packages/ui-binding/lib/compositions/use-data-grid-binding.ts +++ b/packages/ui-binding/lib/compositions/use-data-grid-binding.ts @@ -19,24 +19,31 @@ export function useDataGridBinding(elementRef: ElementRef, options: BindingOptio const entityPath = toShortPath(); const bindingPath = `/${entityPath?.join('/')}`; if (change.type === EntityChangeType.ValueChange || change.type === EntityChangeType.Update) { + + // 值变更和更新变更:先加工路径(移除最后一段属性的路径),再比较变更 const changePaths = change.path.clone(); const nodes = changePaths.getNodes(); let node = nodes[nodes.length - 1]; + while (node.getNodeType() !== EntityPathNodeType.IdValue) { nodes.pop(); node = nodes[nodes.length - 1]; } nodes.pop(); + const changePath = '/' + nodes.filter((node: EntityPathNode) => node.getNodeType() !== EntityPathNodeType.IdValue).map((node: EntityPathNode) => node.getNodeValue()).join('/'); if (!(changePath === bindingPath || bindingPath.startsWith(changePath))) { return; } } else { + + // 非值变更和更新变更:仅关注当前节点和父节点的变更 const changePath = change && change.path.toShortPath(); if (!(changePath === bindingPath || bindingPath.startsWith(changePath))) { return; } } + const entityData: Record[] | undefined = viewModel.entityStore?.getEntityListByPath(options.entityPath).toJSON(); const datas = cloneDeep(entityData); const entities = viewModel.entityStore?.getEntityListByPath(options.entityPath).getEntities(); @@ -96,18 +103,19 @@ export function useDataGridBinding(elementRef: ElementRef, options: BindingOptio let dataSourceCurrentId: any = newCurrentEntity.idValue; const changePath = change && change.path.toShortPath(); - // 上级表行切换,触发数据重新加载,并设置新数据的当前行ID + // 更新数据 if (changePath !== bindingPath && bindingPath.startsWith(changePath)) { componentRef.value.updateDataSource(datas); const entityList = viewModel.entityStore?.getEntityListByPath(options.entityPath); dataSourceCurrentId = entityList?.getCurrentEntity().idValue; } - // 设置当前行(仅设置,不触发事件,避免死循环) - componentRef.value.activeRowById(dataSourceCurrentId); - // 更新ids变量 updateIdsState(dataSourceCurrentId); + + // 设置当前行 + selectItemById(dataSourceCurrentId); + } else if (change.type === EntityChangeType.Remove) { const removeChange = change as RemoveEntityChange; const ids = removeChange.entities.map((entity: Entity) => entity.idValue); @@ -183,7 +191,11 @@ export function useDataGridBinding(elementRef: ElementRef, options: BindingOptio const componentCurrentEntity = entityList?.getEntityById(componentCurrentId); const currentEntity = viewModel.entityStore?.getEntityListByPath(options.entityPath).getCurrentEntity(); const entity = componentCurrentEntity || currentEntity; + + // 更新数据 componentRef.value.updateDataSource(datas); + + // 更新数据源当前行(优先使用组件记录的当前行) if (componentCurrentEntity?.idValue !== currentEntity?.idValue) { viewModel.entityStore?.changeCurrentEntityByPath(options.entityPath, entity?.idValue); } @@ -213,6 +225,7 @@ export function useDataGridBinding(elementRef: ElementRef, options: BindingOptio } function selectItemById(id: string, forceSelect: boolean = false) { if (!id) { + componentRef.value.activeRowById(id); return; } const currentId = componentRef.value.getCurrentRowId(); diff --git a/packages/ui-binding/lib/compositions/use-list-view-binding.ts b/packages/ui-binding/lib/compositions/use-list-view-binding.ts index 39150329412b6ec0cce23340a511add463a79c54..6a4d4a33937471bb83d9879c8300a8c17e25e27a 100644 --- a/packages/ui-binding/lib/compositions/use-list-view-binding.ts +++ b/packages/ui-binding/lib/compositions/use-list-view-binding.ts @@ -37,10 +37,13 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio return; } } + + // 获取当前组件绑定的数据 const entityData: Record[] | undefined = viewModel.entityStore?.getEntityListByPath(options.entityPath).toJSON(); const datas = cloneDeep(entityData); const entities = viewModel.entityStore?.getEntityListByPath(options.entityPath).getEntities(); data.value = datas; + if (change.type === EntityChangeType.Load) { const serverPagination = viewModel.entityStore?.getPaginationByPath(viewModel.bindingPath); if (serverPagination && serverPagination.pageSize !== 0) { @@ -86,12 +89,12 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio selectItemById(primaryValue); } // componentRef.value.scrollToBottom(); - } else if (change.type === EntityChangeType.CurrentChange) { const newCurrentEntity: Entity = (change as any).newCurrentEntity as Entity; let dataSourceCurrentId: any = newCurrentEntity.idValue; const changePath = change && change.path.toShortPath(); - // 上级表行切换 + + // 父表行切换,更新数据 if (changePath !== bindingPath && bindingPath.startsWith(changePath)) { componentRef.value.updateDataSource(datas); const entityList = viewModel.entityStore?.getEntityListByPath(options.entityPath); @@ -103,7 +106,6 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio // 选中当前行 selectItemById(dataSourceCurrentId); - } else if (change.type === EntityChangeType.Remove) { const removeChange = change as RemoveEntityChange; @@ -120,6 +122,7 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio updateIdsState(null); return; } + updateIdsState(newCurrentId); // 选中当前行 @@ -129,16 +132,26 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio const index = getEntityIndexById(entities, newCurrentId); scrollToRowByIndex(index); } else if (change.type === EntityChangeType.ValueChange) { + + // 更新数据 componentRef.value.updateDataSource(datas); } else if (change.type === EntityChangeType.Update) { + + // 组件当前行 const componentCurrentId = componentRef.value.getCurrentRowId(); const entityList = viewModel.entityStore?.getEntityListByPath(options.entityPath); const componentCurrentEntity = entityList?.getEntityById(componentCurrentId); + + // 数据源中的当前实体 const currentEntity = viewModel.entityStore?.getEntityListByPath(options.entityPath).getCurrentEntity(); + + // 优先使用组件记录的当前行,不存在时使用数据源中的当前行 const entity = componentCurrentEntity || currentEntity; // 更新数据 componentRef.value.updateDataSource(datas); + + // 更新数据源当前行 if (componentCurrentEntity?.idValue !== currentEntity?.idValue) { viewModel.entityStore?.changeCurrentEntityByPath(options.entityPath, entity?.idValue); } @@ -168,6 +181,7 @@ export function useListViewBinding(elementRef: ElementRef, options: BindingOptio } function selectItemById(id: string, forceSelect: boolean = false) { if (!id) { + componentRef.value.activeRowById(id); return; } const currentId = componentRef.value.getCurrentRowId(); diff --git a/packages/ui-binding/lib/compositions/use-tree-grid-binding.ts b/packages/ui-binding/lib/compositions/use-tree-grid-binding.ts index c1b0bc0e7617dd9f2d10916621da613cda4c1e59..e7828392ba98eff911d81158412512563fad6876 100644 --- a/packages/ui-binding/lib/compositions/use-tree-grid-binding.ts +++ b/packages/ui-binding/lib/compositions/use-tree-grid-binding.ts @@ -61,15 +61,19 @@ export function useTreeGridBinding(elementRef: ElementRef, options: TreeBuilderB // 更新数据 componentRef.value.updateDataSource(data); + + // 更新当前行 if (rootId && isFirstRender) { - // 设置当前行 + // 首次加载:将根节点设置为当前行 selectItemById(rootId); isFirstRender = false; // 更新数据源当前行 viewModel.entityStore?.changeCurrentEntityByPath(viewModel.bindingPath, rootId); } else { + + // 非首次加载:使用数据源中的当前行 const entity = viewModel.entityStore?.getEntityListByPath(options.entityPath).getCurrentEntity(); const primaryValue = entity?.idValue; selectItemById(primaryValue); @@ -90,6 +94,8 @@ export function useTreeGridBinding(elementRef: ElementRef, options: TreeBuilderB const primaryValue = appendChange.entities[0].idValue; selectItemById(primaryValue); } else if (change.type === EntityChangeType.Remove) { + + // 更新数据 componentRef.value.updateDataSource(data); const newCurrentId = viewModel.getModule().getContext().getParam('__TREE_CURRENT_ID__'); if (!newCurrentId) { @@ -99,6 +105,7 @@ export function useTreeGridBinding(elementRef: ElementRef, options: TreeBuilderB // 设置当前行 selectItemById(newCurrentId); } else if (change.type === EntityChangeType.Update) { + const entity = viewModel.entityStore?.getEntityByPath(change.path); componentRef.value.reassignRowData(entity?.idValue, entity?.toJSON()); } else if (change.type === EntityChangeType.ValueChange) { @@ -115,6 +122,7 @@ export function useTreeGridBinding(elementRef: ElementRef, options: TreeBuilderB }; function selectItemById(id: string, forceSelect: boolean = false) { if (!id) { + componentRef.value.activeRowById(id); return; } const currentId = componentRef.value.getCurrentRowId();