06316e1715
git-subtree-dir: frontend git-subtree-split: 63375d198b2160a3a5fc4427efe888f6c37bd92f
45 lines
972 B
TypeScript
45 lines
972 B
TypeScript
import {
|
|
Directive,
|
|
ElementRef,
|
|
EventEmitter,
|
|
HostListener,
|
|
inject,
|
|
Input,
|
|
Output,
|
|
} from '@angular/core'
|
|
|
|
@Directive({
|
|
selector: '[appCe] [contenteditable]',
|
|
standalone: false,
|
|
})
|
|
export class CeDirective {
|
|
readonly el: ElementRef<HTMLElement> = inject(ElementRef)
|
|
|
|
@Input() multiline = false
|
|
@Output() edit = new EventEmitter<string | string[]>()
|
|
private originalValue: string | ChildNode[]
|
|
|
|
constructor() {
|
|
this.originalValue = this.el.nativeElement.innerText
|
|
}
|
|
|
|
@HostListener('focusin') focusin() {
|
|
this.originalValue = this.el.nativeElement.innerText
|
|
}
|
|
|
|
@HostListener('focusout') focusout() {
|
|
const newText = this.el.nativeElement.innerText
|
|
if (newText != this.originalValue) {
|
|
this.edit.emit(newText)
|
|
}
|
|
}
|
|
|
|
@HostListener('keydown', ['$event']) keyup(e: KeyboardEvent) {
|
|
if (this.multiline) return
|
|
if (e.key == 'Enter') {
|
|
e.preventDefault()
|
|
this.el.nativeElement.blur()
|
|
}
|
|
}
|
|
}
|