Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(frontend): simplify HighlightCodeBlock and fix text overflow (#18205)

boojackac6df143f89b
diff --git a/frontend/src/components/HighlightCodeBlock.ts b/frontend/src/components/HighlightCodeBlock.ts
deleted file mode 100644
index 8749b4ab9b..0000000000
--- a/frontend/src/components/HighlightCodeBlock.ts
+++ /dev/null
@@ -1,208 +0,0 @@
-import { useVirtualizer } from "@tanstack/vue-virtual";
-import hljs from "highlight.js/lib/core";
-import { computed, defineComponent, h, onMounted, ref, watch } from "vue";
-
-// Enable virtual scrolling for code with more lines than this
-const VIRTUAL_SCROLL_THRESHOLD = 100;
-
-export default defineComponent({
- name: "HighlightCodeBlock",
- props: {
- code: {
- type: String,
- required: true,
- },
- language: {
- type: String,
- default: "sql",
- },
- tag: {
- type: String,
- default: "pre",
- },
- lazy: {
- type: Boolean,
- default: false,
- },
- virtual: {
- type: Boolean,
- default: false,
- },
- lineHeight: {
- type: Number,
- default: 20,
- },
- },
- setup(props) {
- const highlighted = ref(!props.lazy);
- const highlightedCode = ref("");
- const containerRef = ref<HTMLElement>();
- const lineCache = ref<Map<number, string>>(new Map());
-
- const lines = computed(() => props.code.split("\n"));
- const shouldUseVirtual = computed(
- () => props.virtual && lines.value.length > VIRTUAL_SCROLL_THRESHOLD
- );
-
- const highlightLine = (line: string): string => {
- try {
- const result = hljs.highlight(line, {
- language: props.language,
- ignoreIllegals: true,
- });
- return result.value;
- } catch {
- return line;
- }
- };
-
- const doHighlight = () => {
- if (shouldUseVirtual.value) {
- // For virtual mode, highlighting happens per-line on demand
- highlighted.value = true;
- return;
- }
-
- const highlightFn = () => {
- const result = hljs.highlight(props.code, {
- language: props.language,
- });
- highlightedCode.value = result.value;
- highlighted.value = true;
- };
-
- if (props.lazy) {
- // Async highlighting
- if (typeof requestIdleCallback !== "undefined") {
- requestIdleCallback(highlightFn);
- } else {
- setTimeout(highlightFn, 0);
- }
- } else {
- // Sync highlighting
- highlightFn();
- }
- };
-
- // Virtual scrolling setup
- const virtualizer = useVirtualizer(
- computed(() => ({
- count: lines.value.length,
- getScrollElement: () => containerRef.value ?? null,
- estimateSize: () => props.lineHeight,
- overscan: 10,
- }))
- );
-
- const virtualItems = computed(() => virtualizer.value.getVirtualItems());
-
- // Initialize highlighting
- if (!props.lazy) {
- doHighlight();
- } else {
- onMounted(() => {
- doHighlight();
- });
- }
-
- watch(
- () => props.code,
- () => {
- highlighted.value = false;
- lineCache.value.clear();
- doHighlight();
- }
- );
-
- return {
- highlighted,
- highlightedCode,
- containerRef,
- lines,
- shouldUseVirtual,
- virtualizer,
- virtualItems,
- highlightLine,
- lineCache,
- };
- },
- render() {
- const { code, language, tag } = this.$props;
- const { class: additionalClass } = this.$attrs;
-
- // Virtual scrolling mode
- if (this.shouldUseVirtual && this.highlighted) {
- const virtualHeight = this.virtualizer.getTotalSize();
- const virtualStart = this.virtualItems[0]?.start ?? 0;
-
- return h(
- "div",
- {
- ref: "containerRef",
- class: ["overflow-auto", additionalClass],
- },
- [
- h(
- "div",
- {
- style: {
- height: `${virtualHeight}px`,
- position: "relative",
- },
- },
- [
- h(
- tag,
- {
- class: [language],
- style: {
- position: "absolute",
- top: 0,
- left: 0,
- width: "100%",
- transform: `translateY(${virtualStart}px)`,
- margin: 0,
- },
- },
- this.virtualItems.map((virtualRow) => {
- const lineIndex = virtualRow.index;
- const line = this.lines[lineIndex];
-
- // Check cache first
- if (!this.lineCache.has(lineIndex)) {
- this.lineCache.set(lineIndex, this.highlightLine(line));
- }
-
- return h("div", {
- key: String(virtualRow.key),
- style: {
- height: `${virtualRow.size}px`,
- },
- innerHTML: this.lineCache.get(lineIndex),
- });
- })
- ),
- ]
- ),
- ]
- );
- }
-
- // Non-virtual mode - show plain text while highlighting
- if (!this.highlighted) {
- return h(
- tag,
- {
- class: [language, additionalClass],
- },
- code
- );
- }
-
- // Highlighted content
- return h(tag, {
- class: [language, additionalClass],
- innerHTML: this.highlightedCode,
- });
- },
-});
diff --git a/frontend/src/components/HighlightCodeBlock.vue b/frontend/src/components/HighlightCodeBlock.vue
new file mode 100644
index 0000000000..d7c953e901
--- /dev/null
+++ b/frontend/src/components/HighlightCodeBlock.vue
@@ -0,0 +1,96 @@
+<template>
+ <!-- eslint-disable-next-line vue/no-v-text-v-html-on-component -->
+ <component :is="tag" :class="[language, $attrs.class]" v-html="highlightedCode" />
+</template>
+
+<script lang="ts" setup>
+import hljs from "highlight.js/lib/core";
+import { computed, onMounted, ref, watch } from "vue";
+
+const props = withDefaults(
+ defineProps<{
+ code: string;
+ language?: string;
+ tag?: string;
+ lazy?: boolean;
+ }>(),
+ {
+ language: "sql",
+ tag: "pre",
+ lazy: false,
+ }
+);
+
+defineOptions({
+ inheritAttrs: false,
+});
+
+const isReady = ref(!props.lazy);
+const cachedHtml = ref("");
+
+const highlightedCode = computed(() => {
+ if (!isReady.value) {
+ return escapeHtml(props.code);
+ }
+ return cachedHtml.value || escapeHtml(props.code);
+});
+
+const escapeHtml = (text: string): string => {
+ if (!text) return "";
+ return text
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;");
+};
+
+const doHighlight = () => {
+ if (!props.code) {
+ cachedHtml.value = "";
+ isReady.value = true;
+ return;
+ }
+
+ try {
+ const result = hljs.highlight(props.code, {
+ language: props.language,
+ ignoreIllegals: true,
+ });
+ cachedHtml.value = result.value;
+ } catch {
+ cachedHtml.value = escapeHtml(props.code);
+ }
+ isReady.value = true;
+};
+
+const scheduleHighlight = () => {
+ if (props.lazy) {
+ if (typeof requestIdleCallback !== "undefined") {
+ requestIdleCallback(() => doHighlight(), { timeout: 100 });
+ } else {
+ setTimeout(doHighlight, 0);
+ }
+ } else {
+ doHighlight();
+ }
+};
+
+// Initialize
+if (!props.lazy) {
+ doHighlight();
+}
+
+onMounted(() => {
+ if (props.lazy) {
+ scheduleHighlight();
+ }
+});
+
+// Watch for changes
+watch(
+ () => [props.code, props.language] as const,
+ () => {
+ isReady.value = false;
+ scheduleHighlight();
+ }
+);
+</script>
diff --git a/frontend/src/components/Plan/components/RolloutView/v2/TaskItem.vue b/frontend/src/components/Plan/components/RolloutView/v2/TaskItem.vue
index 8a429a1e3c..3b37ba27ec 100644
--- a/frontend/src/components/Plan/components/RolloutView/v2/TaskItem.vue
+++ b/frontend/src/components/Plan/components/RolloutView/v2/TaskItem.vue
@@ -84,7 +84,7 @@
</NTag>
<div
:class="readonly ? '' : 'cursor-pointer'"
- class="text-xs text-gray-600 font-mono truncate"
+ class="text-xs text-gray-600 font-mono truncate min-w-0"
@click="handleNavigateToDetail"
>
{{ statementPreview }}
@@ -161,8 +161,7 @@
:code="displayedStatement"
language="sql"
:lazy="true"
- :virtual="true"
- class="text-sm whitespace-pre-wrap wrap-break-word max-h-64 rounded p-2 bg-white border border-gray-200"
+ class="text-sm whitespace-pre-wrap wrap-break-word max-h-64 overflow-auto rounded p-2 bg-white border border-gray-200"
/>
</div>
@@ -247,7 +246,7 @@ import { NButton, NCheckbox, NTag } from "naive-ui";
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import BBSpin from "@/bbkit/BBSpin.vue";
-import HighlightCodeBlock from "@/components/HighlightCodeBlock";
+import HighlightCodeBlock from "@/components/HighlightCodeBlock.vue";
import Timestamp from "@/components/misc/Timestamp.vue";
import { usePlanContextWithRollout } from "@/components/Plan";
import DatabaseDisplay from "@/components/Plan/components/common/DatabaseDisplay.vue";
diff --git a/frontend/src/plugins/ai/components/ChatView/Markdown/Markdown.vue b/frontend/src/plugins/ai/components/ChatView/Markdown/Markdown.vue
index 9b549f5791..f18df482b2 100644
--- a/frontend/src/plugins/ai/components/ChatView/Markdown/Markdown.vue
+++ b/frontend/src/plugins/ai/components/ChatView/Markdown/Markdown.vue
@@ -20,7 +20,7 @@ import remarkGfm from "remark-gfm";
import remarkParse from "remark-parse";
import { unified } from "unified";
import { computed } from "vue";
-import HighlightCodeBlock from "@/components/HighlightCodeBlock";
+import HighlightCodeBlock from "@/components/HighlightCodeBlock.vue";
import AstToMarkdown from "./AstToVNode.vue";
import CodeBlock, { type CodeBlockProps } from "./CodeBlock.vue";
diff --git a/frontend/src/plugins/highlight.ts b/frontend/src/plugins/highlight.ts
index f5d5153cb1..cbdcddb3b7 100644
--- a/frontend/src/plugins/highlight.ts
+++ b/frontend/src/plugins/highlight.ts
@@ -1,16 +1,12 @@
-import "highlight.js/lib/common";
import hljs from "highlight.js/lib/core";
import sql from "highlight.js/lib/languages/sql";
import "highlight.js/styles/github.css";
import type { App } from "vue";
-import HighlightCodeBlock from "@/components/HighlightCodeBlock";
+import HighlightCodeBlock from "@/components/HighlightCodeBlock.vue";
export default {
install(app: App) {
hljs.registerLanguage("sql", sql);
- hljs.configure({
- languages: ["sql"],
- });
app.component("HighlightCodeBlock", HighlightCodeBlock);
},
};