1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 19:22:35 +02:00

Fix #397: resolve to leaf text in selection methods

This commit is contained in:
Samy Pesse
2016-10-25 18:50:03 +02:00
parent 0e32043315
commit 5477754f63
2 changed files with 36 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
import memoize from '../utils/memoize' import memoize from '../utils/memoize'
import getLeafText from '../utils/get-leaf-text'
import warning from '../utils/warning'
import { Record } from 'immutable' import { Record } from 'immutable'
/** /**
@@ -312,6 +314,7 @@ class Selection extends new Record(DEFAULTS) {
// If the anchor node isn't a text node, match it to one. // If the anchor node isn't a text node, match it to one.
if (anchorNode.kind != 'text') { if (anchorNode.kind != 'text') {
warning('Selection anchor is on a non text node, matching to leaf')
let anchorText = anchorNode.getTextAtOffset(anchorOffset) let anchorText = anchorNode.getTextAtOffset(anchorOffset)
let offset = anchorNode.getOffset(anchorText) let offset = anchorNode.getOffset(anchorText)
anchorOffset = anchorOffset - offset anchorOffset = anchorOffset - offset
@@ -320,6 +323,7 @@ class Selection extends new Record(DEFAULTS) {
// If the focus node isn't a text node, match it to one. // If the focus node isn't a text node, match it to one.
if (focusNode.kind != 'text') { if (focusNode.kind != 'text') {
warning('Selection focus is on a non text node, matching to leaf')
let focusText = focusNode.getTextAtOffset(focusOffset) let focusText = focusNode.getTextAtOffset(focusOffset)
let offset = focusNode.getOffset(focusText) let offset = focusNode.getOffset(focusText)
focusOffset = focusOffset - offset focusOffset = focusOffset - offset
@@ -433,10 +437,13 @@ class Selection extends new Record(DEFAULTS) {
/** /**
* Move to the start of a `node`. * Move to the start of a `node`.
* *
* @param {Node} node
* @return {Selection} selection * @return {Selection} selection
*/ */
collapseToStartOf(node) { collapseToStartOf(node) {
node = getLeafText(node)
return this.merge({ return this.merge({
anchorKey: node.key, anchorKey: node.key,
anchorOffset: 0, anchorOffset: 0,
@@ -453,6 +460,8 @@ class Selection extends new Record(DEFAULTS) {
*/ */
collapseToEndOf(node) { collapseToEndOf(node) {
node = getLeafText(node)
return this.merge({ return this.merge({
anchorKey: node.key, anchorKey: node.key,
anchorOffset: node.length, anchorOffset: node.length,
@@ -472,6 +481,9 @@ class Selection extends new Record(DEFAULTS) {
*/ */
moveToRangeOf(start, end = start) { moveToRangeOf(start, end = start) {
start = getLeafText(start)
end = getLeafText(end)
return this.merge({ return this.merge({
anchorKey: start.key, anchorKey: start.key,
anchorOffset: 0, anchorOffset: 0,

View File

@@ -0,0 +1,24 @@
/**
* Get leaf text for a node
*
* @param {Node} node
* @return {Text} text
*/
function getLeafText(node) {
if (node.kind == 'text') {
return node
}
const texts = node.getTexts()
return texts.first()
}
/**
* Export.
*
* @type {Function}
*/
export default getLeafText