1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 09:59:48 +02:00

Remove previous document comparison logic

This commit is contained in:
Soreine
2016-11-14 11:05:08 +01:00
committed by Nicolas Gaborit
parent db7ae6aae6
commit 56c2df067c
2 changed files with 13 additions and 40 deletions

View File

@@ -53,7 +53,7 @@ function Plugin(options = {}) {
if (prevState && state.document == prevState.document) return state if (prevState && state.document == prevState.document) return state
const newState = state.transform() const newState = state.transform()
.normalizeWith(schema, prevState ? prevState.document : null) .normalizeWith(schema)
.apply({ save: false }) .apply({ save: false })
return newState return newState

View File

@@ -1,7 +1,6 @@
import warning from '../utils/warning' import warning from '../utils/warning'
import { default as defaultSchema } from '../plugins/schema' import { default as defaultSchema } from '../plugins/schema'
import Normalize from '../utils/normalize' import Normalize from '../utils/normalize'
import { Map } from 'immutable'
// Maximum recursive calls for normalization // Maximum recursive calls for normalization
const MAX_CALLS = 50 const MAX_CALLS = 50
@@ -12,21 +11,15 @@ const MAX_CALLS = 50
* @param {Transform} transform * @param {Transform} transform
* @param {Schema} schema * @param {Schema} schema
* @param {Node} node * @param {Node} node
* @param {Node} prevNode
* @return {Transform} * @return {Transform}
*/ */
export function normalizeNodeWith(transform, schema, node, prevNode) { export function normalizeNodeWith(transform, schema, node) {
// Node has not changed
if (prevNode == node) {
return transform
}
// For performance considerations, we will check if the transform was changed // For performance considerations, we will check if the transform was changed
const opCount = transform.operations.length const opCount = transform.operations.length
// Iterate over its children // Iterate over its children
normalizeChildrenWith(transform, schema, node, prevNode) normalizeChildrenWith(transform, schema, node)
const hasChanged = transform.operations.length != opCount const hasChanged = transform.operations.length != opCount
if (hasChanged) { if (hasChanged) {
@@ -77,11 +70,10 @@ export function normalizeParentsWith(transform, schema, node) {
* *
* @param {Transform} transform * @param {Transform} transform
* @param {Schema} schema * @param {Schema} schema
* @param {Document} prevDocument
* @return {Transform} transform * @return {Transform} transform
*/ */
export function normalizeWith(transform, schema, prevDocument) { export function normalizeWith(transform, schema) {
const { state } = transform const { state } = transform
const { document } = state const { document } = state
@@ -90,7 +82,7 @@ export function normalizeWith(transform, schema, prevDocument) {
return transform return transform
} }
return transform.normalizeNodeWith(schema, document, prevDocument) return transform.normalizeNodeWith(schema, document)
} }
/** /**
@@ -115,10 +107,7 @@ export function normalize(transform) {
*/ */
export function normalizeDocument(transform) { export function normalizeDocument(transform) {
const { prevState } = transform return transform.normalizeWith(defaultSchema)
const { document: prevDocument } = prevState || {}
return transform.normalizeWith(defaultSchema, prevDocument)
} }
/** /**
@@ -131,14 +120,12 @@ export function normalizeDocument(transform) {
export function normalizeNodeByKey(transform, key) { export function normalizeNodeByKey(transform, key) {
key = Normalize.key(key) key = Normalize.key(key)
const { state, prevState } = transform const { state } = transform
const { document } = state const { document } = state
const { document: prevDocument } = prevState || {}
const node = document.key == key ? document : document.assertDescendant(key) const node = document.key == key ? document : document.assertDescendant(key)
const prevNode = document.key == key ? prevDocument : prevDocument.getDescendant(key)
transform.normalizeNodeWith(defaultSchema, node, prevNode) transform.normalizeNodeWith(defaultSchema, node)
return transform return transform
} }
@@ -152,13 +139,11 @@ export function normalizeNodeByKey(transform, key) {
export function normalizeParentsByKey(transform, key) { export function normalizeParentsByKey(transform, key) {
key = Normalize.key(key) key = Normalize.key(key)
const { state, prevState } = transform const { state } = transform
const { document } = state const { document } = state
const { document: prevDocument } = prevState || {}
const node = document.key == key ? document : document.assertDescendant(key) const node = document.key == key ? document : document.assertDescendant(key)
const prevNode = document.key == key ? prevDocument : prevDocument.getDescendant(key)
transform.normalizeParentsWith(defaultSchema, node, prevNode) transform.normalizeParentsWith(defaultSchema, node)
return transform return transform
} }
@@ -220,28 +205,16 @@ function refreshNode(transform, node) {
* @param {Transform} transform * @param {Transform} transform
* @param {Schema} schema * @param {Schema} schema
* @param {Node} node * @param {Node} node
* @param {Node} prevNode
* @return {Transform} transform * @return {Transform} transform
*/ */
function normalizeChildrenWith(transform, schema, node, prevNode) { function normalizeChildrenWith(transform, schema, node) {
if ( if (node.kind == 'text') {
node.kind == 'text'
|| (prevNode && prevNode.nodes == node.nodes)
) {
return transform return transform
} }
// Create a map beforehand, for performant lookup
const prevChildrenMap = new Map().withMutations(map => {
if (prevNode) prevNode.nodes.forEach(n => map.set(n.key, n))
})
return node.nodes.reduce( return node.nodes.reduce(
(t, child) => { (t, child) => t.normalizeNodeWith(schema, child),
const prevChild = prevChildrenMap.get(child.key)
return t.normalizeNodeWith(schema, child, prevChild)
},
transform transform
) )
} }