@taskctrl/canvas-grid 0.1.0 → 0.1.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/dist/canvas-grid.cjs.js +1 -1
- package/dist/canvas-grid.es.js +978 -790
- package/dist/capture.d.ts +11 -1
- package/dist/dom/ColumnHeaders.d.ts +8 -2
- package/dist/layers/CellLayer.d.ts +5 -0
- package/dist/types.d.ts +33 -2
- package/package.json +1 -1
package/dist/canvas-grid.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("react/jsx-runtime"),m=require("react"),Yt=require("react-dom");class Ue{constructor(e){this.scrollX=0,this.scrollY=0,this.canvasWidth=0,this.canvasHeight=0,this.rowHeight=32,this.pinnedWidth=0,this._columnOffsets=[],this._columnWidths=[],this._pinnedOffsets=[],this._pinnedWidths=[],this._scrollableOffsets=[],this._scrollableWidths=[],e&&this.update(e)}update(e){e.scrollX!==void 0&&(this.scrollX=e.scrollX),e.scrollY!==void 0&&(this.scrollY=e.scrollY),e.canvasWidth!==void 0&&(this.canvasWidth=e.canvasWidth),e.canvasHeight!==void 0&&(this.canvasHeight=e.canvasHeight),e.rowHeight!==void 0&&(this.rowHeight=e.rowHeight)}setColumnOffsets(e){this._columnOffsets=e}setColumnWidths(e){this._columnWidths=e}setPinnedOffsets(e,o){this._pinnedOffsets=e,this._pinnedWidths=o}setScrollableOffsets(e,o){this._scrollableOffsets=e,this._scrollableWidths=o}rowToY(e){return e*this.rowHeight-this.scrollY}yToRow(e){return Math.floor((e+this.scrollY)/this.rowHeight)}colToX(e,o){return o===!0?this._pinnedOffsets[e]??0:o===!1?(this._scrollableOffsets[e]??0)-this.scrollX+this.pinnedWidth:(this._columnOffsets[e]??0)-this.scrollX}xToColPinned(e){for(let n=0;n<this._pinnedOffsets.length;n++){const r=this._pinnedOffsets[n],d=this._pinnedWidths[n]??0;if(e>=r&&e<r+d)return{index:n,pinned:!0}}const o=e-this.pinnedWidth+this.scrollX;for(let n=0;n<this._scrollableOffsets.length;n++){const r=this._scrollableOffsets[n],d=this._scrollableWidths[n]??0;if(o>=r&&o<r+d)return{index:n,pinned:!1}}return null}xToCol(e){const o=e+this.scrollX;for(let n=0;n<this._columnOffsets.length;n++){const r=this._columnOffsets[n],d=this._columnWidths[n]??0;if(o>=r&&o<r+d)return n}return-1}}const mt=["grid","cells","overlay"];class Vt{constructor(e){this._dirty={grid:!1,cells:!1,overlay:!1},this._rafId=null,this._drawFn=e}markDirty(e){this._dirty[e]=!0,this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}markAllDirty(){for(const e of mt)this._dirty[e]=!0;this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}destroy(){this._rafId!==null&&(cancelAnimationFrame(this._rafId),this._rafId=null)}_flush(){this._rafId=null;for(const e of mt)this._dirty[e]&&(this._dirty[e]=!1,this._drawFn(e))}}class xt{constructor(e,o){this._rows=e,this._expandedRows=o,this._depthMap=new Map,this._childrenMap=new Map,this._buildMaps(),this._flattenedRows=this._buildFlattenedRows()}_buildMaps(){for(const n of this._rows)this._childrenMap.has(n.id)||this._childrenMap.set(n.id,[]),n.parentId!==void 0&&(this._childrenMap.has(n.parentId)||this._childrenMap.set(n.parentId,[]),this._childrenMap.get(n.parentId).push(n));const o=this._rows.filter(n=>n.parentId===void 0).map(n=>({row:n,depth:0}));for(;o.length>0;){const{row:n,depth:r}=o.pop();this._depthMap.set(n.id,r);const d=this._childrenMap.get(n.id)??[];for(const a of d)o.push({row:a,depth:r+1})}}_buildFlattenedRows(){const e=[],o=this._rows.filter(r=>r.parentId===void 0),n=r=>{if(e.push(r),this._expandedRows.has(r.id)){const d=this._childrenMap.get(r.id)??[];for(const a of d)n(a)}};for(const r of o)n(r);return e}get flattenedRows(){return this._flattenedRows}getDepth(e){return this._depthMap.get(e)??0}hasChildren(e){const o=this._childrenMap.get(e);return o!==void 0&&o.length>0}isExpanded(e){return this._expandedRows.has(e)}toggle(e){const o=new Set(this._expandedRows);return o.has(e)?o.delete(e):o.add(e),o}getVisibleRange(e,o,n){if(n<=0||this._flattenedRows.length===0)return{startIndex:0,endIndex:0};const r=Math.max(0,Math.floor(e/n)),d=Math.min(this._flattenedRows.length-1,Math.ceil((e+o)/n)-1);return{startIndex:r,endIndex:d}}}const Gt=30;class me{constructor(e){this._columns=e}getVisibleColumns(){return this._columns.filter(e=>!e.hidden)}getPinnedColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned==="left")}getScrollableColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned!=="left")}getPinnedWidth(){return this.getPinnedColumns().reduce((e,o)=>e+o.width,0)}getAllColumns(){return[...this._columns]}getTotalWidth(){return this.getVisibleColumns().reduce((e,o)=>e+o.width,0)}getColumnX(e){const o=this.getPinnedColumns(),n=this.getScrollableColumns();let r=0;for(const d of o){if(d.id===e)return r;r+=d.width}for(const d of n){if(d.id===e)return r;r+=d.width}return-1}getColumnAtX(e){let o=0;for(const n of this.getPinnedColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}for(const n of this.getScrollableColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}return null}getVisibleRange(e,o){const n=this.getScrollableColumns();if(n.length===0)return{startIndex:0,endIndex:0};const r=this.getPinnedWidth(),d=o-r;let a=0,w=0,f=n.length-1;for(let S=0;S<n.length;S++)a+n[S].width<=e&&(w=S+1),a<e+d&&(f=S),a+=n[S].width;return w=Math.min(w,n.length-1),f=Math.min(f,n.length-1),{startIndex:w,endIndex:f}}getPinnedRange(){const e=this.getPinnedColumns();return e.length===0?{startIndex:0,endIndex:-1}:{startIndex:0,endIndex:e.length-1}}resize(e,o){const n=this._columns.map(r=>{if(r.id!==e)return r;const d=r.minWidth??Gt;return{...r,width:Math.max(d,o)}});return new me(n)}reorder(e,o){const n=[...this._columns],r=n.findIndex(w=>w.id===e);if(r===-1)return new me(n);const[d]=n.splice(r,1),a=Math.max(0,Math.min(o,n.length));return n.splice(a,0,d),new me(n)}hide(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!0}:n);return new me(o)}show(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!1}:n);return new me(o)}}function xe(t,e){return`${t}|${e}`}function $e(){return{cells:new Set,rows:new Set,columns:new Set,anchor:void 0}}class yt{constructor(e){e?this._selection={cells:new Set(e.cells),rows:new Set(e.rows),columns:new Set(e.columns),anchor:e.anchor}:this._selection=$e()}click(e,o,n,r){this._selection=$e(),this._selection.cells.add(xe(e,o)),this._selection.anchor={rowId:e,colId:o}}ctrlClick(e,o){const n=xe(e,o);this._selection.cells.has(n)?this._selection.cells.delete(n):this._selection.cells.add(n),this._selection.anchor={rowId:e,colId:o}}shiftClick(e,o,n,r){const d=this._selection.anchor;if(!d){this.click(e,o,n,r);return}const a=n.indexOf(d.rowId),w=n.indexOf(e),f=r.indexOf(d.colId),S=r.indexOf(o),y=Math.min(a,w),L=Math.max(a,w),j=Math.min(f,S),X=Math.max(f,S),k=new Set;for(let G=y;G<=L;G++)for(let $=j;$<=X;$++)k.add(xe(n[G],r[$]));this._selection.cells=k,this._selection.rows=new Set,this._selection.columns=new Set}selectRow(e,o){for(const n of o)this._selection.cells.add(xe(e,n));this._selection.rows.add(e),this._selection.anchor={rowId:e,colId:o[0]??""}}selectColumn(e,o){for(const n of o)this._selection.cells.add(xe(n,e));this._selection.columns.add(e),this._selection.anchor={rowId:o[0]??"",colId:e}}clear(){this._selection=$e()}isSelected(e,o){return this._selection.cells.has(xe(e,o))}isRowSelected(e){return this._selection.rows.has(e)}isColumnSelected(e){return this._selection.columns.has(e)}getSelection(){return{cells:new Set(this._selection.cells),rows:new Set(this._selection.rows),columns:new Set(this._selection.columns),anchor:this._selection.anchor}}}function Le(t,e,o,n,r){const d=o.yToRow(e),a=n.flattenedRows;if(d<0||d>=a.length)return null;const w=r.getPinnedColumns(),f=r.getPinnedWidth();if(w.length>0){if(t<f){let X=0;for(const k of w){if(t>=X&&t<X+k.width)return{rowId:a[d].id,colId:k.id};X+=k.width}return null}const y=t-f+o.scrollX,L=r.getScrollableColumns();let j=0;for(const X of L){if(y>=j&&y<j+X.width)return{rowId:a[d].id,colId:X.id};j+=X.width}return null}const S=r.getColumnAtX(t+o.scrollX);return S===null?null:{rowId:a[d].id,colId:S}}function $t(t,e,o){const n=window.devicePixelRatio||1;(t.width!==Math.round(e*n)||t.height!==Math.round(o*n))&&(t.width=Math.round(e*n),t.height=Math.round(o*n),t.style.width=`${e}px`,t.style.height=`${o}px`);const d=t.getContext("2d");return d.setTransform(n,0,0,n,0,0),d}function It(t,e,o,n,r={}){const{canvasWidth:d,canvasHeight:a,rowHeight:w}=e,{rowStyle:f}=r;t.clearRect(0,0,d,a),t.fillStyle="#f3f4f6",t.fillRect(0,0,d,a);const S=o.getVisibleRange(e.scrollY,a,w),y=o.flattenedRows,L=n.getPinnedColumns(),j=n.getScrollableColumns(),X=n.getPinnedWidth(),k=L.length>0;if(y.length===0||L.length===0&&j.length===0)return;const G=n.getTotalWidth(),$=Math.min(d,G-e.scrollX+(k?e.scrollX:0)),P=k?Math.min(d,X+n.getScrollableColumns().reduce((W,x)=>W+x.width,0)-e.scrollX):$,s=y.length*w,Y=Math.min(a,s-e.scrollY);for(let W=S.startIndex;W<=S.endIndex;W++){const x=y[W];if(!x)continue;const H=e.rowToY(W),b=o.getDepth(x.id);let h=W%2===0?"#ffffff":"#f9fafb";if(f){const u=f(x,b);u!=null&&u.backgroundColor&&(h=u.backgroundColor)}t.fillStyle=h,t.fillRect(0,H,Math.max(P,X),w),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,H+w),t.lineTo(Math.max(P,X),H+w),t.stroke()}if(Y>0){const W=n.getVisibleRange(e.scrollX,d);t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let x=W.startIndex;x<=W.endIndex;x++){const H=j[x];if(!H)continue;const b=e.colToX(x,!1);t.beginPath(),t.moveTo(b+H.width,0),t.lineTo(b+H.width,Y),t.stroke()}}if(k&&Y>0){t.fillStyle="#ffffff",t.fillRect(0,0,X,a);for(let x=S.startIndex;x<=S.endIndex;x++){const H=y[x];if(!H)continue;const b=e.rowToY(x),h=o.getDepth(H.id);let u=x%2===0?"#ffffff":"#f9fafb";if(f){const I=f(H,h);I!=null&&I.backgroundColor&&(u=I.backgroundColor)}t.fillStyle=u,t.fillRect(0,b,X,w),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,b+w),t.lineTo(X,b+w),t.stroke()}const W=n.getPinnedRange();t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let x=W.startIndex;x<=W.endIndex;x++){const H=L[x];if(!H)continue;const b=e.colToX(x,!0);t.beginPath(),t.moveTo(b+H.width,0),t.lineTo(b+H.width,Y),t.stroke()}t.strokeStyle="#d1d5db",t.lineWidth=1,t.beginPath(),t.moveTo(X,0),t.lineTo(X,Y),t.stroke()}}function Rt(t){function e(a,w,f,S,y,L){t.fillStyle=L,t.beginPath(),t.roundRect(a,w,f,S,y),t.fill()}function o(a,w,f,S){t.fillText(a,w,f,S)}function n(a,w){if(t.measureText(a).width<=w)return a;let f=a;for(;f.length>0;)if(f=f.slice(0,-1),t.measureText(f+"…").width<=w)return f+"…";return"…"}function r(a,w,f,S,y){const k=t.measureText(f).width+16;return e(a,w,k,20,20/2,S),t.fillStyle=y,t.fillText(f,a+8,w+20/2),k}function d(a,w,f,S,y,L){e(a,w,f,S,S/2,"#d1d5db");const j=f*Math.max(0,Math.min(1,y));j>0&&e(a,w,j,S,S/2,L)}return{roundRect:e,fillText:o,truncateText:n,badge:r,progressBar:d}}function St(t,e,o,n,r,d){const{canvasWidth:a,canvasHeight:w,rowHeight:f}=e,{cellRenderer:S,getCellData:y,hoveredCell:L}=d;t.clearRect(0,0,a,w);const j=o.getVisibleRange(e.scrollY,w,f),X=o.flattenedRows,k=n.getPinnedColumns(),G=n.getScrollableColumns(),$=k.length>0;if(X.length===0||k.length===0&&G.length===0)return;const P=Rt(t),s=(W,x,H,b)=>{const h={x:H,y:b,width:x.width,height:f},u={rowId:W.id,colId:x.id,data:y(W.id,x.id)},I={selected:r.isSelected(W.id,x.id),hovered:L!=null&&L.rowId===W.id&&L.colId===x.id,rowSelected:r.isRowSelected(W.id),colSelected:r.isColumnSelected(x.id),filtered:!0,depth:o.getDepth(W.id),hasChildren:o.hasChildren(W.id),expanded:o.isExpanded(W.id)};t.save(),t.beginPath(),t.rect(H,b,x.width,f),t.clip(),S(t,u,h,I,P),t.restore()},Y=n.getVisibleRange(e.scrollX,a);for(let W=j.startIndex;W<=j.endIndex;W++){const x=X[W];if(!x)continue;const H=e.rowToY(W);for(let b=Y.startIndex;b<=Y.endIndex;b++){const h=G[b];if(!h)continue;const u=e.colToX(b,$?!1:void 0);s(x,h,u,H)}}if($){n.getPinnedWidth();const W=n.getPinnedRange();for(let x=j.startIndex;x<=j.endIndex;x++){const H=X[x];if(!H)continue;const b=e.rowToY(x);t.save(),t.fillStyle="#ffffff",t.restore();for(let h=W.startIndex;h<=W.endIndex;h++){const u=k[h];if(!u)continue;const I=e.colToX(h,!0);s(H,u,I,b)}}}}function Bt(t,e,o,n,r,d={}){const{canvasWidth:a,canvasHeight:w,rowHeight:f}=e,{hoveredCell:S,hoveredRowId:y,selectedCell:L,resizingColumn:j,loading:X}=d;t.clearRect(0,0,a,w);const k=o.getVisibleRange(e.scrollY,w,f),G=o.flattenedRows,$=n.getPinnedColumns(),P=n.getScrollableColumns(),s=$.length>0;if(n.getPinnedWidth(),G.length===0||$.length===0&&P.length===0)return;const Y=n.getVisibleRange(e.scrollX,a),W=n.getPinnedRange(),x=b=>{for(let h=Y.startIndex;h<=Y.endIndex;h++){const u=P[h];if(!u)continue;const I=s?e.colToX(h,!1):e.colToX(h);b(u,I)}if(s)for(let h=W.startIndex;h<=W.endIndex;h++){const u=$[h];if(!u)continue;const I=e.colToX(h,!0);b(u,I)}};if(L){for(let b=k.startIndex;b<=k.endIndex;b++){const h=G[b];if(!h||h.id!==L.rowId)continue;const u=e.rowToY(b);x((I,F)=>{t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(F,u,I.width,f)});break}x((b,h)=>{if(b.id===L.colId)for(let u=k.startIndex;u<=k.endIndex;u++){if(!G[u])continue;const F=e.rowToY(u);t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(h,F,b.width,f)}})}const H=(b,h,u,I,F)=>{const B=b.width,K=f,c=r.isSelected(u.id,b.id),D=r.isRowSelected(u.id),E=r.isColumnSelected(b.id),z=y!=null&&y===u.id,V=S!=null&&S.rowId===u.id&&S.colId===b.id;c?(t.fillStyle="rgba(59,130,246,0.12)",t.fillRect(h,I,B,K),t.strokeStyle="#3b82f6",t.lineWidth=2,t.strokeRect(h+1,I+1,B-2,K-2)):D||E||V?(t.fillStyle="rgba(59,130,246,0.08)",t.fillRect(h,I,B,K)):z&&(t.fillStyle="rgba(59,130,246,0.04)",t.fillRect(h,I,B,K))};for(let b=k.startIndex;b<=k.endIndex;b++){const h=G[b];if(!h)continue;const u=e.rowToY(b);for(let I=Y.startIndex;I<=Y.endIndex;I++){const F=P[I];if(!F)continue;const B=s?e.colToX(I,!1):e.colToX(I);H(F,B,h,u)}}if(s)for(let b=k.startIndex;b<=k.endIndex;b++){const h=G[b];if(!h)continue;const u=e.rowToY(b);for(let I=W.startIndex;I<=W.endIndex;I++){const F=$[I];if(!F)continue;const B=e.colToX(I,!0);H(F,B,h,u)}}if(j&&(t.save(),t.strokeStyle="#3b82f6",t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(j.x,0),t.lineTo(j.x,w),t.stroke(),t.setLineDash([]),t.restore()),X){const h=w-32;t.fillStyle="#f3f4f6",t.fillRect(0,h,a,32),t.strokeStyle="#e5e7eb",t.lineWidth=1,t.beginPath(),t.moveTo(0,h),t.lineTo(a,h),t.stroke(),t.fillStyle="#6b7280",t.font="13px system-ui, sans-serif",t.textAlign="center",t.textBaseline="middle",t.fillText("Loading...",a/2,h+32/2)}}function Kt({treeEngine:t,width:e,rowHeight:o,scrollTop:n,viewportHeight:r,sidebarRenderer:d,onToggle:a,onRowClick:w,hoveredRowId:f,onRowHover:S}){const y=t.flattenedRows,{startIndex:L,endIndex:j}=t.getVisibleRange(n,r,o),X=[];for(let $=L;$<=j;$++){const P=y[$];if(!P)continue;const s=t.getDepth(P.id),Y=t.hasChildren(P.id),W=t.isExpanded(P.id),x=$*o;let H;if(d)H=d(P,s,W,Y);else{const h=s*16+8;H=T.jsxs("div",{style:{display:"flex",alignItems:"center",height:"100%",paddingLeft:h,overflow:"hidden",whiteSpace:"nowrap",cursor:Y?"pointer":"default",userSelect:"none"},onPointerDown:Y?u=>{u.stopPropagation(),a(P.id)}:void 0,children:[Y?T.jsx("span",{style:{marginRight:4,flexShrink:0,fontSize:10,color:"#6b7280"},children:W?"▾":"▸"}):T.jsx("span",{style:{width:14,flexShrink:0}}),T.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",fontSize:12},children:String(P.title??P.id)})]})}const b=f===P.id;X.push(T.jsx("div",{role:"rowheader","aria-level":s+1,"aria-expanded":Y?W:void 0,style:{position:"absolute",top:x,left:0,width:"100%",height:o,boxSizing:"border-box",fontSize:13,color:"#374151",borderBottom:"1px solid #e5e7eb",cursor:w?"pointer":"default",backgroundColor:b?"rgba(59,130,246,0.04)":void 0},onPointerDown:w?h=>w(P.id,h):void 0,onPointerEnter:()=>S==null?void 0:S(P.id),onPointerLeave:()=>S==null?void 0:S(null),children:H},String(P.id)))}const k=y.length*o,G=Math.min(r,k-n);return T.jsxs("div",{style:{width:e,height:r,overflow:"hidden",position:"relative",flexShrink:0,backgroundColor:"#ffffff"},children:[T.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:Math.max(0,G),backgroundColor:"#e5e7eb",zIndex:1}}),T.jsx("div",{style:{position:"absolute",top:0,left:0,width:"100%",height:k,transform:`translateY(-${n}px)`},children:X})]})}const Be=28,Ke=28,Ft=4;function Ut(t){switch(t){case"vertical":return 120;default:return 32}}function Nt({columnEngine:t,columnGroups:e,scrollX:o,orientation:n="horizontal",onColumnClick:r,onColumnResize:d,onColumnReorder:a,columnHeaderRenderer:w}){const f=t.getPinnedColumns(),S=t.getScrollableColumns(),y=t.getVisibleColumns(),L=t.getPinnedWidth(),j=new Map;if(e)for(const c of e)j.set(c.id,c);const X=m.useRef(null),[k,G]=m.useState(null),$=m.useRef(null),P=m.useRef(!1),s=m.useRef(null),Y=m.useRef(null);$.current=k;const W=[...f,...S],x=[];if(e&&e.length>0){let c,D=0,E=0,z=!1;for(const V of W){const J=t.getColumnX(V.id),Q=V.pinned==="left";if(V.group!==c){if(c!==void 0&&E>0){const Z=j.get(c);Z&&x.push({group:Z,startX:D,width:E,pinned:z})}c=V.group,D=J,E=V.width,z=Q}else E+=V.width}if(c!==void 0&&E>0){const V=j.get(c);V&&x.push({group:V,startX:D,width:E,pinned:z})}}const H=x.length>0,b=Ut(n),h=H?Be+b:b,u=m.useCallback((c,D)=>{D.stopPropagation(),D.preventDefault();const E=y.find(z=>z.id===c);E&&(X.current={colId:c,startX:D.clientX,startWidth:E.width})},[y]);m.useEffect(()=>{const c=E=>{const z=X.current;if(!z)return;const V=E.clientX-z.startX,J=Math.max(z.startWidth+V,30);d==null||d(z.colId,J)},D=()=>{X.current=null};return document.addEventListener("mousemove",c),document.addEventListener("mouseup",D),()=>{document.removeEventListener("mousemove",c),document.removeEventListener("mouseup",D)}},[d]);const I=m.useCallback(c=>{const D=Y.current;if(!D)return{dropIndex:0};const E=D.getBoundingClientRect(),z=c-E.left+o;let V=0;for(let J=0;J<y.length;J++){const Q=y[J],le=t.getColumnX(Q.id)+Q.width/2;z>le&&(V=J+1)}return{dropIndex:V}},[y,t,o]),F=m.useCallback((c,D)=>{D.target.dataset.resizeHandle||a&&(D.preventDefault(),P.current=!1,s.current={colId:c,startX:D.clientX,currentX:D.clientX})},[a]);m.useEffect(()=>{if(!a)return;const c=z=>{const V=s.current;if(!V)return;const J=Math.abs(z.clientX-V.startX);if(!P.current&&J>=Ft){P.current=!0;const Q=y.find(We=>We.id===V.colId),Z=t.getColumnX(V.colId),{dropIndex:le}=I(z.clientX);G({colId:V.colId,startX:V.startX,currentX:z.clientX,dropIndex:le,ghostLeft:Z-o,ghostWidth:(Q==null?void 0:Q.width)??80,ghostTitle:(Q==null?void 0:Q.title)??""});return}if(P.current){const{dropIndex:Q}=I(z.clientX);G(Z=>Z&&{...Z,currentX:z.clientX,dropIndex:Q})}},D=()=>{P.current&&$.current&&a($.current.colId,$.current.dropIndex),s.current=null,P.current=!1,G(null)},E=z=>{z.key==="Escape"&&(s.current=null,P.current=!1,G(null))};return document.addEventListener("mousemove",c),document.addEventListener("mouseup",D),document.addEventListener("keydown",E),()=>{document.removeEventListener("mousemove",c),document.removeEventListener("mouseup",D),document.removeEventListener("keydown",E)}},[a,y,t,o,I]);const B=()=>{if(!k)return 0;const{dropIndex:c}=k;if(c>=y.length){const D=y[y.length-1];return t.getColumnX(D.id)+D.width-o}return t.getColumnX(y[c].id)-o},K=(c,D,E)=>{const z=(k==null?void 0:k.colId)===c.id,J=c.pinned==="left"?"horizontal":n,Q=w==null?void 0:w(c.id,c.title);let Z;return Q?Z=Q:J==="vertical"?Z=T.jsx("div",{style:{writingMode:"vertical-rl",textOrientation:"mixed",transform:"rotate(180deg)",height:E-16,lineHeight:`${c.width}px`,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:c.title}):Z=T.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},children:c.title}),T.jsxs("div",{role:"columnheader",title:c.title,style:{position:"absolute",left:D,width:c.width,height:E,boxSizing:"border-box",borderRight:"1px solid #d1d5db",display:"flex",alignItems:J==="horizontal"?"center":"flex-end",justifyContent:J==="horizontal"?"flex-start":"center",paddingLeft:J==="horizontal"?8:0,paddingBottom:J!=="horizontal"?6:0,cursor:a?"grab":r?"pointer":"default",userSelect:"none",fontSize:13,fontWeight:500,color:"#6C737F",opacity:z?.4:1},onMouseDown:a?le=>F(c.id,le):void 0,onPointerDown:r&&!a?le=>r(c.id,le):void 0,children:[Z,d&&T.jsx("div",{"data-resize-handle":"true",style:{position:"absolute",right:0,top:0,width:4,height:"100%",cursor:"col-resize",zIndex:1},onMouseDown:le=>u(c.id,le)})]},c.id)};return T.jsxs("div",{ref:Y,style:{flex:1,height:h+1,overflow:"hidden",position:"relative",backgroundColor:"#f9fafb"},children:[T.jsxs("div",{style:{position:"absolute",top:0,left:0,transform:`translateX(-${o}px)`,height:h},children:[T.jsx("div",{style:{position:"absolute",top:0,height:h},children:S.map(c=>{const D=t.getColumnX(c.id);return K(c,D,h)})}),H&&T.jsx("div",{style:{position:"relative",height:Be,zIndex:1},children:x.filter(c=>!c.pinned).map(c=>T.jsx("div",{style:{position:"absolute",left:c.startX,width:c.width,height:Be,borderBottom:`1px solid ${c.group.color??"#3b82f6"}`,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:c.group.title},c.group.id))})]}),f.length>0&&T.jsxs("div",{style:{position:"absolute",top:0,left:0,width:L,height:h,backgroundColor:"#f9fafb",zIndex:2,display:"flex",flexDirection:"column"},children:[H&&T.jsx("div",{style:{position:"absolute",top:0,left:0,width:L,height:h-Ke},children:x.filter(c=>c.pinned).map(c=>T.jsx("div",{style:{position:"absolute",left:c.startX,width:c.width,height:"100%",borderBottom:"1px solid #d1d5db",borderRight:"1px solid #d1d5db",display:"flex",alignItems:"center",justifyContent:"center",boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:c.group.title},c.group.id))}),T.jsx("div",{style:{position:"absolute",bottom:0,left:0,width:L,height:Ke,borderTop:"1px solid #d1d5db"},children:f.map(c=>{const D=t.getColumnX(c.id);return K(c,D,Ke)})}),T.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:"100%",backgroundColor:"#d1d5db",zIndex:3}})]}),T.jsx("div",{style:{position:"absolute",bottom:0,left:0,right:0,height:1,backgroundColor:"#d1d5db",zIndex:5}}),k&&T.jsx("div",{style:{position:"absolute",top:0,left:B(),width:2,height:h,backgroundColor:"#3b82f6",pointerEvents:"none",zIndex:10}}),k&&T.jsx("div",{style:{position:"fixed",top:0,left:k.currentX-k.ghostWidth/2,width:k.ghostWidth,height:32,backgroundColor:"#dbeafe",border:"1px solid #3b82f6",borderRadius:4,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:12,fontWeight:600,color:"#1d4ed8",opacity:.85,pointerEvents:"none",zIndex:9999,whiteSpace:"nowrap",overflow:"hidden"},children:k.ghostTitle})]})}function qt({columnEngine:t,onColumnVisibilityChange:e}){const[o,n]=m.useState(!1),r=m.useRef(null),d=t.getAllColumns();return m.useEffect(()=>{if(!o)return;const a=f=>{r.current&&!r.current.contains(f.target)&&n(!1)},w=f=>{f.key==="Escape"&&n(!1)};return document.addEventListener("mousedown",a),document.addEventListener("keydown",w),()=>{document.removeEventListener("mousedown",a),document.removeEventListener("keydown",w)}},[o]),T.jsxs("div",{ref:r,style:{position:"relative",display:"inline-block"},children:[T.jsx("button",{onClick:()=>n(a=>!a),style:{display:"flex",alignItems:"center",gap:4,padding:"0 8px",height:28,fontSize:12,fontWeight:600,color:"#374151",backgroundColor:o?"#e5e7eb":"#f9fafb",border:"1px solid #d1d5db",borderRadius:4,cursor:"pointer",whiteSpace:"nowrap"},title:"Toggle column visibility",children:"Columns"}),o&&T.jsx("div",{style:{position:"absolute",top:"100%",left:0,marginTop:4,minWidth:160,backgroundColor:"#ffffff",border:"1px solid #d1d5db",borderRadius:6,boxShadow:"0 4px 12px rgba(0,0,0,0.12)",zIndex:1e3,padding:"4px 0"},children:d.map(a=>{const w=!a.hidden;return T.jsxs("label",{style:{display:"flex",alignItems:"center",gap:8,padding:"6px 12px",cursor:"pointer",fontSize:13,color:"#374151",userSelect:"none"},onMouseEnter:f=>{f.currentTarget.style.backgroundColor="#f3f4f6"},onMouseLeave:f=>{f.currentTarget.style.backgroundColor=""},children:[T.jsx("input",{type:"checkbox",checked:w,onChange:f=>{e(a.id,f.target.checked)},style:{cursor:"pointer",width:14,height:14}}),T.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:a.title})]},a.id)})})]})}function Jt({content:t,position:e}){return Yt.createPortal(T.jsx("div",{style:{position:"fixed",left:e.left,top:e.top,zIndex:9999,pointerEvents:"none"},children:t}),document.body)}function Qt({message:t}){return T.jsx("div",{role:"status","aria-live":"polite","aria-atomic":"true",style:{position:"absolute",width:1,height:1,padding:0,margin:-1,overflow:"hidden",clip:"rect(0,0,0,0)",whiteSpace:"nowrap",borderWidth:0},children:t})}const je=24,Fe=32;function Zt(t){const{treeEngine:e,columnEngine:o,columnGroups:n,rowHeight:r,sidebarWidth:d,gridLayerConfig:a,cellLayerConfig:w,scale:f=2}=t,S=n!=null&&n.length>0,y=S?je+Fe:Fe,L=o.getVisibleColumns(),j=e.flattenedRows,X=o.getTotalWidth(),k=j.length*r,G=d+X,$=y+k,P=document.createElement("canvas");P.width=Math.round(G*f),P.height=Math.round($*f),P.style.width=`${G}px`,P.style.height=`${$}px`;const s=P.getContext("2d");s.setTransform(f,0,0,f,0,0),s.fillStyle="#ffffff",s.fillRect(0,0,G,$);const Y=new Ue({scrollX:0,scrollY:0,canvasWidth:X,canvasHeight:k,rowHeight:r}),W=[],x=[];let H=0;for(const u of L)W.push(H),x.push(u.width),H+=u.width;Y.setColumnOffsets(W),Y.setColumnWidths(x),s.save(),s.translate(d,y),It(s,Y,e,o,a),St(s,Y,e,o,t.selectionEngine,w),s.restore(),s.save(),s.translate(0,y);const b="#ffffff";s.fillStyle=b,s.fillRect(0,0,d,k),s.strokeStyle="#e5e7eb",s.lineWidth=.5,s.beginPath(),s.moveTo(d,0),s.lineTo(d,k),s.stroke(),s.font="13px system-ui, sans-serif",s.textBaseline="middle",s.fillStyle="#374151";for(let u=0;u<j.length;u++){const I=j[u];if(!I)continue;const F=e.getDepth(I.id),B=u*r,K=F*16+8,c=String(I.title??I.id),D=d-K-8;s.fillStyle=u%2===0?"#ffffff":"#f9fafb",s.fillRect(0,B,d,r),s.strokeStyle="#e5e7eb",s.lineWidth=.5,s.beginPath(),s.moveTo(0,B+r),s.lineTo(d,B+r),s.stroke(),s.fillStyle="#374151";let E=c;if(s.measureText(E).width>D&&D>0){for(;E.length>0&&s.measureText(E+"…").width>D;)E=E.slice(0,-1);E=E+"…"}s.fillText(E,K,B+r/2)}if(s.restore(),s.save(),s.translate(0,0),s.fillStyle="#f9fafb",s.fillRect(0,0,G,y),s.strokeStyle="#e5e7eb",s.lineWidth=1,s.beginPath(),s.moveTo(0,y),s.lineTo(G,y),s.stroke(),s.fillStyle="#f9fafb",s.fillRect(0,0,d,y),s.strokeStyle="#e5e7eb",s.lineWidth=.5,s.beginPath(),s.moveTo(d,0),s.lineTo(d,y),s.stroke(),S&&n){const u=new Map;for(const c of n)u.set(c.id,c);let I,F=0,B=0;const K=(c,D,E)=>{if(c===void 0||E<=0)return;const z=u.get(c);if(!z)return;const V=d+D;s.fillStyle=z.color??"#3b82f6",s.fillRect(V,je-2,E,2),s.font="600 11px system-ui, sans-serif",s.fillStyle="#6b7280",s.textBaseline="middle",s.textAlign="left",s.fillText(z.title,V+8,je/2)};for(const c of L){const D=o.getColumnX(c.id);c.group!==I?(K(I,F,B),I=c.group,F=D,B=c.width):B+=c.width}K(I,F,B)}const h=S?je:0;s.font="600 12px system-ui, sans-serif",s.textBaseline="middle",s.textAlign="left",s.fillStyle="#374151";for(const u of L){const I=o.getColumnX(u.id),F=d+I;s.strokeStyle="#e5e7eb",s.lineWidth=.5,s.beginPath(),s.moveTo(F+u.width,h),s.lineTo(F+u.width,y),s.stroke(),s.fillStyle="#374151";const B=u.width-16;let K=u.title;if(B>0&&s.measureText(K).width>B){for(;K.length>0&&s.measureText(K+"…").width>B;)K=K.slice(0,-1);K=K+"…"}s.fillText(K,F+8,h+Fe/2)}return s.restore(),P}const wt=180,bt=32,en=24,Ct=200;function vt(t,e){let o;switch(t){case"vertical":o=120;break;default:o=32}return e?en+o:o}const tn=m.memo(m.forwardRef(function(e,o){const{rows:n,columns:r,columnGroups:d,getCellData:a,cellRenderer:w,sidebarRenderer:f,sidebarWidth:S=wt,rowHeight:y=bt,headerOrientation:L="horizontal",rowStyle:j,selection:X,onSelectionChange:k,onCellClick:G,onCellDoubleClick:$,onCellContextMenu:P,onCellHover:s,onColumnHeaderClick:Y,onTreeToggle:W,defaultExpandedRows:x,onExpandChange:H,onLoadMore:b,hasMore:h,loading:u,hoverContent:I,hoverPosition:F,showColumnVisibilityMenu:B,ariaLabel:K="Data grid"}=e,[c,D]=m.useState(()=>new Set(x??[])),[E,z]=m.useState(0),[V,J]=m.useState(0),[Q,Z]=m.useState(r),[le,We]=m.useState(null),[kt,we]=m.useState(""),[_t,ze]=m.useState(null),De=m.useRef(null),Ne=m.useRef(null);m.useEffect(()=>{Z(r)},[r]);const qe=m.useRef(null),fe=m.useRef([]),ie=m.useRef(new Ue({rowHeight:y})),N=m.useRef(null),A=m.useRef(e),de=m.useRef(null),Xe=m.useRef(null),He=m.useRef(!1);A.current=e;const ye=m.useMemo(()=>new xt(n,c),[n,c]),se=m.useMemo(()=>new me(Q),[Q]),Je=m.useMemo(()=>new yt(X),[X]);de.current={treeEngine:ye,columnEngine:se,selectionEngine:Je};const Ae=se.getPinnedColumns().length>0,Ie=Ae?0:S,Qe=m.useCallback(l=>{const g=de.current;if(!g)return;const{treeEngine:R,columnEngine:M,selectionEngine:v}=g,C=ie.current,i=l==="grid"?0:l==="cells"?1:2,p=fe.current[i];if(!p)return;const _=$t(p,C.canvasWidth,C.canvasHeight);l==="grid"?It(_,C,R,M,{rowStyle:A.current.rowStyle}):l==="cells"?St(_,C,R,M,v,{cellRenderer:A.current.cellRenderer,getCellData:A.current.getCellData,hoveredCell:Xe.current}):Bt(_,C,R,M,v,{hoveredCell:Xe.current,hoveredRowId:De.current,selectedCell:Ne.current,resizingColumn:le??void 0,loading:A.current.loading})},[le]);m.useEffect(()=>{const l=se.getPinnedColumns(),g=se.getScrollableColumns(),R=se.getPinnedWidth(),M=[],v=[];let C=0;for(const ne of l)M.push(C),v.push(ne.width),C+=ne.width;const i=[],p=[];let _=0;for(const ne of g)i.push(_),p.push(ne.width),_+=ne.width;const O=ie.current;O.pinnedWidth=R,O.setPinnedOffsets(M,v),O.setScrollableOffsets(i,p);const U=se.getVisibleColumns(),ee=[],re=[];let be=0;for(const ne of U)ee.push(be),re.push(ne.width),be+=ne.width;O.setColumnOffsets(ee),O.setColumnWidths(re),O.update({rowHeight:y})},[se,y]),m.useEffect(()=>{const l=new Vt(Qe);return N.current=l,l.markAllDirty(),()=>{l.destroy(),N.current=null}},[Qe]),m.useEffect(()=>{const l=qe.current;if(!l)return;const g=new ResizeObserver(R=>{var M,v;for(const C of R){const{width:i,height:p}=C.contentRect,_=i-Ie,O=vt(A.current.headerOrientation??"horizontal",(((M=A.current.columnGroups)==null?void 0:M.length)??0)>0),U=p-O;ie.current.update({canvasWidth:Math.max(0,_),canvasHeight:Math.max(0,U)}),(v=N.current)==null||v.markAllDirty()}});return g.observe(l),()=>g.disconnect()},[Ie]),m.useEffect(()=>{var l;(l=N.current)==null||l.markAllDirty()},[ye,se,Je]),m.useEffect(()=>{var l;(l=N.current)==null||l.markDirty("overlay")},[u]);const Ye=m.useCallback(l=>{var re;l.preventDefault();const g=ie.current,R=de.current;if(!R)return;const M=R.treeEngine.flattenedRows.length*g.rowHeight,v=R.columnEngine.getTotalWidth(),C=Math.max(0,M-g.canvasHeight),i=v-R.columnEngine.getPinnedWidth(),p=g.canvasWidth-R.columnEngine.getPinnedWidth(),_=Math.max(0,i-p),O=Math.max(0,Math.min(C,g.scrollY+l.deltaY)),U=Math.max(0,Math.min(_,g.scrollX+l.deltaX));g.update({scrollY:O,scrollX:U}),z(O),J(U),(re=N.current)==null||re.markAllDirty();const ee=A.current;ee.hasMore&&ee.onLoadMore&&!ee.loading&&C-O<Ct?He.current||(He.current=!0,ee.onLoadMore()):C-O>=Ct&&(He.current=!1)},[]);m.useEffect(()=>{const l=fe.current[2];if(l)return l.addEventListener("wheel",Ye,{passive:!1}),()=>l.removeEventListener("wheel",Ye)},[Ye]),m.useEffect(()=>{u||(He.current=!1)},[u]);const Tt=m.useCallback(l=>{var p,_,O;const g=de.current;if(!g)return;const R=fe.current[2];if(!R)return;const M=R.getBoundingClientRect(),v=l.clientX-M.left,C=l.clientY-M.top,i=Le(v,C,ie.current,g.treeEngine,g.columnEngine);Xe.current=i,De.current=(i==null?void 0:i.rowId)??null,ze((i==null?void 0:i.rowId)??null),(p=N.current)==null||p.markDirty("overlay"),(O=(_=A.current).onCellHover)==null||O.call(_,(i==null?void 0:i.rowId)??null,(i==null?void 0:i.colId)??null,l.nativeEvent)},[]),Ve=m.useCallback(l=>{D(g=>{var M,v;const R=new Set(g);return R.has(l)?(R.delete(l),we(`Row ${l} collapsed`)):(R.add(l),we(`Row ${l} expanded`)),(v=(M=A.current).onExpandChange)==null||v.call(M,[...R]),R})},[]),Mt=m.useCallback(l=>{var Re,Se,ke,_e,Te;const g=de.current;if(!g)return;const R=fe.current[2];if(!R)return;const M=R.getBoundingClientRect(),v=l.clientX-M.left,C=l.clientY-M.top,i=Le(v,C,ie.current,g.treeEngine,g.columnEngine);if(!i)return;const{treeEngine:p,columnEngine:_,selectionEngine:O}=g,U=_.getPinnedColumns();if(U.length>0){const ae=U[0].id;if(p.hasChildren(i.rowId)&&i.colId===ae){A.current.onTreeToggle?A.current.onTreeToggle(i.rowId):Ve(i.rowId);return}}const ee=p.flattenedRows.map(ae=>ae.id),re=_.getVisibleColumns().map(ae=>ae.id);l.ctrlKey||l.metaKey?O.ctrlClick(i.rowId,i.colId):l.shiftKey?O.shiftClick(i.rowId,i.colId,ee,re):O.click(i.rowId,i.colId,ee,re),Ne.current={rowId:i.rowId,colId:i.colId},(Se=(Re=A.current).onSelectionChange)==null||Se.call(Re,O.getSelection()),(_e=(ke=A.current).onCellClick)==null||_e.call(ke,i.rowId,i.colId,l.nativeEvent),(Te=N.current)==null||Te.markDirty("overlay");const be=String(i.rowId),ne=_.getVisibleColumns().find(ae=>ae.id===i.colId),Pe=(ne==null?void 0:ne.title)??i.colId;we(`Row ${be}, Column ${Pe} selected`)},[Ve]),Wt=m.useCallback(l=>{var p,_;const g=de.current;if(!g)return;const R=fe.current[2];if(!R)return;const M=R.getBoundingClientRect(),v=l.clientX-M.left,C=l.clientY-M.top,i=Le(v,C,ie.current,g.treeEngine,g.columnEngine);i&&((_=(p=A.current).onCellDoubleClick)==null||_.call(p,i.rowId,i.colId,l.nativeEvent))},[]),Dt=m.useCallback(l=>{var p,_;l.preventDefault();const g=de.current;if(!g)return;const R=fe.current[2];if(!R)return;const M=R.getBoundingClientRect(),v=l.clientX-M.left,C=l.clientY-M.top,i=Le(v,C,ie.current,g.treeEngine,g.columnEngine);i&&((_=(p=A.current).onCellContextMenu)==null||_.call(p,i.rowId,i.colId,l.nativeEvent))},[]),Xt=m.useCallback(()=>{var l,g,R;Xe.current=null,De.current=null,ze(null),(l=N.current)==null||l.markDirty("overlay"),(R=(g=A.current).onCellHover)==null||R.call(g,null,null,new PointerEvent("pointerleave"))},[]),Ht=m.useCallback((l,g)=>{var R,M;(M=(R=A.current).onColumnHeaderClick)==null||M.call(R,l,g.nativeEvent)},[]),Pt=m.useCallback((l,g)=>{var M,v,C,i;Z(p=>p.map(O=>{if(O.id!==l)return O;const U=O.minWidth??30;return{...O,width:Math.max(U,g)}}));const R=(M=de.current)==null?void 0:M.columnEngine;if(R){const p=R.getColumnX(l),_=R.getVisibleColumns().find(O=>O.id===l);if(_){const U=_.pinned==="left"?p+Math.max(_.minWidth??30,g):p+Math.max(_.minWidth??30,g)-ie.current.scrollX;We({colId:l,x:U})}}(C=(v=A.current).onColumnResize)==null||C.call(v,l,g),(i=N.current)==null||i.markAllDirty()},[]);m.useEffect(()=>{const l=()=>{We(null)};return document.addEventListener("mouseup",l),()=>document.removeEventListener("mouseup",l)},[]);const Et=m.useCallback((l,g)=>{var R,M,v;Z(C=>{const i=[...C],p=i.findIndex(U=>U.id===l);if(p===-1)return C;const[_]=i.splice(p,1),O=Math.max(0,Math.min(g,i.length));return i.splice(O,0,_),i}),(M=(R=A.current).onColumnReorder)==null||M.call(R,l,g),(v=N.current)==null||v.markAllDirty()},[]),Ot=m.useCallback((l,g)=>{var R,M,v;Z(C=>C.map(i=>i.id===l?{...i,hidden:!g}:i)),(M=(R=A.current).onColumnVisibilityChange)==null||M.call(R,l,g),(v=N.current)==null||v.markAllDirty()},[]),Lt=m.useCallback(l=>{var re,be,ne,Pe,Re,Se,ke,_e,Te,ae,Ge,et,tt,nt,ot,lt,st,it,rt,ct,dt,at,ht,ft;const g=de.current;if(!g)return;const{treeEngine:R,columnEngine:M,selectionEngine:v}=g,C=ie.current,i=R.flattenedRows.map(q=>q.id),p=M.getVisibleColumns().map(q=>q.id);if(i.length===0||p.length===0)return;const _=v.getSelection().anchor,O=l.ctrlKey||l.metaKey,U=(q,te)=>{const oe=i.length*C.rowHeight,ue=M.getTotalWidth(),ge=Math.max(0,oe-C.canvasHeight),Ee=Math.max(0,ue-C.canvasWidth),Ce=q*C.rowHeight,ve=Ce+C.rowHeight;let ce=C.scrollY;Ce<C.scrollY?ce=Ce:ve>C.scrollY+C.canvasHeight&&(ce=ve-C.canvasHeight),ce=Math.max(0,Math.min(ge,ce));const pe=M.getVisibleColumns()[te],Me=pe?M.getColumnX(pe.id):0,Oe=Me+((pe==null?void 0:pe.width)??0);let he=C.scrollX;Me<C.scrollX?he=Me:Oe>C.scrollX+C.canvasWidth&&(he=Oe-C.canvasWidth),he=Math.max(0,Math.min(Ee,he)),(ce!==C.scrollY||he!==C.scrollX)&&(C.update({scrollY:ce,scrollX:he}),z(ce),J(he))},ee=(q,te,oe)=>{var ut,gt,pt;let ue=_?i.indexOf(_.rowId):-1,ge=_?p.indexOf(_.colId):-1;ue===-1&&(ue=0),ge===-1&&(ge=0);const Ee=Math.max(0,Math.min(i.length-1,ue+q)),Ce=Math.max(0,Math.min(p.length-1,ge+te)),ve=i[Ee],ce=p[Ce];oe?v.shiftClick(ve,ce,i,p):v.click(ve,ce,i,p),(gt=(ut=A.current).onSelectionChange)==null||gt.call(ut,v.getSelection()),(pt=N.current)==null||pt.markDirty("overlay"),U(Ee,Ce);const pe=M.getVisibleColumns()[Ce],Me=String(ve),Oe=(pe==null?void 0:pe.title)??String(ce),he=v.getSelection().cells.size;he>1?we(`${he} cells selected`):we(`Row ${Me}, Column ${Oe} selected`)};switch(l.key){case"ArrowUp":l.preventDefault(),ee(-1,0,l.shiftKey);break;case"ArrowDown":l.preventDefault(),ee(1,0,l.shiftKey);break;case"ArrowLeft":l.preventDefault(),ee(0,-1,l.shiftKey);break;case"ArrowRight":l.preventDefault(),ee(0,1,l.shiftKey);break;case"Tab":{if(l.preventDefault(),l.shiftKey){const q=_?i.indexOf(_.rowId):0;if((_?p.indexOf(_.colId):0)>0)ee(0,-1,!1);else{const oe=Math.max(0,(q===-1?0:q)-1),ue=i[oe],ge=p[p.length-1];v.click(ue,ge,i,p),(Re=(Pe=A.current).onSelectionChange)==null||Re.call(Pe,v.getSelection()),(Se=N.current)==null||Se.markDirty("overlay"),U(oe,p.length-1)}}else{const q=_?i.indexOf(_.rowId):0;if((_?p.indexOf(_.colId):-1)<p.length-1)ee(0,1,!1);else{const oe=Math.min(i.length-1,(q===-1?0:q)+1),ue=i[oe],ge=p[0];v.click(ue,ge,i,p),(be=(re=A.current).onSelectionChange)==null||be.call(re,v.getSelection()),(ne=N.current)==null||ne.markDirty("overlay"),U(oe,0)}}break}case"Escape":l.preventDefault(),v.clear(),(_e=(ke=A.current).onSelectionChange)==null||_e.call(ke,v.getSelection()),(Te=N.current)==null||Te.markDirty("overlay"),we("Selection cleared");break;case"a":case"A":if(O){l.preventDefault();for(const q of i)for(const te of p)v.ctrlClick(q,te);(Ge=(ae=A.current).onSelectionChange)==null||Ge.call(ae,v.getSelection()),(et=N.current)==null||et.markDirty("overlay"),we(`${i.length*p.length} cells selected`)}break;case"Home":{l.preventDefault();const q=_?i.indexOf(_.rowId):0;if(O)v.click(i[0],p[0],i,p),(nt=(tt=A.current).onSelectionChange)==null||nt.call(tt,v.getSelection()),(ot=N.current)==null||ot.markDirty("overlay"),U(0,0);else{const te=q===-1?0:q;v.click(i[te],p[0],i,p),(st=(lt=A.current).onSelectionChange)==null||st.call(lt,v.getSelection()),(it=N.current)==null||it.markDirty("overlay"),U(te,0)}break}case"End":{l.preventDefault();const q=_?i.indexOf(_.rowId):0;if(O){const te=i.length-1,oe=p.length-1;v.click(i[te],p[oe],i,p),(ct=(rt=A.current).onSelectionChange)==null||ct.call(rt,v.getSelection()),(dt=N.current)==null||dt.markDirty("overlay"),U(te,oe)}else{const te=q===-1?0:q,oe=p.length-1;v.click(i[te],p[oe],i,p),(ht=(at=A.current).onSelectionChange)==null||ht.call(at,v.getSelection()),(ft=N.current)==null||ft.markDirty("overlay"),U(te,oe)}break}}},[]);m.useImperativeHandle(o,()=>({captureToCanvas(l){const g=de.current;if(!g)throw new Error("CanvasGrid: engines not initialized");const{treeEngine:R,columnEngine:M,selectionEngine:v}=g,C=A.current,i=C.rowHeight??bt,p=C.sidebarWidth??wt;return Zt({treeEngine:R,columnEngine:M,selectionEngine:v,columnGroups:C.columnGroups,rowHeight:i,sidebarWidth:p,gridLayerConfig:{rowStyle:C.rowStyle},cellLayerConfig:{cellRenderer:C.cellRenderer,getCellData:C.getCellData,hoveredCell:null},scale:(l==null?void 0:l.scale)??2})}}),[]);const jt=d!=null&&d.length>0,Ze=vt(L??"horizontal",jt),zt=Ie+se.getTotalWidth()+2,At=Ze+ye.flattenedRows.length*y+2;return T.jsxs("div",{ref:qe,tabIndex:0,role:"grid","aria-label":K,"aria-rowcount":ye.flattenedRows.length,"aria-colcount":se.getVisibleColumns().length,onKeyDown:Lt,style:{display:"flex",flexDirection:"column",width:zt,height:At,maxWidth:"100%",maxHeight:"100%",overflow:"hidden",fontFamily:"system-ui, sans-serif",outline:"none",border:"1px solid #d1d5db",borderRadius:4},children:[T.jsxs("div",{style:{display:"flex",flexDirection:"row",height:Ze,flexShrink:0},children:[!Ae&&T.jsx("div",{style:{width:Ie,flexShrink:0,borderRight:"1px solid #e5e7eb",borderBottom:"1px solid #e5e7eb",backgroundColor:"#f9fafb",display:"flex",flexDirection:"column",overflow:"hidden"},children:e.sidebarHeaderRenderer?T.jsx("div",{style:{width:"100%",flex:1},children:e.sidebarHeaderRenderer()}):B?T.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",flex:1},children:T.jsx(qt,{columnEngine:se,onColumnVisibilityChange:Ot})}):null}),T.jsx(Nt,{columnEngine:se,columnGroups:d,scrollX:V,orientation:L,onColumnClick:Y?Ht:void 0,onColumnResize:e.onColumnResize!==void 0?Pt:void 0,onColumnReorder:e.onColumnReorder!==void 0?Et:void 0,columnHeaderRenderer:e.columnHeaderRenderer})]}),T.jsxs("div",{style:{display:"flex",flexDirection:"row",flex:1,overflow:"hidden"},children:[!Ae&&T.jsx(Kt,{treeEngine:ye,width:Ie,rowHeight:y,scrollTop:E,viewportHeight:ie.current.canvasHeight||600,sidebarRenderer:f,onToggle:Ve,onRowClick:e.onRowHeaderClick?(l,g)=>e.onRowHeaderClick(l,g.nativeEvent):void 0,hoveredRowId:_t,onRowHover:l=>{var g;De.current=l,ze(l),(g=N.current)==null||g.markDirty("overlay")}}),T.jsxs("div",{style:{flex:1,position:"relative",overflow:"hidden"},children:[T.jsx("canvas",{ref:l=>{l&&(fe.current[0]=l)},style:{position:"absolute",top:0,left:0,zIndex:0}}),T.jsx("canvas",{ref:l=>{l&&(fe.current[1]=l)},style:{position:"absolute",top:0,left:0,zIndex:1}}),T.jsx("canvas",{ref:l=>{l&&(fe.current[2]=l)},style:{position:"absolute",top:0,left:0,zIndex:2},onPointerMove:Tt,onPointerDown:Mt,onDoubleClick:Wt,onContextMenu:Dt,onPointerLeave:Xt})]})]}),I!=null&&F!=null&&T.jsx(Jt,{content:I,position:F}),T.jsx(Qt,{message:kt})]})}));exports.CanvasGrid=tn;exports.ColumnEngine=me;exports.SelectionEngine=yt;exports.TreeEngine=xt;exports.ViewState=Ue;exports.createDrawHelpers=Rt;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react/jsx-runtime"),g=require("react"),pn=require("react-dom");class pt{constructor(e){this.scrollX=0,this.scrollY=0,this.canvasWidth=0,this.canvasHeight=0,this.rowHeight=32,this.pinnedWidth=0,this._columnOffsets=[],this._columnWidths=[],this._pinnedOffsets=[],this._pinnedWidths=[],this._scrollableOffsets=[],this._scrollableWidths=[],e&&this.update(e)}update(e){e.scrollX!==void 0&&(this.scrollX=e.scrollX),e.scrollY!==void 0&&(this.scrollY=e.scrollY),e.canvasWidth!==void 0&&(this.canvasWidth=e.canvasWidth),e.canvasHeight!==void 0&&(this.canvasHeight=e.canvasHeight),e.rowHeight!==void 0&&(this.rowHeight=e.rowHeight)}setColumnOffsets(e){this._columnOffsets=e}setColumnWidths(e){this._columnWidths=e}setPinnedOffsets(e,o){this._pinnedOffsets=e,this._pinnedWidths=o}setScrollableOffsets(e,o){this._scrollableOffsets=e,this._scrollableWidths=o}rowToY(e){return e*this.rowHeight-this.scrollY}yToRow(e){return Math.floor((e+this.scrollY)/this.rowHeight)}colToX(e,o){return o===!0?this._pinnedOffsets[e]??0:o===!1?(this._scrollableOffsets[e]??0)-this.scrollX+this.pinnedWidth:(this._columnOffsets[e]??0)-this.scrollX}xToColPinned(e){for(let n=0;n<this._pinnedOffsets.length;n++){const r=this._pinnedOffsets[n],c=this._pinnedWidths[n]??0;if(e>=r&&e<r+c)return{index:n,pinned:!0}}const o=e-this.pinnedWidth+this.scrollX;for(let n=0;n<this._scrollableOffsets.length;n++){const r=this._scrollableOffsets[n],c=this._scrollableWidths[n]??0;if(o>=r&&o<r+c)return{index:n,pinned:!1}}return null}xToCol(e){const o=e+this.scrollX;for(let n=0;n<this._columnOffsets.length;n++){const r=this._columnOffsets[n],c=this._columnWidths[n]??0;if(o>=r&&o<r+c)return n}return-1}}const Vt=["grid","cells","overlay"];class mn{constructor(e){this._dirty={grid:!1,cells:!1,overlay:!1},this._rafId=null,this._drawFn=e}markDirty(e){this._dirty[e]=!0,this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}markAllDirty(){for(const e of Vt)this._dirty[e]=!0;this._rafId===null&&(this._rafId=requestAnimationFrame(()=>this._flush()))}destroy(){this._rafId!==null&&(cancelAnimationFrame(this._rafId),this._rafId=null)}_flush(){this._rafId=null;for(const e of Vt)this._dirty[e]&&(this._dirty[e]=!1,this._drawFn(e))}}class Kt{constructor(e,o){this._rows=e,this._expandedRows=o,this._depthMap=new Map,this._childrenMap=new Map,this._buildMaps(),this._flattenedRows=this._buildFlattenedRows()}_buildMaps(){for(const n of this._rows)this._childrenMap.has(n.id)||this._childrenMap.set(n.id,[]),n.parentId!==void 0&&(this._childrenMap.has(n.parentId)||this._childrenMap.set(n.parentId,[]),this._childrenMap.get(n.parentId).push(n));const o=this._rows.filter(n=>n.parentId===void 0).map(n=>({row:n,depth:0}));for(;o.length>0;){const{row:n,depth:r}=o.pop();this._depthMap.set(n.id,r);const c=this._childrenMap.get(n.id)??[];for(const a of c)o.push({row:a,depth:r+1})}}_buildFlattenedRows(){const e=[],o=this._rows.filter(r=>r.parentId===void 0),n=r=>{if(e.push(r),this._expandedRows.has(r.id)){const c=this._childrenMap.get(r.id)??[];for(const a of c)n(a)}};for(const r of o)n(r);return e}get flattenedRows(){return this._flattenedRows}getDepth(e){return this._depthMap.get(e)??0}hasChildren(e){const o=this._childrenMap.get(e);return o!==void 0&&o.length>0}isExpanded(e){return this._expandedRows.has(e)}toggle(e){const o=new Set(this._expandedRows);return o.has(e)?o.delete(e):o.add(e),o}getVisibleRange(e,o,n){if(n<=0||this._flattenedRows.length===0)return{startIndex:0,endIndex:0};const r=Math.max(0,Math.floor(e/n)),c=Math.min(this._flattenedRows.length-1,Math.ceil((e+o)/n)-1);return{startIndex:r,endIndex:c}}}const wn=30;class Te{constructor(e){this._columns=e}getVisibleColumns(){return this._columns.filter(e=>!e.hidden)}getPinnedColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned==="left")}getScrollableColumns(){return this._columns.filter(e=>!e.hidden&&e.pinned!=="left")}getPinnedWidth(){return this.getPinnedColumns().reduce((e,o)=>e+o.width,0)}getAllColumns(){return[...this._columns]}getTotalWidth(){return this.getVisibleColumns().reduce((e,o)=>e+o.width,0)}getColumnX(e){const o=this.getPinnedColumns(),n=this.getScrollableColumns();let r=0;for(const c of o){if(c.id===e)return r;r+=c.width}for(const c of n){if(c.id===e)return r;r+=c.width}return-1}getColumnAtX(e){let o=0;for(const n of this.getPinnedColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}for(const n of this.getScrollableColumns()){if(e>=o&&e<o+n.width)return n.id;o+=n.width}return null}getVisibleRange(e,o){const n=this.getScrollableColumns();if(n.length===0)return{startIndex:0,endIndex:0};const r=this.getPinnedWidth(),c=o-r;let a=0,f=0,u=n.length-1;for(let M=0;M<n.length;M++)a+n[M].width<=e&&(f=M+1),a<e+c&&(u=M),a+=n[M].width;return f=Math.min(f,n.length-1),u=Math.min(u,n.length-1),{startIndex:f,endIndex:u}}getPinnedRange(){const e=this.getPinnedColumns();return e.length===0?{startIndex:0,endIndex:-1}:{startIndex:0,endIndex:e.length-1}}resize(e,o){const n=this._columns.map(r=>{if(r.id!==e)return r;const c=r.minWidth??wn;return{...r,width:Math.max(c,o)}});return new Te(n)}reorder(e,o){const n=[...this._columns],r=n.findIndex(f=>f.id===e);if(r===-1)return new Te(n);const[c]=n.splice(r,1),a=Math.max(0,Math.min(o,n.length));return n.splice(a,0,c),new Te(n)}hide(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!0}:n);return new Te(o)}show(e){const o=this._columns.map(n=>n.id===e?{...n,hidden:!1}:n);return new Te(o)}}function Pe(t,e){return`${t}|${e}`}function ft(){return{cells:new Set,rows:new Set,columns:new Set,anchor:void 0}}class Ut{constructor(e){e?this._selection={cells:new Set(e.cells),rows:new Set(e.rows),columns:new Set(e.columns),anchor:e.anchor}:this._selection=ft()}click(e,o,n,r){this._selection=ft(),this._selection.cells.add(Pe(e,o)),this._selection.anchor={rowId:e,colId:o}}ctrlClick(e,o){const n=Pe(e,o);this._selection.cells.has(n)?this._selection.cells.delete(n):this._selection.cells.add(n),this._selection.anchor={rowId:e,colId:o}}shiftClick(e,o,n,r){const c=this._selection.anchor;if(!c){this.click(e,o,n,r);return}const a=n.indexOf(c.rowId),f=n.indexOf(e),u=r.indexOf(c.colId),M=r.indexOf(o),H=Math.min(a,f),O=Math.max(a,f),I=Math.min(u,M),D=Math.max(u,M),X=new Set;for(let B=H;B<=O;B++)for(let L=I;L<=D;L++)X.add(Pe(n[B],r[L]));this._selection.cells=X,this._selection.rows=new Set,this._selection.columns=new Set}selectRow(e,o){for(const n of o)this._selection.cells.add(Pe(e,n));this._selection.rows.add(e),this._selection.anchor={rowId:e,colId:o[0]??""}}selectColumn(e,o){for(const n of o)this._selection.cells.add(Pe(n,e));this._selection.columns.add(e),this._selection.anchor={rowId:o[0]??"",colId:e}}clear(){this._selection=ft()}isSelected(e,o){return this._selection.cells.has(Pe(e,o))}isRowSelected(e){return this._selection.rows.has(e)}isColumnSelected(e){return this._selection.columns.has(e)}getSelection(){return{cells:new Set(this._selection.cells),rows:new Set(this._selection.rows),columns:new Set(this._selection.columns),anchor:this._selection.anchor}}}function ot(t,e,o,n,r){const c=o.yToRow(e),a=n.flattenedRows;if(c<0||c>=a.length)return null;const f=r.getPinnedColumns(),u=r.getPinnedWidth();if(f.length>0){if(t<u){let D=0;for(const X of f){if(t>=D&&t<D+X.width)return{rowId:a[c].id,colId:X.id};D+=X.width}return null}const H=t-u+o.scrollX,O=r.getScrollableColumns();let I=0;for(const D of O){if(H>=I&&H<I+D.width)return{rowId:a[c].id,colId:D.id};I+=D.width}return null}const M=r.getColumnAtX(t+o.scrollX);return M===null?null:{rowId:a[c].id,colId:M}}function bn(t,e,o){const n=window.devicePixelRatio||1;(t.width!==Math.round(e*n)||t.height!==Math.round(o*n))&&(t.width=Math.round(e*n),t.height=Math.round(o*n),t.style.width=`${e}px`,t.style.height=`${o}px`);const c=t.getContext("2d");return c.setTransform(n,0,0,n,0,0),c}function Nt(t,e,o,n,r={}){const{canvasWidth:c,canvasHeight:a,rowHeight:f}=e,{rowStyle:u}=r;t.clearRect(0,0,c,a),t.fillStyle="#f3f4f6",t.fillRect(0,0,c,a);const M=o.getVisibleRange(e.scrollY,a,f),H=o.flattenedRows,O=n.getPinnedColumns(),I=n.getScrollableColumns(),D=n.getPinnedWidth(),X=O.length>0;if(H.length===0||O.length===0&&I.length===0)return;const B=n.getTotalWidth(),L=Math.min(c,B-e.scrollX+(X?e.scrollX:0)),Y=X?Math.min(c,D+n.getScrollableColumns().reduce((S,b)=>S+b.width,0)-e.scrollX):L,Q=H.length*f,z=Math.min(a,Q-e.scrollY);for(let S=M.startIndex;S<=M.endIndex;S++){const b=H[S];if(!b)continue;const W=e.rowToY(S),m=o.getDepth(b.id);let v=S%2===0?"#ffffff":"#f9fafb";if(u){const T=u(b,m);T!=null&&T.backgroundColor&&(v=T.backgroundColor)}t.fillStyle=v,t.fillRect(0,W,Math.max(Y,D),f),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,W+f),t.lineTo(Math.max(Y,D),W+f),t.stroke()}if(z>0){const S=n.getVisibleRange(e.scrollX,c);t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let b=S.startIndex;b<=S.endIndex;b++){const W=I[b];if(!W)continue;const m=e.colToX(b,!1);t.beginPath(),t.moveTo(m+W.width,0),t.lineTo(m+W.width,z),t.stroke()}}if(X&&z>0){t.fillStyle="#ffffff",t.fillRect(0,0,D,a);for(let b=M.startIndex;b<=M.endIndex;b++){const W=H[b];if(!W)continue;const m=e.rowToY(b),v=o.getDepth(W.id);let T=b%2===0?"#ffffff":"#f9fafb";if(u){const k=u(W,v);k!=null&&k.backgroundColor&&(T=k.backgroundColor)}t.fillStyle=T,t.fillRect(0,m,D,f),t.strokeStyle="#d1d5db",t.lineWidth=.5,t.beginPath(),t.moveTo(0,m+f),t.lineTo(D,m+f),t.stroke()}const S=n.getPinnedRange();t.strokeStyle="#d1d5db",t.lineWidth=.5;for(let b=S.startIndex;b<=S.endIndex;b++){const W=O[b];if(!W)continue;const m=e.colToX(b,!0);t.beginPath(),t.moveTo(m+W.width,0),t.lineTo(m+W.width,z),t.stroke()}t.strokeStyle="#d1d5db",t.lineWidth=1,t.beginPath(),t.moveTo(D,0),t.lineTo(D,z),t.stroke()}}function qt(t){function e(a,f,u,M,H,O){t.fillStyle=O,t.beginPath(),t.roundRect(a,f,u,M,H),t.fill()}function o(a,f,u,M){t.fillText(a,f,u,M)}function n(a,f){if(t.measureText(a).width<=f)return a;let u=a;for(;u.length>0;)if(u=u.slice(0,-1),t.measureText(u+"…").width<=f)return u+"…";return"…"}function r(a,f,u,M,H){const X=t.measureText(u).width+16;return e(a,f,X,20,20/2,M),t.fillStyle=H,t.fillText(u,a+8,f+20/2),X}function c(a,f,u,M,H,O){e(a,f,u,M,M/2,"#d1d5db");const I=u*Math.max(0,Math.min(1,H));I>0&&e(a,f,I,M,M/2,O)}return{roundRect:e,fillText:o,truncateText:n,badge:r,progressBar:c}}function Zt(t,e,o,n,r,c){const{canvasWidth:a,canvasHeight:f,rowHeight:u}=e,{cellRenderer:M,getCellData:H,hoveredCell:O}=c;c.clearBackground!==!1&&t.clearRect(0,0,a,f);const I=o.getVisibleRange(e.scrollY,f,u),D=o.flattenedRows,X=n.getPinnedColumns(),B=n.getScrollableColumns(),L=X.length>0;if(D.length===0||X.length===0&&B.length===0)return;const Y=qt(t),Q=(S,b,W,m)=>{const v={x:W,y:m,width:b.width,height:u},T={rowId:S.id,colId:b.id,data:H(S.id,b.id)},k={selected:r.isSelected(S.id,b.id),hovered:O!=null&&O.rowId===S.id&&O.colId===b.id,rowSelected:r.isRowSelected(S.id),colSelected:r.isColumnSelected(b.id),filtered:!0,depth:o.getDepth(S.id),hasChildren:o.hasChildren(S.id),expanded:o.isExpanded(S.id)};t.save(),t.beginPath(),t.rect(W,m,b.width,u),t.clip(),M(t,T,v,k,Y),t.restore()},z=n.getVisibleRange(e.scrollX,a);for(let S=I.startIndex;S<=I.endIndex;S++){const b=D[S];if(!b)continue;const W=e.rowToY(S);for(let m=z.startIndex;m<=z.endIndex;m++){const v=B[m];if(!v)continue;const T=e.colToX(m,L?!1:void 0);Q(b,v,T,W)}}if(L){n.getPinnedWidth();const S=n.getPinnedRange();for(let b=I.startIndex;b<=I.endIndex;b++){const W=D[b];if(!W)continue;const m=e.rowToY(b);t.save(),t.fillStyle="#ffffff",t.restore();for(let v=S.startIndex;v<=S.endIndex;v++){const T=X[v];if(!T)continue;const k=e.colToX(v,!0);Q(W,T,k,m)}}}}function vn(t,e,o,n,r,c={}){const{canvasWidth:a,canvasHeight:f,rowHeight:u}=e,{hoveredCell:M,hoveredRowId:H,selectedCell:O,resizingColumn:I,loading:D}=c;t.clearRect(0,0,a,f);const X=o.getVisibleRange(e.scrollY,f,u),B=o.flattenedRows,L=n.getPinnedColumns(),Y=n.getScrollableColumns(),Q=L.length>0;if(n.getPinnedWidth(),B.length===0||L.length===0&&Y.length===0)return;const z=n.getVisibleRange(e.scrollX,a),S=n.getPinnedRange(),b=m=>{for(let v=z.startIndex;v<=z.endIndex;v++){const T=Y[v];if(!T)continue;const k=Q?e.colToX(v,!1):e.colToX(v);m(T,k)}if(Q)for(let v=S.startIndex;v<=S.endIndex;v++){const T=L[v];if(!T)continue;const k=e.colToX(v,!0);m(T,k)}};if(O){for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v||v.id!==O.rowId)continue;const T=e.rowToY(m);b((k,N)=>{t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(N,T,k.width,u)});break}b((m,v)=>{if(m.id===O.colId)for(let T=X.startIndex;T<=X.endIndex;T++){if(!B[T])continue;const N=e.rowToY(T);t.fillStyle="rgba(59,130,246,0.06)",t.fillRect(v,N,m.width,u)}})}const W=(m,v,T,k,N)=>{const i=m.width,oe=u,_e=r.isSelected(T.id,m.id),ye=r.isRowSelected(T.id),d=r.isColumnSelected(m.id),P=H!=null&&H===T.id,$=M!=null&&M.rowId===T.id&&M.colId===m.id;_e?(t.fillStyle="rgba(59,130,246,0.12)",t.fillRect(v,k,i,oe),t.strokeStyle="#3b82f6",t.lineWidth=2,t.strokeRect(v+1,k+1,i-2,oe-2)):ye||d||$?(t.fillStyle="rgba(59,130,246,0.08)",t.fillRect(v,k,i,oe)):P&&(t.fillStyle="rgba(59,130,246,0.04)",t.fillRect(v,k,i,oe))};for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v)continue;const T=e.rowToY(m);for(let k=z.startIndex;k<=z.endIndex;k++){const N=Y[k];if(!N)continue;const i=Q?e.colToX(k,!1):e.colToX(k);W(N,i,v,T)}}if(Q)for(let m=X.startIndex;m<=X.endIndex;m++){const v=B[m];if(!v)continue;const T=e.rowToY(m);for(let k=S.startIndex;k<=S.endIndex;k++){const N=L[k];if(!N)continue;const i=e.colToX(k,!0);W(N,i,v,T)}}if(I&&(t.save(),t.strokeStyle="#3b82f6",t.lineWidth=1,t.setLineDash([4,4]),t.beginPath(),t.moveTo(I.x,0),t.lineTo(I.x,f),t.stroke(),t.setLineDash([]),t.restore()),D){const v=f-32;t.fillStyle="#f3f4f6",t.fillRect(0,v,a,32),t.strokeStyle="#e5e7eb",t.lineWidth=1,t.beginPath(),t.moveTo(0,v),t.lineTo(a,v),t.stroke(),t.fillStyle="#6b7280",t.font="13px system-ui, sans-serif",t.textAlign="center",t.textBaseline="middle",t.fillText("Loading...",a/2,v+32/2)}}function Cn({treeEngine:t,width:e,rowHeight:o,scrollTop:n,viewportHeight:r,sidebarRenderer:c,onToggle:a,onRowClick:f,hoveredRowId:u,onRowHover:M}){const H=t.flattenedRows,{startIndex:O,endIndex:I}=t.getVisibleRange(n,r,o),D=[];for(let L=O;L<=I;L++){const Y=H[L];if(!Y)continue;const Q=t.getDepth(Y.id),z=t.hasChildren(Y.id),S=t.isExpanded(Y.id),b=L*o;let W;if(c)W=c(Y,Q,S,z);else{const v=Q*16+8;W=x.jsxs("div",{style:{display:"flex",alignItems:"center",height:"100%",paddingLeft:v,overflow:"hidden",whiteSpace:"nowrap",cursor:z?"pointer":"default",userSelect:"none"},onPointerDown:z?T=>{T.stopPropagation(),a(Y.id)}:void 0,children:[z?x.jsx("span",{style:{marginRight:4,flexShrink:0,fontSize:10,color:"#6b7280"},children:S?"▾":"▸"}):x.jsx("span",{style:{width:14,flexShrink:0}}),x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",fontSize:12},children:String(Y.title??Y.id)})]})}const m=u===Y.id;D.push(x.jsx("div",{role:"rowheader","aria-level":Q+1,"aria-expanded":z?S:void 0,style:{position:"absolute",top:b,left:0,width:"100%",height:o,boxSizing:"border-box",fontSize:13,color:"#374151",borderBottom:"1px solid #e5e7eb",cursor:f?"pointer":"default",backgroundColor:m?"rgba(59,130,246,0.04)":void 0},onPointerDown:f?v=>f(Y.id,v):void 0,onPointerEnter:()=>M==null?void 0:M(Y.id),onPointerLeave:()=>M==null?void 0:M(null),children:W},String(Y.id)))}const X=H.length*o,B=Math.min(r,X-n);return x.jsxs("div",{style:{width:e,height:r,overflow:"hidden",position:"relative",flexShrink:0,backgroundColor:"#ffffff"},children:[x.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:Math.max(0,B),backgroundColor:"#e5e7eb",zIndex:1}}),x.jsx("div",{style:{position:"absolute",top:0,left:0,width:"100%",height:X,transform:`translateY(-${n}px)`},children:D})]})}const ut=28,gt=28,xn=4;function yn(t){switch(t){case"vertical":return 120;default:return 32}}function Rn({active:t}){return x.jsx("svg",{width:13,height:13,viewBox:"0 0 24 24",fill:"none",stroke:t?"#2563eb":"#9ca3af",strokeWidth:2,strokeLinecap:"round",strokeLinejoin:"round","aria-hidden":"true",children:x.jsx("polygon",{points:"22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"})})}function In({columnEngine:t,columnGroups:e,scrollX:o,orientation:n="horizontal",columnHeaderHeight:r,onColumnClick:c,onColumnResize:a,onColumnReorder:f,onColumnFilterClick:u,columnHeaderRenderer:M}){const H=t.getPinnedColumns(),O=t.getScrollableColumns(),I=t.getVisibleColumns(),D=t.getPinnedWidth(),X=new Map;if(e)for(const d of e)X.set(d.id,d);const B=g.useRef(null),[L,Y]=g.useState(null),Q=g.useRef(null),z=g.useRef(!1),S=g.useRef(null),b=g.useRef(null);Q.current=L;const W=[...H,...O],m=[];if(e&&e.length>0){let d,P=0,$=0,V=!1;for(const G of W){const U=t.getColumnX(G.id),F=G.pinned==="left";if(G.group!==d){if(d!==void 0&&$>0){const le=X.get(d);le&&m.push({group:le,startX:P,width:$,pinned:V})}d=G.group,P=U,$=G.width,V=F}else $+=G.width}if(d!==void 0&&$>0){const G=X.get(d);G&&m.push({group:G,startX:P,width:$,pinned:V})}}const v=m.length>0,T=r??yn(n),k=v?ut+T:T,N=g.useCallback((d,P)=>{P.stopPropagation(),P.preventDefault();const $=I.find(V=>V.id===d);$&&(B.current={colId:d,startX:P.clientX,startWidth:$.width})},[I]);g.useEffect(()=>{const d=$=>{const V=B.current;if(!V)return;const G=$.clientX-V.startX,U=Math.max(V.startWidth+G,30);a==null||a(V.colId,U)},P=()=>{B.current=null};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",P),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",P)}},[a]);const i=g.useCallback(d=>{const P=b.current;if(!P)return{dropIndex:0};const $=P.getBoundingClientRect(),V=d-$.left+o;let G=0;for(let U=0;U<I.length;U++){const F=I[U],ge=t.getColumnX(F.id)+F.width/2;V>ge&&(G=U+1)}return{dropIndex:G}},[I,t,o]),oe=g.useCallback((d,P)=>{P.target.dataset.resizeHandle||f&&(P.preventDefault(),z.current=!1,S.current={colId:d,startX:P.clientX,currentX:P.clientX})},[f]);g.useEffect(()=>{if(!f)return;const d=V=>{const G=S.current;if(!G)return;const U=Math.abs(V.clientX-G.startX);if(!z.current&&U>=xn){z.current=!0;const F=I.find(ne=>ne.id===G.colId),le=t.getColumnX(G.colId),{dropIndex:ge}=i(V.clientX);Y({colId:G.colId,startX:G.startX,currentX:V.clientX,dropIndex:ge,ghostLeft:le-o,ghostWidth:(F==null?void 0:F.width)??80,ghostTitle:(F==null?void 0:F.title)??""});return}if(z.current){const{dropIndex:F}=i(V.clientX);Y(le=>le&&{...le,currentX:V.clientX,dropIndex:F})}},P=()=>{z.current&&Q.current&&f(Q.current.colId,Q.current.dropIndex),S.current=null,z.current=!1,Y(null)},$=V=>{V.key==="Escape"&&(S.current=null,z.current=!1,Y(null))};return document.addEventListener("mousemove",d),document.addEventListener("mouseup",P),document.addEventListener("keydown",$),()=>{document.removeEventListener("mousemove",d),document.removeEventListener("mouseup",P),document.removeEventListener("keydown",$)}},[f,I,t,o,i]);const _e=()=>{if(!L)return 0;const{dropIndex:d}=L;if(d>=I.length){const P=I[I.length-1];return t.getColumnX(P.id)+P.width-o}return t.getColumnX(I[d].id)-o},ye=(d,P,$)=>{const V=(L==null?void 0:L.colId)===d.id,U=d.pinned==="left"?"horizontal":n,F=M==null?void 0:M(d.id,d.title,{width:d.width,height:$,orientation:U}),le=d.filterable&&!!u;let ge;return F?ge=F:U==="vertical"?ge=x.jsx("div",{style:{writingMode:"vertical-rl",textOrientation:"mixed",transform:"rotate(180deg)",height:$-16,lineHeight:`${d.width}px`,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:d.title}):ge=x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},children:d.title}),x.jsxs("div",{role:"columnheader","aria-label":d.title,style:{position:"absolute",left:P,width:d.width,height:$,boxSizing:"border-box",borderRight:"1px solid #d1d5db",display:"flex",alignItems:U==="horizontal"?"center":"flex-end",justifyContent:U==="horizontal"?"flex-start":"center",paddingLeft:U==="horizontal"?8:0,paddingBottom:U!=="horizontal"?6:0,cursor:f?"grab":c?"pointer":"default",userSelect:"none",fontSize:13,fontWeight:500,color:"#6C737F",opacity:V?.4:1},onMouseDown:f?ne=>oe(d.id,ne):void 0,onPointerDown:c&&!f?ne=>c(d.id,ne):void 0,children:[ge,le&&x.jsx("button",{type:"button","aria-label":"Filter column","data-cg-filter-icon":d.id,style:{position:"absolute",top:2,right:5,zIndex:2,display:"flex",alignItems:"center",justifyContent:"center",padding:1,border:"none",borderRadius:3,background:"transparent",cursor:"pointer",lineHeight:0},onMouseDown:ne=>ne.stopPropagation(),onPointerDown:ne=>ne.stopPropagation(),onClick:ne=>{ne.stopPropagation(),u==null||u(d.id,ne.currentTarget.getBoundingClientRect())},children:x.jsx(Rn,{active:d.filterActive})}),a&&x.jsx("div",{"data-resize-handle":"true",style:{position:"absolute",right:0,top:0,width:4,height:"100%",cursor:"col-resize",zIndex:1},onMouseDown:ne=>N(d.id,ne)})]},d.id)};return x.jsxs("div",{ref:b,style:{flex:1,height:k,overflow:"hidden",position:"relative",backgroundColor:"#f9fafb"},children:[x.jsxs("div",{style:{position:"absolute",top:0,left:0,transform:`translateX(-${o}px)`,height:k},children:[x.jsx("div",{style:{position:"absolute",top:0,height:k},children:O.map(d=>{const P=t.getColumnX(d.id);return ye(d,P,k)})}),v&&x.jsx("div",{style:{position:"relative",height:ut,zIndex:1},children:m.filter(d=>!d.pinned).map(d=>x.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:ut,borderBottom:`1px solid ${d.group.color??"#3b82f6"}`,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))})]}),H.length>0&&x.jsxs("div",{style:{position:"absolute",top:0,left:0,width:D,height:k,backgroundColor:"#f9fafb",zIndex:2,display:"flex",flexDirection:"column"},children:[v&&x.jsx("div",{style:{position:"absolute",top:0,left:0,width:D,height:k-gt},children:m.filter(d=>d.pinned).map(d=>x.jsx("div",{style:{position:"absolute",left:d.startX,width:d.width,height:"100%",borderBottom:"1px solid #d1d5db",borderRight:"1px solid #d1d5db",display:"flex",alignItems:"center",justifyContent:"center",boxSizing:"border-box",fontSize:11,fontWeight:600,color:"#6b7280",overflow:"hidden",whiteSpace:"nowrap",backgroundColor:"#f9fafb"},children:d.group.title},d.group.id))}),x.jsx("div",{style:{position:"absolute",bottom:0,left:0,width:D,height:gt,borderTop:"1px solid #d1d5db"},children:H.map(d=>{const P=t.getColumnX(d.id);return ye(d,P,gt)})}),x.jsx("div",{style:{position:"absolute",top:0,right:0,width:1,height:"100%",backgroundColor:"#d1d5db",zIndex:3}})]}),x.jsx("div",{style:{position:"absolute",bottom:0,left:0,right:0,height:1,backgroundColor:"#d1d5db",zIndex:5}}),L&&x.jsx("div",{style:{position:"absolute",top:0,left:_e(),width:2,height:k,backgroundColor:"#3b82f6",pointerEvents:"none",zIndex:10}}),L&&x.jsx("div",{style:{position:"fixed",top:0,left:L.currentX-L.ghostWidth/2,width:L.ghostWidth,height:32,backgroundColor:"#dbeafe",border:"1px solid #3b82f6",borderRadius:4,display:"flex",alignItems:"center",paddingLeft:8,boxSizing:"border-box",fontSize:12,fontWeight:600,color:"#1d4ed8",opacity:.85,pointerEvents:"none",zIndex:9999,whiteSpace:"nowrap",overflow:"hidden"},children:L.ghostTitle})]})}function Sn({columnEngine:t,onColumnVisibilityChange:e}){const[o,n]=g.useState(!1),r=g.useRef(null),c=t.getAllColumns();return g.useEffect(()=>{if(!o)return;const a=u=>{r.current&&!r.current.contains(u.target)&&n(!1)},f=u=>{u.key==="Escape"&&n(!1)};return document.addEventListener("mousedown",a),document.addEventListener("keydown",f),()=>{document.removeEventListener("mousedown",a),document.removeEventListener("keydown",f)}},[o]),x.jsxs("div",{ref:r,style:{position:"relative",display:"inline-block"},children:[x.jsx("button",{onClick:()=>n(a=>!a),style:{display:"flex",alignItems:"center",gap:4,padding:"0 8px",height:28,fontSize:12,fontWeight:600,color:"#374151",backgroundColor:o?"#e5e7eb":"#f9fafb",border:"1px solid #d1d5db",borderRadius:4,cursor:"pointer",whiteSpace:"nowrap"},title:"Toggle column visibility",children:"Columns"}),o&&x.jsx("div",{style:{position:"absolute",top:"100%",left:0,marginTop:4,minWidth:160,backgroundColor:"#ffffff",border:"1px solid #d1d5db",borderRadius:6,boxShadow:"0 4px 12px rgba(0,0,0,0.12)",zIndex:1e3,padding:"4px 0"},children:c.map(a=>{const f=!a.hidden;return x.jsxs("label",{style:{display:"flex",alignItems:"center",gap:8,padding:"6px 12px",cursor:"pointer",fontSize:13,color:"#374151",userSelect:"none"},onMouseEnter:u=>{u.currentTarget.style.backgroundColor="#f3f4f6"},onMouseLeave:u=>{u.currentTarget.style.backgroundColor=""},children:[x.jsx("input",{type:"checkbox",checked:f,onChange:u=>{e(a.id,u.target.checked)},style:{cursor:"pointer",width:14,height:14}}),x.jsx("span",{style:{overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:a.title})]},a.id)})})]})}function kn({content:t,position:e}){return pn.createPortal(x.jsx("div",{style:{position:"fixed",left:e.left,top:e.top,zIndex:9999,pointerEvents:"none"},children:t}),document.body)}function Mn({message:t}){return x.jsx("div",{role:"status","aria-live":"polite","aria-atomic":"true",style:{position:"absolute",width:1,height:1,padding:0,margin:-1,overflow:"hidden",clip:"rect(0,0,0,0)",whiteSpace:"nowrap",borderWidth:0},children:t})}const Ke=28,_n=32,Tn=120;function Wn(t){var Le,Oe;const{treeEngine:e,columnEngine:o,columnGroups:n,rowHeight:r,sidebarWidth:c,gridLayerConfig:a,cellLayerConfig:f,headerOrientation:u="horizontal",scale:M=2}=t,H=n!=null&&n.length>0,O=t.columnHeaderHeight??(u==="vertical"?Tn:_n),I=H?Ke+O:O,D=o.getVisibleColumns(),X=e.flattenedRows,B=Math.max(0,((Le=t.rowRange)==null?void 0:Le.start)??0),L=Math.min(X.length,((Oe=t.rowRange)==null?void 0:Oe.end)??X.length),Y=Math.max(0,L-B),Q=B*r,z=o.getTotalWidth(),S=Y*r,b=c+z,W=I+S,m=16384,T=Math.min(m/b,m/W,Math.sqrt(256e6/(b*W)));let k=Math.min(M,T);(!Number.isFinite(k)||k<=0)&&(k=1);const N=document.createElement("canvas");N.width=Math.round(b*k),N.height=Math.round(W*k),N.style.width=`${b}px`,N.style.height=`${W}px`;const i=N.getContext("2d");i.setTransform(k,0,0,k,0,0),i.fillStyle="#ffffff",i.fillRect(0,0,b,W);const oe=new pt({scrollX:0,scrollY:Q,canvasWidth:z,canvasHeight:S,rowHeight:r}),_e=[],ye=[];let d=0;for(const j of D)_e.push(d),ye.push(j.width),d+=j.width;oe.setColumnOffsets(_e),oe.setColumnWidths(ye);const P=o.getPinnedColumns(),$=o.getScrollableColumns(),V=[],G=[];let U=0;for(const j of P)V.push(U),G.push(j.width),U+=j.width;const F=[],le=[];let ge=0;for(const j of $)F.push(ge),le.push(j.width),ge+=j.width;if(oe.pinnedWidth=o.getPinnedWidth(),oe.setPinnedOffsets(V,G),oe.setScrollableOffsets(F,le),i.save(),i.translate(c,I),Nt(i,oe,e,o,a),Zt(i,oe,e,o,t.selectionEngine,{...f,clearBackground:!1}),i.restore(),c>0){i.save(),i.translate(0,I);const j="#ffffff";i.fillStyle=j,i.fillRect(0,0,c,S),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(c,0),i.lineTo(c,S),i.stroke(),i.font="13px system-ui, sans-serif",i.textBaseline="middle",i.fillStyle="#374151";for(let se=B;se<L;se++){const Z=X[se];if(!Z)continue;const be=e.getDepth(Z.id),ee=(se-B)*r,ie=be*16+8,Re=String(Z.title??Z.id),pe=c-ie-8;i.fillStyle=se%2===0?"#ffffff":"#f9fafb",i.fillRect(0,ee,c,r),i.strokeStyle="#e5e7eb",i.lineWidth=.5,i.beginPath(),i.moveTo(0,ee+r),i.lineTo(c,ee+r),i.stroke(),i.fillStyle="#374151";let ae=Re;if(i.measureText(ae).width>pe&&pe>0){for(;ae.length>0&&i.measureText(ae+"…").width>pe;)ae=ae.slice(0,-1);ae=ae+"…"}i.fillText(ae,ie,ee+r/2)}i.restore()}if(i.save(),i.translate(0,0),i.fillStyle="#f9fafb",i.fillRect(0,0,b,I),i.fillStyle="#d1d5db",i.fillRect(0,0,b,1),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(0,I),i.lineTo(b,I),i.stroke(),i.fillStyle="#f9fafb",i.fillRect(0,0,c,I),i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(c,0),i.lineTo(c,I),i.stroke(),H&&n){const j=new Map;for(const ie of n)j.set(ie.id,ie);let se,Z=0,be=0;const ee=(ie,Re,pe)=>{if(ie===void 0||pe<=0)return;const ae=j.get(ie);if(!ae)return;const He=c+Re;i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(He,Ke),i.lineTo(He+pe,Ke),i.stroke(),i.font="600 11px system-ui, sans-serif",i.fillStyle="#6b7280",i.textBaseline="middle",i.textAlign="left",i.fillText(ae.title,He+8,Ke/2)};for(const ie of D){const Re=o.getColumnX(ie.id);ie.group!==se?(ee(se,Z,be),se=ie.group,Z=Re,be=ie.width):be+=ie.width}ee(se,Z,be)}const ne=H?Ke:0,Ue=(j,se)=>{if(se<=0||i.measureText(j).width<=se)return j;let Z=j;for(;Z.length>0&&i.measureText(Z+"…").width>se;)Z=Z.slice(0,-1);return Z+"…"};for(const j of D){const se=o.getColumnX(j.id),Z=c+se,be=u==="vertical"&&j.pinned!=="left";if(i.strokeStyle="#d1d5db",i.lineWidth=1,i.beginPath(),i.moveTo(Z+j.width,ne),i.lineTo(Z+j.width,I),i.stroke(),!!j.title)if(i.fillStyle="#6b7280",i.textBaseline="middle",be){const ee=O-12;i.save(),i.translate(Z+j.width/2+4,I-6),i.rotate(-Math.PI/2),i.font="11px system-ui, sans-serif",i.textAlign="left",i.fillText(Ue(j.title,ee),0,0,ee),i.restore()}else{i.font="600 12px system-ui, sans-serif",i.textAlign="left";const ee=j.width-16,ie=u==="vertical"?I-14:ne+O/2;i.fillText(Ue(j.title,ee),Z+8,ie,ee)}}return i.restore(),i.fillStyle="#d1d5db",i.fillRect(0,0,b,1),i.fillRect(0,W-1,b,1),i.fillRect(0,0,1,W),i.fillRect(b-1,0,1,W),N}const Bt=180,Gt=32,Hn=28,$t=200;function Ft(t,e,o){const n=o??(t==="vertical"?120:32);return e?Hn+n:n}const Dn=g.memo(g.forwardRef(function(e,o){const{rows:n,columns:r,columnGroups:c,getCellData:a,cellRenderer:f,sidebarRenderer:u,sidebarWidth:M=Bt,rowHeight:H=Gt,headerOrientation:O="horizontal",columnHeaderHeight:I,maxHeight:D,rowStyle:X,selection:B,onSelectionChange:L,onCellClick:Y,onCellDoubleClick:Q,onCellContextMenu:z,onCellHover:S,onColumnHeaderClick:b,onTreeToggle:W,defaultExpandedRows:m,onExpandChange:v,onLoadMore:T,hasMore:k,loading:N,hoverContent:i,hoverPosition:oe,showColumnVisibilityMenu:_e,ariaLabel:ye="Data grid"}=e,[d,P]=g.useState(()=>new Set(m??[])),[$,V]=g.useState(0),[G,U]=g.useState(0),[F,le]=g.useState({width:0,height:0}),[ge,ne]=g.useState(!1),[Ue,Le]=g.useState(!1),[Oe,j]=g.useState(r),[se,Z]=g.useState(null),[be,ee]=g.useState(""),[ie,Re]=g.useState(null),pe=g.useRef(null),ae=g.useRef(null);g.useEffect(()=>{j(r)},[r]);const He=g.useRef(null),Ie=g.useRef([]),he=g.useRef(new pt({rowHeight:H})),q=g.useRef(null),A=g.useRef(e),me=g.useRef(null),Ne=g.useRef(null),qe=g.useRef(!1);A.current=e;const De=g.useMemo(()=>new Kt(n,d),[n,d]),fe=g.useMemo(()=>new Te(Oe),[Oe]),mt=g.useMemo(()=>new Ut(B),[B]);me.current={treeEngine:De,columnEngine:fe,selectionEngine:mt};const st=fe.getPinnedColumns().length>0,je=st?0:M,wt=g.useCallback(s=>{const h=me.current;if(!h)return;const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=he.current,l=s==="grid"?0:s==="cells"?1:2,p=Ie.current[l];if(!p)return;const R=bn(p,w.canvasWidth,w.canvasHeight);s==="grid"?Nt(R,w,y,_,{rowStyle:A.current.rowStyle}):s==="cells"?Zt(R,w,y,_,C,{cellRenderer:A.current.cellRenderer,getCellData:A.current.getCellData,hoveredCell:Ne.current}):vn(R,w,y,_,C,{hoveredCell:Ne.current,hoveredRowId:pe.current,selectedCell:ae.current,resizingColumn:se??void 0,loading:A.current.loading})},[se]);g.useEffect(()=>{const s=fe.getPinnedColumns(),h=fe.getScrollableColumns(),y=fe.getPinnedWidth(),_=[],C=[];let w=0;for(const ce of s)_.push(w),C.push(ce.width),w+=ce.width;const l=[],p=[];let R=0;for(const ce of h)l.push(R),p.push(ce.width),R+=ce.width;const E=he.current;E.pinnedWidth=y,E.setPinnedOffsets(_,C),E.setScrollableOffsets(l,p);const K=fe.getVisibleColumns(),J=[],ue=[];let ve=0;for(const ce of K)J.push(ve),ue.push(ce.width),ve+=ce.width;E.setColumnOffsets(J),E.setColumnWidths(ue),E.update({rowHeight:H})},[fe,H]),g.useEffect(()=>{const s=new mn(wt);return q.current=s,s.markAllDirty(),()=>{s.destroy(),q.current=null}},[wt]),g.useEffect(()=>{const s=He.current;if(!s)return;const h=new ResizeObserver(y=>{var _,C;for(const w of y){const{width:l,height:p}=w.contentRect,R=l-je,E=Ft(A.current.headerOrientation??"horizontal",(((_=A.current.columnGroups)==null?void 0:_.length)??0)>0,A.current.columnHeaderHeight),K=p-E;he.current.update({canvasWidth:Math.max(0,R),canvasHeight:Math.max(0,K)}),le({width:Math.max(0,R),height:Math.max(0,K)}),(C=q.current)==null||C.markAllDirty()}});return h.observe(s),()=>h.disconnect()},[je]),g.useEffect(()=>{var s;(s=q.current)==null||s.markAllDirty()},[De,fe,mt]),g.useEffect(()=>{var s;(s=q.current)==null||s.markDirty("overlay")},[N]);const it=g.useCallback(s=>{var ue;s.preventDefault();const h=he.current,y=me.current;if(!y)return;const _=y.treeEngine.flattenedRows.length*h.rowHeight,C=y.columnEngine.getTotalWidth(),w=Math.max(0,_-h.canvasHeight),l=C-y.columnEngine.getPinnedWidth(),p=h.canvasWidth-y.columnEngine.getPinnedWidth(),R=Math.max(0,l-p),E=Math.max(0,Math.min(w,h.scrollY+s.deltaY)),K=Math.max(0,Math.min(R,h.scrollX+s.deltaX));h.update({scrollY:E,scrollX:K}),V(E),U(K),(ue=q.current)==null||ue.markAllDirty();const J=A.current;J.hasMore&&J.onLoadMore&&!J.loading&&w-E<$t?qe.current||(qe.current=!0,J.onLoadMore()):w-E>=$t&&(qe.current=!1)},[]);g.useEffect(()=>{const s=Ie.current[2];if(s)return s.addEventListener("wheel",it,{passive:!1}),()=>s.removeEventListener("wheel",it)},[it]),g.useEffect(()=>{N||(qe.current=!1)},[N]);const Jt=g.useCallback(s=>{var p,R,E;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);Ne.current=l,pe.current=(l==null?void 0:l.rowId)??null,Re((l==null?void 0:l.rowId)??null),(p=q.current)==null||p.markDirty("overlay"),(E=(R=A.current).onCellHover)==null||E.call(R,(l==null?void 0:l.rowId)??null,(l==null?void 0:l.colId)??null,s.nativeEvent)},[]),lt=g.useCallback(s=>{P(h=>{var _,C;const y=new Set(h);return y.has(s)?(y.delete(s),ee(`Row ${s} collapsed`)):(y.add(s),ee(`Row ${s} expanded`)),(C=(_=A.current).onExpandChange)==null||C.call(_,[...y]),y})},[]),Qt=g.useCallback(s=>{var Ye,Ve,Be,Ge,$e;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);if(!l)return;const{treeEngine:p,columnEngine:R,selectionEngine:E}=h,K=R.getPinnedColumns();if(K.length>0){const Ce=K[0].id;if(p.hasChildren(l.rowId)&&l.colId===Ce){A.current.onTreeToggle?A.current.onTreeToggle(l.rowId):lt(l.rowId);return}}const J=p.flattenedRows.map(Ce=>Ce.id),ue=R.getVisibleColumns().map(Ce=>Ce.id);s.ctrlKey||s.metaKey?E.ctrlClick(l.rowId,l.colId):s.shiftKey?E.shiftClick(l.rowId,l.colId,J,ue):E.click(l.rowId,l.colId,J,ue),ae.current={rowId:l.rowId,colId:l.colId},(Ve=(Ye=A.current).onSelectionChange)==null||Ve.call(Ye,E.getSelection()),(Ge=(Be=A.current).onCellClick)==null||Ge.call(Be,l.rowId,l.colId,s.nativeEvent),($e=q.current)==null||$e.markDirty("overlay");const ve=String(l.rowId),ce=R.getVisibleColumns().find(Ce=>Ce.id===l.colId),et=(ce==null?void 0:ce.title)??l.colId;ee(`Row ${ve}, Column ${et} selected`)},[lt]),en=g.useCallback(s=>{var p,R;const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);l&&((R=(p=A.current).onCellDoubleClick)==null||R.call(p,l.rowId,l.colId,s.nativeEvent))},[]),tn=g.useCallback(s=>{var p,R;s.preventDefault();const h=me.current;if(!h)return;const y=Ie.current[2];if(!y)return;const _=y.getBoundingClientRect(),C=s.clientX-_.left,w=s.clientY-_.top,l=ot(C,w,he.current,h.treeEngine,h.columnEngine);l&&((R=(p=A.current).onCellContextMenu)==null||R.call(p,l.rowId,l.colId,s.nativeEvent))},[]),nn=g.useCallback(()=>{var s,h,y;Ne.current=null,pe.current=null,Re(null),(s=q.current)==null||s.markDirty("overlay"),(y=(h=A.current).onCellHover)==null||y.call(h,null,null,new PointerEvent("pointerleave"))},[]),on=g.useCallback((s,h)=>{var y,_;(_=(y=A.current).onColumnHeaderClick)==null||_.call(y,s,h.nativeEvent)},[]),sn=g.useCallback((s,h)=>{var _,C,w,l;j(p=>p.map(E=>{if(E.id!==s)return E;const K=E.minWidth??30;return{...E,width:Math.max(K,h)}}));const y=(_=me.current)==null?void 0:_.columnEngine;if(y){const p=y.getColumnX(s),R=y.getVisibleColumns().find(E=>E.id===s);if(R){const K=R.pinned==="left"?p+Math.max(R.minWidth??30,h):p+Math.max(R.minWidth??30,h)-he.current.scrollX;Z({colId:s,x:K})}}(w=(C=A.current).onColumnResize)==null||w.call(C,s,h),(l=q.current)==null||l.markAllDirty()},[]);g.useEffect(()=>{const s=()=>{Z(null)};return document.addEventListener("mouseup",s),()=>document.removeEventListener("mouseup",s)},[]);const ln=g.useCallback((s,h)=>{var y,_,C;j(w=>{const l=[...w],p=l.findIndex(K=>K.id===s);if(p===-1)return w;const[R]=l.splice(p,1),E=Math.max(0,Math.min(h,l.length));return l.splice(E,0,R),l}),(_=(y=A.current).onColumnReorder)==null||_.call(y,s,h),(C=q.current)==null||C.markAllDirty()},[]),rn=g.useCallback((s,h)=>{var y,_,C;j(w=>w.map(l=>l.id===s?{...l,hidden:!h}:l)),(_=(y=A.current).onColumnVisibilityChange)==null||_.call(y,s,h),(C=q.current)==null||C.markAllDirty()},[]),bt=g.useCallback((s,h,y,_)=>{h.preventDefault(),h.stopPropagation(),Le(!0);const C=s==="y"?h.clientY:h.clientX,w=s==="y"?he.current.scrollY:he.current.scrollX,l=R=>{var ve;const E=(s==="y"?R.clientY:R.clientX)-C,K=y>0?E/y*_:0,J=Math.max(0,Math.min(_,w+K)),ue=he.current;s==="y"?(ue.update({scrollY:J}),V(J)):(ue.update({scrollX:J}),U(J)),(ve=q.current)==null||ve.markAllDirty()},p=()=>{Le(!1),document.removeEventListener("mousemove",l),document.removeEventListener("mouseup",p)};document.addEventListener("mousemove",l),document.addEventListener("mouseup",p)},[]),cn=g.useCallback(s=>{var ue,ve,ce,et,Ye,Ve,Be,Ge,$e,Ce,ht,kt,Mt,_t,Tt,Wt,Ht,Dt,Xt,Et,Pt,Lt,Ot,jt;const h=me.current;if(!h)return;const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=he.current,l=y.flattenedRows.map(te=>te.id),p=_.getVisibleColumns().map(te=>te.id);if(l.length===0||p.length===0)return;const R=C.getSelection().anchor,E=s.ctrlKey||s.metaKey,K=(te,re)=>{const de=l.length*w.rowHeight,Se=_.getTotalWidth(),ke=Math.max(0,de-w.canvasHeight),tt=Math.max(0,Se-w.canvasWidth),We=te*w.rowHeight,Ee=We+w.rowHeight;let we=w.scrollY;We<w.scrollY?we=We:Ee>w.scrollY+w.canvasHeight&&(we=Ee-w.canvasHeight),we=Math.max(0,Math.min(ke,we));const Me=_.getVisibleColumns()[re],Fe=Me?_.getColumnX(Me.id):0,nt=Fe+((Me==null?void 0:Me.width)??0);let xe=w.scrollX;Fe<w.scrollX?xe=Fe:nt>w.scrollX+w.canvasWidth&&(xe=nt-w.canvasWidth),xe=Math.max(0,Math.min(tt,xe)),(we!==w.scrollY||xe!==w.scrollX)&&(w.update({scrollY:we,scrollX:xe}),V(we),U(xe))},J=(te,re,de)=>{var At,zt,Yt;let Se=R?l.indexOf(R.rowId):-1,ke=R?p.indexOf(R.colId):-1;Se===-1&&(Se=0),ke===-1&&(ke=0);const tt=Math.max(0,Math.min(l.length-1,Se+te)),We=Math.max(0,Math.min(p.length-1,ke+re)),Ee=l[tt],we=p[We];de?C.shiftClick(Ee,we,l,p):C.click(Ee,we,l,p),(zt=(At=A.current).onSelectionChange)==null||zt.call(At,C.getSelection()),(Yt=q.current)==null||Yt.markDirty("overlay"),K(tt,We);const Me=_.getVisibleColumns()[We],Fe=String(Ee),nt=(Me==null?void 0:Me.title)??String(we),xe=C.getSelection().cells.size;xe>1?ee(`${xe} cells selected`):ee(`Row ${Fe}, Column ${nt} selected`)};switch(s.key){case"ArrowUp":s.preventDefault(),J(-1,0,s.shiftKey);break;case"ArrowDown":s.preventDefault(),J(1,0,s.shiftKey);break;case"ArrowLeft":s.preventDefault(),J(0,-1,s.shiftKey);break;case"ArrowRight":s.preventDefault(),J(0,1,s.shiftKey);break;case"Tab":{if(s.preventDefault(),s.shiftKey){const te=R?l.indexOf(R.rowId):0;if((R?p.indexOf(R.colId):0)>0)J(0,-1,!1);else{const de=Math.max(0,(te===-1?0:te)-1),Se=l[de],ke=p[p.length-1];C.click(Se,ke,l,p),(Ye=(et=A.current).onSelectionChange)==null||Ye.call(et,C.getSelection()),(Ve=q.current)==null||Ve.markDirty("overlay"),K(de,p.length-1)}}else{const te=R?l.indexOf(R.rowId):0;if((R?p.indexOf(R.colId):-1)<p.length-1)J(0,1,!1);else{const de=Math.min(l.length-1,(te===-1?0:te)+1),Se=l[de],ke=p[0];C.click(Se,ke,l,p),(ve=(ue=A.current).onSelectionChange)==null||ve.call(ue,C.getSelection()),(ce=q.current)==null||ce.markDirty("overlay"),K(de,0)}}break}case"Escape":s.preventDefault(),C.clear(),(Ge=(Be=A.current).onSelectionChange)==null||Ge.call(Be,C.getSelection()),($e=q.current)==null||$e.markDirty("overlay"),ee("Selection cleared");break;case"a":case"A":if(E){s.preventDefault();for(const te of l)for(const re of p)C.ctrlClick(te,re);(ht=(Ce=A.current).onSelectionChange)==null||ht.call(Ce,C.getSelection()),(kt=q.current)==null||kt.markDirty("overlay"),ee(`${l.length*p.length} cells selected`)}break;case"Home":{s.preventDefault();const te=R?l.indexOf(R.rowId):0;if(E)C.click(l[0],p[0],l,p),(_t=(Mt=A.current).onSelectionChange)==null||_t.call(Mt,C.getSelection()),(Tt=q.current)==null||Tt.markDirty("overlay"),K(0,0);else{const re=te===-1?0:te;C.click(l[re],p[0],l,p),(Ht=(Wt=A.current).onSelectionChange)==null||Ht.call(Wt,C.getSelection()),(Dt=q.current)==null||Dt.markDirty("overlay"),K(re,0)}break}case"End":{s.preventDefault();const te=R?l.indexOf(R.rowId):0;if(E){const re=l.length-1,de=p.length-1;C.click(l[re],p[de],l,p),(Et=(Xt=A.current).onSelectionChange)==null||Et.call(Xt,C.getSelection()),(Pt=q.current)==null||Pt.markDirty("overlay"),K(re,de)}else{const re=te===-1?0:te,de=p.length-1;C.click(l[re],p[de],l,p),(Ot=(Lt=A.current).onSelectionChange)==null||Ot.call(Lt,C.getSelection()),(jt=q.current)==null||jt.markDirty("overlay"),K(re,de)}break}}},[]);g.useImperativeHandle(o,()=>({captureToCanvas(s){const h=me.current;if(!h)throw new Error("CanvasGrid: engines not initialized");const{treeEngine:y,columnEngine:_,selectionEngine:C}=h,w=A.current,l=w.rowHeight??Gt,p=w.sidebarWidth??Bt;return Wn({treeEngine:y,columnEngine:_,selectionEngine:C,columnGroups:w.columnGroups,rowHeight:l,sidebarWidth:p,headerOrientation:w.headerOrientation??"horizontal",columnHeaderHeight:w.columnHeaderHeight,gridLayerConfig:{rowStyle:w.rowStyle},cellLayerConfig:{cellRenderer:w.cellRenderer,getCellData:w.getCellData,hoveredCell:null},scale:(s==null?void 0:s.scale)??2,rowRange:s==null?void 0:s.rowRange})},getRowCount(){var s;return((s=me.current)==null?void 0:s.treeEngine.flattenedRows.length)??0}}),[]);const dn=c!=null&&c.length>0,vt=Ft(O??"horizontal",dn,I),an=je+fe.getTotalWidth()+2,hn=vt+De.flattenedRows.length*H+2,Xe=12,Ct=24,Ze=ge||Ue,xt=De.flattenedRows.length*H,fn=fe.getTotalWidth(),rt=fe.getPinnedWidth(),Je=Math.max(0,xt-F.height),yt=fn-rt,ct=Math.max(0,F.width-rt),Qe=Math.max(0,yt-ct),Rt=2,It=Je>Rt&&F.height>0,St=Qe>Rt&&F.width>0,Ae=F.height-(St?Xe:0),dt=Math.min(Ae,Math.max(Ct,Ae*(F.height/xt))),un=Je>0?$/Je*(Ae-dt):0,ze=ct-(It?Xe:0),at=Math.min(ze,Math.max(Ct,ze*(ct/yt))),gn=Qe>0?G/Qe*(ze-at):0;return x.jsxs("div",{ref:He,tabIndex:0,role:"grid","aria-label":ye,"aria-rowcount":De.flattenedRows.length,"aria-colcount":fe.getVisibleColumns().length,onKeyDown:cn,style:{display:"flex",flexDirection:"column",width:an,height:hn,maxWidth:"100%",maxHeight:D??"100%",overflow:"hidden",fontFamily:"system-ui, sans-serif",outline:"none",border:"1px solid #d1d5db",borderRadius:4},children:[x.jsxs("div",{style:{display:"flex",flexDirection:"row",height:vt,flexShrink:0},children:[!st&&x.jsx("div",{style:{width:je,flexShrink:0,borderRight:"1px solid #e5e7eb",borderBottom:"1px solid #e5e7eb",backgroundColor:"#f9fafb",display:"flex",flexDirection:"column",overflow:"hidden"},children:e.sidebarHeaderRenderer?x.jsx("div",{style:{width:"100%",flex:1},children:e.sidebarHeaderRenderer()}):_e?x.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",flex:1},children:x.jsx(Sn,{columnEngine:fe,onColumnVisibilityChange:rn})}):null}),x.jsx(In,{columnEngine:fe,columnGroups:c,scrollX:G,orientation:O,columnHeaderHeight:I,onColumnClick:b?on:void 0,onColumnResize:e.onColumnResize!==void 0?sn:void 0,onColumnReorder:e.onColumnReorder!==void 0?ln:void 0,onColumnFilterClick:e.onColumnFilterClick,columnHeaderRenderer:e.columnHeaderRenderer})]}),x.jsxs("div",{style:{display:"flex",flexDirection:"row",flex:1,overflow:"hidden"},children:[!st&&x.jsx(Cn,{treeEngine:De,width:je,rowHeight:H,scrollTop:$,viewportHeight:he.current.canvasHeight||600,sidebarRenderer:u,onToggle:lt,onRowClick:e.onRowHeaderClick?(s,h)=>e.onRowHeaderClick(s,h.nativeEvent):void 0,hoveredRowId:ie,onRowHover:s=>{var h;pe.current=s,Re(s),(h=q.current)==null||h.markDirty("overlay")}}),x.jsxs("div",{style:{flex:1,position:"relative",overflow:"hidden"},onMouseEnter:()=>ne(!0),onMouseLeave:()=>ne(!1),children:[x.jsx("canvas",{ref:s=>{s&&(Ie.current[0]=s)},style:{position:"absolute",top:0,left:0,zIndex:0}}),x.jsx("canvas",{ref:s=>{s&&(Ie.current[1]=s)},style:{position:"absolute",top:0,left:0,zIndex:1}}),x.jsx("canvas",{ref:s=>{s&&(Ie.current[2]=s)},style:{position:"absolute",top:0,left:0,zIndex:2},onPointerMove:Jt,onPointerDown:Qt,onDoubleClick:en,onContextMenu:tn,onPointerLeave:nn}),It&&x.jsx("div",{style:{position:"absolute",top:0,right:0,width:Xe,height:Ae,zIndex:3,opacity:Ze?1:0,transition:"opacity 150ms ease",pointerEvents:Ze?"auto":"none"},children:x.jsx("div",{onMouseDown:s=>bt("y",s,Ae-dt,Je),style:{position:"absolute",top:un,left:2,width:Xe-4,height:dt,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})}),St&&x.jsx("div",{style:{position:"absolute",bottom:0,left:rt,height:Xe,width:ze,zIndex:3,opacity:Ze?1:0,transition:"opacity 150ms ease",pointerEvents:Ze?"auto":"none"},children:x.jsx("div",{onMouseDown:s=>bt("x",s,ze-at,Qe),style:{position:"absolute",left:gn,top:2,height:Xe-4,width:at,borderRadius:8,backgroundColor:"rgba(0, 0, 0, 0.3)",cursor:"pointer"}})})]})]}),i!=null&&oe!=null&&x.jsx(kn,{content:i,position:oe}),x.jsx(Mn,{message:be})]})}));exports.CanvasGrid=Dn;exports.ColumnEngine=Te;exports.SelectionEngine=Ut;exports.TreeEngine=Kt;exports.ViewState=pt;exports.createDrawHelpers=qt;
|