Skip to content

Commit

Permalink
fix(editor): editor initial value in reactive forms
Browse files Browse the repository at this point in the history
close #18
  • Loading branch information
Raiper34 committed Oct 21, 2023
1 parent 2d1348d commit 756f74d
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ElementRef, Inject, Input, ViewChild} from '@angular/core';
import {AfterViewInit, Component, ElementRef, Inject, Input, ViewChild} from '@angular/core';
import {DOCUMENT} from '@angular/common';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
import {ST_BUTTONS} from '../../constants/editor-buttons';
Expand All @@ -20,7 +20,7 @@ const DEFAULT_CONFIG: EditorConfig = {
CommandService
]
})
export class EditorComponent implements ControlValueAccessor {
export class EditorComponent implements AfterViewInit, ControlValueAccessor {

@Input() set config(val: EditorConfig) {
this._config = {...DEFAULT_CONFIG, ...val};
Expand All @@ -40,11 +40,13 @@ export class EditorComponent implements ControlValueAccessor {
constructor(@Inject(DOCUMENT) private readonly document: any,
private readonly commandService: CommandService) { }

ngAfterViewInit(): void {
this.updateContentEditable();
}

writeValue(val: string): void {
this.content = val;
if (this.contentEditable) {
this.contentEditable.nativeElement.innerHTML = val;
}
this.updateContentEditable();
}

registerOnChange(fn: (val: string) => void): void {
Expand Down Expand Up @@ -79,4 +81,10 @@ export class EditorComponent implements ControlValueAccessor {
return item.name;
}

private updateContentEditable(): void {
if (this.contentEditable) {
this.contentEditable.nativeElement.innerHTML = this.content;
}
}

}

0 comments on commit 756f74d

Please sign in to comment.