Файли
openidea/src/features/database/csvModes.ts
T
LU XIANGRUIтаwildcatDownstairs c2c6931f0e feat(database): open .csv / .tsv files as a table (#6493)
.csv and .tsv files now open with Text / Data tabs under the editor. Data shows the file as a read-only table in the result grid, with header sort, the local column filter and search. The delimiter (comma, semicolon, tab or pipe) is guessed from the first 64 KB (.tsv is always tab), and the header row is guessed with a First row is header override. Quoted fields, line breaks inside quotes and a UTF-8 BOM are handled; ragged rows are padded, unnamed columns numbered and repeated names suffixed. Parsing runs in a Web Worker and the table re-reads the editor text (debounced) only while Data is shown; Text stays mounted so undo history and unsaved edits are kept.

visibleRows also computes each row's sort key once instead of per comparison, which speeds up local sorting of query results too.

Closes #6368

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-27 20:38:44 +08:00

43 рядки
1.6 KiB
TypeScript

/** Text / Data tab of each open CSV editor, and a counter of each CSV file's text changes. */
import { ViewPlugin, type EditorView, type ViewUpdate } from "@codemirror/view";
import { create } from "zustand";
import { registerEditorContribution } from "@/editor/api";
import { isCsvPath } from "./csv";
export type CsvMode = "text" | "data";
export const useCsvModes = create<Record<string, CsvMode | undefined>>(() => ({}));
export const useCsvSourceHidden = (tabId: string) => useCsvModes((s) => s[tabId] === "data");
/** Bumped when a CSV file's text changes. */
export const useCsvVersions = create<Record<string, number | undefined>>(() => ({}));
const bump = (path: string) => useCsvVersions.setState((s) => ({ [path]: (s[path] ?? 0) + 1 }));
const TEXT_DELAY = 300;
export function registerCsvLiveSource(): () => void {
return registerEditorContribution({
id: "database.csvLive",
extensions: (ctx) =>
!ctx.virtual && isCsvPath(ctx.path)
? [
ViewPlugin.fromClass(
class {
private timer: ReturnType<typeof setTimeout> | undefined;
constructor(_view: EditorView) {
bump(ctx.path);
}
update(u: ViewUpdate) {
if (!u.docChanged) return;
clearTimeout(this.timer);
this.timer = setTimeout(() => bump(ctx.path), TEXT_DELAY);
}
destroy() {
clearTimeout(this.timer);
}
},
),
]
: [],
});
}