adf0d17497
publish / version_or_publish (push) Has been cancelled
storybook-build / changes (push) Has been cancelled
storybook-build / :storybook-build (push) Has been cancelled
Sync Gradio Skills to Hugging Face / sync-skills (push) Has been cancelled
functional / changes (push) Has been cancelled
functional / build-frontend (push) Has been cancelled
functional / functional-test-SSR=false (push) Has been cancelled
functional / functional-reload (push) Has been cancelled
js / changes (push) Has been cancelled
js / js-test (push) Has been cancelled
docs-build / changes (push) Has been cancelled
docs-build / docs-build (push) Has been cancelled
docs-build / website-build (push) Has been cancelled
functional / functional-test-SSR=true (push) Has been cancelled
hygiene / hygiene-test (push) Has been cancelled
python / changes (push) Has been cancelled
python / build (push) Has been cancelled
python / test-ubuntu-latest-flaky (push) Has been cancelled
python / test-ubuntu-latest-not-flaky (push) Has been cancelled
python / test-windows-latest-flaky (push) Has been cancelled
python / test-windows-latest-not-flaky (push) Has been cancelled
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import type { CellValue, Headers, HeadersWithIDs, TableData } from "../types";
|
|
import { dsvFormat } from "d3-dsv";
|
|
|
|
export function make_cell_id(row: number, col: number): string {
|
|
return `cell-${row}-${col}`;
|
|
}
|
|
|
|
export function make_header_id(col: number): string {
|
|
return `header-${col}`;
|
|
}
|
|
|
|
export async function copy_table_data(
|
|
data: TableData,
|
|
selected_cells: [number, number][] | null
|
|
): Promise<void> {
|
|
if (!data || !data.length) return;
|
|
|
|
const cells_to_copy =
|
|
selected_cells ||
|
|
data.flatMap((row, r) => row.map((_, c) => [r, c] as [number, number]));
|
|
|
|
const csv = cells_to_copy.reduce(
|
|
(acc: { [key: string]: { [key: string]: string } }, [row, col]) => {
|
|
acc[row] = acc[row] || {};
|
|
const value = String(data[row][col].value);
|
|
acc[row][col] =
|
|
value.includes(",") || value.includes('"') || value.includes("\n")
|
|
? `"${value.replace(/"/g, '""')}"`
|
|
: value;
|
|
return acc;
|
|
},
|
|
{}
|
|
);
|
|
|
|
const rows = Object.keys(csv).sort((a, b) => +a - +b);
|
|
if (!rows.length) return;
|
|
|
|
const cols = Object.keys(csv[rows[0]]).sort((a, b) => +a - +b);
|
|
const text = rows
|
|
.map((r) => cols.map((c) => csv[r][c] || "").join(","))
|
|
.join("\n");
|
|
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
} catch (err) {
|
|
throw new Error("Failed to copy to clipboard: " + (err as Error).message);
|
|
}
|
|
}
|
|
|
|
export function guess_delimiter(
|
|
text: string,
|
|
possibleDelimiters: string[]
|
|
): string[] {
|
|
return possibleDelimiters.filter(weedOut);
|
|
|
|
function weedOut(delimiter: string): boolean {
|
|
var cache = -1;
|
|
return text.split("\n").every(checkLength);
|
|
|
|
function checkLength(line: string): boolean {
|
|
if (!line) return true;
|
|
var length = line.split(delimiter).length;
|
|
if (cache < 0) cache = length;
|
|
return cache === length && length > 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function data_uri_to_blob(data_uri: string): Blob {
|
|
const byte_str = atob(data_uri.split(",")[1]);
|
|
const mime_str = data_uri.split(",")[0].split(":")[1].split(";")[0];
|
|
const ab = new ArrayBuffer(byte_str.length);
|
|
const ia = new Uint8Array(ab);
|
|
for (let i = 0; i < byte_str.length; i++) {
|
|
ia[i] = byte_str.charCodeAt(i);
|
|
}
|
|
return new Blob([ab], { type: mime_str });
|
|
}
|
|
|
|
export function handle_file_upload(
|
|
data_uri: string,
|
|
update_headers: (headers: Headers) => HeadersWithIDs[],
|
|
update_values: (values: CellValue[][]) => void
|
|
): void {
|
|
const blob = data_uri_to_blob(data_uri);
|
|
const reader = new FileReader();
|
|
reader.addEventListener("loadend", (e) => {
|
|
if (!e?.target?.result || typeof e.target.result !== "string") return;
|
|
const [delimiter] = guess_delimiter(e.target.result, [",", "\t"]);
|
|
const [head, ...rest] = dsvFormat(delimiter).parseRows(e.target.result);
|
|
update_headers(head);
|
|
update_values(rest);
|
|
});
|
|
reader.readAsText(blob);
|
|
}
|