@toolbox-web/grid 2.0.0-alpha → 2.0.0-alpha.1
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/all.js +2 -2
- package/all.js.map +1 -1
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/lib/plugins/export/excel-styles.d.ts +45 -0
- package/lib/plugins/export/index.d.ts +1 -1
- package/lib/plugins/export/index.js +1 -1
- package/lib/plugins/export/index.js.map +1 -1
- package/lib/plugins/export/types.d.ts +68 -0
- package/lib/plugins/grouping-rows/GroupingRowsPlugin.d.ts +91 -2
- package/lib/plugins/grouping-rows/grouping-rows.d.ts +29 -1
- package/lib/plugins/grouping-rows/index.d.ts +1 -1
- package/lib/plugins/grouping-rows/index.js +2 -2
- package/lib/plugins/grouping-rows/index.js.map +1 -1
- package/lib/plugins/grouping-rows/types.d.ts +104 -0
- package/lib/plugins/server-side/index.js +1 -1
- package/lib/plugins/server-side/index.js.map +1 -1
- package/package.json +1 -1
- package/umd/grid.all.umd.js +1 -1
- package/umd/grid.all.umd.js.map +1 -1
- package/umd/plugins/export.umd.js +1 -1
- package/umd/plugins/export.umd.js.map +1 -1
- package/umd/plugins/grouping-rows.umd.js +1 -1
- package/umd/plugins/grouping-rows.umd.js.map +1 -1
- package/umd/plugins/server-side.umd.js +1 -1
- package/umd/plugins/server-side.umd.js.map +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ExcelCellStyle, ExcelStyleConfig } from './types';
|
|
2
|
+
/** Maps a hash → style ID and the original style. */
|
|
3
|
+
export interface StyleEntry {
|
|
4
|
+
id: string;
|
|
5
|
+
style: ExcelCellStyle;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Collects unique styles and assigns each an `ss:StyleID`.
|
|
9
|
+
* Register styles via `register()`, then call `getStyleId()` to look up
|
|
10
|
+
* the ID for a given style, and `toXml()` to emit the `<Styles>` block.
|
|
11
|
+
*/
|
|
12
|
+
export declare class StyleRegistry {
|
|
13
|
+
#private;
|
|
14
|
+
/**
|
|
15
|
+
* Register a style and return its assigned ID.
|
|
16
|
+
* If an identical style was already registered, returns the existing ID.
|
|
17
|
+
*/
|
|
18
|
+
register(style: ExcelCellStyle): string;
|
|
19
|
+
/** Look up the ID previously assigned to a style (or undefined). */
|
|
20
|
+
getStyleId(style: ExcelCellStyle): string | undefined;
|
|
21
|
+
/** Number of unique styles registered so far. */
|
|
22
|
+
get size(): number;
|
|
23
|
+
/** Emit the full `<Styles>…</Styles>` XML fragment, or empty string if no styles. */
|
|
24
|
+
toXml(): string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Build a StyleRegistry pre-populated with all styles declared in the config.
|
|
28
|
+
* This covers headerStyle, defaultStyle, and columnStyles.
|
|
29
|
+
* (cellStyle callbacks are resolved per-cell at render time.)
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildStyleRegistry(config: ExcelStyleConfig): StyleRegistry;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the style ID for a data cell. Precedence (highest → lowest):
|
|
34
|
+
* 1. cellStyle callback return value
|
|
35
|
+
* 2. columnStyles[field]
|
|
36
|
+
* 3. defaultStyle
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveDataStyleId(registry: StyleRegistry, config: ExcelStyleConfig, value: unknown, field: string, row: unknown): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Build `<Column>` elements for explicit or auto-fit widths.
|
|
41
|
+
*/
|
|
42
|
+
export declare function buildColumnWidthsXml(columns: {
|
|
43
|
+
field: string;
|
|
44
|
+
header?: string;
|
|
45
|
+
}[], rows: Record<string, unknown>[], config: ExcelStyleConfig): string;
|
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
* @module Plugins/Export
|
|
6
6
|
*/
|
|
7
7
|
export { ExportPlugin } from './ExportPlugin';
|
|
8
|
-
export type { ExportCompleteDetail, ExportConfig, ExportFormat, ExportParams } from './types';
|
|
8
|
+
export type { ExcelBorder, ExcelCellStyle, ExcelStyleConfig, ExportCompleteDetail, ExportConfig, ExportFormat, ExportParams, } from './types';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function e(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function t(t,r,n,o){return`${e(n,o)} ${t}: ${r}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(t)}`}["__otorp__","__retteGenifed__","__retteSenifed__","rotcurtsnoc","wodniw","sihTlabolg","labolg","ssecorp","noitcnuF","tropmi","lave","tcelfeR","yxorP","rorrE","stnemugra","tnemucod","noitacol","eikooc","egarotSlacol","egarotSnoisses","BDdexedni","hctef","tseuqeRpttHLMX","tekcoSbeW","rekroW","rekroWderahS","rekroWecivreS","renepo","tnerap","pot","semarf","fles"].map(e=>e.split("").reverse().join(""));const r=/* @__PURE__ */new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),n=/^on\w+$/i,o=/* @__PURE__ */new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),s=/^\s*(javascript|vbscript|data|blob):/i;function i(e){if(!e||"string"!=typeof e)return"";if(-1===e.indexOf("<"))return e;const t=document.createElement("template");return t.innerHTML=e,function(e){const t=[],i=e.querySelectorAll("*");for(const l of i){const e=l.tagName.toLowerCase();if(r.has(e)){t.push(l);continue}if("svg"===e||"http://www.w3.org/2000/svg"===l.namespaceURI){if(Array.from(l.attributes).some(e=>n.test(e.name)||"href"===e.name||"xlink:href"===e.name)){t.push(l);continue}}const i=[];for(const t of l.attributes){const e=t.name.toLowerCase();n.test(e)?i.push(t.name):(o.has(e)&&s.test(t.value)||"style"===e&&/expression\s*\(|javascript:|behavior\s*:/i.test(t.value))&&i.push(t.name)}i.forEach(e=>l.removeAttribute(e))}t.forEach(e=>e.remove())}(t.content),t.innerHTML}const l='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',a={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:l,filterActive:l,print:"🖨️"};class c{static dependencies;static manifest;aliases;version="undefined"!=typeof __GRID_VERSION__?__GRID_VERSION__:"dev";styles;cellRenderers;headerRenderers;cellEditors;grid;config;userConfig;#e;get defaultConfig(){return{}}constructor(e={}){this.userConfig=e}attach(e){this.#e?.abort(),this.#e=new AbortController,this.grid=e,this.config={...this.defaultConfig,...this.userConfig}}detach(){this.#e?.abort(),this.#e=void 0}getPlugin(e){return this.grid?.getPlugin(e)}emit(e,t){this.grid?.dispatchEvent?.(new CustomEvent(e,{detail:t,bubbles:!0}))}emitCancelable(e,t){const r=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(r),r.defaultPrevented}on(e,t){this.grid?._pluginManager?.subscribe(this,e,t)}off(e){this.grid?._pluginManager?.unsubscribe(this,e)}emitPluginEvent(e,t){this.grid?._pluginManager?.emitPluginEvent(e,t)}requestRender(){this.grid?.requestRender?.()}requestColumnsRender(){this.grid?.requestColumnsRender?.()}requestRenderWithFocus(){this.grid?.requestRenderWithFocus?.()}requestAfterRender(){this.grid?.requestAfterRender?.()}requestVirtualRefresh(){this.grid?.requestVirtualRefresh?.()}get rows(){return this.grid?.rows??[]}get sourceRows(){return this.grid?.sourceRows??[]}get columns(){return this.grid?.columns??[]}get visibleColumns(){return this.grid?._visibleColumns??[]}get gridElement(){return this.grid?._hostElement}get disconnectSignal(){return this.#e?.signal??this.grid?.disconnectSignal}get gridIcons(){const e=this.grid?.gridConfig?.icons??{};return{...a,...e}}get isAnimationEnabled(){const e=this.grid?.effectiveConfig?.animation?.mode??"reduced-motion";if(!1===e||"off"===e)return!1;if(!0===e||"on"===e)return!0;const t=this.gridElement;if(t){return"0"!==getComputedStyle(t).getPropertyValue("--tbw-animation-enabled").trim()}return!0}get animationDuration(){const e=this.gridElement;if(e){const t=getComputedStyle(e).getPropertyValue("--tbw-animation-duration").trim(),r=parseInt(t,10);if(!isNaN(r))return r}return 200}setIcon(e,t,r){e.dataset.icon=t.replace(/([A-Z])/g,"-$1").toLowerCase(),"collapse"===t?e.dataset.expanded="":"expand"===t&&delete e.dataset.expanded;const n=this.#t(t,r);void 0!==n?"string"==typeof n?e.innerHTML=i(n):n instanceof HTMLElement&&(e.innerHTML="",e.appendChild(n.cloneNode(!0))):e.innerHTML=""}#t(e,t){return void 0!==t?t:this.grid?.gridConfig?.icons?.[e]}updateSortIndicator(e,t){e.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove();const r=document.createElement("span");r.setAttribute("part","sort-indicator"),r.className="sort-indicator",t?(e.setAttribute("aria-sort","asc"===t?"ascending":"descending"),e.setAttribute("data-sort",t),this.setIcon(r,"asc"===t?"sortAsc":"sortDesc")):(e.setAttribute("aria-sort","none"),e.removeAttribute("data-sort"),this.setIcon(r,"sortNone"));const n=e.querySelector(".tbw-filter-btn")??e.querySelector(".resize-handle");return n?e.insertBefore(r,n):e.appendChild(r),r}warn(r,n){void 0!==n?console.warn(t(r,n,this.gridElement.id,this.name)):console.warn(`${e(this.gridElement.id,this.name)} ${r}`)}throwDiagnostic(e,r){throw new Error(t(e,r,this.gridElement.id,this.name))}}function d(e,t){return t?.length?[...t].sort((e,t)=>e-t).map(t=>e[t]).filter(e=>null!=e):e}function u(e,t=!0){if(null==e)return"";if(e instanceof Date)return e.toISOString();if("object"==typeof e)return JSON.stringify(e);const r=String(e);return t&&(r.includes(",")||r.includes('"')||r.includes("\n")||r.includes("\r"))?`"${r.replace(/"/g,'""')}"`:r}function f(e,t){const r=URL.createObjectURL(e),n=document.createElement("a");n.href=r,n.download=t,n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n),URL.revokeObjectURL(r)}function p(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}class h extends c{name="export";get defaultConfig(){return{fileName:"export",includeHeaders:!0,onlyVisible:!0,onlySelected:!1}}isExportingFlag=!1;lastExportInfo=null;performExport(e,t){const r=this.config,n={format:e,fileName:t?.fileName??r.fileName??"export",includeHeaders:t?.includeHeaders??r.includeHeaders,processCell:t?.processCell,processHeader:t?.processHeader,columns:t?.columns,rowIndices:t?.rowIndices},o=function(e,t,r=!0){let n=e;if(r&&(n=n.filter(e=>!e.hidden&&!e.field.startsWith("__")&&!0!==e.meta?.utility)),t?.length){const e=new Set(t);n=n.filter(t=>e.has(t.field))}return n}(this.columns,t?.columns,r.onlyVisible);let s;if(t?.rowIndices)s=d(this.rows,t.rowIndices);else if(r.onlySelected){const e=this.getSelectionState();s=e?.selected?.size?d(this.rows,[...e.selected]):[...this.rows]}else s=[...this.rows];this.isExportingFlag=!0;let i=n.fileName;try{switch(e){case"csv":{const e=function(e,t,r,n={}){const o=n.delimiter??",",s=n.newline??"\n",i=[],l=n.bom?"\ufeff":"";if(!1!==r.includeHeaders){const e=t.map(e=>{const t=e.header||e.field;return u(r.processHeader?r.processHeader(t,e.field):t)});i.push(e.join(o))}for(const a of e){const e=t.map(e=>{let t=a[e.field];return r.processCell&&(t=r.processCell(t,e.field,a)),u(t)});i.push(e.join(o))}return l+i.join(s)}(s,o,n,{bom:!0});i=i.endsWith(".csv")?i:`${i}.csv`,function(e,t){f(new Blob([e],{type:"text/csv;charset=utf-8;"}),t)}(e,i);break}case"excel":{const e=function(e,t,r){let n='<?xml version="1.0" encoding="UTF-8"?>\n<?mso-application progid="Excel.Sheet"?>\n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\n xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">\n<Worksheet ss:Name="Sheet1">\n<Table>';if(!1!==r.includeHeaders){n+="\n<Row>";for(const e of t){const t=e.header||e.field;n+=`<Cell><Data ss:Type="String">${p(r.processHeader?r.processHeader(t,e.field):t)}</Data></Cell>`}n+="</Row>"}for(const o of e){n+="\n<Row>";for(const e of t){let t=o[e.field];r.processCell&&(t=r.processCell(t,e.field,o));let s="String",i="";null==t?i="":"number"!=typeof t||isNaN(t)?t instanceof Date?(s="DateTime",i=t.toISOString()):i=p(String(t)):(s="Number",i=String(t)),n+=`<Cell><Data ss:Type="${s}">${i}</Data></Cell>`}n+="</Row>"}return n+="\n</Table>\n</Worksheet>\n</Workbook>",n}(s,o,n);i=i.endsWith(".xls")?i:`${i}.xls`,function(e,t){const r=t.endsWith(".xls")?t:`${t}.xls`;f(new Blob([e],{type:"application/vnd.ms-excel;charset=utf-8;"}),r)}(e,i);break}case"json":{const e=s.map(e=>{const t={};for(const r of o){let o=e[r.field];n.processCell&&(o=n.processCell(o,r.field,e)),t[r.field]=o}return t}),t=JSON.stringify(e,null,2);i=i.endsWith(".json")?i:`${i}.json`;f(new Blob([t],{type:"application/json"}),i);break}}this.lastExportInfo={format:e,timestamp:/* @__PURE__ */new Date},this.emit("export-complete",{format:e,fileName:i,rowCount:s.length,columnCount:o.length})}finally{this.isExportingFlag=!1}}getSelectionState(){try{return this.grid?.getPluginState?.("selection")??null}catch{return null}}exportCsv(e){this.performExport("csv",e)}exportExcel(e){this.performExport("excel",e)}exportJson(e){this.performExport("json",e)}isExporting(){return this.isExportingFlag}getLastExport(){return this.lastExportInfo}}export{h as ExportPlugin};
|
|
1
|
+
function e(e,t){return`[tbw-grid${e?`#${e}`:""}${t?`:${t}`:""}]`}function t(t,r,n,o){return`${e(n,o)} ${t}: ${r}\n\n → More info: ${function(e){return`https://toolboxjs.com/grid/errors#${e.toLowerCase()}`}(t)}`}["__otorp__","__retteGenifed__","__retteSenifed__","rotcurtsnoc","wodniw","sihTlabolg","labolg","ssecorp","noitcnuF","tropmi","lave","tcelfeR","yxorP","rorrE","stnemugra","tnemucod","noitacol","eikooc","egarotSlacol","egarotSnoisses","BDdexedni","hctef","tseuqeRpttHLMX","tekcoSbeW","rekroW","rekroWderahS","rekroWecivreS","renepo","tnerap","pot","semarf","fles"].map(e=>e.split("").reverse().join(""));const r=/* @__PURE__ */new Set(["script","iframe","object","embed","form","input","button","textarea","select","link","meta","base","style","template","slot","portal","frame","frameset","applet","noscript","noembed","plaintext","xmp","listing"]),n=/^on\w+$/i,o=/* @__PURE__ */new Set(["href","src","action","formaction","data","srcdoc","xlink:href","poster","srcset"]),s=/^\s*(javascript|vbscript|data|blob):/i;function i(e){if(!e||"string"!=typeof e)return"";if(-1===e.indexOf("<"))return e;const t=document.createElement("template");return t.innerHTML=e,function(e){const t=[],i=e.querySelectorAll("*");for(const l of i){const e=l.tagName.toLowerCase();if(r.has(e)){t.push(l);continue}if("svg"===e||"http://www.w3.org/2000/svg"===l.namespaceURI){if(Array.from(l.attributes).some(e=>n.test(e.name)||"href"===e.name||"xlink:href"===e.name)){t.push(l);continue}}const i=[];for(const t of l.attributes){const e=t.name.toLowerCase();n.test(e)?i.push(t.name):(o.has(e)&&s.test(t.value)||"style"===e&&/expression\s*\(|javascript:|behavior\s*:/i.test(t.value))&&i.push(t.name)}i.forEach(e=>l.removeAttribute(e))}t.forEach(e=>e.remove())}(t.content),t.innerHTML}const l='<svg viewBox="0 0 16 16" width="12" height="12"><path fill="currentColor" d="M6 10.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm-2-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/></svg>',a={expand:"▶",collapse:"▼",sortAsc:"▲",sortDesc:"▼",sortNone:"⇅",submenuArrow:"▶",dragHandle:"⋮⋮",toolPanel:"☰",filter:l,filterActive:l,print:"🖨️"};class c{static dependencies;static manifest;aliases;version="undefined"!=typeof __GRID_VERSION__?__GRID_VERSION__:"dev";styles;cellRenderers;headerRenderers;cellEditors;grid;config;userConfig;#e;get defaultConfig(){return{}}constructor(e={}){this.userConfig=e}attach(e){this.#e?.abort(),this.#e=new AbortController,this.grid=e,this.config={...this.defaultConfig,...this.userConfig}}detach(){this.#e?.abort(),this.#e=void 0}getPlugin(e){return this.grid?.getPlugin(e)}emit(e,t){this.grid?.dispatchEvent?.(new CustomEvent(e,{detail:t,bubbles:!0}))}emitCancelable(e,t){const r=new CustomEvent(e,{detail:t,bubbles:!0,cancelable:!0});return this.grid?.dispatchEvent?.(r),r.defaultPrevented}on(e,t){this.grid?._pluginManager?.subscribe(this,e,t)}off(e){this.grid?._pluginManager?.unsubscribe(this,e)}emitPluginEvent(e,t){this.grid?._pluginManager?.emitPluginEvent(e,t)}requestRender(){this.grid?.requestRender?.()}requestColumnsRender(){this.grid?.requestColumnsRender?.()}requestRenderWithFocus(){this.grid?.requestRenderWithFocus?.()}requestAfterRender(){this.grid?.requestAfterRender?.()}requestVirtualRefresh(){this.grid?.requestVirtualRefresh?.()}get rows(){return this.grid?.rows??[]}get sourceRows(){return this.grid?.sourceRows??[]}get columns(){return this.grid?.columns??[]}get visibleColumns(){return this.grid?._visibleColumns??[]}get gridElement(){return this.grid?._hostElement}get disconnectSignal(){return this.#e?.signal??this.grid?.disconnectSignal}get gridIcons(){const e=this.grid?.gridConfig?.icons??{};return{...a,...e}}get isAnimationEnabled(){const e=this.grid?.effectiveConfig?.animation?.mode??"reduced-motion";if(!1===e||"off"===e)return!1;if(!0===e||"on"===e)return!0;const t=this.gridElement;if(t){return"0"!==getComputedStyle(t).getPropertyValue("--tbw-animation-enabled").trim()}return!0}get animationDuration(){const e=this.gridElement;if(e){const t=getComputedStyle(e).getPropertyValue("--tbw-animation-duration").trim(),r=parseInt(t,10);if(!isNaN(r))return r}return 200}setIcon(e,t,r){e.dataset.icon=t.replace(/([A-Z])/g,"-$1").toLowerCase(),"collapse"===t?e.dataset.expanded="":"expand"===t&&delete e.dataset.expanded;const n=this.#t(t,r);void 0!==n?"string"==typeof n?e.innerHTML=i(n):n instanceof HTMLElement&&(e.innerHTML="",e.appendChild(n.cloneNode(!0))):e.innerHTML=""}#t(e,t){return void 0!==t?t:this.grid?.gridConfig?.icons?.[e]}updateSortIndicator(e,t){e.querySelector('[part~="sort-indicator"], .sort-indicator')?.remove();const r=document.createElement("span");r.setAttribute("part","sort-indicator"),r.className="sort-indicator",t?(e.setAttribute("aria-sort","asc"===t?"ascending":"descending"),e.setAttribute("data-sort",t),this.setIcon(r,"asc"===t?"sortAsc":"sortDesc")):(e.setAttribute("aria-sort","none"),e.removeAttribute("data-sort"),this.setIcon(r,"sortNone"));const n=e.querySelector(".tbw-filter-btn")??e.querySelector(".resize-handle");return n?e.insertBefore(r,n):e.appendChild(r),r}warn(r,n){void 0!==n?console.warn(t(r,n,this.gridElement.id,this.name)):console.warn(`${e(this.gridElement.id,this.name)} ${r}`)}throwDiagnostic(e,r){throw new Error(t(e,r,this.gridElement.id,this.name))}}function d(e,t){return t?.length?[...t].sort((e,t)=>e-t).map(t=>e[t]).filter(e=>null!=e):e}function u(e,t=!0){if(null==e)return"";if(e instanceof Date)return e.toISOString();if("object"==typeof e)return JSON.stringify(e);const r=String(e);return t&&(r.includes(",")||r.includes('"')||r.includes("\n")||r.includes("\r"))?`"${r.replace(/"/g,'""')}"`:r}function f(e,t){const r=URL.createObjectURL(e),n=document.createElement("a");n.href=r,n.download=t,n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n),URL.revokeObjectURL(r)}function g(e){return JSON.stringify(h(e))}function h(e){if(null==e||"object"!=typeof e)return e;if(Array.isArray(e))return e.map(h);const t={};for(const r of Object.keys(e).sort())t[r]=h(e[r]);return t}class m{#r=/* @__PURE__ */new Map;#n=0;register(e){const t=g(e),r=this.#r.get(t);if(r)return r.id;this.#n++;const n=`s${this.#n}`;return this.#r.set(t,{id:n,style:e}),n}getStyleId(e){return this.#r.get(g(e))?.id}get size(){return this.#r.size}toXml(){if(0===this.#r.size)return"";let e="\n<Styles>";for(const{id:t,style:r}of this.#r.values())e+=p(t,r);return e+="\n</Styles>",e}}function p(e,t){let r=`\n<Style ss:ID="${e}">`;if(t.font&&(r+="<Font",t.font.name&&(r+=` ss:FontName="${t.font.name}"`),t.font.size&&(r+=` ss:Size="${t.font.size}"`),t.font.bold&&(r+=' ss:Bold="1"'),t.font.italic&&(r+=' ss:Italic="1"'),t.font.color&&(r+=` ss:Color="${t.font.color}"`),r+="/>"),t.fill){const e=t.fill.pattern??"Solid";r+=`<Interior ss:Color="${t.fill.color}" ss:Pattern="${e}"/>`}return t.numberFormat&&(r+=`<NumberFormat ss:Format="${t.numberFormat}"/>`),t.alignment&&(r+="<Alignment",t.alignment.horizontal&&(r+=` ss:Horizontal="${t.alignment.horizontal}"`),t.alignment.vertical&&(r+=` ss:Vertical="${t.alignment.vertical}"`),t.alignment.wrapText&&(r+=' ss:WrapText="1"'),r+="/>"),t.borders&&(r+="<Borders>",t.borders.top&&(r+=b("Top",t.borders.top)),t.borders.bottom&&(r+=b("Bottom",t.borders.bottom)),t.borders.left&&(r+=b("Left",t.borders.left)),t.borders.right&&(r+=b("Right",t.borders.right)),r+="</Borders>"),r+="</Style>",r}function b(e,t){let r=`<Border ss:Position="${e}" ss:LineStyle="Continuous" ss:Weight="${function(e){switch(e){case"Thin":return 1;case"Medium":return 2;case"Thick":return 3}}(t.style)}"`;return t.color&&(r+=` ss:Color="${t.color}"`),r+="/>",r}function y(e,t,r,n,o){if(t.cellStyle){const s=t.cellStyle(r,n,o);if(s)return e.register(s)}const s=t.columnStyles?.[n];return s?e.getStyleId(s):t.defaultStyle?e.getStyleId(t.defaultStyle):void 0}function S(e,t){const r=Math.min(t.length,50);let n=(e.header??e.field).length;for(let o=0;o<r;o++){const r=t[o][e.field],s=null==r?0:String(r).length;s>n&&(n=s)}return n+2}function w(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function C(e,t,r){const n=r.excelStyles,o=n?function(e){const t=new m;if(e.headerStyle&&t.register(e.headerStyle),e.defaultStyle&&t.register(e.defaultStyle),e.columnStyles)for(const r of Object.values(e.columnStyles))t.register(r);return t}(n):void 0;let s='<?xml version="1.0" encoding="UTF-8"?>\n<?mso-application progid="Excel.Sheet"?>\n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\n xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';if(o){if(n.cellStyle)for(const r of e)for(const e of t){const t=r[e.field],s=n.cellStyle(t,e.field,r);s&&o.register(s)}s+=o.toXml()}s+='\n<Worksheet ss:Name="Sheet1">\n<Table>',n&&(s+=function(e,t,r){const n=r.columnWidths,o=r.autoFitColumns;if(!n&&!o)return"";let s="";for(const i of e){let e=n?.[i.field];null==e&&o&&(e=S(i,t)),s+=null!=e?`\n<Column ss:Width="${7*e}"/>`:"\n<Column/>"}return s}(t,e,n));const i=n?.headerStyle&&o?o.getStyleId(n.headerStyle):void 0;if(!1!==r.includeHeaders){s+="\n<Row>";for(const e of t){const t=e.header||e.field;s+=`<Cell${i?` ss:StyleID="${i}"`:""}><Data ss:Type="String">${w(r.processHeader?r.processHeader(t,e.field):t)}</Data></Cell>`}s+="</Row>"}for(const l of e){s+="\n<Row>";for(const e of t){let t=l[e.field];r.processCell&&(t=r.processCell(t,e.field,l));let i="String",a="";null==t?a="":"number"!=typeof t||isNaN(t)?t instanceof Date?(i="DateTime",a=t.toISOString()):a=w(String(t)):(i="Number",a=String(t));const c=o&&n?y(o,n,t,e.field,l):void 0;s+=`<Cell${c?` ss:StyleID="${c}"`:""}><Data ss:Type="${i}">${a}</Data></Cell>`}s+="</Row>"}return s+="\n</Table>\n</Worksheet>\n</Workbook>",s}class v extends c{name="export";get defaultConfig(){return{fileName:"export",includeHeaders:!0,onlyVisible:!0,onlySelected:!1}}isExportingFlag=!1;lastExportInfo=null;performExport(e,t){const r=this.config,n={format:e,fileName:t?.fileName??r.fileName??"export",includeHeaders:t?.includeHeaders??r.includeHeaders,processCell:t?.processCell,processHeader:t?.processHeader,columns:t?.columns,rowIndices:t?.rowIndices,excelStyles:t?.excelStyles,fileExtension:t?.fileExtension},o=function(e,t,r=!0){let n=e;if(r&&(n=n.filter(e=>!e.hidden&&!e.field.startsWith("__")&&!0!==e.meta?.utility)),t?.length){const e=new Set(t);n=n.filter(t=>e.has(t.field))}return n}(this.columns,t?.columns,r.onlyVisible);let s;if(t?.rowIndices)s=d(this.rows,t.rowIndices);else if(r.onlySelected){const e=this.getSelectionState();s=e?.selected?.size?d(this.rows,[...e.selected]):[...this.rows]}else s=[...this.rows];this.isExportingFlag=!0;let i=n.fileName;try{switch(e){case"csv":{const e=function(e,t,r,n={}){const o=n.delimiter??",",s=n.newline??"\n",i=[],l=n.bom?"\ufeff":"";if(!1!==r.includeHeaders){const e=t.map(e=>{const t=e.header||e.field;return u(r.processHeader?r.processHeader(t,e.field):t)});i.push(e.join(o))}for(const a of e){const e=t.map(e=>{let t=a[e.field];return r.processCell&&(t=r.processCell(t,e.field,a)),u(t)});i.push(e.join(o))}return l+i.join(s)}(s,o,n,{bom:!0});i=i.endsWith(".csv")?i:`${i}.csv`,function(e,t){f(new Blob([e],{type:"text/csv;charset=utf-8;"}),t)}(e,i);break}case"excel":{const e=C(s,o,n),t=n.fileExtension??".xls",r=t.startsWith(".")?t:`.${t}`;i=i.endsWith(r)?i:`${i}${r}`,function(e,t){f(new Blob([e],{type:"application/vnd.ms-excel;charset=utf-8;"}),t)}(e,i);break}case"json":{const e=s.map(e=>{const t={};for(const r of o){let o=e[r.field];n.processCell&&(o=n.processCell(o,r.field,e)),t[r.field]=o}return t}),t=JSON.stringify(e,null,2);i=i.endsWith(".json")?i:`${i}.json`;f(new Blob([t],{type:"application/json"}),i);break}}this.lastExportInfo={format:e,timestamp:/* @__PURE__ */new Date},this.emit("export-complete",{format:e,fileName:i,rowCount:s.length,columnCount:o.length})}finally{this.isExportingFlag=!1}}getSelectionState(){try{return this.grid?.getPluginState?.("selection")??null}catch{return null}}exportCsv(e){this.performExport("csv",e)}exportExcel(e){this.performExport("excel",e)}exportJson(e){this.performExport("json",e)}isExporting(){return this.isExportingFlag}getLastExport(){return this.lastExportInfo}}export{v as ExportPlugin};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|