mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-21 22:45:18 +02:00
fix to make lists of operations immutable too
This commit is contained in:
@@ -11,7 +11,9 @@ This document maintains a list of changes to the `slate` package with each new v
|
|||||||
|
|
||||||
###### BREAKING
|
###### BREAKING
|
||||||
|
|
||||||
- **The operation objects in Slate are now immutable records.** Previously they were native, mutable Javascript objects. Now, there's a new immutable `Operation` model in Slate, ensuring that all of the data inside `Value` objects are immutable. And it allows for easy serialization of operations using `operation.toJSON()` for when sending them between editors. This should not affect most users, unless you are relying on changing the values of the low-level Slate operations (simply reading them is fine).
|
- **Operation objects in Slate are now immutable records.** Previously they were native, mutable Javascript objects. Now, there's a new immutable `Operation` model in Slate, ensuring that all of the data inside `Value` objects are immutable. And it allows for easy serialization of operations using `operation.toJSON()` for when sending them between editors. This should not affect most users, unless you are relying on changing the values of the low-level Slate operations (simply reading them is fine).
|
||||||
|
|
||||||
|
- **Operation lists in Slate are now immutable lists.** Previously they were native, mutable Javascript arrays. Now, to keep consistent with other immutable uses, they are immutable lists. This should not affect most users.
|
||||||
|
|
||||||
###### NEW
|
###### NEW
|
||||||
|
|
||||||
|
@@ -76,7 +76,7 @@ function normalizeNodeAndChildren(change, node, schema) {
|
|||||||
|
|
||||||
// While there is still a child key that hasn't been normalized yet...
|
// While there is still a child key that hasn't been normalized yet...
|
||||||
while (keys.length) {
|
while (keys.length) {
|
||||||
const ops = change.operations.length
|
const { size } = change.operations
|
||||||
let key
|
let key
|
||||||
|
|
||||||
// PERF: use a mutable set here since we'll be add to it a lot.
|
// PERF: use a mutable set here since we'll be add to it a lot.
|
||||||
@@ -95,7 +95,7 @@ function normalizeNodeAndChildren(change, node, schema) {
|
|||||||
|
|
||||||
// PERF: Only re-find the node and re-normalize any new children if
|
// PERF: Only re-find the node and re-normalize any new children if
|
||||||
// operations occured that might have changed it.
|
// operations occured that might have changed it.
|
||||||
if (change.operations.length != ops) {
|
if (change.operations.size > size) {
|
||||||
node = refindNode(change, node)
|
node = refindNode(change, node)
|
||||||
|
|
||||||
// Add any new children back onto the stack.
|
// Add any new children back onto the stack.
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
import Debug from 'debug'
|
import Debug from 'debug'
|
||||||
import isPlainObject from 'is-plain-object'
|
import isPlainObject from 'is-plain-object'
|
||||||
import pick from 'lodash/pick'
|
import pick from 'lodash/pick'
|
||||||
|
import { List } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES from '../constants/model-types'
|
||||||
import Changes from '../changes'
|
import Changes from '../changes'
|
||||||
@@ -45,7 +46,7 @@ class Change {
|
|||||||
constructor(attrs) {
|
constructor(attrs) {
|
||||||
const { value } = attrs
|
const { value } = attrs
|
||||||
this.value = value
|
this.value = value
|
||||||
this.operations = []
|
this.operations = new List()
|
||||||
this.flags = pick(attrs, ['merge', 'save'])
|
this.flags = pick(attrs, ['merge', 'save'])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ class Change {
|
|||||||
* Apply an `operation` to the current value, saving the operation to the
|
* Apply an `operation` to the current value, saving the operation to the
|
||||||
* history if needed.
|
* history if needed.
|
||||||
*
|
*
|
||||||
* @param {Object} operation
|
* @param {Operation|Object} operation
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @return {Change}
|
* @return {Change}
|
||||||
*/
|
*/
|
||||||
@@ -86,7 +87,7 @@ class Change {
|
|||||||
|
|
||||||
// Derive the default option values.
|
// Derive the default option values.
|
||||||
const {
|
const {
|
||||||
merge = operations.length == 0 ? null : true,
|
merge = operations.size == 0 ? null : true,
|
||||||
save = true,
|
save = true,
|
||||||
skip = null,
|
skip = null,
|
||||||
} = options
|
} = options
|
||||||
@@ -103,14 +104,14 @@ class Change {
|
|||||||
|
|
||||||
// Update the mutable change object.
|
// Update the mutable change object.
|
||||||
this.value = value
|
this.value = value
|
||||||
this.operations.push(operation)
|
this.operations = operations.push(operation)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a series of `operations` to the current value.
|
* Apply a series of `operations` to the current value.
|
||||||
*
|
*
|
||||||
* @param {Array} operations
|
* @param {Array|List} operations
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
* @return {Change}
|
* @return {Change}
|
||||||
*/
|
*/
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
import Debug from 'debug'
|
import Debug from 'debug'
|
||||||
import isEqual from 'lodash/isEqual'
|
import isEqual from 'lodash/isEqual'
|
||||||
import isPlainObject from 'is-plain-object'
|
import isPlainObject from 'is-plain-object'
|
||||||
import { Record, Stack } from 'immutable'
|
import { List, Record, Stack } from 'immutable'
|
||||||
|
|
||||||
import MODEL_TYPES from '../constants/model-types'
|
import MODEL_TYPES from '../constants/model-types'
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ class History extends Record(DEFAULTS) {
|
|||||||
let { undos, redos } = history
|
let { undos, redos } = history
|
||||||
let { merge, skip } = options
|
let { merge, skip } = options
|
||||||
const prevBatch = undos.peek()
|
const prevBatch = undos.peek()
|
||||||
const prevOperation = prevBatch && prevBatch[prevBatch.length - 1]
|
const prevOperation = prevBatch && prevBatch.last()
|
||||||
|
|
||||||
if (skip == null) {
|
if (skip == null) {
|
||||||
skip = shouldSkip(operation, prevOperation)
|
skip = shouldSkip(operation, prevOperation)
|
||||||
@@ -131,15 +131,14 @@ class History extends Record(DEFAULTS) {
|
|||||||
|
|
||||||
// If the `merge` flag is true, add the operation to the previous batch.
|
// If the `merge` flag is true, add the operation to the previous batch.
|
||||||
if (merge && prevBatch) {
|
if (merge && prevBatch) {
|
||||||
const batch = prevBatch.slice()
|
const batch = prevBatch.push(operation)
|
||||||
batch.push(operation)
|
|
||||||
undos = undos.pop()
|
undos = undos.pop()
|
||||||
undos = undos.push(batch)
|
undos = undos.push(batch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, create a new batch with the operation.
|
// Otherwise, create a new batch with the operation.
|
||||||
else {
|
else {
|
||||||
const batch = [operation]
|
const batch = new List([operation])
|
||||||
undos = undos.push(batch)
|
undos = undos.push(batch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -189,9 +189,9 @@ class Schema extends Record(DEFAULTS) {
|
|||||||
return (change) => {
|
return (change) => {
|
||||||
debug(`normalizing`, { reason, context })
|
debug(`normalizing`, { reason, context })
|
||||||
const { rule } = context
|
const { rule } = context
|
||||||
const count = change.operations.length
|
const { size } = change.operations
|
||||||
if (rule.normalize) rule.normalize(change, reason, context)
|
if (rule.normalize) rule.normalize(change, reason, context)
|
||||||
if (change.operations.length > count) return
|
if (change.operations.size > size) return
|
||||||
this.normalize(change, reason, context)
|
this.normalize(change, reason, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user