1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-23 15:32:59 +02:00
This commit is contained in:
Ian Storm Taylor
2016-06-16 12:21:39 -07:00
parent de6aeb9dfe
commit ec7da72562
3 changed files with 13 additions and 9 deletions

View File

@@ -65,7 +65,7 @@ class Editor extends React.Component {
onKeyDown(e) { onKeyDown(e) {
for (const plugin of this.state.plugins) { for (const plugin of this.state.plugins) {
if (plugin.onKeyDown) { if (plugin.onKeyDown) {
const newState = plugin.onKeyDown(e, this) const newState = plugin.onKeyDown(e, this.props.state, this)
if (newState == null) continue if (newState == null) continue
this.props.onChange(newState) this.props.onChange(newState)
break break

View File

@@ -53,7 +53,7 @@ class State extends StateRecord {
setNode(key, node) { setNode(key, node) {
if (this.nodes.get(key)) { if (this.nodes.get(key)) {
const nodes = this.nodes.set(key, node) const nodes = this.nodes.set(key, node)
return this.set('nodes', nodes) return this.merge({ nodes })
} }
const nodes = this.nodes.map((child) => { const nodes = this.nodes.map((child) => {
@@ -62,7 +62,7 @@ class State extends StateRecord {
: child : child
}) })
return this.set('nodes', nodes) return this.merge({ nodes })
} }
/** /**
@@ -152,11 +152,13 @@ class State extends StateRecord {
collapseBackward() { collapseBackward() {
let { selection } = this let { selection } = this
let { anchorKey, anchorOffset } = selection let { anchorKey, anchorOffset } = selection
selection = selection.merge({ selection = selection.merge({
focusKey: anchorKey, focusKey: anchorKey,
focusOffset: anchorOffset focusOffset: anchorOffset
}) })
let state = this.set('selection', selection)
let state = this.merge({ selection })
return state return state
} }
@@ -169,11 +171,13 @@ class State extends StateRecord {
collapseForward() { collapseForward() {
let { selection } = this let { selection } = this
let { focusKey, focusOffset } = selection let { focusKey, focusOffset } = selection
selection = selection.merge({ selection = selection.merge({
anchorKey: focusKey, anchorKey: focusKey,
anchorOffset: focusOffset anchorOffset: focusOffset
}) })
let state = this.set('selection', selection)
let state = this.merge({ selection })
return state return state
} }
@@ -246,7 +250,7 @@ class State extends StateRecord {
.take(Infinity) .take(Infinity)
// ...and remove the text from the first and last nodes // ...and remove the text from the first and last nodes
let state = this.set('nodes', nodes) let state = this.merge({ nodes })
state = state.removeText(startNode, startOffset, startNode.text.length) state = state.removeText(startNode, startOffset, startNode.text.length)
state = state.removeText(endNode, 0, endOffset) state = state.removeText(endNode, 0, endOffset)
return state return state
@@ -269,7 +273,7 @@ class State extends StateRecord {
return startOffset <= i && i < endOffset return startOffset <= i && i < endOffset
}) })
node = node.set('characters', characters) node = node.merge({ characters })
let state = this.setNode(node.key, node) let state = this.setNode(node.key, node)
return state return state
} }

View File

@@ -12,12 +12,12 @@ const CORE_PLUGIN = {
* The core `onKeyDown` handler. * The core `onKeyDown` handler.
* *
* @param {Event} e * @param {Event} e
* @param {State} state
* @param {Editor} editor * @param {Editor} editor
* @return {State or Null} newState * @return {State or Null} newState
*/ */
onKeyDown(e, editor) { onKeyDown(e, state, editor) {
const state = editor.getState()
const key = keycode(e.which) const key = keycode(e.which)
switch (key) { switch (key) {