Skip to content

Build your personal knowledge base with Trilium Notes

feat(attachments): keep a live transcript scrolled to its newest turn

Elian Dorana71bf2e0ffee
diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.css
index c64f03b5a3..8edc0d76ae 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.css
+++ b/apps/client/src/widgets/type_widgets/Attachment.css
@@ -356,6 +356,10 @@ body[dir=rtl] .attachment-icon-marker {
overflow: hidden;
}
+.attachment-content-wrapper.attachment-transcript {
+ overflow: hidden;
+}
+
.attachment-content-wrapper.attachment-canvas-drawing {
overflow: hidden;
diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css
index 2634c1ce19..be36c42c7d 100644
--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css
+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css
@@ -1,8 +1,11 @@
.transcript {
+ box-sizing: border-box;
+ height: 100%;
+ overflow-y: auto;
padding: 0.5rem 1rem 1rem;
- max-width: var(--preferred-max-content-width, 900px);
.transcript-turn {
+ max-width: var(--preferred-max-content-width, 900px);
margin-bottom: 1rem;
}
diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx
index 5cf11270c6..3abb99c681 100644
--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx
@@ -78,4 +78,53 @@ describe("TranscriptView", () => {
"transcript.speaker_others0:03Yes."
]);
});
+
+ it("follows new turns for a reader at the end, not for one who scrolled up", async () => {
+ const utterance = (index: number): TranscriptUtterance =>
+ ({ source: "microphone", offsetMs: index * 60_000, text: `Line ${index}` });
+ // Each reload adds a turn, alternating sources so that none merge.
+ let count = 1;
+ const getBlob = vi.fn(async () => ({
+ content: transcript(Array.from({ length: count }, (_, index) => ({
+ ...utterance(index), source: index % 2 ? "system" : "microphone"
+ })))
+ }));
+ const attachment = { attachmentId: "transcript1", getBlob } as unknown as FAttachment;
+ const component = new Component();
+ const container = renderInto(
+ <ParentComponent.Provider value={component}>
+ <TranscriptView attachment={attachment} />
+ </ParentComponent.Provider>
+ );
+ await vi.waitFor(() => expect(container.querySelector(".transcript")).not.toBeNull());
+
+ // happy-dom lays nothing out: 100px of view over 50px a turn.
+ const scroller = container.querySelector<HTMLElement>(".transcript");
+ if (!scroller) {
+ throw new Error("no scroller");
+ }
+ Object.defineProperty(scroller, "clientHeight", { value: 100 });
+ Object.defineProperty(scroller, "scrollHeight", {
+ get: () => scroller.querySelectorAll(".transcript-turn").length * 50
+ });
+ const addTurn = async () => {
+ count++;
+ const loadResults = new LoadResults([]);
+ const row = { attachmentId: "transcript1", ownerId: "meeting" };
+ loadResults.addAttachmentRow(row as never);
+ await act(async () => {
+ await component.handleEvent("entitiesReloaded", { loadResults });
+ });
+ await vi.waitFor(() =>
+ expect(scroller.querySelectorAll(".transcript-turn")).toHaveLength(count));
+ };
+
+ await addTurn();
+ await addTurn();
+ expect(scroller.scrollTop).toBe(150);
+
+ scroller.scrollTop = 0;
+ await addTurn();
+ expect(scroller.scrollTop).toBe(0);
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx
index 547de4283e..13c4e9d4c8 100644
--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx
+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx
@@ -1,24 +1,31 @@
import "./TranscriptView.css";
import type { Transcript, TranscriptSource, TranscriptUtterance } from "@triliumnext/commons";
-import { useCallback, useEffect, useState } from "preact/hooks";
+import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
import type FAttachment from "../../../entities/fattachment";
import { t } from "../../../services/i18n";
import { useTriliumEvent } from "../../react/hooks";
import NoItems from "../../react/NoItems";
+/** How close to the end, in pixels, still counts as reading the newest turn. */
+const FOLLOW_THRESHOLD = 16;
+
/**
* Shows a `transcript` attachment as the conversation it records, one paragraph per speaker turn.
- * Follows the attachment as a running recording rewrites it.
+ * Follows the attachment as a running recording rewrites it, and keeps a reader who is at the end
+ * there as new turns arrive.
*/
export default function TranscriptView({ attachment }: { attachment: FAttachment }) {
const [ content, setContent ] = useState<string | null>();
+ const scrollerRef = useRef<HTMLDivElement>(null);
+ const followRef = useRef(false);
const load = useCallback(() => {
let current = true;
void attachment.getBlob().then((blob) => {
if (current) {
+ followRef.current = isScrolledToEnd(scrollerRef.current);
setContent(blob?.content ?? null);
}
});
@@ -27,6 +34,12 @@ export default function TranscriptView({ attachment }: { attachment: FAttachment
};
}, [ attachment ]);
useEffect(load, [ load ]);
+ useLayoutEffect(() => {
+ const scroller = scrollerRef.current;
+ if (followRef.current && scroller) {
+ scroller.scrollTop = scroller.scrollHeight;
+ }
+ }, [ content ]);
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
const { attachmentId } = attachment;
if (loadResults.getAttachmentRows().some((row) => row.attachmentId === attachmentId)) {
@@ -47,7 +60,7 @@ export default function TranscriptView({ attachment }: { attachment: FAttachment
}
return (
- <div className="transcript">
+ <div ref={scrollerRef} className="transcript">
{groupIntoTurns(transcript.utterances).map((turn) => (
<div key={turn.offsetMs} className={`transcript-turn transcript-${turn.source}`}>
<div className="transcript-turn-header">
@@ -108,6 +121,13 @@ export function formatOffset(milliseconds: number) {
: `${minutes}:${seconds}`;
}
+function isScrolledToEnd(scroller: HTMLElement | null) {
+ if (!scroller) {
+ return false;
+ }
+ return scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= FOLLOW_THRESHOLD;
+}
+
function speakerName(source: TranscriptSource) {
return source === "microphone" ? t("transcript.speaker_me") : t("transcript.speaker_others");
}