fix: 修复多页面同时渲染场景查找组件优先级问题 (#9972)

This commit is contained in:
liaoxuezhi 2024-04-09 11:22:45 +08:00 committed by GitHub
parent c890d8dc1b
commit b1ea4d236b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -107,6 +107,10 @@ export interface IScopedContext {
unRegisterComponent: (component: ScopedComponentType) => void;
getComponentByName: (name: string) => ScopedComponentType;
getComponentById: (id: string) => ScopedComponentType | undefined;
getComponentByIdUnderCurrentScope: (
id: string,
ignoreScope?: IScopedContext
) => ScopedComponentType | undefined;
getComponents: () => Array<ScopedComponentType>;
reload: (target: string, ctx: RendererData) => void;
send: (target: string, ctx: RendererData) => void;
@ -183,16 +187,15 @@ function createScopedTools(
return resolved || (parent && parent.getComponentByName(name));
},
getComponentById(id: string) {
let root: AliasIScopedContext = this;
// 找到顶端scoped
while (root.parent && root.parent !== rootScopedContext) {
root = root.parent;
}
// 向下查找
getComponentByIdUnderCurrentScope(
id: string,
ignoreScope?: IScopedContext
) {
let component = undefined;
findTree([root], (item: TreeItem) =>
findTree(
[this],
(item: TreeItem) =>
item !== ignoreScope &&
item.getComponents().find((cmpt: ScopedComponentType) => {
if (filter(cmpt.props.id, cmpt.props.data) === id) {
component = cmpt;
@ -205,6 +208,34 @@ function createScopedTools(
return component;
},
getComponentById(id: string) {
let root: AliasIScopedContext = this;
let ignoreScope: AliasIScopedContext | undefined = undefined;
// 找到顶端scoped
while (root) {
// 优先从当前scope查找
// 直接跑到顶层查找,对于有历史标签一次渲染多个页面的情况,会有问题
const component = root.getComponentByIdUnderCurrentScope(
id,
ignoreScope
);
if (component) {
return component;
}
if (!root.parent || root.parent === rootScopedContext) {
break;
}
ignoreScope = root;
root = root.parent;
}
return undefined;
},
/**
*
* ${xxx}