From 9ca2a7526fb1f46db44c2be5014c53f287e308f1 Mon Sep 17 00:00:00 2001 From: GovardhanCel Date: Thu, 5 Dec 2019 15:23:44 +0530 Subject: [PATCH 1/2] Two Froalas on same page with same options but second Froala doesn't update froalaModel --- src/editor/editor.directive.ts | 53 +++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/editor/editor.directive.ts b/src/editor/editor.directive.ts index e3a1310..240c375 100644 --- a/src/editor/editor.directive.ts +++ b/src/editor/editor.directive.ts @@ -64,7 +64,9 @@ export class FroalaEditorDirective implements ControlValueAccessor { // froalaEditor directive as input: store the editor options @Input() set froalaEditor(opts: any) { - this._opts = opts || this._opts; + + // https://github.com/froala-labs/froala-editor-js-2/issues/1824 + this._opts = this.clone(opts || this._opts) } // froalaModel directive as input: store initial editor content @@ -72,6 +74,55 @@ export class FroalaEditorDirective implements ControlValueAccessor { this.updateEditor(content); } + // TODO: replace clone method with better possible alternate + private clone(item) { + const me = this; + if (!item) { return item; } // null, undefined values check + + let types = [ Number, String, Boolean ], + result; + + // normalizing primitives if someone did new String('aaa'), or new Number('444'); + types.forEach(function(type) { + if (item instanceof type) { + result = type( item ); + } + }); + + if (typeof result == "undefined") { + if (Object.prototype.toString.call( item ) === "[object Array]") { + result = []; + item.forEach(function(child, index, array) { + result[index] = me.clone( child ); + }); + } else if (typeof item == "object") { + // testing that this is DOM + if (item.nodeType && typeof item.cloneNode == "function") { + result = item.cloneNode( true ); + } else if (!item.prototype) { // check that this is a literal + if (item instanceof Date) { + result = new Date(item); + } else { + // it is an object literal + result = {}; + for (var i in item) { + result[i] = me.clone( item[i] ); + } + } + } else { + if (false && item.constructor) { + result = new item.constructor(); + } else { + result = item; + } + } + } else { + result = item; + } + } + + return result; + } // Update editor with model contents. private updateEditor(content: any) { if (JSON.stringify(this._oldModel) == JSON.stringify(content)) { From a7a1ff80829d7a06821c4032841a64c77741cbfe Mon Sep 17 00:00:00 2001 From: GovardhanCel Date: Thu, 5 Dec 2019 15:35:31 +0530 Subject: [PATCH 2/2] Comment changes --- src/editor/editor.directive.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/editor/editor.directive.ts b/src/editor/editor.directive.ts index 240c375..ed89d00 100644 --- a/src/editor/editor.directive.ts +++ b/src/editor/editor.directive.ts @@ -65,7 +65,7 @@ export class FroalaEditorDirective implements ControlValueAccessor { // froalaEditor directive as input: store the editor options @Input() set froalaEditor(opts: any) { - // https://github.com/froala-labs/froala-editor-js-2/issues/1824 + // Cloning is done to prevent referencing the events of editor intialized before https://github.com/froala-labs/froala-editor-js-2/issues/1824 this._opts = this.clone(opts || this._opts) }