@simon_he/vue-tui 0.0.7 → 1.0.0-rc.0
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/README.md +222 -444
- package/dist/TSelect-BmSkar0D.js +8697 -0
- package/dist/ansi-palette-Bv5Vqr4k.js +1499 -0
- package/dist/ansi-styles.d.ts +200 -0
- package/dist/cli/headless-renderer.d.ts +29 -0
- package/dist/cli/input.d.ts +49 -0
- package/dist/cli/node-file-writers.d.ts +12 -0
- package/dist/cli/node-module.d.ts +2 -0
- package/dist/cli/parse-kitty.d.ts +2 -0
- package/dist/cli/parse-mouse.d.ts +2 -0
- package/dist/cli/parse-utils.d.ts +14 -0
- package/dist/cli/parse-xterm.d.ts +2 -0
- package/dist/cli/path-provider-types.d.ts +1 -0
- package/dist/cli/path-provider.d.ts +4 -0
- package/dist/cli/path-suggest-core.d.ts +1 -0
- package/dist/cli/path-suggest.d.ts +15 -0
- package/dist/cli/recording.d.ts +4 -0
- package/dist/cli/stdin-buffer.d.ts +33 -0
- package/dist/cli.cjs +10664 -0
- package/dist/cli.d.cts +22 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +9521 -0
- package/dist/core/ansi/capability.d.ts +20 -0
- package/dist/core/ansi/colors.d.ts +41 -0
- package/dist/core/ansi/sgr.d.ts +7 -0
- package/dist/core/ansi/theme.d.ts +6 -0
- package/dist/core/ansi-palette.d.ts +14 -0
- package/dist/core/buffer/buffer.d.ts +54 -0
- package/dist/core/buffer/width.d.ts +4 -0
- package/dist/core/debug-logger.d.ts +18 -0
- package/dist/core/hyperlink.d.ts +9 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/path-provider-types.d.ts +24 -0
- package/dist/core/path-suggest.d.ts +35 -0
- package/dist/core/render-plane.d.ts +3 -0
- package/dist/core/terminal/create-terminal.d.ts +7 -0
- package/dist/core/terminal/emitter.d.ts +7 -0
- package/dist/core/types.d.ts +107 -0
- package/dist/core.cjs +3072 -0
- package/dist/core.d.cts +14 -0
- package/dist/core.d.ts +14 -0
- package/dist/core.js +422 -0
- package/dist/create-terminal-app.d.ts +34 -0
- package/dist/dom-renderer-CDVQIcIL.js +1172 -0
- package/dist/events/index.d.ts +6 -0
- package/dist/events/manager/cli-event-manager.d.ts +22 -0
- package/dist/events/manager/event-manager.d.ts +30 -0
- package/dist/events/manager/selection-suppression.d.ts +3 -0
- package/dist/events/manager/types.d.ts +108 -0
- package/dist/events/recording.d.ts +44 -0
- package/dist/experimental.cjs +9380 -0
- package/dist/experimental.d.cts +39 -0
- package/dist/experimental.d.ts +39 -0
- package/dist/experimental.js +7624 -0
- package/dist/frame-perf-C-w8mjJL.js +98 -0
- package/dist/hyperlink-B5PD7a3Q.js +331 -0
- package/dist/index.cjs +12037 -16507
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -1353
- package/dist/index.js +13 -18738
- package/dist/markdown.cjs +21929 -0
- package/dist/markdown.d.cts +10 -0
- package/dist/markdown.d.ts +10 -0
- package/dist/markdown.js +1649 -0
- package/dist/observability/cli-latency-node.d.ts +3 -0
- package/dist/observability/cli-latency-types.d.ts +41 -0
- package/dist/observability/cli-latency.d.ts +3 -0
- package/dist/observability/frame-perf-store.d.ts +13 -0
- package/dist/observability/frame-perf.d.ts +40 -0
- package/dist/observability/trace.d.ts +40 -0
- package/dist/observability/tui-profiler.d.ts +33 -0
- package/dist/observability.cjs +99 -0
- package/dist/observability.d.cts +6 -0
- package/dist/observability.d.ts +6 -0
- package/dist/observability.js +3 -0
- package/dist/path-DDiSCXrQ.js +99 -0
- package/dist/path-suggest-0Q2BDsuj.js +274 -0
- package/dist/path-suggest-Dk9TRLh5.js +3 -0
- package/dist/{path-suggest-CZQYJIIg.js → path-suggest-bvR9SY6Q.js} +86 -18
- package/dist/renderer/capabilities.d.ts +13 -0
- package/dist/renderer/cli/index.d.ts +2 -0
- package/dist/renderer/cli/stdout-metrics.d.ts +18 -0
- package/dist/renderer/cli/stdout-renderer.d.ts +48 -0
- package/dist/renderer/dom/dom-renderer.d.ts +157 -0
- package/dist/renderer/dom/index.d.ts +2 -0
- package/dist/renderer/index.d.ts +2 -0
- package/dist/renderer-dom.cjs +1511 -0
- package/dist/renderer-dom.d.cts +4 -0
- package/dist/renderer-dom.d.ts +4 -0
- package/dist/renderer-dom.js +6 -0
- package/dist/runtime/index.d.ts +36 -0
- package/dist/runtime/osc52.d.ts +9 -0
- package/dist/runtime-DtrcQidD.js +1197 -0
- package/dist/runtime.cjs +1697 -0
- package/dist/runtime.d.cts +9 -0
- package/dist/runtime.d.ts +9 -0
- package/dist/runtime.js +4 -0
- package/dist/selection/terminal-selection.d.ts +122 -0
- package/dist/sgr-DMK2AvuT.js +304 -0
- package/dist/style-cache-i0ZABZRq.js +178 -0
- package/dist/terminal-selection-BCOh9jsL.js +329 -0
- package/dist/utils/env.d.ts +3 -0
- package/dist/utils/newlines.d.ts +1 -0
- package/dist/utils/path.d.ts +7 -0
- package/dist/utils/url-safety.d.ts +2 -0
- package/dist/vue/components/TAnchor.d.ts +108 -0
- package/dist/vue/components/TBox.d.ts +144 -0
- package/dist/vue/components/TDebugOverlay.d.ts +43 -0
- package/dist/vue/components/TDialog.d.ts +256 -0
- package/dist/vue/components/TFlow.d.ts +85 -0
- package/dist/vue/components/TInput.d.ts +554 -0
- package/dist/vue/components/TInputBox.d.ts +158 -0
- package/dist/vue/components/TJsonEditor.d.ts +165 -0
- package/dist/vue/components/TList.d.ts +119 -0
- package/dist/vue/components/TLogLinksPanel.d.ts +238 -0
- package/dist/vue/components/TLogMinimap.d.ts +263 -0
- package/dist/vue/components/TLogScrollbar.d.ts +236 -0
- package/dist/vue/components/TLogSearchBar.d.ts +267 -0
- package/dist/vue/components/TLogSearchPager.d.ts +162 -0
- package/dist/vue/components/TLogSearchResults.d.ts +200 -0
- package/dist/vue/components/TLogView.d.ts +502 -0
- package/dist/vue/components/TLogVirtualLinksPanel.d.ts +141 -0
- package/dist/vue/components/TLogVirtualSearchResults.d.ts +146 -0
- package/dist/vue/components/TMarkdownText.d.ts +134 -0
- package/dist/vue/components/TMultilineModal.d.ts +62 -0
- package/dist/vue/components/TPathPicker.d.ts +213 -0
- package/dist/vue/components/TRenderLayer.d.ts +16 -0
- package/dist/vue/components/TRenderPlane.d.ts +17 -0
- package/dist/vue/components/TSelect.d.ts +215 -0
- package/dist/vue/components/TText.d.ts +116 -0
- package/dist/vue/components/TTransition.d.ts +83 -0
- package/dist/vue/components/TView.d.ts +127 -0
- package/dist/vue/components/TVirtualList.d.ts +182 -0
- package/dist/vue/components/TVirtualMarkdown.d.ts +168 -0
- package/dist/vue/components/TerminalProvider.d.ts +195 -0
- package/dist/vue/components/input/host.d.ts +17 -0
- package/dist/vue/components/input/plugins/hostPlugin.d.ts +7 -0
- package/dist/vue/components/input/plugins/hostPlugin.node.d.ts +15 -0
- package/dist/vue/components/input/plugins/mentionUtils.d.ts +17 -0
- package/dist/vue/components/input/plugins/nodeMentionPathProvider.d.ts +2 -0
- package/dist/vue/components/input/plugins/promptMentionPlugin.d.ts +8 -0
- package/dist/vue/components/input/plugins/promptMentionState.d.ts +98 -0
- package/dist/vue/components/input/plugins/restrictText.d.ts +27 -0
- package/dist/vue/components/input/plugins/types.d.ts +90 -0
- package/dist/vue/components/input/utils/inlineText.d.ts +57 -0
- package/dist/vue/components/input/utils/inlineTextTokens.d.ts +34 -0
- package/dist/vue/components/input/utils/primitives.d.ts +3 -0
- package/dist/vue/components/input/utils/wordNavigation.d.ts +6 -0
- package/dist/vue/components/list/list-geometry.d.ts +16 -0
- package/dist/vue/components/list/list-paint.d.ts +1 -0
- package/dist/vue/components/list/scroll-state.d.ts +25 -0
- package/dist/vue/components/list/selection-state.d.ts +6 -0
- package/dist/vue/components/terminal-provider/copy-toast.d.ts +6 -0
- package/dist/vue/components/terminal-provider/portals.d.ts +17 -0
- package/dist/vue/components/terminal-provider/scheduler.d.ts +29 -0
- package/dist/vue/components/terminal-provider/selection-config.d.ts +15 -0
- package/dist/vue/components/terminal-provider/utils.d.ts +5 -0
- package/dist/vue/composables/use-layout.d.ts +2 -0
- package/dist/vue/composables/use-render-node.d.ts +19 -0
- package/dist/vue/composables/use-render-stack.d.ts +3 -0
- package/dist/vue/composables/use-runtime.d.ts +5 -0
- package/dist/vue/composables/use-terminal-node.d.ts +14 -0
- package/dist/vue/composables/use-terminal.d.ts +2 -0
- package/dist/vue/composables/use-visibility.d.ts +12 -0
- package/dist/vue/context.d.ts +153 -0
- package/dist/vue/index.d.ts +39 -0
- package/dist/vue/injection-key.d.ts +2 -0
- package/dist/vue/log/append-only-log-store.d.ts +2 -0
- package/dist/vue/log/tlog-keymap.d.ts +28 -0
- package/dist/vue/log/tlog-plugins.d.ts +144 -0
- package/dist/vue/log/tlog-session.d.ts +81 -0
- package/dist/vue/log/tlog-theme.d.ts +68 -0
- package/dist/vue/log/types.d.ts +43 -0
- package/dist/vue/log/use-tlog-link-controller.d.ts +31 -0
- package/dist/vue/log/use-tlog-retained-index.d.ts +26 -0
- package/dist/vue/log/use-tlog-search-controller.d.ts +51 -0
- package/dist/vue/log/use-tlog-search-results-page.d.ts +31 -0
- package/dist/vue/log/use-tlog-virtual-search-results.d.ts +22 -0
- package/dist/vue/markdown/ast.d.ts +3 -0
- package/dist/vue/markdown/block-source.d.ts +22 -0
- package/dist/vue/markdown/document.d.ts +18 -0
- package/dist/vue/markdown/layout.d.ts +16 -0
- package/dist/vue/markdown/parser.d.ts +11 -0
- package/dist/vue/markdown/render.d.ts +10 -0
- package/dist/vue/markdown/theme.d.ts +186 -0
- package/dist/vue/markdown/types.d.ts +47 -0
- package/dist/vue/render/context.d.ts +3 -0
- package/dist/vue/render/render-manager.d.ts +94 -0
- package/dist/vue/router/RouterView.d.ts +25 -0
- package/dist/vue/router/composables.d.ts +4 -0
- package/dist/vue/router/context.d.ts +4 -0
- package/dist/vue/router/router.d.ts +7 -0
- package/dist/vue/router/types.d.ts +29 -0
- package/dist/vue/scheduler/frame-mailbox.d.ts +55 -0
- package/dist/vue/scheduler/frame-scheduler.d.ts +51 -0
- package/dist/vue/utils/frame-task.d.ts +6 -0
- package/dist/vue/utils/rect.d.ts +4 -0
- package/dist/vue/utils/style-cache.d.ts +3 -0
- package/dist/vue/utils/text.d.ts +26 -0
- package/dist/vue/utils/wheel-scroll.d.ts +18 -0
- package/dist/vue.cjs +18882 -0
- package/dist/vue.d.cts +3 -0
- package/dist/vue.d.ts +3 -0
- package/dist/vue.js +3066 -0
- package/dist/wheel-scroll-B-WlFuNB.js +819 -0
- package/package.json +226 -58
- package/dist/path-suggest-DIMby3r0.js +0 -3
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,1649 @@
|
|
|
1
|
+
import { sanitizeDomHref } from "./hyperlink-B5PD7a3Q.js";
|
|
2
|
+
import { terminalSelectionRowSpans, terminalSelectionVisibleRowSpans } from "./terminal-selection-BCOh9jsL.js";
|
|
3
|
+
import { EventZIndexContextKey, applyWheelScroll, createWheelScrollState, forEachTextCellSegment, intersectRect, normalizeCellRect, repeatChar, resetWheelScrollState, sanitizeInlineText, sanitizeTextBlock, sliceByCellsRange, spaces, textCellWidth, translateRect, useLayout, useRenderNode, useTerminal, useTerminalNode, useVisibility } from "./wheel-scroll-B-WlFuNB.js";
|
|
4
|
+
import { computed, defineComponent, getCurrentInstance, h, inject, markRaw, onBeforeUnmount, ref, shallowRef, watch, watchEffect } from "vue";
|
|
5
|
+
import { getMarkdown, parseMarkdownToStructure } from "stream-markdown-parser";
|
|
6
|
+
|
|
7
|
+
//#region src/vue/markdown/parser.ts
|
|
8
|
+
const markdownInstanceCache = new Map();
|
|
9
|
+
const MAX_MARKDOWN_INSTANCE_CACHE = 32;
|
|
10
|
+
function sanitizeMarkdownLink(url) {
|
|
11
|
+
return sanitizeDomHref(url, { allowRelative: true });
|
|
12
|
+
}
|
|
13
|
+
function isSafeMarkdownLink(url) {
|
|
14
|
+
return sanitizeMarkdownLink(url) != null;
|
|
15
|
+
}
|
|
16
|
+
function normalizeCustomHtmlTags(config) {
|
|
17
|
+
return Array.from(new Set(config?.customHtmlTags?.filter(Boolean) ?? [])).sort();
|
|
18
|
+
}
|
|
19
|
+
function markdownInstanceCacheKey(customHtmlTags) {
|
|
20
|
+
return JSON.stringify({ customHtmlTags });
|
|
21
|
+
}
|
|
22
|
+
function getCachedMarkdownInstance(customHtmlTags) {
|
|
23
|
+
const cacheKey = markdownInstanceCacheKey(customHtmlTags);
|
|
24
|
+
const cached = markdownInstanceCache.get(cacheKey);
|
|
25
|
+
if (cached) {
|
|
26
|
+
markdownInstanceCache.delete(cacheKey);
|
|
27
|
+
markdownInstanceCache.set(cacheKey, cached);
|
|
28
|
+
return cached;
|
|
29
|
+
}
|
|
30
|
+
const created = getMarkdown(`vue-tui-markdown:${cacheKey}`, { customHtmlTags });
|
|
31
|
+
markdownInstanceCache.set(cacheKey, created);
|
|
32
|
+
while (markdownInstanceCache.size > MAX_MARKDOWN_INSTANCE_CACHE) {
|
|
33
|
+
const oldest = markdownInstanceCache.keys().next().value;
|
|
34
|
+
if (oldest == null) break;
|
|
35
|
+
markdownInstanceCache.delete(oldest);
|
|
36
|
+
}
|
|
37
|
+
return created;
|
|
38
|
+
}
|
|
39
|
+
function createTuiMarkdownParser(config) {
|
|
40
|
+
const customHtmlTags = normalizeCustomHtmlTags(config);
|
|
41
|
+
const md = getCachedMarkdownInstance(customHtmlTags);
|
|
42
|
+
function parse(content, final) {
|
|
43
|
+
return parseMarkdownToStructure(content, md, {
|
|
44
|
+
final,
|
|
45
|
+
customHtmlTags,
|
|
46
|
+
requireClosingStrong: final || !config?.streaming,
|
|
47
|
+
validateLink: isSafeMarkdownLink
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return { parse };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/vue/markdown/ast.ts
|
|
55
|
+
const EMPTY_PREFIX = Object.freeze([]);
|
|
56
|
+
const HARD_BREAK_SEGMENT = Object.freeze({
|
|
57
|
+
text: "",
|
|
58
|
+
hardBreak: true
|
|
59
|
+
});
|
|
60
|
+
function mergeStyle$1(base, overlay) {
|
|
61
|
+
if (!base) return overlay;
|
|
62
|
+
if (!overlay) return base;
|
|
63
|
+
return {
|
|
64
|
+
...base,
|
|
65
|
+
...overlay
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function trimBlockArray(blocks) {
|
|
69
|
+
let start = 0;
|
|
70
|
+
let end = blocks.length;
|
|
71
|
+
while (start < end && blocks[start]?.type === "blank") start++;
|
|
72
|
+
while (end > start && blocks[end - 1]?.type === "blank") end--;
|
|
73
|
+
return blocks.slice(start, end);
|
|
74
|
+
}
|
|
75
|
+
function nodeChildren(node) {
|
|
76
|
+
return "children" in node && Array.isArray(node.children) ? node.children : [];
|
|
77
|
+
}
|
|
78
|
+
function stringProp(node, key) {
|
|
79
|
+
const value = node[key];
|
|
80
|
+
return typeof value === "string" ? value : "";
|
|
81
|
+
}
|
|
82
|
+
function booleanProp(node, key) {
|
|
83
|
+
return node[key] === true;
|
|
84
|
+
}
|
|
85
|
+
function numberProp(node, key) {
|
|
86
|
+
const value = node[key];
|
|
87
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
88
|
+
}
|
|
89
|
+
function nodeItems(node) {
|
|
90
|
+
const value = node.items;
|
|
91
|
+
return Array.isArray(value) ? value : [];
|
|
92
|
+
}
|
|
93
|
+
function pushTextSegments(out, text, style) {
|
|
94
|
+
if (!text) return;
|
|
95
|
+
const normalized = sanitizeTextBlock(text);
|
|
96
|
+
if (!normalized) return;
|
|
97
|
+
const parts = normalized.split("\n");
|
|
98
|
+
for (let i = 0; i < parts.length; i++) {
|
|
99
|
+
const part = sanitizeInlineText(parts[i] ?? "");
|
|
100
|
+
if (part) out.push(style ? {
|
|
101
|
+
text: part,
|
|
102
|
+
style
|
|
103
|
+
} : { text: part });
|
|
104
|
+
if (i < parts.length - 1) out.push(HARD_BREAK_SEGMENT);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function sanitizeCodeBlockText(text, tabSize = 4) {
|
|
108
|
+
const normalized = String(text ?? "").replace(/\r\n?/g, "\n").replace(/\t/g, spaces(tabSize)).replace(/\u00a0/g, " ");
|
|
109
|
+
let out = "";
|
|
110
|
+
for (const ch of normalized) {
|
|
111
|
+
const code = ch.charCodeAt(0);
|
|
112
|
+
if (code >= 0 && code <= 8 || code === 11 || code === 12) continue;
|
|
113
|
+
if (code >= 14 && code <= 31 || code === 127) continue;
|
|
114
|
+
out += ch;
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
function inlineNodeSegments(nodes, theme, inheritedStyle) {
|
|
119
|
+
const out = [];
|
|
120
|
+
for (const node of nodes) switch (node.type) {
|
|
121
|
+
case "text":
|
|
122
|
+
pushTextSegments(out, stringProp(node, "content"), inheritedStyle);
|
|
123
|
+
break;
|
|
124
|
+
case "inline":
|
|
125
|
+
case "paragraph":
|
|
126
|
+
case "heading":
|
|
127
|
+
case "list_item":
|
|
128
|
+
case "blockquote":
|
|
129
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, inheritedStyle));
|
|
130
|
+
break;
|
|
131
|
+
case "strong":
|
|
132
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, mergeStyle$1(inheritedStyle, theme.strong)));
|
|
133
|
+
break;
|
|
134
|
+
case "emphasis":
|
|
135
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, mergeStyle$1(inheritedStyle, theme.emphasis)));
|
|
136
|
+
break;
|
|
137
|
+
case "strikethrough":
|
|
138
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, mergeStyle$1(inheritedStyle, theme.strikethrough)));
|
|
139
|
+
break;
|
|
140
|
+
case "highlight":
|
|
141
|
+
case "insert":
|
|
142
|
+
case "subscript":
|
|
143
|
+
case "superscript":
|
|
144
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, inheritedStyle));
|
|
145
|
+
break;
|
|
146
|
+
case "inline_code":
|
|
147
|
+
pushTextSegments(out, stringProp(node, "code"), mergeStyle$1(inheritedStyle, theme.inlineCode));
|
|
148
|
+
break;
|
|
149
|
+
case "hardbreak":
|
|
150
|
+
out.push(HARD_BREAK_SEGMENT);
|
|
151
|
+
break;
|
|
152
|
+
case "link": {
|
|
153
|
+
const href = stringProp(node, "href");
|
|
154
|
+
const safeHref = href ? sanitizeMarkdownLink(href) : null;
|
|
155
|
+
const linkStyle = safeHref ? mergeStyle$1(inheritedStyle, {
|
|
156
|
+
...theme.link,
|
|
157
|
+
href: safeHref
|
|
158
|
+
}) : inheritedStyle;
|
|
159
|
+
const children = nodeChildren(node);
|
|
160
|
+
if (children.length) out.push(...inlineNodeSegments(children, theme, linkStyle));
|
|
161
|
+
else pushTextSegments(out, stringProp(node, "text"), linkStyle);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
case "image":
|
|
165
|
+
pushTextSegments(out, stringProp(node, "alt") || stringProp(node, "src"), mergeStyle$1(inheritedStyle, theme.link));
|
|
166
|
+
break;
|
|
167
|
+
case "checkbox":
|
|
168
|
+
case "checkbox_input":
|
|
169
|
+
pushTextSegments(out, booleanProp(node, "checked") ? "[x]" : "[ ]", inheritedStyle);
|
|
170
|
+
break;
|
|
171
|
+
case "emoji":
|
|
172
|
+
pushTextSegments(out, stringProp(node, "markup") || stringProp(node, "name"), inheritedStyle);
|
|
173
|
+
break;
|
|
174
|
+
case "math_inline":
|
|
175
|
+
pushTextSegments(out, stringProp(node, "content"), mergeStyle$1(inheritedStyle, theme.inlineCode));
|
|
176
|
+
break;
|
|
177
|
+
case "reference":
|
|
178
|
+
case "footnote_reference":
|
|
179
|
+
case "footnote_anchor":
|
|
180
|
+
pushTextSegments(out, stringProp(node, "raw"), inheritedStyle);
|
|
181
|
+
break;
|
|
182
|
+
case "html_inline":
|
|
183
|
+
pushTextSegments(out, stringProp(node, "content"), mergeStyle$1(inheritedStyle, theme.html));
|
|
184
|
+
break;
|
|
185
|
+
case "html_block":
|
|
186
|
+
pushTextSegments(out, stringProp(node, "content"), mergeStyle$1(inheritedStyle, theme.html));
|
|
187
|
+
break;
|
|
188
|
+
default:
|
|
189
|
+
if (nodeChildren(node).length) {
|
|
190
|
+
out.push(...inlineNodeSegments(nodeChildren(node), theme, inheritedStyle));
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
pushTextSegments(out, stringProp(node, "raw"), inheritedStyle);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
function inlineBlock(key, segments, context) {
|
|
199
|
+
return {
|
|
200
|
+
type: "inline",
|
|
201
|
+
key,
|
|
202
|
+
segments,
|
|
203
|
+
prefixSegments: context.prefixSegments,
|
|
204
|
+
continuationPrefixSegments: context.continuationPrefixSegments
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function blockFromParagraph(node, key, context, theme) {
|
|
208
|
+
return inlineBlock(key, inlineNodeSegments(nodeChildren(node), theme), context);
|
|
209
|
+
}
|
|
210
|
+
function blockFromHeading(node, key, context, theme) {
|
|
211
|
+
const level = Math.min(6, Math.max(1, Math.floor(numberProp(node, "level") ?? 1)));
|
|
212
|
+
return inlineBlock(key, inlineNodeSegments(nodeChildren(node), theme, theme.heading[level - 1]), context);
|
|
213
|
+
}
|
|
214
|
+
function blockFromCodeBlock(node, key, context, theme) {
|
|
215
|
+
return {
|
|
216
|
+
type: "code_block",
|
|
217
|
+
key,
|
|
218
|
+
lines: sanitizeCodeBlockText(stringProp(node, "code")).split("\n"),
|
|
219
|
+
style: theme.codeBlock,
|
|
220
|
+
prefixSegments: context.prefixSegments,
|
|
221
|
+
continuationPrefixSegments: context.continuationPrefixSegments
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function blockFromHtmlBlock(node, key, context, theme) {
|
|
225
|
+
return inlineBlock(key, inlineNodeSegments([{
|
|
226
|
+
type: "html_block",
|
|
227
|
+
raw: stringProp(node, "raw"),
|
|
228
|
+
content: stringProp(node, "content")
|
|
229
|
+
}], theme), context);
|
|
230
|
+
}
|
|
231
|
+
function childSequenceToBlocks(nodes, context, theme, keyPrefix) {
|
|
232
|
+
const out = [];
|
|
233
|
+
let appended = false;
|
|
234
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
235
|
+
const childBlocks = trimBlockArray(nodeToBlocks(nodes[i], context, theme, `${keyPrefix}-${i}`));
|
|
236
|
+
if (!childBlocks.length) continue;
|
|
237
|
+
if (appended && out[out.length - 1]?.type !== "blank" && childBlocks[0]?.type !== "blank") out.push({
|
|
238
|
+
type: "blank",
|
|
239
|
+
key: `${keyPrefix}-${i}-gap`
|
|
240
|
+
});
|
|
241
|
+
out.push(...childBlocks);
|
|
242
|
+
appended = true;
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
function listItemBlocks(item, index, ordered, start, context, theme, keyPrefix) {
|
|
247
|
+
const markerText = ordered ? `${start + index}. ` : "- ";
|
|
248
|
+
const markerCells = textCellWidth(markerText);
|
|
249
|
+
const markerSegment = {
|
|
250
|
+
text: markerText,
|
|
251
|
+
style: theme.listMarker
|
|
252
|
+
};
|
|
253
|
+
const indentSegment = { text: spaces(markerCells) };
|
|
254
|
+
const firstContext = {
|
|
255
|
+
prefixSegments: [...context.prefixSegments, markerSegment],
|
|
256
|
+
continuationPrefixSegments: [...context.continuationPrefixSegments, indentSegment]
|
|
257
|
+
};
|
|
258
|
+
const nestedContext = {
|
|
259
|
+
prefixSegments: [...context.continuationPrefixSegments, indentSegment],
|
|
260
|
+
continuationPrefixSegments: [...context.continuationPrefixSegments, indentSegment]
|
|
261
|
+
};
|
|
262
|
+
const children = nodeChildren(item);
|
|
263
|
+
if (!children.length) return [inlineBlock(`${keyPrefix}-empty`, [], firstContext)];
|
|
264
|
+
const out = [];
|
|
265
|
+
let usedLead = false;
|
|
266
|
+
for (let i = 0; i < children.length; i++) {
|
|
267
|
+
const child = children[i];
|
|
268
|
+
const useLeadContext = !usedLead && (child.type === "paragraph" || child.type === "heading" || child.type === "code_block");
|
|
269
|
+
if (!usedLead && !useLeadContext) {
|
|
270
|
+
out.push(inlineBlock(`${keyPrefix}-marker`, [], firstContext));
|
|
271
|
+
usedLead = true;
|
|
272
|
+
}
|
|
273
|
+
const childBlocks = trimBlockArray(nodeToBlocks(child, useLeadContext && !usedLead ? firstContext : nestedContext, theme, `${keyPrefix}-${i}`));
|
|
274
|
+
if (!childBlocks.length) continue;
|
|
275
|
+
const startsNestedList = child.type === "list" && out.length > 0 && out[out.length - 1]?.type === "inline" && childBlocks[0]?.type === "inline";
|
|
276
|
+
if (out.length && out[out.length - 1]?.type !== "blank" && childBlocks[0]?.type !== "blank" && !startsNestedList) out.push({
|
|
277
|
+
type: "blank",
|
|
278
|
+
key: `${keyPrefix}-${i}-gap`
|
|
279
|
+
});
|
|
280
|
+
out.push(...childBlocks);
|
|
281
|
+
usedLead = true;
|
|
282
|
+
}
|
|
283
|
+
if (!out.length) out.push(inlineBlock(`${keyPrefix}-fallback`, [], firstContext));
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
function listBlocks(node, context, theme, keyPrefix) {
|
|
287
|
+
const out = [];
|
|
288
|
+
const items = nodeItems(node);
|
|
289
|
+
for (let i = 0; i < items.length; i++) out.push(...listItemBlocks(items[i], i, booleanProp(node, "ordered"), numberProp(node, "start") ?? 1, context, theme, `${keyPrefix}-${i}`));
|
|
290
|
+
return out;
|
|
291
|
+
}
|
|
292
|
+
function nodeToBlocks(node, context, theme, keyPrefix) {
|
|
293
|
+
switch (node.type) {
|
|
294
|
+
case "paragraph":
|
|
295
|
+
if ("children" in node && Array.isArray(node.children)) return [blockFromParagraph(node, keyPrefix, context, theme)];
|
|
296
|
+
break;
|
|
297
|
+
case "heading":
|
|
298
|
+
if ("children" in node && Array.isArray(node.children) && "level" in node) return [blockFromHeading(node, keyPrefix, context, theme)];
|
|
299
|
+
break;
|
|
300
|
+
case "code_block":
|
|
301
|
+
if ("code" in node) return [blockFromCodeBlock(node, keyPrefix, context, theme)];
|
|
302
|
+
break;
|
|
303
|
+
case "thematic_break": return [{
|
|
304
|
+
type: "thematic_break",
|
|
305
|
+
key: keyPrefix,
|
|
306
|
+
char: "─",
|
|
307
|
+
style: theme.thematicBreak,
|
|
308
|
+
prefixSegments: context.prefixSegments
|
|
309
|
+
}];
|
|
310
|
+
case "blockquote": {
|
|
311
|
+
const quoteSegment = {
|
|
312
|
+
text: "│ ",
|
|
313
|
+
style: theme.blockquote
|
|
314
|
+
};
|
|
315
|
+
const quoteContext = {
|
|
316
|
+
prefixSegments: [...context.prefixSegments, quoteSegment],
|
|
317
|
+
continuationPrefixSegments: [...context.continuationPrefixSegments, quoteSegment]
|
|
318
|
+
};
|
|
319
|
+
return childSequenceToBlocks(nodeChildren(node), quoteContext, theme, keyPrefix);
|
|
320
|
+
}
|
|
321
|
+
case "list":
|
|
322
|
+
if ("items" in node && Array.isArray(node.items)) return listBlocks(node, context, theme, keyPrefix);
|
|
323
|
+
break;
|
|
324
|
+
case "inline": return [inlineBlock(keyPrefix, inlineNodeSegments(nodeChildren(node), theme), context)];
|
|
325
|
+
case "html_block":
|
|
326
|
+
if ("content" in node) return [blockFromHtmlBlock(node, keyPrefix, context, theme)];
|
|
327
|
+
break;
|
|
328
|
+
case "text": return [inlineBlock(keyPrefix, inlineNodeSegments([node], theme), context)];
|
|
329
|
+
}
|
|
330
|
+
if (nodeChildren(node).length) return childSequenceToBlocks(nodeChildren(node), context, theme, keyPrefix);
|
|
331
|
+
return [inlineBlock(keyPrefix, inlineNodeSegments([node], theme), context)];
|
|
332
|
+
}
|
|
333
|
+
function markdownAstToBlocks(nodes, theme) {
|
|
334
|
+
const blocks = trimBlockArray(childSequenceToBlocks(nodes, {
|
|
335
|
+
prefixSegments: EMPTY_PREFIX,
|
|
336
|
+
continuationPrefixSegments: EMPTY_PREFIX
|
|
337
|
+
}, theme, "md"));
|
|
338
|
+
return blocks.length ? blocks : [{
|
|
339
|
+
type: "blank",
|
|
340
|
+
key: "md-empty"
|
|
341
|
+
}];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region src/vue/markdown/layout.ts
|
|
346
|
+
function toVisualSegment(segment) {
|
|
347
|
+
if (!segment.text) return null;
|
|
348
|
+
const cells = textCellWidth(segment.text);
|
|
349
|
+
if (cells <= 0) return null;
|
|
350
|
+
return {
|
|
351
|
+
text: segment.text,
|
|
352
|
+
style: segment.style,
|
|
353
|
+
cells
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function segmentsPlainText(segments) {
|
|
357
|
+
return segments.map((segment) => segment.text).join("");
|
|
358
|
+
}
|
|
359
|
+
function segmentsCellWidth(segments) {
|
|
360
|
+
let cells = 0;
|
|
361
|
+
for (const segment of segments) {
|
|
362
|
+
if (segment.hardBreak || !segment.text) continue;
|
|
363
|
+
cells += textCellWidth(segment.text);
|
|
364
|
+
}
|
|
365
|
+
return cells;
|
|
366
|
+
}
|
|
367
|
+
function normalizeInlineSegments(segments) {
|
|
368
|
+
const lines = [[]];
|
|
369
|
+
for (const segment of segments) {
|
|
370
|
+
if (segment.hardBreak) {
|
|
371
|
+
lines.push([]);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
lines[lines.length - 1].push(segment);
|
|
375
|
+
}
|
|
376
|
+
return lines;
|
|
377
|
+
}
|
|
378
|
+
function clipInlineSegmentsToWidth(segments, width) {
|
|
379
|
+
let remaining = Math.max(0, Math.floor(width));
|
|
380
|
+
if (remaining <= 0) return [];
|
|
381
|
+
const out = [];
|
|
382
|
+
for (const segment of segments) {
|
|
383
|
+
if (segment.hardBreak || !segment.text || remaining <= 0) continue;
|
|
384
|
+
const piece = sliceByCellsRange(segment.text, 0, remaining);
|
|
385
|
+
const cells = textCellWidth(piece);
|
|
386
|
+
if (!piece || cells <= 0) continue;
|
|
387
|
+
out.push(segment.style ? {
|
|
388
|
+
text: piece,
|
|
389
|
+
style: segment.style
|
|
390
|
+
} : { text: piece });
|
|
391
|
+
remaining -= cells;
|
|
392
|
+
}
|
|
393
|
+
return out;
|
|
394
|
+
}
|
|
395
|
+
function wrapLineSegments(source, prefixSegments, continuationPrefixSegments, width) {
|
|
396
|
+
const rows = [];
|
|
397
|
+
const prefixCells = segmentsCellWidth(prefixSegments);
|
|
398
|
+
const continuationPrefixCells = segmentsCellWidth(continuationPrefixSegments);
|
|
399
|
+
const firstPrefix = prefixCells >= width ? clipInlineSegmentsToWidth(prefixSegments, width) : prefixSegments;
|
|
400
|
+
const firstPrefixCells = segmentsCellWidth(firstPrefix);
|
|
401
|
+
const continuationPrefix = continuationPrefixCells >= width ? clipInlineSegmentsToWidth(continuationPrefixSegments, Math.max(0, width - 1)) : continuationPrefixSegments;
|
|
402
|
+
const trimmedContinuationPrefixCells = segmentsCellWidth(continuationPrefix);
|
|
403
|
+
const maxFirst = Math.max(0, width - firstPrefixCells);
|
|
404
|
+
const maxNext = Math.max(0, width - trimmedContinuationPrefixCells);
|
|
405
|
+
const openRow = (useContinuation) => {
|
|
406
|
+
const prefix = useContinuation ? continuationPrefix : firstPrefix;
|
|
407
|
+
const renderedPrefix = prefix.map(toVisualSegment).filter(Boolean);
|
|
408
|
+
return {
|
|
409
|
+
useContinuation,
|
|
410
|
+
segments: [...renderedPrefix],
|
|
411
|
+
remaining: useContinuation ? maxNext : maxFirst
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
const openDegradedRow = () => ({
|
|
415
|
+
useContinuation: true,
|
|
416
|
+
segments: [],
|
|
417
|
+
remaining: width
|
|
418
|
+
});
|
|
419
|
+
let row = openRow(false);
|
|
420
|
+
if (!source.length) {
|
|
421
|
+
rows.push(row.segments);
|
|
422
|
+
return rows;
|
|
423
|
+
}
|
|
424
|
+
const pushRow = () => {
|
|
425
|
+
rows.push(row.segments);
|
|
426
|
+
row = openRow(true);
|
|
427
|
+
};
|
|
428
|
+
const prefixLengthForRow = () => row.useContinuation ? continuationPrefix.length : firstPrefix.length;
|
|
429
|
+
const rowHasBody = () => row.segments.length > prefixLengthForRow();
|
|
430
|
+
for (const segment of source) {
|
|
431
|
+
let aborted = false;
|
|
432
|
+
forEachTextCellSegment(segment.text, (piece) => {
|
|
433
|
+
while (true) {
|
|
434
|
+
if (row.remaining <= 0) {
|
|
435
|
+
pushRow();
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (piece.cells > row.remaining) {
|
|
439
|
+
if (rowHasBody()) {
|
|
440
|
+
pushRow();
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (!row.useContinuation) {
|
|
444
|
+
rows.push(row.segments);
|
|
445
|
+
row = openDegradedRow();
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (piece.cells <= width) {
|
|
449
|
+
row = openDegradedRow();
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
aborted = true;
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
row.segments.push({
|
|
456
|
+
text: piece.text,
|
|
457
|
+
style: segment.style,
|
|
458
|
+
cells: piece.cells
|
|
459
|
+
});
|
|
460
|
+
row.remaining -= piece.cells;
|
|
461
|
+
return void 0;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
if (aborted) continue;
|
|
465
|
+
}
|
|
466
|
+
rows.push(row.segments);
|
|
467
|
+
return rows;
|
|
468
|
+
}
|
|
469
|
+
function inlineBlockRows(block, width) {
|
|
470
|
+
const rows = [];
|
|
471
|
+
const lines = normalizeInlineSegments(block.segments);
|
|
472
|
+
const prefix = block.prefixSegments ?? [];
|
|
473
|
+
const continuationPrefix = block.continuationPrefixSegments ?? prefix;
|
|
474
|
+
let rowInBlock = 0;
|
|
475
|
+
for (const line of lines) {
|
|
476
|
+
const wrapped = wrapLineSegments(line, prefix, continuationPrefix, width);
|
|
477
|
+
for (const visualSegments of wrapped) {
|
|
478
|
+
rows.push({
|
|
479
|
+
key: `${block.key}:${rowInBlock}`,
|
|
480
|
+
blockKey: block.key,
|
|
481
|
+
rowInBlock,
|
|
482
|
+
plainText: segmentsPlainText(visualSegments),
|
|
483
|
+
segments: visualSegments
|
|
484
|
+
});
|
|
485
|
+
rowInBlock++;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return rows.length ? rows : [{
|
|
489
|
+
key: `${block.key}:0`,
|
|
490
|
+
blockKey: block.key,
|
|
491
|
+
rowInBlock: 0,
|
|
492
|
+
plainText: "",
|
|
493
|
+
segments: prefix.map(toVisualSegment).filter(Boolean)
|
|
494
|
+
}];
|
|
495
|
+
}
|
|
496
|
+
function codeBlockRows(block, width) {
|
|
497
|
+
const rows = [];
|
|
498
|
+
const prefix = block.prefixSegments ?? [];
|
|
499
|
+
const continuationPrefix = block.continuationPrefixSegments ?? prefix;
|
|
500
|
+
const lineSegments = block.lines.length ? block.lines : [""];
|
|
501
|
+
let rowInBlock = 0;
|
|
502
|
+
for (const line of lineSegments) {
|
|
503
|
+
const sourceSegments = [{
|
|
504
|
+
text: line,
|
|
505
|
+
style: block.style
|
|
506
|
+
}];
|
|
507
|
+
const wrapped = wrapLineSegments(sourceSegments, prefix, continuationPrefix, width);
|
|
508
|
+
for (const visualSegments of wrapped) {
|
|
509
|
+
rows.push({
|
|
510
|
+
key: `${block.key}:${rowInBlock}`,
|
|
511
|
+
blockKey: block.key,
|
|
512
|
+
rowInBlock,
|
|
513
|
+
plainText: segmentsPlainText(visualSegments),
|
|
514
|
+
segments: visualSegments
|
|
515
|
+
});
|
|
516
|
+
rowInBlock++;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return rows;
|
|
520
|
+
}
|
|
521
|
+
function thematicBreakRows(block, width) {
|
|
522
|
+
const prefix = (block.prefixSegments ?? []).map(toVisualSegment).filter(Boolean);
|
|
523
|
+
const prefixCells = prefix.reduce((sum, segment) => sum + segment.cells, 0);
|
|
524
|
+
const fill = Math.max(0, width - prefixCells);
|
|
525
|
+
const bar = fill > 0 ? [{
|
|
526
|
+
text: repeatChar(block.char ?? "─", fill),
|
|
527
|
+
style: block.style,
|
|
528
|
+
cells: fill
|
|
529
|
+
}] : [];
|
|
530
|
+
return [{
|
|
531
|
+
key: `${block.key}:0`,
|
|
532
|
+
blockKey: block.key,
|
|
533
|
+
rowInBlock: 0,
|
|
534
|
+
plainText: segmentsPlainText([...prefix, ...bar]),
|
|
535
|
+
segments: [...prefix, ...bar]
|
|
536
|
+
}];
|
|
537
|
+
}
|
|
538
|
+
function styleSignature(style) {
|
|
539
|
+
if (!style) return "";
|
|
540
|
+
return [
|
|
541
|
+
style.fg ?? "",
|
|
542
|
+
style.bg ?? "",
|
|
543
|
+
style.bold ? "1" : "0",
|
|
544
|
+
style.dim ? "1" : "0",
|
|
545
|
+
style.italic ? "1" : "0",
|
|
546
|
+
style.underline ? "1" : "0",
|
|
547
|
+
style.inverse ? "1" : "0",
|
|
548
|
+
style.href ?? ""
|
|
549
|
+
].join("");
|
|
550
|
+
}
|
|
551
|
+
function inlineSegmentSignature(segment) {
|
|
552
|
+
return [
|
|
553
|
+
segment.text,
|
|
554
|
+
segment.hardBreak ? "1" : "0",
|
|
555
|
+
styleSignature(segment.style)
|
|
556
|
+
].join("");
|
|
557
|
+
}
|
|
558
|
+
function inlineSegmentsSignature(segments) {
|
|
559
|
+
return (segments ?? []).map(inlineSegmentSignature).join("");
|
|
560
|
+
}
|
|
561
|
+
function blockLayoutSignature(block) {
|
|
562
|
+
switch (block.type) {
|
|
563
|
+
case "inline": return [
|
|
564
|
+
block.type,
|
|
565
|
+
inlineSegmentsSignature(block.segments),
|
|
566
|
+
inlineSegmentsSignature(block.prefixSegments),
|
|
567
|
+
inlineSegmentsSignature(block.continuationPrefixSegments)
|
|
568
|
+
].join("");
|
|
569
|
+
case "code_block": return [
|
|
570
|
+
block.type,
|
|
571
|
+
block.lines.join(""),
|
|
572
|
+
styleSignature(block.style),
|
|
573
|
+
inlineSegmentsSignature(block.prefixSegments),
|
|
574
|
+
inlineSegmentsSignature(block.continuationPrefixSegments)
|
|
575
|
+
].join("");
|
|
576
|
+
case "thematic_break": return [
|
|
577
|
+
block.type,
|
|
578
|
+
block.char ?? "",
|
|
579
|
+
styleSignature(block.style),
|
|
580
|
+
inlineSegmentsSignature(block.prefixSegments)
|
|
581
|
+
].join("");
|
|
582
|
+
case "blank": return block.type;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
function layoutMarkdownBlock(block, width) {
|
|
586
|
+
switch (block.type) {
|
|
587
|
+
case "inline": return inlineBlockRows(block, width);
|
|
588
|
+
case "code_block": return codeBlockRows(block, width);
|
|
589
|
+
case "thematic_break": return thematicBreakRows(block, width);
|
|
590
|
+
case "blank": return [{
|
|
591
|
+
key: `${block.key}:0`,
|
|
592
|
+
blockKey: block.key,
|
|
593
|
+
rowInBlock: 0,
|
|
594
|
+
plainText: "",
|
|
595
|
+
segments: []
|
|
596
|
+
}];
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function layoutMarkdownBlocksCached(blocks, width, previous) {
|
|
600
|
+
const normalizedWidth = Math.max(0, Math.floor(width));
|
|
601
|
+
if (normalizedWidth <= 0) return {
|
|
602
|
+
rows: [],
|
|
603
|
+
cache: {
|
|
604
|
+
width: normalizedWidth,
|
|
605
|
+
entries: new Map()
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
const rows = [];
|
|
609
|
+
const entries = new Map();
|
|
610
|
+
const previousEntries = previous?.width === normalizedWidth ? previous.entries : void 0;
|
|
611
|
+
for (let index = 0; index < blocks.length; index++) {
|
|
612
|
+
const block = blocks[index];
|
|
613
|
+
const cacheKey = `${index}\u0000${block.key}`;
|
|
614
|
+
const signature = blockLayoutSignature(block);
|
|
615
|
+
const cached = previousEntries?.get(cacheKey);
|
|
616
|
+
const blockRows = cached?.signature === signature ? cached.rows : layoutMarkdownBlock(block, normalizedWidth);
|
|
617
|
+
if (blockRows.length) {
|
|
618
|
+
rows.push(...blockRows);
|
|
619
|
+
entries.set(cacheKey, {
|
|
620
|
+
signature,
|
|
621
|
+
rows: blockRows
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (!rows.length) {
|
|
626
|
+
const emptyRows = [{
|
|
627
|
+
key: "md-empty:0",
|
|
628
|
+
blockKey: "md-empty",
|
|
629
|
+
rowInBlock: 0,
|
|
630
|
+
plainText: "",
|
|
631
|
+
segments: []
|
|
632
|
+
}];
|
|
633
|
+
entries.set("0\0md-empty", {
|
|
634
|
+
signature: "empty",
|
|
635
|
+
rows: emptyRows
|
|
636
|
+
});
|
|
637
|
+
rows.push(...emptyRows);
|
|
638
|
+
}
|
|
639
|
+
return {
|
|
640
|
+
rows,
|
|
641
|
+
cache: {
|
|
642
|
+
width: normalizedWidth,
|
|
643
|
+
entries
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
function layoutMarkdownBlocks(blocks, width) {
|
|
648
|
+
return layoutMarkdownBlocksCached(blocks, width).rows;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
//#endregion
|
|
652
|
+
//#region src/vue/markdown/theme.ts
|
|
653
|
+
function freezeStyle(style) {
|
|
654
|
+
return Object.freeze(style);
|
|
655
|
+
}
|
|
656
|
+
function mergeFrozenStyle(base, overlay) {
|
|
657
|
+
if (!overlay) return base;
|
|
658
|
+
return freezeStyle({
|
|
659
|
+
...base,
|
|
660
|
+
...overlay
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
const DEFAULT_HEADING = Object.freeze([
|
|
664
|
+
freezeStyle({
|
|
665
|
+
bold: true,
|
|
666
|
+
fg: "cyanBright"
|
|
667
|
+
}),
|
|
668
|
+
freezeStyle({
|
|
669
|
+
bold: true,
|
|
670
|
+
fg: "cyanBright"
|
|
671
|
+
}),
|
|
672
|
+
freezeStyle({
|
|
673
|
+
bold: true,
|
|
674
|
+
fg: "blueBright"
|
|
675
|
+
}),
|
|
676
|
+
freezeStyle({
|
|
677
|
+
bold: true,
|
|
678
|
+
fg: "blueBright"
|
|
679
|
+
}),
|
|
680
|
+
freezeStyle({ bold: true }),
|
|
681
|
+
freezeStyle({ bold: true })
|
|
682
|
+
]);
|
|
683
|
+
const DEFAULT_TUI_MARKDOWN_THEME = Object.freeze({
|
|
684
|
+
heading: DEFAULT_HEADING,
|
|
685
|
+
strong: freezeStyle({ bold: true }),
|
|
686
|
+
emphasis: freezeStyle({ italic: true }),
|
|
687
|
+
strikethrough: freezeStyle({ dim: true }),
|
|
688
|
+
inlineCode: freezeStyle({ fg: "yellowBright" }),
|
|
689
|
+
link: freezeStyle({
|
|
690
|
+
fg: "blueBright",
|
|
691
|
+
underline: true
|
|
692
|
+
}),
|
|
693
|
+
blockquote: freezeStyle({ dim: true }),
|
|
694
|
+
listMarker: freezeStyle({
|
|
695
|
+
fg: "cyanBright",
|
|
696
|
+
bold: true
|
|
697
|
+
}),
|
|
698
|
+
codeBlock: freezeStyle({ fg: "yellowBright" }),
|
|
699
|
+
thematicBreak: freezeStyle({ dim: true }),
|
|
700
|
+
html: freezeStyle({ dim: true })
|
|
701
|
+
});
|
|
702
|
+
function stableSerialize(value) {
|
|
703
|
+
if (value == null) return "null";
|
|
704
|
+
if (typeof value !== "object") return JSON.stringify(value);
|
|
705
|
+
if (Array.isArray(value)) return `[${value.map((item) => stableSerialize(item)).join(",")}]`;
|
|
706
|
+
const entries = Object.entries(value).sort(([a], [b]) => a.localeCompare(b));
|
|
707
|
+
return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${stableSerialize(item)}`).join(",")}}`;
|
|
708
|
+
}
|
|
709
|
+
function markdownThemeSignature(overrides) {
|
|
710
|
+
return stableSerialize(overrides ?? null);
|
|
711
|
+
}
|
|
712
|
+
function resolveTuiMarkdownTheme(overrides) {
|
|
713
|
+
if (!overrides) return DEFAULT_TUI_MARKDOWN_THEME;
|
|
714
|
+
const heading = DEFAULT_TUI_MARKDOWN_THEME.heading.map((style, index) => mergeFrozenStyle(style, overrides.heading?.[index]));
|
|
715
|
+
return Object.freeze({
|
|
716
|
+
heading: Object.freeze(heading),
|
|
717
|
+
strong: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.strong, overrides.strong),
|
|
718
|
+
emphasis: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.emphasis, overrides.emphasis),
|
|
719
|
+
strikethrough: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.strikethrough, overrides.strikethrough),
|
|
720
|
+
inlineCode: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.inlineCode, overrides.inlineCode),
|
|
721
|
+
link: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.link, overrides.link),
|
|
722
|
+
blockquote: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.blockquote, overrides.blockquote),
|
|
723
|
+
listMarker: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.listMarker, overrides.listMarker),
|
|
724
|
+
codeBlock: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.codeBlock, overrides.codeBlock),
|
|
725
|
+
thematicBreak: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.thematicBreak, overrides.thematicBreak),
|
|
726
|
+
html: mergeFrozenStyle(DEFAULT_TUI_MARKDOWN_THEME.html, overrides.html)
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/vue/markdown/document.ts
|
|
732
|
+
/**
|
|
733
|
+
* This helper always builds visual rows from the full source string. Components
|
|
734
|
+
* that need streaming reuse can call buildMarkdownBlocks and cache block layout.
|
|
735
|
+
*/
|
|
736
|
+
function buildMarkdownBlocks(content, parser, options) {
|
|
737
|
+
const theme = resolveTuiMarkdownTheme(options?.theme);
|
|
738
|
+
try {
|
|
739
|
+
const nodes = parser.parse(content, options?.final ?? true);
|
|
740
|
+
const blocks = markdownAstToBlocks(nodes, theme);
|
|
741
|
+
return {
|
|
742
|
+
nodes,
|
|
743
|
+
blocks
|
|
744
|
+
};
|
|
745
|
+
} catch (error) {
|
|
746
|
+
console.warn("[vue-tui] Markdown parse failed; falling back to plain text rendering.", error);
|
|
747
|
+
return {
|
|
748
|
+
nodes: [],
|
|
749
|
+
blocks: plainTextFallbackBlocks(content)
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
function buildMarkdownVisualRows(content, width, parser, options) {
|
|
754
|
+
const { blocks } = buildMarkdownBlocks(content, parser, options);
|
|
755
|
+
return layoutMarkdownBlocks(blocks, width);
|
|
756
|
+
}
|
|
757
|
+
function plainTextFallbackBlocks(content) {
|
|
758
|
+
const normalized = sanitizeTextBlock(content);
|
|
759
|
+
const lines = normalized.split("\n");
|
|
760
|
+
const blocks = [];
|
|
761
|
+
for (let index = 0; index < lines.length; index++) {
|
|
762
|
+
const line = sanitizeInlineText(lines[index] ?? "");
|
|
763
|
+
if (!line) {
|
|
764
|
+
blocks.push({
|
|
765
|
+
type: "blank",
|
|
766
|
+
key: `md-fallback:${index}`
|
|
767
|
+
});
|
|
768
|
+
continue;
|
|
769
|
+
}
|
|
770
|
+
blocks.push({
|
|
771
|
+
type: "inline",
|
|
772
|
+
key: `md-fallback:${index}`,
|
|
773
|
+
segments: [{ text: line }]
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
return blocks.length ? blocks : [{
|
|
777
|
+
type: "blank",
|
|
778
|
+
key: "md-fallback:0"
|
|
779
|
+
}];
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
//#endregion
|
|
783
|
+
//#region src/vue/markdown/render.ts
|
|
784
|
+
const mergedStyleCache = new WeakMap();
|
|
785
|
+
function mergeStyle(base, overlay) {
|
|
786
|
+
if (!overlay) return base;
|
|
787
|
+
let bucket = mergedStyleCache.get(base);
|
|
788
|
+
if (!bucket) {
|
|
789
|
+
bucket = new WeakMap();
|
|
790
|
+
mergedStyleCache.set(base, bucket);
|
|
791
|
+
}
|
|
792
|
+
const cached = bucket.get(overlay);
|
|
793
|
+
if (cached) return cached;
|
|
794
|
+
const merged = Object.freeze({
|
|
795
|
+
...base,
|
|
796
|
+
...overlay
|
|
797
|
+
});
|
|
798
|
+
bucket.set(overlay, merged);
|
|
799
|
+
return merged;
|
|
800
|
+
}
|
|
801
|
+
function paintMarkdownVisualRow(terminal, row, options) {
|
|
802
|
+
const clipStart = Math.max(0, Math.floor(options.clipStart ?? 0));
|
|
803
|
+
const clipEnd = clipStart + options.w;
|
|
804
|
+
if (!row) {
|
|
805
|
+
if (options.clear !== false) terminal.write(spaces(options.w), {
|
|
806
|
+
x: options.x,
|
|
807
|
+
y: options.y,
|
|
808
|
+
style: options.baseStyle
|
|
809
|
+
});
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
let logicalX = 0;
|
|
813
|
+
let used = 0;
|
|
814
|
+
for (const segment of row.segments) {
|
|
815
|
+
if (used >= options.w || !segment.text) {
|
|
816
|
+
logicalX += segment.cells;
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
const segmentStart = logicalX;
|
|
820
|
+
const segmentEnd = logicalX + segment.cells;
|
|
821
|
+
logicalX += segment.cells;
|
|
822
|
+
if (segmentEnd <= clipStart || segmentStart >= clipEnd) continue;
|
|
823
|
+
const segmentStyle = mergeStyle(options.baseStyle, segment.style);
|
|
824
|
+
let pieceStart = segmentStart;
|
|
825
|
+
forEachTextCellSegment(segment.text, (piece) => {
|
|
826
|
+
if (used >= options.w) return false;
|
|
827
|
+
const pieceEnd = pieceStart + piece.cells;
|
|
828
|
+
const visibleStart = Math.max(pieceStart, clipStart);
|
|
829
|
+
const visibleEnd = Math.min(pieceEnd, clipEnd);
|
|
830
|
+
pieceStart = pieceEnd;
|
|
831
|
+
if (visibleEnd <= visibleStart) return;
|
|
832
|
+
const expectedUsed = visibleStart - clipStart;
|
|
833
|
+
if (expectedUsed > used) {
|
|
834
|
+
const pad$1 = Math.min(expectedUsed - used, options.w - used);
|
|
835
|
+
terminal.write(spaces(pad$1), {
|
|
836
|
+
x: options.x + used,
|
|
837
|
+
y: options.y,
|
|
838
|
+
style: options.baseStyle
|
|
839
|
+
});
|
|
840
|
+
used += pad$1;
|
|
841
|
+
}
|
|
842
|
+
if (visibleStart === pieceStart - piece.cells && visibleEnd === pieceEnd) {
|
|
843
|
+
terminal.write(piece.text, {
|
|
844
|
+
x: options.x + used,
|
|
845
|
+
y: options.y,
|
|
846
|
+
style: segmentStyle
|
|
847
|
+
});
|
|
848
|
+
used += piece.cells;
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const pad = Math.min(visibleEnd - visibleStart, options.w - used);
|
|
852
|
+
if (pad > 0) {
|
|
853
|
+
terminal.write(spaces(pad), {
|
|
854
|
+
x: options.x + used,
|
|
855
|
+
y: options.y,
|
|
856
|
+
style: segmentStyle
|
|
857
|
+
});
|
|
858
|
+
used += pad;
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
if (used < options.w && options.clear !== false) terminal.write(spaces(options.w - used), {
|
|
863
|
+
x: options.x + used,
|
|
864
|
+
y: options.y,
|
|
865
|
+
style: options.baseStyle
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
//#endregion
|
|
870
|
+
//#region src/vue/components/TMarkdownText.ts
|
|
871
|
+
const TMarkdownText = defineComponent({
|
|
872
|
+
name: "TMarkdownText",
|
|
873
|
+
props: {
|
|
874
|
+
x: {
|
|
875
|
+
type: Number,
|
|
876
|
+
required: true
|
|
877
|
+
},
|
|
878
|
+
y: {
|
|
879
|
+
type: Number,
|
|
880
|
+
required: true
|
|
881
|
+
},
|
|
882
|
+
zIndex: {
|
|
883
|
+
type: Number,
|
|
884
|
+
default: 0
|
|
885
|
+
},
|
|
886
|
+
content: {
|
|
887
|
+
type: String,
|
|
888
|
+
required: true
|
|
889
|
+
},
|
|
890
|
+
w: {
|
|
891
|
+
type: Number,
|
|
892
|
+
required: true
|
|
893
|
+
},
|
|
894
|
+
h: {
|
|
895
|
+
type: Number,
|
|
896
|
+
default: void 0
|
|
897
|
+
},
|
|
898
|
+
style: {
|
|
899
|
+
type: Object,
|
|
900
|
+
default: void 0
|
|
901
|
+
},
|
|
902
|
+
final: {
|
|
903
|
+
type: Boolean,
|
|
904
|
+
default: true
|
|
905
|
+
},
|
|
906
|
+
streaming: {
|
|
907
|
+
type: Boolean,
|
|
908
|
+
default: false
|
|
909
|
+
},
|
|
910
|
+
clear: {
|
|
911
|
+
type: Boolean,
|
|
912
|
+
default: true
|
|
913
|
+
},
|
|
914
|
+
customHtmlTags: {
|
|
915
|
+
type: Array,
|
|
916
|
+
default: void 0
|
|
917
|
+
},
|
|
918
|
+
theme: {
|
|
919
|
+
type: Object,
|
|
920
|
+
default: void 0
|
|
921
|
+
}
|
|
922
|
+
},
|
|
923
|
+
setup(props) {
|
|
924
|
+
const instance = getCurrentInstance();
|
|
925
|
+
const { terminal, defaultStyle, scheduler } = useTerminal();
|
|
926
|
+
const layout = useLayout();
|
|
927
|
+
const { visible, rootProps } = useVisibility();
|
|
928
|
+
const rows = shallowRef(markRaw([]));
|
|
929
|
+
const documentVersion = shallowRef(0);
|
|
930
|
+
let builtOnce = false;
|
|
931
|
+
let rebuildVersion = 0;
|
|
932
|
+
let alive = true;
|
|
933
|
+
const parser = shallowRef(markRaw(createTuiMarkdownParser({
|
|
934
|
+
streaming: props.streaming,
|
|
935
|
+
customHtmlTags: props.customHtmlTags
|
|
936
|
+
})));
|
|
937
|
+
watch(() => `${props.streaming ? 1 : 0}:${(props.customHtmlTags ?? []).join("\0")}`, () => {
|
|
938
|
+
parser.value = markRaw(createTuiMarkdownParser({
|
|
939
|
+
streaming: props.streaming,
|
|
940
|
+
customHtmlTags: props.customHtmlTags
|
|
941
|
+
}));
|
|
942
|
+
});
|
|
943
|
+
function rebuildRows() {
|
|
944
|
+
rows.value = markRaw(buildMarkdownVisualRows(props.content, props.w, parser.value, {
|
|
945
|
+
final: props.final,
|
|
946
|
+
theme: props.theme
|
|
947
|
+
}));
|
|
948
|
+
documentVersion.value++;
|
|
949
|
+
}
|
|
950
|
+
function scheduleRebuild() {
|
|
951
|
+
const currentVersion = ++rebuildVersion;
|
|
952
|
+
if (!builtOnce || !props.streaming) {
|
|
953
|
+
builtOnce = true;
|
|
954
|
+
rebuildRows();
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
scheduler.queueFrameTask({
|
|
958
|
+
id: `TMarkdownText:${instance?.uid ?? "unknown"}:markdown`,
|
|
959
|
+
reason: props.streaming ? "stream" : "data",
|
|
960
|
+
priority: props.streaming ? "low" : "normal",
|
|
961
|
+
sync: false,
|
|
962
|
+
run: () => {
|
|
963
|
+
if (!alive) return;
|
|
964
|
+
if (currentVersion !== rebuildVersion) return;
|
|
965
|
+
rebuildRows();
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
watch([
|
|
970
|
+
() => props.content,
|
|
971
|
+
() => props.w,
|
|
972
|
+
() => parser.value,
|
|
973
|
+
() => props.final,
|
|
974
|
+
() => markdownThemeSignature(props.theme)
|
|
975
|
+
], () => {
|
|
976
|
+
scheduleRebuild();
|
|
977
|
+
}, { immediate: true });
|
|
978
|
+
onBeforeUnmount(() => {
|
|
979
|
+
alive = false;
|
|
980
|
+
rebuildVersion++;
|
|
981
|
+
});
|
|
982
|
+
const fullRect = computed(() => {
|
|
983
|
+
const height = props.h ?? Math.max(1, rows.value.length);
|
|
984
|
+
return translateRect({
|
|
985
|
+
x: props.x,
|
|
986
|
+
y: props.y,
|
|
987
|
+
w: props.w,
|
|
988
|
+
h: height
|
|
989
|
+
}, layout.originX, layout.originY);
|
|
990
|
+
});
|
|
991
|
+
const absRect = computed(() => {
|
|
992
|
+
const translated = fullRect.value;
|
|
993
|
+
if (!layout.clipRect) return translated;
|
|
994
|
+
return intersectRect(translated, layout.clipRect) ?? {
|
|
995
|
+
x: 0,
|
|
996
|
+
y: 0,
|
|
997
|
+
w: 0,
|
|
998
|
+
h: 0
|
|
999
|
+
};
|
|
1000
|
+
});
|
|
1001
|
+
useRenderNode(() => ({
|
|
1002
|
+
zIndex: props.zIndex,
|
|
1003
|
+
rect: visible.value ? absRect.value : {
|
|
1004
|
+
x: 0,
|
|
1005
|
+
y: 0,
|
|
1006
|
+
w: 0,
|
|
1007
|
+
h: 0
|
|
1008
|
+
},
|
|
1009
|
+
deps: [
|
|
1010
|
+
visible.value,
|
|
1011
|
+
absRect.value,
|
|
1012
|
+
fullRect.value,
|
|
1013
|
+
props.style,
|
|
1014
|
+
props.clear,
|
|
1015
|
+
documentVersion.value,
|
|
1016
|
+
defaultStyle.value
|
|
1017
|
+
],
|
|
1018
|
+
paint: (dirtyRows) => {
|
|
1019
|
+
if (!visible.value) return;
|
|
1020
|
+
const r = absRect.value;
|
|
1021
|
+
const full = fullRect.value;
|
|
1022
|
+
if (r.w <= 0 || r.h <= 0) return;
|
|
1023
|
+
const baseStyle = props.style ?? defaultStyle.value;
|
|
1024
|
+
const clipStart = Math.max(0, Math.floor(r.x - full.x));
|
|
1025
|
+
const paintRow = (y) => {
|
|
1026
|
+
if (y < r.y || y >= r.y + r.h) return;
|
|
1027
|
+
const rowIndex = Math.floor(y - full.y);
|
|
1028
|
+
const row = rows.value[rowIndex];
|
|
1029
|
+
if (!row && !props.clear) return;
|
|
1030
|
+
paintMarkdownVisualRow(terminal, row, {
|
|
1031
|
+
x: r.x,
|
|
1032
|
+
y,
|
|
1033
|
+
w: r.w,
|
|
1034
|
+
clipStart,
|
|
1035
|
+
baseStyle,
|
|
1036
|
+
clear: props.clear
|
|
1037
|
+
});
|
|
1038
|
+
};
|
|
1039
|
+
if (dirtyRows?.length) {
|
|
1040
|
+
for (const y of dirtyRows) paintRow(y);
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
for (let y = r.y; y < r.y + r.h; y++) paintRow(y);
|
|
1044
|
+
}
|
|
1045
|
+
}));
|
|
1046
|
+
return () => h("span", rootProps);
|
|
1047
|
+
}
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
//#endregion
|
|
1051
|
+
//#region src/vue/components/TVirtualMarkdown.ts
|
|
1052
|
+
function clamp(n, min, max) {
|
|
1053
|
+
return Math.max(min, Math.min(max, n));
|
|
1054
|
+
}
|
|
1055
|
+
function markdownStyleSignature(style) {
|
|
1056
|
+
if (!style) return "";
|
|
1057
|
+
return [
|
|
1058
|
+
style.fg ?? "",
|
|
1059
|
+
style.bg ?? "",
|
|
1060
|
+
style.bold ? "1" : "0",
|
|
1061
|
+
style.dim ? "1" : "0",
|
|
1062
|
+
style.italic ? "1" : "0",
|
|
1063
|
+
style.underline ? "1" : "0",
|
|
1064
|
+
style.inverse ? "1" : "0",
|
|
1065
|
+
style.href ?? ""
|
|
1066
|
+
].join("");
|
|
1067
|
+
}
|
|
1068
|
+
function markdownRowSignature(row) {
|
|
1069
|
+
if (!row) return "";
|
|
1070
|
+
return [
|
|
1071
|
+
row.key,
|
|
1072
|
+
row.plainText,
|
|
1073
|
+
row.segments.map((segment) => `${segment.text}\u0001${segment.cells}\u0001${markdownStyleSignature(segment.style)}`).join("")
|
|
1074
|
+
].join("");
|
|
1075
|
+
}
|
|
1076
|
+
function getWheelScrollInput(e) {
|
|
1077
|
+
const deltaY = Number(e.deltaY ?? 0);
|
|
1078
|
+
const deltaMode = typeof e.deltaMode === "number" ? e.deltaMode : void 0;
|
|
1079
|
+
if (Number.isInteger(deltaY) && deltaY !== 0 && Math.abs(deltaY) >= 100 && Math.abs(deltaY) % 100 === 0 && deltaMode == null) return {
|
|
1080
|
+
deltaY: deltaY / 100,
|
|
1081
|
+
mode: "line"
|
|
1082
|
+
};
|
|
1083
|
+
if (deltaMode === 1) return {
|
|
1084
|
+
deltaY,
|
|
1085
|
+
mode: "line"
|
|
1086
|
+
};
|
|
1087
|
+
if (deltaMode === 0) return {
|
|
1088
|
+
deltaY,
|
|
1089
|
+
mode: "pixel"
|
|
1090
|
+
};
|
|
1091
|
+
return {
|
|
1092
|
+
deltaY,
|
|
1093
|
+
mode: "auto"
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
const TVirtualMarkdown = defineComponent({
|
|
1097
|
+
name: "TVirtualMarkdown",
|
|
1098
|
+
props: {
|
|
1099
|
+
x: {
|
|
1100
|
+
type: Number,
|
|
1101
|
+
required: true
|
|
1102
|
+
},
|
|
1103
|
+
y: {
|
|
1104
|
+
type: Number,
|
|
1105
|
+
required: true
|
|
1106
|
+
},
|
|
1107
|
+
w: {
|
|
1108
|
+
type: Number,
|
|
1109
|
+
required: true
|
|
1110
|
+
},
|
|
1111
|
+
h: {
|
|
1112
|
+
type: Number,
|
|
1113
|
+
required: true
|
|
1114
|
+
},
|
|
1115
|
+
zIndex: {
|
|
1116
|
+
type: Number,
|
|
1117
|
+
default: 0
|
|
1118
|
+
},
|
|
1119
|
+
content: {
|
|
1120
|
+
type: String,
|
|
1121
|
+
default: ""
|
|
1122
|
+
},
|
|
1123
|
+
blocks: {
|
|
1124
|
+
type: Array,
|
|
1125
|
+
default: void 0
|
|
1126
|
+
},
|
|
1127
|
+
scrollTop: {
|
|
1128
|
+
type: Number,
|
|
1129
|
+
default: 0
|
|
1130
|
+
},
|
|
1131
|
+
style: {
|
|
1132
|
+
type: Object,
|
|
1133
|
+
default: void 0
|
|
1134
|
+
},
|
|
1135
|
+
final: {
|
|
1136
|
+
type: Boolean,
|
|
1137
|
+
default: true
|
|
1138
|
+
},
|
|
1139
|
+
streaming: {
|
|
1140
|
+
type: Boolean,
|
|
1141
|
+
default: false
|
|
1142
|
+
},
|
|
1143
|
+
autoFocus: {
|
|
1144
|
+
type: Boolean,
|
|
1145
|
+
default: false
|
|
1146
|
+
},
|
|
1147
|
+
selectable: {
|
|
1148
|
+
type: Boolean,
|
|
1149
|
+
default: true
|
|
1150
|
+
},
|
|
1151
|
+
customHtmlTags: {
|
|
1152
|
+
type: Array,
|
|
1153
|
+
default: void 0
|
|
1154
|
+
},
|
|
1155
|
+
theme: {
|
|
1156
|
+
type: Object,
|
|
1157
|
+
default: void 0
|
|
1158
|
+
}
|
|
1159
|
+
},
|
|
1160
|
+
emits: [
|
|
1161
|
+
"update:scrollTop",
|
|
1162
|
+
"scroll",
|
|
1163
|
+
"focus",
|
|
1164
|
+
"blur",
|
|
1165
|
+
"keydown"
|
|
1166
|
+
],
|
|
1167
|
+
setup(props, { emit }) {
|
|
1168
|
+
const instance = getCurrentInstance();
|
|
1169
|
+
const { terminal, defaultStyle, events, scheduler, selection } = useTerminal();
|
|
1170
|
+
const layout = useLayout();
|
|
1171
|
+
const { visible, rootProps } = useVisibility();
|
|
1172
|
+
const parentEventZ = inject(EventZIndexContextKey, computed(() => 0));
|
|
1173
|
+
const eventZ = computed(() => (parentEventZ.value ?? 0) + (props.zIndex ?? 0));
|
|
1174
|
+
const internalScrollTop = ref(0);
|
|
1175
|
+
const documentVersion = ref(0);
|
|
1176
|
+
const rows = shallowRef(markRaw([]));
|
|
1177
|
+
let pendingSelectionScrollFocusRemap = false;
|
|
1178
|
+
const wheelState = createWheelScrollState();
|
|
1179
|
+
let builtOnce = false;
|
|
1180
|
+
let rebuildVersion = 0;
|
|
1181
|
+
let layoutCache;
|
|
1182
|
+
let alive = true;
|
|
1183
|
+
const parser = shallowRef(markRaw(createTuiMarkdownParser({
|
|
1184
|
+
streaming: props.streaming,
|
|
1185
|
+
customHtmlTags: props.customHtmlTags
|
|
1186
|
+
})));
|
|
1187
|
+
watch(() => `${props.streaming ? 1 : 0}:${(props.customHtmlTags ?? []).join("\0")}`, () => {
|
|
1188
|
+
parser.value = markRaw(createTuiMarkdownParser({
|
|
1189
|
+
streaming: props.streaming,
|
|
1190
|
+
customHtmlTags: props.customHtmlTags
|
|
1191
|
+
}));
|
|
1192
|
+
});
|
|
1193
|
+
function rebuildRows() {
|
|
1194
|
+
const prevScrollTop = internalScrollTop.value;
|
|
1195
|
+
const prevVisibleRows = visibleRowSignatures(rows.value, prevScrollTop);
|
|
1196
|
+
const blocks = props.blocks ?? buildMarkdownBlocks(props.content, parser.value, {
|
|
1197
|
+
final: props.final,
|
|
1198
|
+
theme: props.theme
|
|
1199
|
+
}).blocks;
|
|
1200
|
+
const nextLayout = layoutMarkdownBlocksCached(blocks, props.w, layoutCache);
|
|
1201
|
+
layoutCache = markRaw(nextLayout.cache);
|
|
1202
|
+
const nextRows = nextLayout.rows;
|
|
1203
|
+
rows.value = markRaw(nextRows);
|
|
1204
|
+
reconcileScrollTop();
|
|
1205
|
+
const nextScrollTop = internalScrollTop.value;
|
|
1206
|
+
const nextVisibleRows = visibleRowSignatures(nextRows, nextScrollTop);
|
|
1207
|
+
const visibleChanged = prevVisibleRows.length !== nextVisibleRows.length || prevVisibleRows.some((row, index) => row !== nextVisibleRows[index]);
|
|
1208
|
+
if (!builtOnce || prevScrollTop !== nextScrollTop || visibleChanged) {
|
|
1209
|
+
documentVersion.value++;
|
|
1210
|
+
selection.refresh();
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
function scheduleRebuild() {
|
|
1214
|
+
const currentVersion = ++rebuildVersion;
|
|
1215
|
+
if (!builtOnce || !props.streaming) {
|
|
1216
|
+
builtOnce = true;
|
|
1217
|
+
rebuildRows();
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
scheduler.queueFrameTask({
|
|
1221
|
+
id: `TVirtualMarkdown:${instance?.uid ?? "unknown"}:markdown`,
|
|
1222
|
+
reason: props.streaming ? "stream" : "data",
|
|
1223
|
+
priority: props.streaming ? "low" : "normal",
|
|
1224
|
+
sync: false,
|
|
1225
|
+
run: () => {
|
|
1226
|
+
if (!alive) return;
|
|
1227
|
+
if (currentVersion !== rebuildVersion) return;
|
|
1228
|
+
rebuildRows();
|
|
1229
|
+
}
|
|
1230
|
+
});
|
|
1231
|
+
}
|
|
1232
|
+
const fullRect = computed(() => translateRect({
|
|
1233
|
+
x: props.x,
|
|
1234
|
+
y: props.y,
|
|
1235
|
+
w: props.w,
|
|
1236
|
+
h: props.h
|
|
1237
|
+
}, layout.originX, layout.originY));
|
|
1238
|
+
const absRect = computed(() => {
|
|
1239
|
+
const translated = fullRect.value;
|
|
1240
|
+
if (!layout.clipRect) return translated;
|
|
1241
|
+
return intersectRect(translated, layout.clipRect) ?? {
|
|
1242
|
+
x: 0,
|
|
1243
|
+
y: 0,
|
|
1244
|
+
w: 0,
|
|
1245
|
+
h: 0
|
|
1246
|
+
};
|
|
1247
|
+
});
|
|
1248
|
+
function normalizedRect() {
|
|
1249
|
+
return normalizeCellRect(absRect.value);
|
|
1250
|
+
}
|
|
1251
|
+
function normalizedFullRect() {
|
|
1252
|
+
return normalizeCellRect(fullRect.value);
|
|
1253
|
+
}
|
|
1254
|
+
function clipOffsets() {
|
|
1255
|
+
const full = normalizedFullRect();
|
|
1256
|
+
const clip = normalizedRect();
|
|
1257
|
+
return {
|
|
1258
|
+
x: Math.max(0, clip.x - full.x),
|
|
1259
|
+
y: Math.max(0, clip.y - full.y)
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
function maxScrollTop() {
|
|
1263
|
+
const clip = normalizedRect();
|
|
1264
|
+
const { y: clipY } = clipOffsets();
|
|
1265
|
+
return Math.max(0, rows.value.length - (clipY + clip.h));
|
|
1266
|
+
}
|
|
1267
|
+
function visibleRowSignatures(sourceRows, scrollTop) {
|
|
1268
|
+
const clip = normalizedRect();
|
|
1269
|
+
const { y: clipY } = clipOffsets();
|
|
1270
|
+
const start = Math.max(0, scrollTop + clipY);
|
|
1271
|
+
const end = Math.max(start, start + clip.h);
|
|
1272
|
+
const out = [];
|
|
1273
|
+
for (let index = start; index < end; index++) out.push(markdownRowSignature(sourceRows[index]));
|
|
1274
|
+
return out;
|
|
1275
|
+
}
|
|
1276
|
+
function hasControlledScrollTop() {
|
|
1277
|
+
return Object.prototype.hasOwnProperty.call(instance?.vnode.props ?? {}, "scrollTop");
|
|
1278
|
+
}
|
|
1279
|
+
function applyControlledScrollTop(next, remapSelectionFocus = false) {
|
|
1280
|
+
const desired = Math.floor(Number(next) || 0);
|
|
1281
|
+
const clamped = clamp(desired, 0, maxScrollTop());
|
|
1282
|
+
const changed = internalScrollTop.value !== clamped;
|
|
1283
|
+
if (changed) internalScrollTop.value = clamped;
|
|
1284
|
+
if (desired !== clamped) {
|
|
1285
|
+
emit("update:scrollTop", clamped);
|
|
1286
|
+
emit("scroll", clamped);
|
|
1287
|
+
}
|
|
1288
|
+
if (changed || remapSelectionFocus) selection.refresh(remapSelectionFocus ? { remapFocus: true } : void 0);
|
|
1289
|
+
}
|
|
1290
|
+
function setScrollTop(next, emitChange = true, refreshOptions) {
|
|
1291
|
+
const desired = Math.floor(Number(next) || 0);
|
|
1292
|
+
const clamped = clamp(desired, 0, maxScrollTop());
|
|
1293
|
+
const changed = internalScrollTop.value !== clamped;
|
|
1294
|
+
if (changed) internalScrollTop.value = clamped;
|
|
1295
|
+
if (emitChange && (changed || refreshOptions?.emitClampEvenIfUnchanged && desired !== clamped)) {
|
|
1296
|
+
emit("update:scrollTop", clamped);
|
|
1297
|
+
emit("scroll", clamped);
|
|
1298
|
+
}
|
|
1299
|
+
if (changed || refreshOptions?.remapSelectionFocus) selection.refresh(refreshOptions?.remapSelectionFocus ? { remapFocus: true } : void 0);
|
|
1300
|
+
}
|
|
1301
|
+
function reconcileScrollTop() {
|
|
1302
|
+
const desired = hasControlledScrollTop() ? Math.floor(Number(props.scrollTop) || 0) : internalScrollTop.value;
|
|
1303
|
+
const clamped = clamp(desired, 0, maxScrollTop());
|
|
1304
|
+
if (internalScrollTop.value === clamped) return;
|
|
1305
|
+
internalScrollTop.value = clamped;
|
|
1306
|
+
if (desired !== clamped) {
|
|
1307
|
+
emit("update:scrollTop", clamped);
|
|
1308
|
+
emit("scroll", clamped);
|
|
1309
|
+
}
|
|
1310
|
+
selection.refresh();
|
|
1311
|
+
}
|
|
1312
|
+
watch(() => props.scrollTop, () => {
|
|
1313
|
+
if (!hasControlledScrollTop()) return;
|
|
1314
|
+
const remap = pendingSelectionScrollFocusRemap;
|
|
1315
|
+
pendingSelectionScrollFocusRemap = false;
|
|
1316
|
+
applyControlledScrollTop(props.scrollTop, remap);
|
|
1317
|
+
});
|
|
1318
|
+
watch([
|
|
1319
|
+
() => props.content,
|
|
1320
|
+
() => props.blocks,
|
|
1321
|
+
() => props.w,
|
|
1322
|
+
() => parser.value,
|
|
1323
|
+
() => props.final,
|
|
1324
|
+
() => markdownThemeSignature(props.theme)
|
|
1325
|
+
], () => {
|
|
1326
|
+
scheduleRebuild();
|
|
1327
|
+
}, { immediate: true });
|
|
1328
|
+
watch([
|
|
1329
|
+
() => props.content,
|
|
1330
|
+
() => props.w,
|
|
1331
|
+
() => props.h,
|
|
1332
|
+
() => absRect.value.h
|
|
1333
|
+
], () => {
|
|
1334
|
+
resetWheelScrollState(wheelState);
|
|
1335
|
+
});
|
|
1336
|
+
watch([
|
|
1337
|
+
() => props.h,
|
|
1338
|
+
() => absRect.value.y,
|
|
1339
|
+
() => absRect.value.h,
|
|
1340
|
+
() => fullRect.value.y,
|
|
1341
|
+
() => fullRect.value.h
|
|
1342
|
+
], () => {
|
|
1343
|
+
reconcileScrollTop();
|
|
1344
|
+
}, { immediate: true });
|
|
1345
|
+
function scrollSelectionBy(delta) {
|
|
1346
|
+
const n = Math.trunc(Number(delta));
|
|
1347
|
+
if (!Number.isFinite(n) || n === 0) return false;
|
|
1348
|
+
if (hasControlledScrollTop()) {
|
|
1349
|
+
const nextTop = clamp(internalScrollTop.value + n, 0, maxScrollTop());
|
|
1350
|
+
if (nextTop === internalScrollTop.value) return false;
|
|
1351
|
+
if (pendingSelectionScrollFocusRemap) return false;
|
|
1352
|
+
pendingSelectionScrollFocusRemap = true;
|
|
1353
|
+
emit("update:scrollTop", nextTop);
|
|
1354
|
+
emit("scroll", nextTop);
|
|
1355
|
+
return true;
|
|
1356
|
+
}
|
|
1357
|
+
const before = internalScrollTop.value;
|
|
1358
|
+
setScrollTop(before + n, true, { remapSelectionFocus: true });
|
|
1359
|
+
return internalScrollTop.value !== before;
|
|
1360
|
+
}
|
|
1361
|
+
function selectionPointForCell(point) {
|
|
1362
|
+
const r = normalizedRect();
|
|
1363
|
+
if (point.x < r.x || point.y < r.y || point.x >= r.x + r.w || point.y >= r.y + r.h) return null;
|
|
1364
|
+
const { x: clipX, y: clipY } = clipOffsets();
|
|
1365
|
+
const visualY = internalScrollTop.value + clipY + (point.y - r.y);
|
|
1366
|
+
if (visualY < 0 || visualY >= rows.value.length) return null;
|
|
1367
|
+
return {
|
|
1368
|
+
x: clamp(clipX + (point.x - r.x), 0, Math.max(0, props.w - 1)),
|
|
1369
|
+
y: visualY
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
function canHandleSelectionRange(range) {
|
|
1373
|
+
return Boolean(selectionPointForCell(range.anchor) && selectionPointForCell(range.focus));
|
|
1374
|
+
}
|
|
1375
|
+
function textForSelectionRange(range) {
|
|
1376
|
+
const cols = Math.max(1, Math.floor(props.w));
|
|
1377
|
+
return terminalSelectionRowSpans(range, cols, rows.value.length).map((span) => {
|
|
1378
|
+
const text = sliceByCellsRange(rows.value[span.y]?.plainText ?? "", span.x0, span.x1);
|
|
1379
|
+
return span.x1 >= cols ? text.trimEnd() : text;
|
|
1380
|
+
}).join("\n");
|
|
1381
|
+
}
|
|
1382
|
+
function visibleSpansForSelectionRange(providerRange, _screenRange) {
|
|
1383
|
+
const r = normalizedRect();
|
|
1384
|
+
const { x: clipX, y: clipY } = clipOffsets();
|
|
1385
|
+
const cols = Math.max(1, Math.floor(props.w));
|
|
1386
|
+
const top = internalScrollTop.value + clipY;
|
|
1387
|
+
const bottom = top + r.h;
|
|
1388
|
+
const providerSpans = terminalSelectionVisibleRowSpans(providerRange, cols, rows.value.length, top, bottom);
|
|
1389
|
+
const result = [];
|
|
1390
|
+
for (const span of providerSpans) {
|
|
1391
|
+
const screenY = r.y + (span.y - top);
|
|
1392
|
+
const screenX0 = r.x + span.x0 - clipX;
|
|
1393
|
+
const screenX1 = r.x + span.x1 - clipX;
|
|
1394
|
+
const x0 = Math.max(r.x, screenX0);
|
|
1395
|
+
const x1 = Math.min(r.x + r.w, screenX1);
|
|
1396
|
+
if (screenY >= r.y && screenY < r.y + r.h && x1 > x0) result.push({
|
|
1397
|
+
y: screenY,
|
|
1398
|
+
x0,
|
|
1399
|
+
x1
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1402
|
+
return result;
|
|
1403
|
+
}
|
|
1404
|
+
function onKeydown(event) {
|
|
1405
|
+
emit("keydown", event);
|
|
1406
|
+
const page = Math.max(1, normalizedRect().h);
|
|
1407
|
+
if (event.key === "ArrowUp") {
|
|
1408
|
+
event.preventDefault();
|
|
1409
|
+
setScrollTop(internalScrollTop.value - 1);
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
if (event.key === "ArrowDown") {
|
|
1413
|
+
event.preventDefault();
|
|
1414
|
+
setScrollTop(internalScrollTop.value + 1);
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
if (event.key === "PageUp") {
|
|
1418
|
+
event.preventDefault();
|
|
1419
|
+
setScrollTop(internalScrollTop.value - page);
|
|
1420
|
+
return;
|
|
1421
|
+
}
|
|
1422
|
+
if (event.key === "PageDown") {
|
|
1423
|
+
event.preventDefault();
|
|
1424
|
+
setScrollTop(internalScrollTop.value + page);
|
|
1425
|
+
return;
|
|
1426
|
+
}
|
|
1427
|
+
if (event.key === "Home") {
|
|
1428
|
+
event.preventDefault();
|
|
1429
|
+
setScrollTop(0);
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
if (event.key === "End") {
|
|
1433
|
+
event.preventDefault();
|
|
1434
|
+
setScrollTop(maxScrollTop());
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
const selectionTextProvider = {
|
|
1438
|
+
id: `TVirtualMarkdown:${instance?.uid ?? "unknown"}:selection-text`,
|
|
1439
|
+
get rect() {
|
|
1440
|
+
return normalizedRect();
|
|
1441
|
+
},
|
|
1442
|
+
canHandle: canHandleSelectionRange,
|
|
1443
|
+
pointForCell: selectionPointForCell,
|
|
1444
|
+
getText: textForSelectionRange,
|
|
1445
|
+
getVisibleSpans: visibleSpansForSelectionRange
|
|
1446
|
+
};
|
|
1447
|
+
const unregisterSelectionTextProvider = selection.registerTextProvider(selectionTextProvider);
|
|
1448
|
+
onBeforeUnmount(unregisterSelectionTextProvider);
|
|
1449
|
+
const eventNode = useTerminalNode(() => ({
|
|
1450
|
+
rect: normalizedRect(),
|
|
1451
|
+
zIndex: eventZ.value,
|
|
1452
|
+
visible: visible.value,
|
|
1453
|
+
focusable: true,
|
|
1454
|
+
selectable: props.selectable,
|
|
1455
|
+
selectionScrollBy: scrollSelectionBy,
|
|
1456
|
+
handlers: {
|
|
1457
|
+
click: (_event) => {},
|
|
1458
|
+
wheel: (event) => {
|
|
1459
|
+
const { deltaY, mode } = getWheelScrollInput(event);
|
|
1460
|
+
if (!deltaY) return;
|
|
1461
|
+
const now = typeof event.time === "number" ? event.time : typeof event.timeStamp === "number" ? event.timeStamp : Date.now();
|
|
1462
|
+
const { nextTop, dir } = applyWheelScroll(wheelState, deltaY, internalScrollTop.value, maxScrollTop(), now, mode, { disableAcceleration: mode === "pixel" });
|
|
1463
|
+
if (!dir || nextTop === internalScrollTop.value) return;
|
|
1464
|
+
event.preventDefault?.();
|
|
1465
|
+
setScrollTop(nextTop);
|
|
1466
|
+
},
|
|
1467
|
+
focus: () => {
|
|
1468
|
+
emit("focus");
|
|
1469
|
+
},
|
|
1470
|
+
blur: () => {
|
|
1471
|
+
emit("blur");
|
|
1472
|
+
},
|
|
1473
|
+
keydown: onKeydown
|
|
1474
|
+
}
|
|
1475
|
+
}));
|
|
1476
|
+
watchEffect(() => {
|
|
1477
|
+
if (!props.autoFocus || !visible.value) return;
|
|
1478
|
+
const manager = events.value;
|
|
1479
|
+
const nodeId = eventNode.id.value;
|
|
1480
|
+
if (!nodeId) return;
|
|
1481
|
+
if (!manager) return;
|
|
1482
|
+
if (manager.getFocused() === nodeId) return;
|
|
1483
|
+
manager.focus(nodeId);
|
|
1484
|
+
});
|
|
1485
|
+
onBeforeUnmount(() => {
|
|
1486
|
+
alive = false;
|
|
1487
|
+
rebuildVersion++;
|
|
1488
|
+
});
|
|
1489
|
+
useRenderNode(() => ({
|
|
1490
|
+
zIndex: props.zIndex,
|
|
1491
|
+
rect: visible.value ? normalizedRect() : {
|
|
1492
|
+
x: 0,
|
|
1493
|
+
y: 0,
|
|
1494
|
+
w: 0,
|
|
1495
|
+
h: 0
|
|
1496
|
+
},
|
|
1497
|
+
deps: [
|
|
1498
|
+
visible.value,
|
|
1499
|
+
absRect.value,
|
|
1500
|
+
fullRect.value,
|
|
1501
|
+
internalScrollTop.value,
|
|
1502
|
+
props.style,
|
|
1503
|
+
defaultStyle.value,
|
|
1504
|
+
documentVersion.value
|
|
1505
|
+
],
|
|
1506
|
+
paint: (dirtyRows) => {
|
|
1507
|
+
if (!visible.value) return;
|
|
1508
|
+
const r = normalizedRect();
|
|
1509
|
+
if (r.w <= 0 || r.h <= 0) return;
|
|
1510
|
+
const baseStyle = props.style ?? defaultStyle.value;
|
|
1511
|
+
const { x: clipX, y: clipY } = clipOffsets();
|
|
1512
|
+
const paintRow = (y) => {
|
|
1513
|
+
if (y < r.y || y >= r.y + r.h) return;
|
|
1514
|
+
const visualIndex = internalScrollTop.value + clipY + (y - r.y);
|
|
1515
|
+
paintMarkdownVisualRow(terminal, rows.value[visualIndex], {
|
|
1516
|
+
x: r.x,
|
|
1517
|
+
y,
|
|
1518
|
+
w: r.w,
|
|
1519
|
+
clipStart: clipX,
|
|
1520
|
+
baseStyle,
|
|
1521
|
+
clear: true
|
|
1522
|
+
});
|
|
1523
|
+
};
|
|
1524
|
+
if (dirtyRows?.length) {
|
|
1525
|
+
for (const y of dirtyRows) paintRow(y);
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
for (let y = r.y; y < r.y + r.h; y++) paintRow(y);
|
|
1529
|
+
}
|
|
1530
|
+
}));
|
|
1531
|
+
return () => h("span", rootProps);
|
|
1532
|
+
}
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
//#endregion
|
|
1536
|
+
//#region src/vue/markdown/block-source.ts
|
|
1537
|
+
function withBlockKeyPrefix(block, prefix, index) {
|
|
1538
|
+
const key = `${prefix}:${index}:${block.key}`;
|
|
1539
|
+
switch (block.type) {
|
|
1540
|
+
case "inline": return {
|
|
1541
|
+
...block,
|
|
1542
|
+
key
|
|
1543
|
+
};
|
|
1544
|
+
case "code_block": return {
|
|
1545
|
+
...block,
|
|
1546
|
+
key
|
|
1547
|
+
};
|
|
1548
|
+
case "thematic_break": return {
|
|
1549
|
+
...block,
|
|
1550
|
+
key
|
|
1551
|
+
};
|
|
1552
|
+
case "blank": return {
|
|
1553
|
+
...block,
|
|
1554
|
+
key
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
function parseBlockGroup(text, key, parser, theme, final) {
|
|
1559
|
+
const { blocks } = buildMarkdownBlocks(text, parser, {
|
|
1560
|
+
final,
|
|
1561
|
+
theme
|
|
1562
|
+
});
|
|
1563
|
+
return {
|
|
1564
|
+
key,
|
|
1565
|
+
blocks: blocks.map((block, index) => withBlockKeyPrefix(block, key, index))
|
|
1566
|
+
};
|
|
1567
|
+
}
|
|
1568
|
+
function shouldInsertGap(previous, next) {
|
|
1569
|
+
return previous != null && next != null && previous.type !== "blank" && next.type !== "blank";
|
|
1570
|
+
}
|
|
1571
|
+
function mergeGroups(groups) {
|
|
1572
|
+
const out = [];
|
|
1573
|
+
for (const group of groups) {
|
|
1574
|
+
if (!group.blocks.length) continue;
|
|
1575
|
+
if (shouldInsertGap(out[out.length - 1], group.blocks[0])) out.push({
|
|
1576
|
+
type: "blank",
|
|
1577
|
+
key: `${group.key}:gap`
|
|
1578
|
+
});
|
|
1579
|
+
out.push(...group.blocks);
|
|
1580
|
+
}
|
|
1581
|
+
return out;
|
|
1582
|
+
}
|
|
1583
|
+
function createMarkdownBlockSource(options) {
|
|
1584
|
+
const parser = options?.parser ?? createTuiMarkdownParser({
|
|
1585
|
+
streaming: true,
|
|
1586
|
+
customHtmlTags: options?.customHtmlTags
|
|
1587
|
+
});
|
|
1588
|
+
const theme = options?.theme;
|
|
1589
|
+
const finalizedGroups = [];
|
|
1590
|
+
let tailText = "";
|
|
1591
|
+
let nextGroupIndex = 0;
|
|
1592
|
+
let version = 0;
|
|
1593
|
+
let cachedVersion = -1;
|
|
1594
|
+
let cachedBlocks = [];
|
|
1595
|
+
const invalidate = () => {
|
|
1596
|
+
version++;
|
|
1597
|
+
cachedVersion = -1;
|
|
1598
|
+
};
|
|
1599
|
+
const currentGroups = () => {
|
|
1600
|
+
if (!tailText) return finalizedGroups;
|
|
1601
|
+
return [...finalizedGroups, parseBlockGroup(tailText, `md-tail-${nextGroupIndex}`, parser, theme, false)];
|
|
1602
|
+
};
|
|
1603
|
+
const blocks = () => {
|
|
1604
|
+
if (cachedVersion === version) return cachedBlocks;
|
|
1605
|
+
cachedBlocks = mergeGroups(currentGroups());
|
|
1606
|
+
cachedVersion = version;
|
|
1607
|
+
return cachedBlocks;
|
|
1608
|
+
};
|
|
1609
|
+
const snapshot = () => ({
|
|
1610
|
+
version,
|
|
1611
|
+
blocks: blocks()
|
|
1612
|
+
});
|
|
1613
|
+
return {
|
|
1614
|
+
get version() {
|
|
1615
|
+
return version;
|
|
1616
|
+
},
|
|
1617
|
+
get blocks() {
|
|
1618
|
+
return blocks();
|
|
1619
|
+
},
|
|
1620
|
+
appendDelta(text) {
|
|
1621
|
+
if (!text) return;
|
|
1622
|
+
tailText += text;
|
|
1623
|
+
invalidate();
|
|
1624
|
+
},
|
|
1625
|
+
replaceTailBlock(text) {
|
|
1626
|
+
if (tailText === text) return;
|
|
1627
|
+
tailText = text;
|
|
1628
|
+
invalidate();
|
|
1629
|
+
},
|
|
1630
|
+
finalizeBlock() {
|
|
1631
|
+
if (!tailText) return;
|
|
1632
|
+
const key = `md-block-${nextGroupIndex++}`;
|
|
1633
|
+
finalizedGroups.push(parseBlockGroup(tailText, key, parser, theme, true));
|
|
1634
|
+
tailText = "";
|
|
1635
|
+
invalidate();
|
|
1636
|
+
},
|
|
1637
|
+
clear() {
|
|
1638
|
+
if (!tailText && finalizedGroups.length === 0) return;
|
|
1639
|
+
finalizedGroups.length = 0;
|
|
1640
|
+
tailText = "";
|
|
1641
|
+
nextGroupIndex = 0;
|
|
1642
|
+
invalidate();
|
|
1643
|
+
},
|
|
1644
|
+
snapshot
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
//#endregion
|
|
1649
|
+
export { TMarkdownText, TVirtualMarkdown, buildMarkdownBlocks, buildMarkdownVisualRows, createMarkdownBlockSource, createTuiMarkdownParser, isSafeMarkdownLink, layoutMarkdownBlocks };
|