1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-03 04:02:33 +02:00

Add support to iframe (#1396)

* pass window to util functions

* remove unused getWindow function

* fix findDOMNode in after plugin
This commit is contained in:
João Pereira
2017-11-13 19:42:09 +00:00
committed by Ian Storm Taylor
parent 5a6878bccf
commit 267800c26f
5 changed files with 11 additions and 16 deletions

View File

@@ -154,7 +154,7 @@ class Content extends React.Component {
// Otherwise, figure out which DOM nodes should be selected... // Otherwise, figure out which DOM nodes should be selected...
const current = !!rangeCount && native.getRangeAt(0) const current = !!rangeCount && native.getRangeAt(0)
const range = findDOMRange(selection) const range = findDOMRange(selection, window)
if (!range) { if (!range) {
logger.error('Unable to find a native DOM range from the current selection.', { selection }) logger.error('Unable to find a native DOM range from the current selection.', { selection })

View File

@@ -172,7 +172,7 @@ function AfterPlugin() {
if (isVoid) { if (isVoid) {
const r = range.cloneRange() const r = range.cloneRange()
const n = isVoidBlock ? endBlock : endInline const n = isVoidBlock ? endBlock : endInline
const node = findDOMNode(n) const node = findDOMNode(n, window)
r.setEndAfter(node) r.setEndAfter(node)
contents = r.cloneContents() contents = r.cloneContents()
attach = contents.childNodes[contents.childNodes.length - 1].firstChild attach = contents.childNodes[contents.childNodes.length - 1].firstChild
@@ -190,7 +190,7 @@ function AfterPlugin() {
.size !== 0 .size !== 0
if (hasMarks) { if (hasMarks) {
const r = range.cloneRange() const r = range.cloneRange()
const node = findDOMNode(startText) const node = findDOMNode(startText, window)
r.setStartBefore(node) r.setStartBefore(node)
contents = r.cloneContents() contents = r.cloneContents()
attach = contents.childNodes[contents.childNodes.length - 1].firstChild attach = contents.childNodes[contents.childNodes.length - 1].firstChild
@@ -329,6 +329,7 @@ function AfterPlugin() {
const { value } = change const { value } = change
const { document, selection } = value const { document, selection } = value
const window = getWindow(event.target)
let target = getEventRange(event, value) let target = getEventRange(event, value)
if (!target) return if (!target) return
@@ -396,7 +397,7 @@ function AfterPlugin() {
// Until this is fixed in React, we dispatch a mouseup event on that // Until this is fixed in React, we dispatch a mouseup event on that
// DOM node, since that will make it go back to normal. // DOM node, since that will make it go back to normal.
const focusNode = document.getNode(target.focusKey) const focusNode = document.getNode(target.focusKey)
const el = findDOMNode(focusNode) const el = findDOMNode(focusNode, window)
if (!el) return if (!el) return
el.dispatchEvent(new MouseEvent('mouseup', { el.dispatchEvent(new MouseEvent('mouseup', {

View File

@@ -8,7 +8,7 @@ import { Node } from 'slate'
* @return {Element} * @return {Element}
*/ */
function findDOMNode(key) { function findDOMNode(key, window) {
if (Node.isNode(key)) { if (Node.isNode(key)) {
key = key.key key = key.key
} }

View File

@@ -1,6 +1,4 @@
import getWindow from 'get-window'
import findDOMNode from './find-dom-node' import findDOMNode from './find-dom-node'
/** /**
@@ -12,9 +10,8 @@ import findDOMNode from './find-dom-node'
* @return {Object} * @return {Object}
*/ */
function findDOMPoint(key, offset) { function findDOMPoint(key, offset, window) {
const el = findDOMNode(key) const el = findDOMNode(key, window)
const window = getWindow(el)
let start = 0 let start = 0
let n let n

View File

@@ -1,6 +1,4 @@
import getWindow from 'get-window'
import findDOMPoint from './find-dom-point' import findDOMPoint from './find-dom-point'
/** /**
@@ -10,13 +8,12 @@ import findDOMPoint from './find-dom-point'
* @return {Object|Null} * @return {Object|Null}
*/ */
function findDOMRange(range) { function findDOMRange(range, window) {
const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range
const anchor = findDOMPoint(anchorKey, anchorOffset) const anchor = findDOMPoint(anchorKey, anchorOffset, window)
const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset) const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, window)
if (!anchor || !focus) return null if (!anchor || !focus) return null
const window = getWindow(anchor.node)
const r = window.document.createRange() const r = window.document.createRange()
const start = isBackward ? focus : anchor const start = isBackward ? focus : anchor
const end = isBackward ? anchor : focus const end = isBackward ? anchor : focus