Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
import type { UmbApiConstructorArgumentsMethodType } from '@umbraco-cms/backoffice/extension-api';
import type { UmbBlockDataType, UMB_BLOCK_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/block';
import type { UmbVariantId } from '@umbraco-cms/backoffice/variant';

import '../../../block/workspace/views/edit/block-workspace-view-edit-content-no-router.element.js';

Expand Down Expand Up @@ -54,9 +55,16 @@ export class UmbInlineListBlockElement extends UmbLitElement {
@state()
private _variantName?: string;

#hasAutoExpanded = false;

constructor() {
super();

this.#setupBlockListEntryContext();
this.#setupWorkspaceContext();
}

#setupBlockListEntryContext() {
this.consumeContext(UMB_BLOCK_LIST_ENTRY_CONTEXT, (blockContext) => {
this.#blockContext = blockContext;
this.observe(
Expand All @@ -68,7 +76,9 @@ export class UmbInlineListBlockElement extends UmbLitElement {
'observeContentKey',
);
});
}

#setupWorkspaceContext() {
new UmbExtensionApiInitializer(
this,
umbExtensionsRegistry,
Expand All @@ -77,50 +87,79 @@ export class UmbInlineListBlockElement extends UmbLitElement {
(permitted, ctrl) => {
const context = ctrl.api as typeof UMB_BLOCK_WORKSPACE_CONTEXT.TYPE;
if (permitted && context) {
this.#workspaceContext = context;
this.#workspaceContext.establishLiveSync();
this.#load();
this.#initializeWorkspaceContext(context);
}
},
);
}

#initializeWorkspaceContext(context: typeof UMB_BLOCK_WORKSPACE_CONTEXT.TYPE) {
this.#workspaceContext = context;
this.#workspaceContext.establishLiveSync();
this.#load();

this.#observeExposed();
this.#observeContentTypeName(context);
this.#observeVariant(context);

this.observe(
this.#workspaceContext.exposed,
(exposed) => {
this._exposed = exposed;
},
'observeExposed',
);

this.observe(
context.content.structure.ownerContentTypeName,
(name) => {
this._ownerContentTypeName = name;
},
'observeContentTypeName',
);

this.observe(
context.variantId,
async (variantId) => {
if (variantId) {
context.content.setup(this, variantId);
// TODO: Support segment name?
const culture = variantId.culture;
if (culture) {
const languageRepository = new UmbLanguageItemRepository(this);
const { data } = await languageRepository.requestItems([culture]);
const name = data?.[0].name;
this._variantName = name ? this.localize.string(name) : undefined;
}
}
},
'observeVariant',
);

new UmbExtensionsApiInitializer(this, umbExtensionsRegistry, 'workspaceContext', [this.#workspaceContext]);
new UmbExtensionsApiInitializer(this, umbExtensionsRegistry, 'workspaceContext', [this.#workspaceContext]);
}

#observeExposed() {
this.observe(
this.#workspaceContext!.exposed,
(exposed) => {
this._exposed = exposed;
// If block is newly created (not exposed yet) and we haven't auto-expanded yet, expand it automatically
// This restores the Umbraco 13 behavior where newly added blocks are expanded for immediate editing
if (exposed !== undefined && this.#shouldAutoExpand(exposed)) {
this._isOpen = true;
this.#hasAutoExpanded = true;
}
},
'observeExposed',
);
}

#shouldAutoExpand(exposed: boolean): boolean {
if (this.#hasAutoExpanded) return false;
if (exposed !== false) return false;
if (this._isOpen !== false) return false;
return true;
}

#observeContentTypeName(context: typeof UMB_BLOCK_WORKSPACE_CONTEXT.TYPE) {
this.observe(
context.content.structure.ownerContentTypeName,
(name) => {
this._ownerContentTypeName = name;
},
'observeContentTypeName',
);
}

#observeVariant(context: typeof UMB_BLOCK_WORKSPACE_CONTEXT.TYPE) {
this.observe(
context.variantId,
async (variantId) => {
if (!variantId) return;

context.content.setup(this, variantId);
await this.#updateVariantName(variantId);
},
'observeVariant',
);
}

async #updateVariantName(variantId: UmbVariantId) {
if (!variantId.culture) return;

const languageRepository = new UmbLanguageItemRepository(this);
const { data } = await languageRepository.requestItems([variantId.culture]);
const name = data?.[0].name;
this._variantName = name ? this.localize.string(name) : undefined;
}

#load() {
if (!this.#workspaceContext || !this.#contentKey) return;
this.#workspaceContext.load(this.#contentKey);
Expand Down
Loading