-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
117 lines (100 loc) · 3.66 KB
/
Copy pathindex.ts
File metadata and controls
117 lines (100 loc) · 3.66 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
import type { Parser } from "streaming-markdown";
import * as smd from "streaming-markdown";
class StreamingMarkdownElement extends HTMLElement {
private observer: MutationObserver;
private container: HTMLElement;
private shadowRootRef: ShadowRoot;
private parser: Parser | null = null;
constructor() {
super();
this.shadowRootRef = this.attachShadow({ mode: "open" });
const style = document.createElement("style");
style.textContent = `
:host { display: block; }
:host([hidden]) { display: none; }
.content { white-space: normal; }
.content p { margin: 0 0 0.75rem 0; }
`;
this.container = document.createElement("div");
this.container.className = "content";
this.container.setAttribute("part", "content");
this.shadowRootRef.append(style, this.container);
this.observer = new MutationObserver(this.onMutations);
}
connectedCallback(): void {
if (!this.hasAttribute("role")) this.setAttribute("role", "article");
this.setAttribute("aria-live", "polite");
// Init streaming-markdown parser bound to our shadow content container
this.parser = smd.parser(smd.default_renderer(this.container));
// Capture text nodes appended by htmx SSE/WebSocket handlers
this.observer.observe(this, { childList: true });
// Consume any initial child content present at connect time
this.consumeInitialChildren();
}
disconnectedCallback(): void {
this.observer.disconnect();
this.parser = null;
}
// Public minimal API for manual usage (e.g., WebSocket)
public appendChunk(text: string): void {
if (!text) return;
if (!this.parser) {
this.parser = smd.parser(smd.default_renderer(this.container));
}
smd.parser_write(this.parser, text);
this.autoScrollIfNearBottom();
}
public finish(): void {
if (this.parser) smd.parser_end(this.parser);
this.setAttribute("aria-live", "off");
}
public reset(): void {
this.container.innerHTML = "";
this.parser = smd.parser(smd.default_renderer(this.container));
this.setAttribute("aria-live", "polite");
}
private onMutations = (mutations: MutationRecord[]): void => {
for (const m of mutations) {
for (const node of Array.from(m.addedNodes)) {
if (node.nodeType === Node.TEXT_NODE) {
const text = (node as Text).data;
if (text) this.appendChunk(text);
node.parentNode?.removeChild(node);
} else if (node.nodeType === Node.ELEMENT_NODE) {
const text = (node as Element).textContent || "";
if (text) this.appendChunk(text);
node.parentNode?.removeChild(node);
}
}
}
};
private consumeInitialChildren(): void {
const snapshot = Array.from(this.childNodes);
for (const node of snapshot) {
if (node.nodeType === Node.TEXT_NODE) {
const text = (node as Text).data;
if (text) this.appendChunk(text);
node.parentNode?.removeChild(node);
} else if (node.nodeType === Node.ELEMENT_NODE) {
const text = (node as Element).textContent || "";
if (text) this.appendChunk(text);
node.parentNode?.removeChild(node);
}
}
}
private autoScrollIfNearBottom(): void {
// Only scroll if the user is near the bottom to avoid disrupting reading
const host = this;
const nearBottom = host.scrollHeight - host.scrollTop - host.clientHeight < 64;
if (nearBottom) host.scrollTop = host.scrollHeight;
}
}
declare global {
interface Window {
// no globals required for minimal build
}
}
if (!customElements.get("streaming-md")) {
customElements.define("streaming-md", StreamingMarkdownElement);
}
export { StreamingMarkdownElement };