Templates: Retain layout from file when loading template (closes #20524) (#20529)

Retain layout from file when loading template.
This commit is contained in:
Andy Butland
2025-10-17 10:46:54 +02:00
committed by GitHub
parent 7d0adc4755
commit 65798b5722
2 changed files with 19 additions and 8 deletions

View File

@@ -102,7 +102,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement {
}
#resetMasterTemplate() {
this.#templateWorkspaceContext?.setMasterTemplate(null);
this.#templateWorkspaceContext?.setMasterTemplate(null, true);
}
#openMasterTemplatePicker() {
@@ -121,7 +121,7 @@ export class UmbTemplateWorkspaceEditorElement extends UmbLitElement {
?.onSubmit()
.then((value) => {
if (!value?.selection) return;
this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null);
this.#templateWorkspaceContext?.setMasterTemplate(value.selection[0] ?? null, true);
})
.catch(() => undefined);
}

View File

@@ -67,7 +67,15 @@ export class UmbTemplateWorkspaceContext
override async load(unique: string) {
const response = await super.load(unique);
await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null);
// On load we want to set the master template details but not update the layout block in the Razor file.
// This is because you can still set a layout in code by setting `Layout = "_Layout.cshtml";` in the template file.
// This gets set automatically if you create a template under a parent, but you don't have to do that, you can
// just set the `Layout` property in the Razor template file itself.
// So even if there's no master template set by there being a parent, there may still be one set in the Razor
// code, and we shouldn't overwrite it.
await this.setMasterTemplate(response.data?.masterTemplate?.unique ?? null, false);
return response;
}
@@ -79,9 +87,9 @@ export class UmbTemplateWorkspaceContext
},
});
// 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);
// On create set or reset the master template depending on whether the template is being created under a parent.
// This is important to reset when a new template is created so the UI reflects the correct state.
await this.setMasterTemplate(parent.unique, true);
return data;
}
@@ -102,7 +110,7 @@ export class UmbTemplateWorkspaceContext
return this.getData()?.content ? this.getLayoutBlockRegexPattern().test(this.getData()?.content as string) : false;
}
async setMasterTemplate(unique: string | null) {
async setMasterTemplate(unique: string | null, updateLayoutBlock: boolean) {
if (unique === null) {
this.#masterTemplate.setValue(null);
} else {
@@ -113,7 +121,10 @@ export class UmbTemplateWorkspaceContext
}
}
this.#updateMasterTemplateLayoutBlock();
if (updateLayoutBlock) {
this.#updateMasterTemplateLayoutBlock();
}
this._data.updateCurrent({ masterTemplate: unique ? { unique } : null });
return unique;