1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 20:24:01 +02:00

refactor onBeforeChange

This commit is contained in:
Ian Storm Taylor
2016-08-04 13:18:20 -07:00
parent f25c5d9a64
commit 917906e378
2 changed files with 28 additions and 27 deletions

View File

@@ -102,17 +102,21 @@ class Editor extends React.Component {
} }
/** /**
* When the `props` are updated, recompute the plugins. * When the `props` are updated, recompute the state and plugins.
* *
* @param {Object} props * @param {Object} props
*/ */
componentWillReceiveProps = (props) => { componentWillReceiveProps = (props) => {
this.state.state = this.onBeforeChange(props.state)
if (props.plugins != this.props.plugins) { if (props.plugins != this.props.plugins) {
this.setState({ plugins: this.resolvePlugins(props) }) this.setState({
plugins: this.resolvePlugins(props)
})
} }
this.setState({
state: this.onBeforeChange(props.state)
})
} }
/** /**
@@ -151,6 +155,26 @@ class Editor extends React.Component {
return this.state.state return this.state.state
} }
/**
* When the editor receives a new 'state'
*
* @param {State} state
* @return {State} newState
*/
onBeforeChange = (state) => {
if (state == this.state.state) return
for (const plugin of this.state.plugins) {
if (!plugin.onBeforeChange) continue
const newState = plugin.onBeforeChange(state, this)
if (newState == null) continue
state = newState
}
return state
}
/** /**
* When the `state` changes, pass through plugins, then bubble up. * When the `state` changes, pass through plugins, then bubble up.
* *
@@ -182,26 +206,6 @@ class Editor extends React.Component {
} }
} }
/**
* When the editor receives a new 'state'
*
* @param {State} state
* @return {State} newState
*/
onBeforeChange = (state) => {
if (state == this.state.state) return
for (const plugin of this.state.plugins) {
if (!plugin.onBeforeChange) continue
const newState = plugin.onBeforeChange(state, this)
if (newState == null) continue
state = newState
}
return state
}
/** /**
* When an event by `name` fires, pass it through the plugins, and update the * When an event by `name` fires, pass it through the plugins, and update the
* state if one of them chooses to. * state if one of them chooses to.

View File

@@ -21,7 +21,6 @@ const debug = Debug('slate:core')
* @property {Element} placeholder * @property {Element} placeholder
* @property {String} placeholderClassName * @property {String} placeholderClassName
* @property {Object} placeholderStyle * @property {Object} placeholderStyle
* @property {Function} onBeforeChange
* @return {Object} * @return {Object}
*/ */
@@ -30,7 +29,6 @@ function Plugin(options = {}) {
placeholder, placeholder,
placeholderClassName, placeholderClassName,
placeholderStyle, placeholderStyle,
onBeforeChange
} = options } = options
/** /**
@@ -614,7 +612,6 @@ function Plugin(options = {}) {
*/ */
return { return {
onBeforeChange,
onBeforeInput, onBeforeInput,
onBlur, onBlur,
onCopy, onCopy,