diff --git a/npm/ng-packs/packages/cms-kit/src/public-api.ts b/npm/ng-packs/packages/cms-kit/src/public-api.ts index e1bb0f97fc..077f2b8e14 100644 --- a/npm/ng-packs/packages/cms-kit/src/public-api.ts +++ b/npm/ng-packs/packages/cms-kit/src/public-api.ts @@ -1,3 +1,4 @@ // Main package entry point // Use @abp/ng.cms-kit/admin or @abp/ng.cms-kit/public for specific functionality export * from './components'; +export * from './utils'; diff --git a/npm/ng-packs/packages/cms-kit/src/utils/form.utils.ts b/npm/ng-packs/packages/cms-kit/src/utils/form.utils.ts new file mode 100644 index 0000000000..e19adc4a34 --- /dev/null +++ b/npm/ng-packs/packages/cms-kit/src/utils/form.utils.ts @@ -0,0 +1,37 @@ +import { FormGroup } from '@angular/forms'; +import { DestroyRef } from '@angular/core'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { dasharize } from './text.utils'; + +/** + * Sets up automatic slug generation from a source control (e.g., title, name) to a target control (slug). + * The slug is automatically updated when the source control value changes. + * + * @param form - The form group containing the controls + * @param sourceControlName - Name of the source control (e.g., 'title', 'name') + * @param targetControlName - Name of the target control (e.g., 'slug') + * @param destroyRef - DestroyRef for automatic subscription cleanup + */ +export function prepareSlugFromControl( + form: FormGroup, + sourceControlName: string, + targetControlName: string, + destroyRef: DestroyRef, +): void { + const sourceControl = form.get(sourceControlName); + const targetControl = form.get(targetControlName); + + if (!sourceControl || !targetControl) { + return; + } + + sourceControl.valueChanges.pipe(takeUntilDestroyed(destroyRef)).subscribe(value => { + if (value && typeof value === 'string') { + const dasharized = dasharize(value); + const currentSlug = targetControl.value || ''; + if (dasharized !== currentSlug) { + targetControl.setValue(dasharized, { emitEvent: false }); + } + } + }); +} diff --git a/npm/ng-packs/packages/cms-kit/src/utils/index.ts b/npm/ng-packs/packages/cms-kit/src/utils/index.ts new file mode 100644 index 0000000000..68c64a471a --- /dev/null +++ b/npm/ng-packs/packages/cms-kit/src/utils/index.ts @@ -0,0 +1,2 @@ +export * from './text.utils'; +export * from './form.utils'; diff --git a/npm/ng-packs/packages/cms-kit/src/utils/text.utils.ts b/npm/ng-packs/packages/cms-kit/src/utils/text.utils.ts new file mode 100644 index 0000000000..123666c369 --- /dev/null +++ b/npm/ng-packs/packages/cms-kit/src/utils/text.utils.ts @@ -0,0 +1,11 @@ +export function dasharize(text: string) { + return text + .trim() + .replace(/([a-z])([A-Z])/g, '$1-$2') + .replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2') + .replace(/[\s_]+/g, '-') + .replace(/[^\w\s-]/g, '') + .replace(/-+/g, '-') + .replace(/^-+|-+$/g, '') + .toLowerCase(); +}