mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-12-16 04:06:58 +08:00
fix(core): getNode 添加stict参数来表示必须知道页面片容器id才会返回页面内的节点
This commit is contained in:
parent
b3ce1a3b93
commit
83664cd440
@ -279,13 +279,13 @@ export default class EventHelper extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const toNodes = [];
|
const toNodes = [];
|
||||||
const toNode = this.app.getNode(to);
|
const toNode = this.app.getNode(to, { strict: true });
|
||||||
if (toNode) {
|
if (toNode) {
|
||||||
toNodes.push(toNode);
|
toNodes.push(toNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [, page] of this.app.pageFragments) {
|
for (const [, page] of this.app.pageFragments) {
|
||||||
const node = page.getNode(to);
|
const node = page.getNode(to, { strict: true });
|
||||||
if (node) {
|
if (node) {
|
||||||
toNodes.push(node);
|
toNodes.push(node);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -191,7 +191,7 @@ class Node extends EventEmitter {
|
|||||||
if (this.app.eventHelper) {
|
if (this.app.eventHelper) {
|
||||||
for (const eventConfig of this.app.eventHelper.getEventQueue()) {
|
for (const eventConfig of this.app.eventHelper.getEventQueue()) {
|
||||||
for (const [, page] of this.app.pageFragments) {
|
for (const [, page] of this.app.pageFragments) {
|
||||||
const node = page.getNode(eventConfig.toId);
|
const node = page.getNode(eventConfig.toId, { strict: true });
|
||||||
if (node && node === this) {
|
if (node && node === this) {
|
||||||
if (typeof instance[eventConfig.method] === 'function') {
|
if (typeof instance[eventConfig.method] === 'function') {
|
||||||
await instance[eventConfig.method](eventConfig.fromCpt, ...eventConfig.args);
|
await instance[eventConfig.method](eventConfig.fromCpt, ...eventConfig.args);
|
||||||
|
|||||||
@ -84,14 +84,16 @@ class Page extends Node {
|
|||||||
|
|
||||||
public getNode<T extends TMagicNode = TMagicNode>(
|
public getNode<T extends TMagicNode = TMagicNode>(
|
||||||
id: Id,
|
id: Id,
|
||||||
{ iteratorContainerId, iteratorIndex, pageFragmentContainerId }: GetNodeOptions = {},
|
{ iteratorContainerId, iteratorIndex, pageFragmentContainerId, strict }: GetNodeOptions = {},
|
||||||
): T | undefined {
|
): T | undefined {
|
||||||
if (this.nodes.has(id)) {
|
if (this.nodes.has(id)) {
|
||||||
return this.nodes.get(id) as T;
|
return this.nodes.get(id) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pageFragmentContainerId) {
|
if (pageFragmentContainerId) {
|
||||||
return this.app.pageFragments.get(pageFragmentContainerId)?.getNode(id, { iteratorContainerId, iteratorIndex });
|
return this.app.pageFragments
|
||||||
|
.get(pageFragmentContainerId)
|
||||||
|
?.getNode(id, { iteratorContainerId, iteratorIndex, strict: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(iteratorContainerId) && iteratorContainerId.length && Array.isArray(iteratorIndex)) {
|
if (Array.isArray(iteratorContainerId) && iteratorContainerId.length && Array.isArray(iteratorIndex)) {
|
||||||
@ -107,7 +109,7 @@ class Page extends Node {
|
|||||||
return iteratorContainer?.getNode(id, iteratorIndex[iteratorIndex.length - 1]) as T;
|
return iteratorContainer?.getNode(id, iteratorIndex[iteratorIndex.length - 1]) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.app.pageFragments.size) {
|
if (!strict && this.app.pageFragments.size) {
|
||||||
for (const [, pageFragment] of this.app.pageFragments) {
|
for (const [, pageFragment] of this.app.pageFragments) {
|
||||||
if (pageFragment.nodes.has(id)) {
|
if (pageFragment.nodes.has(id)) {
|
||||||
return pageFragment.nodes.get(id) as T;
|
return pageFragment.nodes.get(id) as T;
|
||||||
|
|||||||
@ -48,4 +48,6 @@ export interface GetNodeOptions {
|
|||||||
iteratorContainerId?: Id[];
|
iteratorContainerId?: Id[];
|
||||||
iteratorIndex?: number[];
|
iteratorIndex?: number[];
|
||||||
pageFragmentContainerId?: Id;
|
pageFragmentContainerId?: Id;
|
||||||
|
/** 严格模式,如果为true,页面片中的节点必须指定pageFragmentContainerId,为false时,没有pageFragmentContainerId的时候获得第一个页面片容器中的节点 */
|
||||||
|
strict?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,13 +78,13 @@ export const createDataSourceManager = (app: TMagicApp, useMock?: boolean, initi
|
|||||||
replaceChildNode(newNode, [app.page.data]);
|
replaceChildNode(newNode, [app.page.data]);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.getNode(node.id)?.setData(newNode);
|
app.getNode(node.id, { strict: true })?.setData(newNode);
|
||||||
|
|
||||||
for (const [, pageFragment] of app.pageFragments) {
|
for (const [, pageFragment] of app.pageFragments) {
|
||||||
if (pageFragment.data.id === newNode.id) {
|
if (pageFragment.data.id === newNode.id) {
|
||||||
pageFragment.setData(newNode);
|
pageFragment.setData(newNode);
|
||||||
} else if (pageFragment.data.id === page.id) {
|
} else if (pageFragment.data.id === page.id) {
|
||||||
pageFragment.getNode(newNode.id)?.setData(newNode);
|
pageFragment.getNode(newNode.id, { strict: true })?.setData(newNode);
|
||||||
if (!pageFragment.instance) {
|
if (!pageFragment.instance) {
|
||||||
replaceChildNode(newNode, [pageFragment.data]);
|
replaceChildNode(newNode, [pageFragment.data]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,6 +47,7 @@ export const useNode = <T extends TMagicNode = TMagicNode>(
|
|||||||
iteratorContainerId: props.iteratorContainerId,
|
iteratorContainerId: props.iteratorContainerId,
|
||||||
iteratorIndex: props.iteratorIndex,
|
iteratorIndex: props.iteratorIndex,
|
||||||
pageFragmentContainerId: props.pageFragmentContainerId,
|
pageFragmentContainerId: props.pageFragmentContainerId,
|
||||||
|
strict: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return void 0;
|
return void 0;
|
||||||
|
|||||||
@ -82,7 +82,7 @@ export const useFormConfig = (props: AppProps) => {
|
|||||||
if (!parent) throw new Error('未找到父节点');
|
if (!parent) throw new Error('未找到父节点');
|
||||||
|
|
||||||
if (config.type !== 'page') {
|
if (config.type !== 'page') {
|
||||||
const parentNode = app?.page?.getNode(parent.id);
|
const parentNode = app?.page?.getNode(parent.id, { strict: true });
|
||||||
parentNode && app?.page?.initNode(config, parentNode);
|
parentNode && app?.page?.initNode(config, parentNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ export const useFormConfig = (props: AppProps) => {
|
|||||||
const newNode = app.dataSourceManager?.compiledNode(config) || config;
|
const newNode = app.dataSourceManager?.compiledNode(config) || config;
|
||||||
replaceChildNode(reactive(newNode), [root.value], parentId);
|
replaceChildNode(reactive(newNode), [root.value], parentId);
|
||||||
|
|
||||||
const nodeInstance = app.page?.getNode(config.id);
|
const nodeInstance = app.page?.getNode(config.id, { strict: true });
|
||||||
if (nodeInstance) {
|
if (nodeInstance) {
|
||||||
nodeInstance.setData(config);
|
nodeInstance.setData(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,7 @@ export const useNode = <T extends TMagicNode = TMagicNode>(
|
|||||||
iteratorContainerId: props.iteratorContainerId,
|
iteratorContainerId: props.iteratorContainerId,
|
||||||
iteratorIndex: props.iteratorIndex,
|
iteratorIndex: props.iteratorIndex,
|
||||||
pageFragmentContainerId: props.pageFragmentContainerId,
|
pageFragmentContainerId: props.pageFragmentContainerId,
|
||||||
|
strict: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return void 0;
|
return void 0;
|
||||||
|
|||||||
@ -74,7 +74,7 @@ export const useEditorDsl = (app = inject<TMagicApp>('app'), win = window) => {
|
|||||||
if (!parent) throw new Error('未找到父节点');
|
if (!parent) throw new Error('未找到父节点');
|
||||||
|
|
||||||
if (config.type !== 'page') {
|
if (config.type !== 'page') {
|
||||||
const parentNode = app?.page?.getNode(parent.id);
|
const parentNode = app?.page?.getNode(parent.id, { strict: true });
|
||||||
parentNode && app?.page?.initNode(config, parentNode);
|
parentNode && app?.page?.initNode(config, parentNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ export const useEditorDsl = (app = inject<TMagicApp>('app'), win = window) => {
|
|||||||
|
|
||||||
replaceChildNode(reactive(newNode), [root.value], parentId);
|
replaceChildNode(reactive(newNode), [root.value], parentId);
|
||||||
|
|
||||||
const nodeInstance = app.getNode(config.id);
|
const nodeInstance = app.getNode(config.id, { strict: true });
|
||||||
if (nodeInstance) {
|
if (nodeInstance) {
|
||||||
nodeInstance.setData(newNode);
|
nodeInstance.setData(newNode);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user