Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(transcript): copy the transcript as plain text
Elian Doran32e66b17514e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex f1126e066c..d265ac35c2 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4620,7 +4620,8 @@"offset": "{{offset}} into the transcription","cleanup": "Hide filler words","cleanup_tooltip": "Hides “uh”, “um” and stuttered words. The transcript itself is not changed.",- "no_audio": "No audio"+ "no_audio": "No audio",+ "copy": "Copy transcript"},"resource_download": {"title": "Download required",diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.cssindex 7c6720238e..ee06e9fea6 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css@@ -29,7 +29,9 @@.transcript-options {display: flex;+ align-items: center;justify-content: flex-end;+ gap: 0.5rem;max-width: var(--preferred-max-content-width, 900px);margin-bottom: 0.5rem;color: var(--muted-text-color);diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsxindex 47b4fce68f..71d7696dbf 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx@@ -15,7 +15,8 @@ import TranscriptView, {formatOffset, groupIntoTurns, levelToMeter, TranscriptRecordingLevels, type TranscriptTurn} from "./TranscriptView";-const h = vi.hoisted(() => ({ post: vi.fn(), put: vi.fn() }));+const h = vi.hoisted(() => ({ post: vi.fn(), put: vi.fn(), copyTextWithToast: vi.fn() }));+vi.mock("../../../services/clipboard_ext", () => ({ copyTextWithToast: h.copyTextWithToast }));vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));// keyboard_actions asks the server for its shortcuts at import.vi.mock("../../../services/server", () => ({@@ -626,4 +627,35 @@ describe("TranscriptView", () => {expect(h.put).toHaveBeenCalledWith("options", { transcriptCleanupEnabled: "false" });expect(spans()).toEqual([ "Uh the the data is old.", " Uh.", " I know, I know." ]);});++ it("copies the transcript as shown, one named and timed turn after another", async () => {+ options.set("transcriptCleanupEnabled", "true");+ const content = transcript([+ { source: "microphone", offsetMs: 5000, durationMs: 1000, text: "Uh hello." },+ { source: "microphone", offsetMs: 6000, durationMs: 1000, text: "Can you hear me?" },+ { source: "microphone", offsetMs: 30_000, text: "Um." },+ { source: "microphone", offsetMs: 40_000, text: "Still there?" },+ { source: "system", offsetMs: 70_000, speaker: 1, text: "Yes, I can." }+ ], STARTED_AT, { "system:1": "Olivier" });+ const attachment = { attachmentId: "transcript1", getBlob: async () => ({ content }) };+ const container = renderInto(+ <ParentComponent.Provider value={new Component()}>+ <TranscriptView attachment={attachment as unknown as FAttachment} />+ </ParentComponent.Provider>+ );+ const copy = () => container.querySelector<HTMLButtonElement>(+ ".transcript-options .transcript-copy");+ await vi.waitFor(() => expect(copy()).not.toBeNull());++ act(() => copy()?.click());+ expect(h.copyTextWithToast).toHaveBeenCalledExactlyOnceWith([+ "transcript.speaker_me (14:30)",+ "Hello. Can you hear me?",+ "",+ "Still there?",+ "",+ "Olivier (14:31)",+ "Yes, I can."+ ].join("\n"));+ });});diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsxindex d5bbfbd141..62173bc2ab 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx@@ -8,11 +8,13 @@ import clsx from "clsx";import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";import type FAttachment from "../../../entities/fattachment";+import { copyTextWithToast } from "../../../services/clipboard_ext";import { t } from "../../../services/i18n";import server from "../../../services/server";import { isIMEComposing } from "../../../services/shortcuts";import utils from "../../../services/utils";import { useTranscriptionState } from "../../launch_bar/RecordMeetingButton";+import ActionButton from "../../react/ActionButton";import FormTextBox from "../../react/FormTextBox";import FormToggle from "../../react/FormToggle";import { useStaticTooltip, useTriliumEvent, useTriliumOptionBool } from "../../react/hooks";@@ -127,6 +129,14 @@ export default function TranscriptView({ attachment }: { attachment: FAttachment>{transcript.utterances.length > 0 && (<div className="transcript-options">+ <ActionButton+ className="transcript-copy"+ icon="bx bx-copy"+ text={t("transcript.copy")}+ onClick={() => copyTextWithToast(formatTranscriptText(+ turns, transcript.startedAt, names,+ (utterance) => shownTexts.get(utterance) ?? utterance.text))}+ /><FormToggleswitchOnName={t("transcript.cleanup")}switchOffName={t("transcript.cleanup")}@@ -340,8 +350,7 @@ async function renameSpeaker(attachment: FAttachment, key: string, name: string)function TurnTime({ startedAt, offsetMs }: { startedAt: string; offsetMs: number }) {const timeRef = useRef<HTMLTimeElement>(null);const offset = formatOffset(offsetMs);- const startedAtMs = Date.parse(startedAt);- const time = Number.isNaN(startedAtMs) ? null : new Date(startedAtMs + offsetMs);+ const time = getTurnTime(startedAt, offsetMs);const tooltip = time ? t("transcript.offset", { offset }) : "";const tooltipConfig = useMemo(() => ({ title: tooltip }), [ tooltip ]);useStaticTooltip(timeRef, tooltipConfig);@@ -421,6 +430,31 @@ export function levelToMeter(level: number) {return Math.min(1, Math.max(0, fraction));}+/**+ * The transcript as plain text: each turn a line with its speaker's name and clock time, then its+ * paragraphs, each as `textOf` gives its utterances, with a blank line between paragraphs and+ * turns.+ */+export function formatTranscriptText(+ turns: TranscriptTurn[], startedAt: string, names: SpeakerNames,+ textOf: (utterance: TranscriptUtterance) => string+) {+ return turns.map((turn) => {+ const time = getTurnTime(startedAt, turn.offsetMs);+ const clock = time ? utils.formatTime(time) : formatOffset(turn.offsetMs);+ const heading = `${speakerName(turn.source, turn.speaker, names)} (${clock})`;+ const paragraphs = turn.paragraphs+ .map((paragraph) => paragraph.map(textOf).join(" "));+ return `${heading}\n${paragraphs.join("\n\n")}`;+ }).join("\n\n");+}++/** The clock time a turn began at, or `null` when the transcript's start is unreadable. */+function getTurnTime(startedAt: string, offsetMs: number) {+ const startedAtMs = Date.parse(startedAt);+ return Number.isNaN(startedAtMs) ? null : new Date(startedAtMs + offsetMs);+}+export function formatOffset(milliseconds: number) {const totalSeconds = Math.max(0, Math.floor(milliseconds / 1000));const hours = Math.floor(totalSeconds / 3600);diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Meeting transcription.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Meeting transcription.htmlindex cb5b21e379..24474596b0 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Meeting transcription.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Meeting transcription.html@@ -76,6 +76,10 @@<li>To show the transcript with or without hesitations such as "uh", "um"and stuttered words, use the <em>Hide filler words</em> switch above it.This affects only what is shown; the stored transcript keeps every word.</li>+ <li>To copy the transcript as plain text, press the <span class="tn-icon bx bx-copy"></span> <em>Copy transcript</em> button+ above it. Each turn is copied with the speaker's name and the time it started,+ as shown, so the copy leaves out filler words while <em>Hide filler words</em> is+ on.</li></ul><p>If the meeting note is <a href="#root/_help_bwg0e8ewQMak">protected</a> andthe protected session is locked during the meeting, the transcript cannotdiff --git a/docs/User Guide/User Guide/Meeting transcription.md b/docs/User Guide/User Guide/Meeting transcription.mdindex 191b90a752..646d572391 100644--- a/docs/User Guide/User Guide/Meeting transcription.md+++ b/docs/User Guide/User Guide/Meeting transcription.md@@ -37,6 +37,7 @@ The transcript is stored as an [attachment](Basic%20Concepts%20and%20Features/No* What the other participants say is labeled _Speaker 1_, _Speaker 2_ and so on, while _Identify speakers_ is on; otherwise it is labeled _Others_.* To name a speaker, click their name, type the new name and press <kbd>Enter</kbd>. The name changes on every turn of that speaker. Press <kbd>Esc</kbd> to cancel, or clear the name to restore the default one.* To show the transcript with or without hesitations such as "uh", "um" and stuttered words, use the _Hide filler words_ switch above it. This affects only what is shown; the stored transcript keeps every word.+* To copy the transcript as plain text, press the <span class="tn-icon bx bx-copy"></span> _Copy transcript_ button above it. Each turn is copied with the speaker's name and the time it started, as shown, so the copy leaves out filler words while _Hide filler words_ is on.If the meeting note is [protected](Basic%20Concepts%20and%20Features/Notes/Protected%20Notes.md) and the protected session is locked during the meeting, the transcript cannot be saved. Trilium keeps it in memory and saves it once you enter the protected session again, as long as Trilium stays open.