1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 18:09:49 +02:00

re-add the onBeforeChange handling for normalizing to the <Editor> (#1195)

* re-add the onBeforeChange handling for normalizing to the <Editor>

* fix syntax

* add normalization with core schema too

* fix lint warning
This commit is contained in:
Ian Storm Taylor
2017-09-29 09:11:41 -07:00
committed by GitHub
parent 563e2bc5c0
commit f442255739
3 changed files with 27 additions and 36 deletions

View File

@@ -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 })
}

View File

@@ -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')
}

View File

@@ -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,
}