mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 19:01:54 +02:00
fix history saving logic for fresh states, fixes #325
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
|
@@ -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,
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user