fix(editor): 合并语句执行状态与运行按钮

This commit is contained in:
zipg authored and GitHub committed 2026-07-20 12:29:15 +08:00
1 parent 3420659020
commit f6bb56dc2f
4 files changed
+219 -94

No files matched your search

@@ -0,0 +1,40 @@
// @vitest-environment happy-dom
import { describe, expect, it } from "vitest";
import { createStatementGutterMarkerDom, shouldShowStatementGutter } from "@/lib/editor/codemirrorStatementGutter";
describe("CodeMirror statement gutter marker", () => {
it("omits the gutter when run buttons and status markers are absent", () => {
expect(shouldShowStatementGutter(false, 0)).toBe(false);
expect(shouldShowStatementGutter(true, 0)).toBe(true);
expect(shouldShowStatementGutter(false, 1)).toBe(true);
});
it("keeps execution status inside the run button column", () => {
const marker = createStatementGutterMarkerDom({
canExecute: true,
executeLabel: "Execute SQL",
status: "success",
statusLabel: "1 statement succeeded",
});
expect(marker.tagName).toBe("BUTTON");
expect(marker.classList.contains("cm-run-statement-marker")).toBe(true);
expect(marker.querySelectorAll(".cm-statement-execution-badge--success")).toHaveLength(1);
expect(marker.getAttribute("aria-label")).toBe("Execute SQL. 1 statement succeeded");
});
it("renders a standalone status marker when run buttons are disabled", () => {
const marker = createStatementGutterMarkerDom({
canExecute: false,
executeLabel: "Execute SQL",
status: "error",
statusLabel: "1 statement failed",
});
expect(marker.tagName).toBe("SPAN");
expect(marker.classList.contains("cm-statement-execution-marker--error")).toBe(true);
expect(marker.querySelectorAll(".cm-statement-execution-badge")).toHaveLength(0);
expect(marker.getAttribute("aria-label")).toBe("1 statement failed");
});
});
@@ -0,0 +1,67 @@
import { createRunStatementButtonDom } from "@/lib/editor/editorThemes";
import type { StatementExecutionMarkerStatus } from "@/lib/tabs/tabPresentation";
export interface StatementGutterMarkerDomOptions {
canExecute: boolean;
executeLabel: string;
status?: StatementExecutionMarkerStatus;
statusLabel?: string;
}
export function shouldShowStatementGutter(showRunButtons: boolean, markerCount: number): boolean {
return showRunButtons || markerCount > 0;
}
function createStatusIconDom(status: StatementExecutionMarkerStatus, includeCircle: boolean) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("viewBox", "0 0 24 24");
svg.setAttribute("fill", "none");
svg.setAttribute("stroke", "currentColor");
svg.setAttribute("stroke-width", includeCircle ? "2" : "3.5");
svg.setAttribute("stroke-linecap", "round");
svg.setAttribute("stroke-linejoin", "round");
svg.setAttribute("aria-hidden", "true");
if (includeCircle) {
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
circle.setAttribute("cx", "12");
circle.setAttribute("cy", "12");
circle.setAttribute("r", "10");
svg.appendChild(circle);
}
const mark = document.createElementNS("http://www.w3.org/2000/svg", "path");
mark.setAttribute("d", status === "error" ? "m16 8-8 8m0-8 8 8" : "m7 12 3 3 7-7");
svg.appendChild(mark);
return svg;
}
export function createStatementGutterMarkerDom(options: StatementGutterMarkerDomOptions): HTMLElement {
const statusLabel = options.statusLabel?.trim();
if (options.canExecute) {
const button = createRunStatementButtonDom(options.executeLabel);
if (!options.status) return button;
const badge = document.createElement("span");
badge.className = `cm-statement-execution-badge cm-statement-execution-badge--${options.status}`;
badge.setAttribute("aria-hidden", "true");
badge.appendChild(createStatusIconDom(options.status, false));
button.appendChild(badge);
if (statusLabel) {
const label = `${options.executeLabel}. ${statusLabel}`;
button.title = label;
button.setAttribute("aria-label", label);
}
return button;
}
const marker = document.createElement("span");
marker.className = `cm-statement-execution-marker cm-statement-execution-marker--${options.status}`;
if (statusLabel) {
marker.title = statusLabel;
marker.setAttribute("aria-label", statusLabel);
}
if (options.status) marker.appendChild(createStatusIconDom(options.status, true));
return marker;
}
+27 -1
View File
@@ -790,6 +790,7 @@ export function buildEditorFontThemeRules(opts?: { fixedHeight?: boolean; scroll
margin: "0",
outline: "none",
padding: "0",
position: "relative",
transition: "color 0.15s, background-color 0.15s",
userSelect: "none",
verticalAlign: "middle",
@@ -805,13 +806,38 @@ export function buildEditorFontThemeRules(opts?: { fixedHeight?: boolean; scroll
background: "rgb(16 185 129 / 0.2)",
color: "rgb(6 95 70)",
},
"&.cm-editor .cm-run-statement-marker svg": {
"&.cm-editor .cm-run-statement-marker > svg": {
display: "block",
flexShrink: "0",
height: "min(14px, 70%)",
pointerEvents: "none",
width: "min(14px, 70%)",
},
".cm-statement-execution-badge": {
alignItems: "center",
borderRadius: "9999px",
bottom: "-1px",
boxShadow: "0 0 0 1px rgb(255 255 255 / 0.9)",
color: "white",
display: "inline-flex",
height: "min(9px, 45%)",
justifyContent: "center",
pointerEvents: "none",
position: "absolute",
right: "-1px",
width: "min(9px, 45%)",
},
".cm-statement-execution-badge--success": {
background: "rgb(5 150 105)",
},
".cm-statement-execution-badge--error": {
background: "rgb(220 38 38)",
},
".cm-statement-execution-badge svg": {
display: "block",
height: "75%",
width: "75%",
},
"&.cm-editor.cm-focused .cm-run-statement-marker:focus-visible": {
outline: "1px solid var(--ring)",
outlineOffset: "1px",