From 16ae0790f6a1c615b336f825b151a8ee5a313903 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 15 Sep 2016 14:38:07 -0700 Subject: [PATCH] fix history saving logic for fresh states, fixes #325 --- src/models/transform.js | 21 +++++++++++++++++---- src/transforms/operations.js | 32 ++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/models/transform.js b/src/models/transform.js index 1eb22743c..2ee133035 100644 --- a/src/models/transform.js +++ b/src/models/transform.js @@ -1,6 +1,15 @@ +import Debug from 'debug' import Transforms from '../transforms' +/** + * Debug. + * + * @type {Function} + */ + +const debug = Debug('slate:transform') + /** * Transform. * @@ -42,7 +51,7 @@ class Transform { */ apply(options = {}) { - let { merge, isNative = false, save = true } = options + let { merge, save, isNative = false } = options let { state, operations } = this let { history } = state let { undos, redos } = history @@ -61,11 +70,14 @@ class Transform { ) } - // Save the new operations. - if (save || !previous) { - this.save({ merge }) + // If the save flag isn't set, determine whether we should save. + if (save == null) { + save = !isOnlySelections(operations) } + // Save the new operations. + if (save) this.save({ merge }) + // Return the new state with the `isNative` flag set. return this.state.merge({ isNative: !!isNative }) } @@ -78,6 +90,7 @@ class Transform { Object.keys(Transforms).forEach((type) => { Transform.prototype[type] = function (...args) { + debug(type, { args }) return Transforms[type](this, ...args) } }) diff --git a/src/transforms/operations.js b/src/transforms/operations.js index f5d7cc5ff..5880d8e68 100644 --- a/src/transforms/operations.js +++ b/src/transforms/operations.js @@ -360,23 +360,27 @@ export function setSelectionOperation(transform, properties) { const { state } = transform const { document, selection } = state const prevProps = {} + const props = {} + + // Remove any properties that are already equal to the current selection. And + // create a dictionary of the previous values for all of the properties that + // are being changed, for the inverse operation. + for (const k in properties) { + if (properties[k] == selection[k]) continue + props[k] = properties[k] + prevProps[k] = selection[k] + } // If the current selection has marks, and the new selection doesn't change // them in some way, they are old and should be removed. if (selection.marks && properties.marks == selection.marks) { - properties.marks = null - } - - // Create a dictionary of the previous values for all of the properties that - // are being changed, for the inverse operation. - for (const k in properties) { - prevProps[k] = selection[k] + props.marks = null } // Resolve the selection keys into paths. - if (properties.anchorKey) { - properties.anchorPath = document.getPath(properties.anchorKey) - delete properties.anchorKey + if (props.anchorKey) { + props.anchorPath = document.getPath(props.anchorKey) + delete props.anchorKey } if (prevProps.anchorKey) { @@ -384,9 +388,9 @@ export function setSelectionOperation(transform, properties) { delete prevProps.anchorKey } - if (properties.focusKey) { - properties.focusPath = document.getPath(properties.focusKey) - delete properties.focusKey + if (props.focusKey) { + props.focusPath = document.getPath(props.focusKey) + delete props.focusKey } if (prevProps.focusKey) { @@ -403,7 +407,7 @@ export function setSelectionOperation(transform, properties) { // Define the operation. const operation = { type: 'set_selection', - properties, + properties: props, inverse, }