mirror of https://github.com/abpframework/abp.git
4 changed files with 51 additions and 0 deletions
@ -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'; |
|||
|
|||
@ -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 }); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
@ -0,0 +1,2 @@ |
|||
export * from './text.utils'; |
|||
export * from './form.utils'; |
|||
@ -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(); |
|||
} |
|||
Loading…
Reference in new issue