Skip to content

Commit

Permalink
chore: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Aug 24, 2024
1 parent 8a8ebec commit 073f184
Showing 1 changed file with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ export function selectRootNodeByDom(view: EditorView, coords: { x: number, y: nu
if (!root)
return null

const pos = view.posAtCoords({
left: coords.x,
top: coords.y,
})?.inside
if (pos == null || pos < 0)
return null

let $pos = view.state.doc.resolve(pos)
let node = view.state.doc.nodeAt(pos)
let element = view.nodeDOM(pos) as HTMLElement | null

const filter = (needLookup: boolean) => {
const checkDepth = $pos.depth >= 1 && $pos.index($pos.depth) === 0
const shouldLookUp = needLookup || checkDepth

if (!shouldLookUp)
return

const ancestorPos = $pos.before($pos.depth)
node = view.state.doc.nodeAt(ancestorPos)
element = view.nodeDOM(ancestorPos) as HTMLElement | null
$pos = view.state.doc.resolve(ancestorPos)

if (!filterNodes($pos, node!))
filter(true)
try {
const pos = view.posAtCoords({
left: coords.x,
top: coords.y,
})?.inside
if (pos == null || pos < 0)
return null

let $pos = view.state.doc.resolve(pos)
let node = view.state.doc.nodeAt(pos)
let element = view.nodeDOM(pos) as HTMLElement | null

const filter = (needLookup: boolean) => {
const checkDepth = $pos.depth >= 1 && $pos.index($pos.depth) === 0
const shouldLookUp = needLookup || checkDepth

if (!shouldLookUp)
return

const ancestorPos = $pos.before($pos.depth)
node = view.state.doc.nodeAt(ancestorPos)
element = view.nodeDOM(ancestorPos) as HTMLElement | null
$pos = view.state.doc.resolve(ancestorPos)

if (!filterNodes($pos, node!))
filter(true)
}

// If filterNodes returns false, we should look up the parent node.
const filterResult = filterNodes($pos, node!)
filter(!filterResult)

if (!element || !node)
return null

return { node, $pos, el: element }
}

// If filterNodes returns false, we should look up the parent node.
const filterResult = filterNodes($pos, node!)
filter(!filterResult)

if (!element || !node)
catch {
return null

return { node, $pos, el: element }
}
}

0 comments on commit 073f184

Please sign in to comment.