11import { ControlValueAccessor , NG_VALUE_ACCESSOR } from "@angular/forms" ;
22import { Directive , ElementRef , EventEmitter , Input , NgZone , Output , forwardRef } from '@angular/core' ;
3+ import cloneDeep from "lodash.clonedeep" ;
4+ import isEqual from 'lodash.isequal' ;
35
46import FroalaEditor from 'froala-editor' ;
57
8+ type FroalaOptions = Record < string , any > ;
9+
610@Directive ( {
711 selector : '[froalaEditor]' ,
812 exportAs : 'froalaEditor' ,
@@ -15,7 +19,7 @@ import FroalaEditor from 'froala-editor';
1519export class FroalaEditorDirective implements ControlValueAccessor {
1620
1721 // editor options
18- private _opts : any = {
22+ private _opts : FroalaOptions = {
1923 immediateAngularModelUpdate : false ,
2024 angularIgnoreAttrs : null
2125 } ;
@@ -65,65 +69,25 @@ export class FroalaEditorDirective implements ControlValueAccessor {
6569 // froalaEditor directive as input: store the editor options
6670 @Input ( ) set froalaEditor ( opts : any ) {
6771 this . _opts = this . clone ( opts || this . _opts ) ;
68- this . _opts = { ...this . _opts } ;
6972 }
7073
71- // TODO: replace clone method with better possible alternate
72- private clone ( item ) {
73- const me = this ;
74- if ( ! item ) { return item ; } // null, undefined values check
75-
76- let types = [ Number , String , Boolean ] ,
77- result ;
78-
79- // normalizing primitives if someone did new String('aaa'), or new Number('444');
80- types . forEach ( function ( type ) {
81- if ( item instanceof type ) {
82- result = type ( item ) ;
83- }
84- } ) ;
85-
86- if ( typeof result == "undefined" ) {
87- if ( Object . prototype . toString . call ( item ) === "[object Array]" ) {
88- result = [ ] ;
89- item . forEach ( function ( child , index , array ) {
90- result [ index ] = me . clone ( child ) ;
91- } ) ;
92- } else if ( typeof item == "object" ) {
93- // testing that this is DOM
94- if ( item . nodeType && typeof item . cloneNode == "function" ) {
95- result = item . cloneNode ( true ) ;
96- } else if ( ! item . prototype ) { // check that this is a literal
97- if ( item instanceof Date ) {
98- result = new Date ( item ) ;
99- } else {
100- // it is an object literal
101- result = { } ;
102- for ( var i in item ) {
103- result [ i ] = me . clone ( item [ i ] ) ;
104- }
105- }
106- } else {
107- if ( false && item . constructor ) {
108- result = new item . constructor ( ) ;
109- } else {
110- result = item ;
111- }
112- }
113- } else {
114- result = item ;
115- }
116- }
117- return result ;
74+ // clone object
75+ private clone ( opts : FroalaOptions ) : FroalaOptions {
76+ return cloneDeep ( opts ) ;
11877 }
11978 // froalaModel directive as input: store initial editor content
12079 @Input ( ) set froalaModel ( content : any ) {
12180 this . updateEditor ( content ) ;
12281 }
12382
83+ // exposing the editor
84+ public get editor ( ) {
85+ return this . _editor ;
86+ }
87+
12488 // Update editor with model contents.
12589 private updateEditor ( content : any ) {
126- if ( JSON . stringify ( this . _oldModel ) == JSON . stringify ( content ) ) {
90+ if ( isEqual ( this . _oldModel , content ) ) {
12791 return ;
12892 }
12993
@@ -223,9 +187,7 @@ export class FroalaEditorDirective implements ControlValueAccessor {
223187 if ( this . _editor . events ) {
224188 // bind contentChange and keyup event to froalaModel
225189 this . _editor . events . on ( 'contentChanged' , function ( ) {
226- setTimeout ( function ( ) {
227- self . updateModel ( ) ;
228- } , 0 ) ;
190+ self . updateModel ( ) ;
229191 } ) ;
230192 this . _editor . events . on ( 'mousedown' , function ( ) {
231193 setTimeout ( function ( ) {
@@ -278,7 +240,11 @@ export class FroalaEditorDirective implements ControlValueAccessor {
278240 }
279241
280242 private setHtml ( ) {
281- this . _editor . html . set ( this . _model || "" ) ;
243+ if ( this . _hasSpecialTag ) {
244+ this . _editor . html . set ( this . _model || "" ) ;
245+ } else {
246+ this . _editor . html . set ( this . _oldModel || "" ) ;
247+ }
282248
283249 // This will reset the undo stack everytime the model changes externally. Can we fix this?
284250 this . _editor . undo . reset ( ) ;
@@ -291,6 +257,7 @@ export class FroalaEditorDirective implements ControlValueAccessor {
291257 // Set initial content
292258 if ( this . _model || this . _model == '' ) {
293259 this . _oldModel = this . _model ;
260+
294261 if ( this . _hasSpecialTag ) {
295262
296263 let tags : Object = this . _model ;
@@ -317,7 +284,7 @@ export class FroalaEditorDirective implements ControlValueAccessor {
317284 self . setHtml ( ) ;
318285 }
319286 }
320- }
287+ }
321288 }
322289
323290 private destroyEditor ( ) {
0 commit comments