Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 27ab310

Browse files
author
Piotr Jasiun
committed
Merge pull request #1493 from ckeditor/t/1477
Other: Make `toJSON()` to serialize nested objects. Closes #1477.
2 parents e334195 + f2863d5 commit 27ab310

35 files changed

+235
-99
lines changed

src/dev-utils/enableenginedebug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ function enableReplayerTools() {
427427
this._appliedOperations = [];
428428
}
429429

430-
this._appliedOperations.push( operation.toJSON() );
430+
this._appliedOperations.push( operation );
431431

432432
return _modelApplyOperation.call( this, operation );
433433
} );
@@ -455,7 +455,7 @@ function enableDocumentTools() {
455455
this._operationLogs = [];
456456
}
457457

458-
this._operationLogs.push( JSON.stringify( operation.toJSON() ) );
458+
this._operationLogs.push( JSON.stringify( operation ) );
459459

460460
return _modelApplyOperation.call( this, operation );
461461
} );

src/model/node.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,14 @@ export default class Node {
380380
toJSON() {
381381
const json = {};
382382

383+
// Serializes attributes to the object.
384+
// attributes = { a: 'foo', b: 1, c: true }.
383385
if ( this._attrs.size ) {
384-
json.attributes = [ ...this._attrs ];
386+
json.attributes = Array.from( this._attrs ).reduce( ( result, attr ) => {
387+
result[ attr[ 0 ] ] = attr[ 1 ];
388+
389+
return result;
390+
}, {} );
385391
}
386392

387393
return json;

src/model/operation/attributeoperation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ export default class AttributeOperation extends Operation {
107107
return new AttributeOperation( this.range, this.key, this.newValue, this.oldValue, this.baseVersion + 1 );
108108
}
109109

110+
/**
111+
* @inheritDoc
112+
*/
113+
toJSON() {
114+
const json = super.toJSON();
115+
116+
json.range = this.range.toJSON();
117+
118+
return json;
119+
}
120+
110121
/**
111122
* @inheritDoc
112123
*/

src/model/operation/detachoperation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export default class DetachOperation extends Operation {
5353
return 'detach';
5454
}
5555

56+
/**
57+
* @inheritDoc
58+
*/
59+
toJSON() {
60+
const json = super.toJSON();
61+
62+
json.sourcePosition = this.sourcePosition.toJSON();
63+
64+
return json;
65+
}
66+
5667
/**
5768
* @inheritDoc
5869
*/

src/model/operation/insertoperation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ export default class InsertOperation extends Operation {
128128
_insert( this.position, originalNodes );
129129
}
130130

131+
/**
132+
* @inheritDoc
133+
*/
134+
toJSON() {
135+
const json = super.toJSON();
136+
137+
json.position = this.position.toJSON();
138+
json.nodes = this.nodes.toJSON();
139+
140+
return json;
141+
}
142+
131143
/**
132144
* @inheritDoc
133145
*/

src/model/operation/markeroperation.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ export default class MarkerOperation extends Operation {
109109
toJSON() {
110110
const json = super.toJSON();
111111

112+
if ( this.oldRange ) {
113+
json.oldRange = this.oldRange.toJSON();
114+
}
115+
116+
if ( this.newRange ) {
117+
json.newRange = this.newRange.toJSON();
118+
}
119+
112120
delete json._markers;
113121

114122
return json;

src/model/operation/mergeoperation.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ export default class MergeOperation extends Operation {
152152
_move( Range.createOn( mergedElement ), this.graveyardPosition );
153153
}
154154

155+
/**
156+
* @inheritDoc
157+
*/
158+
toJSON() {
159+
const json = super.toJSON();
160+
161+
json.sourcePosition = json.sourcePosition.toJSON();
162+
json.targetPosition = json.targetPosition.toJSON();
163+
json.graveyardPosition = json.graveyardPosition.toJSON();
164+
165+
return json;
166+
}
167+
155168
/**
156169
* @inheritDoc
157170
*/

src/model/operation/moveoperation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ export default class MoveOperation extends Operation {
175175
_move( Range.createFromPositionAndShift( this.sourcePosition, this.howMany ), this.targetPosition );
176176
}
177177

178+
/**
179+
* @inheritDoc
180+
*/
181+
toJSON() {
182+
const json = super.toJSON();
183+
184+
json.sourcePosition = this.sourcePosition.toJSON();
185+
json.targetPosition = this.targetPosition.toJSON();
186+
187+
return json;
188+
}
189+
178190
/**
179191
* @inheritDoc
180192
*/

src/model/operation/operation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* @module engine/model/operation/operation
88
*/
99

10-
import { clone } from 'lodash-es';
11-
1210
/**
1311
* Abstract base operation class.
1412
*
@@ -94,7 +92,9 @@ export default class Operation {
9492
* @returns {Object} Clone of this object with the operation property replaced with string.
9593
*/
9694
toJSON() {
97-
const json = clone( this, true );
95+
// This method creates only a shallow copy, all nested objects should be defined separately.
96+
// See https://github.com/ckeditor/ckeditor5-engine/issues/1477.
97+
const json = Object.assign( {}, this );
9898

9999
json.__className = this.constructor.className;
100100

src/model/operation/renameoperation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ export default class RenameOperation extends Operation {
116116
element.name = this.newName;
117117
}
118118

119+
/**
120+
* @inheritDoc
121+
*/
122+
toJSON() {
123+
const json = super.toJSON();
124+
125+
json.position = this.position.toJSON();
126+
127+
return json;
128+
}
129+
119130
/**
120131
* @inheritDoc
121132
*/

0 commit comments

Comments
 (0)