Skip to content

Fixes froala-labs/froala-editor-js-2#1824 #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/editor/editor.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,65 @@ export class FroalaEditorDirective implements ControlValueAccessor {

// froalaEditor directive as input: store the editor options
@Input() set froalaEditor(opts: any) {
this._opts = opts || this._opts;

// 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)
}

// froalaModel directive as input: store initial editor content
@Input() set froalaModel(content: any) {
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)) {
Expand Down