@@ -75,28 +75,26 @@ async function readAheadHandleKeydown(event: KeyboardEvent): Promise<void> {
7575let tunnelVisionObserver : MutationObserver | undefined ;
7676let tunnelVisionFrame : number | undefined ;
7777
78- function updateTunnelVisionCaretPosition ( ) : void {
79- tunnelVisionFrame = undefined ;
80-
81- const caret = document . getElementById ( "caret" ) ;
82- const words = document . getElementById ( "words" ) ;
83- if ( caret === null || words === null ) return ;
84-
85- const caretRect = caret . getBoundingClientRect ( ) ;
86- const wordsRect = words . getBoundingClientRect ( ) ;
87- words . style . setProperty (
88- "--caret-center-x" ,
89- `${ caretRect . left + caretRect . width / 2 - wordsRect . left } px` ,
90- ) ;
91- words . style . setProperty (
92- "--caret-center-y" ,
93- `${ caretRect . top + caretRect . height / 2 - wordsRect . top } px` ,
94- ) ;
95- }
96-
97- function scheduleTunnelVisionCaretPositionUpdate ( ) : void {
78+ function requestCaretPositionUpdate ( ) : void {
9879 if ( tunnelVisionFrame !== undefined ) return ;
99- tunnelVisionFrame = requestAnimationFrame ( updateTunnelVisionCaretPosition ) ;
80+ tunnelVisionFrame = requestAnimationFrame ( ( ) => {
81+ tunnelVisionFrame = undefined ;
82+
83+ const caret = document . getElementById ( "caret" ) ;
84+ const words = document . getElementById ( "words" ) ;
85+ if ( caret === null || words === null ) return ;
86+
87+ const caretRect = caret . getBoundingClientRect ( ) ;
88+ const wordsRect = words . getBoundingClientRect ( ) ;
89+ words . style . setProperty (
90+ "--caret-center-x" ,
91+ `${ caretRect . left + caretRect . width / 2 - wordsRect . left } px` ,
92+ ) ;
93+ words . style . setProperty (
94+ "--caret-center-y" ,
95+ `${ caretRect . top + caretRect . height / 2 - wordsRect . top } px` ,
96+ ) ;
97+ } ) ;
10098}
10199
102100//todo move to its own file
@@ -536,25 +534,17 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
536534 const caret = document . getElementById ( "caret" ) ;
537535 if ( caret === null ) return ;
538536
539- tunnelVisionObserver = new MutationObserver (
540- scheduleTunnelVisionCaretPositionUpdate ,
541- ) ;
537+ tunnelVisionObserver = new MutationObserver ( requestCaretPositionUpdate ) ;
542538 tunnelVisionObserver . observe ( caret , {
543539 attributes : true ,
544540 attributeFilter : [ "class" , "style" ] ,
545541 } ) ;
546- window . addEventListener (
547- "resize" ,
548- scheduleTunnelVisionCaretPositionUpdate ,
549- ) ;
542+ window . addEventListener ( "resize" , requestCaretPositionUpdate ) ;
550543 } ,
551544 clearGlobal ( ) : void {
552545 tunnelVisionObserver ?. disconnect ( ) ;
553546 tunnelVisionObserver = undefined ;
554- window . removeEventListener (
555- "resize" ,
556- scheduleTunnelVisionCaretPositionUpdate ,
557- ) ;
547+ window . removeEventListener ( "resize" , requestCaretPositionUpdate ) ;
558548
559549 if ( tunnelVisionFrame !== undefined ) {
560550 cancelAnimationFrame ( tunnelVisionFrame ) ;
0 commit comments