@koeroesi86/canvas-grid 26.11.2 → 26.11.3

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.
@@ -10,7 +10,9 @@ declare class ColumnStore {
10
10
  get(slot: number): unknown;
11
11
  getUpdatedAt(slot: number): number | undefined;
12
12
  set(slot: number, value: unknown, updatedAt?: number): void;
13
- /** Drops the values of the given slots (ascending) and shifts the following ones down. */
14
- removeSlots(slots: readonly number[]): void;
13
+ /** Empties the given slots, leaving holes; the other slots keep their place. */
14
+ clearSlots(slots: readonly number[]): void;
15
+ /** Keeps only the given slots (ascending), renumbering them 0, 1, 2... in that order. */
16
+ compact(slots: ArrayLike<number>): void;
15
17
  }
16
18
  export default ColumnStore;
@@ -19,6 +19,8 @@ declare class DataStore {
19
19
  private pending;
20
20
  private sortModel;
21
21
  private sortDirty;
22
+ /** Ids of the rows changed since the last sort; only those have to move when few changed. */
23
+ private sortChanged;
22
24
  private environment;
23
25
  constructor(environment: DataStoreEnvironment);
24
26
  get hasPending(): boolean;
@@ -44,6 +46,10 @@ declare class DataStore {
44
46
  private applyUpdate;
45
47
  private applyValue;
46
48
  private markSortDirty;
49
+ /** Orders two rows by the sort model, ties by slot so the order is total. */
50
+ private createComparator;
47
51
  private sort;
52
+ /** Re-sorts after a flush: moves only the changed rows when they are few, otherwise sorts everything. */
53
+ private sortChanges;
48
54
  }
49
55
  export default DataStore;
@@ -3,34 +3,56 @@ export type Row = RowData;
3
3
  /**
4
4
  * Rows in display order: a dense array with an id lookup and each row's top offset,
5
5
  * so appends, lookups and finding the row at a scroll offset never scan every row.
6
- * Removing rows renumbers the rows after the first removed one (once per call).
7
- * Rows keep a `slot` into the column stores that does not change when the rows are sorted.
6
+ * Removing rows renumbers the positions after the first removed one (once per call).
7
+ * Rows keep a `slot` into the column stores that does not change when the rows are sorted or others removed;
8
+ * removed rows leave holes, which are closed when they outnumber the rows.
8
9
  */
9
10
  declare class RowStore {
10
11
  private items;
11
- private readonly indexById;
12
+ private readonly byId;
12
13
  /** `tops[i]` is the offset of row `i` from the top of the first row. */
13
14
  private tops;
14
15
  private end;
16
+ /** Slots handed out so far, including holes left by removed rows. */
17
+ private nextSlot;
15
18
  get length(): number;
16
19
  /** Sum of all row heights. */
17
20
  get totalHeight(): number;
21
+ /** Number of slots in use or left as holes; the length the column stores need. */
22
+ get slotCount(): number;
18
23
  get keys(): RowId[];
19
24
  get(id: Key): Row | undefined;
20
25
  at(index: number): Row | undefined;
21
26
  topOf(index: number): number;
22
27
  /** Index of the first row that ends below `offset`; `length` when the offset is past the last row. */
23
28
  indexAt(offset: number): number;
24
- /** Appends the row, or replaces the row with the same id in place. */
25
- set(row: Omit<Row, 'index' | 'slot'>): void;
29
+ /** Appends the row, or replaces the row with the same id in place; returns the stored row. */
30
+ set(row: Omit<Row, 'index' | 'slot'>): Row;
26
31
  /**
27
32
  * Removes the rows with the given ids, ignoring unknown ones.
28
- * Returns the slots of the removed rows in ascending order, which the column stores must drop too.
33
+ * Returns the slots of the removed rows in ascending order, which the column stores must clear.
34
+ * Other rows keep their slots, so removing costs nothing per column; see `compactSlots`.
29
35
  */
30
36
  removeMany(ids: Key[]): number[];
37
+ /**
38
+ * Renumbers the slots densely once more than half of them are holes left by removed rows.
39
+ * Returns the previous slots of the remaining rows in ascending order (the column stores keep exactly those),
40
+ * or undefined when nothing was renumbered. Doing this rarely keeps removal cheap for streams that drop old rows.
41
+ */
42
+ compactSlots(): Int32Array | undefined;
31
43
  /** Reorders the rows; the sort is stable, so equal rows keep their relative order. */
32
44
  sort(compare: (a: Row, b: Row) => number): void;
45
+ /**
46
+ * Puts the given rows back in order, assuming every other row is already ordered.
47
+ * `compare` must be a total order (no ties), which a slot tiebreak provides. Costs one comparison pass per
48
+ * moved row (a binary search) plus a copy, instead of sorting every row.
49
+ */
50
+ resort(ids: Iterable<Key>, compare: (a: Row, b: Row) => number): void;
33
51
  clear(): void;
52
+ /** Removes the items at the given ascending indexes in place, moving the others down in runs. */
53
+ private closeGaps;
54
+ /** Index of the first row in `rows` that is not ordered before `row`. */
55
+ private lowerBound;
34
56
  /** Recomputes positions and offsets from `from` onwards. */
35
57
  private reindexFrom;
36
58
  }
@@ -1,2 +1,2 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?module.exports=t():typeof define==`function`&&define.amd?define([],t):(e=typeof globalThis<`u`?globalThis:e||self,e.CanvasGrid=t())})(this,function(){var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t,t.exports),e=null),t.exports),s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,o)=>(o=n==null?{}:e(i(n)),s(r||!n||!n.__esModule||!a.call(n,`default`)?t(o,`default`,{value:n,enumerable:!0}):o,n)),l=o(((e,t)=>{var n=`Expected a function`,r=NaN,i=/^\s+|\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,o=/^0b[01]+$/i,s=/^0o[0-7]+$/i,c=parseInt,l=typeof global==`object`&&global&&global.Object===Object&&global,u=typeof self==`object`&&self&&self.Object===Object&&self,d=l||u||Function(`return this`)(),f=Object.prototype.toString,p=Math.max,m=Math.min,h=function(){return d.Date.now()};function g(e,t,r){var i,a,o,s,c,l,u=0,d=!1,f=!1,g=!0;if(typeof e!=`function`)throw TypeError(n);t=x(t)||0,v(r)&&(d=!!r.leading,f=`maxWait`in r,o=f?p(x(r.maxWait)||0,t):o,g=`trailing`in r?!!r.trailing:g);function _(t){var n=i,r=a;return i=a=void 0,u=t,s=e.apply(r,n),s}function y(e){return u=e,c=setTimeout(C,t),d?_(e):s}function b(e){var n=e-l,r=e-u,i=t-n;return f?m(i,o-r):i}function S(e){var n=e-l,r=e-u;return l===void 0||n>=t||n<0||f&&r>=o}function C(){var e=h();if(S(e))return w(e);c=setTimeout(C,b(e))}function w(e){return c=void 0,g&&i?_(e):(i=a=void 0,s)}function T(){c!==void 0&&clearTimeout(c),u=0,i=l=a=c=void 0}function E(){return c===void 0?s:w(h())}function D(){var e=h(),n=S(e);if(i=arguments,a=this,l=e,n){if(c===void 0)return y(l);if(f)return c=setTimeout(C,t),_(l)}return c===void 0&&(c=setTimeout(C,t)),s}return D.cancel=T,D.flush=E,D}function _(e,t,r){var i=!0,a=!0;if(typeof e!=`function`)throw TypeError(n);return v(r)&&(i=`leading`in r?!!r.leading:i,a=`trailing`in r?!!r.trailing:a),g(e,t,{leading:i,maxWait:t,trailing:a})}function v(e){var t=typeof e;return!!e&&(t==`object`||t==`function`)}function y(e){return!!e&&typeof e==`object`}function b(e){return typeof e==`symbol`||y(e)&&f.call(e)==`[object Symbol]`}function x(e){if(typeof e==`number`)return e;if(b(e))return r;if(v(e)){var t=typeof e.valueOf==`function`?e.valueOf():e;e=v(t)?t+``:t}if(typeof e!=`string`)return e===0?e:+e;e=e.replace(i,``);var n=o.test(e);return n||s.test(e)?c(e.slice(2),n?2:8):a.test(e)?r:+e}t.exports=_})),u=o(((e,t)=>{var n=NaN,r=/^\s+|\s+$/g,i=/^[-+]0x[0-9a-f]+$/i,a=/^0b[01]+$/i,o=/^0o[0-7]+$/i,s=parseInt,c=typeof global==`object`&&global&&global.Object===Object&&global,l=typeof self==`object`&&self&&self.Object===Object&&self,u=c||l||Function(`return this`)(),d=Object.prototype.toString,f=Math.max,p=Math.min,m=function(){return u.Date.now()};function h(e,t,n){var r,i,a,o,s,c,l=0,u=!1,d=!1,h=!0;if(typeof e!=`function`)throw TypeError(`Expected a function`);t=y(t)||0,g(n)&&(u=!!n.leading,d=`maxWait`in n,a=d?f(y(n.maxWait)||0,t):a,h=`trailing`in n?!!n.trailing:h);function _(t){var n=r,a=i;return r=i=void 0,l=t,o=e.apply(a,n),o}function v(e){return l=e,s=setTimeout(S,t),u?_(e):o}function b(e){var n=e-c,r=e-l,i=t-n;return d?p(i,a-r):i}function x(e){var n=e-c,r=e-l;return c===void 0||n>=t||n<0||d&&r>=a}function S(){var e=m();if(x(e))return C(e);s=setTimeout(S,b(e))}function C(e){return s=void 0,h&&r?_(e):(r=i=void 0,o)}function w(){s!==void 0&&clearTimeout(s),l=0,r=c=i=s=void 0}function T(){return s===void 0?o:C(m())}function E(){var e=m(),n=x(e);if(r=arguments,i=this,c=e,n){if(s===void 0)return v(c);if(d)return s=setTimeout(S,t),_(c)}return s===void 0&&(s=setTimeout(S,t)),o}return E.cancel=w,E.flush=T,E}function g(e){var t=typeof e;return!!e&&(t==`object`||t==`function`)}function _(e){return!!e&&typeof e==`object`}function v(e){return typeof e==`symbol`||_(e)&&d.call(e)==`[object Symbol]`}function y(e){if(typeof e==`number`)return e;if(v(e))return n;if(g(e)){var t=typeof e.valueOf==`function`?e.valueOf():e;e=g(t)?t+``:t}if(typeof e!=`string`)return e===0?e:+e;e=e.replace(r,``);var c=a.test(e);return c||o.test(e)?s(e.slice(2),c?2:8):i.test(e)?n:+e}t.exports=h})),d=c(l()),f=c(u()),p=class{entries=new Map;constructor(){this.get=this.get.bind(this),this.set=this.set.bind(this),this.clear=this.clear.bind(this),this.forEach=this.forEach.bind(this)}get keys(){return Array.from(this.entries.values(),e=>e.key)}get(e){return this.entries.get(String(e))?.value}set(e,t){let n=this.entries.get(String(e));n?n.value=t:this.entries.set(String(e),{key:e,value:t})}add(e){this.set(this.entries.size,e)}delete(e){this.entries.delete(String(e))}clear(e){typeof e==`function`?this.entries.forEach(({key:t,value:n},r)=>{e(n,t)&&this.entries.delete(r)}):this.entries.clear()}forEach(e){this.entries.forEach(({key:t,value:n})=>e(n,t))}get length(){return this.entries.size}},m=class{values;updatedAt=void 0;constructor(e=0){this.values=Array(e).fill(void 0)}get(e){return this.values[e]}getUpdatedAt(e){return this.updatedAt?.[e]||void 0}set(e,t,n){for(;this.values.length<e;)this.values.push(void 0);if(this.values[e]=t,n!==void 0||this.updatedAt!==void 0){for(this.updatedAt??=Array(this.values.length).fill(0);this.updatedAt.length<this.values.length;)this.updatedAt.push(0);this.updatedAt[e]=n??0}}removeSlots(e){if(e.length===0)return;let t=t=>{let n=0,r=0;for(let i=0;i<t.length;i++)r<e.length&&e[r]===i?r++:t[n++]=t[i];t.length=n};t(this.values),this.updatedAt&&t(this.updatedAt)}},h=class{items=[];indexById=new Map;tops=[];end=0;get length(){return this.items.length}get totalHeight(){return this.end}get keys(){return this.items.map(e=>e.id)}get(e){let t=this.indexById.get(String(e));return t===void 0?void 0:this.items[t]}at(e){return this.items[e]}topOf(e){return this.tops[e]??this.end}indexAt(e){if(e<=0)return 0;if(e>=this.end)return this.length;let t=0,n=this.items.length-1;for(;t<n;){let r=Math.ceil((t+n)/2);this.tops[r]<=e?t=r:n=r-1}return t}set(e){let t=this.indexById.get(String(e.id));if(t!==void 0){this.items[t]={...e,index:t,slot:this.items[t].slot},this.reindexFrom(t);return}let n=this.items.length;this.items.push({...e,index:n,slot:n}),this.indexById.set(String(e.id),n),this.tops.push(this.end),this.end+=Math.round(e.height)}removeMany(e){let t=new Set;if(e.forEach(e=>{let n=this.get(e);n&&t.add(n)}),t.size===0)return[];let n=Array.from(t,e=>e.slot).sort((e,t)=>e-t),r=Array.from(t).reduce((e,t)=>Math.min(e,t.index),1/0);t.forEach(e=>this.indexById.delete(String(e.id))),this.items=this.items.filter(e=>!t.has(e));let i=new Int32Array(this.items.length+n.length);for(let e=0,t=0;e<i.length;e++)n[t]===e&&t++,i[e]=t;return this.items.forEach(e=>{e.slot-=i[e.slot]}),this.reindexFrom(r),n}sort(e){this.items.sort(e),this.reindexFrom(0)}clear(){this.items=[],this.indexById.clear(),this.tops=[],this.end=0}reindexFrom(e){this.tops.length=this.items.length;let t=e===0?0:this.tops[e-1]+Math.round(this.items[e-1].height);for(let n=e;n<this.items.length;n++){let e=this.items[n];e.index=n,this.indexById.set(String(e.id),n),this.tops[n]=t,t+=Math.round(e.height)}this.end=t}},g=()=>new Date().valueOf(),_=(e,t)=>e.id,v=new Intl.Collator,y=e=>e==null?3:typeof e==`number`||typeof e==`bigint`?0:typeof e==`string`?1:2,b=(e,t)=>{let n=y(e)-y(t);return n===0?y(e)===3?0:y(e)===0?Number(e)-Number(t):v.compare(String(e),String(t)):n},x=class{rows=new h;columns=new Map;pending=[];sortModel=[];sortDirty=!1;environment;constructor(e){this.environment=e}get hasPending(){return this.pending.length>0}getSortModel(){return this.sortModel}setSortModel(e){this.sortModel=[...e],this.sort()}getValue(e,t){let n=this.rows.get(e);return n?this.columns.get(String(t))?.get(n.slot):void 0}getUpdatedAt(e,t){let n=this.rows.get(e);return n?this.columns.get(String(t))?.getUpdatedAt(n.slot):void 0}queueUpdate(e){this.pending.push({type:`update`,row:e})}queueRemove(e){this.pending.push({type:`remove`,row:e})}clear(){this.rows.clear(),this.columns.clear()}setRows(e){this.pending=[],this.clear(),e.forEach((e,t)=>this.addRow(this.environment.getRowId(e,t),e,void 0)),this.sort()}flush(e=g()){let t=this.pending;this.pending=[];let n=[],r=!1,i=()=>{r=this.removeRows(n)||r,n.length=0};return t.forEach(({type:t,row:r})=>{t===`remove`?n.push(this.environment.getRowId(r)):(i(),this.applyUpdate(r,e))}),i(),this.sortDirty?(this.sort(),!0):r}column(e){let t=this.columns.get(e);return t||(t=new m(this.rows.length),this.columns.set(e,t)),t}addRow(e,t,n){this.rows.set({id:e,height:this.environment.getCellHeight()});let r=this.rows.get(e)?.slot??0;Object.keys(t).forEach(e=>this.column(e).set(r,t[e],n)),this.markSortDirty(Object.keys(t))}removeRows(e){if(e.length===0)return!1;let t=this.rows.removeMany(e);return this.columns.forEach(e=>e.removeSlots(t)),t.length>0}applyUpdate(e,t){let n=this.environment.getRowId(e),r=this.rows.get(n);r?Object.keys(e).forEach(n=>this.applyValue(r.slot,n,e[n],t)):this.addRow(n,e,t)}applyValue(e,t,n,r){let i=this.column(t),a=i.get(e);a!==n&&(i.set(e,n,a===void 0?void 0:r),this.markSortDirty([t]))}markSortDirty(e){this.sortModel.some(({columnId:t})=>e.includes(String(t)))&&(this.sortDirty=!0)}sort(){this.sortDirty=!1;let e=this.sortModel.map(({columnId:e,direction:t=`asc`,compare:n=b})=>({values:this.columns.get(String(e))?.values,compare:n,sign:t===`desc`?-1:1}));this.rows.sort((t,n)=>{for(let{values:r,compare:i,sign:a}of e){let e=r?i(r[t.slot],r[n.slot]):0;if(e!==0)return e*a}return t.slot-n.slot})}},S=class{entries=new Map;limit;constructor(e){this.limit=e}get(e){let t=this.entries.get(e);if(t!==void 0)return this.entries.delete(e),this.entries.set(e,t),t}set(e,t){this.entries.delete(e),this.entries.set(e,t);let[n]=this.entries.keys();this.entries.size>this.limit&&n!==void 0&&this.entries.delete(n)}clear(){this.entries.clear()}get size(){return this.entries.size}},C=`canvas-grid{isolation:isolate;display:block;position:relative}canvas-grid,canvas-grid *{box-sizing:border-box;margin:0;padding:0}canvas-grid .drawingLayer{contain:strict;width:100%;height:100%;position:absolute;top:0;left:0}canvas-grid .scrollArea{z-index:10;backface-visibility:hidden;will-change:transform;width:100%;height:100%;position:absolute;top:0;left:0;overflow:auto}canvas-grid .scrollArea>div{will-change:scroll-position, transform}`,w=e=>!!e,T=e=>t=>({draw:(n,r,i)=>e(n,t,r,i)}),E=({x:e=0,y:t=0,children:n})=>({draw:(r,i,a)=>[n].flat(2).filter(w).forEach(n=>n.draw(r,i+e,a+t))}),D={createElement(e,t,...n){return e(Object.assign({},t,{children:n}))}},O=class{options;constructor(e){this.options={...e}}update(e){let t={...this.options};Object.assign(this.options,e),this.optionsUpdated(t)}optionsUpdated(e){}clearCache(){}},k=class extends O{bitmaps=new S(4096);cacheKey(e){}pixelRatio=1;clearCache(){this.bitmaps.clear()}paint(e,t,n,r){let i=this.cacheKey(t),a=(i===void 0?void 0:this.bitmaps.get(i))??this.renderBitmap(t,i);a.width<=0||a.height<=0||e.drawImage(a,n+(t.x??0),r+(t.y??0),a.width/this.pixelRatio,a.height/this.pixelRatio)}element=T((e,t,n,r)=>this.paint(e,t,n,r));renderBitmap(e,t){let n=this.render(e);if(t===void 0||n.width<=0||n.height<=0)return n;let r=document.createElement(`canvas`);return r.width=n.width,r.height=n.height,r.getContext(`2d`)?.drawImage(n,0,0),this.bitmaps.set(t,r),r}},A=(e,t)=>{let n=e.getContext(`2d`,t);if(!n)throw Error(`2D canvas context is not available`);return n},j=class extends O{ctx;offCanvas=document.createElement(`canvas`);offCtx;rotation=Math.PI/180*270;speed=6;count=75;constructor(e){super(e),this.ctx=this.options.ctx,this.offCanvas.width=250,this.offCanvas.height=250,this.offCtx=A(this.offCanvas),this.offCtx.lineWidth=.5,this.offCtx.strokeStyle=`rgba(255, 255, 255, 0.75)`}updateLoader(){this.rotation+=this.speed/100}renderLoader(){this.offCtx.save(),this.offCtx.globalCompositeOperation=`source-over`,this.offCtx.translate(125,125),this.offCtx.rotate(this.rotation);let e=this.count;for(;e--;)this.offCtx.beginPath(),this.offCtx.arc(0,0,e+Math.random()*35,Math.random(),Math.PI/3+Math.random()/12,!1),this.offCtx.stroke();this.offCtx.restore()}render(){this.offCtx.globalCompositeOperation=`destination-out`,this.offCtx.fillStyle=`rgba(255, 255, 255, 0.03)`,this.offCtx.fillRect(0,0,250,250),this.updateLoader(),this.renderLoader();let e=Math.round((this.ctx.canvas.width-this.offCanvas.width)/2),t=Math.round((this.ctx.canvas.height-this.offCanvas.height)/2);return this.ctx.clearRect(e,t,250,250),this.ctx.drawImage(this.offCanvas,e,t),this.offCanvas}},M=class{name;canvas=document.createElement(`canvas`);ctx;constructor(e,{alpha:t=!0,desynchronized:n=!1}={}){this.name=e,this.canvas.className=`drawingLayer ${e}`,this.ctx=A(this.canvas,{alpha:t,desynchronized:n})}width=0;height=0;pixelRatio=1;resize(e,t,n=1){return e===this.width&&t===this.height&&n===this.pixelRatio?!1:(this.width=e,this.height=t,this.pixelRatio=n,this.canvas.width=Math.round(e*n),this.canvas.height=Math.round(t*n),this.ctx.setTransform(n,0,0,n,0,0),!0)}clear(){this.ctx.clearRect(0,0,this.width,this.height)}},N=class{renderers=new Map;warned=new Set;register(e,t){this.renderers.set(e,t),this.warned.delete(e)}unregister(e){this.renderers.delete(e)}get(e){return this.renderers.get(e)}resolve(e){if(typeof e!=`string`)return e;let t=this.renderers.get(e);return!t&&!this.warned.has(e)&&(this.warned.add(e),console.warn(`canvas-grid: no renderer registered as "${e}"`)),t}clearCaches(){this.renderers.forEach(e=>e.clearCache?.())}},P=T((e,{x:t=0,y:n=0,width:r,height:i,color:a,alpha:o=1},s,c)=>{let l=Math.trunc(r),u=Math.trunc(i);!a||l<=0||u<=0||o<=0||(e.fillStyle=a,o>=1?e.fillRect(s+t,c+n,l,u):(e.globalAlpha=o,e.fillRect(s+t,c+n,l,u),e.globalAlpha=1))}),F=class e extends O{static defaultOptions={layer:`background`,enableFlashing:!0,background:`#333`,flashInDuration:200,flashOutDuration:400,flashRGB:`214, 144, 13`,borderRightWidth:0,borderRightColor:`#fff`,borderBottomWidth:0,borderBottomColor:`#fff`};constructor(t={}){super({...e.defaultOptions,...t})}get layer(){return this.options.layer}isAnimating({age:e}){return this.options.enableFlashing&&e<=this.options.flashInDuration+this.options.flashOutDuration}getFlashAlpha(e){if(!this.options.enableFlashing)return 0;let{flashInDuration:t,flashOutDuration:n}=this.options,r=e<t?e/t:(t+n-e)/n;return Math.max(0,Math.round(r*1e3)/1e3)}draw({ctx:e,x:t,y:n,width:r,height:i,age:a}){let{background:o,flashRGB:s,borderRightWidth:c,borderRightColor:l,borderBottomWidth:u,borderBottomColor:d}=this.options,f=this.getFlashAlpha(a);D.createElement(E,null,D.createElement(P,{width:r,height:i,color:o}),f>0&&D.createElement(P,{width:r,height:i,color:`rgb(${s})`,alpha:f}),c>0&&D.createElement(P,{x:r-c,width:c,height:i,color:l}),u>0&&D.createElement(P,{y:i-u,width:r,height:u,color:d})).draw(e,t,n)}},I=class e extends k{static defaultOptions={font:`12px monospace`,color:`#fff`,alpha:!0,clearBeforeRender:!0};charWidthCache=new p;canvas=document.createElement(`canvas`);ctx;constructor(t={}){super({...e.defaultOptions,...t}),this.ctx=A(this.canvas,{alpha:this.options.alpha}),this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`}set font(e){this.options.font!==e&&(this.options.font=e)}set color(e){this.options.color=e}clearCache(){super.clearCache(),this.charWidthCache.clear()}cacheKey({value:e=``,height:t=0}){return`${this.options.font}|${this.options.color}|${this.pixelRatio}|${t}|${e}`}getCharacterWidth(e=``){let t=this.charWidthCache.get(e);if(t)return t;this.ctx.font!==this.options.font&&(this.ctx.font=this.options.font,this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`);let n=Math.ceil(this.ctx.measureText(e).width);return this.charWidthCache.set(e,n),n}render({value:e=``,height:t=0}){let{pixelRatio:n}=this;return this.canvas.width=Math.round(this.getCharacterWidth(e)*n),this.canvas.height=Math.round(t*n),this.ctx.setTransform(n,0,0,n,0,0),this.ctx.font=this.options.font,this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`,this.ctx.fillStyle!==this.options.color&&(this.ctx.fillStyle=this.options.color),this.ctx.fillText(e,0,0),this.canvas}},L=class e extends O{static defaultOptions={ellipsis:`…`,cellPadding:{top:3,right:3,bottom:3,left:3},font:`12px monospace`,color:`#fff`,isEllipsisRight:!0};characterRenderer;constructor(t={}){super({...e.defaultOptions,...t}),this.characterRenderer=new I({font:this.options.font,color:this.options.color})}set font(e){this.options.font!==e&&(this.options.font=e,this.characterRenderer.font=e,this.clearCache())}get font(){return this.options.font}set color(e){this.options.color=e,this.characterRenderer.color=e}get color(){return this.options.color}set pixelRatio(e){this.characterRenderer.pixelRatio=e}get pixelRatio(){return this.characterRenderer.pixelRatio}clearCache(){this.characterRenderer.clearCache()}getTextWidth(e){let t=0;for(let n=0;n<e.length;n++){let r=e.charAt(n);t+=this.characterRenderer.getCharacterWidth(r)}return t}paint(e,{value:t=``,x:n=0,y:r=0,width:i=0,height:a=0},o,s){let c=this.characterRenderer.getCharacterWidth(this.options.ellipsis),l=this.options.cellPadding.left+this.options.cellPadding.right,u=i-c-l,d=o+n,f=s+r+this.options.cellPadding.top,p=a-this.options.cellPadding.top-this.options.cellPadding.bottom,m=this.options.cellPadding.left,h=0;for(let n=0;n<t.length;n++){let r=t.charAt(n),i=Math.round(this.characterRenderer.getCharacterWidth(r));if(Math.ceil(h+i)>=u){this.characterRenderer.paint(e,{value:this.options.ellipsis,height:p},d+m,f);break}this.characterRenderer.paint(e,{value:r,height:p},d+m,f),h+=i,m+=i}}element=T((e,t,n,r)=>this.paint(e,t,n,r))},R=class e extends O{static defaultOptions={layer:`header`,background:`#333`,font:`13px monospace`,color:`#fff`,borderRightWidth:1,borderRightColor:`#fff`,borderBottomWidth:1,borderBottomColor:`#fff`};textRenderer;constructor(t={}){super({...e.defaultOptions,...t}),this.textRenderer=new L({font:this.options.font,color:this.options.color})}get layer(){return this.options.layer}set font(e){this.options.font=e,this.textRenderer.font=e}get font(){return this.textRenderer.font}set pixelRatio(e){this.textRenderer.pixelRatio=e}get pixelRatio(){return this.textRenderer.pixelRatio}clearCache(){this.textRenderer.clearCache()}draw({ctx:e,x:t,y:n,width:r,height:i,formatted:a}){if(r===0||i===0)return;let{background:o,borderRightWidth:s,borderRightColor:c,borderBottomWidth:l,borderBottomColor:u}=this.options;D.createElement(E,null,D.createElement(P,{width:r,height:i,color:o}),s>0&&D.createElement(P,{x:r-s,width:s,height:i,color:c}),l>0&&D.createElement(P,{y:i-l,width:r,height:l,color:u}),D.createElement(this.textRenderer.element,{value:a,width:r,height:i})).draw(e,t,n)}},z=class e extends O{static defaultOptions={layer:`content`,font:`12px monospace`,color:`#fff`};renderer;constructor(t={}){super({...e.defaultOptions,...t}),this.renderer=new L({font:this.options.font,color:this.options.color})}get layer(){return this.options.layer}set font(e){this.options.font=e,this.renderer.font=e}get font(){return this.options.font}set color(e){this.options.color=e,this.renderer.color=e}get color(){return this.options.color}set pixelRatio(e){this.renderer.pixelRatio=e}get pixelRatio(){return this.renderer.pixelRatio}clearCache(){this.renderer.clearCache()}draw({ctx:e,x:t,y:n,width:r,height:i,formatted:a,valueChanged:o,forced:s}){(o||s)&&(e.clearRect(t,n,r,i),D.createElement(this.renderer.element,{value:a,width:r,height:i}).draw(e,t,n))}},B={scrollArea:`scrollArea`},V={background:`background`,content:`content`,header:`header`,backgroundRenderer:`background`,valueRenderer:`value`,headerRenderer:`header`},H=`canvas-grid-styles`,U=()=>{if(document.getElementById(H))return;let e=document.createElement(`style`);e.id=H,e.textContent=C,document.head.appendChild(e)},W=class extends HTMLElement{fps=30;_font=`12px monospace`;_headerFont=`13px monospace`;headerHeight=30;cellHeight=20;cellPadding={top:3,right:3,bottom:3,left:3};showRowNumbers=!0;showHeaders=!0;rowNumbersWidth=30;ellipsis=`…`;_backgroundColor=`#333`;_headerBackgroundColor=`#222`;textColor=`#efefef`;dataStore=new x({getRowId:(e,t)=>this.getRowId(e,t),getCellHeight:()=>this.cellHeight});_dataCollection=this.dataStore.rows;cellEntries=new S(2048);getRowId=_;headers=new p;animationFrame=void 0;scrollYOffset=0;prevScrollYOffset=0;scrollXOffset=0;prevScrollXOffset=0;lastRender=new Date().valueOf();lastDraw=this.lastRender;isScrolling=!1;scrollHelper=document.createElement(`div`);scrollArea=this.createScrollArea();layers=[];layersByName=new Map;warnedLayers=new Set;renderers=new N;cellRenderers=[V.backgroundRenderer,V.valueRenderer];cellBackgroundRenderer=new F({layer:V.background,background:this._backgroundColor,enableFlashing:!0,flashInDuration:200,flashOutDuration:400,flashRGB:`214, 144, 13`,borderRightWidth:1,borderRightColor:`#3f3f3f`,borderBottomWidth:1,borderBottomColor:`#3f3f3f`});cellValueRenderer=new z({layer:V.content,font:this._font,color:this.textColor});headerCellRenderer=new R({layer:V.header,font:this._headerFont,color:this.textColor,background:`#222`,borderRightWidth:1,borderRightColor:`#2f2f2f`,borderBottomWidth:1,borderBottomColor:`#2f2f2f`});loadingIndicator;cell;placeholderState={};throttleTransation=(0,d.default)(()=>{let e=this.totalHeight,t=this.dataStore.flush();e!==this.totalHeight&&this.refreshScrollbar(),t&&this.forceDraw(),this.requestRender()},50);agApi;constructor(){super(),this.resize=this.resize.bind(this),this.setValues=this.setValues.bind(this),this.setColumns=this.setColumns.bind(this),this.clearValues=this.clearValues.bind(this),this.updateLastRender=this.updateLastRender.bind(this),this.transaction=this.transaction.bind(this),this.queueUpdate=this.queueUpdate.bind(this),this.addLayer(V.background,{alpha:!1}),this.addLayer(V.content),this.addLayer(V.header),this.registerRenderer(V.backgroundRenderer,this.cellBackgroundRenderer),this.registerRenderer(V.valueRenderer,this.cellValueRenderer),this.registerRenderer(V.headerRenderer,this.headerCellRenderer),this.loadingIndicator=new j({ctx:this.requireLayer(V.header).ctx}),this.cell={ctx:this.requireLayer(V.content).ctx,x:0,y:0,width:0,height:0,row:void 0,column:void 0,value:void 0,formatted:``,updatedAt:void 0,age:1/0,now:0,dt:0,valueChanged:!1,forced:!1,isScrolling:!1,state:{}};let e=this;this.agApi={setRowData:this.setValues,updateRowData:this.transaction,setColumnDefs:this.setColumns,get getRowNodeId(){return e.getRowId},set getRowNodeId(t){typeof t==`function`&&(e.getRowId=t)}}}get canvas(){return this.requireLayer(V.content).canvas}get flashInDuration(){return this.cellBackgroundRenderer.options.flashInDuration}set flashInDuration(e){this.cellBackgroundRenderer.update({flashInDuration:e})}get flashOutDuration(){return this.cellBackgroundRenderer.options.flashOutDuration}set flashOutDuration(e){this.cellBackgroundRenderer.update({flashOutDuration:e})}getLayer(e){return this.layersByName.get(e)}requireLayer(e){let t=this.getLayer(e);if(!t)throw Error(`canvas-grid: no layer named "${e}"`);return t}addLayer(e,{alpha:t=!0,desynchronized:n=!1,before:r}={}){if(this.getLayer(e))throw Error(`canvas-grid: layer "${e}" already exists`);let i=new M(e,{alpha:t,desynchronized:n}),a=r===void 0?void 0:this.requireLayer(r);return this.layers.splice(a?this.layers.indexOf(a):this.layers.length,0,i),this.layersByName.set(e,i),this.scrollArea.parentElement===this&&(this.insertBefore(i.canvas,a?.canvas??this.scrollArea),i.resize(this.clientWidth,this.clientHeight,this.pixelRatio),this.forceDraw()),i}removeLayer(e){let t=this.requireLayer(e);this.layers=this.layers.filter(e=>e!==t),this.layersByName.delete(e),t.canvas.remove()}registerRenderer(e,t){this.renderers.register(e,t)}unregisterRenderer(e){this.renderers.unregister(e)}createScrollArea(){let e=document.createElement(`div`);return e.className=B.scrollArea,e.appendChild(this.scrollHelper),e}set font(e){this._font!==e&&(this._font=e,this.cellValueRenderer.font=e)}get font(){return this._font}set headerFont(e){this._headerFont!==e&&(this._headerFont=e,this.headerCellRenderer.font=e)}get headerFont(){return this._headerFont}connectedCallback(){this.layers.forEach(e=>this.appendChild(e.canvas)),this.appendChild(this.scrollArea),this.updateLastRender(),this.resize(),this.headers.length===0&&this.setColumns(Array(50).fill(1).map((e,t)=>t)),document.addEventListener(`visibilitychange`,this.onVisibilityChange),this.observeSize(),this.pixelRatioQuery=window.matchMedia(`(resolution: ${this.pixelRatio}dppx)`),this.pixelRatioQuery.addEventListener(`change`,this.watchPixelRatio,{once:!0}),document.fonts?document.fonts.addEventListener(`loadingdone`,this.onFontsLoaded):this.fontsTimeout=setTimeout(this.onFontsLoaded,2e3);let e=(0,f.default)(()=>{this.isScrolling=!1},200),t=(0,d.default)((t,n)=>{this.scrollXOffset=Math.round(n),this.scrollYOffset=Math.round(t),e(),this.requestRender()},10);this.scrollArea.addEventListener(`scroll`,()=>{let{scrollTop:e,scrollLeft:n}=this.scrollArea;this.isScrolling=!0,t(e,n)})}fontsTimeout=void 0;onVisibilityChange=()=>{document.visibilityState===`hidden`?this.cancelRender():this.resize()};onFontsLoaded=()=>{this.renderers.clearCaches(),this.resize()};cancelRender(){this.animationFrame!==void 0&&cancelAnimationFrame(this.animationFrame),this.animationFrame=void 0}requestRender(){this.animationFrame??=requestAnimationFrame(()=>this.render())}updateLastRender(){this.lastRender=new Date().valueOf()}render(){this.animationFrame=void 0,new Date().valueOf()-this.lastRender<1e3/this.fps?this.requestRender():(this.draw(),this.updateLastRender())}hasScrolled(){return this.hasXScrolled()||this.hasYScrolled()}hasYScrolled(){return this.prevScrollYOffset!==this.scrollYOffset}hasXScrolled(){return this.prevScrollXOffset!==this.scrollXOffset}isInViewport(e=0,t=0,n=0,r=0){return!(e>this.viewportWidth||t>this.viewportHeight||e+n<0||t+r<0)}describe(e,t,n,r,i){return Object.assign(this.cell,{x:e,y:t,width:n,height:r,row:void 0,column:void 0,value:void 0,formatted:``,updatedAt:void 0,age:1/0,valueChanged:!0,forced:!0,state:this.placeholderState},i)}paint(e){let t=this.renderers.resolve(e),n=t&&this.layersByName.get(t.layer);t&&!n&&!this.warnedLayers.has(t.layer)&&(this.warnedLayers.add(t.layer),console.warn(`canvas-grid: renderer draws on missing layer "${t.layer}"`)),t&&n&&(this.cell.ctx=n.ctx,t.draw(this.cell))}paintAll(e){e.forEach(e=>this.paint(e))}isAnimating(e){return e.some(e=>this.renderers.resolve(e)?.isAnimating?.(this.cell)===!0)}draw(e=!1){let t=new Date().valueOf(),n=e||this.hasScrolled();this.cell.now=t,this.cell.dt=t-this.lastDraw,this.cell.isScrolling=this.isScrolling,this.lastDraw=t;let r=this._dataCollection.length===0;if((e||this.hasXScrolled())&&this.showHeaders&&this.renderHeaders(),e&&this.showRowNumbers&&this._dataCollection.length===0)for(let e=0;e<=200;e++)this.renderRowNumber({index:e},e*this.cellHeight+(this.showHeaders?this.headerHeight:0)-this.scrollYOffset);if(this._dataCollection.length===0){this.loadingIndicator.render();for(let e=0;e<=200;e++)this.headers.forEach(t=>{this.describe(Math.round(t.index*t.width+(this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset),Math.round(e*this.cellHeight+(this.showHeaders?this.headerHeight:0)-this.scrollYOffset),t.width,this.cellHeight,{column:t,valueChanged:!1}),this.paintAll(t.renderers??this.cellRenderers)})}let i=Math.round((this.showHeaders?this.headerHeight:0)-this.scrollYOffset),a=Math.round((this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset),o=this._dataCollection.indexAt(-i),s=this.getVisibleColumns(a);for(let a=o;a<this._dataCollection.length;a++){let o=this._dataCollection.at(a),c=i+this._dataCollection.topOf(a);if(!o||c>=this.viewportHeight)break;let{id:l,slot:u}=o;if((e||this.hasYScrolled())&&this.showRowNumbers&&this.renderRowNumber(o,c),o.height+c>0){let e=this.cellEntries.get(String(l));e||(e={},this.cellEntries.set(String(l),e));let i={id:l,index:o.index};for(let{header:a,id:l,x:d,store:f}of s){let s=f?.get(u);if(s===void 0)continue;let p=e[l]??={value:void 0,formatted:void 0,animating:!1,state:{}},m=p.formatted!==void 0&&p.value===s?p.formatted:a.valueFormatter(s),h=p.formatted!==m,g=f?.getUpdatedAt(u),{cell:_}=this;_.x=d,_.y=c,_.width=a.width,_.height=o.height,_.row=i,_.column=a,_.value=s,_.formatted=m,_.updatedAt=g,_.age=g===void 0?1/0:t-g,_.valueChanged=h,_.forced=n,_.state=p.state;let v=a.renderers??this.cellRenderers,y=this.isAnimating(v);(n||h||y||p.animating)&&this.paintAll(v),p.animating=y,p.value=s,p.formatted=m,r||=y}}}(this.scrollYOffset>0||this.scrollXOffset>0)&&this.renderBrick(),this.prevScrollYOffset=this.scrollYOffset,this.prevScrollXOffset=this.scrollXOffset,r&&this.requestRender()}getVisibleColumns(e){let t=[],n=e;return this.headers.forEach((e,r)=>{this.isInViewport(n,0,e.width,0)&&t.push({header:e,id:String(r),x:n,store:this.dataStore.columns.get(String(r))}),n+=Math.round(e.width)}),t}renderRowNumber(e,t){this.isInViewport(0,t,this.rowNumbersWidth,e.height)&&(this.describe(0,Math.ceil(t),this.rowNumbersWidth,e.height??0,{row:{id:e.index,index:e.index},formatted:`${e.index+1}`}),this.paint(V.headerRenderer))}forceDraw(){this.layers.forEach(e=>e.clear()),this.draw(!0)}viewportWidth=0;viewportHeight=0;get pixelRatio(){return window.devicePixelRatio||1}resize(){this.cancelRender();let{pixelRatio:e}=this;this.viewportWidth=this.clientWidth,this.viewportHeight=this.clientHeight,this.layers.forEach(t=>t.resize(this.clientWidth,this.clientHeight,e)),this.cellValueRenderer.pixelRatio!==e&&this.renderers.clearCaches(),this.cellValueRenderer.pixelRatio=e,this.headerCellRenderer.pixelRatio=e,this.forceDraw()}observeSize(){this.resizeObserver??=new ResizeObserver(()=>this.resize()),this.resizeObserver.observe(this)}watchPixelRatio=()=>{this.pixelRatioQuery?.removeEventListener(`change`,this.watchPixelRatio),this.pixelRatioQuery=window.matchMedia(`(resolution: ${this.pixelRatio}dppx)`),this.pixelRatioQuery.addEventListener(`change`,this.watchPixelRatio,{once:!0}),this.resize()};resizeObserver=void 0;pixelRatioQuery=void 0;getRowData(e){let t=this._dataCollection.get(e);if(!t)return;let n={};return this.dataStore.columns.forEach((e,r)=>{let i=e.get(t.slot);i!==void 0&&(n[r]=i)}),{id:t.id,index:t.index,height:t.height,values:n}}setSortModel(e){this.dataStore.setSortModel(e),this.forceDraw()}getSortModel(){return this.dataStore.getSortModel()}renderBrick(){this.showHeaders&&this.showRowNumbers&&this._headerBackgroundColor&&(this.describe(0,0,this.rowNumbersWidth,this.headerHeight,{}),this.paint(V.headerRenderer))}renderHeaders(){let e=(this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset;this.headers.forEach(t=>{this.isInViewport(e,0,t.width,this.headerHeight)&&(this.describe(e,0,t.width,this.headerHeight,{column:t,formatted:t.displayName??``}),this.paint(t.headerRenderer)),e+=t.width}),this.scrollXOffset+this.rowNumbersWidth>0&&this.renderBrick()}getColumnHeader(e){return this.headers.get(e)}get totalHeight(){return this._dataCollection.totalHeight}refreshScrollbar(){let e=this.totalHeight+this.headerHeight;this.scrollHelper.style.height=`${e}px`;let t=0;this.headers.forEach(e=>{t+=Math.round(e.width)}),this.scrollHelper.style.width=`${t}px`}queueUpdate(e){this.dataStore.queueUpdate(e),this.throttleTransation()}queueRemove(e){this.dataStore.queueRemove(e),this.throttleTransation()}transaction({add:e=[],remove:t=[],update:n=[]}){Array.isArray(e)&&e.forEach(e=>this.queueUpdate(e)),Array.isArray(n)&&n.forEach(e=>this.queueUpdate(e)),Array.isArray(t)&&t.forEach(e=>this.queueRemove(e))}clearValues(){this.dataStore.clear(),this.cellEntries.clear()}formatValue(e,t){return this.headers.get(e)?.valueFormatter(t)??String(t)}setValues(e){this.throttleTransation.cancel(),this.clearValues(),Array.isArray(e)&&(this.dataStore.setRows(e),this.refreshScrollbar(),this.forceDraw())}setColumns(e){if(Array.isArray(e)){this.headers.clear(),this.cellEntries.clear();let t={width:100,valueFormatter:e=>String(e),headerRenderer:V.headerRenderer};e.forEach((e,n)=>{if(typeof e==`string`||typeof e==`number`){let r=e+``;this.headers.set(r,{...t,index:n,id:e,displayName:r})}typeof e==`object`&&this.headers.set(e.id+``,{...t,index:n,...e})}),this.refreshScrollbar(),this.forceDraw()}}disconnectedCallback(){this.cancelRender(),document.removeEventListener(`visibilitychange`,this.onVisibilityChange),this.resizeObserver?.disconnect(),this.pixelRatioQuery?.removeEventListener(`change`,this.watchPixelRatio),document.fonts?.removeEventListener(`loadingdone`,this.onFontsLoaded),clearTimeout(this.fontsTimeout)}};return window&&window.customElements&&(U(),customElements.define(`canvas-grid`,W)),W});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?module.exports=t():typeof define==`function`&&define.amd?define([],t):(e=typeof globalThis<`u`?globalThis:e||self,e.CanvasGrid=t())})(this,function(){var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,t)=>()=>(t||(e((t={exports:{}}).exports,t,t.exports),e=null),t.exports),s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,o)=>(o=n==null?{}:e(i(n)),s(r||!n||!n.__esModule||!a.call(n,`default`)?t(o,`default`,{value:n,enumerable:!0}):o,n)),l=o(((e,t)=>{var n=`Expected a function`,r=NaN,i=/^\s+|\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,o=/^0b[01]+$/i,s=/^0o[0-7]+$/i,c=parseInt,l=typeof global==`object`&&global&&global.Object===Object&&global,u=typeof self==`object`&&self&&self.Object===Object&&self,d=l||u||Function(`return this`)(),f=Object.prototype.toString,p=Math.max,m=Math.min,h=function(){return d.Date.now()};function g(e,t,r){var i,a,o,s,c,l,u=0,d=!1,f=!1,g=!0;if(typeof e!=`function`)throw TypeError(n);t=x(t)||0,v(r)&&(d=!!r.leading,f=`maxWait`in r,o=f?p(x(r.maxWait)||0,t):o,g=`trailing`in r?!!r.trailing:g);function _(t){var n=i,r=a;return i=a=void 0,u=t,s=e.apply(r,n),s}function y(e){return u=e,c=setTimeout(C,t),d?_(e):s}function b(e){var n=e-l,r=e-u,i=t-n;return f?m(i,o-r):i}function S(e){var n=e-l,r=e-u;return l===void 0||n>=t||n<0||f&&r>=o}function C(){var e=h();if(S(e))return w(e);c=setTimeout(C,b(e))}function w(e){return c=void 0,g&&i?_(e):(i=a=void 0,s)}function T(){c!==void 0&&clearTimeout(c),u=0,i=l=a=c=void 0}function E(){return c===void 0?s:w(h())}function D(){var e=h(),n=S(e);if(i=arguments,a=this,l=e,n){if(c===void 0)return y(l);if(f)return c=setTimeout(C,t),_(l)}return c===void 0&&(c=setTimeout(C,t)),s}return D.cancel=T,D.flush=E,D}function _(e,t,r){var i=!0,a=!0;if(typeof e!=`function`)throw TypeError(n);return v(r)&&(i=`leading`in r?!!r.leading:i,a=`trailing`in r?!!r.trailing:a),g(e,t,{leading:i,maxWait:t,trailing:a})}function v(e){var t=typeof e;return!!e&&(t==`object`||t==`function`)}function y(e){return!!e&&typeof e==`object`}function b(e){return typeof e==`symbol`||y(e)&&f.call(e)==`[object Symbol]`}function x(e){if(typeof e==`number`)return e;if(b(e))return r;if(v(e)){var t=typeof e.valueOf==`function`?e.valueOf():e;e=v(t)?t+``:t}if(typeof e!=`string`)return e===0?e:+e;e=e.replace(i,``);var n=o.test(e);return n||s.test(e)?c(e.slice(2),n?2:8):a.test(e)?r:+e}t.exports=_})),u=o(((e,t)=>{var n=NaN,r=/^\s+|\s+$/g,i=/^[-+]0x[0-9a-f]+$/i,a=/^0b[01]+$/i,o=/^0o[0-7]+$/i,s=parseInt,c=typeof global==`object`&&global&&global.Object===Object&&global,l=typeof self==`object`&&self&&self.Object===Object&&self,u=c||l||Function(`return this`)(),d=Object.prototype.toString,f=Math.max,p=Math.min,m=function(){return u.Date.now()};function h(e,t,n){var r,i,a,o,s,c,l=0,u=!1,d=!1,h=!0;if(typeof e!=`function`)throw TypeError(`Expected a function`);t=y(t)||0,g(n)&&(u=!!n.leading,d=`maxWait`in n,a=d?f(y(n.maxWait)||0,t):a,h=`trailing`in n?!!n.trailing:h);function _(t){var n=r,a=i;return r=i=void 0,l=t,o=e.apply(a,n),o}function v(e){return l=e,s=setTimeout(S,t),u?_(e):o}function b(e){var n=e-c,r=e-l,i=t-n;return d?p(i,a-r):i}function x(e){var n=e-c,r=e-l;return c===void 0||n>=t||n<0||d&&r>=a}function S(){var e=m();if(x(e))return C(e);s=setTimeout(S,b(e))}function C(e){return s=void 0,h&&r?_(e):(r=i=void 0,o)}function w(){s!==void 0&&clearTimeout(s),l=0,r=c=i=s=void 0}function T(){return s===void 0?o:C(m())}function E(){var e=m(),n=x(e);if(r=arguments,i=this,c=e,n){if(s===void 0)return v(c);if(d)return s=setTimeout(S,t),_(c)}return s===void 0&&(s=setTimeout(S,t)),o}return E.cancel=w,E.flush=T,E}function g(e){var t=typeof e;return!!e&&(t==`object`||t==`function`)}function _(e){return!!e&&typeof e==`object`}function v(e){return typeof e==`symbol`||_(e)&&d.call(e)==`[object Symbol]`}function y(e){if(typeof e==`number`)return e;if(v(e))return n;if(g(e)){var t=typeof e.valueOf==`function`?e.valueOf():e;e=g(t)?t+``:t}if(typeof e!=`string`)return e===0?e:+e;e=e.replace(r,``);var c=a.test(e);return c||o.test(e)?s(e.slice(2),c?2:8):i.test(e)?n:+e}t.exports=h})),d=c(l()),f=c(u()),p=class{entries=new Map;constructor(){this.get=this.get.bind(this),this.set=this.set.bind(this),this.clear=this.clear.bind(this),this.forEach=this.forEach.bind(this)}get keys(){return Array.from(this.entries.values(),e=>e.key)}get(e){return this.entries.get(String(e))?.value}set(e,t){let n=this.entries.get(String(e));n?n.value=t:this.entries.set(String(e),{key:e,value:t})}add(e){this.set(this.entries.size,e)}delete(e){this.entries.delete(String(e))}clear(e){typeof e==`function`?this.entries.forEach(({key:t,value:n},r)=>{e(n,t)&&this.entries.delete(r)}):this.entries.clear()}forEach(e){this.entries.forEach(({key:t,value:n})=>e(n,t))}get length(){return this.entries.size}},m=class{values;updatedAt=void 0;constructor(e=0){this.values=Array(e).fill(void 0)}get(e){return this.values[e]}getUpdatedAt(e){return this.updatedAt?.[e]||void 0}set(e,t,n){for(;this.values.length<e;)this.values.push(void 0);if(this.values[e]=t,n!==void 0||this.updatedAt!==void 0){for(this.updatedAt??=Array(this.values.length).fill(0);this.updatedAt.length<this.values.length;)this.updatedAt.push(0);this.updatedAt[e]=n??0}}clearSlots(e){e.forEach(e=>{e<this.values.length&&(this.values[e]=void 0),this.updatedAt&&e<this.updatedAt.length&&(this.updatedAt[e]=0)})}compact(e){this.values=Array.from(e,e=>this.values[e]);let{updatedAt:t}=this;t&&(this.updatedAt=Array.from(e,e=>t[e]??0))}},h=1024,g=class{items=[];byId=new Map;tops=[];end=0;nextSlot=0;get length(){return this.items.length}get totalHeight(){return this.end}get slotCount(){return this.nextSlot}get keys(){return this.items.map(e=>e.id)}get(e){return this.byId.get(String(e))}at(e){return this.items[e]}topOf(e){return this.tops[e]??this.end}indexAt(e){if(e<=0)return 0;if(e>=this.end)return this.length;let t=0,n=this.items.length-1;for(;t<n;){let r=Math.ceil((t+n)/2);this.tops[r]<=e?t=r:n=r-1}return t}set(e){let t=String(e.id),n=this.byId.get(t);if(n){let r={id:e.id,height:e.height,index:n.index,slot:n.slot};return this.items[n.index]=r,this.byId.set(t,r),this.reindexFrom(n.index),r}let r=this.items.length,i={id:e.id,height:e.height,index:r,slot:this.nextSlot++};return this.items.push(i),this.byId.set(t,i),this.tops.push(this.end),this.end+=Math.round(e.height),i}removeMany(e){let t=new Set;if(e.forEach(e=>{let n=this.get(e);n&&t.add(n)}),t.size===0)return[];t.forEach(e=>this.byId.delete(String(e.id)));let n=Array.from(t,e=>e.index).sort((e,t)=>e-t);return this.closeGaps(n),this.reindexFrom(n[0]),Array.from(t,e=>e.slot).sort((e,t)=>e-t)}compactSlots(){let e=this.nextSlot-this.items.length;if(e<h||e<=this.items.length)return;let t=Int32Array.from(this.items,e=>e.slot).sort(),n=new Int32Array(this.nextSlot);return t.forEach((e,t)=>{n[e]=t}),this.items.forEach(e=>{e.slot=n[e.slot]}),this.nextSlot=this.items.length,t}sort(e){this.items.sort(e),this.reindexFrom(0)}resort(e,t){let n=new Set;for(let t of e){let e=this.get(t);e&&n.add(e)}if(n.size===0)return;let r=Array.from(n,e=>e.index).sort((e,t)=>e-t);this.closeGaps(r);let i=Array.from(n).sort(t),a=i.map(e=>this.lowerBound(this.items,e,t)),o=this.items.length;i.forEach(e=>this.items.push(e));let{items:s}=this,c=o;for(let e=i.length-1;e>=0;e--){for(let t=c-1;t>=a[e];t--)s[t+e+1]=s[t];s[a[e]+e]=i[e],c=a[e]}this.reindexFrom(Math.min(r[0],a[0]))}clear(){this.items=[],this.byId.clear(),this.tops=[],this.end=0,this.nextSlot=0}closeGaps(e){let{items:t}=this,n=e[0];e.forEach((r,i)=>{let a=e[i+1]??t.length;for(let e=r+1;e<a;e++)t[n++]=t[e]}),t.length=n}lowerBound(e,t,n){let r=0,i=e.length;for(;r<i;){let a=r+i>>>1;n(e[a],t)<0?r=a+1:i=a}return r}reindexFrom(e){this.tops.length=this.items.length;let t=e===0?0:this.tops[e-1]+Math.round(this.items[e-1].height);for(let n=e;n<this.items.length;n++){let e=this.items[n];e.index=n,this.tops[n]=t,t+=Math.round(e.height)}this.end=t}},_=()=>new Date().valueOf(),v=(e,t)=>e.id,y=new Intl.Collator,b=8,x=e=>e==null?3:typeof e==`number`||typeof e==`bigint`?0:typeof e==`string`?1:2,S=(e,t)=>{let n=x(e)-x(t);return n===0?x(e)===3?0:x(e)===0?Number(e)-Number(t):y.compare(String(e),String(t)):n},C=e=>{let t=new Uint8Array(e.length),n=new Float64Array(e.length),r=new Map;e.forEach((e,i)=>{t[i]=x(e),t[i]===0&&(n[i]=Number(e)),(t[i]===1||t[i]===2)&&r.set(String(e),0)});let i=0,a=Array.from(r.keys()).sort(y.compare);return a.forEach((e,t)=>{t>0&&y.compare(a[t-1],e)!==0&&i++,r.set(e,i)}),e.forEach((e,i)=>{(t[i]===1||t[i]===2)&&(n[i]=r.get(String(e))??0)}),{ranks:t,keys:n}},w=class{rows=new g;columns=new Map;pending=[];sortModel=[];sortDirty=!1;sortChanged=new Set;environment;constructor(e){this.environment=e}get hasPending(){return this.pending.length>0}getSortModel(){return this.sortModel}setSortModel(e){this.sortModel=[...e],this.sort()}getValue(e,t){let n=this.rows.get(e);return n?this.columns.get(String(t))?.get(n.slot):void 0}getUpdatedAt(e,t){let n=this.rows.get(e);return n?this.columns.get(String(t))?.getUpdatedAt(n.slot):void 0}queueUpdate(e){this.pending.push({type:`update`,row:e})}queueRemove(e){this.pending.push({type:`remove`,row:e})}clear(){this.rows.clear(),this.columns.clear()}setRows(e){this.pending=[],this.clear(),this.sortChanged.clear(),Object.keys(e[0]??{}).forEach(t=>this.columns.set(t,new m(e.length))),e.forEach((e,t)=>this.addRow(this.environment.getRowId(e,t),e,void 0)),this.sortModel.length>0&&this.sort()}flush(e=_()){let t=this.pending;this.pending=[];let n=[],r=!1,i=()=>{r=this.removeRows(n)||r,n.length=0};return t.forEach(({type:t,row:r})=>{t===`remove`?n.push(this.environment.getRowId(r)):(i(),this.applyUpdate(r,e))}),i(),this.sortDirty?(this.sortChanges(),!0):r}column(e){let t=this.columns.get(e);return t||(t=new m(this.rows.slotCount),this.columns.set(e,t)),t}addRow(e,t,n){let{slot:r}=this.rows.set({id:e,height:this.environment.getCellHeight()}),i=Object.keys(t);for(let e of i)this.column(e).set(r,t[e],n);this.markSortDirty(e,i)}removeRows(e){if(e.length===0)return!1;let t=this.rows.removeMany(e);this.columns.forEach(e=>e.clearSlots(t));let n=this.rows.compactSlots();return n&&this.columns.forEach(e=>e.compact(n)),t.length>0}applyUpdate(e,t){let n=this.environment.getRowId(e),r=this.rows.get(n);r?Object.keys(e).forEach(n=>this.applyValue(r.id,r.slot,n,e[n],t)):this.addRow(n,e,t)}applyValue(e,t,n,r,i){let a=this.column(n),o=a.get(t);o!==r&&(a.set(t,r,o===void 0?void 0:i),this.markSortDirty(e,[n]))}markSortDirty(e,t){this.sortModel.some(({columnId:e})=>t.includes(String(e)))&&(this.sortDirty=!0,this.sortChanged.add(String(e)))}createComparator(e){let t=this.sortModel.map(({columnId:t,direction:n=`asc`,compare:r})=>{let i=this.columns.get(String(t))?.values,a=n===`desc`?-1:1;if(!i)return{order:()=>0,sign:a};if(r)return{order:(e,t)=>r(i[e.slot],i[t.slot]),sign:a};if(!e)return{order:(e,t)=>S(i[e.slot],i[t.slot]),sign:a};let{ranks:o,keys:s}=C(i);return{order:(e,t)=>o[e.slot]-o[t.slot]||(o[e.slot]===3?0:s[e.slot]-s[t.slot])||0,sign:a}});return(e,n)=>{for(let{order:r,sign:i}of t){let t=r(e,n);if(t!==0)return t*i}return e.slot-n.slot}}sort(){this.sortDirty=!1,this.sortChanged.clear(),this.rows.sort(this.createComparator(!0))}sortChanges(){let e=this.sortChanged;e.size*b>=this.rows.length?this.sort():(this.sortDirty=!1,this.sortChanged=new Set,this.rows.resort(e,this.createComparator(!1)))}},T=class{entries=new Map;limit;constructor(e){this.limit=e}get(e){let t=this.entries.get(e);if(t!==void 0)return this.entries.delete(e),this.entries.set(e,t),t}set(e,t){this.entries.delete(e),this.entries.set(e,t);let[n]=this.entries.keys();this.entries.size>this.limit&&n!==void 0&&this.entries.delete(n)}clear(){this.entries.clear()}get size(){return this.entries.size}},E=`canvas-grid{isolation:isolate;display:block;position:relative}canvas-grid,canvas-grid *{box-sizing:border-box;margin:0;padding:0}canvas-grid .drawingLayer{contain:strict;width:100%;height:100%;position:absolute;top:0;left:0}canvas-grid .scrollArea{z-index:10;backface-visibility:hidden;will-change:transform;width:100%;height:100%;position:absolute;top:0;left:0;overflow:auto}canvas-grid .scrollArea>div{will-change:scroll-position, transform}`,D=e=>!!e,O=e=>t=>({draw:(n,r,i)=>e(n,t,r,i)}),k=({x:e=0,y:t=0,children:n})=>({draw:(r,i,a)=>[n].flat(2).filter(D).forEach(n=>n.draw(r,i+e,a+t))}),A={createElement(e,t,...n){return e(Object.assign({},t,{children:n}))}},j=class{options;constructor(e){this.options={...e}}update(e){let t={...this.options};Object.assign(this.options,e),this.optionsUpdated(t)}optionsUpdated(e){}clearCache(){}},M=class extends j{bitmaps=new T(4096);cacheKey(e){}pixelRatio=1;clearCache(){this.bitmaps.clear()}paint(e,t,n,r){let i=this.cacheKey(t),a=(i===void 0?void 0:this.bitmaps.get(i))??this.renderBitmap(t,i);a.width<=0||a.height<=0||e.drawImage(a,n+(t.x??0),r+(t.y??0),a.width/this.pixelRatio,a.height/this.pixelRatio)}element=O((e,t,n,r)=>this.paint(e,t,n,r));renderBitmap(e,t){let n=this.render(e);if(t===void 0||n.width<=0||n.height<=0)return n;let r=document.createElement(`canvas`);return r.width=n.width,r.height=n.height,r.getContext(`2d`)?.drawImage(n,0,0),this.bitmaps.set(t,r),r}},N=(e,t)=>{let n=e.getContext(`2d`,t);if(!n)throw Error(`2D canvas context is not available`);return n},P=class extends j{ctx;offCanvas=document.createElement(`canvas`);offCtx;rotation=Math.PI/180*270;speed=6;count=75;constructor(e){super(e),this.ctx=this.options.ctx,this.offCanvas.width=250,this.offCanvas.height=250,this.offCtx=N(this.offCanvas),this.offCtx.lineWidth=.5,this.offCtx.strokeStyle=`rgba(255, 255, 255, 0.75)`}updateLoader(){this.rotation+=this.speed/100}renderLoader(){this.offCtx.save(),this.offCtx.globalCompositeOperation=`source-over`,this.offCtx.translate(125,125),this.offCtx.rotate(this.rotation);let e=this.count;for(;e--;)this.offCtx.beginPath(),this.offCtx.arc(0,0,e+Math.random()*35,Math.random(),Math.PI/3+Math.random()/12,!1),this.offCtx.stroke();this.offCtx.restore()}render(){this.offCtx.globalCompositeOperation=`destination-out`,this.offCtx.fillStyle=`rgba(255, 255, 255, 0.03)`,this.offCtx.fillRect(0,0,250,250),this.updateLoader(),this.renderLoader();let e=Math.round((this.ctx.canvas.width-this.offCanvas.width)/2),t=Math.round((this.ctx.canvas.height-this.offCanvas.height)/2);return this.ctx.clearRect(e,t,250,250),this.ctx.drawImage(this.offCanvas,e,t),this.offCanvas}},F=class{name;canvas=document.createElement(`canvas`);ctx;constructor(e,{alpha:t=!0,desynchronized:n=!1}={}){this.name=e,this.canvas.className=`drawingLayer ${e}`,this.ctx=N(this.canvas,{alpha:t,desynchronized:n})}width=0;height=0;pixelRatio=1;resize(e,t,n=1){return e===this.width&&t===this.height&&n===this.pixelRatio?!1:(this.width=e,this.height=t,this.pixelRatio=n,this.canvas.width=Math.round(e*n),this.canvas.height=Math.round(t*n),this.ctx.setTransform(n,0,0,n,0,0),!0)}clear(){this.ctx.clearRect(0,0,this.width,this.height)}},I=class{renderers=new Map;warned=new Set;register(e,t){this.renderers.set(e,t),this.warned.delete(e)}unregister(e){this.renderers.delete(e)}get(e){return this.renderers.get(e)}resolve(e){if(typeof e!=`string`)return e;let t=this.renderers.get(e);return!t&&!this.warned.has(e)&&(this.warned.add(e),console.warn(`canvas-grid: no renderer registered as "${e}"`)),t}clearCaches(){this.renderers.forEach(e=>e.clearCache?.())}},L=O((e,{x:t=0,y:n=0,width:r,height:i,color:a,alpha:o=1},s,c)=>{let l=Math.trunc(r),u=Math.trunc(i);!a||l<=0||u<=0||o<=0||(e.fillStyle=a,o>=1?e.fillRect(s+t,c+n,l,u):(e.globalAlpha=o,e.fillRect(s+t,c+n,l,u),e.globalAlpha=1))}),R=class e extends j{static defaultOptions={layer:`background`,enableFlashing:!0,background:`#333`,flashInDuration:200,flashOutDuration:400,flashRGB:`214, 144, 13`,borderRightWidth:0,borderRightColor:`#fff`,borderBottomWidth:0,borderBottomColor:`#fff`};constructor(t={}){super({...e.defaultOptions,...t})}get layer(){return this.options.layer}isAnimating({age:e}){return this.options.enableFlashing&&e<=this.options.flashInDuration+this.options.flashOutDuration}getFlashAlpha(e){if(!this.options.enableFlashing)return 0;let{flashInDuration:t,flashOutDuration:n}=this.options,r=e<t?e/t:(t+n-e)/n;return Math.max(0,Math.round(r*1e3)/1e3)}draw({ctx:e,x:t,y:n,width:r,height:i,age:a}){let{background:o,flashRGB:s,borderRightWidth:c,borderRightColor:l,borderBottomWidth:u,borderBottomColor:d}=this.options,f=this.getFlashAlpha(a);A.createElement(k,null,A.createElement(L,{width:r,height:i,color:o}),f>0&&A.createElement(L,{width:r,height:i,color:`rgb(${s})`,alpha:f}),c>0&&A.createElement(L,{x:r-c,width:c,height:i,color:l}),u>0&&A.createElement(L,{y:i-u,width:r,height:u,color:d})).draw(e,t,n)}},z=class e extends M{static defaultOptions={font:`12px monospace`,color:`#fff`,alpha:!0,clearBeforeRender:!0};charWidthCache=new p;canvas=document.createElement(`canvas`);ctx;constructor(t={}){super({...e.defaultOptions,...t}),this.ctx=N(this.canvas,{alpha:this.options.alpha}),this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`}set font(e){this.options.font!==e&&(this.options.font=e)}set color(e){this.options.color=e}clearCache(){super.clearCache(),this.charWidthCache.clear()}cacheKey({value:e=``,height:t=0}){return`${this.options.font}|${this.options.color}|${this.pixelRatio}|${t}|${e}`}getCharacterWidth(e=``){let t=this.charWidthCache.get(e);if(t)return t;this.ctx.font!==this.options.font&&(this.ctx.font=this.options.font,this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`);let n=Math.ceil(this.ctx.measureText(e).width);return this.charWidthCache.set(e,n),n}render({value:e=``,height:t=0}){let{pixelRatio:n}=this;return this.canvas.width=Math.round(this.getCharacterWidth(e)*n),this.canvas.height=Math.round(t*n),this.ctx.setTransform(n,0,0,n,0,0),this.ctx.font=this.options.font,this.ctx.textAlign=`left`,this.ctx.textBaseline=`top`,this.ctx.fillStyle!==this.options.color&&(this.ctx.fillStyle=this.options.color),this.ctx.fillText(e,0,0),this.canvas}},B=class e extends j{static defaultOptions={ellipsis:`…`,cellPadding:{top:3,right:3,bottom:3,left:3},font:`12px monospace`,color:`#fff`,isEllipsisRight:!0};characterRenderer;constructor(t={}){super({...e.defaultOptions,...t}),this.characterRenderer=new z({font:this.options.font,color:this.options.color})}set font(e){this.options.font!==e&&(this.options.font=e,this.characterRenderer.font=e,this.clearCache())}get font(){return this.options.font}set color(e){this.options.color=e,this.characterRenderer.color=e}get color(){return this.options.color}set pixelRatio(e){this.characterRenderer.pixelRatio=e}get pixelRatio(){return this.characterRenderer.pixelRatio}clearCache(){this.characterRenderer.clearCache()}getTextWidth(e){let t=0;for(let n=0;n<e.length;n++){let r=e.charAt(n);t+=this.characterRenderer.getCharacterWidth(r)}return t}paint(e,{value:t=``,x:n=0,y:r=0,width:i=0,height:a=0},o,s){let c=this.characterRenderer.getCharacterWidth(this.options.ellipsis),l=this.options.cellPadding.left+this.options.cellPadding.right,u=i-c-l,d=o+n,f=s+r+this.options.cellPadding.top,p=a-this.options.cellPadding.top-this.options.cellPadding.bottom,m=this.options.cellPadding.left,h=0;for(let n=0;n<t.length;n++){let r=t.charAt(n),i=Math.round(this.characterRenderer.getCharacterWidth(r));if(Math.ceil(h+i)>=u){this.characterRenderer.paint(e,{value:this.options.ellipsis,height:p},d+m,f);break}this.characterRenderer.paint(e,{value:r,height:p},d+m,f),h+=i,m+=i}}element=O((e,t,n,r)=>this.paint(e,t,n,r))},V=class e extends j{static defaultOptions={layer:`header`,background:`#333`,font:`13px monospace`,color:`#fff`,borderRightWidth:1,borderRightColor:`#fff`,borderBottomWidth:1,borderBottomColor:`#fff`};textRenderer;constructor(t={}){super({...e.defaultOptions,...t}),this.textRenderer=new B({font:this.options.font,color:this.options.color})}get layer(){return this.options.layer}set font(e){this.options.font=e,this.textRenderer.font=e}get font(){return this.textRenderer.font}set pixelRatio(e){this.textRenderer.pixelRatio=e}get pixelRatio(){return this.textRenderer.pixelRatio}clearCache(){this.textRenderer.clearCache()}draw({ctx:e,x:t,y:n,width:r,height:i,formatted:a}){if(r===0||i===0)return;let{background:o,borderRightWidth:s,borderRightColor:c,borderBottomWidth:l,borderBottomColor:u}=this.options;A.createElement(k,null,A.createElement(L,{width:r,height:i,color:o}),s>0&&A.createElement(L,{x:r-s,width:s,height:i,color:c}),l>0&&A.createElement(L,{y:i-l,width:r,height:l,color:u}),A.createElement(this.textRenderer.element,{value:a,width:r,height:i})).draw(e,t,n)}},H=class e extends j{static defaultOptions={layer:`content`,font:`12px monospace`,color:`#fff`};renderer;constructor(t={}){super({...e.defaultOptions,...t}),this.renderer=new B({font:this.options.font,color:this.options.color})}get layer(){return this.options.layer}set font(e){this.options.font=e,this.renderer.font=e}get font(){return this.options.font}set color(e){this.options.color=e,this.renderer.color=e}get color(){return this.options.color}set pixelRatio(e){this.renderer.pixelRatio=e}get pixelRatio(){return this.renderer.pixelRatio}clearCache(){this.renderer.clearCache()}draw({ctx:e,x:t,y:n,width:r,height:i,formatted:a,valueChanged:o,forced:s}){(o||s)&&(e.clearRect(t,n,r,i),A.createElement(this.renderer.element,{value:a,width:r,height:i}).draw(e,t,n))}},U={scrollArea:`scrollArea`},W={background:`background`,content:`content`,header:`header`,backgroundRenderer:`background`,valueRenderer:`value`,headerRenderer:`header`},G=`canvas-grid-styles`,K=()=>{if(document.getElementById(G))return;let e=document.createElement(`style`);e.id=G,e.textContent=E,document.head.appendChild(e)},q=class extends HTMLElement{fps=30;_font=`12px monospace`;_headerFont=`13px monospace`;headerHeight=30;cellHeight=20;cellPadding={top:3,right:3,bottom:3,left:3};showRowNumbers=!0;showHeaders=!0;rowNumbersWidth=30;ellipsis=`…`;_backgroundColor=`#333`;_headerBackgroundColor=`#222`;textColor=`#efefef`;dataStore=new w({getRowId:(e,t)=>this.getRowId(e,t),getCellHeight:()=>this.cellHeight});_dataCollection=this.dataStore.rows;cellEntries=new T(2048);getRowId=v;headers=new p;animationFrame=void 0;scrollYOffset=0;prevScrollYOffset=0;scrollXOffset=0;prevScrollXOffset=0;lastRender=new Date().valueOf();lastDraw=this.lastRender;isScrolling=!1;scrollHelper=document.createElement(`div`);scrollArea=this.createScrollArea();layers=[];layersByName=new Map;warnedLayers=new Set;renderers=new I;cellRenderers=[W.backgroundRenderer,W.valueRenderer];cellBackgroundRenderer=new R({layer:W.background,background:this._backgroundColor,enableFlashing:!0,flashInDuration:200,flashOutDuration:400,flashRGB:`214, 144, 13`,borderRightWidth:1,borderRightColor:`#3f3f3f`,borderBottomWidth:1,borderBottomColor:`#3f3f3f`});cellValueRenderer=new H({layer:W.content,font:this._font,color:this.textColor});headerCellRenderer=new V({layer:W.header,font:this._headerFont,color:this.textColor,background:`#222`,borderRightWidth:1,borderRightColor:`#2f2f2f`,borderBottomWidth:1,borderBottomColor:`#2f2f2f`});loadingIndicator;cell;placeholderState={};throttleTransation=(0,d.default)(()=>{let e=this.totalHeight,t=this.dataStore.flush();e!==this.totalHeight&&this.refreshScrollbar(),t&&this.forceDraw(),this.requestRender()},50);agApi;constructor(){super(),this.resize=this.resize.bind(this),this.setValues=this.setValues.bind(this),this.setColumns=this.setColumns.bind(this),this.clearValues=this.clearValues.bind(this),this.updateLastRender=this.updateLastRender.bind(this),this.transaction=this.transaction.bind(this),this.queueUpdate=this.queueUpdate.bind(this),this.addLayer(W.background,{alpha:!1}),this.addLayer(W.content),this.addLayer(W.header),this.registerRenderer(W.backgroundRenderer,this.cellBackgroundRenderer),this.registerRenderer(W.valueRenderer,this.cellValueRenderer),this.registerRenderer(W.headerRenderer,this.headerCellRenderer),this.loadingIndicator=new P({ctx:this.requireLayer(W.header).ctx}),this.cell={ctx:this.requireLayer(W.content).ctx,x:0,y:0,width:0,height:0,row:void 0,column:void 0,value:void 0,formatted:``,updatedAt:void 0,age:1/0,now:0,dt:0,valueChanged:!1,forced:!1,isScrolling:!1,state:{}};let e=this;this.agApi={setRowData:this.setValues,updateRowData:this.transaction,setColumnDefs:this.setColumns,get getRowNodeId(){return e.getRowId},set getRowNodeId(t){typeof t==`function`&&(e.getRowId=t)}}}get canvas(){return this.requireLayer(W.content).canvas}get flashInDuration(){return this.cellBackgroundRenderer.options.flashInDuration}set flashInDuration(e){this.cellBackgroundRenderer.update({flashInDuration:e})}get flashOutDuration(){return this.cellBackgroundRenderer.options.flashOutDuration}set flashOutDuration(e){this.cellBackgroundRenderer.update({flashOutDuration:e})}getLayer(e){return this.layersByName.get(e)}requireLayer(e){let t=this.getLayer(e);if(!t)throw Error(`canvas-grid: no layer named "${e}"`);return t}addLayer(e,{alpha:t=!0,desynchronized:n=!1,before:r}={}){if(this.getLayer(e))throw Error(`canvas-grid: layer "${e}" already exists`);let i=new F(e,{alpha:t,desynchronized:n}),a=r===void 0?void 0:this.requireLayer(r);return this.layers.splice(a?this.layers.indexOf(a):this.layers.length,0,i),this.layersByName.set(e,i),this.scrollArea.parentElement===this&&(this.insertBefore(i.canvas,a?.canvas??this.scrollArea),i.resize(this.clientWidth,this.clientHeight,this.pixelRatio),this.forceDraw()),i}removeLayer(e){let t=this.requireLayer(e);this.layers=this.layers.filter(e=>e!==t),this.layersByName.delete(e),t.canvas.remove()}registerRenderer(e,t){this.renderers.register(e,t)}unregisterRenderer(e){this.renderers.unregister(e)}createScrollArea(){let e=document.createElement(`div`);return e.className=U.scrollArea,e.appendChild(this.scrollHelper),e}set font(e){this._font!==e&&(this._font=e,this.cellValueRenderer.font=e)}get font(){return this._font}set headerFont(e){this._headerFont!==e&&(this._headerFont=e,this.headerCellRenderer.font=e)}get headerFont(){return this._headerFont}connectedCallback(){this.layers.forEach(e=>this.appendChild(e.canvas)),this.appendChild(this.scrollArea),this.updateLastRender(),this.resize(),this.headers.length===0&&this.setColumns(Array(50).fill(1).map((e,t)=>t)),document.addEventListener(`visibilitychange`,this.onVisibilityChange),this.observeSize(),this.pixelRatioQuery=window.matchMedia(`(resolution: ${this.pixelRatio}dppx)`),this.pixelRatioQuery.addEventListener(`change`,this.watchPixelRatio,{once:!0}),document.fonts?document.fonts.addEventListener(`loadingdone`,this.onFontsLoaded):this.fontsTimeout=setTimeout(this.onFontsLoaded,2e3);let e=(0,f.default)(()=>{this.isScrolling=!1},200),t=(0,d.default)((t,n)=>{this.scrollXOffset=Math.round(n),this.scrollYOffset=Math.round(t),e(),this.requestRender()},10);this.scrollArea.addEventListener(`scroll`,()=>{let{scrollTop:e,scrollLeft:n}=this.scrollArea;this.isScrolling=!0,t(e,n)})}fontsTimeout=void 0;onVisibilityChange=()=>{document.visibilityState===`hidden`?this.cancelRender():this.resize()};onFontsLoaded=()=>{this.renderers.clearCaches(),this.resize()};cancelRender(){this.animationFrame!==void 0&&cancelAnimationFrame(this.animationFrame),this.animationFrame=void 0}requestRender(){this.animationFrame??=requestAnimationFrame(()=>this.render())}updateLastRender(){this.lastRender=new Date().valueOf()}render(){this.animationFrame=void 0,new Date().valueOf()-this.lastRender<1e3/this.fps?this.requestRender():(this.draw(),this.updateLastRender())}hasScrolled(){return this.hasXScrolled()||this.hasYScrolled()}hasYScrolled(){return this.prevScrollYOffset!==this.scrollYOffset}hasXScrolled(){return this.prevScrollXOffset!==this.scrollXOffset}isInViewport(e=0,t=0,n=0,r=0){return!(e>this.viewportWidth||t>this.viewportHeight||e+n<0||t+r<0)}describe(e,t,n,r,i){return Object.assign(this.cell,{x:e,y:t,width:n,height:r,row:void 0,column:void 0,value:void 0,formatted:``,updatedAt:void 0,age:1/0,valueChanged:!0,forced:!0,state:this.placeholderState},i)}paint(e){let t=this.renderers.resolve(e),n=t&&this.layersByName.get(t.layer);t&&!n&&!this.warnedLayers.has(t.layer)&&(this.warnedLayers.add(t.layer),console.warn(`canvas-grid: renderer draws on missing layer "${t.layer}"`)),t&&n&&(this.cell.ctx=n.ctx,t.draw(this.cell))}paintAll(e){e.forEach(e=>this.paint(e))}isAnimating(e){return e.some(e=>this.renderers.resolve(e)?.isAnimating?.(this.cell)===!0)}draw(e=!1){let t=new Date().valueOf(),n=e||this.hasScrolled();this.cell.now=t,this.cell.dt=t-this.lastDraw,this.cell.isScrolling=this.isScrolling,this.lastDraw=t;let r=this._dataCollection.length===0;if((e||this.hasXScrolled())&&this.showHeaders&&this.renderHeaders(),e&&this.showRowNumbers&&this._dataCollection.length===0)for(let e=0;e<=200;e++)this.renderRowNumber({index:e},e*this.cellHeight+(this.showHeaders?this.headerHeight:0)-this.scrollYOffset);if(this._dataCollection.length===0){this.loadingIndicator.render();for(let e=0;e<=200;e++)this.headers.forEach(t=>{this.describe(Math.round(t.index*t.width+(this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset),Math.round(e*this.cellHeight+(this.showHeaders?this.headerHeight:0)-this.scrollYOffset),t.width,this.cellHeight,{column:t,valueChanged:!1}),this.paintAll(t.renderers??this.cellRenderers)})}let i=Math.round((this.showHeaders?this.headerHeight:0)-this.scrollYOffset),a=Math.round((this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset),o=this._dataCollection.indexAt(-i),s=this.getVisibleColumns(a);for(let a=o;a<this._dataCollection.length;a++){let o=this._dataCollection.at(a),c=i+this._dataCollection.topOf(a);if(!o||c>=this.viewportHeight)break;let{id:l,slot:u}=o;if((e||this.hasYScrolled())&&this.showRowNumbers&&this.renderRowNumber(o,c),o.height+c>0){let e=this.cellEntries.get(String(l));e||(e={},this.cellEntries.set(String(l),e));let i={id:l,index:o.index};for(let{header:a,id:l,x:d,store:f}of s){let s=f?.get(u);if(s===void 0)continue;let p=e[l]??={value:void 0,formatted:void 0,animating:!1,state:{}},m=p.formatted!==void 0&&p.value===s?p.formatted:a.valueFormatter(s),h=p.formatted!==m,g=f?.getUpdatedAt(u),{cell:_}=this;_.x=d,_.y=c,_.width=a.width,_.height=o.height,_.row=i,_.column=a,_.value=s,_.formatted=m,_.updatedAt=g,_.age=g===void 0?1/0:t-g,_.valueChanged=h,_.forced=n,_.state=p.state;let v=a.renderers??this.cellRenderers,y=this.isAnimating(v);(n||h||y||p.animating)&&this.paintAll(v),p.animating=y,p.value=s,p.formatted=m,r||=y}}}(this.scrollYOffset>0||this.scrollXOffset>0)&&this.renderBrick(),this.prevScrollYOffset=this.scrollYOffset,this.prevScrollXOffset=this.scrollXOffset,r&&this.requestRender()}getVisibleColumns(e){let t=[],n=e;return this.headers.forEach((e,r)=>{this.isInViewport(n,0,e.width,0)&&t.push({header:e,id:String(r),x:n,store:this.dataStore.columns.get(String(r))}),n+=Math.round(e.width)}),t}renderRowNumber(e,t){this.isInViewport(0,t,this.rowNumbersWidth,e.height)&&(this.describe(0,Math.ceil(t),this.rowNumbersWidth,e.height??0,{row:{id:e.index,index:e.index},formatted:`${e.index+1}`}),this.paint(W.headerRenderer))}forceDraw(){this.layers.forEach(e=>e.clear()),this.draw(!0)}viewportWidth=0;viewportHeight=0;get pixelRatio(){return window.devicePixelRatio||1}resize(){this.cancelRender();let{pixelRatio:e}=this;this.viewportWidth=this.clientWidth,this.viewportHeight=this.clientHeight,this.layers.forEach(t=>t.resize(this.clientWidth,this.clientHeight,e)),this.cellValueRenderer.pixelRatio!==e&&this.renderers.clearCaches(),this.cellValueRenderer.pixelRatio=e,this.headerCellRenderer.pixelRatio=e,this.forceDraw()}observeSize(){this.resizeObserver??=new ResizeObserver(()=>this.resize()),this.resizeObserver.observe(this)}watchPixelRatio=()=>{this.pixelRatioQuery?.removeEventListener(`change`,this.watchPixelRatio),this.pixelRatioQuery=window.matchMedia(`(resolution: ${this.pixelRatio}dppx)`),this.pixelRatioQuery.addEventListener(`change`,this.watchPixelRatio,{once:!0}),this.resize()};resizeObserver=void 0;pixelRatioQuery=void 0;getRowData(e){let t=this._dataCollection.get(e);if(!t)return;let n={};return this.dataStore.columns.forEach((e,r)=>{let i=e.get(t.slot);i!==void 0&&(n[r]=i)}),{id:t.id,index:t.index,height:t.height,values:n}}setSortModel(e){this.dataStore.setSortModel(e),this.forceDraw()}getSortModel(){return this.dataStore.getSortModel()}renderBrick(){this.showHeaders&&this.showRowNumbers&&this._headerBackgroundColor&&(this.describe(0,0,this.rowNumbersWidth,this.headerHeight,{}),this.paint(W.headerRenderer))}renderHeaders(){let e=(this.showRowNumbers?this.rowNumbersWidth:0)-this.scrollXOffset;this.headers.forEach(t=>{this.isInViewport(e,0,t.width,this.headerHeight)&&(this.describe(e,0,t.width,this.headerHeight,{column:t,formatted:t.displayName??``}),this.paint(t.headerRenderer)),e+=t.width}),this.scrollXOffset+this.rowNumbersWidth>0&&this.renderBrick()}getColumnHeader(e){return this.headers.get(e)}get totalHeight(){return this._dataCollection.totalHeight}refreshScrollbar(){let e=this.totalHeight+this.headerHeight;this.scrollHelper.style.height=`${e}px`;let t=0;this.headers.forEach(e=>{t+=Math.round(e.width)}),this.scrollHelper.style.width=`${t}px`}queueUpdate(e){this.dataStore.queueUpdate(e),this.throttleTransation()}queueRemove(e){this.dataStore.queueRemove(e),this.throttleTransation()}transaction({add:e=[],remove:t=[],update:n=[]}){Array.isArray(e)&&e.forEach(e=>this.queueUpdate(e)),Array.isArray(n)&&n.forEach(e=>this.queueUpdate(e)),Array.isArray(t)&&t.forEach(e=>this.queueRemove(e))}clearValues(){this.dataStore.clear(),this.cellEntries.clear()}formatValue(e,t){return this.headers.get(e)?.valueFormatter(t)??String(t)}setValues(e){this.throttleTransation.cancel(),this.clearValues(),Array.isArray(e)&&(this.dataStore.setRows(e),this.refreshScrollbar(),this.forceDraw())}setColumns(e){if(Array.isArray(e)){this.headers.clear(),this.cellEntries.clear();let t={width:100,valueFormatter:e=>String(e),headerRenderer:W.headerRenderer};e.forEach((e,n)=>{if(typeof e==`string`||typeof e==`number`){let r=e+``;this.headers.set(r,{...t,index:n,id:e,displayName:r})}typeof e==`object`&&this.headers.set(e.id+``,{...t,index:n,...e})}),this.refreshScrollbar(),this.forceDraw()}}disconnectedCallback(){this.cancelRender(),document.removeEventListener(`visibilitychange`,this.onVisibilityChange),this.resizeObserver?.disconnect(),this.pixelRatioQuery?.removeEventListener(`change`,this.watchPixelRatio),document.fonts?.removeEventListener(`loadingdone`,this.onFontsLoaded),clearTimeout(this.fontsTimeout)}};return window&&window.customElements&&(K(),customElements.define(`canvas-grid`,q)),q});
2
2
  //# sourceMappingURL=web-component.js.map