feat(grid): add boolean cell checkbox editing

This commit is contained in:
Diego Fabricio authored and GitHub committed 2026-08-03 15:08:09 +08:00
1 parent a537565875
commit a49e795680
6 files changed
+384 -19

No files matched your search

+134 -4
View File
@@ -135,6 +135,8 @@ import { canFormatCellDetailJson, cellDetailEditorText, compactJsonText, default
import { buildDataGridCellDetail, buildDataGridColumnDetail, buildDataGridRowDetail, CELL_DETAIL_VALUE_PREVIEW_MAX_LENGTH, dataGridColumnDetailJson, dataGridColumnDetailTsv, dataGridRowDetailJson, dataGridRowDetailTsv, type DataGridCellDetail } from "@/lib/dataGrid/dataGridDetail";
import { applyColumnFormatter, buildColumnFormatterKey, getSupportedTimeZoneOptions, normalizeColumnFormatter, resolveColumnFormatter, type ColumnFormatterConfig, type DateTimeFormatterUnit, DateTimePatterns } from "@/lib/dataGrid/columnFormatter";
import { temporalCellEditorConfig, type TemporalCellEditorConfig } from "@/lib/dataGrid/dataGridTemporalEditor";
import { isBooleanCheckboxValue, isBooleanColumnType, isPointInBooleanCheckbox, normalizeBooleanCellValue } from "@/lib/dataGrid/dataGridBooleanColumn";
import { resolveDataGridColumnsByResultIndex } from "@/lib/dataGrid/dataGridColumnMetadata";
import { isCancelSearchShortcut, isCopyCurrentRowShortcut, isDeleteCurrentRowShortcut, isFocusSearchShortcut, isModRShortcut, isSaveShortcut, isToggleTransposeShortcut } from "@/lib/editor/keyboardShortcuts";
import { dataGridHeaderContentWidth, scrollbarGutterWidth } from "@/lib/dataGrid/dataGridScrollGutter";
import { canFetchNextDataGridSegment, canGoNextDataGridPage, dataGridTotalRowCountLabelKey, hasCompleteLocalDataGridResult, resolveDataGridPaginationTotal, type DataGridInexactTotalRowCountMode } from "@/lib/dataGrid/dataGridPagination";
@@ -2945,6 +2947,7 @@ const {
startEdit,
commitEdit,
commitEditAndMaybeAutoSave,
cycleBooleanCellValue,
commitEditFromBlur,
applyCellValue,
restoreCellValue,
@@ -3112,6 +3115,7 @@ function showReadonlyCellDetailsOnDblClick(item: RowItem, rowIndex: number, visi
}
function onDomCellDblClick(item: RowItem, rowIndex: number, visibleColIdx: number, actualColIdx: number, event: MouseEvent) {
if (isBooleanGridCell(item, actualColIdx) && canEditCellItem(item, actualColIdx)) return;
if (showReadonlyCellDetailsOnDblClick(item, rowIndex, visibleColIdx, actualColIdx)) return;
startDomCellEdit(item.id, actualColIdx, formatCellCached(item.data[actualColIdx], actualColIdx), event);
}
@@ -3151,10 +3155,29 @@ function measureCellTextWidth(text: string, font: string): number {
return width;
}
const cellTextWidthCache = new Map<string, number>();
function measureCellTextWidthCached(text: string, font: string): number {
const key = `${font}|${text}`;
let width = cellTextWidthCache.get(key);
if (width === undefined) {
width = measureCellTextWidth(text, font);
if (cellTextWidthCache.size >= 200) cellTextWidthCache.clear();
cellTextWidthCache.set(key, width);
}
return width;
}
const tableColumnsByResultIndex = computed(() =>
resolveDataGridColumnsByResultIndex({
resultColumns: props.result.columns,
sourceColumns: props.sourceColumns,
tableColumns: props.tableMeta?.columns ?? [],
}),
);
function tableColumnForGridColumn(columnIndex: number): ColumnInfo | undefined {
const columnName = props.sourceColumns?.[columnIndex] ?? props.result.columns[columnIndex];
if (!columnName) return undefined;
return props.tableMeta?.columns.find((column) => column.name.toLowerCase() === columnName.toLowerCase());
return tableColumnsByResultIndex.value[columnIndex];
}
function resultColumnInfoForGridColumn(columnIndex: number): Pick<ColumnInfo, "data_type"> | undefined {
@@ -3183,6 +3206,35 @@ function isEnumEditorInitialNull(rowId: number | undefined, columnIndex: number)
return getRowItem(rowId)?.data[columnIndex] === null;
}
const booleanGridColumns = computed(() =>
props.result.columns.map((_, columnIndex) => {
const columnInfo = tableColumnsByResultIndex.value[columnIndex] ?? resultColumnInfoForGridColumn(columnIndex);
return isBooleanColumnType(columnInfo?.data_type, props.databaseType);
}),
);
function isBooleanGridColumn(columnIndex: number): boolean {
return booleanGridColumns.value[columnIndex] === true;
}
function isBooleanGridCell(item: RowItem | undefined, columnIndex: number): boolean {
return !!item && isBooleanGridColumn(columnIndex) && isBooleanCheckboxValue(item.data[columnIndex]);
}
function isBooleanGridColumnNullable(columnIndex: number): boolean {
return tableColumnForGridColumn(columnIndex)?.is_nullable ?? true;
}
function booleanCellChecked(value: unknown): boolean {
return normalizeBooleanCellValue(value) === true;
}
function cycleBooleanGridCell(item: RowItem | undefined, actualColIdx: number, event: MouseEvent) {
if (!item || !isBooleanGridCell(item, actualColIdx) || !canEditCellItem(item, actualColIdx)) return;
event.stopPropagation();
void cycleBooleanCellValue(item.id, actualColIdx, isBooleanGridColumnNullable(actualColIdx));
}
function cellEditInputModeForColumn(columnIndex: number): "decimal" | "numeric" | undefined {
const dataType = normalizedColumnDataType(tableColumnForGridColumn(columnIndex));
if (isIntegerColumnType(dataType)) return "numeric";
@@ -4967,7 +5019,8 @@ function onCanvasMouseMove(event: MouseEvent) {
const next = hit && hitItem ? { rowIndex: hitItem.displayIndex, visibleColIdx: hit.rowNumber ? -1 : hit.visibleColIdx } : null;
const actualColIdx = next ? visibleColumnIndexes.value[next.visibleColIdx] : undefined;
if (canvasRef.value) {
canvasRef.value.style.cursor = hit?.rowNumber ? "default" : hitItem && actualColIdx !== undefined && canEditCellItem(hitItem, actualColIdx) ? "text" : "cell";
const overBooleanInteractive = hit != null && !hit.rowNumber && hitItem != null && actualColIdx !== undefined && isBooleanGridCell(hitItem, actualColIdx) && canEditCellItem(hitItem, actualColIdx) && booleanInteractiveHitFromCanvasEvent(hitItem, hit, actualColIdx, event);
canvasRef.value.style.cursor = hit?.rowNumber ? "default" : overBooleanInteractive ? "pointer" : hitItem && actualColIdx !== undefined && canEditCellItem(hitItem, actualColIdx) ? "text" : "cell";
}
if (next?.rowIndex === canvasHoverCell.value?.rowIndex && next?.visibleColIdx === canvasHoverCell.value?.visibleColIdx) {
return;
@@ -5010,6 +5063,43 @@ function clearCanvasDetailHover(event?: MouseEvent) {
onCanvasMouseLeave();
}
function booleanCheckboxHitFromCanvasEvent(item: RowItem, hit: { rowIndex: number; visibleColIdx: number }, actualColIdx: number, event: MouseEvent): boolean {
if (item.data[actualColIdx] === null) return false;
const canvas = canvasRef.value;
const canvasRect = canvas?.getBoundingClientRect();
const cellRect = canvasCellViewportRect(hit.rowIndex, hit.visibleColIdx);
if (!canvasRect || !cellRect) return false;
return isPointInBooleanCheckbox({ x: event.clientX - canvasRect.left, y: event.clientY - canvasRect.top }, { left: cellRect.left, top: cellRect.top, width: cellRect.width, height: cellRect.height });
}
function booleanNullTextHitFromCanvasEvent(item: RowItem, hit: { rowIndex: number; visibleColIdx: number }, actualColIdx: number, event: MouseEvent): boolean {
if (item.data[actualColIdx] !== null) return false;
const canvas = canvasRef.value;
const canvasRect = canvas?.getBoundingClientRect();
const cellRect = canvasCellViewportRect(hit.rowIndex, hit.visibleColIdx);
if (!canvasRect || !cellRect) return false;
const text = firstLineCellDisplayValue(formatCellCached(item.data[actualColIdx], actualColIdx));
if (!text) return false;
const textWidth = measureCellTextWidthCached(text, `italic 400 ${tableFontSize.value}px ${tableFontFamily.value}`);
if (textWidth <= 0) return false;
const left = cellRect.left + (cellRect.width - textWidth) / 2 - 2;
const right = left + textWidth + 4;
const x = event.clientX - canvasRect.left;
const y = event.clientY - canvasRect.top;
return x >= left && x <= right && y >= cellRect.top && y <= cellRect.top + cellRect.height;
}
function booleanInteractiveHitFromCanvasEvent(item: RowItem, hit: { rowIndex: number; visibleColIdx: number }, actualColIdx: number, event: MouseEvent): boolean {
return item.data[actualColIdx] === null ? booleanNullTextHitFromCanvasEvent(item, hit, actualColIdx, event) : booleanCheckboxHitFromCanvasEvent(item, hit, actualColIdx, event);
}
function tryCycleBooleanCheckboxOnCanvasMouseDown(item: RowItem, hit: { rowIndex: number; visibleColIdx: number }, actualColIdx: number, event: MouseEvent): boolean {
if (!isBooleanGridCell(item, actualColIdx) || !canEditCellItem(item, actualColIdx)) return false;
if (!booleanInteractiveHitFromCanvasEvent(item, hit, actualColIdx, event)) return false;
void cycleBooleanCellValue(item.id, actualColIdx, isBooleanGridColumnNullable(actualColIdx));
return true;
}
function onCanvasMouseDown(event: MouseEvent) {
if (event.button !== 0) return;
const hit = canvasHitTest(event);
@@ -5026,6 +5116,7 @@ function onCanvasMouseDown(event: MouseEvent) {
onRowNumberMouseDown(item, event);
} else {
handleDataCellMousedown(item.displayIndex, hit.visibleColIdx, item.id, event);
if (actualColIdx !== undefined) tryCycleBooleanCheckboxOnCanvasMouseDown(item, hit, actualColIdx, event);
}
gridRef.value?.focus({ preventScroll: true });
scheduleCanvasDraw();
@@ -5058,6 +5149,7 @@ function onCanvasDblClick(event: MouseEvent) {
const actualColIdx = visibleColumnIndexes.value[hit.visibleColIdx];
if (!item || actualColIdx === undefined) return;
if (showReadonlyCellDetailsOnDblClick(item, item.displayIndex, hit.visibleColIdx, actualColIdx)) return;
if (isBooleanGridCell(item, actualColIdx) && canEditCellItem(item, actualColIdx)) return;
startCellEdit(item.id, actualColIdx, canvasCellContentOverflows(item, actualColIdx, hit.visibleColIdx));
}
@@ -5222,6 +5314,7 @@ function drawCanvasGrid() {
searchMatchKeys: searchMatchSet.value,
currentSearchMatch: currentSearchMatch.value,
formatCell: formatCellCached,
columnIsBoolean: isBooleanGridColumn,
draftCellPlaceholder: t("grid.quickEntryDraftPlaceholder"),
isRowActive,
rowCellsUseSelectionVisual,
@@ -5712,6 +5805,7 @@ function onTransposeCellDblClick(rowIndex: number, actualColIdx: number, display
showTransposeCellDetails(rowIndex, actualColIdx);
return;
}
if (isBooleanGridCell(item, actualColIdx)) return;
startDomCellEdit(item.id, actualColIdx, displayText, event);
}
@@ -6212,6 +6306,10 @@ function editSelectedCell(): boolean {
const item = displayItemAt(position.rowIndex);
const actualColIndex = actualColumnIndex(position.colIndex);
if (!item || !canEditCellItem(item, actualColIndex)) return false;
if (isBooleanGridCell(item, actualColIndex)) {
void cycleBooleanCellValue(item.id, actualColIndex, isBooleanGridColumnNullable(actualColIndex));
return true;
}
startEdit(item.id, actualColIndex);
return true;
}
@@ -8639,6 +8737,22 @@ const gridContextMenuItems = computed<ContextMenuItem[]>(() => {
<template v-if="draftCellPlaceholder(displayItems[cell.recordIndex], cell.valueIndex)">
<span class="text-muted-foreground/70 italic">{{ draftCellPlaceholder(displayItems[cell.recordIndex], cell.valueIndex) }}</span>
</template>
<template v-else-if="isBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex) && !cell.isNull">
<span class="flex w-full justify-center">
<span
class="flex h-4 w-4 shrink-0 items-center justify-center rounded border"
:class="[booleanCellChecked(displayItems[cell.recordIndex]?.data[cell.valueIndex]) ? 'border-primary bg-primary text-primary-foreground' : '', canEditCellItem(displayItems[cell.recordIndex], cell.valueIndex) ? 'cursor-pointer' : '']"
@click="cycleBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex, $event)"
>
<Check v-if="booleanCellChecked(displayItems[cell.recordIndex]?.data[cell.valueIndex])" class="h-3 w-3" />
</span>
</span>
</template>
<template v-else-if="isBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex)">
<span class="flex w-full justify-center">
<span :class="canEditCellItem(displayItems[cell.recordIndex], cell.valueIndex) ? 'cursor-pointer' : ''" @click="cycleBooleanGridCell(displayItems[cell.recordIndex], cell.valueIndex, $event)">{{ firstLineCellDisplayValue(cell.display) }}</span>
</span>
</template>
<template v-else>{{ firstLineCellDisplayValue(cell.display) }}</template>
<div v-if="cellDetailButtonVisible(cell.recordIndex, cell.valueIndex)" class="absolute right-2 top-1/2 flex -translate-y-1/2 items-center gap-1">
<LightDropdownMenu
@@ -9307,6 +9421,22 @@ const gridContextMenuItems = computed<ContextMenuItem[]>(() => {
<template v-if="draftCellPlaceholder(item, col.actualColIdx)">
<span class="text-muted-foreground/70 italic">{{ draftCellPlaceholder(item, col.actualColIdx) }}</span>
</template>
<template v-else-if="isBooleanGridCell(item, col.actualColIdx) && !isNull(item.data[col.actualColIdx])">
<span class="flex w-full justify-center">
<span
class="flex h-4 w-4 shrink-0 items-center justify-center rounded border"
:class="[booleanCellChecked(item.data[col.actualColIdx]) ? 'border-primary bg-primary text-primary-foreground' : '', canEditCellItem(item, col.actualColIdx) ? 'cursor-pointer' : '']"
@click="cycleBooleanGridCell(item, col.actualColIdx, $event)"
>
<Check v-if="booleanCellChecked(item.data[col.actualColIdx])" class="h-3 w-3" />
</span>
</span>
</template>
<template v-else-if="isBooleanGridCell(item, col.actualColIdx)">
<span class="flex w-full justify-center">
<span :class="canEditCellItem(item, col.actualColIdx) ? 'cursor-pointer' : ''" @click="cycleBooleanGridCell(item, col.actualColIdx, $event)">{{ firstLineCellDisplayValue(formatCellCached(item.data[col.actualColIdx], col.actualColIdx)) }}</span>
</span>
</template>
<template v-else>{{ firstLineCellDisplayValue(formatCellCached(item.data[col.actualColIdx], col.actualColIdx)) }}</template>
<div v-if="cellDetailButtonVisible(item.displayIndex, col.actualColIdx)" class="absolute right-2 top-1/2 flex -translate-y-1/2 items-center gap-1">
<LightDropdownMenu