mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-01 05:16:10 +01: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:
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`.
|
||||
*
|
||||
@ -64,8 +85,8 @@ class History extends Record(DEFAULTS) {
|
||||
const { redos = [], undos = [] } = object
|
||||
|
||||
const history = new History({
|
||||
redos: new Stack(redos),
|
||||
undos: new Stack(undos),
|
||||
redos: new Stack(redos.map(this.createOperationsList)),
|
||||
undos: new Stack(undos.map(this.createOperationsList)),
|
||||
})
|
||||
|
||||
return history
|
||||
|
@ -95,7 +95,7 @@ class Value extends Record(DEFAULTS) {
|
||||
*/
|
||||
|
||||
static fromJSON(object, options = {}) {
|
||||
let { document = {}, selection = {}, schema = {} } = object
|
||||
let { document = {}, selection = {}, schema = {}, history = {} } = object
|
||||
|
||||
let data = new Map()
|
||||
|
||||
@ -114,6 +114,7 @@ class Value extends Record(DEFAULTS) {
|
||||
|
||||
selection = Range.fromJSON(selection)
|
||||
schema = Schema.fromJSON(schema)
|
||||
history = History.fromJSON(history)
|
||||
|
||||
// Allow plugins to set a default value for `data`.
|
||||
if (options.plugins) {
|
||||
@ -137,6 +138,7 @@ class Value extends Record(DEFAULTS) {
|
||||
document,
|
||||
selection,
|
||||
schema,
|
||||
history,
|
||||
})
|
||||
|
||||
if (options.normalize !== false) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user