дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
Revert Selected drops the pending edits, deletes and added rows of the selected rows only, from the row menu of the table editor and editable results; it takes Cmd+Alt+Z / Ctrl+Alt+Z while the selection has changes, and Revert keeps the shortcut otherwise. Copy Aggregation Result copies one aggregate of the selected cells as the Aggregates panel shows it: Cmd+Shift+C / Ctrl+Shift+C copies the Sum of numbers or the Count otherwise, and the grid context menu lists every aggregate the selection has. Fixes #6308.
148 рядки
4.9 KiB
TypeScript
148 рядки
4.9 KiB
TypeScript
import { registerActions, type ActionContext } from "@/platform/actions";
|
|
import { getContext, setContext } from "@/platform/context";
|
|
import { writeClipboardText } from "@/platform/clipboard";
|
|
import { aggregateText, defaultAggregate, type Aggregates } from "./resultView";
|
|
|
|
export type CellCopy = "tsv" | "csv" | "onerow" | "header" | "rowsTsv" | "rowsCsv";
|
|
|
|
export interface CellActions {
|
|
canToggle(): boolean;
|
|
canSetNull(): boolean;
|
|
canSetDefault(): boolean;
|
|
canFilter(): boolean;
|
|
toggle(): void;
|
|
setNull(): void;
|
|
setDefault(): void;
|
|
/** Opens the local filter of the selected cell's column. */
|
|
filter(): void;
|
|
canEditCell(): boolean;
|
|
editCell(): void;
|
|
hasSelection(): boolean;
|
|
copy(kind: CellCopy): void;
|
|
/** Rows of the selection, top to bottom. */
|
|
selectedRows(): number[];
|
|
/** The Aggregates panel's figures for the selected cells; null without a selection. */
|
|
aggregates(): Aggregates | null;
|
|
}
|
|
|
|
/** Copy Aggregation Result: one aggregate of the selection, as the Aggregates panel shows it. */
|
|
export function copyAggregate(grid: CellActions, key?: keyof Aggregates): void {
|
|
const a = grid.aggregates();
|
|
const text = a && aggregateText(a, key ?? defaultAggregate(a));
|
|
if (text !== null && text !== undefined) void writeClipboardText(text);
|
|
}
|
|
|
|
const grids = new WeakMap<Element, CellActions>();
|
|
let active: CellActions | null = null;
|
|
|
|
/** The focused grid (not while a text field in it has focus). */
|
|
export function activeCellGrid(): CellActions | null {
|
|
return getContext().dbCellGridFocus ? active : null;
|
|
}
|
|
|
|
const COPIES: Array<[CellCopy, string, string]> = [
|
|
["tsv", "db.copyAsTsv", "Copy as TSV"],
|
|
["csv", "db.copyAsCsv", "Copy as CSV"],
|
|
["onerow", "db.copyAsOneRow", "Copy as One-Row"],
|
|
["header", "db.copyWithHeader", "Copy with Header"],
|
|
["rowsTsv", "db.copyRowsAsTsv", "Copy Rows as TSV"],
|
|
["rowsCsv", "db.copyRowsAsCsv", "Copy Rows as CSV"],
|
|
];
|
|
|
|
export function registerCellGrid(root: Element, actions: CellActions): () => void {
|
|
grids.set(root, actions);
|
|
return () => {
|
|
grids.delete(root);
|
|
if (active === actions) {
|
|
active = null;
|
|
setContext("dbCellGridFocus", false);
|
|
}
|
|
};
|
|
}
|
|
|
|
export function installCellActions(): () => void {
|
|
const onFocus = (event: FocusEvent) => {
|
|
const target = event.target instanceof Element ? event.target : null;
|
|
const root = target?.closest("[data-db-cell-grid]");
|
|
// A popup can query the grid that opened it with its captured context.
|
|
if (!root && target?.closest("[data-popup], [role=dialog]")) {
|
|
setContext("dbCellGridFocus", false);
|
|
return;
|
|
}
|
|
active =
|
|
root && !target?.closest("input, textarea, select, [contenteditable='true']") ? (grids.get(root) ?? null) : null;
|
|
setContext("dbCellGridFocus", !!active);
|
|
};
|
|
document.addEventListener("focusin", onFocus);
|
|
const handle = ({ arg, ctx }: ActionContext) =>
|
|
(arg as CellActions | undefined) ?? (ctx.dbCellGridFocus ? active : null);
|
|
const dispose = registerActions([
|
|
{
|
|
id: "db.toggleBooleanCell",
|
|
title: "Toggle Boolean Cell",
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.canToggle() ?? false,
|
|
run: (c) => handle(c)?.toggle(),
|
|
},
|
|
{
|
|
id: "db.setCellNull",
|
|
title: "Set NULL",
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.canSetNull() ?? false,
|
|
run: (c) => handle(c)?.setNull(),
|
|
},
|
|
{
|
|
id: "db.setCellDefault",
|
|
title: "Set DEFAULT",
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.canSetDefault() ?? false,
|
|
run: (c) => handle(c)?.setDefault(),
|
|
},
|
|
{
|
|
id: "db.localFilter",
|
|
title: "Local Filter",
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.canFilter() ?? false,
|
|
run: (c) => handle(c)?.filter(),
|
|
},
|
|
{
|
|
id: "db.editCell",
|
|
title: "Edit Cell",
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.canEditCell() ?? false,
|
|
run: (c) => handle(c)?.editCell(),
|
|
},
|
|
{
|
|
id: "db.copyAggregationResult",
|
|
title: "Copy Aggregation Result",
|
|
category: "Database",
|
|
keywords: ["sum", "count", "aggregate", "total"],
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c) => handle(c)?.hasSelection() ?? false,
|
|
run: (c) => {
|
|
const grid = handle(c);
|
|
if (grid?.hasSelection()) copyAggregate(grid);
|
|
},
|
|
},
|
|
...COPIES.map(([kind, id, title]) => ({
|
|
id,
|
|
title,
|
|
category: "Database",
|
|
keyWhen: "dbCellGridFocus",
|
|
enabled: (c: ActionContext) => handle(c)?.hasSelection() ?? false,
|
|
run: (c: ActionContext) => handle(c)?.copy(kind),
|
|
})),
|
|
]);
|
|
return () => {
|
|
dispose();
|
|
document.removeEventListener("focusin", onFocus);
|
|
active = null;
|
|
setContext("dbCellGridFocus", false);
|
|
};
|
|
}
|