From a0f5369525a109745e20ec42192a2a7dfdc5d585 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Fri, 24 Jun 2016 10:46:01 -0700 Subject: [PATCH] move before input handling to core plugin --- lib/components/content.js | 27 ++------------------------- lib/components/editor.js | 3 ++- lib/plugins/core.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/lib/components/content.js b/lib/components/content.js index 184494578..a4b8cd83f 100644 --- a/lib/components/content.js +++ b/lib/components/content.js @@ -3,7 +3,6 @@ import OffsetKey from '../utils/offset-key' import React from 'react' import ReactDOM from 'react-dom' import Text from './text' -import TextModel from '../models/text' import keycode from 'keycode' /** @@ -17,6 +16,7 @@ class Content extends React.Component { */ static propTypes = { + onBeforeInput: React.PropTypes.func, onChange: React.PropTypes.func, onKeyDown: React.PropTypes.func, onSelect: React.PropTypes.func, @@ -112,30 +112,7 @@ class Content extends React.Component { */ onBeforeInput(e) { - let { state } = this.props - const { selection } = state - const { data } = e - if (!data) return - - // If the selection is still expanded, delete anything inside it first. - if (selection.isExpanded) { - e.preventDefault() - state = state - .transform() - .delete() - .insertText(data) - .apply() - } - - // Otherwise, insert text. - else { - state = state - .transform() - .insertText(data) - .apply({ isNative: true }) - } - - this.onChange(state) + this.props.onBeforeInput(e) } /** diff --git a/lib/components/editor.js b/lib/components/editor.js index 2793d6015..ca5faddeb 100644 --- a/lib/components/editor.js +++ b/lib/components/editor.js @@ -104,9 +104,10 @@ class Editor extends React.Component { this.onChange(state)} - onKeyDown={e => this.onEvent('onKeyDown', e)} renderMark={mark => this.renderMark(mark)} renderNode={node => this.renderNode(node)} + onBeforeInput={e => this.onEvent('onBeforeInput', e)} + onKeyDown={e => this.onEvent('onKeyDown', e)} /> ) } diff --git a/lib/plugins/core.js b/lib/plugins/core.js index 70a59deee..b8af26bdd 100644 --- a/lib/plugins/core.js +++ b/lib/plugins/core.js @@ -9,6 +9,41 @@ import { IS_WINDOWS, IS_MAC } from '../utils/environment' export default { + /** + * The core `onBeforeInput` handler. + * + * @param {Event} e + * @param {State} state + * @param {Editor} editor + * @return {State or Null} newState + */ + + onBeforeInput(e, state, editor) { + const { data } = e + + // When does this happen... + if (!data) { + debugger + return + } + + // If the selection is still expanded, delete anything inside it first. + if (state.isExpanded) { + e.preventDefault() + return state + .transform() + .delete() + .insertText(data) + .apply() + } + + // Otherwise, insert text natively, without re-rendering. + return state + .transform() + .insertText(data) + .apply({ isNative: true }) + }, + /** * The core `onKeyDown` handler. *