File size: 1,754 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { FoliateView } from '@/types/view';
import { throttle } from './throttle';
import { debounce } from './debounce';

export const handleA11yNavigation = (
  view: FoliateView | null,
  document: Document,
  index: number,
) => {
  if (!view) return;

  const state = {
    skipInitial: true,
    hasRecentRelocate: false,
    relocateTimer: null as ReturnType<typeof setTimeout> | null,
  };

  const markRelocateEnd = debounce(() => {
    state.hasRecentRelocate = false;
  }, 2000);

  const markRelocated = () => {
    state.hasRecentRelocate = true;
    markRelocateEnd();
  };

  const throttledMarkRelocated = throttle(markRelocated, 1000);
  view.renderer.addEventListener('scroll', throttledMarkRelocated, { passive: true });
  view.renderer.addEventListener('relocate', throttledMarkRelocated);

  const observer = new IntersectionObserver(
    (entries) => {
      if (state.skipInitial) {
        state.skipInitial = false;
        return;
      }
      if (state.hasRecentRelocate) return;
      for (const entry of entries) {
        if (entry.isIntersecting) {
          const range = document.createRange();
          range.selectNodeContents(entry.target);
          const cfi = view.getCFI(index, range);
          setTimeout(() => {
            if (state.hasRecentRelocate) return;
            const resolved = view.resolveNavigation(cfi);
            view.renderer.goTo?.(resolved);
            console.log('Navigating to new location from screen reader');
          }, 500);
          break;
        }
      }
    },
    { threshold: 0 },
  );

  document.querySelectorAll('a').forEach((el) => {
    el.setAttribute('tabindex', '-1');
  });

  document.querySelectorAll('p').forEach((el) => {
    observer.observe(el);
  });
};