1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-01 13:00:16 +02:00

fix a few void block transform and rendering bugs

This commit is contained in:
Ian Storm Taylor
2016-07-15 12:49:29 -07:00
parent a48235401c
commit d995b1ab35
5 changed files with 40 additions and 7 deletions

View File

@@ -324,7 +324,7 @@ class Content extends React.Component {
focusKey: focus.key,
focusOffset: focus.offset
})
.apply({ isNative: true })
.apply()
this.onChange(state)
}

View File

@@ -763,6 +763,17 @@ const Node = {
})
},
/**
* Check if a node has a void parent by `key`.
*
* @param {String or Node} key
* @return {Boolean}
*/
hasVoidParent(key) {
return !!this.getClosest(key, n => n.isVoid)
},
/**
* Insert child `nodes` after child by `key`.
*
@@ -772,7 +783,6 @@ const Node = {
*/
insertChildrenAfter(key, nodes) {
key = normalizeKey(key)
const child = this.getChild(key)
const index = this.nodes.indexOf(child)

View File

@@ -560,9 +560,17 @@ class State extends new Record(DEFAULTS) {
}
else if (selection.isAtStartOf(startNode)) {
const parent = document.getParent(startNode)
const previous = document.getPreviousSibling(parent).nodes.first()
after = selection.collapseToEndOf(previous)
const previous = document.getPreviousText(startNode)
const prevBlock = document.getClosestBlock(previous)
const prevInline = document.getClosestInline(previous)
if (prevBlock && prevBlock.isVoid) {
after = selection
} else if (prevInline && prevInline.isVoid) {
after = selection
} else {
after = selection.collapseToEndOf(previous)
}
}
else {

View File

@@ -117,6 +117,14 @@ const Transforms = {
if (range.isAtStartOf(startNode)) {
const previous = node.getPreviousText(startNode)
// If the previous descendant is void, remove it.
const prevBlock = node.getClosestBlock(previous)
if (prevBlock && prevBlock.isVoid) return node.removeDescendant(prevBlock)
const prevInline = node.getClosestInline(previous)
if (prevInline && prevInline.isVoid) return node.removeDescendant(prevInline)
range = range.extendToEndOf(previous)
range = range.normalize(node)
return node.deleteAtRange(range)

View File

@@ -147,11 +147,18 @@ function Plugin(options = {}) {
onKeyDown(e, state, editor) {
const key = keycode(e.which)
const transform = state.transform()
let transform = state.transform()
switch (key) {
case 'enter': {
return transform.splitBlock().apply()
const { startBlock } = state
if (startBlock && !startBlock.isVoid) return transform.splitBlock().apply()
const { document, startKey } = state
const text = document.getNextText(startKey)
if (!text) return
return transform.collapseToStartOf(text).apply()
}
case 'backspace': {