1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 06:53:25 +02:00

fix to default window in findDOM* utils

This commit is contained in:
Ian Storm Taylor
2017-11-16 13:12:42 -08:00
parent 7cfc7fae68
commit ed32159be7
3 changed files with 13 additions and 11 deletions

View File

@@ -5,15 +5,16 @@ import { Node } from 'slate'
* Find the DOM node for a `key`.
*
* @param {String|Node} key
* @param {Window} win (optional)
* @return {Element}
*/
function findDOMNode(key, window) {
function findDOMNode(key, win = window) {
if (Node.isNode(key)) {
key = key.key
}
const el = window.document.querySelector(`[data-key="${key}"]`)
const el = win.document.querySelector(`[data-key="${key}"]`)
if (!el) {
throw new Error(`Unable to find a DOM node for "${key}". This is often because of forgetting to add \`props.attributes\` to a custom component.`)

View File

@@ -4,20 +4,20 @@ import findDOMNode from './find-dom-node'
/**
* Find a native DOM selection point from a Slate `key` and `offset`.
*
* @param {Element} root
* @param {String} key
* @param {Number} offset
* @return {Object}
* @param {Window} win (optional)
* @return {Object|Null}
*/
function findDOMPoint(key, offset, window) {
const el = findDOMNode(key, window)
function findDOMPoint(key, offset, win = window) {
const el = findDOMNode(key, win)
let start = 0
let n
// COMPAT: In IE, this method's arguments are not optional, so we have to
// pass in all four even though the last two are defaults. (2017/10/25)
const iterator = window.document.createNodeIterator(
const iterator = win.document.createNodeIterator(
el,
NodeFilter.SHOW_TEXT,
() => NodeFilter.FILTER_ACCEPT,

View File

@@ -5,16 +5,17 @@ import findDOMPoint from './find-dom-point'
* Find a native DOM range Slate `range`.
*
* @param {Range} range
* @param {Window} win (optional)
* @return {Object|Null}
*/
function findDOMRange(range, window) {
function findDOMRange(range, win = window) {
const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range
const anchor = findDOMPoint(anchorKey, anchorOffset, window)
const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, window)
const anchor = findDOMPoint(anchorKey, anchorOffset, win)
const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, win)
if (!anchor || !focus) return null
const r = window.document.createRange()
const r = win.document.createRange()
const start = isBackward ? focus : anchor
const end = isBackward ? anchor : focus
r.setStart(start.node, start.offset)