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