Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add plaintext export #386

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
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
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');
}