1
0
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:
Samy Pesse
2016-10-19 00:41:01 +02:00
parent 808ae3c1fb
commit d2d7122ffc
3 changed files with 35 additions and 78 deletions

View File

@@ -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
}
} }
/** /**

View File

@@ -414,23 +414,6 @@ class State extends new Record(DEFAULTS) {
: this.document.getTextsAtRange(this.selection) : 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. * Return a new `Transform` with the current state as a starting point.
* *

View File

@@ -1,6 +1,40 @@
import Schema from '../models/schema' import Schema from '../models/schema'
import { default as defaultSchema } from '../plugins/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. * Normalize the state with a schema.
* *
@@ -12,7 +46,7 @@ import { default as defaultSchema } from '../plugins/schema'
export function normalizeWith(transform, schema) { export function normalizeWith(transform, schema) {
const { state } = transform const { state } = transform
const { document } = state 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) { export function normalize(transform) {
return transform.normalizeWith(defaultSchema) 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
*/
} }
/** /**