Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Convert speaker navigation to index-based for virtual scroll

Murtaza Nasir04c372c10ea7
diff --git a/static/js/app.modular.js b/static/js/app.modular.js
index 8c5e9d61..256b707f 100644
--- a/static/js/app.modular.js
+++ b/static/js/app.modular.js
@@ -991,11 +991,9 @@ document.addEventListener('DOMContentLoaded', async () => {
setGlobalError
});
- // Speakers composable needs processedTranscription, so initialize it after
- const speakersComposable = useSpeakers(state, utils, processedTranscription);
-
// =========================================================================
// VIRTUAL SCROLL SETUP (for performance with long transcriptions)
+ // Must be before speakers composable since it uses scrollToSegmentIndex
// =========================================================================
// Create a computed ref for the segments array
const transcriptSegments = computed(() => processedTranscription.value.simpleSegments || []);
@@ -1025,6 +1023,12 @@ document.addEventListener('DOMContentLoaded', async () => {
}
};
+ // Add scrollToSegmentIndex to utils for composables that need it
+ utils.scrollToSegmentIndex = scrollToSegmentIndex;
+
+ // Speakers composable needs processedTranscription and scrollToSegmentIndex
+ const speakersComposable = useSpeakers(state, utils, processedTranscription);
+
const groupedRecordings = computed(() => {
const groups = {};
const groupDates = {}; // Track the most recent date in each group
diff --git a/static/js/modules/composables/speakers.js b/static/js/modules/composables/speakers.js
index 6b713cfd..a5a63010 100644
--- a/static/js/modules/composables/speakers.js
+++ b/static/js/modules/composables/speakers.js
@@ -523,72 +523,42 @@ export function useSpeakers(state, utils, processedTranscription) {
};
// =========================================
- // Speaker Navigation
+ // Speaker Navigation (Index-Based for Virtual Scroll)
// =========================================
+ /**
+ * Find speaker groups by analyzing segment data (not DOM).
+ * Returns groups with startIndex instead of startElement for virtual scroll compatibility.
+ */
const findSpeakerGroups = (speakerId) => {
if (!speakerId) return [];
+ // Get segments from processedTranscription
+ const segments = processedTranscription.value?.simpleSegments || [];
+ if (segments.length === 0) return [];
+
const groups = [];
- const modalTranscript = document.querySelector('div.speaker-modal-transcript');
- const mainTranscript = document.querySelector('.transcription-simple-view, .transcription-with-speakers, .transcription-content');
- const transcriptContainer = modalTranscript || mainTranscript;
-
- if (!transcriptContainer) return [];
-
- // For JSON-based transcripts with segments
- const allSegments = transcriptContainer.querySelectorAll('.speaker-segment');
- if (allSegments.length > 0) {
- let currentGroup = null;
- let lastSpeakerId = null;
-
- allSegments.forEach(segment => {
- const speakerTag = segment.querySelector('[data-speaker-id]');
- const segmentSpeakerId = speakerTag?.dataset.speakerId;
-
- if (segmentSpeakerId === speakerId) {
- // If this is a new group (not consecutive with previous)
- if (lastSpeakerId !== speakerId) {
- currentGroup = {
- startElement: segment,
- elements: [segment]
- };
- groups.push(currentGroup);
- } else if (currentGroup) {
- // Add to existing group
- currentGroup.elements.push(segment);
- }
- }
- lastSpeakerId = segmentSpeakerId;
- });
- } else {
- // For plain text transcripts with speaker tags
- const allTags = transcriptContainer.querySelectorAll('[data-speaker-id]');
- let currentGroup = null;
-
- allTags.forEach(tag => {
- if (tag.dataset.speakerId === speakerId) {
- // Find the parent element that contains this speaker's content
- const parentSegment = tag.closest('.speaker-segment') || tag.parentElement;
-
- if (!currentGroup || !currentGroup.lastElement ||
- !parentSegment.previousElementSibling ||
- parentSegment.previousElementSibling !== currentGroup.lastElement) {
- // Start a new group
- currentGroup = {
- startElement: parentSegment,
- elements: [parentSegment],
- lastElement: parentSegment
- };
- groups.push(currentGroup);
- } else {
- // Continue the group
- currentGroup.elements.push(parentSegment);
- currentGroup.lastElement = parentSegment;
- }
+ let currentGroup = null;
+ let lastSpeakerId = null;
+
+ segments.forEach((segment, index) => {
+ const segmentSpeakerId = segment.speakerId;
+
+ if (segmentSpeakerId === speakerId) {
+ // If this is a new group (not consecutive with previous)
+ if (lastSpeakerId !== speakerId) {
+ currentGroup = {
+ startIndex: index,
+ indices: [index]
+ };
+ groups.push(currentGroup);
+ } else if (currentGroup) {
+ // Add to existing group
+ currentGroup.indices.push(index);
}
- });
- }
+ }
+ lastSpeakerId = segmentSpeakerId;
+ });
return groups;
};
@@ -597,18 +567,19 @@ export function useSpeakers(state, utils, processedTranscription) {
highlightedSpeaker.value = speakerId;
if (speakerId) {
- // Find all speaker groups for navigation
+ // Find all speaker groups for navigation (index-based, no DOM queries)
speakerGroups.value = findSpeakerGroups(speakerId);
currentSpeakerGroupIndex.value = 0;
- // Scroll to the first group
+ // Scroll to the first group using virtual scroll
if (speakerGroups.value.length > 0) {
- nextTick(() => {
- const firstGroup = speakerGroups.value[0];
- if (firstGroup && firstGroup.startElement) {
- firstGroup.startElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
+ const firstGroup = speakerGroups.value[0];
+ if (firstGroup && typeof firstGroup.startIndex === 'number') {
+ // Use the scrollToSegmentIndex function passed via utils
+ if (utils.scrollToSegmentIndex) {
+ utils.scrollToSegmentIndex(firstGroup.startIndex);
}
- });
+ }
}
} else {
speakerGroups.value = [];
@@ -618,7 +589,7 @@ export function useSpeakers(state, utils, processedTranscription) {
/**
* Select a speaker for navigation from the dropdown.
- * This ensures proper timing with Vue's reactivity and DOM updates.
+ * Uses index-based navigation compatible with virtual scrolling.
*/
const selectSpeakerForNavigation = (speakerId) => {
if (!speakerId) {
@@ -630,45 +601,46 @@ export function useSpeakers(state, utils, processedTranscription) {
highlightedSpeaker.value = speakerId;
- // Wait for Vue to render, then find groups
- // Use double nextTick to ensure transcript panel content is fully rendered
- nextTick(() => {
- nextTick(() => {
- speakerGroups.value = findSpeakerGroups(speakerId);
- currentSpeakerGroupIndex.value = 0;
-
- // Scroll to first occurrence
- if (speakerGroups.value.length > 0) {
- const firstGroup = speakerGroups.value[0];
- if (firstGroup && firstGroup.startElement) {
- firstGroup.startElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
- }
+ // Find groups immediately (no DOM dependency)
+ speakerGroups.value = findSpeakerGroups(speakerId);
+ currentSpeakerGroupIndex.value = 0;
+
+ // Scroll to first occurrence
+ if (speakerGroups.value.length > 0) {
+ const firstGroup = speakerGroups.value[0];
+ if (firstGroup && typeof firstGroup.startIndex === 'number') {
+ if (utils.scrollToSegmentIndex) {
+ utils.scrollToSegmentIndex(firstGroup.startIndex);
}
- });
- });
+ }
+ }
};
const navigateToNextSpeakerGroup = () => {
if (speakerGroups.value.length === 0) return;
- // Don't reset the speaker groups, just update the index
+ // Update the index
currentSpeakerGroupIndex.value = (currentSpeakerGroupIndex.value + 1) % speakerGroups.value.length;
const group = speakerGroups.value[currentSpeakerGroupIndex.value];
- if (group && group.startElement) {
- group.startElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
+ if (group && typeof group.startIndex === 'number') {
+ if (utils.scrollToSegmentIndex) {
+ utils.scrollToSegmentIndex(group.startIndex);
+ }
}
};
const navigateToPrevSpeakerGroup = () => {
if (speakerGroups.value.length === 0) return;
- // Don't reset the speaker groups, just update the index
+ // Update the index
currentSpeakerGroupIndex.value = currentSpeakerGroupIndex.value <= 0
? speakerGroups.value.length - 1
: currentSpeakerGroupIndex.value - 1;
const group = speakerGroups.value[currentSpeakerGroupIndex.value];
- if (group && group.startElement) {
- group.startElement.scrollIntoView({ behavior: 'smooth', block: 'center' });
+ if (group && typeof group.startIndex === 'number') {
+ if (utils.scrollToSegmentIndex) {
+ utils.scrollToSegmentIndex(group.startIndex);
+ }
}
};