Adds prependCondition and prependConditions so you can make sure your condition loads first, as order of conditions are important

This commit is contained in:
Warren Buckley
2024-08-29 11:10:48 +00:00
committed by Niels Lyngsø
parent 3b0ba436ff
commit e57df8025c
2 changed files with 132 additions and 5 deletions

View File

@@ -554,3 +554,92 @@ describe('Append Conditions', () => {
expect(ext.conditions?.length).to.equal(3);
});
});
describe('Prepend Conditions', () => {
let extensionRegistry: UmbExtensionRegistry<any>;
let manifests: Array<ManifestWithDynamicConditions>;
beforeEach(() => {
extensionRegistry = new UmbExtensionRegistry<ManifestWithDynamicConditions>();
manifests = [
{
type: 'section',
name: 'test-section-1',
alias: 'Umb.Test.Section.1',
weight: 1,
conditions: [
{
alias: "Umb.Test.Condition.Valid"
}
]
},
{
type: 'section',
name: 'test-section-2',
alias: 'Umb.Test.Section.2',
weight: 200
},
];
manifests.forEach((manifest) => extensionRegistry.register(manifest));
extensionRegistry.register({
type: 'condition',
name: 'test-condition-invalid',
alias: 'Umb.Test.Condition.Invalid'
});
});
it('allows an extension condition to be prepended', () => {
expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Condition.Invalid')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Condition.Valid')).to.be.false;
const ext = extensionRegistry.getByAlias('Umb.Test.Section.1') as ManifestWithDynamicConditions;
expect(ext.conditions?.length).to.equal(1);
// Register new condition as if I was in my own entrypoint
extensionRegistry.register({
type: 'condition',
name: 'test-condition-valid',
alias: 'Umb.Test.Condition.Valid'
});
// Prepend the new condition to the extension
const conditionToPrepend: UmbConditionConfigBase = {
alias: 'Umb.Test.Condition.Valid'
};
extensionRegistry.prependCondition('Umb.Test.Section.1', conditionToPrepend);
// Check new condition is registered
expect(extensionRegistry.isRegistered('Umb.Test.Condition.Valid')).to.be.true;
// Verify the extension now has two conditions and the new condition is prepended
const updatedExt = extensionRegistry.getByAlias('Umb.Test.Section.1') as ManifestWithDynamicConditions;
expect(updatedExt.conditions?.length).to.equal(2);
expect(updatedExt.conditions?.[0]?.alias).to.equal('Umb.Test.Condition.Valid');
});
it('allows an extension to update with multiple prepended conditions', () => {
const ext = extensionRegistry.getByAlias('Umb.Test.Section.1') as ManifestWithDynamicConditions;
expect(ext.conditions?.length).to.equal(1);
const conditions: Array<UmbConditionConfigBase> = [
{
alias: 'Umb.Test.Condition.Valid'
},
{
alias: 'Umb.Condition.WorkspaceAlias',
match: 'Umb.Workspace.Document'
} as WorkspaceAliasConditionConfig
];
extensionRegistry.prependConditions('Umb.Test.Section.1', conditions);
const extUpdated = extensionRegistry.getByAlias('Umb.Test.Section.1') as ManifestWithDynamicConditions;
expect(extUpdated.conditions?.length).to.equal(3);
expect(extUpdated.conditions?.[0]?.alias).to.equal('Umb.Condition.WorkspaceAlias');
expect(extUpdated.conditions?.[1]?.alias).to.equal('Umb.Test.Condition.Valid');
});
});

View File

@@ -1,4 +1,9 @@
import type { ManifestBase, ManifestKind, ManifestWithDynamicConditions, UmbConditionConfigBase } from '../types/index.js';
import type {
ManifestBase,
ManifestKind,
ManifestWithDynamicConditions,
UmbConditionConfigBase,
} from '../types/index.js';
import type { SpecificManifestTypeOrManifestBase } from '../types/map.types.js';
import { UmbBasicState } from '@umbraco-cms/backoffice/observable-api';
import type { Observable } from '@umbraco-cms/backoffice/external/rxjs';
@@ -434,7 +439,7 @@ export class UmbExtensionRegistry<
distinctUntilChanged(extensionAndKindMatchArrayMemoization),
) as Observable<Array<ExtensionTypes>>;
}
/**
* Appends a new condition to an existing extension
* Useful to add a condition for example the Save And Publish workspace action shipped by core
@@ -447,12 +452,12 @@ export class UmbExtensionRegistry<
const allExtensions = this._extensions.getValue();
const extensionToUpdate = allExtensions.find((ext) => ext.alias === alias) as ManifestWithDynamicConditions;
if(extensionToUpdate === undefined) {
if (extensionToUpdate === undefined) {
console.error(`Extension with alias ${alias} not found`);
}
// Append the condition to the extensions conditions array
if (extensionToUpdate.conditions){
if (extensionToUpdate.conditions) {
extensionToUpdate.conditions.push(newCondition);
} else {
extensionToUpdate.conditions = [newCondition];
@@ -467,7 +472,40 @@ export class UmbExtensionRegistry<
* @param alias {string} - The alias of the extension to append the condition to
* @param newConditions {Array<UmbConditionConfigBase>} - A collection of conditions to append to an extension.
*/
appendConditions(alias: string, newConditions: Array<UmbConditionConfigBase>){
appendConditions(alias: string, newConditions: Array<UmbConditionConfigBase>) {
newConditions.forEach((condition) => this.appendCondition(alias, condition));
}
/**
* Prepends a new condition to an existing extension
* @param alias {string} - The alias of the extension to prepend the condition to
* @param newCondition {UmbConditionConfigBase} - The condition to prepend to the extension.
*/
prependCondition(alias: string, newCondition: UmbConditionConfigBase) {
const allExtensions = this._extensions.getValue();
const extensionToUpdate = allExtensions.find((ext) => ext.alias === alias) as ManifestWithDynamicConditions;
if (extensionToUpdate === undefined) {
console.error(`Extension with alias ${alias} not found`);
}
// Prepend the condition to the extensions conditions array
if (extensionToUpdate.conditions) {
extensionToUpdate.conditions.unshift(newCondition);
} else {
extensionToUpdate.conditions = [newCondition];
}
// Update the extensions observable
this._extensions.setValue(allExtensions);
}
/**
* Prepends a collection of conditions to an existing extension
* @param alias {string} - The alias of the extension to prepend the conditions to
* @param newConditions {Array<UmbConditionConfigBase>} - A collection of conditions to prepend to an extension.
*/
prependConditions(alias: string, newConditions: Array<UmbConditionConfigBase>) {
newConditions.forEach((condition) => this.prependCondition(alias, condition));
}
}