mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-17 20:51:20 +02:00
remove some polyfills (#1288)
* remove some polyfills * use for-of for iteration when possible
This commit is contained in:
@@ -8,7 +8,6 @@ import logger from 'slate-dev-logger'
|
||||
|
||||
import EVENT_HANDLERS from '../constants/event-handlers'
|
||||
import Node from './node'
|
||||
import findClosestNode from '../utils/find-closest-node'
|
||||
import findDOMRange from '../utils/find-dom-range'
|
||||
import findRange from '../utils/find-range'
|
||||
import scrollToSelection from '../utils/scroll-to-selection'
|
||||
@@ -210,7 +209,7 @@ class Content extends React.Component {
|
||||
const el = target.nodeType === 3 ? target.parentNode : target
|
||||
return (
|
||||
(el.isContentEditable) &&
|
||||
(el === element || findClosestNode(el, '[data-slate-editor]') === element)
|
||||
(el === element || el.closest('[data-slate-editor]') === element)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -269,7 +268,7 @@ class Content extends React.Component {
|
||||
handler == 'onDragStart'
|
||||
) {
|
||||
const { target } = event
|
||||
const targetEditorNode = findClosestNode(target, '[data-slate-editor]')
|
||||
const targetEditorNode = target.closest('[data-slate-editor]')
|
||||
if (targetEditorNode !== this.element) return
|
||||
}
|
||||
|
||||
|
@@ -140,8 +140,7 @@ class Editor extends React.Component {
|
||||
let isNew = false
|
||||
|
||||
// Check to see if any plugin-related properœties have changed.
|
||||
for (let i = 0; i < PLUGINS_PROPS.length; i++) {
|
||||
const prop = PLUGINS_PROPS[i]
|
||||
for (const prop of PLUGINS_PROPS) {
|
||||
if (props[prop] == this.props[prop]) continue
|
||||
isNew = true
|
||||
break
|
||||
@@ -377,9 +376,8 @@ function resolvePlugins(props) {
|
||||
* Mix in the property types for the event handlers.
|
||||
*/
|
||||
|
||||
for (let i = 0; i < EVENT_HANDLERS.length; i++) {
|
||||
const property = EVENT_HANDLERS[i]
|
||||
Editor.propTypes[property] = Types.func
|
||||
for (const prop of EVENT_HANDLERS) {
|
||||
Editor.propTypes[prop] = Types.func
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -59,16 +59,14 @@ let OS
|
||||
if (browser) {
|
||||
const { userAgent } = window.navigator
|
||||
|
||||
for (let i = 0; i < BROWSER_RULES.length; i++) {
|
||||
const [ name, regexp ] = BROWSER_RULES[i]
|
||||
for (const [ name, regexp ] of BROWSER_RULES) {
|
||||
if (regexp.test(userAgent)) {
|
||||
BROWSER = name
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < OS_RULES.length; i++) {
|
||||
const [ name, regexp ] = OS_RULES[i]
|
||||
for (const [ name, regexp ] of OS_RULES) {
|
||||
if (regexp.test(userAgent)) {
|
||||
OS = name
|
||||
break
|
||||
@@ -78,8 +76,7 @@ if (browser) {
|
||||
const testEl = window.document.createElement('div')
|
||||
testEl.contentEditable = true
|
||||
|
||||
for (let i = 0; i < EVENT_RULES.length; i++) {
|
||||
const [ name, testFn ] = EVENT_RULES[i]
|
||||
for (const [ name, testFn ] of EVENT_RULES) {
|
||||
EVENTS[name] = testFn(testEl)
|
||||
}
|
||||
}
|
||||
|
@@ -1,37 +0,0 @@
|
||||
|
||||
/**
|
||||
* Find the closest ancestor of a DOM `element` that matches a given selector.
|
||||
*
|
||||
* COMPAT: In IE11, the `Node.closest` method doesn't exist natively, so we
|
||||
* have to polyfill it. (2017/09/06)
|
||||
*
|
||||
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
|
||||
*
|
||||
* @param {Element} node
|
||||
* @param {String} selector
|
||||
* @return {Element}
|
||||
*/
|
||||
|
||||
function findClosestNode(node, selector) {
|
||||
if (typeof node.closest === 'function') return node.closest(selector)
|
||||
|
||||
const matches = (node.document || node.ownerDocument).querySelectorAll(selector)
|
||||
let parentNode = node
|
||||
let i
|
||||
|
||||
do {
|
||||
i = matches.length
|
||||
while (--i >= 0 && matches.item(i) !== parentNode);
|
||||
}
|
||||
while ((i < 0) && (parentNode = parentNode.parentElement))
|
||||
|
||||
return parentNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Export.
|
||||
*
|
||||
* @type {Function}
|
||||
*/
|
||||
|
||||
export default findClosestNode
|
@@ -1,6 +1,4 @@
|
||||
|
||||
import findClosestNode from './find-closest-node'
|
||||
|
||||
/**
|
||||
* Find a Slate node from a DOM `element`.
|
||||
*
|
||||
@@ -9,7 +7,7 @@ import findClosestNode from './find-closest-node'
|
||||
*/
|
||||
|
||||
function findNode(element, state) {
|
||||
const closest = findClosestNode(element, '[data-key]')
|
||||
const closest = element.closest('[data-key]')
|
||||
if (!closest) return null
|
||||
|
||||
const key = closest.getAttribute('data-key')
|
||||
|
@@ -2,7 +2,6 @@
|
||||
import getWindow from 'get-window'
|
||||
|
||||
import OffsetKey from './offset-key'
|
||||
import findClosestNode from './find-closest-node'
|
||||
|
||||
/**
|
||||
* Constants.
|
||||
@@ -32,7 +31,7 @@ function findPoint(nativeNode, nativeOffset, state) {
|
||||
|
||||
const window = getWindow(nativeNode)
|
||||
const { parentNode } = nearestNode
|
||||
let rangeNode = findClosestNode(parentNode, RANGE_SELECTOR)
|
||||
let rangeNode = parentNode.closest(RANGE_SELECTOR)
|
||||
let offset
|
||||
let node
|
||||
|
||||
@@ -40,7 +39,7 @@ function findPoint(nativeNode, nativeOffset, state) {
|
||||
// determine what the offset relative to the text node is.
|
||||
if (rangeNode) {
|
||||
const range = window.document.createRange()
|
||||
const textNode = findClosestNode(rangeNode, TEXT_SELECTOR)
|
||||
const textNode = rangeNode.closest(TEXT_SELECTOR)
|
||||
range.setStart(textNode, 0)
|
||||
range.setEnd(nearestNode, nearestOffset)
|
||||
node = textNode
|
||||
@@ -50,7 +49,7 @@ function findPoint(nativeNode, nativeOffset, state) {
|
||||
// For void nodes, the element with the offset key will be a cousin, not an
|
||||
// ancestor, so find it by going down from the nearest void parent.
|
||||
else {
|
||||
const voidNode = findClosestNode(parentNode, VOID_SELECTOR)
|
||||
const voidNode = parentNode.closest(VOID_SELECTOR)
|
||||
if (!voidNode) return null
|
||||
rangeNode = voidNode.querySelector(RANGE_SELECTOR)
|
||||
node = rangeNode
|
||||
|
Reference in New Issue
Block a user