Merge branch 'feature/entity-action-kind' of https://github.com/umbraco/Umbraco.CMS.Backoffice into feature/entity-action-kind

This commit is contained in:
Mads Rasmussen
2024-03-03 22:13:36 +01:00
7 changed files with 28 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import { createExtensionElementWithApi } from '../functions/create-extension-element-with-api.function.js';
import type { UmbApiConstructorArgumentsMethodType } from '../index.js';
import type { UmbApi } from '../models/api.interface.js';
import type { UmbExtensionRegistry } from '../registry/extension.registry.js';
import type { ManifestElementAndApi, ManifestCondition, ManifestWithDynamicConditions } from '../types/index.js';
@@ -26,7 +27,7 @@ export class UmbExtensionElementAndApiInitializer<
#defaultElement?: string;
#component?: ExtensionElementInterface;
#api?: ExtensionApiInterface;
#constructorArguments?: Array<unknown>;
#constructorArguments?: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType>;
/**
* The component that is created for this extension.
@@ -98,7 +99,7 @@ export class UmbExtensionElementAndApiInitializer<
host: UmbControllerHost,
extensionRegistry: UmbExtensionRegistry<ManifestCondition>,
alias: string,
constructorArguments: Array<unknown> | undefined,
constructorArguments: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType> | undefined,
onPermissionChanged: (isPermitted: boolean, controller: ControllerType) => void,
defaultElement?: string,
) {

View File

@@ -1,6 +1,7 @@
import type { ManifestBase } from '../types/index.js';
import type { UmbExtensionRegistry } from '../registry/extension.registry.js';
import type { SpecificManifestTypeOrManifestBase } from '../types/map.types.js';
import type { UmbApiConstructorArgumentsMethodType } from '../index.js';
import { UmbExtensionElementAndApiInitializer } from './extension-element-and-api-initializer.controller.js';
import {
type PermittedControllerType,
@@ -27,7 +28,7 @@ export class UmbExtensionsElementAndApiInitializer<
//
#extensionRegistry;
#defaultElement?: string;
#constructorArgs: Array<unknown> | undefined;
#constructorArgs: Array<unknown> | UmbApiConstructorArgumentsMethodType<ManifestType> | undefined;
#elProps?: Record<string, unknown>;
#apiProps?: Record<string, unknown>;

View File

@@ -1,15 +1,20 @@
import type { UmbApi } from '../models/api.interface.js';
import type { ManifestApi, ManifestElementAndApi } from '../types/base.types.js';
import { loadManifestApi } from './load-manifest-api.function.js';
import type { UmbApiConstructorArgumentsMethodType } from './types.js';
export async function createExtensionApi<ApiType extends UmbApi = UmbApi>(
manifest: ManifestApi<ApiType> | ManifestElementAndApi<any, ApiType>,
constructorArguments: Array<unknown> = [],
constructorArgs:
| Array<unknown>
| UmbApiConstructorArgumentsMethodType<ManifestApi<ApiType> | ManifestElementAndApi<any, ApiType>> = [],
): Promise<ApiType | undefined> {
if (manifest.api) {
const apiConstructor = await loadManifestApi<ApiType>(manifest.api);
if (apiConstructor) {
return new apiConstructor(...constructorArguments);
const additionalArgs =
(typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? [];
return new apiConstructor(...additionalArgs);
} else {
console.error(
`-- Extension of alias "${manifest.alias}" did not succeed instantiate a API class via the extension manifest property 'api', using either a 'api' or 'default' export`,
@@ -21,7 +26,9 @@ export async function createExtensionApi<ApiType extends UmbApi = UmbApi>(
if (manifest.js) {
const apiConstructor2 = await loadManifestApi<ApiType>(manifest.js);
if (apiConstructor2) {
return new apiConstructor2(...constructorArguments);
const additionalArgs =
(typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? [];
return new apiConstructor2(...additionalArgs);
} else {
console.error(
`-- Extension of alias "${manifest.alias}" did not succeed instantiate a API class via the extension manifest property 'js', using either a 'api' or 'default' export`,

View File

@@ -3,6 +3,7 @@ import type { ManifestElementAndApi } from '../types/base.types.js';
import type { ClassConstructor } from '../types/utils.js';
import { loadManifestApi } from './load-manifest-api.function.js';
import { loadManifestElement } from './load-manifest-element.function.js';
import type { UmbApiConstructorArgumentsMethodType } from './types.js';
import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
export async function createExtensionElementWithApi<
@@ -11,7 +12,7 @@ export async function createExtensionElementWithApi<
>(
manifest: ManifestElementAndApi<ElementType, ApiType>,
fallbackElement?: string,
constructorArgs?: unknown[],
constructorArgs?: unknown[] | UmbApiConstructorArgumentsMethodType<ManifestElementAndApi<ElementType, ApiType>>,
): Promise<{ element?: ElementType; api?: ApiType }> {
const apiPropValue = manifest.api ?? manifest.js;
if (!apiPropValue) {
@@ -53,7 +54,10 @@ export async function createExtensionElementWithApi<
}
if (element && apiConstructor) {
const api = new apiConstructor(element, ...(constructorArgs ?? []));
// If constructorArgs is a function, call it with the manifest to get the arguments:
const additionalArgs = (typeof constructorArgs === 'function' ? constructorArgs(manifest) : constructorArgs) ?? [];
const api = new apiConstructor(element, ...additionalArgs);
// Return object with element & api:
return { element, api };
}

View File

@@ -1,8 +1,9 @@
export * from './create-extension-api.function.js';
export * from './create-extension-element.function.js';
export * from './create-extension-element-with-api.function.js';
export * from './create-extension-element.function.js';
export * from './has-init-export.function.js';
export * from './load-manifest-api.function.js';
export * from './load-manifest-element.function.js';
export * from './load-manifest-plain-js.function.js';
export * from './load-manifest-plain-css.function.js';
export * from './load-manifest-plain-js.function.js';
export * from './types.js';

View File

@@ -0,0 +1 @@
export type UmbApiConstructorArgumentsMethodType<ManifestType> = (manifest: ManifestType) => unknown[];

View File

@@ -1,7 +1,9 @@
import type { UmbTreeItemContext, UmbTreeItemModelBase } from '../../index.js';
import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
import type { ManifestElementAndApi } from '@umbraco-cms/backoffice/extension-api';
export interface ManifestTreeItem extends ManifestElementAndApi<HTMLElement, UmbTreeItemContext<UmbTreeItemModelBase>> {
export interface ManifestTreeItem
extends ManifestElementAndApi<UmbControllerHostElement, UmbTreeItemContext<UmbTreeItemModelBase>> {
type: 'treeItem';
meta: MetaTreeItem;
}