-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.ts
More file actions
163 lines (142 loc) · 4.71 KB
/
shared.ts
File metadata and controls
163 lines (142 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const SCROLL_BOTTOM_TOLERANCE_PX = 64;
let timelineScrollFollow = false;
let timelineScrollFollowListenerInstalled = false;
function syncTimelineScrollFollowFromScrollPosition(): void {
timelineScrollFollow = isDocumentPinnedToBottom(document.scrollingElement);
}
/**
* Keeps `timelineScrollFollow` in sync with whether the viewport is pinned to
* the document bottom. Auto-scroll runs only while this is true, except when
* {@link setTimelineScrollFollow} is used to opt in at run start.
*/
export function ensureTimelineScrollFollowListener(): void {
if (typeof window === "undefined") return;
if (timelineScrollFollowListenerInstalled) return;
timelineScrollFollowListenerInstalled = true;
const onScroll = () => {
syncTimelineScrollFollowFromScrollPosition();
};
window.addEventListener("scroll", onScroll, { passive: true });
syncTimelineScrollFollowFromScrollPosition();
}
/** Opt in/out of timeline auto-scroll (e.g. true when a live/demo run starts). */
export function setTimelineScrollFollow(follow: boolean): void {
timelineScrollFollow = follow;
}
/** Scrolls the main document so the latest timeline content is in view. */
export function scrollDocumentToBottom(): void {
const root = document.scrollingElement;
if (!root) return;
root.scrollTop = root.scrollHeight;
}
/** Like {@link scrollDocumentToBottom} but only when the user is following the timeline. */
export function scrollTimelineToBottomIfFollowing(): void {
if (!timelineScrollFollow) return;
scrollDocumentToBottom();
}
export function escapeHtml(str: string): string {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
export function isDocumentPinnedToBottom(root: Element | null): boolean {
if (!root) return true;
const gap = root.scrollHeight - root.scrollTop - root.clientHeight;
return gap <= SCROLL_BOTTOM_TOLERANCE_PX;
}
export interface StreamRenderer {
push(chunk: string): void;
cancel(): void;
finish(): string;
}
export function createStreamRenderer(contentEl: HTMLElement): StreamRenderer {
const textSpan = document.createElement("span");
textSpan.className = "stream-text";
const cursorSpan = document.createElement("span");
cursorSpan.className = "cursor";
contentEl.replaceChildren(textSpan, cursorSpan);
let fullText = "";
let raf = 0;
let scrollRaf = 0;
const flushText = () => {
raf = 0;
textSpan.textContent = fullText;
};
const flushScroll = () => {
scrollRaf = 0;
if (timelineScrollFollow) {
scrollDocumentToBottom();
}
};
return {
push(chunk: string) {
if (!chunk) return;
fullText += chunk;
if (raf === 0) raf = requestAnimationFrame(flushText);
if (scrollRaf === 0) scrollRaf = requestAnimationFrame(flushScroll);
},
cancel() {
if (raf !== 0) cancelAnimationFrame(raf);
raf = 0;
if (scrollRaf !== 0) cancelAnimationFrame(scrollRaf);
scrollRaf = 0;
},
finish() {
if (raf !== 0) cancelAnimationFrame(raf);
raf = 0;
if (scrollRaf !== 0) cancelAnimationFrame(scrollRaf);
scrollRaf = 0;
textSpan.textContent = fullText;
contentEl.replaceChildren(textSpan);
scrollTimelineToBottomIfFollowing();
return fullText;
},
};
}
export interface RoundDisplay {
id: string;
speaker: string;
cls: string;
label: string;
}
export interface AppendRoundCardResult {
card: HTMLDivElement;
contentEl: HTMLElement;
typingEl: HTMLElement;
}
export function appendRoundCard(
roundDisplay: RoundDisplay,
): AppendRoundCardResult {
const timeline = document.getElementById("timeline");
if (!timeline) {
throw new Error("Missing element #timeline");
}
const card = document.createElement("div");
card.className = `round-card ${roundDisplay.cls} visible`;
card.id = roundDisplay.id;
card.innerHTML = `
<div class="card-inner">
<div class="card-header">
<span class="speaker-label">${escapeHtml(roundDisplay.speaker)}</span>
<span class="round-badge">${escapeHtml(roundDisplay.label)}</span>
<div class="typing-indicator" id="typing-${roundDisplay.id}">
<span></span><span></span><span></span>
</div>
</div>
<div class="card-content" id="content-${roundDisplay.id}"></div>
</div>
`;
timeline.appendChild(card);
requestAnimationFrame(() => {
card.classList.add("animated");
scrollTimelineToBottomIfFollowing();
});
const contentEl = document.getElementById(`content-${roundDisplay.id}`);
const typingEl = document.getElementById(`typing-${roundDisplay.id}`);
if (!contentEl || !typingEl) {
throw new Error("appendRoundCard: content or typing node missing");
}
return { card, contentEl, typingEl };
}