Fix issue with newly created template under an existing one (#19669)

* Fix issue with newly created template under an existing one.

* feat: allows to set masterTemplate as preset

* fix: create new sub-templates with a preset already set for the master template (if applicable)

* fix: always resets master template, because you could be coming from an existing editor

* fix: always set the master template even if it is null

* fix: adds updateCurrent to also update the underlying _data model

also refactor function a bit

---------

Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>
This commit is contained in:
Andy Butland
2025-09-03 15:17:20 +02:00
committed by GitHub
parent 355f5c373f
commit f5322b5bee
2 changed files with 22 additions and 18 deletions

View File

@@ -43,7 +43,7 @@ export class UmbTemplateServerDataSource implements UmbDetailDataSource<UmbTempl
name: '',
alias: '',
content: scaffold,
masterTemplate: null,
masterTemplate: preset.masterTemplate ?? null,
...preset,
};

View File

@@ -13,6 +13,7 @@ import {
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { IRoutingInfo, PageComponent } from '@umbraco-cms/backoffice/router';
import type { UmbEntityModel } from '@umbraco-cms/backoffice/entity';
export class UmbTemplateWorkspaceContext
extends UmbEntityNamedDetailWorkspaceContextBase<UmbTemplateDetailModel, UmbTemplateDetailRepository>
@@ -72,13 +73,15 @@ export class UmbTemplateWorkspaceContext
return response;
}
async create(parent: any) {
const data = await this.createScaffold({ parent });
async create(parent: UmbEntityModel) {
const data = await this.createScaffold({
parent,
});
// Set or reset the master template
// This is important to reset when a new template is created so the UI reflects the correct state
await this.setMasterTemplate(parent.unique);
if (data) {
if (!parent) return;
await this.setMasterTemplate(parent.unique);
}
return data;
}
@@ -98,20 +101,21 @@ export class UmbTemplateWorkspaceContext
return this.getData()?.content ? this.getLayoutBlockRegexPattern().test(this.getData()?.content as string) : false;
}
async setMasterTemplate(id: string | null) {
if (id === null) {
async setMasterTemplate(unique: string | null) {
if (unique === null) {
this.#masterTemplate.setValue(null);
this.#updateMasterTemplateLayoutBlock();
return null;
} else {
// We need the whole template model if the unique id is provided
const { data } = await this.itemRepository.requestItems([unique]);
if (data) {
this.#masterTemplate.setValue(data[0]);
}
}
const { data } = await this.itemRepository.requestItems([id]);
if (data) {
this.#masterTemplate.setValue(data[0]);
this.#updateMasterTemplateLayoutBlock();
return data[0];
}
return null;
this.#updateMasterTemplateLayoutBlock();
this._data.updateCurrent({ masterTemplate: unique ? { unique } : null });
return unique;
}
#updateMasterTemplateLayoutBlock = () => {