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

fix getPoint to work around in-editable content

This commit is contained in:
Ian Storm Taylor
2017-03-29 13:01:16 -04:00
parent 3c49ca8ed1
commit 392fd75722
3 changed files with 42 additions and 14 deletions

View File

@@ -96,8 +96,13 @@ class CheckLists extends React.Component {
} }
/** /**
* On key down, if enter is pressed inside of a check list item, make sure * On key down...
* that when it is split the new item starts unchecked. *
* If enter is pressed inside of a check list item, make sure that when it
* is split the new item starts unchecked.
*
* If backspace is pressed when collapsed at the start of a check list item,
* then turn it back into a paragraph.
* *
* @param {Event} e * @param {Event} e
* @param {Object} data * @param {Object} data
@@ -106,8 +111,10 @@ class CheckLists extends React.Component {
*/ */
onKeyDown = (e, data, state) => { onKeyDown = (e, data, state) => {
if (data.key != 'enter') return if (
if (state.startBlock.type != 'check-list-item') return data.key == 'enter' &&
state.startBlock.type == 'check-list-item'
) {
return state return state
.transform() .transform()
.splitBlock() .splitBlock()
@@ -115,6 +122,19 @@ class CheckLists extends React.Component {
.apply() .apply()
} }
if (
data.key == 'backspace' &&
state.isCollapsed &&
state.startBlock.type == 'check-list-item' &&
state.selection.startOffset == 0
) {
return state
.transform()
.setBlock('paragraph')
.apply()
}
}
/** /**
* Render. * Render.
* *

View File

@@ -1,3 +1,4 @@
import OffsetKey from './offset-key' import OffsetKey from './offset-key'
/** /**

View File

@@ -15,13 +15,13 @@ function normalizeNodeAndOffset(node, offset) {
const isLast = offset == node.childNodes.length const isLast = offset == node.childNodes.length
const direction = isLast ? 'backward' : 'forward' const direction = isLast ? 'backward' : 'forward'
const index = isLast ? offset - 1 : offset const index = isLast ? offset - 1 : offset
node = getNonComment(node, index, direction) node = getEditableChild(node, index, direction)
// If the node has children, traverse until we have a leaf node. Leaf nodes // If the node has children, traverse until we have a leaf node. Leaf nodes
// can be either text nodes, or other void DOM nodes. // can be either text nodes, or other void DOM nodes.
while (node.nodeType == 1 && node.childNodes.length) { while (node.nodeType == 1 && node.childNodes.length) {
const i = isLast ? node.childNodes.length - 1 : 0 const i = isLast ? node.childNodes.length - 1 : 0
node = getNonComment(node, i, direction) node = getEditableChild(node, i, direction)
} }
// Determine the new offset inside the text node. // Determine the new offset inside the text node.
@@ -33,7 +33,8 @@ function normalizeNodeAndOffset(node, offset) {
} }
/** /**
* Get the nearest non-comment to `index` in a `parent`, preferring `direction`. * Get the nearest editable child at `index` in a `parent`, preferring
* `direction`.
* *
* @param {Element} parent * @param {Element} parent
* @param {Number} index * @param {Number} index
@@ -41,14 +42,20 @@ function normalizeNodeAndOffset(node, offset) {
* @return {Element|Null} * @return {Element|Null}
*/ */
function getNonComment(parent, index, direction) { function getEditableChild(parent, index, direction) {
const { childNodes } = parent const { childNodes } = parent
let child = childNodes[index] let child = childNodes[index]
let i = index let i = index
let triedForward = false let triedForward = false
let triedBackward = false let triedBackward = false
while (child.nodeType == 8) { // While the child is a comment node, or an element node with no children,
// keep iterating to find a sibling non-void, non-comment node.
while (
(child.nodeType == 8) ||
(child.nodeType == 1 && child.childNodes.length == 0) ||
(child.nodeType == 1 && child.getAttribute('contenteditable') == 'false')
) {
if (triedForward && triedBackward) break if (triedForward && triedBackward) break
if (i >= childNodes.length) { if (i >= childNodes.length) {