Skip to content

Commit

Permalink
✨ add option for timestamps in plaintext export
Browse files Browse the repository at this point in the history
  • Loading branch information
anuejn committed Dec 4, 2024
1 parent 0b989c4 commit 0e36b68
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 11 additions & 1 deletion frontend/src/editor/export/plaintext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<form className="flex flex-col gap-4 mt-4">
Expand All @@ -17,6 +18,11 @@ export function PlaintextExportBody({ onClose, outputNameBase, editor }: ExportP
value={includeSpeakerNames}
onChange={(x) => setIncludeSpeakerNames(x)}
/>
<Checkbox
label="Include Timestamps"
value={includeTimestamps}
onChange={(x) => setIncludeTimestamps(x)}
/>
<div className="flex justify-between pt-4">
<SecondaryButton type="button" onClick={onClose}>
Cancel
Expand All @@ -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();
}}
Expand Down
30 changes: 25 additions & 5 deletions frontend/src/utils/export/plaintext.ts
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 0e36b68

Please sign in to comment.