Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Block workspace modal size from block type #17501

Merged
Merged
Show file tree
Hide file tree
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
@@ -1,4 +1,4 @@
import type { UmbBlockDataModel, UmbBlockLayoutBaseModel } from '../types.js';

Check notice on line 1 in src/Umbraco.Web.UI.Client/src/packages/block/block/workspace/block-workspace.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 5.41 to 5.39, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
import { UmbBlockWorkspaceEditorElement } from './block-workspace-editor.element.js';
import { UmbBlockElementManager } from './block-element-manager.js';
import {
Expand All @@ -25,6 +25,7 @@
UMB_BLOCK_ENTRY_CONTEXT,
} from '@umbraco-cms/backoffice/block';
import { UmbVariantId } from '@umbraco-cms/backoffice/variant';
import type { UUIModalSidebarSize } from '@umbraco-cms/backoffice/external/uui';

export type UmbBlockWorkspaceElementManagerNames = 'content' | 'settings';
export class UmbBlockWorkspaceContext<LayoutDataType extends UmbBlockLayoutBaseModel = UmbBlockLayoutBaseModel>
Expand All @@ -44,6 +45,7 @@
setOriginData(data: UmbBlockWorkspaceOriginData) {
this.#originData = data;
}
#modalContext?: typeof UMB_MODAL_CONTEXT.TYPE;
#retrieveModalContext;

#entityType: string;
Expand Down Expand Up @@ -83,79 +85,96 @@
this.addValidationContext(this.settings.validation);

this.#retrieveModalContext = this.consumeContext(UMB_MODAL_CONTEXT, (context) => {
this.#modalContext = context;
this.#originData = context?.data.originData;
context.onSubmit().catch(this.#modalRejected);
}).asPromise();

this.#retrieveBlockManager = this.consumeContext(UMB_BLOCK_MANAGER_CONTEXT, (manager) => {
this.#blockManager = manager;

this.observe(
manager.liveEditingMode,
(liveEditingMode) => {
this.#liveEditingMode = liveEditingMode ?? false;
},
'observeLiveEditingMode',
);

this.observe(
observeMultiple([
manager.variantId,
this.content.structure.variesByCulture,
this.content.structure.variesBySegment,
]),
([variantId, variesByCulture, variesBySegment]) => {
if (!variantId || variesByCulture === undefined || variesBySegment === undefined) return;
if (!variesBySegment && !variesByCulture) {
variantId = UmbVariantId.CreateInvariant();
} else if (!variesBySegment) {
variantId = variantId.toSegmentInvariant();
} else if (!variesByCulture) {
variantId = variantId.toCultureInvariant();
}

this.#variantId.setValue(variantId);
},
'observeBlockType',
'observeVariantIds',
);

this.removeUmbControllerByAlias('observeHasExpose');
this.observe(
observeMultiple([this.contentKey, this.variantId]),
([contentKey, variantId]) => {
if (!contentKey || !variantId) return;

this.observe(
manager.hasExposeOf(contentKey, variantId),
(exposed) => {
this.#exposed.setValue(exposed);
},
'observeHasExpose',
);
},
'observeContentKeyAndVariantId',
);

this.observe(
observeMultiple([manager.readOnlyState.isReadOnly, this.variantId]),
([isReadOnly, variantId]) => {
const unique = 'UMB_BLOCK_MANAGER_CONTEXT';
if (variantId === undefined) return;

if (isReadOnly) {
const state = {
unique,
variantId,
message: '',
};

this.readOnlyState?.addState(state);
} else {
this.readOnlyState?.removeState(unique);
}
},
'observeIsReadOnly',
);

this.observe(
this.content.contentTypeId,
(contentTypeId) => {
this.observe(
contentTypeId ? manager.blockTypeOf(contentTypeId) : undefined,
(blockType) => {
if (blockType?.editorSize) {
this.setEditorSize(blockType.editorSize);
}
},
'observeBlockType',
);
},
'observeContentTypeId',
);

Check warning on line 177 in src/Umbraco.Web.UI.Client/src/packages/block/block/workspace/block-workspace.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ Getting worse: Complex Method

UmbBlockWorkspaceContext.constructor increases in cyclomatic complexity from 17 to 20, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
});

this.#retrieveBlockEntries = this.consumeContext(UMB_BLOCK_ENTRIES_CONTEXT, (context) => {
Expand Down Expand Up @@ -200,6 +219,10 @@
]);
}

setEditorSize(editorSize: UUIModalSidebarSize) {
this.#modalContext?.setModalSize(editorSize);
}

protected override resetState() {
super.resetState();
this.#name.setValue(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type UUIDialogElement,
type UUIModalDialogElement,
type UUIModalSidebarElement,
type UUIModalSidebarSize,
} from '@umbraco-cms/backoffice/external/uui';
import { UMB_ROUTE_CONTEXT, type UmbRouterSlotElement } from '@umbraco-cms/backoffice/router';
import { createExtensionElement, loadManifestElement } from '@umbraco-cms/backoffice/extension-api';
Expand Down Expand Up @@ -117,7 +118,13 @@ export class UmbModalElement extends UmbLitElement {

#createSidebarElement() {
const sidebarElement = document.createElement('uui-modal-sidebar');
sidebarElement.size = this.#modalContext!.size;
this.observe(
this.#modalContext!.size,
(size) => {
sidebarElement.size = size as UUIModalSidebarSize;
},
'observeSize',
);
return sidebarElement;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import type { UmbModalToken } from '../token/modal-token.js';
import { UmbModalContext, type UmbModalContextClassArgs } from './modal.context.js';
import type { UUIModalElement, UUIModalSidebarSize } from '@umbraco-cms/backoffice/external/uui';
import { UmbBasicState, appendToFrozenArray } from '@umbraco-cms/backoffice/observable-api';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { ElementLoaderProperty } from '@umbraco-cms/backoffice/extension-api';

export type UmbModalType = 'dialog' | 'sidebar' | 'custom';

export interface UmbModalConfig {
key?: string;
type?: UmbModalType;
size?: UUIModalSidebarSize;

/**
* Used to provide a custom modal element to replace the default uui-modal-dialog or uui-modal-sidebar
*/
element?: ElementLoaderProperty<UUIModalElement>;

/**
* Set the background property of the modal backdrop
*/
backdropBackground?: string;
}

export class UmbModalManagerContext extends UmbContextBase<UmbModalManagerContext> {
// TODO: Investigate if we can get rid of HTML elements in our store, so we can use one of our states.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { UmbModalToken } from '../token/modal-token.js';

Check notice on line 1 in src/Umbraco.Web.UI.Client/src/packages/core/modal/context/modal.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ No longer an issue: Overall Code Complexity

The mean cyclomatic complexity in this module is no longer above the threshold
import type { UmbModalConfig, UmbModalType } from './modal-manager.context.js';
import type { UmbModalConfig, UmbModalType } from '../types.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { IRouterSlot } from '@umbraco-cms/backoffice/external/router-slot';
import type { UUIModalElement, UUIModalSidebarSize } from '@umbraco-cms/backoffice/external/uui';
import { UmbId } from '@umbraco-cms/backoffice/id';
import { UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import { UmbObjectState, UmbStringState } from '@umbraco-cms/backoffice/observable-api';
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import { type UmbDeepPartialObject, umbDeepMerge } from '@umbraco-cms/backoffice/utils';
import type { ElementLoaderProperty } from '@umbraco-cms/backoffice/extension-api';
Expand Down Expand Up @@ -38,15 +38,17 @@
public readonly key: string;
public readonly data: ModalData;
public readonly type: UmbModalType = 'dialog';
public readonly size: UUIModalSidebarSize = 'small';
public element?: ElementLoaderProperty<UUIModalElement>;
public readonly element?: ElementLoaderProperty<UUIModalElement>;
public readonly backdropBackground?: string;
public readonly router: IRouterSlot | null = null;
public readonly alias: string | UmbModalToken<ModalData, ModalValue>;

#value;
public readonly value;

#size = new UmbStringState<UUIModalSidebarSize>('small');
public readonly size = this.#size.asObservable();

constructor(
host: UmbControllerHost,
modalAlias: string | UmbModalToken<ModalData, ModalValue>,
Expand All @@ -57,18 +59,24 @@
this.router = args.router ?? null;
this.alias = modalAlias;

let size = 'small';

if (this.alias instanceof UmbModalToken) {
this.type = this.alias.getDefaultModal()?.type || this.type;
this.size = this.alias.getDefaultModal()?.size || this.size;
size = this.alias.getDefaultModal()?.size ?? size;
this.element = this.alias.getDefaultModal()?.element || this.element;
this.backdropBackground = this.alias.getDefaultModal()?.backdropBackground || this.backdropBackground;
}

this.type = args.modal?.type || this.type;
this.size = args.modal?.size || this.size;
size = args.modal?.size ?? size;
this.element = args.modal?.element || this.element;
this.backdropBackground = args.modal?.backdropBackground || this.backdropBackground;

console.log('size', size);

this.#size.setValue(size);

Check notice on line 79 in src/Umbraco.Web.UI.Client/src/packages/core/modal/context/modal.context.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Complex Method

constructor decreases in cyclomatic complexity from 24 to 22, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
const defaultData = this.alias instanceof UmbModalToken ? this.alias.getDefaultData() : undefined;
this.data = Object.freeze(
// If we have both data and defaultData perform a deep merge
Expand Down Expand Up @@ -153,6 +161,16 @@
this.#value.update(partialValue);
}

/**
* Updates the size this modal.
* @param size
* @public
* @memberof UmbModalContext
*/
setModalSize(size: UUIModalSidebarSize) {
this.#size.setValue(size);
}

public override destroy(): void {
this.dispatchEvent(new CustomEvent('umb:destroy'));
this.#value.destroy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { UmbModalConfig } from '../context/modal-manager.context.js';
import type { UmbModalConfig } from '../types.js';

export interface UmbModalTokenDefaults<
ModalDataType extends { [key: string]: any } = { [key: string]: any },
Expand Down
21 changes: 21 additions & 0 deletions src/Umbraco.Web.UI.Client/src/packages/core/modal/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { UUIModalElement, UUIModalSidebarSize } from '@umbraco-cms/backoffice/external/uui';
import type { ElementLoaderProperty } from '@umbraco-cms/backoffice/extension-api';

export type * from './extensions/index.js';

export interface UmbPickerModalData<ItemType> {
Expand All @@ -15,3 +18,21 @@ export interface UmbPickerModalSearchConfig {
export interface UmbPickerModalValue {
selection: Array<string | null>;
}

export type UmbModalType = 'dialog' | 'sidebar' | 'custom';

export interface UmbModalConfig {
key?: string;
type?: UmbModalType;
size?: UUIModalSidebarSize;

/**
* Used to provide a custom modal element to replace the default uui-modal-dialog or uui-modal-sidebar
*/
element?: ElementLoaderProperty<UUIModalElement>;

/**
* Set the background property of the modal backdrop
*/
backdropBackground?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export class UmbPropertyEditorUIOverlaySizeElement extends UmbLitElement impleme
@property()
value: UUIModalSidebarSize | string = '';

// TODO: Stop having global type of the UUI-SELECT element. And make it possible to have undefined as an option.
@state()
private _list: Array<Option> = [
{ value: 'small', name: 'Small', selected: true },
{ value: undefined as any, name: 'Default', selected: true },
{ value: 'small', name: 'Small' },
{ value: 'medium', name: 'Medium' },
{ value: 'large', name: 'Large' },
{ value: 'full', name: 'Full' },
Expand Down
Loading