diff --git a/src/models/node.js b/src/models/node.js index ad133c8e6..f7ca75c3f 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -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 } + } /** diff --git a/src/models/schema.js b/src/models/schema.js index 9d7d3abef..1b7967c5e 100644 --- a/src/models/schema.js +++ b/src/models/schema.js @@ -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 + } + } /** diff --git a/src/models/state.js b/src/models/state.js index 03a0e90dc..c52dc85ae 100644 --- a/src/models/state.js +++ b/src/models/state.js @@ -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 }) } diff --git a/src/plugins/core.js b/src/plugins/core.js index b50b9c340..fe6a18fc4 100644 --- a/src/plugins/core.js +++ b/src/plugins/core.js @@ -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 }) } /** diff --git a/src/transforms/index.js b/src/transforms/index.js index 7d846cc4d..7fc192338 100644 --- a/src/transforms/index.js +++ b/src/transforms/index.js @@ -144,8 +144,8 @@ import { import { normalize, + normalizeWith, normalizeDocument, - normalizeSelection, } from './normalize' /** @@ -286,7 +286,7 @@ export default { */ normalize, + normalizeWith, normalizeDocument, - normalizeSelection, } diff --git a/src/transforms/normalize.js b/src/transforms/normalize.js index 505a1c78b..12169ea52 100644 --- a/src/transforms/normalize.js +++ b/src/transforms/normalize.js @@ -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 + */ } /**