diff --git a/ui/guest/src/contentController.ts b/ui/guest/src/contentController.ts index d552f4cde9..c3e8f42c3c 100644 --- a/ui/guest/src/contentController.ts +++ b/ui/guest/src/contentController.ts @@ -173,9 +173,9 @@ export function byPathFetchIfNotLoaded(path: string): Observable Boolean(paths[path])), - map((paths) => paths[path]), - map((modelId) => models$.value[modelId]) + map((paths) => models$.value[paths[path]]) ); } else { requestedPaths[path] = true; diff --git a/ui/guest/src/elementRegistry.ts b/ui/guest/src/elementRegistry.ts index 55da0fef0c..f6ce346a1f 100644 --- a/ui/guest/src/elementRegistry.ts +++ b/ui/guest/src/elementRegistry.ts @@ -146,18 +146,18 @@ export function register(payload: ElementRecordRegistration): number { : fieldId.split(',').map((str) => str.trim()); const terminator$ = deregister$.pipe(filter((_id) => _id === String(id))); - function create() { + function create(inheritanceParentModelId?: string) { // Create/register the physical record db[id] = { id, element, - modelId, + modelId: inheritanceParentModelId ?? modelId, index, label, fieldId: fieldIds, iceIds, complete: false, - inherited: fieldIds.some((fieldId) => isInheritedField(modelId, fieldId)) + inherited: fieldIds.some((fieldId) => isInheritedField(inheritanceParentModelId ?? modelId, fieldId)) }; } @@ -172,8 +172,9 @@ export function register(payload: ElementRecordRegistration): number { takeUntil(terminator$), take(1) ) - .subscribe(() => { - create(); + .subscribe((response) => { + const sourceModelId = response.craftercms.id; + create(sourceModelId); completeDeferredRegistration(id); }); } else { @@ -202,13 +203,13 @@ export function register(payload: ElementRecordRegistration): number { return id; } -export function completeDeferredRegistration(id: number): void { +export function completeDeferredRegistration(id: number, inheritanceParentModelId?: string): void { const record = db[id]; const { modelId, index, fieldId: fieldIds, iceIds } = record; if (fieldIds.length > 0) { fieldIds.forEach((fieldId) => { - const iceId = iceRegistry.register({ modelId, index, fieldId }); + const iceId = iceRegistry.register({ modelId: inheritanceParentModelId ?? modelId, index, fieldId }); if (!registry[iceId]) { registry[iceId] = []; } @@ -216,7 +217,7 @@ export function completeDeferredRegistration(id: number): void { !iceIds.includes(iceId) && iceIds.push(iceId); }); } else { - const iceId = iceRegistry.register({ modelId, index }); + const iceId = iceRegistry.register({ modelId: inheritanceParentModelId ?? modelId, index }); if (!registry[iceId]) { registry[iceId] = []; } diff --git a/ui/guest/src/react/GuestProxy.tsx b/ui/guest/src/react/GuestProxy.tsx index e67a99a039..b76e6d9dfa 100644 --- a/ui/guest/src/react/GuestProxy.tsx +++ b/ui/guest/src/react/GuestProxy.tsx @@ -385,6 +385,9 @@ export function GuestProxy() { spinner = spinner.firstChild as HTMLDivElement; const daddy = getParentElementFromICEProps(modelId, fieldId, targetIndex); + // 'daddy' element contains the model of the item that inherits field. Retrieving the model for latter usage + // when rendering the new component. + const inheritedModelId = daddy.getAttribute('data-craftercms-model-id'); // If daddy has children, get the closest one to the one that is being added, and get its width to set it // to the spinner container. @@ -426,8 +429,8 @@ export function GuestProxy() { ifrm.onload = function () { spinner.remove(); - const itemElement = ifrm.contentWindow.document.documentElement.querySelector( - `[data-craftercms-model-id="${modelId}"][data-craftercms-field-id="${fieldId}"][data-craftercms-index="${targetIndex}"]` + let itemElement = ifrm.contentWindow.document.documentElement.querySelector( + `[data-craftercms-model-id="${inheritedModelId}"][data-craftercms-field-id="${fieldId}"][data-craftercms-index="${targetIndex}"]` ); let component = document.createElement('div'); component.innerHTML = itemElement?.outerHTML; diff --git a/ui/guest/src/react/ZoneMenu.tsx b/ui/guest/src/react/ZoneMenu.tsx index 7a904af3d0..695f6128ba 100644 --- a/ui/guest/src/react/ZoneMenu.tsx +++ b/ui/guest/src/react/ZoneMenu.tsx @@ -221,8 +221,7 @@ export function ZoneMenu(props: ZoneMenuProps) { const itemModelId = isNodeSelectorItem ? nodeSelectorItemRecord.modelId : modelId; const itemFieldId = isNodeSelectorItem ? nodeSelectorItemRecord.fieldId : fieldId; const itemIndex = isNodeSelectorItem ? nodeSelectorItemRecord.index : index; - const parentModelId = getParentModelId(itemModelId, models, modelHierarchyMap); - const path = models[parentModelId ?? itemModelId].craftercms.path; + const path = models[itemModelId]?.craftercms.path ?? modelPath; return { path, itemModelId, itemFieldId, itemIndex }; };