Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class TranscriptUI {
this.viewMode = 'single'; // 'single' or 'dual'

// Segments: each has { original, translation, status, speaker, language, confidence }
this.segments = [];
this.segments = []; // Display segments (trimmed for viewport)
this.allSegments = []; // Full history (never trimmed, used for save/copy)
this.provisionalText = '';
this.provisionalSpeaker = null;
this.provisionalLanguage = null;
Expand Down Expand Up @@ -56,15 +57,17 @@ export class TranscriptUI {
*/
addOriginal(text, speaker, language) {
this._removeListening();
this.segments.push({
const seg = {
original: text,
translation: null,
status: 'original',
speaker: speaker || null,
language: language || null,
confidence: this.lastConfidence,
createdAt: Date.now(),
});
};
this.segments.push(seg);
this.allSegments.push(seg); // Same object ref — mutations shared
if (speaker) this.currentSpeaker = speaker;
if (language) this.currentLanguage = language;
this._cleanupStaleOriginals();
Expand All @@ -75,17 +78,28 @@ export class TranscriptUI {
* Apply translation to the oldest untranslated segment
*/
addTranslation(text) {
// Try display list first (shared ref updates allSegments too)
const seg = this.segments.find(s => s.status === 'original');
if (seg) {
seg.translation = text;
seg.status = 'translated';
} else {
this.segments.push({
original: '',
translation: text,
status: 'translated',
speaker: null,
});
// Fallback: check allSegments for originals trimmed from display
const allSeg = this.allSegments.find(s => s.status === 'original');
if (allSeg) {
allSeg.translation = text;
allSeg.status = 'translated';
} else {
// No matching original anywhere — create orphan segment
const newSeg = {
original: '',
translation: text,
status: 'translated',
speaker: null,
};
this.segments.push(newSeg);
this.allSegments.push(newSeg);
}
}
this._render();
}
Expand Down Expand Up @@ -136,6 +150,7 @@ export class TranscriptUI {
</div>
`;
this.segments = [];
this.allSegments = [];
this.provisionalText = '';
this.provisionalSpeaker = null;
this.provisionalLanguage = null;
Expand Down Expand Up @@ -198,7 +213,7 @@ export class TranscriptUI {
*/
getPlainText() {
let lines = [];
for (const seg of this.segments) {
for (const seg of this.allSegments) {
if (seg.original) lines.push(seg.original);
if (seg.translation) lines.push(seg.translation);
if (seg.original || seg.translation) lines.push('');
Expand All @@ -211,7 +226,7 @@ export class TranscriptUI {
* Get formatted content for saving to file (markdown with metadata)
*/
getFormattedContent(metadata = {}) {
if (this.segments.length === 0) return null;
if (this.allSegments.length === 0) return null;

const lines = [];

Expand All @@ -223,12 +238,12 @@ export class TranscriptUI {
if (metadata.targetLang) lines.push(`target_language: ${metadata.targetLang}`);
if (metadata.duration) lines.push(`recording_duration: ${metadata.duration}`);
if (metadata.audioSource) lines.push(`audio_source: ${metadata.audioSource}`);
lines.push(`segments: ${this.segments.length}`);
lines.push(`segments: ${this.allSegments.length}`);
lines.push('---');
lines.push('');

// Transcript entries
for (const seg of this.segments) {
// Transcript entries (full history)
for (const seg of this.allSegments) {
if (seg.speaker) lines.push(`**Speaker ${seg.speaker}:**`);
if (seg.original) lines.push(`> ${seg.original}`);
if (seg.translation) lines.push(seg.translation);
Expand All @@ -242,7 +257,7 @@ export class TranscriptUI {
* Check if there are segments to save
*/
hasSegments() {
return this.segments.length > 0;
return this.allSegments.length > 0;
}

/**
Expand All @@ -251,6 +266,7 @@ export class TranscriptUI {
clear() {
this.container.innerHTML = '';
this.segments = [];
this.allSegments = [];
this.provisionalText = '';
this.provisionalSpeaker = null;
this.provisionalLanguage = null;
Expand Down