mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 10:29:48 +02:00
allow fromJSON to restore history (#1979)
* allow fromJSON to restore history * allow History.toJSON to be passed operations in Arrays instead of Lists (for example when they have been serialized and deserialized) * tweak if condition to not make a new List from a List, waste of perf * s/createList/createOperationsList/ in history.js
This commit is contained in:
committed by
Ian Storm Taylor
parent
1c3415b950
commit
c046652a71
@@ -53,6 +53,27 @@ class History extends Record(DEFAULTS) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a list of `Operations` from `operations`.
|
||||||
|
*
|
||||||
|
* @param {Array<Object>|List<Object>} operations
|
||||||
|
* @return {List<Object>}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static createOperationsList(operations = []) {
|
||||||
|
if (List.isList(operations)) {
|
||||||
|
return operations
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(operations)) {
|
||||||
|
return new List(operations)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`\`History.createList\` only accepts arrays or lists, but you passed it: ${operations}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a `History` from a JSON `object`.
|
* Create a `History` from a JSON `object`.
|
||||||
*
|
*
|
||||||
@@ -64,8 +85,8 @@ class History extends Record(DEFAULTS) {
|
|||||||
const { redos = [], undos = [] } = object
|
const { redos = [], undos = [] } = object
|
||||||
|
|
||||||
const history = new History({
|
const history = new History({
|
||||||
redos: new Stack(redos),
|
redos: new Stack(redos.map(this.createOperationsList)),
|
||||||
undos: new Stack(undos),
|
undos: new Stack(undos.map(this.createOperationsList)),
|
||||||
})
|
})
|
||||||
|
|
||||||
return history
|
return history
|
||||||
|
@@ -95,7 +95,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static fromJSON(object, options = {}) {
|
static fromJSON(object, options = {}) {
|
||||||
let { document = {}, selection = {}, schema = {} } = object
|
let { document = {}, selection = {}, schema = {}, history = {} } = object
|
||||||
|
|
||||||
let data = new Map()
|
let data = new Map()
|
||||||
|
|
||||||
@@ -114,6 +114,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
selection = Range.fromJSON(selection)
|
selection = Range.fromJSON(selection)
|
||||||
schema = Schema.fromJSON(schema)
|
schema = Schema.fromJSON(schema)
|
||||||
|
history = History.fromJSON(history)
|
||||||
|
|
||||||
// Allow plugins to set a default value for `data`.
|
// Allow plugins to set a default value for `data`.
|
||||||
if (options.plugins) {
|
if (options.plugins) {
|
||||||
@@ -137,6 +138,7 @@ class Value extends Record(DEFAULTS) {
|
|||||||
document,
|
document,
|
||||||
selection,
|
selection,
|
||||||
schema,
|
schema,
|
||||||
|
history,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (options.normalize !== false) {
|
if (options.normalize !== false) {
|
||||||
|
Reference in New Issue
Block a user