mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 15:02:51 +02:00
fix to default window in findDOM* utils
This commit is contained in:
@@ -5,15 +5,16 @@ import { Node } from 'slate'
|
|||||||
* Find the DOM node for a `key`.
|
* Find the DOM node for a `key`.
|
||||||
*
|
*
|
||||||
* @param {String|Node} key
|
* @param {String|Node} key
|
||||||
|
* @param {Window} win (optional)
|
||||||
* @return {Element}
|
* @return {Element}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function findDOMNode(key, window) {
|
function findDOMNode(key, win = window) {
|
||||||
if (Node.isNode(key)) {
|
if (Node.isNode(key)) {
|
||||||
key = key.key
|
key = key.key
|
||||||
}
|
}
|
||||||
|
|
||||||
const el = window.document.querySelector(`[data-key="${key}"]`)
|
const el = win.document.querySelector(`[data-key="${key}"]`)
|
||||||
|
|
||||||
if (!el) {
|
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.`)
|
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.`)
|
||||||
|
@@ -4,20 +4,20 @@ import findDOMNode from './find-dom-node'
|
|||||||
/**
|
/**
|
||||||
* Find a native DOM selection point from a Slate `key` and `offset`.
|
* Find a native DOM selection point from a Slate `key` and `offset`.
|
||||||
*
|
*
|
||||||
* @param {Element} root
|
|
||||||
* @param {String} key
|
* @param {String} key
|
||||||
* @param {Number} offset
|
* @param {Number} offset
|
||||||
* @return {Object}
|
* @param {Window} win (optional)
|
||||||
|
* @return {Object|Null}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function findDOMPoint(key, offset, window) {
|
function findDOMPoint(key, offset, win = window) {
|
||||||
const el = findDOMNode(key, window)
|
const el = findDOMNode(key, win)
|
||||||
let start = 0
|
let start = 0
|
||||||
let n
|
let n
|
||||||
|
|
||||||
// COMPAT: In IE, this method's arguments are not optional, so we have to
|
// 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)
|
// 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,
|
el,
|
||||||
NodeFilter.SHOW_TEXT,
|
NodeFilter.SHOW_TEXT,
|
||||||
() => NodeFilter.FILTER_ACCEPT,
|
() => NodeFilter.FILTER_ACCEPT,
|
||||||
|
@@ -5,16 +5,17 @@ import findDOMPoint from './find-dom-point'
|
|||||||
* Find a native DOM range Slate `range`.
|
* Find a native DOM range Slate `range`.
|
||||||
*
|
*
|
||||||
* @param {Range} range
|
* @param {Range} range
|
||||||
|
* @param {Window} win (optional)
|
||||||
* @return {Object|Null}
|
* @return {Object|Null}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function findDOMRange(range, window) {
|
function findDOMRange(range, win = window) {
|
||||||
const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range
|
const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range
|
||||||
const anchor = findDOMPoint(anchorKey, anchorOffset, window)
|
const anchor = findDOMPoint(anchorKey, anchorOffset, win)
|
||||||
const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, window)
|
const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, win)
|
||||||
if (!anchor || !focus) return null
|
if (!anchor || !focus) return null
|
||||||
|
|
||||||
const r = window.document.createRange()
|
const r = win.document.createRange()
|
||||||
const start = isBackward ? focus : anchor
|
const start = isBackward ? focus : anchor
|
||||||
const end = isBackward ? anchor : focus
|
const end = isBackward ? anchor : focus
|
||||||
r.setStart(start.node, start.offset)
|
r.setStart(start.node, start.offset)
|
||||||
|
Reference in New Issue
Block a user