mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +02:00
Add node compare to improve normalize calls
This commit is contained in:
@@ -50,7 +50,7 @@ class State extends new Record(DEFAULTS) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const state = new State({ document, selection })
|
const state = new State({ document, selection })
|
||||||
return state.transform()
|
return state.transform({ normalized: false })
|
||||||
.normalize()
|
.normalize()
|
||||||
.apply({ save: false })
|
.apply({ save: false })
|
||||||
}
|
}
|
||||||
@@ -418,12 +418,16 @@ class State extends new Record(DEFAULTS) {
|
|||||||
/**
|
/**
|
||||||
* Return a new `Transform` with the current state as a starting point.
|
* Return a new `Transform` with the current state as a starting point.
|
||||||
*
|
*
|
||||||
|
* @param {Object} properties
|
||||||
* @return {Transform} transform
|
* @return {Transform} transform
|
||||||
*/
|
*/
|
||||||
|
|
||||||
transform() {
|
transform(properties = {}) {
|
||||||
const state = this
|
const state = this
|
||||||
return new Transform({ state })
|
return new Transform({
|
||||||
|
...properties,
|
||||||
|
state
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -22,11 +22,14 @@ class Transform {
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param {Object} properties
|
* @param {Object} properties
|
||||||
|
* @param {State} properties.state
|
||||||
|
* @param {Boolean} properties.normalized
|
||||||
*/
|
*/
|
||||||
|
|
||||||
constructor(properties) {
|
constructor(properties) {
|
||||||
const { state } = properties
|
const { state, normalized = true } = properties
|
||||||
this.state = state
|
this.state = state
|
||||||
|
this.prevState = normalized ? state : null
|
||||||
this.operations = []
|
this.operations = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,6 +32,7 @@ function Plugin(options = {}) {
|
|||||||
placeholderClassName,
|
placeholderClassName,
|
||||||
placeholderStyle,
|
placeholderStyle,
|
||||||
} = options
|
} = options
|
||||||
|
let prevState
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On before change, enforce the editor's schema.
|
* On before change, enforce the editor's schema.
|
||||||
@@ -45,9 +46,14 @@ function Plugin(options = {}) {
|
|||||||
if (state.isNative) return state
|
if (state.isNative) return state
|
||||||
const schema = editor.getSchema()
|
const schema = editor.getSchema()
|
||||||
|
|
||||||
return state.transform()
|
console.time('onBeforeChange');
|
||||||
.normalizeWith(schema)
|
const newState = state.transform()
|
||||||
|
.normalizeWith(schema, prevState ? prevState.document : null)
|
||||||
.apply({ save: false })
|
.apply({ save: false })
|
||||||
|
console.timeEnd('onBeforeChange');
|
||||||
|
|
||||||
|
prevState = newState
|
||||||
|
return newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -27,17 +27,19 @@ 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) {
|
function _normalizeChildrenWith(transform, schema, node, prevNode) {
|
||||||
if (!node.nodes) {
|
if (!node.nodes) {
|
||||||
return transform
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
return node.nodes.reduce(
|
return node.nodes.reduce(
|
||||||
(t, child) => {
|
(t, child) => {
|
||||||
return t.normalizeNodeWith(schema, child)
|
const prevChild = prevNode ? prevNode.getChild(child) : null
|
||||||
|
return t.normalizeNodeWith(schema, child, prevChild)
|
||||||
},
|
},
|
||||||
transform
|
transform
|
||||||
)
|
)
|
||||||
@@ -93,12 +95,18 @@ function _normalizeNodeWith(transform, schema, node) {
|
|||||||
* @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) {
|
export function normalizeNodeWith(transform, schema, node, prevNode) {
|
||||||
|
// Node has not changed
|
||||||
|
if (prevNode == node) {
|
||||||
|
return transform
|
||||||
|
}
|
||||||
|
|
||||||
// Iterate over its children
|
// Iterate over its children
|
||||||
transform = _normalizeChildrenWith(transform, schema, node)
|
transform = _normalizeChildrenWith(transform, schema, node, prevNode)
|
||||||
|
|
||||||
// Refresh the node reference, and normalize it
|
// Refresh the node reference, and normalize it
|
||||||
node = _refreshNode(transform, node)
|
node = _refreshNode(transform, node)
|
||||||
@@ -144,10 +152,11 @@ 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) {
|
export function normalizeWith(transform, schema, prevDocument) {
|
||||||
const { state } = transform
|
const { state } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
|
|
||||||
@@ -156,7 +165,7 @@ export function normalizeWith(transform, schema) {
|
|||||||
return transform
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
return transform.normalizeNodeWith(schema, document)
|
return transform.normalizeNodeWith(schema, document, prevDocument)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -167,9 +176,12 @@ export function normalizeWith(transform, schema) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalize(transform) {
|
export function normalize(transform) {
|
||||||
return transform
|
console.time('normalize')
|
||||||
|
transform = transform
|
||||||
.normalizeDocument()
|
.normalizeDocument()
|
||||||
.normalizeSelection()
|
.normalizeSelection()
|
||||||
|
console.timeEnd('normalize')
|
||||||
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,7 +192,11 @@ export function normalize(transform) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeDocument(transform) {
|
export function normalizeDocument(transform) {
|
||||||
return transform.normalizeWith(defaultSchema)
|
const { state, prevState } = transform
|
||||||
|
const { document } = state
|
||||||
|
const { document: prevDocument } = prevState || {}
|
||||||
|
|
||||||
|
return transform.normalizeWith(defaultSchema, prevDocument)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,11 +208,17 @@ export function normalizeDocument(transform) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeNodeByKey(transform, key) {
|
export function normalizeNodeByKey(transform, key) {
|
||||||
const { state } = transform
|
const { state, prevState } = transform
|
||||||
const { document } = state
|
const { document } = state
|
||||||
const node = document.key == key ? document : document.assertDescendant(key)
|
const { document: prevDocument } = prevState || {}
|
||||||
|
|
||||||
return transform.normalizeNodeWith(defaultSchema, node)
|
const node = document.key == key ? document : document.assertDescendant(key)
|
||||||
|
const prevNode = document.key == key ? prevDocument : prevDocument.getDescendant(key)
|
||||||
|
|
||||||
|
console.time('normalizeNodeByKey')
|
||||||
|
transform = transform.normalizeNodeWith(defaultSchema, node, prevNode)
|
||||||
|
console.timeEnd('normalizeNodeByKey')
|
||||||
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -208,11 +230,15 @@ export function normalizeNodeByKey(transform, key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeParentsByKey(transform, key) {
|
export function normalizeParentsByKey(transform, key) {
|
||||||
const { state } = transform
|
const { state, prevState } = 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)
|
||||||
return transform.normalizeParentsWith(defaultSchema, node)
|
console.time('normalizeParentsByKey')
|
||||||
|
transform = transform.normalizeParentsWith(defaultSchema, node, prevNode)
|
||||||
|
console.timeEnd('normalizeParentsByKey')
|
||||||
|
return transform
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -223,6 +249,7 @@ export function normalizeParentsByKey(transform, key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function normalizeSelection(transform) {
|
export function normalizeSelection(transform) {
|
||||||
|
console.time('normalizeSelection')
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { document, selection } = state
|
||||||
selection = selection.normalize(document)
|
selection = selection.normalize(document)
|
||||||
@@ -246,5 +273,6 @@ export function normalizeSelection(transform) {
|
|||||||
|
|
||||||
state = state.merge({ selection })
|
state = state.merge({ selection })
|
||||||
transform.state = state
|
transform.state = state
|
||||||
|
console.timeEnd('normalizeSelection')
|
||||||
return transform
|
return transform
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user