work on context proxy and conditions

This commit is contained in:
Niels Lyngsø
2024-01-04 11:36:58 +01:00
parent 6c1ebc5081
commit 39f739ecca
4 changed files with 49 additions and 12 deletions

View File

@@ -1,3 +1,6 @@
import { UMB_BLOCK_GRID_TYPE_WORKSPACE_ALIAS } from '../../block-grid/workspace/index.js';
import { UMB_BLOCK_LIST_TYPE_WORKSPACE_ALIAS } from '../../block-list/workspace/index.js';
import { UMB_BLOCK_RTE_TYPE_WORKSPACE_ALIAS } from '../../block-rte/workspace/index.js';
import { UmbSaveWorkspaceAction } from '@umbraco-cms/backoffice/workspace';
import type { ManifestWorkspaceAction } from '@umbraco-cms/backoffice/extension-registry';
@@ -12,5 +15,15 @@ export const manifests: Array<ManifestWorkspaceAction> = [
look: 'primary',
color: 'positive',
},
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
oneOf: [
UMB_BLOCK_GRID_TYPE_WORKSPACE_ALIAS,
UMB_BLOCK_LIST_TYPE_WORKSPACE_ALIAS,
UMB_BLOCK_RTE_TYPE_WORKSPACE_ALIAS,
],
},
],
},
];

View File

@@ -53,7 +53,7 @@ export class UmbModalElement extends UmbLitElement {
// We do not currently have a good enough control to ensure that the proxy is last, meaning if another context is provided at this element, it might respond after the proxy event has been dispatched.
// To avoid such this hack just prevents proxying the event if its a request for the Modal Context.
if (event.apiAlias !== UMB_MODAL_CONTEXT_TOKEN.contextAlias) {
event.stopPropagation();
event.stopImmediatePropagation();
const clonedEvent = new UmbContextRequestEventImplementation(
event.contextAlias,
event.apiAlias,

View File

@@ -1,4 +1,4 @@
import { UMB_WORKSPACE_CONTEXT } from './workspace-context/index.js';
import { UMB_WORKSPACE_CONTEXT, type UmbWorkspaceContextInterface } from './workspace-context/index.js';
import { UmbBaseController } from '@umbraco-cms/backoffice/class-api';
import {
ManifestCondition,
@@ -16,10 +16,25 @@ export class UmbWorkspaceAliasCondition extends UmbBaseController implements Umb
super(args.host);
this.config = args.config;
this.#onChange = args.onChange;
this.consumeContext(UMB_WORKSPACE_CONTEXT, (context) => {
this.permitted = context.workspaceAlias === this.config.match;
this.#onChange();
});
let permissionCheck: ((context: UmbWorkspaceContextInterface) => boolean) | undefined = undefined;
if (this.config.match) {
permissionCheck = (context: UmbWorkspaceContextInterface) => context.workspaceAlias === this.config.match;
} else if (this.config.oneOf) {
permissionCheck = (context: UmbWorkspaceContextInterface) =>
this.config.oneOf!.indexOf(context.workspaceAlias) !== -1;
}
if (permissionCheck !== undefined) {
this.consumeContext(UMB_WORKSPACE_CONTEXT, (context) => {
this.permitted = permissionCheck!(context);
this.#onChange();
});
} else {
throw new Error(
'Condition `Umb.Condition.WorkspaceAlias` could not be initialized properly. Either "match" or "oneOf" must be defined',
);
}
}
}
@@ -30,7 +45,14 @@ export type WorkspaceAliasConditionConfig = UmbConditionConfigBase<'Umb.Conditio
* @example
* "Umb.Workspace.Document"
*/
match: string;
match?: string;
/**
* Define one or more workspaces that this extension should be available in
*
* @example
* ["Umb.Workspace.Document", "Umb.Workspace.Media"]
*/
oneOf?: Array<string>;
};
export const manifest: ManifestCondition = {

View File

@@ -5,6 +5,8 @@ import type {
ManifestWorkspaceView,
} from '@umbraco-cms/backoffice/extension-registry';
export const UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS = 'Umb.Workspace.DocumentType';
const workspace: ManifestWorkspace = {
type: 'workspace',
alias: 'Umb.Workspace.DocumentType',
@@ -30,7 +32,7 @@ const workspaceViews: Array<ManifestWorkspaceView> = [
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
match: workspace.alias,
match: UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS,
},
],
},
@@ -48,7 +50,7 @@ const workspaceViews: Array<ManifestWorkspaceView> = [
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
match: workspace.alias,
match: UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS,
},
],
},
@@ -66,7 +68,7 @@ const workspaceViews: Array<ManifestWorkspaceView> = [
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
match: workspace.alias,
match: UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS,
},
],
},
@@ -84,7 +86,7 @@ const workspaceViews: Array<ManifestWorkspaceView> = [
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
match: workspace.alias,
match: UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS,
},
],
},
@@ -104,7 +106,7 @@ const workspaceActions: Array<ManifestWorkspaceAction> = [
conditions: [
{
alias: 'Umb.Condition.WorkspaceAlias',
match: workspace.alias,
match: UMB_DOCUMENT_TYPE_WORKSPACE_ALIAS,
},
],
},