mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-25 00:06:30 +02:00
Add transform.normalizeWith
This commit is contained in:
@@ -14,7 +14,6 @@ import includes from 'lodash/includes'
|
||||
import memoize from '../utils/memoize'
|
||||
import uid from '../utils/uid'
|
||||
import { List, Map, OrderedSet, Set } from 'immutable'
|
||||
import { default as defaultSchema } from '../plugins/schema'
|
||||
|
||||
|
||||
/**
|
||||
@@ -1175,38 +1174,8 @@ const Node = {
|
||||
|
||||
validate(schema) {
|
||||
return schema.__validate(this)
|
||||
},
|
||||
|
||||
/**
|
||||
* 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 {Schema} schema
|
||||
* @param {Node} prevNode?
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
normalize(transform, schema, prevNode) {
|
||||
if (prevNode === this) {
|
||||
return this
|
||||
}
|
||||
|
||||
// Normalize children
|
||||
this.nodes.forEach(child => {
|
||||
const prevChild = prevNode ? prevNode.getChild(child.key) : null
|
||||
child.normalize(transform, schema, prevChild)
|
||||
})
|
||||
|
||||
// Normalize the node itself
|
||||
let failure
|
||||
if (failure = this.validate(schema)) {
|
||||
const { value, rule } = failure
|
||||
rule.normalize(transform, this, value)
|
||||
}
|
||||
|
||||
return transform
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -206,6 +206,39 @@ 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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -426,7 +426,7 @@ class State extends new Record(DEFAULTS) {
|
||||
const { document, selection } = this
|
||||
let transform = this.transform()
|
||||
|
||||
transform = document.normalize(transform, schema, null)
|
||||
transform = schema.__normalize(transform, document, null)
|
||||
|
||||
return transform.apply({ save: false })
|
||||
}
|
||||
|
@@ -45,7 +45,10 @@ function Plugin(options = {}) {
|
||||
function onBeforeChange(state, editor) {
|
||||
if (state.isNative) return state
|
||||
const schema = editor.getSchema()
|
||||
return state.normalize(schema)
|
||||
|
||||
return state.transform()
|
||||
.normalizeWith(schema)
|
||||
.apply({ save: false })
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -144,8 +144,8 @@ import {
|
||||
|
||||
import {
|
||||
normalize,
|
||||
normalizeWith,
|
||||
normalizeDocument,
|
||||
normalizeSelection,
|
||||
} from './normalize'
|
||||
|
||||
/**
|
||||
@@ -286,7 +286,7 @@ export default {
|
||||
*/
|
||||
|
||||
normalize,
|
||||
normalizeWith,
|
||||
normalizeDocument,
|
||||
normalizeSelection,
|
||||
|
||||
}
|
||||
|
@@ -1,15 +1,32 @@
|
||||
|
||||
import Schema from '../models/schema'
|
||||
|
||||
import { default as defaultSchema } from '../plugins/schema'
|
||||
|
||||
/**
|
||||
* Normalize the state.
|
||||
* Normalize the state with a schema.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @param {Schema} schema
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
export function normalizeWith(transform, schema) {
|
||||
const { state } = transform
|
||||
const { document } = state
|
||||
return schema.__normalize(transform, document, null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the state using the core schema.
|
||||
* TODO: calling this transform should be useless
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
export function normalize(transform) {
|
||||
return transform.normalizeWith(defaultSchema)
|
||||
/*
|
||||
let { state } = transform
|
||||
let { document, selection } = state
|
||||
let failure
|
||||
@@ -35,22 +52,7 @@ export function normalize(transform) {
|
||||
let nextSelection = selection.normalize(document)
|
||||
if (!selection.equals(nextSelection)) transform.setSelection(selection)
|
||||
return transform
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the document.
|
||||
*
|
||||
* @param {Transform} transform
|
||||
* @return {Transform}
|
||||
*/
|
||||
|
||||
export function normalizeDocument(transform) {
|
||||
let { state } = transform
|
||||
let { document } = state
|
||||
document = document.normalize()
|
||||
state = state.merge({ document })
|
||||
transform.state = state
|
||||
return transform
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user