diff --git a/lib/delta-with-metadata.js b/lib/delta-with-metadata.js index 99aaabb..a89b2be 100644 --- a/lib/delta-with-metadata.js +++ b/lib/delta-with-metadata.js @@ -14,7 +14,7 @@ var DeltaWithMetadata = function (obj) { if (!obj) return; for (var key in config.serializedProperties) { - if (config.serializedProperties[key]) this[key] = obj[key]; + if (config.serializedProperties[key] && obj[key] !== undefined) this[key] = obj[key]; } }; diff --git a/lib/type.js b/lib/type.js index 9933a39..65589aa 100644 --- a/lib/type.js +++ b/lib/type.js @@ -14,17 +14,14 @@ module.exports = { }, apply: function (snapshot, delta) { - const metadata = snapshot && snapshot.metadata; - - snapshot = new Delta(snapshot); delta = new Delta(delta); - snapshot = snapshot.compose(delta); + var composed = new Delta(snapshot).compose(delta); - if (metadata) { - snapshot.metadata = metadata; + for (var key in config.serializedProperties) { + if (config.serializedProperties[key] && snapshot[key] !== undefined) composed[key] = snapshot[key]; } - return snapshot; + return composed; }, compose: function (delta1, delta2) { @@ -58,7 +55,7 @@ module.exports = { var serialized = {ops: delta.ops}; for (var key in config.serializedProperties) { - if (config.serializedProperties[key]) serialized[key] = delta[key]; + if (config.serializedProperties[key] && delta[key] !== undefined) serialized[key] = delta[key]; } return serialized; diff --git a/package.json b/package.json index fb484e2..270fa0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@reedsy/rich-text", - "version": "4.1.0-reedsy.3.1.0", + "version": "4.1.0-reedsy.4.0.0", "description": "OT type for rich text", "author": "Jason Chen ", "homepage": "https://github.com/ottypes/rich-text", diff --git a/test/config.js b/test/config.js index b2fc1f3..d49699f 100644 --- a/test/config.js +++ b/test/config.js @@ -4,6 +4,10 @@ var sinon = require('sinon'); const config = require('../lib/config'); describe('config', function() { + afterEach(function() { + sinon.restore(); + }); + describe('serialization', function() { it('serializes metadata by default', function() { var delta = richText.create(); @@ -28,7 +32,6 @@ describe('config', function() { delta.$doNotSerialize = {foo: '123'}; expect(richText.serialize(delta)).to.eql({ ops: [], - metadata: undefined, }); }); @@ -63,4 +66,46 @@ describe('config', function() { expect(delta.extra).to.eql({lorem: 'ipsum'}); }); }); + + describe('apply', () => { + it('keeps metadata when applying a delta to a snapshot', function() { + var snapshot = { + ops: [{insert: '\n'}], + metadata: {abc: 123}, + }; + var delta = {ops: [{insert: 'foo'}]}; + var applied = richText.apply(snapshot, delta); + expect(applied).to.eql({ + ops: [{insert: 'foo\n'}], + metadata: {abc: 123}, + }); + }); + + it('drops unspecified props when applying a delta to a snapshot', function () { + var snapshot = { + ops: [{insert: '\n'}], + extra: {abc: 123}, + }; + var delta = {ops: [{insert: 'foo'}]}; + var applied = richText.apply(snapshot, delta); + expect(applied).to.eql({ + ops: [{insert: 'foo\n'}], + }); + }); + + it('can specify extra props to keep when applying a delta to a snapshot', function () { + sinon.stub(config, 'serializedProperties').get(() => ({extra: true})); + + var snapshot = { + ops: [{insert: '\n'}], + extra: {abc: 123}, + }; + var delta = {ops: [{insert: 'foo'}]}; + var applied = richText.apply(snapshot, delta); + expect(applied).to.eql({ + ops: [{insert: 'foo\n'}], + extra: {abc: 123}, + }); + }); + }); });