|
| 1 | +import * as Automerge from '@automerge/automerge'; |
| 2 | + |
| 3 | +import { downloadTextAsFile } from '../../utils/download_text_as_file'; |
| 4 | +import { ExportProps } from '.'; |
| 5 | +import { PrimaryButton, SecondaryButton } from '../../components/button'; |
| 6 | +import { Document } from '../../editor/types'; |
| 7 | +import { formattedTime } from '../transcription_editor'; |
| 8 | + |
| 9 | +export function generateTypst(doc: Document): string { |
| 10 | + let last_speaker: string | null = null; |
| 11 | + const header = ` |
| 12 | +#let p(time: none, speaker: none, speaker_change: false, body) = { |
| 13 | + box(context { |
| 14 | + if speaker_change [ |
| 15 | + #v(1em) |
| 16 | + *#speaker:* \ |
| 17 | + ] |
| 18 | + let sizeTime = measure(time) |
| 19 | + place(dx: -sizeTime.width - 1em, text(fill: gray, time)) |
| 20 | + body |
| 21 | + }) |
| 22 | +}\n\n`; |
| 23 | + |
| 24 | + return ( |
| 25 | + header + |
| 26 | + doc.children |
| 27 | + .map((paragraph) => { |
| 28 | + const timeStr = `time: "${formattedTime(paragraph.children[0].start)}"`; |
| 29 | + const speakerStr = `speaker: "${ |
| 30 | + paragraph.speaker && doc.speaker_names[paragraph.speaker] |
| 31 | + }"`; |
| 32 | + const speakerChangeStr = `speaker_change: ${last_speaker !== paragraph.speaker}`; |
| 33 | + let paragraphText = `#p(${timeStr}, ${speakerStr}, ${speakerChangeStr})[\n`; |
| 34 | + |
| 35 | + function escape(text: string): string { |
| 36 | + return text |
| 37 | + .replace(/\]/g, '\\]') |
| 38 | + .replace(/\[/g, '\\[') |
| 39 | + .replace(/\*/g, '\\*') |
| 40 | + .replace(/_/g, '\\_'); |
| 41 | + } |
| 42 | + |
| 43 | + paragraphText += paragraph.children |
| 44 | + .map((x) => escape(x.text)) |
| 45 | + .join('') |
| 46 | + .trim(); |
| 47 | + |
| 48 | + paragraphText += '\n]\n'; |
| 49 | + if (last_speaker !== paragraph.speaker) { |
| 50 | + paragraphText += '\n'; |
| 51 | + } |
| 52 | + |
| 53 | + last_speaker = paragraph.speaker; |
| 54 | + return paragraphText; |
| 55 | + }) |
| 56 | + .filter((x) => x !== '') |
| 57 | + .join('\n') |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export function TypstExportBody({ onClose, outputNameBase, editor }: ExportProps) { |
| 62 | + return ( |
| 63 | + <form className="flex flex-col gap-4 mt-4"> |
| 64 | + <div className="flex justify-between pt-4"> |
| 65 | + <SecondaryButton type="button" onClick={onClose}> |
| 66 | + Cancel |
| 67 | + </SecondaryButton> |
| 68 | + <PrimaryButton |
| 69 | + type="submit" |
| 70 | + onClick={async (e) => { |
| 71 | + e.preventDefault(); |
| 72 | + const plaintext = generateTypst(Automerge.toJS(editor.doc)); |
| 73 | + downloadTextAsFile(`${outputNameBase}.typ`, `text/plain`, plaintext); |
| 74 | + onClose(); |
| 75 | + }} |
| 76 | + > |
| 77 | + Export |
| 78 | + </PrimaryButton> |
| 79 | + </div> |
| 80 | + </form> |
| 81 | + ); |
| 82 | +} |
0 commit comments