@beeblock/svelar-datatable 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beeblock/svelar-datatable",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Full-featured DataTable plugin for Svelar — sorting, searching, pagination, inline editing, export, and server-side processing",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -45,6 +45,10 @@
45
45
  "files": [
46
46
  "dist",
47
47
  "src/ui",
48
+ "src/state",
49
+ "src/export",
50
+ "src/types.ts",
51
+ "src/index.ts",
48
52
  "src/publishable",
49
53
  "LICENSE"
50
54
  ],
@@ -0,0 +1,30 @@
1
+ import type { ColumnDef, ExportFormat } from '../types.js';
2
+ import { generateCSV, downloadCSV } from './csv.js';
3
+ import { copyToClipboard } from './clipboard.js';
4
+ import { printTable } from './print.js';
5
+ import { exportExcel } from './excel.js';
6
+ import { exportPdf } from './pdf.js';
7
+
8
+ export class ExportManager<T = any> {
9
+ async export(format: ExportFormat, data: T[], columns: ColumnDef<T>[], filename?: string): Promise<void> {
10
+ switch (format) {
11
+ case 'csv': {
12
+ const csv = generateCSV(data, columns);
13
+ downloadCSV(csv, filename ?? 'export.csv');
14
+ break;
15
+ }
16
+ case 'excel':
17
+ await exportExcel(data, columns, filename ?? 'export.xlsx');
18
+ break;
19
+ case 'pdf':
20
+ exportPdf(data, columns, filename ?? 'export.pdf');
21
+ break;
22
+ case 'clipboard':
23
+ await copyToClipboard(data, columns);
24
+ break;
25
+ case 'print':
26
+ printTable(data, columns);
27
+ break;
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,14 @@
1
+ import type { ColumnDef } from '../types.js';
2
+
3
+ export async function copyToClipboard<T>(data: T[], columns: ColumnDef<T>[]): Promise<void> {
4
+ const visibleCols = columns.filter((c) => c.visible !== false);
5
+ const header = visibleCols.map((c) => c.header).join('\t');
6
+ const rows = data.map((row: any) =>
7
+ visibleCols.map((col) => {
8
+ const val = row[col.key];
9
+ return val === null || val === undefined ? '' : String(val);
10
+ }).join('\t')
11
+ );
12
+ const text = [header, ...rows].join('\n');
13
+ await navigator.clipboard.writeText(text);
14
+ }
@@ -0,0 +1,36 @@
1
+ import type { ColumnDef } from '../types.js';
2
+
3
+ function escapeCSV(value: any): string {
4
+ if (value === null || value === undefined) return '';
5
+ const str = String(value);
6
+ if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
7
+ return '"' + str.replace(/"/g, '""') + '"';
8
+ }
9
+ return str;
10
+ }
11
+
12
+ export function generateCSV<T>(data: T[], columns: ColumnDef<T>[]): string {
13
+ const visibleCols = columns.filter((c) => c.visible !== false);
14
+ const header = visibleCols.map((c) => escapeCSV(c.header)).join(',');
15
+ const rows = data.map((row: any) =>
16
+ visibleCols.map((col) => escapeCSV(row[col.key])).join(',')
17
+ );
18
+ // BOM for Excel UTF-8 compatibility
19
+ return '\uFEFF' + [header, ...rows].join('\r\n');
20
+ }
21
+
22
+ export function downloadCSV(content: string, filename: string = 'export.csv') {
23
+ const blob = new Blob([content], { type: 'text/csv;charset=utf-8;' });
24
+ downloadBlob(blob, filename);
25
+ }
26
+
27
+ export function downloadBlob(blob: Blob, filename: string) {
28
+ const url = URL.createObjectURL(blob);
29
+ const a = document.createElement('a');
30
+ a.href = url;
31
+ a.download = filename;
32
+ document.body.appendChild(a);
33
+ a.click();
34
+ document.body.removeChild(a);
35
+ URL.revokeObjectURL(url);
36
+ }
@@ -0,0 +1,44 @@
1
+ import type { ColumnDef } from '../types.js';
2
+ import { downloadBlob } from './csv.js';
3
+
4
+ export async function exportExcel<T>(data: T[], columns: ColumnDef<T>[], filename: string = 'export.xlsx') {
5
+ let ExcelJS: any;
6
+ try {
7
+ ExcelJS = await import('exceljs');
8
+ } catch {
9
+ throw new Error('exceljs is required for Excel export. Install it: npm install exceljs');
10
+ }
11
+
12
+ const workbook = new ExcelJS.Workbook();
13
+ const worksheet = workbook.addWorksheet('Data');
14
+ const visibleCols = columns.filter((c) => c.visible !== false);
15
+
16
+ // Header row
17
+ worksheet.columns = visibleCols.map((col) => ({
18
+ header: col.header,
19
+ key: col.key,
20
+ width: 20,
21
+ }));
22
+
23
+ // Style header
24
+ const headerRow = worksheet.getRow(1);
25
+ headerRow.font = { bold: true };
26
+ headerRow.fill = {
27
+ type: 'pattern',
28
+ pattern: 'solid',
29
+ fgColor: { argb: 'FFF0F0F0' },
30
+ };
31
+
32
+ // Data rows
33
+ for (const row of data) {
34
+ const rowData: Record<string, any> = {};
35
+ for (const col of visibleCols) {
36
+ rowData[col.key] = (row as any)[col.key] ?? '';
37
+ }
38
+ worksheet.addRow(rowData);
39
+ }
40
+
41
+ const buffer = await workbook.xlsx.writeBuffer();
42
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
43
+ downloadBlob(blob, filename);
44
+ }
@@ -0,0 +1,6 @@
1
+ export { generateCSV, downloadCSV, downloadBlob } from './csv.js';
2
+ export { copyToClipboard } from './clipboard.js';
3
+ export { printTable } from './print.js';
4
+ export { exportExcel } from './excel.js';
5
+ export { exportPdf } from './pdf.js';
6
+ export { ExportManager } from './ExportManager.js';
@@ -0,0 +1,9 @@
1
+ import type { ColumnDef } from '../types.js';
2
+ import { generateCSV } from './csv.js';
3
+ import { printTable } from './print.js';
4
+
5
+ export function exportPdf<T>(data: T[], columns: ColumnDef<T>[], filename: string = 'export.pdf') {
6
+ // Use browser print as fallback (generates PDF via print dialog)
7
+ // For server-side PDF, users should use @beeblock/svelar/pdf directly
8
+ printTable(data, columns, filename.replace('.pdf', ''));
9
+ }
@@ -0,0 +1,42 @@
1
+ import type { ColumnDef } from '../types.js';
2
+
3
+ export function printTable<T>(data: T[], columns: ColumnDef<T>[], title?: string) {
4
+ const visibleCols = columns.filter((c) => c.visible !== false);
5
+
6
+ const headerCells = visibleCols.map((c) => `<th style="border:1px solid #ddd;padding:8px 12px;text-align:left;background:#f5f5f5;font-weight:600;">${escapeHtml(c.header)}</th>`).join('');
7
+ const bodyRows = data.map((row: any) => {
8
+ const cells = visibleCols.map((col) => {
9
+ const val = row[col.key];
10
+ return `<td style="border:1px solid #ddd;padding:8px 12px;">${escapeHtml(val === null || val === undefined ? '' : String(val))}</td>`;
11
+ }).join('');
12
+ return `<tr>${cells}</tr>`;
13
+ }).join('');
14
+
15
+ const html = `<!DOCTYPE html>
16
+ <html><head>
17
+ <title>${escapeHtml(title ?? 'Data Export')}</title>
18
+ <style>
19
+ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 20px; }
20
+ table { border-collapse: collapse; width: 100%; font-size: 13px; }
21
+ h1 { font-size: 18px; margin-bottom: 16px; }
22
+ @media print { h1 { margin-top: 0; } }
23
+ </style>
24
+ </head><body>
25
+ ${title ? `<h1>${escapeHtml(title)}</h1>` : ''}
26
+ <table>
27
+ <thead><tr>${headerCells}</tr></thead>
28
+ <tbody>${bodyRows}</tbody>
29
+ </table>
30
+ </body></html>`;
31
+
32
+ const win = window.open('', '_blank');
33
+ if (!win) return;
34
+ win.document.write(html);
35
+ win.document.close();
36
+ win.focus();
37
+ setTimeout(() => win.print(), 250);
38
+ }
39
+
40
+ function escapeHtml(str: string): string {
41
+ return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
42
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export { DataTableStore } from './state/DataTableStore.js';
2
+ export { ServerDataTableStore } from './state/ServerDataTableStore.js';
3
+ export { ExportManager } from './export/ExportManager.js';
4
+ export type {
5
+ ColumnDef,
6
+ ColumnType,
7
+ FilterOperator,
8
+ SelectionMode,
9
+ EditorMode,
10
+ FieldType,
11
+ ExportFormat,
12
+ SortState,
13
+ FilterState,
14
+ PaginationState,
15
+ DataTableRequest,
16
+ DataTableResponse,
17
+ EditorFieldDef,
18
+ ButtonDef,
19
+ DataTableConfig,
20
+ DataTableClassNames,
21
+ DataTableState,
22
+ } from './types.js';