File size: 4,855 Bytes
df8fe58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
function manageTimeBar(elemId, time) {
    if (!window.visTimelineInstances) {
        console.error(`Timeline instances collection not found`);
        return;
    }

    const timeline = window.visTimelineInstances[elemId];
    if (!timeline) {
        console.error(`Timeline instance ${elemId} not found`);
        return;
    }
    
    if (!window.customTimeBarIds) {
        window.customTimeBarIds = {};
    }
    
    try {
        timeline.setCustomTime(time, elemId);
    } catch (e) {
        timeline.addCustomTime(time, elemId);
    }
}

function setTimeBarDirect(elemId, time) {
    manageTimeBar(elemId, time);
}

function setTimeBarNormalized(elemId, start, end, normalizedPos) {
    const time = start + (end - start) * normalizedPos;
    manageTimeBar(elemId, time);
}

class VideoTimelineSync  {
    constructor(videoId, timelineId, trackLengthItemId) {
        this.timelineId = timelineId;

        try {
            const trackLengthItemData = getTimelineItemData(timelineId, trackLengthItemId);
            if (trackLengthItemData != null) {
                const trackLengthStart = trackLengthItemData.start;
                const trackLengthEnd = trackLengthItemData.end;
                this.trackLength = trackLengthEnd - trackLengthStart;
            }
        } catch (error) {
            console.error('Error setting timeline video sync:', error);
            return;
        }

        const container = document.getElementById(videoId);
        if (!container) {
            console.error('Video container not found');
            return;
        }

        this.progressElement = container.querySelector('progress');
        if (!this.progressElement) {
            console.error('Progress element not found');
            return;
        }
        
        this.setupProgressObserver();
    }
    
    setupProgressObserver() {
        // Create mutation observer to watch for value changes of the progress element
        this.observer = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.type === 'attributes' && mutation.attributeName === 'value') {
                    this.onProgressUpdate();
                }
            });
        });
        
        // Observe the progress element for value changes
        this.observer.observe(this.progressElement, {
            attributes: true,
            attributeFilter: ['value']
        });
    }
    
    onProgressUpdate() {
        const value = this.progressElement.value;
        if (value === undefined || value === null) return;
        
        // Value is already normalized (between 0 and 1)
        this.syncTimeBarToPlayback(value);
    }
    
    syncTimeBarToPlayback(normalizedPosition) {
        const timeline = window.visTimelineInstances[this.timelineId];
        if (timeline) {
            setTimeBarNormalized(this.timelineId, 0, this.trackLength, normalizedPosition);
        }
    }

    cleanup() {
        // Disconnect observer
        if (this.observer) {
            this.observer.disconnect();
            this.observer = null;
        }
    }
}

function initVideoSync(videoId, timelineId, trackLengthItemId) {
    try {
        // Initialize syncs container if it doesn't exist
        if (!window.timelineSyncs) {
            window.timelineSyncs = {};
        }

        // Cleanup existing sync if any
        if (window.timelineSyncs[timelineId]) {
            window.timelineSyncs[timelineId].cleanup();
        }
        
        // Create new sync instance
        window.timelineSyncs[timelineId] = new VideoTimelineSync(videoId, timelineId, trackLengthItemId);
    } catch (error) {
        console.error('Error initializing video sync:', error);
    }

    return null;
}

function getTimelineItemData(timelineId, itemId) {
    const timeline = window.visTimelineInstances[timelineId];
    if (!timeline) {
        console.error(`Timeline instance ${timelineId} not found`);
        return null;
    }

    const items = timeline.itemSet?.items;
    if (!items) {
        console.error('Timeline items not found');
        return null;
    }

    const item = items[itemId];
    if (!item) {
        return null;
    }

    const itemData = item.data;
    if (!itemData) {
        console.error('Track length item data not found');
        return null;
    }

    return item.data;
}

function setTimelineWindowToItemLength(timelineId, itemId) {
    const itemData = getTimelineItemData(timelineId, itemId);
    if (!itemData) {
        return;
    }

    try {
        const timeline = window.visTimelineInstances[timelineId];
        console.log(itemData.end);
        timeline.setWindow(itemData.start, new Date(itemData.end.getTime() + 20), {animation: false});
    } catch (error) {
        console.error('Error setting timeline window:', error);
    }
}