77 SuccessCheckReturnType ,
88 findElement ,
99 findElementByXPath ,
10- findAllElement ,
1110} from '@/utils'
12- import { useEvent , useHover , useObserverAncestor } from '@/hooks'
11+ import { useEvent , useHover } from '@/hooks'
1312import { ToolTip } from '@/components/ToolTip'
1413
1514import {
@@ -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+
7885interface 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