From eb7ad70dde06cf7718f0c3c062ab8d29717114ae Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sun, 11 Nov 2018 14:34:05 -0800 Subject: [PATCH] fix to always default operation.data to an object --- packages/slate/src/models/operation.js | 10 ++++++---- .../models/operation/create/add-mark-without-data.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/slate/src/models/operation.js b/packages/slate/src/models/operation.js index 49f485bde..4d305053f 100644 --- a/packages/slate/src/models/operation.js +++ b/packages/slate/src/models/operation.js @@ -126,6 +126,11 @@ class Operation extends Record(DEFAULTS) { for (const key of ATTRIBUTES) { let v = object[key] + // Default `data` to an empty object. + if (key === 'data' && v === undefined) { + v = {} + } + if (v === undefined) { // Skip keys for objects that should not be serialized, and are only used // for providing the local-only invert behavior for the history stack. @@ -134,9 +139,6 @@ class Operation extends Record(DEFAULTS) { if (key == 'value') continue if (key == 'node' && type != 'insert_node') continue - // Skip optional user defined data - if (key == 'data') continue - throw new Error( `\`Operation.fromJSON\` was passed a "${type}" operation without the required "${key}" attribute.` ) @@ -311,7 +313,7 @@ class Operation extends Record(DEFAULTS) { value = v } - if (key === 'data' && value) { + if (key === 'data') { value = value.toJSON() } diff --git a/packages/slate/test/models/operation/create/add-mark-without-data.js b/packages/slate/test/models/operation/create/add-mark-without-data.js index 55150a7b1..cda7993f3 100644 --- a/packages/slate/test/models/operation/create/add-mark-without-data.js +++ b/packages/slate/test/models/operation/create/add-mark-without-data.js @@ -23,5 +23,5 @@ export const output = { object: 'mark', type: 'b', }, - data: undefined, + data: {}, }