Browse Source

fix(admin-ui): Fix error with rich text editor (trix)

Michael Bromley 6 years ago
parent
commit
b42ead6dea

+ 15 - 5
admin-ui/src/app/shared/components/rich-text-editor/rich-text-editor.component.ts

@@ -39,6 +39,7 @@ export class RichTextEditorComponent implements ControlValueAccessor, AfterViewI
     onChange: (val: any) => void;
     onTouch: () => void;
     disabled = false;
+    private initialized = false;
 
     @ViewChild('trixEditor') private trixEditor: ElementRef;
 
@@ -49,13 +50,17 @@ export class RichTextEditorComponent implements ControlValueAccessor, AfterViewI
     }
 
     onTrixChangeHandler = () => {
-        this.onChange(this.trix.innerHTML);
-        this.changeDetector.markForCheck();
+        if (this.initialized) {
+            this.onChange(this.trix.innerHTML);
+            this.changeDetector.markForCheck();
+        }
     };
 
     onTrixFocusHandler = () => {
-        this.onTouch();
-        this.changeDetector.markForCheck();
+        if (this.initialized) {
+            this.onTouch();
+            this.changeDetector.markForCheck();
+        }
     };
 
     ngAfterViewInit() {
@@ -82,7 +87,12 @@ export class RichTextEditorComponent implements ControlValueAccessor, AfterViewI
 
     writeValue(value: any) {
         if (this.trix.innerHTML !== value) {
-            this.trix.editor.loadHTML(value);
+            if (!this.initialized) {
+                setTimeout(() => {
+                    this.trix.editor.loadHTML(value);
+                    this.initialized = true;
+                });
+            }
         }
     }
 }