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

fallback findPath query to find closest element with a data-key (#2794)

* fallback findPath query to find closest element with a data-key

* lint

* move example to findEventRange query

* lint
This commit is contained in:
Cary Dunn
2019-05-20 14:41:40 -07:00
committed by Ian Storm Taylor
parent 4eff9b5a06
commit 3c013c567a
3 changed files with 21 additions and 10 deletions

View File

@@ -271,7 +271,7 @@ function BeforePlugin() {
// default, and calling `preventDefault` hides the cursor.
const node = editor.findNode(event.target)
if (editor.isVoid(node)) {
if (!node || editor.isVoid(node)) {
event.preventDefault()
}

View File

@@ -181,13 +181,13 @@ function QueriesPlugin() {
: y - rect.top < rect.top + rect.height - y
const range = document.createRange()
const iterable = isPrevious ? 'previousTexts' : 'nextTexts'
const move = isPrevious ? 'moveToEndOfNode' : 'moveToStartOfNode'
const entry = document[iterable](path)
const entry = document[isPrevious ? 'getPreviousText' : 'getNextText'](
path
)
if (entry) {
const [n] = entry
return range[move](n)
return range[move](entry)
}
return null
@@ -234,13 +234,24 @@ function QueriesPlugin() {
function findPath(editor, element) {
const content = editor.tmp.contentRef.current
let nodeElement = element
if (element === content.ref.current) {
// If element does not have a key, it is likely a string or
// mark, return the closest parent Node that can be looked up.
if (!nodeElement.hasAttribute(DATA_ATTRS.KEY)) {
nodeElement = nodeElement.closest(SELECTORS.KEY)
}
if (!nodeElement || !nodeElement.getAttribute(DATA_ATTRS.KEY)) {
return null
}
if (nodeElement === content.ref.current) {
return PathUtils.create([])
}
const search = (instance, p) => {
if (element === instance) {
if (nodeElement === instance) {
return p
}
@@ -248,7 +259,7 @@ function QueriesPlugin() {
return null
}
if (element === instance.ref.current) {
if (nodeElement === instance.ref.current) {
return p
}