diff --git a/frontend/src/editor/export/plaintext.tsx b/frontend/src/editor/export/plaintext.tsx index 200e921f..bc1eeafa 100644 --- a/frontend/src/editor/export/plaintext.tsx +++ b/frontend/src/editor/export/plaintext.tsx @@ -9,6 +9,7 @@ import { generatePlaintext } from '../../utils/export/plaintext'; export function PlaintextExportBody({ onClose, outputNameBase, editor }: ExportProps) { const [includeSpeakerNames, setIncludeSpeakerNames] = useState(true); + const [includeTimestamps, setIncludeTimestamps] = useState(true); return (
@@ -17,6 +18,11 @@ export function PlaintextExportBody({ onClose, outputNameBase, editor }: ExportP value={includeSpeakerNames} onChange={(x) => setIncludeSpeakerNames(x)} /> + setIncludeTimestamps(x)} + />
Cancel @@ -25,7 +31,11 @@ export function PlaintextExportBody({ onClose, outputNameBase, editor }: ExportP type="submit" onClick={async (e) => { e.preventDefault(); - const plaintext = generatePlaintext(Automerge.toJS(editor.doc), includeSpeakerNames); + const plaintext = generatePlaintext( + Automerge.toJS(editor.doc), + includeSpeakerNames, + includeTimestamps, + ); downloadTextAsFile(`${outputNameBase}.txt`, `text/plain`, plaintext); onClose(); }} diff --git a/frontend/src/utils/export/plaintext.ts b/frontend/src/utils/export/plaintext.ts index 8f9f0a5b..9e0c8fec 100644 --- a/frontend/src/utils/export/plaintext.ts +++ b/frontend/src/utils/export/plaintext.ts @@ -1,18 +1,38 @@ +import { formattedTime } from '../../editor/transcription_editor'; import { Document } from '../../editor/types'; import { getSpeakerName } from '../document'; -export function generatePlaintext(doc: Document, includeSpeakerNames: boolean): string { +export function generatePlaintext( + doc: Document, + includeSpeakerNames: boolean, + includeTimestamps: boolean, +): string { let last_speaker: string | null = null; return doc.children .map((paragraph) => { let paragraphText = ''; + if ( + last_speaker !== null && + ((includeSpeakerNames && last_speaker !== paragraph.speaker) || + (includeTimestamps && !includeSpeakerNames)) + ) { + paragraphText += '\n'; + } + if (includeTimestamps && (last_speaker !== paragraph.speaker || !includeSpeakerNames)) { + paragraphText += `[${formattedTime(paragraph.children[0].start)}]\n`; + } if (includeSpeakerNames && last_speaker !== paragraph.speaker) { paragraphText += `${getSpeakerName(paragraph.speaker, doc.speaker_names)}:\n`; - last_speaker = paragraph.speaker; } - paragraphText += paragraph.children.map((x) => x.text).join(''); - return paragraphText.trim(); + + paragraphText += paragraph.children + .map((x) => x.text) + .join('') + .trim(); + + last_speaker = paragraph.speaker; + return paragraphText; }) .filter((x) => x !== '') - .join('\n\n'); + .join('\n'); }