Skip to content

Commit

Permalink
✨ Add plaintext export
Browse files Browse the repository at this point in the history
Closes #385
  • Loading branch information
pajowu committed Nov 18, 2023
1 parent bf86e69 commit 436512c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions frontend/src/editor/export/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Modal } from '../../components/modal';
import { WebVttExportBody } from './webvtt';
import { TranscribeeExportBody } from './transcribee';
import { ApiDocument } from '../../api/document';
import { PlaintextExportBody } from './plaintext';

export type ExportProps = {
outputNameBase: string;
Expand All @@ -29,6 +30,10 @@ const exportTypes: ExportType[] = [
name: 'Subtitles',
component: WebVttExportBody,
},
{
name: 'Plaintext',
component: PlaintextExportBody,
},
{
name: 'Transcribee Archive',
component: TranscribeeExportBody,
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/editor/export/plaintext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState } from 'react';
import * as Automerge from '@automerge/automerge';

import { Checkbox } from '../../components/form';
import { downloadTextAsFile } from '../../utils/download_text_as_file';
import { ExportProps } from '.';
import { PrimaryButton, SecondaryButton } from '../../components/button';
import { generatePlaintext } from '../../utils/export/plaintext';

export function PlaintextExportBody({ onClose, outputNameBase, editor }: ExportProps) {
const [includeSpeakerNames, setIncludeSpeakerNames] = useState(true);

return (
<form className="flex flex-col gap-4 mt-4">
<Checkbox
label="Include Speaker Names"
value={includeSpeakerNames}
onChange={(x) => setIncludeSpeakerNames(x)}
/>
<div className="flex justify-between pt-4">
<SecondaryButton type="button" onClick={onClose}>
Cancel
</SecondaryButton>
<PrimaryButton
type="submit"
onClick={async (e) => {
e.preventDefault();
const plaintext = generatePlaintext(Automerge.toJS(editor.doc), includeSpeakerNames);
downloadTextAsFile(`${outputNameBase}.txt`, `text/plain`, plaintext);
onClose();
}}
>
Export
</PrimaryButton>
</div>
</form>
);
}
18 changes: 18 additions & 0 deletions frontend/src/utils/export/plaintext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Document } from '../../editor/types';
import { getSpeakerName } from '../document';

export function generatePlaintext(doc: Document, includeSpeakerNames: boolean): string {
let last_speaker: string | null = null;
return doc.children
.map((paragraph) => {
let paragraphText = '';
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();
})
.filter((x) => x !== '')
.join('\n\n');
}

0 comments on commit 436512c

Please sign in to comment.