mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 18:39:51 +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:
@@ -114,12 +114,14 @@ class Editor extends React.Component {
|
|||||||
|
|
||||||
// Create a new `Stack`, omitting the `onChange` property since that has
|
// Create a new `Stack`, omitting the `onChange` property since that has
|
||||||
// special significance on the editor itself.
|
// special significance on the editor itself.
|
||||||
const { state } = props
|
|
||||||
const plugins = resolvePlugins(props)
|
const plugins = resolvePlugins(props)
|
||||||
const stack = Stack.create({ plugins })
|
const stack = Stack.create({ plugins })
|
||||||
this.state.stack = stack
|
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.cacheState(state)
|
||||||
this.state.state = state
|
this.state.state = state
|
||||||
|
|
||||||
@@ -128,9 +130,9 @@ class Editor extends React.Component {
|
|||||||
const method = EVENT_HANDLERS[i]
|
const method = EVENT_HANDLERS[i]
|
||||||
this[method] = (...args) => {
|
this[method] = (...args) => {
|
||||||
const stk = this.state.stack
|
const stk = this.state.stack
|
||||||
const change = this.state.state.change()
|
const c = this.state.state.change()
|
||||||
stk[method](change, this, ...args)
|
stk[method](c, this, ...args)
|
||||||
this.onChange(change)
|
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
|
* @param {Object} props
|
||||||
*/
|
*/
|
||||||
|
|
||||||
componentWillReceiveProps = (props) => {
|
componentWillReceiveProps = (props) => {
|
||||||
const { state } = props
|
let { stack } = this.state
|
||||||
|
|
||||||
// If any plugin-related properties will change, create a new `Stack`.
|
// If any plugin-related properties will change, create a new `Stack`.
|
||||||
for (let i = 0; i < PLUGINS_PROPS.length; i++) {
|
for (let i = 0; i < PLUGINS_PROPS.length; i++) {
|
||||||
const prop = PLUGINS_PROPS[i]
|
const prop = PLUGINS_PROPS[i]
|
||||||
if (props[prop] == this.props[prop]) continue
|
if (props[prop] == this.props[prop]) continue
|
||||||
const plugins = resolvePlugins(props)
|
const plugins = resolvePlugins(props)
|
||||||
const stack = Stack.create({ plugins })
|
stack = Stack.create({ plugins })
|
||||||
this.setState({ stack })
|
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.cacheState(state)
|
||||||
this.setState({ state })
|
this.setState({ state })
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ import Debug from 'debug'
|
|||||||
import Plain from 'slate-plain-serializer'
|
import Plain from 'slate-plain-serializer'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import getWindow from 'get-window'
|
import getWindow from 'get-window'
|
||||||
import { Block, Inline } from 'slate'
|
import { Block, Inline, coreSchema } from 'slate'
|
||||||
|
|
||||||
import Content from '../components/content'
|
import Content from '../components/content'
|
||||||
import Placeholder from '../components/placeholder'
|
import Placeholder from '../components/placeholder'
|
||||||
@@ -49,10 +49,11 @@ function Plugin(options = {}) {
|
|||||||
const schema = editor.getSchema()
|
const schema = editor.getSchema()
|
||||||
const prevState = editor.getState()
|
const prevState = editor.getState()
|
||||||
|
|
||||||
// PERF: Skip normalizing if the document hasn't changed, since the core
|
// PERF: Skip normalizing if the document hasn't changed, since schemas only
|
||||||
// schema only normalizes changes to the document, not selection.
|
// normalize changes to the document, not selection.
|
||||||
if (prevState && state.document == prevState.document) return
|
if (prevState && state.document == prevState.document) return
|
||||||
|
|
||||||
|
change.normalize(coreSchema)
|
||||||
change.normalize(schema)
|
change.normalize(schema)
|
||||||
debug('onBeforeChange')
|
debug('onBeforeChange')
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,6 @@
|
|||||||
|
|
||||||
/**
|
|
||||||
* Models.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import Block from './models/block'
|
import Block from './models/block'
|
||||||
|
import Changes from './changes'
|
||||||
import Character from './models/character'
|
import Character from './models/character'
|
||||||
import Data from './models/data'
|
import Data from './models/data'
|
||||||
import Document from './models/document'
|
import Document from './models/document'
|
||||||
@@ -11,29 +8,14 @@ import History from './models/history'
|
|||||||
import Inline from './models/inline'
|
import Inline from './models/inline'
|
||||||
import Mark from './models/mark'
|
import Mark from './models/mark'
|
||||||
import Node from './models/node'
|
import Node from './models/node'
|
||||||
|
import Operations from './operations'
|
||||||
|
import Range from './models/range'
|
||||||
import Schema from './models/schema'
|
import Schema from './models/schema'
|
||||||
import Selection from './models/selection'
|
import Selection from './models/selection'
|
||||||
import Stack from './models/stack'
|
import Stack from './models/stack'
|
||||||
import State from './models/state'
|
import State from './models/state'
|
||||||
import Text from './models/text'
|
import Text from './models/text'
|
||||||
import Range from './models/range'
|
import coreSchema from './schemas/core'
|
||||||
|
|
||||||
/**
|
|
||||||
* Operations.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import Operations from './operations'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import Changes from './changes'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utils.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key'
|
import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,6 +26,7 @@ import { resetKeyGenerator, setKeyGenerator } from './utils/generate-key'
|
|||||||
|
|
||||||
export {
|
export {
|
||||||
Block,
|
Block,
|
||||||
|
Changes,
|
||||||
Character,
|
Character,
|
||||||
Data,
|
Data,
|
||||||
Document,
|
Document,
|
||||||
@@ -58,13 +41,14 @@ export {
|
|||||||
Stack,
|
Stack,
|
||||||
State,
|
State,
|
||||||
Text,
|
Text,
|
||||||
Changes,
|
coreSchema,
|
||||||
resetKeyGenerator,
|
resetKeyGenerator,
|
||||||
setKeyGenerator,
|
setKeyGenerator,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Block,
|
Block,
|
||||||
|
Changes,
|
||||||
Character,
|
Character,
|
||||||
Data,
|
Data,
|
||||||
Document,
|
Document,
|
||||||
@@ -79,7 +63,7 @@ export default {
|
|||||||
Stack,
|
Stack,
|
||||||
State,
|
State,
|
||||||
Text,
|
Text,
|
||||||
Changes,
|
coreSchema,
|
||||||
resetKeyGenerator,
|
resetKeyGenerator,
|
||||||
setKeyGenerator,
|
setKeyGenerator,
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user