Jake Vanderwerf
2025-11-10 e9967fa22781d922ba4eb8fb44fe72d200ac4b14
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class Media {
    constructor() {
        this.currentWidth = window.innerWidth;
        this.images = document.querySelectorAll('.wp-site-blocks img[data-small]');
 
        if (this.images.length === 0) return;
 
        // Immediately load visible images
        this.loadVisibleImages();
 
        this.initListeners();
    }
 
    loadVisibleImages() {
        // Load first image immediately, plus any in viewport
        this.images.forEach((img, index) => {
            const rect = img.getBoundingClientRect();
            const isVisible = rect.top < window.innerHeight && rect.bottom > 0;
 
            // Always load first image, or if currently visible
            if (index === 0 || isVisible) {
                this.loadAppropriateImage(img);
                img.dataset.loaded = 'true'; // Mark so we don't observe it
            }
        });
    }
 
    initListeners() {
        this.resizeHandler = this.handleResize.bind(this);
        window.addEventListener('resize', this.resizeHandler);
 
        // Only observe images that weren't immediately loaded
        this.observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    this.loadAppropriateImage(entry.target);
                    this.observer.unobserve(entry.target);
                }
            });
        }, {
            rootMargin: '50px',
            threshold: 0.1
        });
 
        this.images.forEach(img => {
            if (!img.dataset.loaded) {
                this.observer.observe(img);
            }
        });
    }
 
    handleResize() {
        window.debouncer.schedule('image-resize', () => {
            const newWidth = window.innerWidth;
            if (Math.abs(newWidth - this.currentWidth) > 100) {
                this.currentWidth = newWidth;
                this.updateVisibleImages();
            }
        }, 150);
    }
 
    updateVisibleImages() {
        this.images.forEach(img => {
            const rect = img.getBoundingClientRect();
            if (rect.top < window.innerHeight && rect.bottom > 0) {
                this.loadAppropriateImage(img, true);
            }
        });
    }
 
    loadAppropriateImage(img, forceUpdate = false) {
        const targetSize = this.getTargetSize();
        const newSrc = img.dataset[targetSize];
 
        if (newSrc && (forceUpdate || newSrc !== img.currentSrc)) {
            img.src = newSrc;
        }
    }
 
    getTargetSize() {
        if (this.currentWidth < 768) return 'small';
        if (this.currentWidth < 1200) return 'medium';
        return 'full';
    }
 
    cleanup() {
        this.observer?.disconnect();
        window.removeEventListener('resize', this.resizeHandler);
    }
}
 
window.isLoaded = false;
document.addEventListener('readystatechange', () => {
    if (!window.isLoaded && document.querySelector('.wp-site-blocks img')) {
        window.jvbMedia = new Media();
        window.isLoaded = true;
    }
});