mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 19:22:35 +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'
|
import Transforms from '../transforms'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debug.
|
||||||
|
*
|
||||||
|
* @type {Function}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const debug = Debug('slate:transform')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform.
|
* Transform.
|
||||||
*
|
*
|
||||||
@@ -42,7 +51,7 @@ class Transform {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
apply(options = {}) {
|
apply(options = {}) {
|
||||||
let { merge, isNative = false, save = true } = options
|
let { merge, save, isNative = false } = options
|
||||||
let { state, operations } = this
|
let { state, operations } = this
|
||||||
let { history } = state
|
let { history } = state
|
||||||
let { undos, redos } = history
|
let { undos, redos } = history
|
||||||
@@ -61,11 +70,14 @@ class Transform {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the new operations.
|
// If the save flag isn't set, determine whether we should save.
|
||||||
if (save || !previous) {
|
if (save == null) {
|
||||||
this.save({ merge })
|
save = !isOnlySelections(operations)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the new operations.
|
||||||
|
if (save) this.save({ merge })
|
||||||
|
|
||||||
// Return the new state with the `isNative` flag set.
|
// Return the new state with the `isNative` flag set.
|
||||||
return this.state.merge({ isNative: !!isNative })
|
return this.state.merge({ isNative: !!isNative })
|
||||||
}
|
}
|
||||||
@@ -78,6 +90,7 @@ class Transform {
|
|||||||
|
|
||||||
Object.keys(Transforms).forEach((type) => {
|
Object.keys(Transforms).forEach((type) => {
|
||||||
Transform.prototype[type] = function (...args) {
|
Transform.prototype[type] = function (...args) {
|
||||||
|
debug(type, { args })
|
||||||
return Transforms[type](this, ...args)
|
return Transforms[type](this, ...args)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@@ -360,23 +360,27 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document, selection } = state
|
const { document, selection } = state
|
||||||
const prevProps = {}
|
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
|
// If the current selection has marks, and the new selection doesn't change
|
||||||
// them in some way, they are old and should be removed.
|
// them in some way, they are old and should be removed.
|
||||||
if (selection.marks && properties.marks == selection.marks) {
|
if (selection.marks && properties.marks == selection.marks) {
|
||||||
properties.marks = null
|
props.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]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve the selection keys into paths.
|
// Resolve the selection keys into paths.
|
||||||
if (properties.anchorKey) {
|
if (props.anchorKey) {
|
||||||
properties.anchorPath = document.getPath(properties.anchorKey)
|
props.anchorPath = document.getPath(props.anchorKey)
|
||||||
delete properties.anchorKey
|
delete props.anchorKey
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevProps.anchorKey) {
|
if (prevProps.anchorKey) {
|
||||||
@@ -384,9 +388,9 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
delete prevProps.anchorKey
|
delete prevProps.anchorKey
|
||||||
}
|
}
|
||||||
|
|
||||||
if (properties.focusKey) {
|
if (props.focusKey) {
|
||||||
properties.focusPath = document.getPath(properties.focusKey)
|
props.focusPath = document.getPath(props.focusKey)
|
||||||
delete properties.focusKey
|
delete props.focusKey
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevProps.focusKey) {
|
if (prevProps.focusKey) {
|
||||||
@@ -403,7 +407,7 @@ export function setSelectionOperation(transform, properties) {
|
|||||||
// Define the operation.
|
// Define the operation.
|
||||||
const operation = {
|
const operation = {
|
||||||
type: 'set_selection',
|
type: 'set_selection',
|
||||||
properties,
|
properties: props,
|
||||||
inverse,
|
inverse,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user