fix(desktop): restore legacy webview dialog and highlight fixes
This commit is contained in:
19 files changed
+810
-332
No files matched your search
@@ -14,6 +14,7 @@
|
||||
import type { AppThemeAppearance } from "@/lib/app/appTheme";
|
||||
import { createAiShikiBlockCodeHighlighter, type AiCodeHighlighter } from "@/lib/ai/aiCodeHighlighter";
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import { sqlSemanticTableNameSpans } from "@/lib/sql/semantic/model";
|
||||
import { LEGACY_WEBVIEW_CLASS } from "@/lib/ui/legacyWebView";
|
||||
|
||||
export interface CodeSnapshotSource {
|
||||
@@ -61,6 +62,13 @@ const SNAPSHOT_CODE_TEXT: Record<AppThemeAppearance, string> = {
|
||||
dark: "#e1e4e8",
|
||||
};
|
||||
|
||||
const SNAPSHOT_TABLE_NAME_COLOR: Record<AppThemeAppearance, string> = {
|
||||
light: "#116329",
|
||||
dark: "#7ee787",
|
||||
};
|
||||
|
||||
const SQL_SNAPSHOT_LANGS = new Set(["sql", "mysql", "postgresql", "postgres", "tsql", "clickhouse", "sqlite"]);
|
||||
|
||||
const TRAFFIC_LIGHT_COLORS = ["#ff5f57", "#febc2e", "#28c840"] as const;
|
||||
const MAX_EXPORT_PIXEL_RATIO = 2;
|
||||
const MAX_EXPORT_CANVAS_EDGE = 16_384;
|
||||
@@ -218,6 +226,81 @@ export function materializeSnapshotCloneLineNumbers(root: ParentNode): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Give SQL table names their own color so they stand apart from field
|
||||
* identifiers — shiki renders quoted table and column identifiers with the
|
||||
* same string token color, while the editor marks table names semantically.
|
||||
* The same metadata-free analyzer as the editor's table-name highlight maps
|
||||
* table spans onto the highlighted HTML; the wrapper carries an inline color,
|
||||
* so the preview and both export paths (clipboard/PNG clone of this DOM) stay
|
||||
* identical.
|
||||
*/
|
||||
export function applySnapshotSqlTableColors(highlightedBody: string, rawCode: string, appearance: AppThemeAppearance): string {
|
||||
let tableSpans: Array<{ start: number; end: number }>;
|
||||
try {
|
||||
tableSpans = sqlSemanticTableNameSpans(rawCode);
|
||||
} catch {
|
||||
return highlightedBody;
|
||||
}
|
||||
if (tableSpans.length === 0) return highlightedBody;
|
||||
|
||||
const parsed = new DOMParser().parseFromString(`<t>${highlightedBody}</t>`, "text/html");
|
||||
const root = parsed.body.firstElementChild;
|
||||
if (!root) return highlightedBody;
|
||||
|
||||
// Line starts in the raw code; shiki splits lines into .line elements whose
|
||||
// text no longer contains the separators, so offsets are mapped per line.
|
||||
const lineStarts: number[] = [0];
|
||||
for (let index = 0; index < rawCode.length; index += 1) {
|
||||
if (rawCode[index] === "\n") lineStarts.push(index + 1);
|
||||
else if (rawCode[index] === "\r") lineStarts.push(index + (rawCode[index + 1] === "\n" ? 2 : 1));
|
||||
}
|
||||
|
||||
const lineElements = Array.from(root.querySelectorAll<HTMLElement>(".line"));
|
||||
const rawLines = rawCode.split(/\r\n|\r|\n/);
|
||||
const color = SNAPSHOT_TABLE_NAME_COLOR[appearance];
|
||||
|
||||
const wrapRanges = (textNode: Text, nodeStart: number, clipEnd: number, totalLength: number) => {
|
||||
const ranges: Array<[number, number]> = [];
|
||||
for (const span of tableSpans) {
|
||||
const start = Math.max(span.start, nodeStart);
|
||||
const end = Math.min(span.end, nodeStart + totalLength, clipEnd);
|
||||
if (start < end) ranges.push([start - nodeStart, end - nodeStart]);
|
||||
}
|
||||
ranges.sort((a, b) => b[0] - a[0]);
|
||||
for (const [relativeStart, relativeEnd] of ranges) {
|
||||
if (relativeEnd < totalLength) textNode.splitText(relativeEnd);
|
||||
const segment = relativeStart > 0 ? textNode.splitText(relativeStart) : textNode;
|
||||
const wrapper = textNode.ownerDocument!.createElement("span");
|
||||
wrapper.className = "dbx-code-snapshot__table-name";
|
||||
wrapper.setAttribute("data-sql-token", "table");
|
||||
// setAttribute (not style.color) keeps the literal color in the DOM;
|
||||
// style.color serializes engine-dependently (rgb() in WebKit/Chromium).
|
||||
wrapper.setAttribute("style", `color:${color}`);
|
||||
segment.parentNode?.insertBefore(wrapper, segment);
|
||||
wrapper.appendChild(segment);
|
||||
}
|
||||
};
|
||||
|
||||
lineElements.forEach((lineElement, lineIndex) => {
|
||||
const lineStart = lineStarts[lineIndex];
|
||||
const lineEnd = lineStart + (rawLines[lineIndex]?.length ?? 0);
|
||||
const walker = lineElement.ownerDocument!.createTreeWalker(lineElement, NodeFilter.SHOW_TEXT);
|
||||
const textNodes: Text[] = [];
|
||||
while (walker.nextNode()) textNodes.push(walker.currentNode as Text);
|
||||
let position = lineStart;
|
||||
for (const textNode of textNodes) {
|
||||
const totalLength = textNode.length;
|
||||
wrapRanges(textNode, position, lineEnd, totalLength);
|
||||
// splitText keeps the leading part in place; the consumed length is the
|
||||
// original node's length regardless of how the ranges split it.
|
||||
position += totalLength;
|
||||
}
|
||||
});
|
||||
|
||||
return root.innerHTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a self-contained snapshot DOM (as an HTML string) for the given code.
|
||||
* The returned string includes its own `<style>` tag and can be injected via
|
||||
@@ -233,6 +316,10 @@ export async function renderCodeSnapshotHtml(source: CodeSnapshotSource, options
|
||||
body = renderEscapedSnapshotLines(source.code);
|
||||
}
|
||||
|
||||
if (SQL_SNAPSHOT_LANGS.has(source.lang.trim().toLowerCase())) {
|
||||
body = applySnapshotSqlTableColors(body, source.code, options.appearance);
|
||||
}
|
||||
|
||||
const appearance = options.appearance;
|
||||
const showBar = options.showTrafficLights !== false || !!source.title;
|
||||
const showLineNumbers = options.showLineNumbers !== false;
|
||||
|
||||
Reference in new issue
Block a user