From f442255739907556bd62e11081bb3e111f162374 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Fri, 29 Sep 2017 09:11:41 -0700 Subject: [PATCH] re-add the onBeforeChange handling for normalizing to the (#1195) * re-add the onBeforeChange handling for normalizing to the * fix syntax * add normalization with core schema too * fix lint warning --- packages/slate-react/src/components/editor.js | 24 ++++++++------ packages/slate-react/src/plugins/core.js | 7 ++-- packages/slate/src/index.js | 32 +++++-------------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/packages/slate-react/src/components/editor.js b/packages/slate-react/src/components/editor.js index f2ce2d341..37d7976ef 100644 --- a/packages/slate-react/src/components/editor.js +++ b/packages/slate-react/src/components/editor.js @@ -114,12 +114,14 @@ class Editor extends React.Component { // Create a new `Stack`, omitting the `onChange` property since that has // special significance on the editor itself. - const { state } = props const plugins = resolvePlugins(props) const stack = Stack.create({ plugins }) this.state.stack = stack - // Cache and set the state. + // Resolve the state, running `onBeforeChange` first. + const change = props.state.change() + stack.onBeforeChange(change, this) + const { state } = change this.cacheState(state) this.state.state = state @@ -128,9 +130,9 @@ class Editor extends React.Component { const method = EVENT_HANDLERS[i] this[method] = (...args) => { const stk = this.state.stack - const change = this.state.state.change() - stk[method](change, this, ...args) - this.onChange(change) + const c = this.state.state.change() + stk[method](c, this, ...args) + this.onChange(c) } } @@ -144,24 +146,28 @@ class Editor extends React.Component { } /** - * When the `props` are updated, create a new `Stack` if necessary. + * When the `props` are updated, create a new `Stack` if necessary and run + * `onBeforeChange` to ensure the state is normalized. * * @param {Object} props */ componentWillReceiveProps = (props) => { - const { state } = props + let { stack } = this.state // If any plugin-related properties will change, create a new `Stack`. for (let i = 0; i < PLUGINS_PROPS.length; i++) { const prop = PLUGINS_PROPS[i] if (props[prop] == this.props[prop]) continue const plugins = resolvePlugins(props) - const stack = Stack.create({ plugins }) + stack = Stack.create({ plugins }) this.setState({ stack }) } - // Cache and save the state. + // Resolve the state, running the `onBeforeChange` handler of the stack. + const change = props.state.change() + stack.onBeforeChange(change, this) + const { state } = change this.cacheState(state) this.setState({ state }) } diff --git a/packages/slate-react/src/plugins/core.js b/packages/slate-react/src/plugins/core.js index b8ba7b893..1c58b3ce2 100644 --- a/packages/slate-react/src/plugins/core.js +++ b/packages/slate-react/src/plugins/core.js @@ -4,7 +4,7 @@ import Debug from 'debug' import Plain from 'slate-plain-serializer' import React from 'react' import getWindow from 'get-window' -import { Block, Inline } from 'slate' +import { Block, Inline, coreSchema } from 'slate' import Content from '../components/content' import Placeholder from '../components/placeholder' @@ -49,10 +49,11 @@ function Plugin(options = {}) { const schema = editor.getSchema() const prevState = editor.getState() - // PERF: Skip normalizing if the document hasn't changed, since the core - // schema only normalizes changes to the document, not selection. + // PERF: Skip normalizing if the document hasn't changed, since schemas only + // normalize changes to the document, not selection. if (prevState && state.document == prevState.document) return + change.normalize(coreSchema) change.normalize(schema) debug('onBeforeChange') } diff --git a/packages/slate/src/index.js b/packages/slate/src/index.js index c7ceb1da3..976689a52 100644 --- a/packages/slate/src/index.js +++ b/packages/slate/src/index.js @@ -1,9 +1,6 @@ -/** - * Models. - */ - import Block from './models/block' +import Changes from './changes' import Character from './models/character' import Data from './models/data' import Document from './models/document' @@ -11,29 +8,14 @@ import History from './models/history' import Inline from './models/inline' import Mark from './models/mark' import Node from './models/node' +import Operations from './operations' +import Range from './models/range' import Schema from './models/schema' import Selection from './models/selection' import Stack from './models/stack' import State from './models/state' import Text from './models/text' -import Range from './models/range' - -/** - * Operations. - */ - -import Operations from './operations' - -/** - * Changes. - */ - -import Changes from './changes' - -/** - * Utils. - */ - +import coreSchema from './schemas/core' import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key' /** @@ -44,6 +26,7 @@ import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key' export { Block, + Changes, Character, Data, Document, @@ -58,13 +41,14 @@ export { Stack, State, Text, - Changes, + coreSchema, resetKeyGenerator, setKeyGenerator, } export default { Block, + Changes, Character, Data, Document, @@ -79,7 +63,7 @@ export default { Stack, State, Text, - Changes, + coreSchema, resetKeyGenerator, setKeyGenerator, }