From 2031b4f8420c38b7c4f60813993b0701034a6060 Mon Sep 17 00:00:00 2001 From: Markus Johansson Date: Fri, 5 Apr 2024 21:29:12 +0200 Subject: [PATCH] Worked on content type condition --- .../entity-content-type-condition/manifest.ts | 23 +++++++ .../workspace-view.element.ts | 20 ++++++ .../extension-registry/conditions/types.ts | 6 +- .../core/workspace/conditions/index.ts | 1 + .../core/workspace/conditions/manifests.ts | 2 + ...workspace-entity-content-type.condition.ts | 65 +++++++++++++++++++ ...th-content-type-workspace-context-token.ts | 9 +++ ...ontent-type-workspace-context.interface.ts | 10 +++ .../core/workspace/contexts/tokens/index.ts | 2 + .../workspace/document-workspace.context.ts | 3 +- 10 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/manifest.ts create mode 100644 src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/workspace-view.element.ts create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/workspace-entity-content-type.condition.ts create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context-token.ts create mode 100644 src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context.interface.ts diff --git a/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/manifest.ts b/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/manifest.ts new file mode 100644 index 0000000000..1afcaef8d1 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/manifest.ts @@ -0,0 +1,23 @@ +import { ManifestWorkspaceView } from '@umbraco-cms/backoffice/extension-registry'; + +const workspace: ManifestWorkspaceView = { + type: 'workspaceView', + alias: 'Example.WorkspaceView.EntityContentTypeCondition', + name: "Example Workspace View With Entity Content Type Condition", + element : () => import('./workspace-view.element.js'), + meta: { + icon : 'icon-bus', + label : 'Conditional', + pathname : 'conditional' + }, + conditions : [ + { + alias : 'Umb.Condition.EntityContentType', + oneOf : ['29643452-cff9-47f2-98cd-7de4b6807681','media-type-1-id'] + } + ] +}; + +export const manifests = [ + workspace +] \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/workspace-view.element.ts b/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/workspace-view.element.ts new file mode 100644 index 0000000000..eaa81ba3b9 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/examples/entity-content-type-condition/workspace-view.element.ts @@ -0,0 +1,20 @@ +import { css, html } from 'lit'; +import { customElement } from 'lit/decorators.js'; +import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; + +@customElement('umb-example-entity-content-type-condition') +export class UmbWorkspaceViewElement extends UmbLitElement { + render() { + return html`

This is a conditional element that is only shown in workspaces based on it's entities content type.

`; + } + + static styles = [css``]; +} + +export default UmbWorkspaceViewElement + +declare global { + interface HTMLElementTagNameMap { + 'umb-example-entity-content-type-condition': UmbWorkspaceViewElement; + } +} \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/conditions/types.ts b/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/conditions/types.ts index 0481a6cb5f..f5b4f38de1 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/conditions/types.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/extension-registry/conditions/types.ts @@ -5,6 +5,7 @@ import type { SectionAliasConditionConfig } from './section-alias.condition.js'; import type { SwitchConditionConfig } from './switch.condition.js'; import type { WorkspaceAliasConditionConfig, + WorkspaceEntityContentTypeConditionConfig, WorkspaceEntityTypeConditionConfig, } from '@umbraco-cms/backoffice/workspace'; import type { UmbConditionConfigBase } from '@umbraco-cms/backoffice/extension-api'; @@ -22,7 +23,7 @@ export type BlockWorkspaceHasSettingsConditionConfig = export type BlockEntryShowContentEditConditionConfig = UmbConditionConfigBase<'Umb.Condition.BlockEntryShowContentEdit'>; -export type ConditionTypes = + export type ConditionTypes = | BlockEntryShowContentEditConditionConfig | BlockWorkspaceHasSettingsConditionConfig | CollectionAliasConditionConfig @@ -33,4 +34,5 @@ export type ConditionTypes = | UmbSectionUserPermissionConditionConfig | WorkspaceAliasConditionConfig | WorkspaceEntityTypeConditionConfig - | UmbConditionConfigBase; + | UmbConditionConfigBase + | WorkspaceEntityContentTypeConditionConfig; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/index.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/index.ts index 028680ae5f..514936ff54 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/index.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/index.ts @@ -1,3 +1,4 @@ export type { WorkspaceAliasConditionConfig } from './workspace-alias.condition.js'; export type { WorkspaceEntityTypeConditionConfig } from './workspace-entity-type.condition.js'; export type { WorkspaceHasCollectionConditionConfig } from './workspace-has-collection.condition.js'; +export type { WorkspaceEntityContentTypeConditionConfig } from './workspace-entity-content-type.condition.js'; \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/manifests.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/manifests.ts index 78ced4ccc8..533329bfb9 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/manifests.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/manifests.ts @@ -1,10 +1,12 @@ import { manifest as workspaceAliasCondition } from './workspace-alias.condition.js'; import { manifest as workspaceEntityTypeCondition } from './workspace-entity-type.condition.js'; import { manifest as workspaceHasCollectionCondition } from './workspace-has-collection.condition.js'; +import { manifest as workspaceEntityContentTypeCondition } from './workspace-entity-content-type.condition.js'; import type { ManifestTypes } from '@umbraco-cms/backoffice/extension-registry'; export const manifests: Array = [ workspaceAliasCondition, workspaceEntityTypeCondition, workspaceHasCollectionCondition, + workspaceEntityContentTypeCondition ]; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/workspace-entity-content-type.condition.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/workspace-entity-content-type.condition.ts new file mode 100644 index 0000000000..90bf0d9d1f --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/conditions/workspace-entity-content-type.condition.ts @@ -0,0 +1,65 @@ +import { UmbConditionBase } from '../../extension-registry/conditions/condition-base.controller.js'; +import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; +import type { + ManifestCondition, + UmbConditionConfigBase, + UmbConditionControllerArguments, + UmbExtensionCondition, +} from '@umbraco-cms/backoffice/extension-api'; +import { UMB_ENTITY_WITH_CONTENT_TYPE_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/workspace'; + +/** + * Condition to apply extension based on a entities content type unique + */ +export class UmbWorkspaceContentTypeCondition extends UmbConditionBase implements UmbExtensionCondition { + constructor(host: UmbControllerHost, args: UmbConditionControllerArguments) { + super(host, args); + + let permissionCheck: ((contentTypeUnique: string) => boolean) | undefined = undefined; + if (this.config.match) { + permissionCheck = (contentTypeUnique: string) => contentTypeUnique === this.config.match; + } else if (this.config.oneOf) { + permissionCheck = (contentTypeUnique: string) => this.config.oneOf!.indexOf(contentTypeUnique) !== -1; + } + + if (permissionCheck !== undefined) { + + this.consumeContext(UMB_ENTITY_WITH_CONTENT_TYPE_WORKSPACE_CONTEXT, (context) => { + + this.observe(context.contentTypeUnique,(contentTypeUnique)=> { + this.permitted = contentTypeUnique ? permissionCheck(contentTypeUnique) : false; + }, + 'workspaceContentTypeUniqueConditionObserver'); + }); + + } else { + throw new Error( + 'Condition `Umb.Condition.EntityContentType` could not be initialized properly. Either "match" or "oneOf" must be defined', + ); + } + } +} + +export type WorkspaceEntityContentTypeConditionConfig = UmbConditionConfigBase<'Umb.Condition.EntityContentType'> & { + /** + * Define the unique content type key where this extension should be available in + * + * @example + * "a1eb4175-3ec1-40ea-8dda-083df6648973" + */ + match?: string; + /** + * Define one or more content type keys that this extension should be available in + * + * @example + * ["a1eb4175-3ec1-40ea-8dda-083df6648973", "2ac00e5d-8763-42d9-a38c-adaaee02cfae"] + */ + oneOf?: Array; +}; + +export const manifest: ManifestCondition = { + type: 'condition', + name: 'Workspace Entity Content Type Condition', + alias: 'Umb.Condition.EntityContentType', + api: UmbWorkspaceContentTypeCondition, +}; diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context-token.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context-token.ts new file mode 100644 index 0000000000..8ea15f9c4d --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context-token.ts @@ -0,0 +1,9 @@ +import type { UmbWorkspaceContext } from './workspace-context.interface.js'; +import { UmbContextToken } from '@umbraco-cms/backoffice/context-api'; +import type { UmbEntityWithContentTypeWorkspaceContext } from '@umbraco-cms/backoffice/workspace'; + +export const UMB_ENTITY_WITH_CONTENT_TYPE_WORKSPACE_CONTEXT = new UmbContextToken( + 'UmbWorkspaceContext', + undefined, + (context): context is UmbEntityWithContentTypeWorkspaceContext => (context as any).contentTypeUnique !== undefined, +); diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context.interface.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context.interface.ts new file mode 100644 index 0000000000..0a24ba3c8f --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/entity-with-content-type-workspace-context.interface.ts @@ -0,0 +1,10 @@ +import type { UmbWorkspaceContext } from './workspace-context.interface.js'; +import type { Observable } from '@umbraco-cms/backoffice/external/rxjs'; + +export interface UmbEntityWithContentTypeWorkspaceContext extends UmbWorkspaceContext { + + /** + * Unique identifier for the entities content type + */ + readonly contentTypeUnique : Observable; +} diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/index.ts b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/index.ts index 69ba11bc07..df2ac1f831 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/index.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/workspace/contexts/tokens/index.ts @@ -6,6 +6,7 @@ export * from './routable-workspace.context-token.js'; export * from './submittable-workspace.context-token.js'; export * from './variant-workspace.context-token.js'; export * from './workspace.context-token.js'; +export * from './entity-with-content-type-workspace-context-token.js'; export type * from './collection-workspace-context.interface.js'; export type * from './entity-workspace-context.interface.js'; export type * from './invariant-dataset-workspace-context.interface.js'; @@ -15,3 +16,4 @@ export type * from './routable-workspace-context.interface.js'; export type * from './submittable-workspace-context.interface.js'; export type * from './variant-dataset-workspace-context.interface.js'; export type * from './workspace-context.interface.js'; +export type * from './entity-with-content-type-workspace-context.interface.js'; \ No newline at end of file diff --git a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts index fd64c6e481..37695f0d56 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/documents/documents/workspace/document-workspace.context.ts @@ -69,7 +69,8 @@ export class UmbDocumentWorkspaceContext implements UmbContentWorkspaceContext, UmbPublishableWorkspaceContext, - UmbCollectionWorkspaceContext + UmbCollectionWorkspaceContext, + UmbEntityWithContentTypeWorkspaceContext { public readonly IS_CONTENT_WORKSPACE_CONTEXT = true as const;