Browse Source

add: utils for the cms kit operations

pull/24234/head
sumeyye 9 months ago
parent
commit
2704848b7c
  1. 1
      npm/ng-packs/packages/cms-kit/src/public-api.ts
  2. 37
      npm/ng-packs/packages/cms-kit/src/utils/form.utils.ts
  3. 2
      npm/ng-packs/packages/cms-kit/src/utils/index.ts
  4. 11
      npm/ng-packs/packages/cms-kit/src/utils/text.utils.ts

1
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';

37
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 });
}
}
});
}

2
npm/ng-packs/packages/cms-kit/src/utils/index.ts

@ -0,0 +1,2 @@
export * from './text.utils';
export * from './form.utils';

11
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();
}
Loading…
Cancel
Save