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

fix on before input

This commit is contained in:
Ian Storm Taylor
2016-06-28 08:42:34 -07:00
parent f34e4b4a04
commit 8213573fb5

View File

@@ -13,6 +13,9 @@ export default {
/** /**
* The core `onBeforeInput` handler. * The core `onBeforeInput` handler.
* *
* If the current selection is collapsed, we can insert the text natively and
* avoid a re-render, improving performance.
*
* @param {Event} e * @param {Event} e
* @param {State} state * @param {State} state
* @param {Editor} editor * @param {Editor} editor
@@ -20,29 +23,14 @@ export default {
*/ */
onBeforeInput(e, state, editor) { onBeforeInput(e, state, editor) {
const { data } = e const isNative = state.isCollapsed
// When does this happen... if (!isNative) e.preventDefault()
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 return state
.transform() .transform()
.insertText(data) .insertText(e.data)
.apply({ isNative: true }) .apply({ isNative })
}, },
/** /**
@@ -56,55 +44,35 @@ export default {
onKeyDown(e, state, editor) { onKeyDown(e, state, editor) {
const key = keycode(e.which) const key = keycode(e.which)
const transform = state.transform()
switch (key) { switch (key) {
case 'enter': { case 'enter': {
return state return transform.splitBlock().apply()
.transform()
.splitBlock()
.apply()
} }
case 'backspace': { case 'backspace': {
return isWord(e) return isWord(e)
? state ? transform.backspaceWord().apply()
.transform() : transform.deleteBackward().apply()
.backspaceWord()
.apply()
: state
.transform()
.deleteBackward()
.apply()
} }
case 'delete': { case 'delete': {
return isWord(e) return isWord(e)
? state ? transform.deleteWord().apply()
.transform() : transform.deleteForward().apply()
.deleteWord()
.apply()
: state
.transform()
.deleteForward()
.apply()
} }
case 'y': { case 'y': {
if (!isWindowsCommand(e)) return if (!isWindowsCommand(e)) return
return state return transform.redo()
.transform()
.redo()
} }
case 'z': { case 'z': {
if (!isCommand(e)) return if (!isCommand(e)) return
return IS_MAC && e.shiftKey return IS_MAC && e.shiftKey
? state ? transform.redo()
.transform() : transform.undo()
.redo()
: state
.transform()
.undo()
} }
} }
}, },
@@ -142,7 +110,9 @@ export default {
*/ */
renderNode(node) { renderNode(node) {
return (props) => <div>{props.children}</div> return node.kind == 'block'
? (props) => <div>{props.children}</div>
: (props) => <span>{props.children}</span>
}, },
/** /**