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

add Node.createRange for range resolution (#2025)

#### Is this adding or improving a _feature_ or fixing a _bug_?

Improvement.

#### What's the new behavior?

Adds a `Node.createRange` method for more easily creating ranges that are normalized to the current document. As well as a `Node.resolveRange` lower-level method for just doing the normalization on an existing range.

Later we can deprecate `Range.normalize` since it should be on the node instead.

#### Have you checked that...?

<!-- 
Please run through this checklist for your pull request: 
-->

* [x] The new code matches the existing patterns and styles.
* [x] The tests pass with `yarn test`.
* [x] The linter passes with `yarn lint`. (Fix errors with `yarn prettier`.)
* [x] The relevant examples still work. (Run examples with `yarn watch`.)

#### Does this fix any issues or need any specific reviewers?

Fixes: #2011
This commit is contained in:
Ian Storm Taylor
2018-08-03 14:16:47 -07:00
committed by GitHub
parent 8a1d75025a
commit de3420b0f1
8 changed files with 69 additions and 46 deletions

View File

@@ -344,10 +344,11 @@ function AfterPlugin() {
// Determine what the selection should be after changing the text.
const delta = textContent.length - text.length
const corrected = selection.collapseToEnd().move(delta)
const entire = selection
let entire = selection
.moveAnchorTo(point.key, start)
.moveFocusTo(point.key, end)
.normalize(document)
entire = document.normalizeRange(entire)
// Change the current value to have the leaf's text replaced.
change.insertTextAtRange(entire, textContent, leaf.marks).select(corrected)
@@ -583,7 +584,7 @@ function AfterPlugin() {
if (next) range = range.moveFocusTo(next.key, 0)
}
range = range.normalize(document)
range = document.normalizeRange(range)
change.select(range)
}

View File

@@ -1,6 +1,5 @@
import getWindow from 'get-window'
import isBackward from 'selection-is-backward'
import { Range } from 'slate'
import { IS_IE, IS_EDGE } from 'slate-dev-environment'
import findPoint from './find-point'
@@ -60,7 +59,8 @@ function findRange(native, value) {
}
}
let range = Range.create({
const { document } = value
const range = document.createRange({
anchorKey: anchor.key,
anchorOffset: anchor.offset,
focusKey: focus.key,
@@ -69,7 +69,6 @@ function findRange(native, value) {
isFocused: true,
})
range = range.normalize(value.document)
return range
}

View File

@@ -1,6 +1,5 @@
import getWindow from 'get-window'
import { Range } from 'slate'
import findNode from './find-node'
import findRange from './find-range'
@@ -35,7 +34,7 @@ function getEventRange(event, value) {
: y - rect.top < rect.top + rect.height - y
const text = node.getFirstText()
const range = Range.create()
const range = document.createRange()
if (isPrevious) {
const previousText = document.getPreviousText(text.key)