Update to use content type alias, removed custom interface and token, update to use property structure workspace

This commit is contained in:
Markus Johansson
2024-06-04 23:31:05 +02:00
parent 4242010d3e
commit 0334f63809
4 changed files with 23 additions and 44 deletions

View File

@@ -12,12 +12,13 @@ const workspace: ManifestWorkspaceView = {
},
conditions : [
{
alias : 'Umb.Condition.EntityContentType',
oneOf : ['29643452-cff9-47f2-98cd-7de4b6807681','media-type-1-id']
alias : 'Umb.Condition.WorkspaceContentTypeAlias',
//match : 'blogPost'
oneOf : ['blogPost','mediaType1']
}
]
};
export const manifests = [
workspace
]
]

View File

@@ -1,65 +1,62 @@
import { UmbConditionBase } from '../../extension-registry/conditions/condition-base.controller.js';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UMB_PROPERTY_STRUCTURE_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/workspace';
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
* Condition to apply workspace extension based on a content type alias
*/
export class UmbWorkspaceEntityContentTypeCondition extends UmbConditionBase<WorkspaceEntityContentTypeConditionConfig> implements UmbExtensionCondition {
export class UmbWorkspaceContentTypeAliasCondition extends UmbConditionBase<WorkspaceEntityContentTypeConditionConfig> implements UmbExtensionCondition {
constructor(host: UmbControllerHost, args: UmbConditionControllerArguments<WorkspaceEntityContentTypeConditionConfig>) {
super(host, args);
let permissionCheck: ((contentTypeUnique: string) => boolean) | undefined = undefined;
let permissionCheck: ((contentTypeAliases: string[]) => boolean) | undefined = undefined;
if (this.config.match) {
permissionCheck = (contentTypeUnique: string) => contentTypeUnique === this.config.match;
permissionCheck = (contentTypeAliases: string[]) => contentTypeAliases.includes(this.config.match!);
} else if (this.config.oneOf) {
permissionCheck = (contentTypeUnique: string) => this.config.oneOf!.indexOf(contentTypeUnique) !== -1;
permissionCheck = (contentTypeAliases: string[]) => contentTypeAliases.some(item => this.config.oneOf!.includes(item));
}
if (permissionCheck !== undefined) {
this.consumeContext(UMB_ENTITY_WITH_CONTENT_TYPE_WORKSPACE_CONTEXT, (context) => {
this.observe(context.contentTypeUnique,(contentTypeUnique)=> {
this.permitted = contentTypeUnique ? permissionCheck(contentTypeUnique) : false;
this.consumeContext(UMB_PROPERTY_STRUCTURE_WORKSPACE_CONTEXT, (context) => {
this.observe(context.structure.contentTypeAliases,(contentTypeAliases) => {
this.permitted = contentTypeAliases ? permissionCheck(contentTypeAliases) : false;
},
'workspaceContentTypeUniqueConditionObserver');
'workspaceContentTypeAliasConditionObserver');
});
} else {
throw new Error(
'Condition `Umb.Condition.EntityContentType` could not be initialized properly. Either "match" or "oneOf" must be defined',
'Condition `Umb.Condition.WorkspaceContentTypeAlias` could not be initialized properly. Either "match" or "oneOf" must be defined',
);
}
}
}
export type WorkspaceEntityContentTypeConditionConfig = UmbConditionConfigBase<'Umb.Condition.EntityContentType'> & {
export type WorkspaceEntityContentTypeConditionConfig = UmbConditionConfigBase<'Umb.Condition.WorkspaceContentTypeAlias'> & {
/**
* Define the unique content type key where this extension should be available in
* Define a content type alias in which workspace this extension should be available
*
* @example
* "a1eb4175-3ec1-40ea-8dda-083df6648973"
* Depends on implementation, but i.e. "article", "image", "blockPage"
*/
match?: string;
/**
* Define one or more content type keys that this extension should be available in
* Define one or more content type aliases in which workspace this extension should be available
*
* @example
* ["a1eb4175-3ec1-40ea-8dda-083df6648973", "2ac00e5d-8763-42d9-a38c-adaaee02cfae"]
* ["article", "image", "blockPage"]
*/
oneOf?: Array<string>;
};
export const manifest: ManifestCondition = {
type: 'condition',
name: 'Workspace Entity Content Type Condition',
alias: 'Umb.Condition.EntityContentType',
api: UmbWorkspaceEntityContentTypeCondition,
name: 'Workspace Content Type Alias Condition',
alias: 'Umb.Condition.WorkspaceContentTypeAlias',
api: UmbWorkspaceContentTypeAliasCondition,
};

View File

@@ -1,9 +0,0 @@
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, UmbEntityWithContentTypeWorkspaceContext>(
'UmbWorkspaceContext',
undefined,
(context): context is UmbEntityWithContentTypeWorkspaceContext => (context as any).contentTypeUnique !== undefined,
);

View File

@@ -1,10 +0,0 @@
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<string | undefined>;
}