dou-ui 0.1.0-alpha.24 → 0.1.0-alpha.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/code-block/code-block.js.map +1 -1
- package/dist/components/code-block/code-block.vue_vue_type_script_setup_true_lang.js +61 -27
- package/dist/components/code-block/code-block.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/types/components/code-block/code-block.d.ts +9 -2
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-block.js","names":["_sfc_main"],"sources":["../../../src/components/code-block/code-block.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n watchEffect,\n} from 'vue'\nimport { useNamespace } from '../../utils/namespace'\nimport { useGlobalConfig, useLocale } from '../config-provider/use-config'\nimport { DouCopyButton } from '../copy-button'\nimport type {\n CodeBlockEmits,\n CodeBlockExpose,\n CodeBlockHighlighter,\n CodeBlockProps,\n CodeBlockSlots,\n} from './code-block'\nimport { htmlLines, parseLines, plainLines } from './lines'\n\ndefineOptions({ name: 'DouCodeBlock' })\n\nconst props = withDefaults(defineProps<CodeBlockProps>(), {\n code: '',\n lang: 'text',\n lineNumbers: false,\n startLine: 1,\n wrap: false,\n maxHeight: undefined,\n copyable: true,\n streaming: false,\n autoScroll: undefined,\n bordered: true,\n})\nconst emit = defineEmits<CodeBlockEmits>()\nconst slots = defineSlots<CodeBlockSlots>()\n\nconst ns = useNamespace('code-block')\nconst { t } = useLocale()\nconst config = useGlobalConfig()\nconst bodyId = `dou-code-block-${useId()}`\n\n// A function prop cannot take its default from ConfigProvider the way\n// other props do, so the configured one is read here.\nconst highlighter = computed<CodeBlockHighlighter | undefined>(\n () =>\n props.highlight ??\n (config.value.defaults as { DouCodeBlock?: { highlight?: CodeBlockHighlighter } }).DouCodeBlock\n ?.highlight,\n)\n\nconst code = computed(() => props.code.replace(/\\n$/, ''))\n\n/*\n * Plain lines until the highlighter answers. A synchronous highlighter\n * answers during setup, so server-rendered markup is already highlighted;\n * an async one fills in after. Only the latest call may land. While the\n * next answer is on its way, lines that were highlighted before and have\n * not changed keep their colors.\n */\nconst lines = shallowRef<string[]>(plainLines(code.value))\nlet run = 0\n// what the last answer covered, and its lines\nlet landed: { source: string; lines: string[] } | undefined\nwatchEffect(() => {\n const current = ++run\n const highlight = highlighter.value\n const raw = props.code\n // while streaming, the line still being written stays plain\n const cut = props.streaming ? raw.lastIndexOf('\\n') + 1 : raw.length\n const source = raw.slice(0, cut).replace(/\\n$/, '')\n const tail = props.streaming ? raw.slice(cut) : ''\n const after = tail ? plainLines(tail) : []\n if (!highlight || !source) {\n lines.value = plainLines(code.value)\n return\n }\n if (landed?.source === source) {\n lines.value = after.length ? [...landed.lines, ...after] : landed.lines\n return\n }\n const kept = landed && source.startsWith(`${landed.source}\\n`) ? landed : undefined\n lines.value = kept\n ? [...kept.lines, ...plainLines(source.slice(kept.source.length + 1)), ...after]\n : [...plainLines(source), ...after]\n const apply = (html: string) => {\n if (current !== run) return\n landed = { source, lines: htmlLines(html) }\n lines.value = after.length ? [...landed.lines, ...after] : landed.lines\n }\n try {\n const result = highlight(source, props.lang)\n if (typeof result === 'string') apply(result)\n else void result.then(apply, () => {})\n } catch {\n // a highlighter that throws leaves the plain text\n }\n})\n\nconst marked = computed(() => parseLines(props.highlightLines))\nconst rows = computed(() =>\n lines.value.map((html, index) => {\n const number = props.startLine + index\n return { html, number, marked: marked.value.has(number) }\n }),\n)\n\nconst expanded = ref(false)\nconst foldable = computed(() => props.maxLines !== undefined && rows.value.length > props.maxLines)\nconst folded = computed(() => foldable.value && !expanded.value)\n\nconst maxHeightValue = computed(() => {\n if (props.maxHeight === undefined || props.maxHeight === '') return undefined\n return typeof props.maxHeight === 'number' ? `${props.maxHeight}px` : props.maxHeight\n})\nconst scrollable = computed(() => maxHeightValue.value !== undefined)\n\nconst bodyRef = useTemplateRef<HTMLElement>('body')\nconst codeRef = useTemplateRef<HTMLElement>('codeEl')\nconst scrollbarWidth = ref(0)\n\nfunction syncScrollbarWidth() {\n const el = bodyRef.value\n if (!el || !scrollable.value || folded.value) {\n scrollbarWidth.value = 0\n return\n }\n scrollbarWidth.value = Math.max(0, el.offsetWidth - el.clientWidth)\n}\n\nconst AT_BOTTOM_THRESHOLD = 24\nconst shouldAutoScroll = computed(() => props.autoScroll ?? props.streaming)\nconst atBottom = ref(true)\nwatch(atBottom, (value) => emit('atBottomChange', value))\n\n// Following is a choice of the reader: scrolling up pauses it, and reaching\n// the bottom resumes it. Appending content alone never turns it off.\nlet following = true\nlet scrollingToBottom = false\nlet lastScrollTop = 0\n\nfunction distanceToBottom(el: HTMLElement) {\n return el.scrollHeight - el.scrollTop - el.clientHeight\n}\n\nfunction isNearBottom(el: HTMLElement) {\n return distanceToBottom(el) <= AT_BOTTOM_THRESHOLD\n}\n\nfunction onBodyScroll() {\n const el = bodyRef.value\n if (!el) return\n const distance = distanceToBottom(el)\n const near = distance <= AT_BOTTOM_THRESHOLD\n if (el.scrollTop < lastScrollTop && distance > 1) {\n scrollingToBottom = false\n following = false\n } else if (near) {\n following = true\n scrollingToBottom = false\n } else if (scrollingToBottom && el.scrollTop >= lastScrollTop) {\n following = true\n } else if (el.scrollTop !== lastScrollTop) {\n scrollingToBottom = false\n following = false\n }\n lastScrollTop = el.scrollTop\n atBottom.value = near\n}\n\nfunction onBodyWheel(event: WheelEvent) {\n const el = bodyRef.value\n if (event.deltaY < 0 && el && el.scrollHeight > el.clientHeight) {\n following = false\n scrollingToBottom = false\n }\n}\n\nfunction scrollToBottom(behavior: ScrollBehavior = 'auto') {\n const el = bodyRef.value\n if (!el) return\n following = true\n const maxTop = Math.max(0, el.scrollHeight - el.clientHeight)\n if (behavior === 'smooth' && typeof el.scrollTo === 'function') {\n el.scrollTo({ top: el.scrollHeight, behavior })\n const near = isNearBottom(el)\n scrollingToBottom = !near\n lastScrollTop = el.scrollTop\n atBottom.value = near\n } else {\n scrollingToBottom = false\n el.scrollTop = maxTop\n lastScrollTop = el.scrollTop\n atBottom.value = true\n }\n}\n\nfunction follow() {\n syncScrollbarWidth()\n const el = bodyRef.value\n if (!el) return\n if (!shouldAutoScroll.value || !following || folded.value) {\n atBottom.value = isNearBottom(el)\n return\n }\n scrollingToBottom = false\n el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight)\n lastScrollTop = el.scrollTop\n atBottom.value = true\n}\n\nlet resizeObserver: ResizeObserver | undefined\nonMounted(() => {\n follow()\n if (typeof ResizeObserver === 'undefined') return\n resizeObserver = new ResizeObserver(follow)\n if (bodyRef.value) resizeObserver.observe(bodyRef.value)\n if (codeRef.value) resizeObserver.observe(codeRef.value)\n})\nonBeforeUnmount(() => resizeObserver?.disconnect())\n\nwatch(\n [() => props.code, lines, maxHeightValue, () => props.wrap, folded, shouldAutoScroll],\n follow,\n { flush: 'post' },\n)\n\nconst title = computed(() => props.filename ?? (props.lang === 'text' ? '' : props.lang))\nconst hasHeader = computed(() => !!(slots.title || title.value || slots.actions))\n\nconst classes = computed(() => [\n ns.b(),\n ns.is('wrap', props.wrap),\n ns.is('numbered', props.lineNumbers),\n ns.is('folded', folded.value),\n ns.is('scrollable', scrollable.value),\n ns.is('headless', !hasHeader.value),\n ns.is('streaming', props.streaming),\n ns.is('bordered', props.bordered),\n])\n\nconst rootStyle = computed(() => {\n const style: Record<string, string | number> = {}\n if (foldable.value && props.maxLines !== undefined) {\n style['--dou-code-block-max-lines'] = props.maxLines\n }\n if (maxHeightValue.value !== undefined) {\n style['--dou-code-block-max-height'] = maxHeightValue.value\n }\n if (scrollbarWidth.value > 0) {\n style['--dou-code-block-scrollbar-width'] = `${scrollbarWidth.value}px`\n }\n return Object.keys(style).length ? style : undefined\n})\n\ndefineExpose<CodeBlockExpose>({\n get body() {\n return bodyRef.value\n },\n scrollToBottom,\n get atBottom() {\n return atBottom.value\n },\n})\n</script>\n\n<template>\n <div :class=\"classes\" :style=\"rootStyle\">\n <div v-if=\"hasHeader\" :class=\"ns.e('header')\">\n <span :class=\"ns.e('title')\">\n <slot name=\"title\">{{ title }}</slot>\n </span>\n <span :class=\"ns.e('actions')\">\n <slot name=\"actions\" />\n <DouCopyButton v-if=\"copyable\" :text=\"code\" size=\"sm\" />\n </span>\n </div>\n <DouCopyButton v-else-if=\"copyable\" :class=\"ns.e('copy')\" :text=\"code\" size=\"sm\" />\n <div\n :id=\"bodyId\"\n ref=\"body\"\n :class=\"ns.e('body')\"\n :data-lang=\"lang\"\n :tabindex=\"scrollable && !folded ? 0 : undefined\"\n @scroll.passive=\"onBodyScroll\"\n @wheel.passive=\"onBodyWheel\"\n >\n <code ref=\"codeEl\" :class=\"ns.e('code')\">\n <span\n v-for=\"row in rows\"\n :key=\"row.number\"\n :class=\"[ns.e('row'), ns.is('marked', row.marked)]\"\n >\n <span v-if=\"lineNumbers\" :class=\"ns.e('number')\" aria-hidden=\"true\">{{\n row.number\n }}</span>\n <span :class=\"ns.e('line')\" v-html=\"row.html\" />\n </span>\n </code>\n </div>\n <button\n v-if=\"foldable\"\n type=\"button\"\n :class=\"ns.e('toggle')\"\n :aria-expanded=\"expanded\"\n :aria-controls=\"bodyId\"\n @click=\"expanded = !expanded\"\n >\n {{ expanded ? t('codeBlock.collapse') : t('codeBlock.expand', { count: rows.length }) }}\n </button>\n </div>\n</template>\n"],"mappings":""}
|
|
1
|
+
{"version":3,"file":"code-block.js","names":["_sfc_main"],"sources":["../../../src/components/code-block/code-block.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useNamespace } from '../../utils/namespace'\nimport { useGlobalConfig, useLocale } from '../config-provider/use-config'\nimport { DouCopyButton } from '../copy-button'\nimport type {\n CodeBlockEmits,\n CodeBlockExpose,\n CodeBlockHighlighter,\n CodeBlockProps,\n CodeBlockSlots,\n} from './code-block'\nimport { htmlLines, parseLines, plainLines } from './lines'\n\ndefineOptions({ name: 'DouCodeBlock' })\n\nconst props = withDefaults(defineProps<CodeBlockProps>(), {\n code: '',\n lang: 'text',\n lineNumbers: false,\n startLine: 1,\n wrap: false,\n maxHeight: undefined,\n copyable: true,\n streaming: false,\n autoScroll: undefined,\n bordered: true,\n})\nconst emit = defineEmits<CodeBlockEmits>()\nconst slots = defineSlots<CodeBlockSlots>()\n\nconst ns = useNamespace('code-block')\nconst { t } = useLocale()\nconst config = useGlobalConfig()\nconst bodyId = `dou-code-block-${useId()}`\n\n// A function prop cannot take its default from ConfigProvider the way\n// other props do, so the configured one is read here.\nconst highlighter = computed<CodeBlockHighlighter | undefined>(\n () =>\n props.highlight ??\n (config.value.defaults as { DouCodeBlock?: { highlight?: CodeBlockHighlighter } }).DouCodeBlock\n ?.highlight,\n)\n\nconst code = computed(() => props.code.replace(/\\n$/, ''))\n\nconst split = computed(() => {\n const raw = props.code\n // while streaming, the line still being written stays plain\n if (!props.streaming) return { source: code.value, tail: '' }\n const cut = raw.lastIndexOf('\\n') + 1\n return {\n source: raw.slice(0, cut).replace(/\\n$/, ''),\n tail: raw.slice(cut),\n }\n})\n\ninterface LandedHighlight {\n source: string\n lang: string\n highlight: CodeBlockHighlighter\n lines: string[]\n}\n\n/*\n * Plain lines until the highlighter answers. A synchronous highlighter\n * answers during setup, so server-rendered markup is already highlighted;\n * an async one fills in after. Only the latest call may land, and any\n * previous in-flight run is aborted when inputs change or the block unmounts.\n * While the next answer is on its way, lines that were highlighted before\n * and have not changed keep their colors.\n */\nconst landed = shallowRef<LandedHighlight | undefined>()\nlet run = 0\nlet activeController: AbortController | undefined\n\nfunction abortActiveHighlight() {\n const controller = activeController\n activeController = undefined\n controller?.abort()\n}\n\nwatch(\n [highlighter, () => split.value.source, () => props.lang],\n ([highlight, source, lang], _prev, onCleanup) => {\n const current = ++run\n abortActiveHighlight()\n if (!highlight || !source) {\n landed.value = undefined\n return\n }\n const last = landed.value\n if (last && last.highlight === highlight && last.lang === lang && last.source === source) {\n return\n }\n const controller = new AbortController()\n activeController = controller\n onCleanup(() => {\n if (activeController === controller) activeController = undefined\n controller.abort()\n })\n const apply = (html: string) => {\n if (current !== run || controller.signal.aborted) return\n landed.value = { source, lang, highlight, lines: htmlLines(html) }\n }\n try {\n const result = highlight(source, lang, { signal: controller.signal })\n if (typeof result === 'string') {\n apply(result)\n } else if (result && typeof result.then === 'function') {\n void result.then(apply, () => {})\n }\n } catch {\n // a highlighter that throws leaves the plain text\n }\n },\n { immediate: true },\n)\n\nconst lines = computed<string[]>(() => {\n const { source, tail } = split.value\n const after = tail ? plainLines(tail) : []\n const highlight = highlighter.value\n if (!highlight || !source) return plainLines(code.value)\n const last = landed.value\n if (last && last.highlight === highlight && last.lang === props.lang) {\n if (last.source === source) return after.length ? [...last.lines, ...after] : last.lines\n if (source.startsWith(`${last.source}\\n`)) {\n return [...last.lines, ...plainLines(source.slice(last.source.length + 1)), ...after]\n }\n }\n return [...plainLines(source), ...after]\n})\n\nconst marked = computed(() => parseLines(props.highlightLines))\nconst rows = computed(() =>\n lines.value.map((html, index) => {\n const number = props.startLine + index\n return { html, number, marked: marked.value.has(number) }\n }),\n)\n\nconst expanded = ref(false)\nconst foldable = computed(() => props.maxLines !== undefined && rows.value.length > props.maxLines)\nconst folded = computed(() => foldable.value && !expanded.value)\n\nconst maxHeightValue = computed(() => {\n if (props.maxHeight === undefined || props.maxHeight === '') return undefined\n return typeof props.maxHeight === 'number' ? `${props.maxHeight}px` : props.maxHeight\n})\nconst scrollable = computed(() => maxHeightValue.value !== undefined)\n\nconst bodyRef = useTemplateRef<HTMLElement>('body')\nconst codeRef = useTemplateRef<HTMLElement>('codeEl')\nconst scrollbarWidth = ref(0)\n\nfunction syncScrollbarWidth() {\n const el = bodyRef.value\n if (!el || !scrollable.value || folded.value) {\n scrollbarWidth.value = 0\n return\n }\n scrollbarWidth.value = Math.max(0, el.offsetWidth - el.clientWidth)\n}\n\nconst AT_BOTTOM_THRESHOLD = 24\nconst shouldAutoScroll = computed(() => props.autoScroll ?? props.streaming)\nconst atBottom = ref(true)\nwatch(atBottom, (value) => emit('atBottomChange', value))\n\n// Following is a choice of the reader: scrolling up pauses it, and reaching\n// the bottom resumes it. Appending content alone never turns it off.\nlet following = true\nlet scrollingToBottom = false\nlet lastScrollTop = 0\n\nfunction distanceToBottom(el: HTMLElement) {\n return el.scrollHeight - el.scrollTop - el.clientHeight\n}\n\nfunction isNearBottom(el: HTMLElement) {\n return distanceToBottom(el) <= AT_BOTTOM_THRESHOLD\n}\n\nfunction onBodyScroll() {\n const el = bodyRef.value\n if (!el) return\n const distance = distanceToBottom(el)\n const near = distance <= AT_BOTTOM_THRESHOLD\n if (el.scrollTop < lastScrollTop && distance > 1) {\n scrollingToBottom = false\n following = false\n } else if (near) {\n following = true\n scrollingToBottom = false\n } else if (scrollingToBottom && el.scrollTop >= lastScrollTop) {\n following = true\n } else if (el.scrollTop !== lastScrollTop) {\n scrollingToBottom = false\n following = false\n }\n lastScrollTop = el.scrollTop\n atBottom.value = near\n}\n\nfunction onBodyWheel(event: WheelEvent) {\n const el = bodyRef.value\n if (event.deltaY < 0 && el && el.scrollHeight > el.clientHeight) {\n following = false\n scrollingToBottom = false\n }\n}\n\nfunction scrollToBottom(behavior: ScrollBehavior = 'auto') {\n const el = bodyRef.value\n if (!el) return\n following = true\n const maxTop = Math.max(0, el.scrollHeight - el.clientHeight)\n if (behavior === 'smooth' && typeof el.scrollTo === 'function') {\n el.scrollTo({ top: el.scrollHeight, behavior })\n const near = isNearBottom(el)\n scrollingToBottom = !near\n lastScrollTop = el.scrollTop\n atBottom.value = near\n } else {\n scrollingToBottom = false\n el.scrollTop = maxTop\n lastScrollTop = el.scrollTop\n atBottom.value = true\n }\n}\n\nfunction follow() {\n syncScrollbarWidth()\n const el = bodyRef.value\n if (!el) return\n if (!shouldAutoScroll.value || !following || folded.value) {\n atBottom.value = isNearBottom(el)\n return\n }\n scrollingToBottom = false\n el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight)\n lastScrollTop = el.scrollTop\n atBottom.value = true\n}\n\nlet resizeObserver: ResizeObserver | undefined\nonMounted(() => {\n follow()\n if (typeof ResizeObserver === 'undefined') return\n resizeObserver = new ResizeObserver(follow)\n if (bodyRef.value) resizeObserver.observe(bodyRef.value)\n if (codeRef.value) resizeObserver.observe(codeRef.value)\n})\nonBeforeUnmount(() => {\n abortActiveHighlight()\n resizeObserver?.disconnect()\n})\n\nwatch(\n [() => props.code, lines, maxHeightValue, () => props.wrap, folded, shouldAutoScroll],\n follow,\n { flush: 'post' },\n)\n\nconst title = computed(() => props.filename ?? (props.lang === 'text' ? '' : props.lang))\nconst hasHeader = computed(() => !!(slots.title || title.value || slots.actions))\n\nconst classes = computed(() => [\n ns.b(),\n ns.is('wrap', props.wrap),\n ns.is('numbered', props.lineNumbers),\n ns.is('folded', folded.value),\n ns.is('scrollable', scrollable.value),\n ns.is('headless', !hasHeader.value),\n ns.is('streaming', props.streaming),\n ns.is('bordered', props.bordered),\n])\n\nconst rootStyle = computed(() => {\n const style: Record<string, string | number> = {}\n if (foldable.value && props.maxLines !== undefined) {\n style['--dou-code-block-max-lines'] = props.maxLines\n }\n if (maxHeightValue.value !== undefined) {\n style['--dou-code-block-max-height'] = maxHeightValue.value\n }\n if (scrollbarWidth.value > 0) {\n style['--dou-code-block-scrollbar-width'] = `${scrollbarWidth.value}px`\n }\n return Object.keys(style).length ? style : undefined\n})\n\ndefineExpose<CodeBlockExpose>({\n get body() {\n return bodyRef.value\n },\n scrollToBottom,\n get atBottom() {\n return atBottom.value\n },\n})\n</script>\n\n<template>\n <div :class=\"classes\" :style=\"rootStyle\">\n <div v-if=\"hasHeader\" :class=\"ns.e('header')\">\n <span :class=\"ns.e('title')\">\n <slot name=\"title\">{{ title }}</slot>\n </span>\n <span :class=\"ns.e('actions')\">\n <slot name=\"actions\" />\n <DouCopyButton v-if=\"copyable\" :text=\"code\" size=\"sm\" />\n </span>\n </div>\n <DouCopyButton v-else-if=\"copyable\" :class=\"ns.e('copy')\" :text=\"code\" size=\"sm\" />\n <div\n :id=\"bodyId\"\n ref=\"body\"\n :class=\"ns.e('body')\"\n :data-lang=\"lang\"\n :tabindex=\"scrollable && !folded ? 0 : undefined\"\n @scroll.passive=\"onBodyScroll\"\n @wheel.passive=\"onBodyWheel\"\n >\n <code ref=\"codeEl\" :class=\"ns.e('code')\">\n <span\n v-for=\"row in rows\"\n :key=\"row.number\"\n :class=\"[ns.e('row'), ns.is('marked', row.marked)]\"\n >\n <span v-if=\"lineNumbers\" :class=\"ns.e('number')\" aria-hidden=\"true\">{{\n row.number\n }}</span>\n <span :class=\"ns.e('line')\" v-html=\"row.html\" />\n </span>\n </code>\n </div>\n <button\n v-if=\"foldable\"\n type=\"button\"\n :class=\"ns.e('toggle')\"\n :aria-expanded=\"expanded\"\n :aria-controls=\"bodyId\"\n @click=\"expanded = !expanded\"\n >\n {{ expanded ? t('codeBlock.collapse') : t('codeBlock.expand', { count: rows.length }) }}\n </button>\n </div>\n</template>\n"],"mappings":""}
|
|
@@ -2,7 +2,7 @@ import { useNamespace } from "../../utils/namespace.js";
|
|
|
2
2
|
import { useGlobalConfig, useLocale } from "../config-provider/use-config.js";
|
|
3
3
|
import { DouCopyButton } from "../copy-button/index.js";
|
|
4
4
|
import { htmlLines, parseLines, plainLines } from "./lines.js";
|
|
5
|
-
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, defineComponent, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, ref, renderList, renderSlot, shallowRef, toDisplayString, unref, useId, useSlots, useTemplateRef, watch
|
|
5
|
+
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, defineComponent, normalizeClass, normalizeStyle, onBeforeUnmount, onMounted, openBlock, ref, renderList, renderSlot, shallowRef, toDisplayString, unref, useId, useSlots, useTemplateRef, watch } from "vue";
|
|
6
6
|
// src/components/code-block/code-block.vue?vue&type=script&setup=true&lang.ts
|
|
7
7
|
var _hoisted_1 = ["data-lang", "tabindex"];
|
|
8
8
|
var _hoisted_2 = ["innerHTML"];
|
|
@@ -56,44 +56,75 @@ var code_block_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
56
56
|
const bodyId = `dou-code-block-${useId()}`;
|
|
57
57
|
const highlighter = computed(() => props.highlight ?? config.value.defaults.DouCodeBlock?.highlight);
|
|
58
58
|
const code = computed(() => props.code.replace(/\n$/, ""));
|
|
59
|
-
const
|
|
59
|
+
const split = computed(() => {
|
|
60
|
+
const raw = props.code;
|
|
61
|
+
if (!props.streaming) return {
|
|
62
|
+
source: code.value,
|
|
63
|
+
tail: ""
|
|
64
|
+
};
|
|
65
|
+
const cut = raw.lastIndexOf("\n") + 1;
|
|
66
|
+
return {
|
|
67
|
+
source: raw.slice(0, cut).replace(/\n$/, ""),
|
|
68
|
+
tail: raw.slice(cut)
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
const landed = shallowRef();
|
|
60
72
|
let run = 0;
|
|
61
|
-
let
|
|
62
|
-
|
|
73
|
+
let activeController;
|
|
74
|
+
function abortActiveHighlight() {
|
|
75
|
+
const controller = activeController;
|
|
76
|
+
activeController = void 0;
|
|
77
|
+
controller?.abort();
|
|
78
|
+
}
|
|
79
|
+
watch([
|
|
80
|
+
highlighter,
|
|
81
|
+
() => split.value.source,
|
|
82
|
+
() => props.lang
|
|
83
|
+
], ([highlight, source, lang], _prev, onCleanup) => {
|
|
63
84
|
const current = ++run;
|
|
64
|
-
|
|
65
|
-
const raw = props.code;
|
|
66
|
-
const cut = props.streaming ? raw.lastIndexOf("\n") + 1 : raw.length;
|
|
67
|
-
const source = raw.slice(0, cut).replace(/\n$/, "");
|
|
68
|
-
const tail = props.streaming ? raw.slice(cut) : "";
|
|
69
|
-
const after = tail ? plainLines(tail) : [];
|
|
85
|
+
abortActiveHighlight();
|
|
70
86
|
if (!highlight || !source) {
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (landed?.source === source) {
|
|
75
|
-
lines.value = after.length ? [...landed.lines, ...after] : landed.lines;
|
|
87
|
+
landed.value = void 0;
|
|
76
88
|
return;
|
|
77
89
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
90
|
+
const last = landed.value;
|
|
91
|
+
if (last && last.highlight === highlight && last.lang === lang && last.source === source) return;
|
|
92
|
+
const controller = new AbortController();
|
|
93
|
+
activeController = controller;
|
|
94
|
+
onCleanup(() => {
|
|
95
|
+
if (activeController === controller) activeController = void 0;
|
|
96
|
+
controller.abort();
|
|
97
|
+
});
|
|
84
98
|
const apply = (html) => {
|
|
85
|
-
if (current !== run) return;
|
|
86
|
-
landed = {
|
|
99
|
+
if (current !== run || controller.signal.aborted) return;
|
|
100
|
+
landed.value = {
|
|
87
101
|
source,
|
|
102
|
+
lang,
|
|
103
|
+
highlight,
|
|
88
104
|
lines: htmlLines(html)
|
|
89
105
|
};
|
|
90
|
-
lines.value = after.length ? [...landed.lines, ...after] : landed.lines;
|
|
91
106
|
};
|
|
92
107
|
try {
|
|
93
|
-
const result = highlight(source,
|
|
108
|
+
const result = highlight(source, lang, { signal: controller.signal });
|
|
94
109
|
if (typeof result === "string") apply(result);
|
|
95
|
-
else result.then(apply, () => {});
|
|
110
|
+
else if (result && typeof result.then === "function") result.then(apply, () => {});
|
|
96
111
|
} catch {}
|
|
112
|
+
}, { immediate: true });
|
|
113
|
+
const lines = computed(() => {
|
|
114
|
+
const { source, tail } = split.value;
|
|
115
|
+
const after = tail ? plainLines(tail) : [];
|
|
116
|
+
const highlight = highlighter.value;
|
|
117
|
+
if (!highlight || !source) return plainLines(code.value);
|
|
118
|
+
const last = landed.value;
|
|
119
|
+
if (last && last.highlight === highlight && last.lang === props.lang) {
|
|
120
|
+
if (last.source === source) return after.length ? [...last.lines, ...after] : last.lines;
|
|
121
|
+
if (source.startsWith(`${last.source}\n`)) return [
|
|
122
|
+
...last.lines,
|
|
123
|
+
...plainLines(source.slice(last.source.length + 1)),
|
|
124
|
+
...after
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
return [...plainLines(source), ...after];
|
|
97
128
|
});
|
|
98
129
|
const marked = computed(() => parseLines(props.highlightLines));
|
|
99
130
|
const rows = computed(() => lines.value.map((html, index) => {
|
|
@@ -203,7 +234,10 @@ var code_block_vue_vue_type_script_setup_true_lang_default = /*@__PURE__*/ defin
|
|
|
203
234
|
if (bodyRef.value) resizeObserver.observe(bodyRef.value);
|
|
204
235
|
if (codeRef.value) resizeObserver.observe(codeRef.value);
|
|
205
236
|
});
|
|
206
|
-
onBeforeUnmount(() =>
|
|
237
|
+
onBeforeUnmount(() => {
|
|
238
|
+
abortActiveHighlight();
|
|
239
|
+
resizeObserver?.disconnect();
|
|
240
|
+
});
|
|
207
241
|
watch([
|
|
208
242
|
() => props.code,
|
|
209
243
|
lines,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code-block.vue_vue_type_script_setup_true_lang.js","names":["_useSlots","_openBlock","_createElementBlock","_normalizeClass","_normalizeStyle","_unref","_createElementVNode","_renderSlot","_createTextVNode","_toDisplayString","_createBlock","_createCommentVNode","_Fragment","_renderList"],"sources":["../../../src/components/code-block/code-block.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n watchEffect,\n} from 'vue'\nimport { useNamespace } from '../../utils/namespace'\nimport { useGlobalConfig, useLocale } from '../config-provider/use-config'\nimport { DouCopyButton } from '../copy-button'\nimport type {\n CodeBlockEmits,\n CodeBlockExpose,\n CodeBlockHighlighter,\n CodeBlockProps,\n CodeBlockSlots,\n} from './code-block'\nimport { htmlLines, parseLines, plainLines } from './lines'\n\ndefineOptions({ name: 'DouCodeBlock' })\n\nconst props = withDefaults(defineProps<CodeBlockProps>(), {\n code: '',\n lang: 'text',\n lineNumbers: false,\n startLine: 1,\n wrap: false,\n maxHeight: undefined,\n copyable: true,\n streaming: false,\n autoScroll: undefined,\n bordered: true,\n})\nconst emit = defineEmits<CodeBlockEmits>()\nconst slots = defineSlots<CodeBlockSlots>()\n\nconst ns = useNamespace('code-block')\nconst { t } = useLocale()\nconst config = useGlobalConfig()\nconst bodyId = `dou-code-block-${useId()}`\n\n// A function prop cannot take its default from ConfigProvider the way\n// other props do, so the configured one is read here.\nconst highlighter = computed<CodeBlockHighlighter | undefined>(\n () =>\n props.highlight ??\n (config.value.defaults as { DouCodeBlock?: { highlight?: CodeBlockHighlighter } }).DouCodeBlock\n ?.highlight,\n)\n\nconst code = computed(() => props.code.replace(/\\n$/, ''))\n\n/*\n * Plain lines until the highlighter answers. A synchronous highlighter\n * answers during setup, so server-rendered markup is already highlighted;\n * an async one fills in after. Only the latest call may land. While the\n * next answer is on its way, lines that were highlighted before and have\n * not changed keep their colors.\n */\nconst lines = shallowRef<string[]>(plainLines(code.value))\nlet run = 0\n// what the last answer covered, and its lines\nlet landed: { source: string; lines: string[] } | undefined\nwatchEffect(() => {\n const current = ++run\n const highlight = highlighter.value\n const raw = props.code\n // while streaming, the line still being written stays plain\n const cut = props.streaming ? raw.lastIndexOf('\\n') + 1 : raw.length\n const source = raw.slice(0, cut).replace(/\\n$/, '')\n const tail = props.streaming ? raw.slice(cut) : ''\n const after = tail ? plainLines(tail) : []\n if (!highlight || !source) {\n lines.value = plainLines(code.value)\n return\n }\n if (landed?.source === source) {\n lines.value = after.length ? [...landed.lines, ...after] : landed.lines\n return\n }\n const kept = landed && source.startsWith(`${landed.source}\\n`) ? landed : undefined\n lines.value = kept\n ? [...kept.lines, ...plainLines(source.slice(kept.source.length + 1)), ...after]\n : [...plainLines(source), ...after]\n const apply = (html: string) => {\n if (current !== run) return\n landed = { source, lines: htmlLines(html) }\n lines.value = after.length ? [...landed.lines, ...after] : landed.lines\n }\n try {\n const result = highlight(source, props.lang)\n if (typeof result === 'string') apply(result)\n else void result.then(apply, () => {})\n } catch {\n // a highlighter that throws leaves the plain text\n }\n})\n\nconst marked = computed(() => parseLines(props.highlightLines))\nconst rows = computed(() =>\n lines.value.map((html, index) => {\n const number = props.startLine + index\n return { html, number, marked: marked.value.has(number) }\n }),\n)\n\nconst expanded = ref(false)\nconst foldable = computed(() => props.maxLines !== undefined && rows.value.length > props.maxLines)\nconst folded = computed(() => foldable.value && !expanded.value)\n\nconst maxHeightValue = computed(() => {\n if (props.maxHeight === undefined || props.maxHeight === '') return undefined\n return typeof props.maxHeight === 'number' ? `${props.maxHeight}px` : props.maxHeight\n})\nconst scrollable = computed(() => maxHeightValue.value !== undefined)\n\nconst bodyRef = useTemplateRef<HTMLElement>('body')\nconst codeRef = useTemplateRef<HTMLElement>('codeEl')\nconst scrollbarWidth = ref(0)\n\nfunction syncScrollbarWidth() {\n const el = bodyRef.value\n if (!el || !scrollable.value || folded.value) {\n scrollbarWidth.value = 0\n return\n }\n scrollbarWidth.value = Math.max(0, el.offsetWidth - el.clientWidth)\n}\n\nconst AT_BOTTOM_THRESHOLD = 24\nconst shouldAutoScroll = computed(() => props.autoScroll ?? props.streaming)\nconst atBottom = ref(true)\nwatch(atBottom, (value) => emit('atBottomChange', value))\n\n// Following is a choice of the reader: scrolling up pauses it, and reaching\n// the bottom resumes it. Appending content alone never turns it off.\nlet following = true\nlet scrollingToBottom = false\nlet lastScrollTop = 0\n\nfunction distanceToBottom(el: HTMLElement) {\n return el.scrollHeight - el.scrollTop - el.clientHeight\n}\n\nfunction isNearBottom(el: HTMLElement) {\n return distanceToBottom(el) <= AT_BOTTOM_THRESHOLD\n}\n\nfunction onBodyScroll() {\n const el = bodyRef.value\n if (!el) return\n const distance = distanceToBottom(el)\n const near = distance <= AT_BOTTOM_THRESHOLD\n if (el.scrollTop < lastScrollTop && distance > 1) {\n scrollingToBottom = false\n following = false\n } else if (near) {\n following = true\n scrollingToBottom = false\n } else if (scrollingToBottom && el.scrollTop >= lastScrollTop) {\n following = true\n } else if (el.scrollTop !== lastScrollTop) {\n scrollingToBottom = false\n following = false\n }\n lastScrollTop = el.scrollTop\n atBottom.value = near\n}\n\nfunction onBodyWheel(event: WheelEvent) {\n const el = bodyRef.value\n if (event.deltaY < 0 && el && el.scrollHeight > el.clientHeight) {\n following = false\n scrollingToBottom = false\n }\n}\n\nfunction scrollToBottom(behavior: ScrollBehavior = 'auto') {\n const el = bodyRef.value\n if (!el) return\n following = true\n const maxTop = Math.max(0, el.scrollHeight - el.clientHeight)\n if (behavior === 'smooth' && typeof el.scrollTo === 'function') {\n el.scrollTo({ top: el.scrollHeight, behavior })\n const near = isNearBottom(el)\n scrollingToBottom = !near\n lastScrollTop = el.scrollTop\n atBottom.value = near\n } else {\n scrollingToBottom = false\n el.scrollTop = maxTop\n lastScrollTop = el.scrollTop\n atBottom.value = true\n }\n}\n\nfunction follow() {\n syncScrollbarWidth()\n const el = bodyRef.value\n if (!el) return\n if (!shouldAutoScroll.value || !following || folded.value) {\n atBottom.value = isNearBottom(el)\n return\n }\n scrollingToBottom = false\n el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight)\n lastScrollTop = el.scrollTop\n atBottom.value = true\n}\n\nlet resizeObserver: ResizeObserver | undefined\nonMounted(() => {\n follow()\n if (typeof ResizeObserver === 'undefined') return\n resizeObserver = new ResizeObserver(follow)\n if (bodyRef.value) resizeObserver.observe(bodyRef.value)\n if (codeRef.value) resizeObserver.observe(codeRef.value)\n})\nonBeforeUnmount(() => resizeObserver?.disconnect())\n\nwatch(\n [() => props.code, lines, maxHeightValue, () => props.wrap, folded, shouldAutoScroll],\n follow,\n { flush: 'post' },\n)\n\nconst title = computed(() => props.filename ?? (props.lang === 'text' ? '' : props.lang))\nconst hasHeader = computed(() => !!(slots.title || title.value || slots.actions))\n\nconst classes = computed(() => [\n ns.b(),\n ns.is('wrap', props.wrap),\n ns.is('numbered', props.lineNumbers),\n ns.is('folded', folded.value),\n ns.is('scrollable', scrollable.value),\n ns.is('headless', !hasHeader.value),\n ns.is('streaming', props.streaming),\n ns.is('bordered', props.bordered),\n])\n\nconst rootStyle = computed(() => {\n const style: Record<string, string | number> = {}\n if (foldable.value && props.maxLines !== undefined) {\n style['--dou-code-block-max-lines'] = props.maxLines\n }\n if (maxHeightValue.value !== undefined) {\n style['--dou-code-block-max-height'] = maxHeightValue.value\n }\n if (scrollbarWidth.value > 0) {\n style['--dou-code-block-scrollbar-width'] = `${scrollbarWidth.value}px`\n }\n return Object.keys(style).length ? style : undefined\n})\n\ndefineExpose<CodeBlockExpose>({\n get body() {\n return bodyRef.value\n },\n scrollToBottom,\n get atBottom() {\n return atBottom.value\n },\n})\n</script>\n\n<template>\n <div :class=\"classes\" :style=\"rootStyle\">\n <div v-if=\"hasHeader\" :class=\"ns.e('header')\">\n <span :class=\"ns.e('title')\">\n <slot name=\"title\">{{ title }}</slot>\n </span>\n <span :class=\"ns.e('actions')\">\n <slot name=\"actions\" />\n <DouCopyButton v-if=\"copyable\" :text=\"code\" size=\"sm\" />\n </span>\n </div>\n <DouCopyButton v-else-if=\"copyable\" :class=\"ns.e('copy')\" :text=\"code\" size=\"sm\" />\n <div\n :id=\"bodyId\"\n ref=\"body\"\n :class=\"ns.e('body')\"\n :data-lang=\"lang\"\n :tabindex=\"scrollable && !folded ? 0 : undefined\"\n @scroll.passive=\"onBodyScroll\"\n @wheel.passive=\"onBodyWheel\"\n >\n <code ref=\"codeEl\" :class=\"ns.e('code')\">\n <span\n v-for=\"row in rows\"\n :key=\"row.number\"\n :class=\"[ns.e('row'), ns.is('marked', row.marked)]\"\n >\n <span v-if=\"lineNumbers\" :class=\"ns.e('number')\" aria-hidden=\"true\">{{\n row.number\n }}</span>\n <span :class=\"ns.e('line')\" v-html=\"row.html\" />\n </span>\n </code>\n </div>\n <button\n v-if=\"foldable\"\n type=\"button\"\n :class=\"ns.e('toggle')\"\n :aria-expanded=\"expanded\"\n :aria-controls=\"bodyId\"\n @click=\"expanded = !expanded\"\n >\n {{ expanded ? t('codeBlock.collapse') : t('codeBlock.expand', { count: rows.length }) }}\n </button>\n </div>\n</template>\n"],"mappings":";;;;;;;;;AAsIA,IAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA5G5B,MAAM,QAAQ;EAYd,MAAM,OAAO;EACb,MAAM,QAAQA,SAAA;EAEd,MAAM,KAAK,aAAa,YAAY;EACpC,MAAM,EAAE,MAAM,UAAU;EACxB,MAAM,SAAS,gBAAgB;EAC/B,MAAM,SAAS,kBAAkB,MAAM;EAIvC,MAAM,cAAc,eAEhB,MAAM,aACL,OAAO,MAAM,SAAqE,cAC/E,SACR;EAEA,MAAM,OAAO,eAAe,MAAM,KAAK,QAAQ,OAAO,EAAE,CAAC;EASzD,MAAM,QAAQ,WAAqB,WAAW,KAAK,KAAK,CAAC;EACzD,IAAI,MAAM;EAEV,IAAI;EACJ,kBAAkB;GAChB,MAAM,UAAU,EAAE;GAClB,MAAM,YAAY,YAAY;GAC9B,MAAM,MAAM,MAAM;GAElB,MAAM,MAAM,MAAM,YAAY,IAAI,YAAY,IAAI,IAAI,IAAI,IAAI;GAC9D,MAAM,SAAS,IAAI,MAAM,GAAG,GAAG,CAAC,CAAC,QAAQ,OAAO,EAAE;GAClD,MAAM,OAAO,MAAM,YAAY,IAAI,MAAM,GAAG,IAAI;GAChD,MAAM,QAAQ,OAAO,WAAW,IAAI,IAAI,CAAC;GACzC,IAAI,CAAC,aAAa,CAAC,QAAQ;IACzB,MAAM,QAAQ,WAAW,KAAK,KAAK;IACnC;GACF;GACA,IAAI,QAAQ,WAAW,QAAQ;IAC7B,MAAM,QAAQ,MAAM,SAAS,CAAC,GAAG,OAAO,OAAO,GAAG,KAAK,IAAI,OAAO;IAClE;GACF;GACA,MAAM,OAAO,UAAU,OAAO,WAAW,GAAG,OAAO,OAAO,GAAG,IAAI,SAAS,KAAA;GAC1E,MAAM,QAAQ,OAC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A,oBAAA,IAAyBA,MAAA,CAAA,CAAC,CAAA,oBAAA,EAAA,OAA8B,KAAA,MAAK,OAAM,CAAA,CAAA,GAAA,IAAA,UAAA,K"}
|
|
1
|
+
{"version":3,"file":"code-block.vue_vue_type_script_setup_true_lang.js","names":["_useSlots","_openBlock","_createElementBlock","_normalizeClass","_normalizeStyle","_unref","_createElementVNode","_renderSlot","_createTextVNode","_toDisplayString","_createBlock","_createCommentVNode","_Fragment","_renderList"],"sources":["../../../src/components/code-block/code-block.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport {\n computed,\n onBeforeUnmount,\n onMounted,\n ref,\n shallowRef,\n useId,\n useTemplateRef,\n watch,\n} from 'vue'\nimport { useNamespace } from '../../utils/namespace'\nimport { useGlobalConfig, useLocale } from '../config-provider/use-config'\nimport { DouCopyButton } from '../copy-button'\nimport type {\n CodeBlockEmits,\n CodeBlockExpose,\n CodeBlockHighlighter,\n CodeBlockProps,\n CodeBlockSlots,\n} from './code-block'\nimport { htmlLines, parseLines, plainLines } from './lines'\n\ndefineOptions({ name: 'DouCodeBlock' })\n\nconst props = withDefaults(defineProps<CodeBlockProps>(), {\n code: '',\n lang: 'text',\n lineNumbers: false,\n startLine: 1,\n wrap: false,\n maxHeight: undefined,\n copyable: true,\n streaming: false,\n autoScroll: undefined,\n bordered: true,\n})\nconst emit = defineEmits<CodeBlockEmits>()\nconst slots = defineSlots<CodeBlockSlots>()\n\nconst ns = useNamespace('code-block')\nconst { t } = useLocale()\nconst config = useGlobalConfig()\nconst bodyId = `dou-code-block-${useId()}`\n\n// A function prop cannot take its default from ConfigProvider the way\n// other props do, so the configured one is read here.\nconst highlighter = computed<CodeBlockHighlighter | undefined>(\n () =>\n props.highlight ??\n (config.value.defaults as { DouCodeBlock?: { highlight?: CodeBlockHighlighter } }).DouCodeBlock\n ?.highlight,\n)\n\nconst code = computed(() => props.code.replace(/\\n$/, ''))\n\nconst split = computed(() => {\n const raw = props.code\n // while streaming, the line still being written stays plain\n if (!props.streaming) return { source: code.value, tail: '' }\n const cut = raw.lastIndexOf('\\n') + 1\n return {\n source: raw.slice(0, cut).replace(/\\n$/, ''),\n tail: raw.slice(cut),\n }\n})\n\ninterface LandedHighlight {\n source: string\n lang: string\n highlight: CodeBlockHighlighter\n lines: string[]\n}\n\n/*\n * Plain lines until the highlighter answers. A synchronous highlighter\n * answers during setup, so server-rendered markup is already highlighted;\n * an async one fills in after. Only the latest call may land, and any\n * previous in-flight run is aborted when inputs change or the block unmounts.\n * While the next answer is on its way, lines that were highlighted before\n * and have not changed keep their colors.\n */\nconst landed = shallowRef<LandedHighlight | undefined>()\nlet run = 0\nlet activeController: AbortController | undefined\n\nfunction abortActiveHighlight() {\n const controller = activeController\n activeController = undefined\n controller?.abort()\n}\n\nwatch(\n [highlighter, () => split.value.source, () => props.lang],\n ([highlight, source, lang], _prev, onCleanup) => {\n const current = ++run\n abortActiveHighlight()\n if (!highlight || !source) {\n landed.value = undefined\n return\n }\n const last = landed.value\n if (last && last.highlight === highlight && last.lang === lang && last.source === source) {\n return\n }\n const controller = new AbortController()\n activeController = controller\n onCleanup(() => {\n if (activeController === controller) activeController = undefined\n controller.abort()\n })\n const apply = (html: string) => {\n if (current !== run || controller.signal.aborted) return\n landed.value = { source, lang, highlight, lines: htmlLines(html) }\n }\n try {\n const result = highlight(source, lang, { signal: controller.signal })\n if (typeof result === 'string') {\n apply(result)\n } else if (result && typeof result.then === 'function') {\n void result.then(apply, () => {})\n }\n } catch {\n // a highlighter that throws leaves the plain text\n }\n },\n { immediate: true },\n)\n\nconst lines = computed<string[]>(() => {\n const { source, tail } = split.value\n const after = tail ? plainLines(tail) : []\n const highlight = highlighter.value\n if (!highlight || !source) return plainLines(code.value)\n const last = landed.value\n if (last && last.highlight === highlight && last.lang === props.lang) {\n if (last.source === source) return after.length ? [...last.lines, ...after] : last.lines\n if (source.startsWith(`${last.source}\\n`)) {\n return [...last.lines, ...plainLines(source.slice(last.source.length + 1)), ...after]\n }\n }\n return [...plainLines(source), ...after]\n})\n\nconst marked = computed(() => parseLines(props.highlightLines))\nconst rows = computed(() =>\n lines.value.map((html, index) => {\n const number = props.startLine + index\n return { html, number, marked: marked.value.has(number) }\n }),\n)\n\nconst expanded = ref(false)\nconst foldable = computed(() => props.maxLines !== undefined && rows.value.length > props.maxLines)\nconst folded = computed(() => foldable.value && !expanded.value)\n\nconst maxHeightValue = computed(() => {\n if (props.maxHeight === undefined || props.maxHeight === '') return undefined\n return typeof props.maxHeight === 'number' ? `${props.maxHeight}px` : props.maxHeight\n})\nconst scrollable = computed(() => maxHeightValue.value !== undefined)\n\nconst bodyRef = useTemplateRef<HTMLElement>('body')\nconst codeRef = useTemplateRef<HTMLElement>('codeEl')\nconst scrollbarWidth = ref(0)\n\nfunction syncScrollbarWidth() {\n const el = bodyRef.value\n if (!el || !scrollable.value || folded.value) {\n scrollbarWidth.value = 0\n return\n }\n scrollbarWidth.value = Math.max(0, el.offsetWidth - el.clientWidth)\n}\n\nconst AT_BOTTOM_THRESHOLD = 24\nconst shouldAutoScroll = computed(() => props.autoScroll ?? props.streaming)\nconst atBottom = ref(true)\nwatch(atBottom, (value) => emit('atBottomChange', value))\n\n// Following is a choice of the reader: scrolling up pauses it, and reaching\n// the bottom resumes it. Appending content alone never turns it off.\nlet following = true\nlet scrollingToBottom = false\nlet lastScrollTop = 0\n\nfunction distanceToBottom(el: HTMLElement) {\n return el.scrollHeight - el.scrollTop - el.clientHeight\n}\n\nfunction isNearBottom(el: HTMLElement) {\n return distanceToBottom(el) <= AT_BOTTOM_THRESHOLD\n}\n\nfunction onBodyScroll() {\n const el = bodyRef.value\n if (!el) return\n const distance = distanceToBottom(el)\n const near = distance <= AT_BOTTOM_THRESHOLD\n if (el.scrollTop < lastScrollTop && distance > 1) {\n scrollingToBottom = false\n following = false\n } else if (near) {\n following = true\n scrollingToBottom = false\n } else if (scrollingToBottom && el.scrollTop >= lastScrollTop) {\n following = true\n } else if (el.scrollTop !== lastScrollTop) {\n scrollingToBottom = false\n following = false\n }\n lastScrollTop = el.scrollTop\n atBottom.value = near\n}\n\nfunction onBodyWheel(event: WheelEvent) {\n const el = bodyRef.value\n if (event.deltaY < 0 && el && el.scrollHeight > el.clientHeight) {\n following = false\n scrollingToBottom = false\n }\n}\n\nfunction scrollToBottom(behavior: ScrollBehavior = 'auto') {\n const el = bodyRef.value\n if (!el) return\n following = true\n const maxTop = Math.max(0, el.scrollHeight - el.clientHeight)\n if (behavior === 'smooth' && typeof el.scrollTo === 'function') {\n el.scrollTo({ top: el.scrollHeight, behavior })\n const near = isNearBottom(el)\n scrollingToBottom = !near\n lastScrollTop = el.scrollTop\n atBottom.value = near\n } else {\n scrollingToBottom = false\n el.scrollTop = maxTop\n lastScrollTop = el.scrollTop\n atBottom.value = true\n }\n}\n\nfunction follow() {\n syncScrollbarWidth()\n const el = bodyRef.value\n if (!el) return\n if (!shouldAutoScroll.value || !following || folded.value) {\n atBottom.value = isNearBottom(el)\n return\n }\n scrollingToBottom = false\n el.scrollTop = Math.max(0, el.scrollHeight - el.clientHeight)\n lastScrollTop = el.scrollTop\n atBottom.value = true\n}\n\nlet resizeObserver: ResizeObserver | undefined\nonMounted(() => {\n follow()\n if (typeof ResizeObserver === 'undefined') return\n resizeObserver = new ResizeObserver(follow)\n if (bodyRef.value) resizeObserver.observe(bodyRef.value)\n if (codeRef.value) resizeObserver.observe(codeRef.value)\n})\nonBeforeUnmount(() => {\n abortActiveHighlight()\n resizeObserver?.disconnect()\n})\n\nwatch(\n [() => props.code, lines, maxHeightValue, () => props.wrap, folded, shouldAutoScroll],\n follow,\n { flush: 'post' },\n)\n\nconst title = computed(() => props.filename ?? (props.lang === 'text' ? '' : props.lang))\nconst hasHeader = computed(() => !!(slots.title || title.value || slots.actions))\n\nconst classes = computed(() => [\n ns.b(),\n ns.is('wrap', props.wrap),\n ns.is('numbered', props.lineNumbers),\n ns.is('folded', folded.value),\n ns.is('scrollable', scrollable.value),\n ns.is('headless', !hasHeader.value),\n ns.is('streaming', props.streaming),\n ns.is('bordered', props.bordered),\n])\n\nconst rootStyle = computed(() => {\n const style: Record<string, string | number> = {}\n if (foldable.value && props.maxLines !== undefined) {\n style['--dou-code-block-max-lines'] = props.maxLines\n }\n if (maxHeightValue.value !== undefined) {\n style['--dou-code-block-max-height'] = maxHeightValue.value\n }\n if (scrollbarWidth.value > 0) {\n style['--dou-code-block-scrollbar-width'] = `${scrollbarWidth.value}px`\n }\n return Object.keys(style).length ? style : undefined\n})\n\ndefineExpose<CodeBlockExpose>({\n get body() {\n return bodyRef.value\n },\n scrollToBottom,\n get atBottom() {\n return atBottom.value\n },\n})\n</script>\n\n<template>\n <div :class=\"classes\" :style=\"rootStyle\">\n <div v-if=\"hasHeader\" :class=\"ns.e('header')\">\n <span :class=\"ns.e('title')\">\n <slot name=\"title\">{{ title }}</slot>\n </span>\n <span :class=\"ns.e('actions')\">\n <slot name=\"actions\" />\n <DouCopyButton v-if=\"copyable\" :text=\"code\" size=\"sm\" />\n </span>\n </div>\n <DouCopyButton v-else-if=\"copyable\" :class=\"ns.e('copy')\" :text=\"code\" size=\"sm\" />\n <div\n :id=\"bodyId\"\n ref=\"body\"\n :class=\"ns.e('body')\"\n :data-lang=\"lang\"\n :tabindex=\"scrollable && !folded ? 0 : undefined\"\n @scroll.passive=\"onBodyScroll\"\n @wheel.passive=\"onBodyWheel\"\n >\n <code ref=\"codeEl\" :class=\"ns.e('code')\">\n <span\n v-for=\"row in rows\"\n :key=\"row.number\"\n :class=\"[ns.e('row'), ns.is('marked', row.marked)]\"\n >\n <span v-if=\"lineNumbers\" :class=\"ns.e('number')\" aria-hidden=\"true\">{{\n row.number\n }}</span>\n <span :class=\"ns.e('line')\" v-html=\"row.html\" />\n </span>\n </code>\n </div>\n <button\n v-if=\"foldable\"\n type=\"button\"\n :class=\"ns.e('toggle')\"\n :aria-expanded=\"expanded\"\n :aria-controls=\"bodyId\"\n @click=\"expanded = !expanded\"\n >\n {{ expanded ? t('codeBlock.collapse') : t('codeBlock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,EAAA,CAAE,CAAC,GAAE,UAAW,IAAI,MAAM,CAAA,CAAA;K,IAEpC,QAAA,eAAZJ,UAAA,GAAAC,mBAES,QAAA;;MAFiB,OAAKC,eAAEE,MAAA,EAAA,CAAE,CAAC,EAAC,QAAA,CAAA;MAAY,eAAY;KAC3D,GAAAI,gBAAA,IAAI,MAAM,GAAA,CAAA,K,8BAEZH,mBAAgD,QAAA;MAAzC,OAAKH,eAAEE,MAAA,EAAA,CAAE,CAAC,EAAC,MAAA,CAAA;MAAU,WAAQ,IAAI;;;IAKtC,SAAA,SADRJ,UAAA,GAAAC,mBASS,UAAA;;KAPP,MAAK;KACJ,OAAKC,eAAEE,MAAA,EAAA,CAAE,CAAC,EAAC,QAAA,CAAA;KACX,iBAAe,SAAA;KACf,iBAAe;KACf,SAAK,OAAA,OAAA,OAAA,MAAA,WAAE,SAAA,QAAQ,CAAI,SAAA;IAEjB,GAAAI,gBAAA,SAAA,QAAWJ,MAAA,CAAA,CAAC,CAAA,oBAAA,IAAyBA,MAAA,CAAA,CAAC,CAAA,oBAAA,EAAA,OAA8B,KAAA,MAAK,OAAM,CAAA,CAAA,GAAA,IAAA,UAAA,K"}
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
export interface CodeBlockHighlightContext {
|
|
2
|
+
/** Aborted when a newer highlight supersedes this run or when the block unmounts. */
|
|
3
|
+
signal: AbortSignal;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* Turns code into highlighted HTML. Shaped like shiki's `codeToHtml`, so
|
|
3
7
|
* `(code, lang) => codeToHtml(code, { lang, themes, defaultColor: false })`
|
|
4
8
|
* works as is. Any highlighter fits as long as it returns lines as
|
|
5
9
|
* `<span class="line">…</span>` separated by newlines, with or without a
|
|
6
|
-
* `<pre><code>` around them.
|
|
10
|
+
* `<pre><code>` around them. An optional third argument carries an
|
|
11
|
+
* `AbortSignal` so async highlighters (such as a worker pool) can cancel
|
|
12
|
+
* queued work when the code, language, or highlighter changes or when the
|
|
13
|
+
* block unmounts.
|
|
7
14
|
*/
|
|
8
|
-
export type CodeBlockHighlighter = (code: string, lang: string) => string | Promise<string>;
|
|
15
|
+
export type CodeBlockHighlighter = (code: string, lang: string, context?: CodeBlockHighlightContext) => string | Promise<string>;
|
|
9
16
|
export interface CodeBlockProps {
|
|
10
17
|
/** The source to show. */
|
|
11
18
|
code?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dou-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.25",
|
|
4
4
|
"description": "为自己打造的 Vue 3 组件库:简洁克制,样式与组件分离,内置无障碍支持",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vue",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"vue": "^3.5.0"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@dou-ui/icons": "0.1.0-alpha.
|
|
53
|
-
"@dou-ui/theme": "0.1.0-alpha.
|
|
52
|
+
"@dou-ui/icons": "0.1.0-alpha.25",
|
|
53
|
+
"@dou-ui/theme": "0.1.0-alpha.25"
|
|
54
54
|
},
|
|
55
55
|
"publishConfig": {
|
|
56
56
|
"access": "public"
|