feat(grid): flatten multiline cell text

This commit is contained in:
ptma authored and GitHub committed 2026-08-13 11:40:22 +08:00
1 parent 01e67ee9a4
commit ee08f356e9
17 files changed
+97 -30

No files matched your search

+26 -10
View File
@@ -3,22 +3,38 @@ import { firstLineCellDisplayValue } from "@/lib/dataGrid/cellValue";
describe("firstLineCellDisplayValue", () => {
it("shows only the first line in fixed-height cells", () => {
expect(firstLineCellDisplayValue("111\n222")).toBe("111");
expect(firstLineCellDisplayValue("111\r\n222")).toBe("111");
expect(firstLineCellDisplayValue("111\r222")).toBe("111");
expect(firstLineCellDisplayValue(null, false)).toBe(null);
expect(firstLineCellDisplayValue("111\n222", false)).toBe("111");
expect(firstLineCellDisplayValue("111\r\n222", false)).toBe("111");
expect(firstLineCellDisplayValue("111\r222", false)).toBe("111");
expect(firstLineCellDisplayValue("single line")).toBe("single line");
expect(firstLineCellDisplayValue(null, true)).toBe(null);
expect(firstLineCellDisplayValue("111\n222", true)).toBe("111¶222");
expect(firstLineCellDisplayValue("111\r\n222", true)).toBe("111¶222");
expect(firstLineCellDisplayValue("111\r222", true)).toBe("111¶222");
expect(firstLineCellDisplayValue("single line", true)).toBe("single line");
});
it("skips leading blank lines without stripping content indentation", () => {
expect(firstLineCellDisplayValue("\nINNODB MONITOR OUTPUT\nbody")).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue("\r\nINNODB MONITOR OUTPUT\r\nbody")).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue("\rINNODB MONITOR OUTPUT\rbody")).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue(" \n indented content\nbody")).toBe(" indented content");
expect(firstLineCellDisplayValue("\nINNODB MONITOR OUTPUT\nbody", false)).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue("\r\nINNODB MONITOR OUTPUT\r\nbody", false)).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue("\rINNODB MONITOR OUTPUT\rbody", false)).toBe("INNODB MONITOR OUTPUT");
expect(firstLineCellDisplayValue(" \n indented content\nbody", false)).toBe(" indented content");
expect(firstLineCellDisplayValue("\nINNODB MONITOR OUTPUT\nbody", true)).toBe("¶INNODB MONITOR OUTPUT¶body");
expect(firstLineCellDisplayValue("\r\nINNODB MONITOR OUTPUT\r\nbody", true)).toBe("¶INNODB MONITOR OUTPUT¶body");
expect(firstLineCellDisplayValue("\rINNODB MONITOR OUTPUT\rbody", true)).toBe("¶INNODB MONITOR OUTPUT¶body");
expect(firstLineCellDisplayValue(" \n indented content\nbody", true)).toBe(" ¶ indented content¶body");
});
it("preserves values that do not contain a content line", () => {
expect(firstLineCellDisplayValue("")).toBe("");
expect(firstLineCellDisplayValue(" ")).toBe(" ");
expect(firstLineCellDisplayValue("\n\r\n \r")).toBe("\n\r\n \r");
expect(firstLineCellDisplayValue("", false)).toBe("");
expect(firstLineCellDisplayValue(" ", false)).toBe(" ");
expect(firstLineCellDisplayValue("\n\r\n \r", false)).toBe("\n\r\n \r");
expect(firstLineCellDisplayValue("", true)).toBe("");
expect(firstLineCellDisplayValue(" ", true)).toBe(" ");
expect(firstLineCellDisplayValue("\n\r\n \r", true)).toBe("¶¶ ¶");
});
});
@@ -30,6 +30,10 @@ describe("dataGridRuntime", () => {
expect(dataGridSource).toContain(":title=\"t('grid.queryError')\"");
});
it("redraws canvas rows when multiline flattening changes", () => {
expect(dataGridSource).toContain("watch(flatteningMultiLineEnabled, () => scheduleCanvasDraw());");
});
it("disposes registered resources in reverse order and only once", () => {
const scope = createDataGridRuntimeScope();
const calls: string[] = [];
@@ -95,6 +95,7 @@ export interface DrawCanvasDataGridOptions {
colorizeDataTypes?: boolean;
rightAlignedActionCell?: CanvasRightAlignedActionCell | null;
booleanDisplayMode?: "checkbox" | "dropdown";
flatteningMultiLineEnabled: boolean;
}
type NumericCanvasContext = CanvasRenderingContext2D & {
@@ -353,6 +354,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
rightAlignedActionCell,
columnIsBoolean,
booleanDisplayMode = "dropdown",
flatteningMultiLineEnabled,
} = options;
// 框选热路径:整次绘制只判断一次。常见情况(单矩形 / 多列且每段都是多格)可跳过逐格 kind 查询
const paintSelectionOuterFrame = dataGridSelectionUsesOuterFrame(selectionFrames);
@@ -575,7 +577,7 @@ export function drawCanvasDataGrid(options: DrawCanvasDataGridOptions) {
}
} else {
const rawDisplayText = item.isDraft && value === null ? (draftCellPlaceholder ?? "") : formatCell(value, actualColIdx);
const displayText = isEditingThisCell ? "" : firstLineCellDisplayValue(rawDisplayText);
const displayText = isEditingThisCell ? "" : firstLineCellDisplayValue(rawDisplayText, flatteningMultiLineEnabled);
const text = isEditingThisCell ? displayText : fitCanvasText(ctx, displayText, cellMaxWidth, isBooleanNullCell ? "left" : isRightAlign ? "right" : "left");
const anchorX = isBooleanNullCell ? alignCanvasPixel(drawX + colWidth / 2, scaleX) : textAnchorX;
ctx.fillText(text, anchorX, textY);
+19 -15
View File
@@ -10,26 +10,30 @@ export function displayCellValue(value: CellValue): string {
return String(value);
}
export function firstLineCellDisplayValue(value: string): string {
export function firstLineCellDisplayValue(value: string, flatteningMultiLine: boolean): string {
const lineBreakPattern = /\r\n|\r|\n/g;
const firstLineBreak = lineBreakPattern.exec(value);
if (!firstLineBreak) return value;
if (flatteningMultiLine) {
return value && value.replaceAll(lineBreakPattern, "¶");
} else {
const firstLineBreak = lineBreakPattern.exec(value);
if (!firstLineBreak) return value;
const firstLine = value.slice(0, firstLineBreak.index);
if (/\S/u.test(firstLine)) return firstLine;
const firstLine = value.slice(0, firstLineBreak.index);
if (/\S/u.test(firstLine)) return firstLine;
let lineStart = lineBreakPattern.lastIndex;
let lineStart = lineBreakPattern.lastIndex;
while (lineStart <= value.length) {
const lineBreak = lineBreakPattern.exec(value);
const lineEnd = lineBreak?.index ?? value.length;
const line = value.slice(lineStart, lineEnd);
if (/\S/u.test(line)) return line;
if (!lineBreak) return value;
lineStart = lineBreakPattern.lastIndex;
while (lineStart <= value.length) {
const lineBreak = lineBreakPattern.exec(value);
const lineEnd = lineBreak?.index ?? value.length;
const line = value.slice(lineStart, lineEnd);
if (/\S/u.test(line)) return line;
if (!lineBreak) return value;
lineStart = lineBreakPattern.lastIndex;
}
return value;
}
return value;
}
export function limitDataGridCellDisplay(value: string, maxLength = DATA_GRID_CELL_DISPLAY_MAX_LENGTH): string {
@@ -43,6 +43,7 @@ export const EDITOR_SETTINGS_DRAFT_KEYS = [
"infiniteScroll",
"regexMaxMatchCount",
"autoCalculateTotalRows",
"flatteningMultiLineText",
"tableColumnTemplateFields",
"shortcuts",
"sqlFormatter",
@@ -180,6 +180,7 @@ export const SETTINGS_SEARCH_DEFINITIONS: readonly SettingsSearchDefinition[] =
{ id: "data-grid-infinite-scroll", category: "data", titleKey: "settings.infiniteScroll", descriptionKey: "settings.infiniteScrollDescription", targetId: "data" },
{ id: "data-grid-auto-transpose", category: "data", titleKey: "settings.dataGridAutoTransposeSingleRow", descriptionKey: "settings.dataGridAutoTransposeSingleRowDescription", targetId: "data" },
{ id: "data-grid-quick-entry", category: "data", titleKey: "settings.dataGridQuickEntry", descriptionKey: "settings.dataGridQuickEntryDescription", targetId: "data" },
{ id: "data-grid-flattening-multi-line", category: "data", titleKey: "settings.flatteningMultiLineText", descriptionKey: "settings.flatteningMultiLineTextDescription", targetId: "data" },
{ id: "appearance-toolbar", category: "appearance", titleKey: "settings.toolbarTitle", descriptionKey: "settings.toolbarHiddenHint", targetId: "appearance" },
{ id: "appearance-exclusive-sidebar-panels", category: "appearance", titleKey: "settings.exclusiveRightSidebarPanels", descriptionKey: "settings.exclusiveRightSidebarPanelsDescription", targetId: "appearance" },
...createToolbarVisibilitySettingsSearchDefinitions(),