Skip to content

Commit bb16aad

Browse files
committed
fix(problems): 修复灵动布局下编辑器元素的查找
1 parent 9ce24a3 commit bb16aad

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

src/content/pages/problems/Timer.tsx

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
SuccessCheckReturnType,
88
findElement,
99
findElementByXPath,
10-
findAllElement,
1110
} from '@/utils'
12-
import { useEvent, useHover, useObserverAncestor } from '@/hooks'
11+
import { useEvent, useHover } from '@/hooks'
1312
import { ToolTip } from '@/components/ToolTip'
1413

1514
import {
@@ -75,6 +74,14 @@ const Button = styled.button<{
7574
`}
7675
`
7776

77+
function isEditor(node: unknown): node is HTMLElement {
78+
return (
79+
node instanceof HTMLElement &&
80+
node.classList.contains('monaco-editor') &&
81+
node.parentElement?.dataset.modeId !== 'plaintext'
82+
)
83+
}
84+
7885
interface TimerProps {
7986
beta?: boolean
8087
root?: HTMLElement
@@ -372,29 +379,6 @@ const Timer: FC<TimerProps> = ({ beta, root, dynamicLayout }) => {
372379
handleClick()
373380
}
374381

375-
const getEditEl = async () => {
376-
let editEl: HTMLElement
377-
378-
if (beta) {
379-
const editEls = await findAllElement(
380-
'.monaco-editor',
381-
els =>
382-
!!els.find(el => el.parentElement?.dataset.modeId !== 'plaintext')
383-
)
384-
editEl = editEls.find(
385-
el => el.parentElement?.dataset.modeId !== 'plaintext'
386-
)!
387-
} else {
388-
editEl = await findElement('.euyvu2f0')
389-
}
390-
return editEl
391-
}
392-
useObserverAncestor(async state => {
393-
const editEl: HTMLElement = await getEditEl()
394-
if (!state.isMount) return
395-
setEditEl(editEl)
396-
return editEl
397-
})
398382
useEffect(() => {
399383
if (!editEl) return
400384
console.log(editEl)
@@ -403,6 +387,30 @@ const Timer: FC<TimerProps> = ({ beta, root, dynamicLayout }) => {
403387
}, [editEl])
404388
//#endregion
405389

390+
useEffect(() => {
391+
const node = Array.from(document.querySelectorAll('.monaco-editor')).find(
392+
isEditor
393+
)
394+
if (node) {
395+
setEditEl(node)
396+
}
397+
const observe = new MutationObserver(mutations => {
398+
for (const mutation of mutations) {
399+
if (mutation.type === 'childList') {
400+
for (const node of mutation.addedNodes) {
401+
if (isEditor(node)) {
402+
setEditEl(node)
403+
}
404+
}
405+
}
406+
}
407+
})
408+
observe.observe(document.body, { childList: true, subtree: true })
409+
return () => {
410+
observe.disconnect()
411+
}
412+
}, [])
413+
406414
if (!root) return null
407415

408416
return (

0 commit comments

Comments
 (0)