1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , useRef } from 'react' ;
22import { useStoreState } from 'easy-peasy' ;
3+ import { ipcRenderer } from 'electron' ;
4+
35import Slide from '../Slide/Slide' ;
46import QuickTools from '../Slide/QuickTools' ;
57import {
@@ -40,8 +42,27 @@ function ShabadDeck() {
4042 themeBg,
4143 currentWorkspace,
4244 } = useStoreState ( ( state ) => state . userSettings ) ;
45+
4346 const [ activeVerse , setActiveVerse ] = useState ( [ ] ) ;
4447 const [ nextVerse , setNextVerse ] = useState ( { } ) ;
48+ const verseRefKeys = useRef ( [ ] ) ;
49+
50+ const observerOptions = {
51+ root : null ,
52+ rootMargin : '0px' ,
53+ threshold : 0.8 ,
54+ } ;
55+
56+ const updateVerse = ( entries ) => {
57+ entries . forEach ( ( entry ) => {
58+ if ( entry . isIntersecting ) {
59+ const visibleVerse = entry . target . dataset . verseid ;
60+ ipcRenderer . send ( 'sync-scroll' , visibleVerse ) ;
61+ }
62+ } ) ;
63+ } ;
64+
65+ const observer = new IntersectionObserver ( updateVerse , observerOptions ) ;
4566
4667 const baniLengthCols = {
4768 short : 'existsSGPC' ,
@@ -50,6 +71,18 @@ function ShabadDeck() {
5071 extralong : 'existsBuddhaDal' ,
5172 } ;
5273
74+ const verseRefs = useRef ( { } ) ;
75+
76+ const updateVerseRef = ( verseId , ref ) => {
77+ if ( ref ) {
78+ verseRefs . current [ verseId ] = ref ;
79+ if ( ! verseRefKeys . current . includes ( verseId ) ) {
80+ verseRefKeys . current = [ ...verseRefKeys . current , verseId ] ;
81+ }
82+ observer . observe ( ref ) ;
83+ }
84+ } ;
85+
5386 const getCurrentThemeInstance = ( ) => themes . find ( ( theme ) => theme . key === currentTheme ) ;
5487
5588 const bakeThemeStyles = ( themeInstance , themeObj ) => {
@@ -174,6 +207,19 @@ function ShabadDeck() {
174207 }
175208 } , [ activeShabadId , activeVerseId , sundarGutkaBaniId , ceremonyId , akhandpatt , displayNextLine ] ) ;
176209
210+ useEffect ( ( ) => {
211+ if ( activeVerseId && akhandpatt ) {
212+ const verseDOM = verseRefs . current [ activeVerseId ] ;
213+
214+ if ( verseDOM ) {
215+ verseDOM . scrollIntoView ( {
216+ behavior : 'smooth' ,
217+ block : 'center' ,
218+ } ) ;
219+ }
220+ }
221+ } , [ activeVerseId , akhandpatt , verseRefKeys . current ] ) ;
222+
177223 useEffect ( ( ) => {
178224 if ( isMiscSlide ) {
179225 if ( activeVerse . length !== 0 ) {
@@ -207,6 +253,7 @@ function ShabadDeck() {
207253 nextLineObj = { nextVerse }
208254 isMiscSlide = { isMiscSlide }
209255 bgColor = { applyOverlay ( ) }
256+ updateVerseRef = { updateVerseRef }
210257 />
211258 ) )
212259 ) : (
0 commit comments