From d5f6f39e6c461ca10b73e920fad6e7be47cb164c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Mon, 15 Apr 2024 10:53:36 +0200 Subject: [PATCH] exclusion feature --- .../registry/extension.registry.test.ts | 85 +++++++++++++++++++ .../registry/extension.registry.ts | 14 +++ 2 files changed, 99 insertions(+) diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.test.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.test.ts index 68e00ddbd7..2a801af75c 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.test.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.test.ts @@ -368,3 +368,88 @@ describe('UmbExtensionRegistry with kinds', () => { .unsubscribe(); }); }); + +describe('UmbExtensionRegistry with exclusions', () => { + let extensionRegistry: UmbExtensionRegistry; + let manifests: Array< + ManifestElementWithElementName | TestManifestWithMeta | ManifestKind + >; + + beforeEach(() => { + extensionRegistry = new UmbExtensionRegistry(); + manifests = [ + { + type: 'kind', + alias: 'Umb.Test.Kind', + matchType: 'section', + matchKind: 'test-kind', + manifest: { + type: 'section', + elementName: 'my-kind-element', + meta: { + label: 'my-kind-meta-label', + }, + }, + }, + { + type: 'section', + kind: 'test-kind' as unknown as undefined, // We do not know about this one, so it makes good sense that its not a valid option. + name: 'test-section-1', + alias: 'Umb.Test.Section.1', + weight: 1, + meta: { + pathname: 'test-section-1', + }, + }, + { + type: 'section', + name: 'test-section-2', + alias: 'Umb.Test.Section.2', + weight: 200, + meta: { + label: 'Test Section 2', + pathname: 'test-section-2', + }, + }, + ]; + + manifests.forEach((manifest) => extensionRegistry.register(manifest)); + }); + + it('should have the extensions registered', () => { + expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true; + expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.true; + expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true; + }); + + it('must not say that Umb.Test.Section.1d is registered, when its added to the exclusion list', () => { + extensionRegistry.exclude('Umb.Test.Section.1'); + expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.false; + // But check that the other ones are still registered: + expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true; + expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true; + }); + + it('does not affect kinds when a kind-alias is put in the exclusion list', () => { + extensionRegistry.exclude('Umb.Test.Kind'); + // This had no effect, so all of them are still available. + expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true; + expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.true; + expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true; + }); + + it('prevents late comers from begin registered', () => { + extensionRegistry.exclude('Umb.Test.Section.Late'); + extensionRegistry.register({ + type: 'section', + name: 'test-section-late', + alias: 'Umb.Test.Section.Late', + weight: 200, + meta: { + label: 'Test Section Late', + pathname: 'test-section-Late', + }, + }); + expect(extensionRegistry.isRegistered('Umb.Test.Section.Late')).to.be.false; + }); +}); diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.ts index 245ddf6667..99449dbf88 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/registry/extension.registry.ts @@ -80,6 +80,7 @@ export class UmbExtensionRegistry< private _kinds = new UmbBasicState>>([]); public readonly kinds = this._kinds.asObservable(); + #exclusions: Array = []; defineKind(kind: ManifestKind): void { const extensionsValues = this._extensions.getValue(); @@ -105,6 +106,15 @@ export class UmbExtensionRegistry< this._kinds.setValue(nextData); } + exclude(alias: string): void { + this.#exclusions.push(alias); + this._extensions.setValue(this._extensions.getValue().filter(this.#acceptExtension)); + } + + #acceptExtension = (ext: ManifestTypes): boolean => { + return !this.#exclusions.includes(ext.alias); + }; + register(manifest: ManifestTypes | ManifestKind): void { const isValid = this.#checkExtension(manifest); if (!isValid) { @@ -163,6 +173,10 @@ export class UmbExtensionRegistry< return false; } + if (!this.#acceptExtension(manifest as ManifestTypes)) { + return false; + } + const extensionsValues = this._extensions.getValue(); const extension = extensionsValues.find((extension) => extension.alias === (manifest as ManifestTypes).alias);