mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 13:11:17 +02:00
Move normalize logic to src/transforms/normalize.js
This commit is contained in:
@@ -206,39 +206,6 @@ class Schema extends new Record(DEFAULTS) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the node using a schema, by pushing operations to a transform.
|
||||
* "prevNode" can be used to prevent iterating over all children.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @param {Node} node
|
||||
* @param {Node} prevNode?
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
__normalize(transform, node, prevNode) {
|
||||
if (prevNode === node) {
|
||||
return transform
|
||||
}
|
||||
|
||||
// Normalize children
|
||||
if (node.nodes) {
|
||||
node.nodes.forEach(child => {
|
||||
const prevChild = prevNode ? prevNode.getChild(child.key) : null
|
||||
this.__normalize(transform, child, prevChild)
|
||||
})
|
||||
}
|
||||
|
||||
// Normalize the node itself
|
||||
let failure
|
||||
if (failure = this.__validate(node)) {
|
||||
const { value, rule } = failure
|
||||
rule.normalize(transform, node, value)
|
||||
}
|
||||
|
||||
return transform
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -414,23 +414,6 @@ class State extends new Record(DEFAULTS) {
|
||||
: this.document.getTextsAtRange(this.selection)
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a state against a `schema`.
|
||||
*
|
||||
* @param {Schema} schema
|
||||
* @return {State}
|
||||
*/
|
||||
|
||||
normalize(schema) {
|
||||
const state = this
|
||||
const { document, selection } = this
|
||||
let transform = this.transform()
|
||||
|
||||
transform = schema.__normalize(transform, document, null)
|
||||
|
||||
return transform.apply({ save: false })
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new `Transform` with the current state as a starting point.
|
||||
*
|
||||
|
@@ -1,6 +1,40 @@
|
||||
import Schema from '../models/schema'
|
||||
import { default as defaultSchema } from '../plugins/schema'
|
||||
|
||||
|
||||
/**
|
||||
* Normalize a node using a schema, by pushing operations to a transform.
|
||||
* "prevNode" can be used to prevent iterating over all children.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @param {Node} node
|
||||
* @param {Node} prevNode?
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
function normalizeNode(transform, schema, node, prevNode) {
|
||||
if (prevNode === node) {
|
||||
return transform
|
||||
}
|
||||
|
||||
// Normalize children
|
||||
if (node.nodes) {
|
||||
transform = node.nodes.reduce((t, child) => {
|
||||
const prevChild = prevNode ? prevNode.getChild(child.key) : null
|
||||
return normalizeNode(transform, schema, child, prevChild)
|
||||
}, transform)
|
||||
}
|
||||
|
||||
// Normalize the node itself
|
||||
let failure
|
||||
if (failure = schema.__validate(node)) {
|
||||
const { value, rule } = failure
|
||||
transform = rule.normalize(transform, node, value)
|
||||
}
|
||||
|
||||
return transform
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the state with a schema.
|
||||
*
|
||||
@@ -12,7 +46,7 @@ import { default as defaultSchema } from '../plugins/schema'
|
||||
export function normalizeWith(transform, schema) {
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
return schema.__normalize(transform, document, null)
|
||||
return normalizeNode(transform, schema, document, null)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,33 +59,6 @@ export function normalizeWith(transform, schema) {
|
||||
|
||||
export function normalize(transform) {
|
||||
return transform.normalizeWith(defaultSchema)
|
||||
/*
|
||||
let { state } = transform
|
||||
let { document, selection } = state
|
||||
let failure
|
||||
|
||||
// Normalize all of the document's nodes.
|
||||
document.filterDescendantsDeep((node) => {
|
||||
if (failure = node.validate(SCHEMA)) {
|
||||
const { value, rule } = failure
|
||||
rule.normalize(transform, node, value)
|
||||
}
|
||||
})
|
||||
|
||||
// Normalize the document itself.
|
||||
if (failure = document.validate(SCHEMA)) {
|
||||
const { value, rule } = failure
|
||||
rule.normalize(transform, document, value)
|
||||
}
|
||||
|
||||
// Normalize the selection.
|
||||
// TODO: turn this into schema rules.
|
||||
state = transform.state
|
||||
document = state.document
|
||||
let nextSelection = selection.normalize(document)
|
||||
if (!selection.equals(nextSelection)) transform.setSelection(selection)
|
||||
return transform
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user