@convai/web-sdk 1.9.0-beta.0 → 1.9.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/embed/chat-embed-v1.js +1 -1
- package/dist/embed/chat-embed-v1.js.map +1 -1
- package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts +14 -3
- package/dist/react/components/rtc-widget/components/MarkdownRenderer.d.ts.map +1 -1
- package/dist/react/components/rtc-widget/components/MarkdownRenderer.js +110 -130
- package/dist/react/components/rtc-widget/components/MarkdownRenderer.js.map +1 -1
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/utils/lightbox.d.ts +16 -0
- package/dist/utils/lightbox.d.ts.map +1 -0
- package/dist/utils/lightbox.js +95 -0
- package/dist/utils/lightbox.js.map +1 -0
- package/dist/utils/linkPresentation.d.ts +69 -0
- package/dist/utils/linkPresentation.d.ts.map +1 -0
- package/dist/utils/linkPresentation.js +358 -0
- package/dist/utils/linkPresentation.js.map +1 -0
- package/dist/vanilla/ConvaiWidget.d.ts.map +1 -1
- package/dist/vanilla/ConvaiWidget.js +216 -189
- package/dist/vanilla/ConvaiWidget.js.map +1 -1
- package/dist/vanilla/index.d.ts +2 -0
- package/dist/vanilla/index.d.ts.map +1 -1
- package/dist/vanilla/index.js +1 -0
- package/dist/vanilla/index.js.map +1 -1
- package/dist/vanilla/styles.d.ts.map +1 -1
- package/dist/vanilla/styles.js +9 -0
- package/dist/vanilla/styles.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* The widget's bubble renderer. Everything visual comes from
|
|
4
|
+
* utils/linkPresentation so this file and the vanilla widget cannot drift;
|
|
5
|
+
* this file only maps tokenizer nodes onto JSX.
|
|
6
|
+
*
|
|
7
|
+
* `data-convai-part` mirrors the vanilla widget's `part` names. React renders
|
|
8
|
+
* in light DOM where `part` means nothing, but the same names let tests and
|
|
9
|
+
* host CSS target both renderers with one vocabulary.
|
|
10
|
+
*/
|
|
2
11
|
interface MarkdownRendererProps {
|
|
3
12
|
text: string;
|
|
4
13
|
/**
|
|
5
|
-
* Render
|
|
6
|
-
*
|
|
7
|
-
*
|
|
14
|
+
* Render cards and chips under links. Bot messages want them; the user's
|
|
15
|
+
* own bubble does not — you already know what you typed, and a preview
|
|
16
|
+
* there would let a URL a user pasted embed remote media into the
|
|
8
17
|
* transcript. Links stay clickable either way.
|
|
9
18
|
*/
|
|
10
19
|
media?: boolean;
|
|
20
|
+
/** Test seam for mixed-content detection. Defaults to the page's protocol. */
|
|
21
|
+
pageProtocol?: string;
|
|
11
22
|
}
|
|
12
23
|
export declare const MarkdownRenderer: React.FC<MarkdownRendererProps>;
|
|
13
24
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownRenderer.d.ts","sourceRoot":"","sources":["../../../../../src/react/components/rtc-widget/components/MarkdownRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"MarkdownRenderer.d.ts","sourceRoot":"","sources":["../../../../../src/react/components/rtc-widget/components/MarkdownRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAqB1B;;;;;;;;GAQG;AACH,UAAU,qBAAqB;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA4RD,eAAO,MAAM,gBAAgB,EAAE,KAAK,CAAC,EAAE,CAAC,qBAAqB,CA4B5D,CAAC"}
|
|
@@ -1,157 +1,137 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import React from "react";
|
|
3
|
-
import { parseInlineMarkdown, compactUrl
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
style.id = SHIMMER;
|
|
18
|
-
style.textContent = `
|
|
19
|
-
@keyframes ${SHIMMER} { from { background-position: 200% 0 } to { background-position: -200% 0 } }
|
|
20
|
-
@media (prefers-reduced-motion: reduce) {
|
|
21
|
-
[data-convai-skeleton] { animation: none !important }
|
|
22
|
-
}`;
|
|
23
|
-
document.head.appendChild(style);
|
|
24
|
-
}
|
|
25
|
-
const linkStyle = {
|
|
26
|
-
color: "#10b981",
|
|
27
|
-
textDecoration: "underline",
|
|
28
|
-
wordBreak: "break-all",
|
|
29
|
-
};
|
|
30
|
-
const mediaStyle = {
|
|
31
|
-
display: "block",
|
|
32
|
-
width: "100%",
|
|
33
|
-
height: "100%",
|
|
34
|
-
objectFit: "cover",
|
|
35
|
-
borderRadius: MEDIA_RADIUS,
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* A preview card sizes itself before the media arrives, so the bubble does not
|
|
39
|
-
* jump when an image decodes. 16/10 is a compromise: tall enough to read, short
|
|
40
|
-
* enough that several in a row still scroll.
|
|
41
|
-
*/
|
|
42
|
-
const CARD_ASPECT = "16 / 10";
|
|
43
|
-
const cardStyle = {
|
|
44
|
-
// A span by default is inline, where aspect-ratio does nothing.
|
|
45
|
-
display: "block",
|
|
46
|
-
position: "relative",
|
|
47
|
-
marginTop: 6,
|
|
48
|
-
maxWidth: 320,
|
|
49
|
-
aspectRatio: CARD_ASPECT,
|
|
50
|
-
borderRadius: MEDIA_RADIUS,
|
|
51
|
-
overflow: "hidden",
|
|
52
|
-
// The widget is embedded in someone else's page on an unknown background, so
|
|
53
|
-
// chrome is derived from the inherited text colour rather than a fixed grey.
|
|
54
|
-
background: "color-mix(in srgb, currentColor 6%, transparent)",
|
|
55
|
-
boxShadow: "inset 0 0 0 1px color-mix(in srgb, currentColor 10%, transparent)",
|
|
56
|
-
};
|
|
57
|
-
const skeletonStyle = {
|
|
58
|
-
position: "absolute",
|
|
59
|
-
inset: 0,
|
|
60
|
-
borderRadius: MEDIA_RADIUS,
|
|
61
|
-
background: "color-mix(in srgb, currentColor 8%, transparent)",
|
|
62
|
-
backgroundImage: "linear-gradient(90deg, transparent, color-mix(in srgb, currentColor 12%, transparent), transparent)",
|
|
63
|
-
backgroundSize: "200% 100%",
|
|
64
|
-
animation: `${SHIMMER} 1.4s ease-in-out infinite`,
|
|
65
|
-
};
|
|
66
|
-
const captionStyle = {
|
|
67
|
-
display: "block",
|
|
68
|
-
marginTop: 4,
|
|
69
|
-
fontSize: 11,
|
|
70
|
-
opacity: 0.7,
|
|
71
|
-
whiteSpace: "nowrap",
|
|
72
|
-
overflow: "hidden",
|
|
73
|
-
textOverflow: "ellipsis",
|
|
3
|
+
import { parseInlineMarkdown, compactUrl } from "../../../../utils/inlineMarkdown.js";
|
|
4
|
+
import { linkKindForUrl, describeUrl, fileNameOf, documentBadge, contactLabel, hoverTitle, isMixedContent, glyph, presentationStyles as S, PREVIEW_TIMEOUT_MS, MAX_CARDS_PER_MESSAGE, } from "../../../../utils/linkPresentation.js";
|
|
5
|
+
import { openLightbox } from "../../../../utils/lightbox.js";
|
|
6
|
+
import { injectKeyframes } from "../../../../vanilla/styles.js";
|
|
7
|
+
const css = (...objs) => Object.assign({}, ...objs);
|
|
8
|
+
const Glyph = ({ name, style }) => (_jsx("span", { style: css(S.contactGlyph, style ?? {}), dangerouslySetInnerHTML: { __html: glyph(name) } }));
|
|
9
|
+
/** Inline styles cannot express :focus-visible, so the ring is toggled by hand. */
|
|
10
|
+
const useFocusRing = () => {
|
|
11
|
+
const [focused, setFocused] = React.useState(false);
|
|
12
|
+
return {
|
|
13
|
+
style: focused ? S.linkFocus : {},
|
|
14
|
+
onFocus: (e) => setFocused(e.target.matches(":focus-visible")),
|
|
15
|
+
onBlur: () => setFocused(false),
|
|
16
|
+
};
|
|
74
17
|
};
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
18
|
+
const Anchor = ({ href, kind, label }) => {
|
|
19
|
+
const ring = useFocusRing();
|
|
20
|
+
// A bare URL is its own label and may be elided for display; the title
|
|
21
|
+
// compares against what is actually shown so the full target is one hover away.
|
|
22
|
+
const display = label === href ? compactUrl(label) : label;
|
|
23
|
+
return (_jsxs("a", { href: href, target: "_blank", rel: "noopener noreferrer", title: hoverTitle(kind, href, display), "data-convai-part": "message-link", style: css(S.link, ring.style), onFocus: ring.onFocus, onBlur: ring.onBlur, children: [display, kind === "ftp" && (_jsx("span", { "data-convai-part": "message-link-badge", style: css(S.badge), children: "FTP" }))] }));
|
|
80
24
|
};
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
textDecoration: "underline",
|
|
85
|
-
textDecorationStyle: "dotted",
|
|
86
|
-
cursor: "help",
|
|
25
|
+
const ContactChip = ({ href, kind }) => {
|
|
26
|
+
const ring = useFocusRing();
|
|
27
|
+
return (_jsxs("a", { href: href, title: hoverTitle(kind, href, href), "data-convai-part": "message-contact", style: css(S.contact, ring.style), onFocus: ring.onFocus, onBlur: ring.onBlur, children: [_jsx(Glyph, { name: kind }), _jsx("span", { children: contactLabel(href) })] }));
|
|
87
28
|
};
|
|
88
|
-
|
|
89
|
-
|
|
29
|
+
const DocumentCard = ({ href }) => {
|
|
30
|
+
const ring = useFocusRing();
|
|
31
|
+
let host = "";
|
|
90
32
|
try {
|
|
91
|
-
|
|
92
|
-
const file = pathname.split("/").filter(Boolean).pop();
|
|
93
|
-
return file ? `${host} · ${file}` : host;
|
|
33
|
+
host = new URL(href).host;
|
|
94
34
|
}
|
|
95
35
|
catch {
|
|
96
|
-
|
|
36
|
+
/* keep empty */
|
|
97
37
|
}
|
|
98
|
-
}
|
|
38
|
+
return (_jsxs("a", { href: href, target: "_blank", rel: "noopener noreferrer", title: href, "data-convai-part": "message-file-card", style: css(S.fileCard, ring.style), onFocus: ring.onFocus, onBlur: ring.onBlur, children: [_jsx("span", { "data-convai-part": "message-file-icon", style: css(S.fileIcon), children: documentBadge(href) }), _jsxs("span", { style: css(S.fileBody), children: [_jsx("span", { "data-convai-part": "message-file-name", style: css(S.fileName), children: fileNameOf(href) }), _jsxs("span", { "data-convai-part": "message-file-meta", style: css(S.fileMeta), children: [host, " \u00B7 Opens in a new tab"] })] })] }));
|
|
39
|
+
};
|
|
99
40
|
/**
|
|
100
|
-
* Inline preview for an image or video URL
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* resolve — which a character can easily invent — collapses to one muted line
|
|
105
|
-
* instead of leaving an empty player in the transcript.
|
|
41
|
+
* Inline preview for an image or video URL. The element is in the tree from
|
|
42
|
+
* the start so the fetch begins immediately; a skeleton covers it until it
|
|
43
|
+
* decodes. Error, timeout, or an insecure URL collapse it to one muted line
|
|
44
|
+
* so a URL the character invented never leaves an empty player behind.
|
|
106
45
|
*/
|
|
107
|
-
const MediaPreview = ({ src, alt, kind }) => {
|
|
108
|
-
const
|
|
109
|
-
React.
|
|
46
|
+
const MediaPreview = ({ src, alt, kind, pageProtocol, }) => {
|
|
47
|
+
const insecure = isMixedContent(src, pageProtocol);
|
|
48
|
+
const [state, setState] = React.useState(insecure ? "error" : "loading");
|
|
49
|
+
const cardRef = React.useRef(null);
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
injectKeyframes(document);
|
|
52
|
+
}, []);
|
|
53
|
+
React.useEffect(() => {
|
|
54
|
+
if (state !== "loading")
|
|
55
|
+
return;
|
|
56
|
+
const t = setTimeout(() => setState((s) => (s === "loading" ? "error" : s)), PREVIEW_TIMEOUT_MS);
|
|
57
|
+
return () => clearTimeout(t);
|
|
58
|
+
}, [state]);
|
|
110
59
|
if (state === "error") {
|
|
111
|
-
return _jsxs("span", { style:
|
|
60
|
+
return (_jsxs("span", { "data-convai-part": "message-media-unavailable", style: css(S.unavailable), children: ["Preview unavailable \u2014 ", describeUrl(src), insecure ? " (insecure URL)" : ""] }));
|
|
112
61
|
}
|
|
113
62
|
const ready = state === "ready";
|
|
114
|
-
|
|
63
|
+
const open = () => openLightbox({
|
|
64
|
+
src,
|
|
65
|
+
alt,
|
|
66
|
+
caption: describeUrl(src),
|
|
67
|
+
href: src,
|
|
68
|
+
root: document,
|
|
69
|
+
returnFocusTo: cardRef.current,
|
|
70
|
+
});
|
|
71
|
+
const buttonProps = kind === "image"
|
|
72
|
+
? {
|
|
73
|
+
role: "button",
|
|
74
|
+
tabIndex: 0,
|
|
75
|
+
"aria-label": `Open image: ${fileNameOf(src)}`,
|
|
76
|
+
onClick: open,
|
|
77
|
+
onKeyDown: (e) => {
|
|
78
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
79
|
+
e.preventDefault();
|
|
80
|
+
open();
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
: {};
|
|
85
|
+
return (_jsxs(_Fragment, { children: [_jsxs("span", { ref: cardRef, "data-convai-part": "message-media-card", style: css(S.card, kind === "image" ? S.cardButton : {}), ...buttonProps, children: [kind === "image" ? (_jsx("img", { src: src, alt: alt, loading: "lazy", "data-convai-part": "message-media", onLoad: () => setState("ready"), onError: () => setState("error"), style: css(S.media, { opacity: ready ? 1 : 0 }) })) : (_jsx("video", { src: src, controls: true, preload: "metadata", "data-convai-part": "message-media", onLoadedMetadata: () => setState("ready"), onError: () => setState("error"), style: css(S.media, S.videoMedia, { opacity: ready ? 1 : 0 }) })), !ready && (_jsx("span", { "data-convai-skeleton": "", "data-convai-part": "message-media-skeleton", style: css(S.skeleton), "aria-hidden": "true" })), kind === "image" && (_jsx("span", { "data-convai-part": "message-media-zoom", style: css(S.zoom), children: _jsx(Glyph, { name: "zoom", style: { fontSize: 14 } }) }))] }), _jsx("span", { "data-convai-part": "message-media-caption", style: css(S.caption), children: describeUrl(src) })] }));
|
|
115
86
|
};
|
|
116
|
-
const renderNode = (node, key, media) => {
|
|
87
|
+
const renderNode = (node, key, media, budget, pageProtocol) => {
|
|
117
88
|
switch (node.kind) {
|
|
118
89
|
case "strong":
|
|
119
90
|
return _jsx("strong", { children: node.text }, key);
|
|
120
91
|
case "em":
|
|
121
92
|
return _jsx("em", { children: node.text }, key);
|
|
122
|
-
case "link":
|
|
123
|
-
return (_jsxs(React.Fragment, { children: [_jsx("a", { href: node.href, target: "_blank", rel: "noopener noreferrer", style: linkStyle, children: node.label === node.href ? compactUrl(node.label) : node.label }), media && node.media && (_jsx(MediaPreview, { src: node.href, alt: node.label, kind: node.media }))] }, key));
|
|
124
93
|
case "blocked":
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
94
|
+
// Not an anchor, ever. The label reads as the character wrote it; the
|
|
95
|
+
// target is only revealed on hover.
|
|
96
|
+
return (_jsx("span", { "data-convai-part": "message-link-blocked", style: css(S.blocked), title: `Link blocked: ${node.href}`, children: node.label }, key));
|
|
97
|
+
case "image": {
|
|
98
|
+
if (media && budget.cards < MAX_CARDS_PER_MESSAGE) {
|
|
99
|
+
budget.cards++;
|
|
100
|
+
return (_jsx(MediaPreview, { src: node.src, alt: node.alt, kind: "image", pageProtocol: pageProtocol }, key));
|
|
101
|
+
}
|
|
102
|
+
// With previews off (or the budget spent), an image still has to say
|
|
103
|
+
// what it pointed at, so it degrades to the same anchor a link renders.
|
|
104
|
+
return _jsx(Anchor, { href: node.src, kind: "image", label: node.alt || node.src }, key);
|
|
105
|
+
}
|
|
106
|
+
case "link": {
|
|
107
|
+
const kind = linkKindForUrl(node.href);
|
|
108
|
+
if (kind === "email" || kind === "phone" || kind === "sms") {
|
|
109
|
+
return media ? (_jsx(ContactChip, { href: node.href, kind: kind }, key)) : (_jsx(Anchor, { href: node.href, kind: kind, label: node.label }, key));
|
|
110
|
+
}
|
|
111
|
+
const wantsCard = media &&
|
|
112
|
+
(kind === "image" || kind === "video" || kind === "document") &&
|
|
113
|
+
budget.cards < MAX_CARDS_PER_MESSAGE;
|
|
114
|
+
if (wantsCard)
|
|
115
|
+
budget.cards++;
|
|
116
|
+
return (_jsxs(React.Fragment, { children: [_jsx(Anchor, { href: node.href, kind: kind, label: node.label }), wantsCard && kind === "document" && _jsx(DocumentCard, { href: node.href }), wantsCard && kind !== "document" && (_jsx(MediaPreview, { src: node.href, alt: node.label, kind: kind, pageProtocol: pageProtocol }))] }, key));
|
|
117
|
+
}
|
|
130
118
|
default:
|
|
131
119
|
return _jsx(React.Fragment, { children: node.text }, key);
|
|
132
120
|
}
|
|
133
121
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return lines.map((line, lineIndex) => {
|
|
145
|
-
// Bold, italic, links, images and bare URLs — see utils/inlineMarkdown.
|
|
146
|
-
const processedLine = parseInlineMarkdown(line).map((node, i) => renderNode(node, `${node.kind}-${lineIndex}-${i}`, media));
|
|
147
|
-
// Handle empty lines (creates paragraph spacing)
|
|
148
|
-
if (line.trim() === "" && lineIndex > 0) {
|
|
122
|
+
const wrapperStyle = { whiteSpace: "pre-wrap", wordWrap: "break-word" };
|
|
123
|
+
export const MarkdownRenderer = ({ text, media = true, pageProtocol, }) => {
|
|
124
|
+
if (!text)
|
|
125
|
+
return _jsx("div", { style: wrapperStyle });
|
|
126
|
+
// Handle both actual newlines and literal "\n" sequences.
|
|
127
|
+
const lines = text.replace(/\\n/g, "\n").split("\n");
|
|
128
|
+
const budget = { cards: 0 };
|
|
129
|
+
return (_jsx("div", { style: wrapperStyle, children: lines.map((line, lineIndex) => {
|
|
130
|
+
// An empty line is paragraph spacing.
|
|
131
|
+
if (line.trim() === "" && lineIndex > 0)
|
|
149
132
|
return _jsx("br", {}, `br-${lineIndex}`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
};
|
|
155
|
-
return _jsx("div", { style: { whiteSpace: 'pre-wrap', wordWrap: 'break-word' }, children: renderMarkdown(text) });
|
|
133
|
+
const nodes = parseInlineMarkdown(line).map((node, i) => renderNode(node, `${node.kind}-${lineIndex}-${i}`, media, budget, pageProtocol));
|
|
134
|
+
return (_jsxs(React.Fragment, { children: [nodes.length > 0 ? nodes : line, lineIndex < lines.length - 1 && _jsx("br", {})] }, `line-${lineIndex}`));
|
|
135
|
+
}) }));
|
|
156
136
|
};
|
|
157
137
|
//# sourceMappingURL=MarkdownRenderer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MarkdownRenderer.js","sourceRoot":"","sources":["../../../../../src/react/components/rtc-widget/components/MarkdownRenderer.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EACL,mBAAmB,EACnB,UAAU,GAGX,MAAM,kCAAkC,CAAC;AAE1C,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,OAAO,GAAG,wBAAwB,CAAC;AAEzC;;;;GAIG;AACH,SAAS,sBAAsB;IAC7B,IAAI,OAAO,QAAQ,KAAK,WAAW;QAAE,OAAO;IAC5C,IAAI,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC;QAAE,OAAO;IAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IAC9C,KAAK,CAAC,EAAE,GAAG,OAAO,CAAC;IACnB,KAAK,CAAC,WAAW,GAAG;aACT,OAAO;;;EAGl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down,\n compactUrl,\n type InlineNode,\n type MediaKind,\n} from \"../../../../utils/inlineMarkdown\";\n\nconst MEDIA_RADIUS = 8;\nconst SHIMMER = \"convai-preview-shimmer\";\n\n/**\n * The renderer styles inline, but a keyframe cannot be expressed that way, so\n * the one animation it needs is injected once per document. Guarded for SSR and\n * for the tests, which render to a string with no document.\n */\nfunction ensureShimmerKeyframes(): void {\n if (typeof document === \"undefined\") return;\n if (document.getElementById(SHIMMER)) return;\n const style = document.createElement(\"style\");\n style.id = SHIMMER;\n style.textContent = `\n@keyframes ${SHIMMER} { from { background-position: 200% 0 } to { background-position: -200% 0 } }\n@media (prefers-reduced-motion: reduce) {\n [data-convai-skeleton] { animation: none !important }\n}`;\n document.head.appendChild(style);\n}\n\ninterface MarkdownRendererProps {\n text: string;\n /**\n * Render inline image/video previews under a media link. Bot messages want\n * them; the user's own bubble does not — you already know what you typed, and\n * a preview there would let a URL a user pasted embed remote media into the\n * transcript. Links stay clickable either way.\n */\n media?: boolean;\n}\n\nconst linkStyle: React.CSSProperties = {\n color: \"#10b981\",\n textDecoration: \"underline\",\n wordBreak: \"break-all\",\n};\n\nconst mediaStyle: React.CSSProperties = {\n display: \"block\",\n width: \"100%\",\n height: \"100%\",\n objectFit: \"cover\",\n borderRadius: MEDIA_RADIUS,\n};\n\n/**\n * A preview card sizes itself before the media arrives, so the bubble does not\n * jump when an image decodes. 16/10 is a compromise: tall enough to read, short\n * enough that several in a row still scroll.\n */\nconst CARD_ASPECT = \"16 / 10\";\n\nconst cardStyle: React.CSSProperties = {\n // A span by default is inline, where aspect-ratio does nothing.\n display: \"block\",\n position: \"relative\",\n marginTop: 6,\n maxWidth: 320,\n aspectRatio: CARD_ASPECT,\n borderRadius: MEDIA_RADIUS,\n overflow: \"hidden\",\n // The widget is embedded in someone else's page on an unknown background, so\n // chrome is derived from the inherited text colour rather than a fixed grey.\n background: \"color-mix(in srgb, currentColor 6%, transparent)\",\n boxShadow: \"inset 0 0 0 1px color-mix(in srgb, currentColor 10%, transparent)\",\n};\n\nconst skeletonStyle: React.CSSProperties = {\n position: \"absolute\",\n inset: 0,\n borderRadius: MEDIA_RADIUS,\n background: \"color-mix(in srgb, currentColor 8%, transparent)\",\n backgroundImage:\n \"linear-gradient(90deg, transparent, color-mix(in srgb, currentColor 12%, transparent), transparent)\",\n backgroundSize: \"200% 100%\",\n animation: `${SHIMMER} 1.4s ease-in-out infinite`,\n};\n\nconst captionStyle: React.CSSProperties = {\n display: \"block\",\n marginTop: 4,\n fontSize: 11,\n opacity: 0.7,\n whiteSpace: \"nowrap\",\n overflow: \"hidden\",\n textOverflow: \"ellipsis\",\n};\n\nconst unavailableStyle: React.CSSProperties = {\n ...captionStyle,\n marginTop: 6,\n opacity: 0.55,\n fontStyle: \"italic\",\n};\n\n/** An unsafe target: the words the character wrote, with no href behind them. */\nconst blockedStyle: React.CSSProperties = {\n opacity: 0.75,\n textDecoration: \"underline\",\n textDecorationStyle: \"dotted\",\n cursor: \"help\",\n};\n\n/** `cdn.example.com/a/b/c.png` → `cdn.example.com · c.png` */\nfunction describeUrl(url: string): string {\n try {\n const { host, pathname } = new URL(url);\n const file = pathname.split(\"/\").filter(Boolean).pop();\n return file ? `${host} · ${file}` : host;\n } catch {\n return compactUrl(url);\n }\n}\n\ntype LoadState = \"loading\" | \"ready\" | \"error\";\n\n/**\n * Inline preview for an image or video URL, shown under its link.\n *\n * The media element is always in the tree so the browser starts fetching it\n * immediately; the skeleton sits on top until it decodes. A URL that does not\n * resolve — which a character can easily invent — collapses to one muted line\n * instead of leaving an empty player in the transcript.\n */\nconst MediaPreview = ({ src, alt, kind }: { src: string; alt: string; kind: MediaKind }) => {\n const [state, setState] = React.useState<LoadState>(\"loading\");\n React.useEffect(ensureShimmerKeyframes, []);\n\n if (state === \"error\") {\n return <span style={unavailableStyle}>Preview unavailable — {describeUrl(src)}</span>;\n }\n\n const ready = state === \"ready\";\n return (\n <>\n <span style={cardStyle}>\n {kind === \"image\" ? (\n <img\n src={src}\n alt={alt}\n loading=\"lazy\"\n onLoad={() => setState(\"ready\")}\n onError={() => setState(\"error\")}\n style={{ ...mediaStyle, opacity: ready ? 1 : 0 }}\n />\n ) : (\n <video\n src={src}\n controls\n preload=\"metadata\"\n onLoadedMetadata={() => setState(\"ready\")}\n onError={() => setState(\"error\")}\n style={{ ...mediaStyle, objectFit: \"contain\", opacity: ready ? 1 : 0 }}\n />\n )}\n {!ready && <span data-convai-skeleton=\"\" style={skeletonStyle} aria-hidden=\"true\" />}\n </span>\n <span style={captionStyle}>{describeUrl(src)}</span>\n </>\n );\n};\n\nconst renderNode = (node: InlineNode, key: string, media: boolean): React.ReactNode => {\n switch (node.kind) {\n case \"strong\":\n return <strong key={key}>{node.text}</strong>;\n case \"em\":\n return <em key={key}>{node.text}</em>;\n case \"link\":\n return (\n <React.Fragment key={key}>\n <a href={node.href} target=\"_blank\" rel=\"noopener noreferrer\" style={linkStyle}>\n {node.label === node.href ? compactUrl(node.label) : node.label}\n </a>\n {media && node.media && (\n <MediaPreview src={node.href} alt={node.label} kind={node.media} />\n )}\n </React.Fragment>\n );\n case \"blocked\":\n return (\n <span key={key} style={blockedStyle} title={`Link blocked: ${node.href}`}>\n {node.label}\n </span>\n );\n case \"image\":\n // With previews off, an image still has to say what it pointed at, so it\n // degrades to the same anchor a plain link would render.\n return media ? (\n <MediaPreview key={key} src={node.src} alt={node.alt} kind=\"image\" />\n ) : (\n <a key={key} href={node.src} target=\"_blank\" rel=\"noopener noreferrer\" style={linkStyle}>\n {node.alt || node.src}\n </a>\n );\n default:\n return <React.Fragment key={key}>{node.text}</React.Fragment>;\n }\n};\n\nexport const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({ text, media = true }) => {\n // Convert markdown to JSX elements\n const renderMarkdown = (input: string): React.ReactNode => {\n if (!input) return null;\n\n // Handle both actual newlines and \\n escape sequences\n // Replace literal \\n strings with actual newlines\n const normalizedText = input.replace(/\\\\n/g, '\\n');\n\n // Split by actual newlines\n const lines = normalizedText.split('\\n');\n\n return lines.map((line, lineIndex) => {\n // Bold, italic, links, images and bare URLs — see utils/inlineMarkdown.\n const processedLine = parseInlineMarkdown(line).map((node, i) =>\n renderNode(node, `${node.kind}-${lineIndex}-${i}`, media),\n );\n\n // Handle empty lines (creates paragraph spacing)\n if (line.trim() === \"\" && lineIndex > 0) {\n return <br key={`br-${lineIndex}`} />;\n }\n\n // Return the line with line break\n return (\n <React.Fragment key={`line-${lineIndex}`}>\n {processedLine.length > 0 ? processedLine : line}\n {lineIndex < lines.length - 1 && <br />}\n </React.Fragment>\n );\n });\n };\n\n return <div style={{ whiteSpace: 'pre-wrap', wordWrap: 'break-word' }}>{renderMarkdown(text)}</div>;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"MarkdownRenderer.js","sourceRoot":"","sources":["../../../../../src/react/components/rtc-widget/components/MarkdownRenderer.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAmB,MAAM,kCAAkC,CAAC;AACpG,OAAO,EACL,cAAc,EACd,WAAW,EACX,UAAU,EACV,aAAa,EACb,YAAY,EACZ,UAAU,EACV,cAAc,EACd,KAAK,EACL,kBAAkB,IAAI,CAAC,EACvB,kBAAkB,EAClB,qBAAqB,GAItB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAwB7D,MAAM,GAAG,GAAG,CAAC,GAAG,IAAmB,EAAuB,EAAE,CAC1D,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,IAAI,CAAwB,CAAC;AAEpD,MAAM,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,EAA4C,EAAE,EAAE,CAAC,CAC3E,eAAM,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,YAAY,EAAE,KAAK,IAAI,EAAE,CAAC,EAAE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ode } from \"../../../../utils/inlineMarkdown\";\nimport {\n linkKindForUrl,\n describeUrl,\n fileNameOf,\n documentBadge,\n contactLabel,\n hoverTitle,\n isMixedContent,\n glyph,\n presentationStyles as S,\n PREVIEW_TIMEOUT_MS,\n MAX_CARDS_PER_MESSAGE,\n type GlyphName,\n type LinkKind,\n type StyleObject,\n} from \"../../../../utils/linkPresentation\";\nimport { openLightbox } from \"../../../../utils/lightbox\";\nimport { injectKeyframes } from \"../../../../vanilla/styles\";\n\n/**\n * The widget's bubble renderer. Everything visual comes from\n * utils/linkPresentation so this file and the vanilla widget cannot drift;\n * this file only maps tokenizer nodes onto JSX.\n *\n * `data-convai-part` mirrors the vanilla widget's `part` names. React renders\n * in light DOM where `part` means nothing, but the same names let tests and\n * host CSS target both renderers with one vocabulary.\n */\ninterface MarkdownRendererProps {\n text: string;\n /**\n * Render cards and chips under links. Bot messages want them; the user's\n * own bubble does not — you already know what you typed, and a preview\n * there would let a URL a user pasted embed remote media into the\n * transcript. Links stay clickable either way.\n */\n media?: boolean;\n /** Test seam for mixed-content detection. Defaults to the page's protocol. */\n pageProtocol?: string;\n}\n\nconst css = (...objs: StyleObject[]): React.CSSProperties =>\n Object.assign({}, ...objs) as React.CSSProperties;\n\nconst Glyph = ({ name, style }: { name: GlyphName; style?: StyleObject }) => (\n <span style={css(S.contactGlyph, style ?? {})} dangerouslySetInnerHTML={{ __html: glyph(name) }} />\n);\n\n/** Inline styles cannot express :focus-visible, so the ring is toggled by hand. */\nconst useFocusRing = () => {\n const [focused, setFocused] = React.useState(false);\n return {\n style: focused ? S.linkFocus : {},\n onFocus: (e: React.FocusEvent<HTMLElement>) => setFocused(e.target.matches(\":focus-visible\")),\n onBlur: () => setFocused(false),\n };\n};\n\nconst Anchor = ({ href, kind, label }: { href: string; kind: LinkKind; label: string }) => {\n const ring = useFocusRing();\n // A bare URL is its own label and may be elided for display; the title\n // compares against what is actually shown so the full target is one hover away.\n const display = label === href ? compactUrl(label) : label;\n return (\n <a\n href={href}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n title={hoverTitle(kind, href, display)}\n data-convai-part=\"message-link\"\n style={css(S.link, ring.style)}\n onFocus={ring.onFocus}\n onBlur={ring.onBlur}\n >\n {display}\n {kind === \"ftp\" && (\n <span data-convai-part=\"message-link-badge\" style={css(S.badge)}>\n FTP\n </span>\n )}\n </a>\n );\n};\n\nconst ContactChip = ({ href, kind }: { href: string; kind: \"email\" | \"phone\" | \"sms\" }) => {\n const ring = useFocusRing();\n return (\n <a\n href={href}\n title={hoverTitle(kind, href, href)}\n data-convai-part=\"message-contact\"\n style={css(S.contact, ring.style)}\n onFocus={ring.onFocus}\n onBlur={ring.onBlur}\n >\n <Glyph name={kind} />\n <span>{contactLabel(href)}</span>\n </a>\n );\n};\n\nconst DocumentCard = ({ href }: { href: string }) => {\n const ring = useFocusRing();\n let host = \"\";\n try {\n host = new URL(href).host;\n } catch {\n /* keep empty */\n }\n return (\n <a\n href={href}\n target=\"_blank\"\n rel=\"noopener noreferrer\"\n title={href}\n data-convai-part=\"message-file-card\"\n style={css(S.fileCard, ring.style)}\n onFocus={ring.onFocus}\n onBlur={ring.onBlur}\n >\n <span data-convai-part=\"message-file-icon\" style={css(S.fileIcon)}>\n {documentBadge(href)}\n </span>\n <span style={css(S.fileBody)}>\n <span data-convai-part=\"message-file-name\" style={css(S.fileName)}>\n {fileNameOf(href)}\n </span>\n <span data-convai-part=\"message-file-meta\" style={css(S.fileMeta)}>\n {host} · Opens in a new tab\n </span>\n </span>\n </a>\n );\n};\n\ntype LoadState = \"loading\" | \"ready\" | \"error\";\n\n/**\n * Inline preview for an image or video URL. The element is in the tree from\n * the start so the fetch begins immediately; a skeleton covers it until it\n * decodes. Error, timeout, or an insecure URL collapse it to one muted line\n * so a URL the character invented never leaves an empty player behind.\n */\nconst MediaPreview = ({\n src,\n alt,\n kind,\n pageProtocol,\n}: {\n src: string;\n alt: string;\n kind: \"image\" | \"video\";\n pageProtocol?: string;\n}) => {\n const insecure = isMixedContent(src, pageProtocol);\n const [state, setState] = React.useState<LoadState>(insecure ? \"error\" : \"loading\");\n const cardRef = React.useRef<HTMLSpanElement>(null);\n React.useEffect(() => {\n injectKeyframes(document);\n }, []);\n React.useEffect(() => {\n if (state !== \"loading\") return;\n const t = setTimeout(() => setState((s) => (s === \"loading\" ? \"error\" : s)), PREVIEW_TIMEOUT_MS);\n return () => clearTimeout(t);\n }, [state]);\n\n if (state === \"error\") {\n return (\n <span data-convai-part=\"message-media-unavailable\" style={css(S.unavailable)}>\n Preview unavailable — {describeUrl(src)}\n {insecure ? \" (insecure URL)\" : \"\"}\n </span>\n );\n }\n\n const ready = state === \"ready\";\n const open = () =>\n openLightbox({\n src,\n alt,\n caption: describeUrl(src),\n href: src,\n root: document,\n returnFocusTo: cardRef.current,\n });\n const buttonProps =\n kind === \"image\"\n ? {\n role: \"button\",\n tabIndex: 0,\n \"aria-label\": `Open image: ${fileNameOf(src)}`,\n onClick: open,\n onKeyDown: (e: React.KeyboardEvent) => {\n if (e.key === \"Enter\" || e.key === \" \") {\n e.preventDefault();\n open();\n }\n },\n }\n : {};\n\n return (\n <>\n <span\n ref={cardRef}\n data-convai-part=\"message-media-card\"\n style={css(S.card, kind === \"image\" ? S.cardButton : {})}\n {...buttonProps}\n >\n {kind === \"image\" ? (\n <img\n src={src}\n alt={alt}\n loading=\"lazy\"\n data-convai-part=\"message-media\"\n onLoad={() => setState(\"ready\")}\n onError={() => setState(\"error\")}\n style={css(S.media, { opacity: ready ? 1 : 0 })}\n />\n ) : (\n <video\n src={src}\n controls\n preload=\"metadata\"\n data-convai-part=\"message-media\"\n onLoadedMetadata={() => setState(\"ready\")}\n onError={() => setState(\"error\")}\n style={css(S.media, S.videoMedia, { opacity: ready ? 1 : 0 })}\n />\n )}\n {!ready && (\n <span\n data-convai-skeleton=\"\"\n data-convai-part=\"message-media-skeleton\"\n style={css(S.skeleton)}\n aria-hidden=\"true\"\n />\n )}\n {kind === \"image\" && (\n <span data-convai-part=\"message-media-zoom\" style={css(S.zoom)}>\n <Glyph name=\"zoom\" style={{ fontSize: 14 }} />\n </span>\n )}\n </span>\n <span data-convai-part=\"message-media-caption\" style={css(S.caption)}>\n {describeUrl(src)}\n </span>\n </>\n );\n};\n\n/** One per message: cards drawn so far, against MAX_CARDS_PER_MESSAGE. */\ninterface CardBudget {\n cards: number;\n}\n\nconst renderNode = (\n node: InlineNode,\n key: string,\n media: boolean,\n budget: CardBudget,\n pageProtocol?: string,\n): React.ReactNode => {\n switch (node.kind) {\n case \"strong\":\n return <strong key={key}>{node.text}</strong>;\n case \"em\":\n return <em key={key}>{node.text}</em>;\n case \"blocked\":\n // Not an anchor, ever. The label reads as the character wrote it; the\n // target is only revealed on hover.\n return (\n <span\n key={key}\n data-convai-part=\"message-link-blocked\"\n style={css(S.blocked)}\n title={`Link blocked: ${node.href}`}\n >\n {node.label}\n </span>\n );\n case \"image\": {\n if (media && budget.cards < MAX_CARDS_PER_MESSAGE) {\n budget.cards++;\n return (\n <MediaPreview key={key} src={node.src} alt={node.alt} kind=\"image\" pageProtocol={pageProtocol} />\n );\n }\n // With previews off (or the budget spent), an image still has to say\n // what it pointed at, so it degrades to the same anchor a link renders.\n return <Anchor key={key} href={node.src} kind=\"image\" label={node.alt || node.src} />;\n }\n case \"link\": {\n const kind = linkKindForUrl(node.href);\n if (kind === \"email\" || kind === \"phone\" || kind === \"sms\") {\n return media ? (\n <ContactChip key={key} href={node.href} kind={kind} />\n ) : (\n <Anchor key={key} href={node.href} kind={kind} label={node.label} />\n );\n }\n const wantsCard =\n media &&\n (kind === \"image\" || kind === \"video\" || kind === \"document\") &&\n budget.cards < MAX_CARDS_PER_MESSAGE;\n if (wantsCard) budget.cards++;\n return (\n <React.Fragment key={key}>\n <Anchor href={node.href} kind={kind} label={node.label} />\n {wantsCard && kind === \"document\" && <DocumentCard href={node.href} />}\n {wantsCard && kind !== \"document\" && (\n <MediaPreview src={node.href} alt={node.label} kind={kind} pageProtocol={pageProtocol} />\n )}\n </React.Fragment>\n );\n }\n default:\n return <React.Fragment key={key}>{node.text}</React.Fragment>;\n }\n};\n\nconst wrapperStyle: React.CSSProperties = { whiteSpace: \"pre-wrap\", wordWrap: \"break-word\" };\n\nexport const MarkdownRenderer: React.FC<MarkdownRendererProps> = ({\n text,\n media = true,\n pageProtocol,\n}) => {\n if (!text) return <div style={wrapperStyle} />;\n\n // Handle both actual newlines and literal \"\\n\" sequences.\n const lines = text.replace(/\\\\n/g, \"\\n\").split(\"\\n\");\n const budget: CardBudget = { cards: 0 };\n\n return (\n <div style={wrapperStyle}>\n {lines.map((line, lineIndex) => {\n // An empty line is paragraph spacing.\n if (line.trim() === \"\" && lineIndex > 0) return <br key={`br-${lineIndex}`} />;\n const nodes = parseInlineMarkdown(line).map((node, i) =>\n renderNode(node, `${node.kind}-${lineIndex}-${i}`, media, budget, pageProtocol),\n );\n return (\n <React.Fragment key={`line-${lineIndex}`}>\n {nodes.length > 0 ? nodes : line}\n {lineIndex < lines.length - 1 && <br />}\n </React.Fragment>\n );\n })}\n </div>\n );\n};\n"]}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export { ConvaiWidget } from './components/ConvaiWidget.js';
|
|
|
3
3
|
export { MarkdownRenderer } from './components/rtc-widget/components/MarkdownRenderer.js';
|
|
4
4
|
export { parseInlineMarkdown, mediaKindForUrl, isSafeHref, compactUrl } from '../utils/inlineMarkdown.js';
|
|
5
5
|
export type { InlineNode, MediaKind } from '../utils/inlineMarkdown.js';
|
|
6
|
+
export { linkKindForUrl, describeUrl } from '../utils/linkPresentation.js';
|
|
7
|
+
export type { LinkKind } from '../utils/linkPresentation.js';
|
|
6
8
|
export * from './hooks/index.js';
|
|
7
9
|
export * from '../core/types.js';
|
|
8
10
|
export { getDisconnectReasonMessage } from '../core/types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAMzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qDAAqD,CAAC;AAGvF,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACvG,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAMzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qDAAqD,CAAC;AAGvF,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACvG,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxE,YAAY,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAG1D,cAAc,SAAS,CAAC;AAGxB,cAAc,eAAe,CAAC;AAG9B,OAAO,EAAE,0BAA0B,EAAE,MAAM,eAAe,CAAC;AAG3D,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAG7D,OAAO,EAAE,iBAAiB,IAAI,aAAa,EAAE,WAAW,IAAI,YAAY,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/react/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { ConvaiWidget } from './components/ConvaiWidget.js';
|
|
|
9
9
|
export { MarkdownRenderer } from './components/rtc-widget/components/MarkdownRenderer.js';
|
|
10
10
|
// The tokenizer behind it, for building a renderer from scratch.
|
|
11
11
|
export { parseInlineMarkdown, mediaKindForUrl, isSafeHref, compactUrl } from '../utils/inlineMarkdown.js';
|
|
12
|
+
export { linkKindForUrl, describeUrl } from '../utils/linkPresentation.js';
|
|
12
13
|
// Additional hooks (for advanced usage)
|
|
13
14
|
export * from './hooks/index.js';
|
|
14
15
|
// Re-export core types for convenience
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,aAAa;AACb,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,uEAAuE;AACvE,oEAAoE;AACpE,8EAA8E;AAC9E,yCAAyC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qDAAqD,CAAC;AAEvF,iEAAiE;AACjE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE1D,aAAa;AACb,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,uEAAuE;AACvE,oEAAoE;AACpE,8EAA8E;AAC9E,yCAAyC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qDAAqD,CAAC;AAEvF,iEAAiE;AACjE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEvG,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAGxE,wCAAwC;AACxC,cAAc,SAAS,CAAC;AAExB,uCAAuC;AACvC,cAAc,eAAe,CAAC;AAE9B,wCAAwC;AACxC,OAAO,EAAE,0BAA0B,EAAE,MAAM,eAAe,CAAC;AAE3D,2CAA2C;AAC3C,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE7D,qEAAqE;AACrE,OAAO,EAAE,iBAAiB,IAAI,aAAa,EAAE,WAAW,IAAI,YAAY,EAAE,MAAM,2BAA2B,CAAC","sourcesContent":["// Main hook (backward compatible)\nexport { useConvaiClient } from './hooks/useConvaiClient';\n\n// Components\nexport { ConvaiWidget } from './components/ConvaiWidget';\n\n// Bubble renderer, for a custom chat UI that is not ConvaiWidget. Pass\n// `media={false}` on the user's own bubble: previews are a bot-side\n// affordance, and rendering them on a line the user typed lets any pasted URL\n// pull remote media into the transcript.\nexport { MarkdownRenderer } from './components/rtc-widget/components/MarkdownRenderer';\n\n// The tokenizer behind it, for building a renderer from scratch.\nexport { parseInlineMarkdown, mediaKindForUrl, isSafeHref, compactUrl } from '../utils/inlineMarkdown';\nexport type { InlineNode, MediaKind } from '../utils/inlineMarkdown';\nexport { linkKindForUrl, describeUrl } from '../utils/linkPresentation';\nexport type { LinkKind } from '../utils/linkPresentation';\n\n// Additional hooks (for advanced usage)\nexport * from './hooks';\n\n// Re-export core types for convenience\nexport * from '../core/types';\n\n// Re-export helper functions explicitly\nexport { getDisconnectReasonMessage } from '../core/types';\n\n// Re-export core client for advanced usage\nexport { ActionResultAckError, ConvaiClient } from '../core';\n\n// Re-export LiveKit components for convenience (backward compatible)\nexport { RoomAudioRenderer as AudioRenderer, RoomContext as AudioContext } from '@livekit/components-react';\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface LightboxOptions {
|
|
2
|
+
src: string;
|
|
3
|
+
alt: string;
|
|
4
|
+
/** Shown under the image; normally `describeUrl(src)`. */
|
|
5
|
+
caption: string;
|
|
6
|
+
/** Target of "Open original". */
|
|
7
|
+
href: string;
|
|
8
|
+
root: Document | ShadowRoot;
|
|
9
|
+
/** Where focus goes back to on close. Defaults to whatever was focused on open. */
|
|
10
|
+
returnFocusTo?: HTMLElement | null;
|
|
11
|
+
}
|
|
12
|
+
export interface LightboxHandle {
|
|
13
|
+
close(): void;
|
|
14
|
+
}
|
|
15
|
+
export declare function openLightbox(opts: LightboxOptions): LightboxHandle;
|
|
16
|
+
//# sourceMappingURL=lightbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lightbox.d.ts","sourceRoot":"","sources":["../../src/utils/lightbox.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,0DAA0D;IAC1D,OAAO,EAAE,MAAM,CAAC;IAChB,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,QAAQ,GAAG,UAAU,CAAC;IAC5B,mFAAmF;IACnF,aAAa,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,IAAI,IAAI,CAAC;CACf;AAWD,wBAAgB,YAAY,CAAC,IAAI,EAAE,eAAe,GAAG,cAAc,CAsFlE"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image lightbox — one plain-DOM implementation used by the React
|
|
3
|
+
* renderer and the vanilla widget. Mounted into a Document or a ShadowRoot so
|
|
4
|
+
* `part` hooks and the injected keyframes resolve wherever the widget lives.
|
|
5
|
+
* One at a time: opening a second closes the first.
|
|
6
|
+
*/
|
|
7
|
+
import { glyph, presentationStyles, toCssText } from "./linkPresentation.js";
|
|
8
|
+
import { injectKeyframes } from "../vanilla/styles.js";
|
|
9
|
+
let current = null;
|
|
10
|
+
const el = (tag, part, style) => {
|
|
11
|
+
const node = document.createElement(tag);
|
|
12
|
+
node.setAttribute("part", part);
|
|
13
|
+
node.style.cssText = toCssText(style);
|
|
14
|
+
return node;
|
|
15
|
+
};
|
|
16
|
+
export function openLightbox(opts) {
|
|
17
|
+
current?.close();
|
|
18
|
+
injectKeyframes(opts.root);
|
|
19
|
+
const mount = opts.root instanceof ShadowRoot ? opts.root : opts.root.body;
|
|
20
|
+
const backdrop = el("div", "lightbox-backdrop", presentationStyles.lightboxBackdrop);
|
|
21
|
+
backdrop.setAttribute("role", "dialog");
|
|
22
|
+
backdrop.setAttribute("aria-modal", "true");
|
|
23
|
+
backdrop.setAttribute("aria-label", opts.alt || "Image preview");
|
|
24
|
+
const box = el("div", "lightbox", presentationStyles.lightbox);
|
|
25
|
+
const img = el("img", "lightbox-image", presentationStyles.lightboxImage);
|
|
26
|
+
img.src = opts.src;
|
|
27
|
+
img.alt = opts.alt;
|
|
28
|
+
const caption = el("span", "lightbox-caption", presentationStyles.lightboxCaption);
|
|
29
|
+
caption.textContent = opts.caption;
|
|
30
|
+
const controls = el("div", "lightbox-controls", presentationStyles.lightboxControls);
|
|
31
|
+
const open = el("a", "lightbox-open", presentationStyles.lightboxButton);
|
|
32
|
+
open.href = opts.href;
|
|
33
|
+
open.target = "_blank";
|
|
34
|
+
open.rel = "noopener noreferrer";
|
|
35
|
+
open.innerHTML = `${glyph("external")}<span>Open original</span>`;
|
|
36
|
+
const close = el("button", "lightbox-close", presentationStyles.lightboxButton);
|
|
37
|
+
close.type = "button";
|
|
38
|
+
close.setAttribute("aria-label", "Close image preview");
|
|
39
|
+
close.innerHTML = glyph("close");
|
|
40
|
+
controls.append(open, close);
|
|
41
|
+
// A character can invent a URL; the card already collapsed for that case,
|
|
42
|
+
// but the full-size load can still fail on its own. Keep the way out visible.
|
|
43
|
+
img.addEventListener("error", () => {
|
|
44
|
+
const fallback = el("div", "lightbox-fallback", presentationStyles.lightboxFallback);
|
|
45
|
+
fallback.textContent = "Image could not be loaded";
|
|
46
|
+
img.replaceWith(fallback);
|
|
47
|
+
});
|
|
48
|
+
const previousOverflow = document.body.style.overflow;
|
|
49
|
+
const restoreFocus = opts.returnFocusTo ?? document.activeElement;
|
|
50
|
+
const activeElement = () => opts.root instanceof ShadowRoot ? opts.root.activeElement : document.activeElement;
|
|
51
|
+
const onKey = (e) => {
|
|
52
|
+
if (e.key === "Escape") {
|
|
53
|
+
e.preventDefault();
|
|
54
|
+
handle.close();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (e.key === "Tab") {
|
|
58
|
+
// Trap between the two controls; there is nothing else to reach.
|
|
59
|
+
const active = activeElement();
|
|
60
|
+
if (e.shiftKey && active === open) {
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
close.focus();
|
|
63
|
+
}
|
|
64
|
+
else if (!e.shiftKey && active === close) {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
open.focus();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const handle = {
|
|
71
|
+
close() {
|
|
72
|
+
if (current !== handle)
|
|
73
|
+
return;
|
|
74
|
+
current = null;
|
|
75
|
+
document.removeEventListener("keydown", onKey, true);
|
|
76
|
+
backdrop.remove();
|
|
77
|
+
document.body.style.overflow = previousOverflow;
|
|
78
|
+
restoreFocus?.focus?.();
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
backdrop.addEventListener("click", (e) => {
|
|
82
|
+
if (e.target === backdrop)
|
|
83
|
+
handle.close();
|
|
84
|
+
});
|
|
85
|
+
close.addEventListener("click", () => handle.close());
|
|
86
|
+
document.addEventListener("keydown", onKey, true);
|
|
87
|
+
box.append(controls, img, caption);
|
|
88
|
+
backdrop.appendChild(box);
|
|
89
|
+
mount.appendChild(backdrop);
|
|
90
|
+
document.body.style.overflow = "hidden";
|
|
91
|
+
current = handle;
|
|
92
|
+
close.focus();
|
|
93
|
+
return handle;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=lightbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lightbox.js","sourceRoot":"","sources":["../../src/utils/lightbox.ts"],"names":[],"mappings":"AAAA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EAAE,IAAI,CAAC,CAAC;YACrD,QAAQ,CAAC,MAAM,EAAE,CAAC;YAClB,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,gBAAgB,CAAC;YAChD,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC;QAC1B,CAAC;KACF,CAAC;IAEF,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE;QACvC,IAAI,CAAC,CAAC,MAAM,KAAK,QAAQ;YAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IAC5C,CAAC,CAAC,CAAC;IACH,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IACtD,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAElD,GAAG,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IACnC,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1B,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC5B,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACxC,OAAO,GAAG,MAAM,CAAC;IACjB,KAAK,CAAC,KAAK,EAAE,CAAC;IACd,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["/**\n * The image lightbox — one plain-DOM implementation used by the React\n * renderer and the vanilla widget. Mounted into a Document or a ShadowRoot so\n * `part` hooks and the injected keyframes resolve wherever the widget lives.\n * One at a time: opening a second closes the first.\n */\nimport { glyph, presentationStyles, toCssText, type StyleObject } from \"./linkPresentation\";\nimport { injectKeyframes } from \"../vanilla/styles\";\n\nexport interface LightboxOptions {\n src: string;\n alt: string;\n /** Shown under the image; normally `describeUrl(src)`. */\n caption: string;\n /** Target of \"Open original\". */\n href: string;\n root: Document | ShadowRoot;\n /** Where focus goes back to on close. Defaults to whatever was focused on open. */\n returnFocusTo?: HTMLElement | null;\n}\n\nexport interface LightboxHandle {\n close(): void;\n}\n\nlet current: LightboxHandle | null = null;\n\nconst el = <K extends keyof HTMLElementTagNameMap>(tag: K, part: string, style: StyleObject) => {\n const node = document.createElement(tag);\n node.setAttribute(\"part\", part);\n node.style.cssText = toCssText(style);\n return node;\n};\n\nexport function openLightbox(opts: LightboxOptions): LightboxHandle {\n current?.close();\n injectKeyframes(opts.root);\n\n const mount: Node = opts.root instanceof ShadowRoot ? opts.root : opts.root.body;\n const backdrop = el(\"div\", \"lightbox-backdrop\", presentationStyles.lightboxBackdrop);\n backdrop.setAttribute(\"role\", \"dialog\");\n backdrop.setAttribute(\"aria-modal\", \"true\");\n backdrop.setAttribute(\"aria-label\", opts.alt || \"Image preview\");\n\n const box = el(\"div\", \"lightbox\", presentationStyles.lightbox);\n const img = el(\"img\", \"lightbox-image\", presentationStyles.lightboxImage);\n img.src = opts.src;\n img.alt = opts.alt;\n const caption = el(\"span\", \"lightbox-caption\", presentationStyles.lightboxCaption);\n caption.textContent = opts.caption;\n\n const controls = el(\"div\", \"lightbox-controls\", presentationStyles.lightboxControls);\n const open = el(\"a\", \"lightbox-open\", presentationStyles.lightboxButton);\n open.href = opts.href;\n open.target = \"_blank\";\n open.rel = \"noopener noreferrer\";\n open.innerHTML = `${glyph(\"external\")}<span>Open original</span>`;\n const close = el(\"button\", \"lightbox-close\", presentationStyles.lightboxButton);\n close.type = \"button\";\n close.setAttribute(\"aria-label\", \"Close image preview\");\n close.innerHTML = glyph(\"close\");\n controls.append(open, close);\n\n // A character can invent a URL; the card already collapsed for that case,\n // but the full-size load can still fail on its own. Keep the way out visible.\n img.addEventListener(\"error\", () => {\n const fallback = el(\"div\", \"lightbox-fallback\", presentationStyles.lightboxFallback);\n fallback.textContent = \"Image could not be loaded\";\n img.replaceWith(fallback);\n });\n\n const previousOverflow = document.body.style.overflow;\n const restoreFocus = opts.returnFocusTo ?? (document.activeElement as HTMLElement | null);\n\n const activeElement = () =>\n opts.root instanceof ShadowRoot ? opts.root.activeElement : document.activeElement;\n\n const onKey = (e: KeyboardEvent) => {\n if (e.key === \"Escape\") {\n e.preventDefault();\n handle.close();\n return;\n }\n if (e.key === \"Tab\") {\n // Trap between the two controls; there is nothing else to reach.\n const active = activeElement();\n if (e.shiftKey && active === open) {\n e.preventDefault();\n close.focus();\n } else if (!e.shiftKey && active === close) {\n e.preventDefault();\n open.focus();\n }\n }\n };\n\n const handle: LightboxHandle = {\n close() {\n if (current !== handle) return;\n current = null;\n document.removeEventListener(\"keydown\", onKey, true);\n backdrop.remove();\n document.body.style.overflow = previousOverflow;\n restoreFocus?.focus?.();\n },\n };\n\n backdrop.addEventListener(\"click\", (e) => {\n if (e.target === backdrop) handle.close();\n });\n close.addEventListener(\"click\", () => handle.close());\n document.addEventListener(\"keydown\", onKey, true);\n\n box.append(controls, img, caption);\n backdrop.appendChild(box);\n mount.appendChild(backdrop);\n document.body.style.overflow = \"hidden\";\n current = handle;\n close.focus();\n return handle;\n}\n"]}
|