Skip to content
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

Update canvas loadFromJSON to restore includeProperties #2921

Merged
merged 4 commits into from
Apr 30, 2016
Merged
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions src/mixins/canvas_serialization.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,27 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, /** @lends fabric.Stati
// serialize if it wasn't already
var serialized = (typeof json === 'string')
? JSON.parse(json)
: json;
: fabric.util.object.clone(json);

this.clear();

var _this = this;
this._enlivenObjects(serialized.objects, function () {
_this._setBgOverlay(serialized, callback);
}, reviver);

// remove parts i cannot set as options
delete serialized.objects;
delete serialized.backgroundImage;
delete serialized.overlayImage;
delete serialized.background;
delete serialized.overlay;
// this._initOptions does too many things to just
// call it. Normally loading an Object from JSON
// create the Object instance. Here the Canvas is
// already an instance and we are just loading things over it
for (var prop in serialized) {
this[prop] = serialized[prop];
}
return this;
},

Expand Down
13 changes: 13 additions & 0 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,19 @@
});
});

test('loadFromJSON with custom properties on Canvas', function() {
var serialized = JSON.parse(PATH_JSON);
serialized.controlsAboveOverlay = true;
serialized.preserveObjectStacking = true;
canvas.loadFromJSON(serialized, function() {
ok(!canvas.isEmpty(), 'canvas is not empty');
equal(fabric.Canvas.prototype.controlsAboveOverlay, false);
equal(fabric.Canvas.prototype.preserveObjectStacking, false);
});
equal(canvas.controlsAboveOverlay, true);
equal(canvas.preserveObjectStacking, true);
});

test('loadFromJSON custom properties', function() {
var rect = new fabric.Rect({ width: 10, height: 20 });
rect.padding = 123;
Expand Down