@vetta-org/theme-ui 0.2.0 → 0.2.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/package.json +1 -1
- package/src/action-approval/StorageCleanListView.test.tsx +36 -0
- package/src/action-approval/StorageCleanListView.tsx +52 -0
- package/src/action-approval/index.ts +2 -0
- package/src/activity/ActivityPanel.tsx +7 -1
- package/src/activity/BackgroundTasksTabPanelView.tsx +18 -2
- package/src/activity/BrowserPanelView.tsx +15 -1
- package/src/activity/BrowserSelectionView.tsx +199 -0
- package/src/activity/index.ts +1 -0
- package/src/chat/EditDiffView.tsx +4 -4
- package/src/chat/HeaderActionBarView.tsx +16 -4
- package/src/chat/streaming-reveal.test.ts +1 -74
- package/src/chat/streaming-reveal.ts +0 -3
- package/src/file-preview/FilePreviewDialogView.tsx +2 -1
- package/src/layout/ResizeHandle.tsx +4 -1
- package/src/layout/resize-handle-reset.test.tsx +50 -0
- package/src/markdown/MarkdownContent.tsx +49 -26
- package/src/markdown/UrlLinkBadge.tsx +84 -0
- package/src/markdown/index.ts +1 -0
- package/src/markdown/nodes.ts +6 -1
- package/src/markdown/rich-labels.ts +3 -0
- package/src/markdown/streaming-fade.test.ts +83 -0
- package/src/markdown/streaming-fade.tsx +141 -0
- package/src/markdown/streaming-reveal.ts +0 -67
- package/src/markdown/streaming.ts +64 -131
- package/src/overlays/CommandMenuRow.tsx +4 -14
- package/src/overlays/CommandMenuView.tsx +11 -4
- package/src/overlays/ReleaseNoteImage.test.tsx +105 -0
- package/src/overlays/ReleaseNoteMarkdown.test.tsx +25 -0
- package/src/overlays/ReleaseNoteMarkdown.tsx +94 -7
- package/src/overlays/UpdateCheckerView.tsx +17 -15
- package/src/overlays/UpdateRestartDialogView.tsx +14 -5
- package/src/overlays/command-menu-types.ts +2 -0
- package/src/overlays/index.ts +1 -0
- package/src/plugin-ui/index.ts +2 -0
- package/src/project/DefaultConversationSectionView.tsx +4 -0
- package/src/project/ManageSessionsSplashView.tsx +0 -2
- package/src/settings/GeneralSettingsView.tsx +19 -0
- package/src/settings/LauncherSettingsView.tsx +0 -1
- package/src/settings/StorageBar.tsx +70 -0
- package/src/settings/StorageExplorer.tsx +488 -0
- package/src/settings/StorageReadout.tsx +64 -0
- package/src/settings/StorageSettingsView.test.tsx +181 -0
- package/src/settings/StorageSettingsView.tsx +458 -0
- package/src/settings/index.ts +10 -0
- package/src/settings/storage-treemap-layout.test.ts +32 -0
- package/src/settings/storage-treemap-layout.ts +55 -0
- package/src/shared/SegmentedControl.tsx +10 -11
- package/src/shared/StageDialogContent.test.tsx +61 -0
- package/src/shared/StageDialogContent.tsx +80 -0
- package/src/shared/index.ts +2 -0
- package/src/sidebar/AddProjectMenuTriggerView.tsx +4 -2
- package/src/styles.css +5 -0
- package/src/markdown/streaming.test.ts +0 -57
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, createElement, useState } from "react";
|
|
3
|
+
import { createRoot } from "react-dom/client";
|
|
4
|
+
import { expect, it } from "vitest";
|
|
5
|
+
import { ResizeHandle } from "./ResizeHandle";
|
|
6
|
+
|
|
7
|
+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
|
8
|
+
|
|
9
|
+
const DEFAULT_WIDTH = 360;
|
|
10
|
+
const USER_WIDTH = 520;
|
|
11
|
+
|
|
12
|
+
// 和宿主一样:拖动累加宽度,双击回到默认宽度;把面板当前宽度与是否在拖动渲染出来。
|
|
13
|
+
function Panel() {
|
|
14
|
+
const [width, setWidth] = useState(USER_WIDTH);
|
|
15
|
+
const [dragging, setDragging] = useState(false);
|
|
16
|
+
return createElement(
|
|
17
|
+
"section",
|
|
18
|
+
{ "data-width": width, "data-dragging": dragging },
|
|
19
|
+
createElement(ResizeHandle, {
|
|
20
|
+
side: "left",
|
|
21
|
+
onResizeStart: () => setDragging(true),
|
|
22
|
+
onResize: (delta: number) => setWidth((current) => current + delta),
|
|
23
|
+
onResizeEnd: () => setDragging(false),
|
|
24
|
+
onReset: () => setWidth(DEFAULT_WIDTH),
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 双击把手是「恢复默认尺寸」的常见约定;宿主据此把活动面板回到当前标签卡的默认宽度。
|
|
30
|
+
it("双击把手触发 onReset,不当作一次拖动", () => {
|
|
31
|
+
const host = document.createElement("div");
|
|
32
|
+
document.body.append(host);
|
|
33
|
+
const root = createRoot(host);
|
|
34
|
+
act(() => root.render(createElement(Panel)));
|
|
35
|
+
|
|
36
|
+
const panel = host.querySelector<HTMLElement>("section");
|
|
37
|
+
const handle = host.querySelector<HTMLElement>("[data-resize-handle]");
|
|
38
|
+
expect(panel?.dataset.width).toBe(String(USER_WIDTH));
|
|
39
|
+
act(() => {
|
|
40
|
+
handle?.dispatchEvent(new MouseEvent("dblclick", { bubbles: true }));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// 面板正好落在默认宽度:没有叠加任何拖动位移,也没有进入拖动态(不挂拖动遮罩、不锁选区)。
|
|
44
|
+
expect(panel?.dataset.width).toBe(String(DEFAULT_WIDTH));
|
|
45
|
+
expect(panel?.dataset.dragging).toBe("false");
|
|
46
|
+
expect(document.body.children).toHaveLength(1);
|
|
47
|
+
expect(document.body.style.userSelect).toBe("");
|
|
48
|
+
act(() => root.unmount());
|
|
49
|
+
host.remove();
|
|
50
|
+
});
|
|
@@ -23,10 +23,17 @@ import {
|
|
|
23
23
|
import { SkillTypeIcon } from "../skills/skill-icon";
|
|
24
24
|
import { InlineTokenChip } from "./InlineTokenChip";
|
|
25
25
|
import { FileLinkBadge } from "./FileLinkBadge";
|
|
26
|
+
import { type OpenUrlHandler, UrlLinkBadge } from "./UrlLinkBadge";
|
|
26
27
|
import { WebsiteIcon } from "./WebsiteIcon";
|
|
27
28
|
import { useRenderSnapshot } from "./use-render-snapshot";
|
|
28
29
|
import { chatUrlTransform, classifyMarkdownLink, normalizeLocalFileLinksInMarkdown } from "./markdown-link";
|
|
29
|
-
import { useStreamingDisplayText
|
|
30
|
+
import { useStreamingDisplayText } from "./streaming";
|
|
31
|
+
import {
|
|
32
|
+
rehypeStreamingFade,
|
|
33
|
+
STREAMING_FADE_TAG,
|
|
34
|
+
StreamingFadeSpan,
|
|
35
|
+
type StreamingReveal,
|
|
36
|
+
} from "./streaming-fade";
|
|
30
37
|
import {
|
|
31
38
|
INLINE_TOKEN_TAG,
|
|
32
39
|
rehypeInlineTokens,
|
|
@@ -63,7 +70,8 @@ export interface MarkdownContentProps {
|
|
|
63
70
|
onOpenFile: (path: string) => void;
|
|
64
71
|
/** 在系统文件管理器中定位文件;传入后文件徽标右键出现「在访达中显示」。 */
|
|
65
72
|
onRevealFile?: (path: string) => void;
|
|
66
|
-
|
|
73
|
+
/** `alternate` 为 true 时改用非默认的打开方式(右键第二项、⌘/Ctrl+点击)。 */
|
|
74
|
+
onOpenUrl: OpenUrlHandler;
|
|
67
75
|
/** 传入即启用行内 token 渲染;仅用户消息需要,助手 markdown 不受影响。 */
|
|
68
76
|
inlineTokens?: InlineTokenSupport;
|
|
69
77
|
}
|
|
@@ -78,12 +86,14 @@ function basename(path: string): string {
|
|
|
78
86
|
}
|
|
79
87
|
|
|
80
88
|
interface MarkdownDocumentProps {
|
|
81
|
-
animateChunks: boolean;
|
|
82
89
|
components: Components;
|
|
83
90
|
definition: MarkdownDefinition;
|
|
84
91
|
inlineTokens?: InlineTokenSupport;
|
|
85
92
|
live: boolean;
|
|
86
93
|
text: string;
|
|
94
|
+
/** 流式尾块里仍在淡入的放出区间,与本文档在整段显示文本中的起始下标。 */
|
|
95
|
+
reveals?: readonly StreamingReveal[];
|
|
96
|
+
revealOffset?: number;
|
|
87
97
|
}
|
|
88
98
|
|
|
89
99
|
/**
|
|
@@ -91,21 +101,24 @@ interface MarkdownDocumentProps {
|
|
|
91
101
|
* 已提交块不会跟着整篇重跑 remark;流式结束后分块结构保持不变,已提交块不会重挂。
|
|
92
102
|
*/
|
|
93
103
|
const MarkdownDocument = memo(function MarkdownDocument({
|
|
94
|
-
animateChunks,
|
|
95
104
|
components,
|
|
96
105
|
definition,
|
|
97
106
|
inlineTokens,
|
|
98
107
|
live,
|
|
99
108
|
text,
|
|
109
|
+
reveals,
|
|
110
|
+
revealOffset = 0,
|
|
100
111
|
}: MarkdownDocumentProps): JSX.Element {
|
|
101
112
|
const hasStructuredAnnotations = inlineTokens?.annotations !== undefined;
|
|
113
|
+
const markdownSource = useMemo(() => normalizeLocalFileLinksInMarkdown(text), [text]);
|
|
114
|
+
// 改写过本地文件链接的源码与显示文本坐标对不上,这时不淡入,免得包错位置。
|
|
115
|
+
const fadeReveals = reveals && reveals.length > 0 && markdownSource === text ? reveals : undefined;
|
|
102
116
|
const rehypePlugins = useMemo(() => {
|
|
103
117
|
const plugins: NonNullable<Options["rehypePlugins"]> = [...(definition.rehypePlugins ?? [])];
|
|
104
|
-
if (
|
|
118
|
+
if (fadeReveals) plugins.push(rehypeStreamingFade(fadeReveals, revealOffset));
|
|
105
119
|
if (inlineTokens && !hasStructuredAnnotations) plugins.push(() => rehypeInlineTokens(inlineTokens.parse));
|
|
106
120
|
return plugins.length > 0 ? plugins : undefined;
|
|
107
|
-
}, [
|
|
108
|
-
const markdownSource = useMemo(() => normalizeLocalFileLinksInMarkdown(text), [text]);
|
|
121
|
+
}, [fadeReveals, revealOffset, hasStructuredAnnotations, inlineTokens, definition]);
|
|
109
122
|
const normalizedAnnotations = useMemo(
|
|
110
123
|
() =>
|
|
111
124
|
inlineTokens?.annotations
|
|
@@ -146,8 +159,8 @@ const MarkdownDocument = memo(function MarkdownDocument({
|
|
|
146
159
|
* Memo'd markdown renderer for chat text blocks. Host injects file/url handlers and theme.
|
|
147
160
|
*
|
|
148
161
|
* `components` 映射的函数引用必须在 streaming 期间保持稳定:React 把 components.p 等
|
|
149
|
-
* 当成元素类型;引用一变就会整树 remount
|
|
150
|
-
*
|
|
162
|
+
* 当成元素类型;引用一变就会整树 remount,已有文本整段重挂,表现为 text block 高频闪烁。
|
|
163
|
+
* labels / 回调通过 ref 读取,不进 deps。
|
|
151
164
|
*/
|
|
152
165
|
export const MarkdownContent = memo(function MarkdownContent({
|
|
153
166
|
text,
|
|
@@ -167,9 +180,7 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
167
180
|
const definition = definitionOverride ?? inheritedDefinition;
|
|
168
181
|
const definitionRef = useRef(definition);
|
|
169
182
|
definitionRef.current = definition;
|
|
170
|
-
const { displayText,
|
|
171
|
-
const chunkedRef = useRef(false);
|
|
172
|
-
if (animateChunks) chunkedRef.current = true;
|
|
183
|
+
const { text: displayText, reveals } = useStreamingDisplayText(text, isStreamingTail);
|
|
173
184
|
const frozenBlocksRef = useRef(false);
|
|
174
185
|
if (isStreamingTail && !inlineTokens) frozenBlocksRef.current = true;
|
|
175
186
|
const splitDocuments = frozenBlocksRef.current && !inlineTokens;
|
|
@@ -273,21 +284,34 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
273
284
|
}
|
|
274
285
|
if (kind.type === "url") {
|
|
275
286
|
return (
|
|
276
|
-
<
|
|
277
|
-
|
|
278
|
-
title={kind.url}
|
|
287
|
+
<UrlLinkBadge
|
|
288
|
+
url={kind.url}
|
|
279
289
|
className={LINK_BADGE_CLASS}
|
|
290
|
+
menuLabels={labelsRef.current.urlLink}
|
|
291
|
+
onOpen={(...args) => onOpenUrlRef.current(...args)}
|
|
292
|
+
>
|
|
293
|
+
<WebsiteIcon href={kind.url} />
|
|
294
|
+
<span className="truncate">{children}</span>
|
|
295
|
+
</UrlLinkBadge>
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
// 邮件地址交给宿主(系统邮件客户端),不进内置浏览器。
|
|
299
|
+
if (/^mailto:/i.test(kind.href)) {
|
|
300
|
+
return (
|
|
301
|
+
<a
|
|
302
|
+
href={kind.href}
|
|
303
|
+
title={kind.href}
|
|
304
|
+
className="text-chart-2 underline decoration-chart-2/30"
|
|
280
305
|
onClick={(e) => {
|
|
281
306
|
e.preventDefault();
|
|
282
|
-
onOpenUrlRef.current(kind.
|
|
307
|
+
onOpenUrlRef.current(kind.href);
|
|
283
308
|
}}
|
|
284
309
|
>
|
|
285
|
-
|
|
286
|
-
<span className="truncate">{children}</span>
|
|
310
|
+
{children}
|
|
287
311
|
</a>
|
|
288
312
|
);
|
|
289
313
|
}
|
|
290
|
-
//
|
|
314
|
+
// fragment / unknown: never target=_blank — that opens the OS browser
|
|
291
315
|
// for misclassified local paths. Keep visible but non-navigating.
|
|
292
316
|
return (
|
|
293
317
|
<span className="text-chart-2 underline decoration-chart-2/30" title={kind.href || undefined}>
|
|
@@ -296,6 +320,7 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
296
320
|
);
|
|
297
321
|
},
|
|
298
322
|
strong: ({ children }) => <strong className="font-semibold text-foreground">{children}</strong>,
|
|
323
|
+
[STREAMING_FADE_TAG]: StreamingFadeSpan,
|
|
299
324
|
"vetta-svg": ({ node }: { node?: HastElement }) => {
|
|
300
325
|
const source = node?.properties?.source;
|
|
301
326
|
const rich = labelsRef.current.rich ?? defaultRichContentLabels;
|
|
@@ -377,10 +402,7 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
377
402
|
[theme],
|
|
378
403
|
);
|
|
379
404
|
|
|
380
|
-
//
|
|
381
|
-
// 表现为回复结尾「卡一下」。「最新短语略暗」只挂在包裹类上,撤掉包裹类就恢复全亮,DOM 不动。
|
|
382
|
-
// 切块一旦启用就保持到实例卸载:流式结束时 `animateChunks` 要等 settle 才关,若此刻把
|
|
383
|
-
// 已冻结块并回单一文档,已上屏的节点会整段重挂并再包成 `.streaming-chunk` 重放淡入。
|
|
405
|
+
// 切块一旦启用就保持到实例卸载:流式结束时若把已冻结块并回单一文档,已上屏的节点会整段重挂。
|
|
384
406
|
// 稳定块只按已闭合的顶层围栏切分,分块与整篇渲染结果一致,因此结束后不需要再合并。
|
|
385
407
|
const split = useMemo(
|
|
386
408
|
() => renderText === displayText ? candidateSplit : splitDocuments ? splitStableMarkdownBlocks(renderText) : null,
|
|
@@ -391,11 +413,10 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
391
413
|
const showTail = !split || tail.length > 0 || committed.length === 0;
|
|
392
414
|
|
|
393
415
|
return (
|
|
394
|
-
<MarkdownHostProvider host={host}><div className={cn("markdown-body break-words",
|
|
416
|
+
<MarkdownHostProvider host={host}><div className={cn("markdown-body break-words", className)}>
|
|
395
417
|
{committed.map((block, index) => (
|
|
396
418
|
<MarkdownDocument
|
|
397
419
|
key={`committed-${index}`}
|
|
398
|
-
animateChunks={false}
|
|
399
420
|
components={components}
|
|
400
421
|
definition={definition}
|
|
401
422
|
live={false}
|
|
@@ -405,12 +426,14 @@ export const MarkdownContent = memo(function MarkdownContent({
|
|
|
405
426
|
{showTail ? (
|
|
406
427
|
<MarkdownDocument
|
|
407
428
|
key="tail"
|
|
408
|
-
animateChunks={chunkedRef.current}
|
|
409
429
|
components={components}
|
|
410
430
|
definition={definition}
|
|
411
431
|
inlineTokens={inlineTokens}
|
|
412
432
|
live={isStreamingTail}
|
|
413
433
|
text={tail}
|
|
434
|
+
// 长尾块降频解析时不淡入:放出记录每次都变,会绕过降频逐次重解析。
|
|
435
|
+
reveals={renderInterval === 0 ? reveals : undefined}
|
|
436
|
+
revealOffset={renderText.length - tail.length}
|
|
414
437
|
/>
|
|
415
438
|
) : null}
|
|
416
439
|
</div></MarkdownHostProvider>
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { JSX, MouseEvent, ReactNode } from "react";
|
|
3
|
+
import { ContextMenuView } from "../shared/ContextMenuView";
|
|
4
|
+
import type { ContextMenuNode } from "../shared/ContextMenuView";
|
|
5
|
+
|
|
6
|
+
export interface UrlLinkMenuLabels {
|
|
7
|
+
/** 按用户设置的默认方式打开,如「在 Vetta 中打开」。 */
|
|
8
|
+
open: string;
|
|
9
|
+
/** 另一种方式,如「在系统浏览器中打开」。 */
|
|
10
|
+
openAlternate: string;
|
|
11
|
+
copy: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** `alternate` 为 true 表示不走默认方式,改用另一种(右键第二项或 ⌘/Ctrl+点击)。 */
|
|
15
|
+
export type OpenUrlHandler = (url: string, options?: { alternate?: boolean }) => void;
|
|
16
|
+
|
|
17
|
+
export interface UrlLinkBadgeProps {
|
|
18
|
+
url: string;
|
|
19
|
+
className: string;
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
menuLabels?: UrlLinkMenuLabels;
|
|
22
|
+
onOpen: OpenUrlHandler;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 消息里的网址徽标:左键按默认方式打开,⌘/Ctrl+点击换另一种,右键可选打开方式或复制链接。 */
|
|
26
|
+
export function UrlLinkBadge({ url, className, children, menuLabels, onOpen }: UrlLinkBadgeProps): JSX.Element {
|
|
27
|
+
const [menu, setMenu] = useState<{ x: number; y: number } | null>(null);
|
|
28
|
+
|
|
29
|
+
const handleContextMenu = (event: MouseEvent<HTMLAnchorElement>): void => {
|
|
30
|
+
if (!menuLabels) return;
|
|
31
|
+
// 有选中文字时让会话的「复制 / 加入输入框」菜单接手。
|
|
32
|
+
const selection = window.getSelection();
|
|
33
|
+
if (selection && !selection.isCollapsed) return;
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
event.stopPropagation();
|
|
36
|
+
setMenu({ x: event.clientX, y: event.clientY });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const items: ContextMenuNode[] = menuLabels
|
|
40
|
+
? [
|
|
41
|
+
{
|
|
42
|
+
kind: "item",
|
|
43
|
+
id: "open",
|
|
44
|
+
label: menuLabels.open,
|
|
45
|
+
iconClassName: "icon-[solar--global-linear]",
|
|
46
|
+
onSelect: () => onOpen(url),
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
kind: "item",
|
|
50
|
+
id: "open-alternate",
|
|
51
|
+
label: menuLabels.openAlternate,
|
|
52
|
+
iconClassName: "icon-[solar--square-top-down-linear]",
|
|
53
|
+
onSelect: () => onOpen(url, { alternate: true }),
|
|
54
|
+
},
|
|
55
|
+
{ kind: "separator", id: "separator" },
|
|
56
|
+
{
|
|
57
|
+
kind: "item",
|
|
58
|
+
id: "copy",
|
|
59
|
+
label: menuLabels.copy,
|
|
60
|
+
iconClassName: "icon-[solar--copy-linear]",
|
|
61
|
+
onSelect: () => void navigator.clipboard?.writeText(url).catch(() => undefined),
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
: [];
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
<a
|
|
69
|
+
href={url}
|
|
70
|
+
title={url}
|
|
71
|
+
className={className}
|
|
72
|
+
onClick={(event) => {
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
if (event.metaKey || event.ctrlKey) onOpen(url, { alternate: true });
|
|
75
|
+
else onOpen(url);
|
|
76
|
+
}}
|
|
77
|
+
onContextMenu={handleContextMenu}
|
|
78
|
+
>
|
|
79
|
+
{children}
|
|
80
|
+
</a>
|
|
81
|
+
{menu ? <ContextMenuView items={items} x={menu.x} y={menu.y} onClose={() => setMenu(null)} /> : null}
|
|
82
|
+
</>
|
|
83
|
+
);
|
|
84
|
+
}
|
package/src/markdown/index.ts
CHANGED
|
@@ -10,3 +10,4 @@ export { MarkdownContent } from "./MarkdownContent";
|
|
|
10
10
|
export type { RichContentLabels } from "./rich-labels";
|
|
11
11
|
export type { StableMarkdownSplit } from "./stable-blocks";
|
|
12
12
|
export { splitStableMarkdownBlocks } from "./stable-blocks";
|
|
13
|
+
export type { OpenUrlHandler, UrlLinkMenuLabels } from "./UrlLinkBadge";
|
package/src/markdown/nodes.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
/** Minimal hast-like nodes for the
|
|
1
|
+
/** Minimal hast-like nodes for the markdown rehype plugins. */
|
|
2
|
+
export interface HastPosition {
|
|
3
|
+
start: { offset?: number };
|
|
4
|
+
end: { offset?: number };
|
|
5
|
+
}
|
|
2
6
|
export interface HastText {
|
|
3
7
|
type: "text";
|
|
4
8
|
value: string;
|
|
9
|
+
position?: HastPosition;
|
|
5
10
|
}
|
|
6
11
|
export interface HastElement {
|
|
7
12
|
type: "element";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { FileLinkMenuLabels } from "./FileLinkBadge";
|
|
2
|
+
import type { UrlLinkMenuLabels } from "./UrlLinkBadge";
|
|
2
3
|
|
|
3
4
|
export interface RichContentLabels {
|
|
4
5
|
preview: string;
|
|
@@ -51,5 +52,7 @@ export interface MarkdownLabels {
|
|
|
51
52
|
copied: string;
|
|
52
53
|
/** 文件徽标右键菜单文案;缺省时不出菜单。 */
|
|
53
54
|
fileLink?: FileLinkMenuLabels;
|
|
55
|
+
/** 网址徽标右键菜单文案;缺省时不出菜单。 */
|
|
56
|
+
urlLink?: UrlLinkMenuLabels;
|
|
54
57
|
rich?: RichContentLabels;
|
|
55
58
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import type { HastElement, HastRoot, HastText } from "./nodes";
|
|
3
|
+
import { pruneReveals, rehypeStreamingFade, STREAMING_FADE_MS, STREAMING_FADE_TAG } from "./streaming-fade";
|
|
4
|
+
|
|
5
|
+
function text(value: string, start: number, end = start + value.length): HastText {
|
|
6
|
+
return { type: "text", value, position: { start: { offset: start }, end: { offset: end } } };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function element(tagName: string, ...children: Array<HastText | HastElement>): HastElement {
|
|
10
|
+
return { type: "element", tagName, properties: {}, children };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 把树压成「普通文字 / [淡入时刻:文字]」的串,便于断言。 */
|
|
14
|
+
function flatten(node: HastRoot | HastElement): string {
|
|
15
|
+
return node.children
|
|
16
|
+
.map((child) => {
|
|
17
|
+
if (child.type === "text") return child.value;
|
|
18
|
+
if (child.tagName === STREAMING_FADE_TAG) return `[${child.properties?.dataRevealAt}:${flatten(child)}]`;
|
|
19
|
+
return flatten(child);
|
|
20
|
+
})
|
|
21
|
+
.join("");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function run(tree: HastRoot, reveals: Parameters<typeof rehypeStreamingFade>[0], offset = 0): string {
|
|
25
|
+
rehypeStreamingFade(reveals, offset)()(tree);
|
|
26
|
+
return flatten(tree);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("rehypeStreamingFade", () => {
|
|
30
|
+
test("wraps only the text revealed within the fade window, one span per reveal", () => {
|
|
31
|
+
const tree: HastRoot = { type: "root", children: [element("p", text("Hello there, general Kenobi.", 0))] };
|
|
32
|
+
const out = run(tree, [
|
|
33
|
+
{ start: 6, end: 13, at: 100 },
|
|
34
|
+
{ start: 13, end: 28, at: 200 },
|
|
35
|
+
]);
|
|
36
|
+
expect(out).toBe("Hello [100:there, ][200:general Kenobi.]");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("maps reveals across inline formatting by source position", () => {
|
|
40
|
+
// 源码 "Say **bold** now":strong 内文字源码位置是 6..10。
|
|
41
|
+
const tree: HastRoot = {
|
|
42
|
+
type: "root",
|
|
43
|
+
children: [element("p", text("Say ", 0), element("strong", text("bold", 6)), text(" now", 12))],
|
|
44
|
+
};
|
|
45
|
+
expect(run(tree, [{ start: 8, end: 16, at: 5 }])).toBe("Say bo[5:ld][5: now]");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("leaves code, tables and svg previews untouched", () => {
|
|
49
|
+
const tree: HastRoot = {
|
|
50
|
+
type: "root",
|
|
51
|
+
children: [element("pre", element("code", text("let a = 1;", 4))), element("p", text("after", 16))],
|
|
52
|
+
};
|
|
53
|
+
expect(run(tree, [{ start: 0, end: 21, at: 7 }])).toBe("let a = 1;[7:after]");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("shifts reveal ranges by the document offset of the streaming tail", () => {
|
|
57
|
+
const tree: HastRoot = { type: "root", children: [element("p", text("tail text", 0))] };
|
|
58
|
+
expect(run(tree, [{ start: 105, end: 109, at: 9 }], 100)).toBe("tail [9:text]");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("assigns a node whose source length differs from its text as a whole by where it ends", () => {
|
|
62
|
+
// "a & b" 渲染成 "a & b":源码 9 个字符,文字 5 个。
|
|
63
|
+
const tree: HastRoot = { type: "root", children: [element("p", text("a & b", 0, 9))] };
|
|
64
|
+
expect(run(tree, [{ start: 4, end: 9, at: 3 }])).toBe("[3:a & b]");
|
|
65
|
+
const older: HastRoot = { type: "root", children: [element("p", text("a & b", 0, 9))] };
|
|
66
|
+
expect(run(older, [{ start: 9, end: 12, at: 3 }])).toBe("a & b");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("does nothing without reveals", () => {
|
|
70
|
+
const tree: HastRoot = { type: "root", children: [element("p", text("steady", 0))] };
|
|
71
|
+
expect(run(tree, [])).toBe("steady");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("pruneReveals", () => {
|
|
76
|
+
test("drops reveals whose fade has finished", () => {
|
|
77
|
+
const reveals = [
|
|
78
|
+
{ start: 0, end: 3, at: 0 },
|
|
79
|
+
{ start: 3, end: 6, at: 100 },
|
|
80
|
+
];
|
|
81
|
+
expect(pruneReveals(reveals, STREAMING_FADE_MS + 50)).toEqual([{ start: 3, end: 6, at: 100 }]);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { JSX, ReactNode } from "react";
|
|
2
|
+
import { useLayoutEffect, useRef } from "react";
|
|
3
|
+
import type { HastElement, HastRoot, HastText } from "./nodes";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 流式新文字的淡入。
|
|
7
|
+
*
|
|
8
|
+
* 放字只有约 10Hz:文字一次性全亮上屏像「闪出来」,按放字节奏分级变亮又像掉帧。这里给每次新放出的
|
|
9
|
+
* 那段文字挂一段平滑的 opacity 淡入,由合成器按屏幕刷新率插值。实测(M4,毛玻璃开关均测):平滑
|
|
10
|
+
* 淡入与 30fps 分格淡入的 WindowServer 开销几乎相同,只比直接上屏多约 7 个百分点,且只在有字流出时发生。
|
|
11
|
+
*
|
|
12
|
+
* 动画起点锁在「放出那一刻」的绝对时间上:尾块每次重渲染都可能重建这些 span,新建的动画会接着原进度
|
|
13
|
+
* 播,而不是从 0 重播(那会让文字反复闪)。淡入结束后的放出记录会被丢弃,包裹随之撤掉。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** 单段新文字的淡入时长。 */
|
|
17
|
+
export const STREAMING_FADE_MS = 220;
|
|
18
|
+
const FADE_EASING = "cubic-bezier(0.2, 0, 0, 1)";
|
|
19
|
+
export const STREAMING_FADE_TAG = "vetta-reveal";
|
|
20
|
+
|
|
21
|
+
/** 一次放出:显示文本从 `start` 长到 `end`(UTF-16 下标),发生在 `at`(performance.now 时间基)。 */
|
|
22
|
+
export interface StreamingReveal {
|
|
23
|
+
readonly start: number;
|
|
24
|
+
readonly end: number;
|
|
25
|
+
readonly at: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 丢掉已经淡入完的放出记录。 */
|
|
29
|
+
export function pruneReveals(reveals: readonly StreamingReveal[], now: number): StreamingReveal[] {
|
|
30
|
+
return reveals.filter((reveal) => now - reveal.at < STREAMING_FADE_MS);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isLiteralTag(tag: string): boolean {
|
|
34
|
+
// 代码、表格、SVG 预览由各自组件按原文渲染,不能拆开包裹。
|
|
35
|
+
return tag === "code" || tag === "pre" || tag === "table" || tag === "vetta-svg";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 把仍在淡入期的放出区间包成 `<vetta-reveal data-reveal-at>`。区间是文档源码坐标,按 hast 文本节点的
|
|
40
|
+
* position 对齐;节点源码长度与文字长度不一致(转义、实体)时整段按其末尾归属,不做逐字映射。
|
|
41
|
+
* `offset` 是本文档在整段显示文本中的起始下标(尾块前面还有已冻结的块)。
|
|
42
|
+
*/
|
|
43
|
+
export function rehypeStreamingFade(reveals: readonly StreamingReveal[], offset: number) {
|
|
44
|
+
return () =>
|
|
45
|
+
(tree: HastRoot): void => {
|
|
46
|
+
if (reveals.length === 0) return;
|
|
47
|
+
const local = reveals
|
|
48
|
+
.map((reveal) => ({ start: reveal.start - offset, end: reveal.end - offset, at: reveal.at }))
|
|
49
|
+
.filter((reveal) => reveal.end > 0);
|
|
50
|
+
if (local.length === 0) return;
|
|
51
|
+
const firstStart = Math.max(0, Math.min(...local.map((reveal) => reveal.start)));
|
|
52
|
+
|
|
53
|
+
const split = (node: HastText): Array<HastText | HastElement> | null => {
|
|
54
|
+
const start = node.position?.start.offset;
|
|
55
|
+
const end = node.position?.end.offset;
|
|
56
|
+
if (start === undefined || end === undefined || end <= firstStart) return null;
|
|
57
|
+
const value = node.value;
|
|
58
|
+
const exact = value.length === end - start;
|
|
59
|
+
const pieces: Array<HastText | HastElement> = [];
|
|
60
|
+
let cursor = 0;
|
|
61
|
+
for (const reveal of local) {
|
|
62
|
+
let from: number;
|
|
63
|
+
let to: number;
|
|
64
|
+
if (exact) {
|
|
65
|
+
from = Math.max(0, reveal.start - start);
|
|
66
|
+
to = Math.min(value.length, reveal.end - start);
|
|
67
|
+
} else {
|
|
68
|
+
const owns = end > reveal.start && end <= reveal.end;
|
|
69
|
+
from = 0;
|
|
70
|
+
to = owns ? value.length : 0;
|
|
71
|
+
}
|
|
72
|
+
const pieceStart = Math.max(cursor, from);
|
|
73
|
+
if (to <= pieceStart) continue;
|
|
74
|
+
if (pieceStart > cursor) pieces.push({ type: "text", value: value.slice(cursor, pieceStart) } as HastText);
|
|
75
|
+
pieces.push({
|
|
76
|
+
type: "element",
|
|
77
|
+
tagName: STREAMING_FADE_TAG,
|
|
78
|
+
properties: { dataRevealAt: String(reveal.at) },
|
|
79
|
+
children: [{ type: "text", value: value.slice(pieceStart, to) } as HastText],
|
|
80
|
+
} as HastElement);
|
|
81
|
+
cursor = to;
|
|
82
|
+
}
|
|
83
|
+
if (pieces.length === 0) return null;
|
|
84
|
+
if (cursor < value.length) pieces.push({ type: "text", value: value.slice(cursor) } as HastText);
|
|
85
|
+
return pieces;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const visit = (node: HastRoot | HastElement): void => {
|
|
89
|
+
const next: Array<(typeof node.children)[number]> = [];
|
|
90
|
+
let changed = false;
|
|
91
|
+
for (const child of node.children) {
|
|
92
|
+
if (child.type === "text") {
|
|
93
|
+
const pieces = split(child as HastText);
|
|
94
|
+
if (pieces) {
|
|
95
|
+
next.push(...(pieces as typeof next));
|
|
96
|
+
changed = true;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
} else if (child.type === "element" && !isLiteralTag(child.tagName)) {
|
|
100
|
+
visit(child);
|
|
101
|
+
}
|
|
102
|
+
next.push(child);
|
|
103
|
+
}
|
|
104
|
+
if (changed) node.children = next as typeof node.children;
|
|
105
|
+
};
|
|
106
|
+
visit(tree);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function prefersReducedMotion(): boolean {
|
|
111
|
+
return typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 正在淡入的一段文字。起点锁在放出时刻,重建也不会重播。 */
|
|
115
|
+
export function StreamingFadeSpan({
|
|
116
|
+
children,
|
|
117
|
+
"data-reveal-at": revealAt,
|
|
118
|
+
}: {
|
|
119
|
+
children?: ReactNode;
|
|
120
|
+
"data-reveal-at"?: string;
|
|
121
|
+
}): JSX.Element {
|
|
122
|
+
const ref = useRef<HTMLSpanElement>(null);
|
|
123
|
+
useLayoutEffect(() => {
|
|
124
|
+
const element = ref.current;
|
|
125
|
+
const at = Number(revealAt);
|
|
126
|
+
if (!element || !Number.isFinite(at) || typeof element.animate !== "function") return;
|
|
127
|
+
if (performance.now() - at >= STREAMING_FADE_MS || prefersReducedMotion()) return;
|
|
128
|
+
const animation = element.animate([{ opacity: 0 }, { opacity: 1 }], {
|
|
129
|
+
duration: STREAMING_FADE_MS,
|
|
130
|
+
easing: FADE_EASING,
|
|
131
|
+
fill: "backwards",
|
|
132
|
+
});
|
|
133
|
+
animation.startTime = at;
|
|
134
|
+
return () => animation.cancel();
|
|
135
|
+
}, [revealAt]);
|
|
136
|
+
return (
|
|
137
|
+
<span ref={ref} className="streaming-fade">
|
|
138
|
+
{children}
|
|
139
|
+
</span>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* - 未闭合的行内语法(`[链接](…)`、行内代码、加粗)先扣着不放,闭合后整体出现,
|
|
9
9
|
* 避免先看到原始标记再翻转成徽标;扣太久或太长就当普通文字放出。
|
|
10
10
|
* - 尾部未写完的词先不显示;停顿太久就不再等。
|
|
11
|
-
* - 分段给 rehype 用的短语切分(`splitStreamingSegments`)保持按标点切,供「最新短语略暗」使用。
|
|
12
11
|
*/
|
|
13
12
|
|
|
14
13
|
/** 放出 tick 的兜底间隔,与宿主增量刷新同周期(100ms)。 */
|
|
@@ -26,80 +25,14 @@ const MIN_RATE_PER_MS = 0.02;
|
|
|
26
25
|
/** 未闭合行内语法最多扣这么久 / 这么长,之后当普通文字放出。 */
|
|
27
26
|
export const HOLD_MAX_MS = 800;
|
|
28
27
|
const HOLD_MAX_CHARS = 240;
|
|
29
|
-
/** 无标点长片段的切分上限(UTF-16 code units),供 rehype 分段。 */
|
|
30
|
-
const MAX_PHRASE_LENGTH = 48;
|
|
31
|
-
/** 按上限切分时,优先在这个长度之后的最后一个空白处断开。 */
|
|
32
|
-
const MIN_SOFT_BREAK_LENGTH = 16;
|
|
33
28
|
|
|
34
|
-
/** 最后一个片放出后,等这么久再撤掉「最新短语略暗」的包裹类。 */
|
|
35
|
-
export const STREAMING_SETTLE_MS = 150;
|
|
36
|
-
/**
|
|
37
|
-
* 流还没结束、但已全部放出且这么久没有新文本时,也撤掉暗色:正文写完到回合结束事件之间常有
|
|
38
|
-
* 近一秒的空档,不能让末尾短语一直暗着。新文本到达时会重新挂上包裹类。
|
|
39
|
-
*/
|
|
40
|
-
export const STREAMING_IDLE_SETTLE_MS = 300;
|
|
41
29
|
/** 尾部未完成的词超过这么久没有新内容,就不再等,直接放出,避免模型停顿时文字「卡住」。 */
|
|
42
30
|
export const STREAMING_STALL_FLUSH_MS = 800;
|
|
43
31
|
|
|
44
|
-
const CJK_BREAK = new Set([",", "。", ";", ":", "!", "?", "、", "…"]);
|
|
45
|
-
const CJK_TRAILING = new Set([",", "。", ";", ":", "!", "?", "、", "…", "”", "’", ")", "」", "』", "》", "】"]);
|
|
46
|
-
const LATIN_BREAK = new Set([",", ".", ";", ":", "!", "?"]);
|
|
47
|
-
const WHITESPACE = /\s/;
|
|
48
|
-
|
|
49
32
|
function isHighSurrogate(code: number): boolean {
|
|
50
33
|
return code >= 0xd800 && code <= 0xdbff;
|
|
51
34
|
}
|
|
52
35
|
|
|
53
|
-
/**
|
|
54
|
-
* 从 `from` 开始的下一个短语的结束位置;尾部还没写完时返回 null。
|
|
55
|
-
* `final` 表示文本不会再增长,此时末尾剩余内容也算一个完整短语。
|
|
56
|
-
*/
|
|
57
|
-
export function nextPhraseEnd(text: string, from: number, final: boolean): number | null {
|
|
58
|
-
const length = text.length;
|
|
59
|
-
if (from >= length) return null;
|
|
60
|
-
|
|
61
|
-
for (let index = from; index < length; index++) {
|
|
62
|
-
const char = text[index] as string;
|
|
63
|
-
if (char === "\n") return index + 1;
|
|
64
|
-
|
|
65
|
-
if (CJK_BREAK.has(char)) {
|
|
66
|
-
let end = index + 1;
|
|
67
|
-
while (end < length && CJK_TRAILING.has(text[end] as string)) end++;
|
|
68
|
-
return end;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 英文标点只有后面跟着空白才算断句:`3.14`、`e.g.x`、URL 里的点都不断开;
|
|
72
|
-
// 标点正好在末尾时还不知道后面是什么,先当作未完成。
|
|
73
|
-
if (LATIN_BREAK.has(char)) {
|
|
74
|
-
if (index + 1 < length) {
|
|
75
|
-
if (WHITESPACE.test(text[index + 1] as string)) return index + 1;
|
|
76
|
-
} else if (!final) {
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (index + 1 - from >= MAX_PHRASE_LENGTH) {
|
|
82
|
-
for (let back = index; back >= from + MIN_SOFT_BREAK_LENGTH; back--) {
|
|
83
|
-
if (WHITESPACE.test(text[back] as string)) return back;
|
|
84
|
-
}
|
|
85
|
-
return isHighSurrogate(text.charCodeAt(index)) ? index + 2 : index + 1;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return final ? length : null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** 把一段文本切成可逐个淡入的短语;拼接结果恒等于输入,文本末尾总是结束最后一个短语。 */
|
|
93
|
-
export function splitStreamingSegments(value: string): string[] {
|
|
94
|
-
const segments: string[] = [];
|
|
95
|
-
for (let start = 0; start < value.length; ) {
|
|
96
|
-
const end = nextPhraseEnd(value, start, true) ?? value.length;
|
|
97
|
-
segments.push(value.slice(start, end));
|
|
98
|
-
start = end;
|
|
99
|
-
}
|
|
100
|
-
return segments;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
36
|
function isWordChar(char: string | undefined): boolean {
|
|
104
37
|
return char !== undefined && /[\p{L}\p{N}_]/u.test(char) && !isCjk(char);
|
|
105
38
|
}
|