/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** Angular Imports */ import { Component, OnInit, inject } from '@angular/core'; import { MatDialogRef, MAT_DIALOG_DATA, MatDialogTitle, MatDialogActions, MatDialogClose } from '@angular/material/dialog'; import { MatTooltip } from '@angular/material/tooltip'; import { UntypedFormGroup, UntypedFormBuilder, Validators, ReactiveFormsModule } from '@angular/forms'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; import { minWordsValidator } from 'app/shared/validators/min-words.validator'; @Component({ selector: 'mifosx-edit-notes-dialog', templateUrl: './edit-notes-dialog.component.html', styleUrls: ['./edit-notes-dialog.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, MatDialogTitle, MatDialogActions, MatDialogClose, MatTooltip ] }) export class EditNotesDialogComponent implements OnInit { dialogRef = inject>(MatDialogRef); private formBuilder = inject(UntypedFormBuilder); data = inject(MAT_DIALOG_DATA); noteForm: UntypedFormGroup; ngOnInit() { this.createNoteForm(); } createNoteForm() { this.noteForm = this.formBuilder.group({ // Same rule as the entity-notes Add form — reject whitespace / single-word. note: [ this.data.noteContent, [ Validators.required, minWordsValidator(2) ] ] }); } /** Block whitespace at position 0 of an otherwise-empty textarea. */ onNoteKeyDown(event: KeyboardEvent): void { if (event.key !== ' ' && event.key !== 'Tab' && event.key !== 'Enter') return; const el = event.target as HTMLTextAreaElement; if (el.selectionStart === 0 && el.value.trim() === '') { event.preventDefault(); } } /** Strip leading whitespace + collapse internal runs of spaces/tabs. */ onNoteInput(event: Event): void { const el = event.target as HTMLTextAreaElement; const before = el.value; const after = before.replace(/^[ \t]+/, '').replace(/[ \t]{2,}/g, ' '); if (before !== after) { const cursorDelta = before.length - after.length; const pos = Math.max(0, (el.selectionStart ?? after.length) - cursorDelta); el.value = after; this.noteForm.get('note')!.setValue(after, { emitEvent: false }); el.setSelectionRange(pos, pos); } } }