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

Allow document to have no nodes when deleting a single void-block. Partially fixes #505 (#622)

This commit is contained in:
Per-Kristian Nordnes
2017-02-24 00:06:20 +01:00
committed by Ian Storm Taylor
parent 86f4844d5d
commit 86cbb0d49a
14 changed files with 94 additions and 4 deletions

View File

@@ -211,7 +211,7 @@ export function deleteBackwardAtRange(transform, range, n = 1, options = {}) {
return
}
// If the closest is not void, but empty, remove it
if (block && !block.isVoid && block.isEmpty) {
if (block && !block.isVoid && block.isEmpty && document.nodes.size !== 1) {
transform.removeNodeByKey(block.key, { normalize })
return
}
@@ -395,7 +395,7 @@ export function deleteForwardAtRange(transform, range, n = 1, options = {}) {
return
}
// If the closest is not void, but empty, remove it
if (block && !block.isVoid && block.isEmpty) {
if (block && !block.isVoid && block.isEmpty && document.nodes.size !== 1) {
transform.removeNodeByKey(block.key, { normalize })
return
}

View File

@@ -60,16 +60,24 @@ export function normalizeNodeByKey(transform, key, schema) {
export function normalizeSelection(transform) {
let { state } = transform
let { document, selection } = state
// If document is empty, return
if (document.nodes.size === 0) {
return
}
selection = selection.normalize(document)
// If the selection is unset, or the anchor or focus key in the selection are
// pointing to nodes that no longer exist, warn and reset the selection.
// pointing to nodes that no longer exist, warn (if not unset) and reset the selection.
if (
selection.isUnset ||
!document.hasDescendant(selection.anchorKey) ||
!document.hasDescendant(selection.focusKey)
) {
warn('The selection was invalid and was reset to start of the document. The selection in question was:', selection)
if (!selection.isUnset) {
warn('The selection was invalid and was reset to start of the document. The selection in question was:', selection)
}
const firstText = document.getFirstText()
selection = selection.merge({

View File

@@ -0,0 +1,10 @@
export default function (state) {
const { document } = state
const nodeToBeFocused = document.nodes.first()
return state
.transform()
.collapseToEndOf(nodeToBeFocused)
.deleteBackward()
.apply()
}

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""

View File

@@ -0,0 +1,10 @@
export default function (state) {
const { document } = state
const nodeToBeFocused = document.nodes.first()
return state
.transform()
.collapseToEndOf(nodeToBeFocused)
.deleteBackward()
.apply()
}

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true

View File

@@ -0,0 +1,10 @@
export default function (state) {
const { document } = state
const nodeToBeFocused = document.nodes.first()
return state
.transform()
.collapseToStartOf(nodeToBeFocused)
.deleteForward()
.apply()
}

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""

View File

@@ -0,0 +1,7 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
text: ""

View File

@@ -0,0 +1,10 @@
export default function (state) {
const { document } = state
const nodeToBeFocused = document.nodes.first()
return state
.transform()
.collapseToStartOf(nodeToBeFocused)
.deleteForward()
.apply()
}

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true