@koeroesi86/canvas-grid 26.11.2 → 26.11.4

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.
@@ -194,6 +194,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
194
194
  }
195
195
  }, m = class {
196
196
  values;
197
+ version = 0;
197
198
  updatedAt = void 0;
198
199
  constructor(e = 0) {
199
200
  this.values = Array(e).fill(void 0);
@@ -205,38 +206,42 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
205
206
  return this.updatedAt?.[e] || void 0;
206
207
  }
207
208
  set(e, t, n) {
208
- for (; this.values.length < e;) this.values.push(void 0);
209
+ for (this.version++; this.values.length < e;) this.values.push(void 0);
209
210
  if (this.values[e] = t, n !== void 0 || this.updatedAt !== void 0) {
210
211
  for (this.updatedAt ??= Array(this.values.length).fill(0); this.updatedAt.length < this.values.length;) this.updatedAt.push(0);
211
212
  this.updatedAt[e] = n ?? 0;
212
213
  }
213
214
  }
214
- removeSlots(e) {
215
- if (e.length === 0) return;
216
- let t = (t) => {
217
- let n = 0, r = 0;
218
- for (let i = 0; i < t.length; i++) r < e.length && e[r] === i ? r++ : t[n++] = t[i];
219
- t.length = n;
220
- };
221
- t(this.values), this.updatedAt && t(this.updatedAt);
215
+ clearSlots(e) {
216
+ this.version++, e.forEach((e) => {
217
+ e < this.values.length && (this.values[e] = void 0), this.updatedAt && e < this.updatedAt.length && (this.updatedAt[e] = 0);
218
+ });
219
+ }
220
+ compact(e) {
221
+ this.version++, this.values = Array.from(e, (e) => this.values[e]);
222
+ let { updatedAt: t } = this;
223
+ t && (this.updatedAt = Array.from(e, (e) => t[e] ?? 0));
222
224
  }
223
- }, h = class {
225
+ }, h = 1024, g = class {
224
226
  items = [];
225
- indexById = /* @__PURE__ */ new Map();
227
+ byId = /* @__PURE__ */ new Map();
226
228
  tops = [];
227
229
  end = 0;
230
+ nextSlot = 0;
228
231
  get length() {
229
232
  return this.items.length;
230
233
  }
231
234
  get totalHeight() {
232
235
  return this.end;
233
236
  }
237
+ get slotCount() {
238
+ return this.nextSlot;
239
+ }
234
240
  get keys() {
235
241
  return this.items.map((e) => e.id);
236
242
  }
237
243
  get(e) {
238
- let t = this.indexById.get(String(e));
239
- return t === void 0 ? void 0 : this.items[t];
244
+ return this.byId.get(String(e));
240
245
  }
241
246
  at(e) {
242
247
  return this.items[e];
@@ -255,21 +260,23 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
255
260
  return t;
256
261
  }
257
262
  set(e) {
258
- let t = this.indexById.get(String(e.id));
259
- if (t !== void 0) {
260
- this.items[t] = {
261
- ...e,
262
- index: t,
263
- slot: this.items[t].slot
264
- }, this.reindexFrom(t);
265
- return;
263
+ let t = String(e.id), n = this.byId.get(t);
264
+ if (n) {
265
+ let r = {
266
+ id: e.id,
267
+ height: e.height,
268
+ index: n.index,
269
+ slot: n.slot
270
+ };
271
+ return this.items[n.index] = r, this.byId.set(t, r), this.reindexFrom(n.index), r;
266
272
  }
267
- let n = this.items.length;
268
- this.items.push({
269
- ...e,
270
- index: n,
271
- slot: n
272
- }), this.indexById.set(String(e.id), n), this.tops.push(this.end), this.end += Math.round(e.height);
273
+ let r = this.items.length, i = {
274
+ id: e.id,
275
+ height: e.height,
276
+ index: r,
277
+ slot: this.nextSlot++
278
+ };
279
+ return this.items.push(i), this.byId.set(t, i), this.tops.push(this.end), this.end += Math.round(e.height), i;
273
280
  }
274
281
  removeMany(e) {
275
282
  let t = /* @__PURE__ */ new Set();
@@ -277,38 +284,100 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
277
284
  let n = this.get(e);
278
285
  n && t.add(n);
279
286
  }), t.size === 0) return [];
280
- 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), Infinity);
281
- t.forEach((e) => this.indexById.delete(String(e.id))), this.items = this.items.filter((e) => !t.has(e));
282
- let i = new Int32Array(this.items.length + n.length);
283
- for (let e = 0, t = 0; e < i.length; e++) n[t] === e && t++, i[e] = t;
284
- return this.items.forEach((e) => {
285
- e.slot -= i[e.slot];
286
- }), this.reindexFrom(r), n;
287
+ t.forEach((e) => this.byId.delete(String(e.id)));
288
+ let n = Array.from(t, (e) => e.index).sort((e, t) => e - t);
289
+ return this.closeGaps(n), this.reindexFrom(n[0]), Array.from(t, (e) => e.slot).sort((e, t) => e - t);
290
+ }
291
+ compactSlots() {
292
+ let e = this.nextSlot - this.items.length;
293
+ if (e < h || e <= this.items.length) return;
294
+ let t = Int32Array.from(this.items, (e) => e.slot).sort(), n = new Int32Array(this.nextSlot);
295
+ return t.forEach((e, t) => {
296
+ n[e] = t;
297
+ }), this.items.forEach((e) => {
298
+ e.slot = n[e.slot];
299
+ }), this.nextSlot = this.items.length, t;
287
300
  }
288
301
  sort(e) {
289
302
  this.items.sort(e), this.reindexFrom(0);
290
303
  }
304
+ resort(e, t) {
305
+ let n = /* @__PURE__ */ new Set();
306
+ for (let t of e) {
307
+ let e = this.get(t);
308
+ e && n.add(e);
309
+ }
310
+ if (n.size === 0) return;
311
+ let r = Array.from(n, (e) => e.index).sort((e, t) => e - t);
312
+ this.closeGaps(r);
313
+ let i = Array.from(n).sort(t), a = i.map((e) => this.lowerBound(this.items, e, t)), o = this.items.length;
314
+ i.forEach((e) => this.items.push(e));
315
+ let { items: s } = this, c = o;
316
+ for (let e = i.length - 1; e >= 0; e--) {
317
+ for (let t = c - 1; t >= a[e]; t--) s[t + e + 1] = s[t];
318
+ s[a[e] + e] = i[e], c = a[e];
319
+ }
320
+ this.reindexFrom(Math.min(r[0], a[0]));
321
+ }
291
322
  clear() {
292
- this.items = [], this.indexById.clear(), this.tops = [], this.end = 0;
323
+ this.items = [], this.byId.clear(), this.tops = [], this.end = 0, this.nextSlot = 0;
324
+ }
325
+ closeGaps(e) {
326
+ let { items: t } = this, n = e[0];
327
+ e.forEach((r, i) => {
328
+ let a = e[i + 1] ?? t.length;
329
+ for (let e = r + 1; e < a; e++) t[n++] = t[e];
330
+ }), t.length = n;
331
+ }
332
+ lowerBound(e, t, n) {
333
+ let r = 0, i = e.length;
334
+ for (; r < i;) {
335
+ let a = r + i >>> 1;
336
+ n(e[a], t) < 0 ? r = a + 1 : i = a;
337
+ }
338
+ return r;
293
339
  }
294
340
  reindexFrom(e) {
295
341
  this.tops.length = this.items.length;
296
342
  let t = e === 0 ? 0 : this.tops[e - 1] + Math.round(this.items[e - 1].height);
297
343
  for (let n = e; n < this.items.length; n++) {
298
344
  let e = this.items[n];
299
- e.index = n, this.indexById.set(String(e.id), n), this.tops[n] = t, t += Math.round(e.height);
345
+ e.index = n, this.tops[n] = t, t += Math.round(e.height);
300
346
  }
301
347
  this.end = t;
302
348
  }
303
- }, g = () => (/* @__PURE__ */ 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) => {
304
- let n = y(e) - y(t);
305
- return n === 0 ? y(e) === 3 ? 0 : y(e) === 0 ? Number(e) - Number(t) : v.compare(String(e), String(t)) : n;
306
- }, x = class {
307
- rows = new h();
349
+ }, _ = () => (/* @__PURE__ */ 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) => {
350
+ let n = x(e) - x(t);
351
+ return n === 0 ? x(e) === 3 ? 0 : x(e) === 0 ? Number(e) - Number(t) : y.compare(String(e), String(t)) : n;
352
+ }, C = (e) => {
353
+ let t = new Uint8Array(e.length), n = new Float64Array(e.length), r = new Int32Array(e.length), i = /* @__PURE__ */ new Map();
354
+ for (let a = 0; a < e.length; a++) {
355
+ let o = e[a], s = x(o);
356
+ if (t[a] = s, s === 0) {
357
+ n[a] = Number(o);
358
+ continue;
359
+ }
360
+ if (s !== 1 && s !== 2) continue;
361
+ let c = String(o), l = i.get(c);
362
+ l === void 0 && (l = i.size, i.set(c, l)), r[a] = l;
363
+ }
364
+ let a = new Float64Array(i.size), o = Array.from(i.keys()).sort(y.compare), s = 0;
365
+ o.forEach((e, t) => {
366
+ t > 0 && y.compare(o[t - 1], e) !== 0 && s++, a[i.get(e) ?? 0] = s;
367
+ });
368
+ for (let i = 0; i < e.length; i++) (t[i] === 1 || t[i] === 2) && (n[i] = a[r[i]]);
369
+ return {
370
+ ranks: t,
371
+ keys: n
372
+ };
373
+ }, w = class {
374
+ rows = new g();
308
375
  columns = /* @__PURE__ */ new Map();
309
376
  pending = [];
310
377
  sortModel = [];
311
378
  sortDirty = !1;
379
+ sortChanged = /* @__PURE__ */ new Set();
380
+ defaultKeys = /* @__PURE__ */ new WeakMap();
312
381
  environment;
313
382
  constructor(e) {
314
383
  this.environment = e;
@@ -346,9 +415,9 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
346
415
  this.rows.clear(), this.columns.clear();
347
416
  }
348
417
  setRows(e) {
349
- this.pending = [], this.clear(), e.forEach((e, t) => this.addRow(this.environment.getRowId(e, t), e, void 0)), this.sort();
418
+ 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();
350
419
  }
351
- flush(e = g()) {
420
+ flush(e = _()) {
352
421
  let t = this.pending;
353
422
  this.pending = [];
354
423
  let n = [], r = !1, i = () => {
@@ -356,52 +425,92 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
356
425
  };
357
426
  return t.forEach(({ type: t, row: r }) => {
358
427
  t === "remove" ? n.push(this.environment.getRowId(r)) : (i(), this.applyUpdate(r, e));
359
- }), i(), this.sortDirty ? (this.sort(), !0) : r;
428
+ }), i(), this.sortDirty ? (this.sortChanges(), !0) : r;
360
429
  }
361
430
  column(e) {
362
431
  let t = this.columns.get(e);
363
- return t || (t = new m(this.rows.length), this.columns.set(e, t)), t;
432
+ return t || (t = new m(this.rows.slotCount), this.columns.set(e, t)), t;
364
433
  }
365
434
  addRow(e, t, n) {
366
- this.rows.set({
435
+ let { slot: r } = this.rows.set({
367
436
  id: e,
368
437
  height: this.environment.getCellHeight()
369
- });
370
- let r = this.rows.get(e)?.slot ?? 0;
371
- Object.keys(t).forEach((e) => this.column(e).set(r, t[e], n)), this.markSortDirty(Object.keys(t));
438
+ }), i = Object.keys(t);
439
+ for (let e of i) this.column(e).set(r, t[e], n);
440
+ this.markSortDirty(e, i);
372
441
  }
373
442
  removeRows(e) {
374
443
  if (e.length === 0) return !1;
375
444
  let t = this.rows.removeMany(e);
376
- return this.columns.forEach((e) => e.removeSlots(t)), t.length > 0;
445
+ this.columns.forEach((e) => e.clearSlots(t));
446
+ let n = this.rows.compactSlots();
447
+ return n && this.columns.forEach((e) => e.compact(n)), t.length > 0;
377
448
  }
378
449
  applyUpdate(e, t) {
379
450
  let n = this.environment.getRowId(e), r = this.rows.get(n);
380
- r ? Object.keys(e).forEach((n) => this.applyValue(r.slot, n, e[n], t)) : this.addRow(n, e, t);
381
- }
382
- applyValue(e, t, n, r) {
383
- let i = this.column(t), a = i.get(e);
384
- a !== n && (i.set(e, n, a === void 0 ? void 0 : r), this.markSortDirty([t]));
385
- }
386
- markSortDirty(e) {
387
- this.sortModel.some(({ columnId: t }) => e.includes(String(t))) && (this.sortDirty = !0);
451
+ r ? Object.keys(e).forEach((n) => this.applyValue(r.id, r.slot, n, e[n], t)) : this.addRow(n, e, t);
452
+ }
453
+ applyValue(e, t, n, r, i) {
454
+ let a = this.column(n), o = a.get(t);
455
+ o !== r && (a.set(t, r, o === void 0 ? void 0 : i), this.markSortDirty(e, [n]));
456
+ }
457
+ markSortDirty(e, t) {
458
+ this.sortModel.some(({ columnId: e }) => t.includes(String(e))) && (this.sortDirty = !0, this.sortChanged.add(String(e)));
459
+ }
460
+ defaultKeyOf(e) {
461
+ let t = this.defaultKeys.get(e);
462
+ if (t && t.version === e.version) return t.key;
463
+ let n = C(e.values);
464
+ return this.defaultKeys.set(e, {
465
+ version: e.version,
466
+ key: n
467
+ }), n;
468
+ }
469
+ createComparator(e) {
470
+ let [t, ...n] = this.sortModel, r = t && !t.compare && e && n.length === 0 ? this.columns.get(String(t.columnId)) : void 0;
471
+ if (r) {
472
+ let { ranks: e, keys: n } = this.defaultKeyOf(r), i = t.direction === "desc" ? -1 : 1;
473
+ return (t, r) => {
474
+ let a = e[t.slot] - e[r.slot] || n[t.slot] - n[r.slot] || 0;
475
+ return a === 0 ? t.slot - r.slot : a * i;
476
+ };
477
+ }
478
+ let i = this.sortModel.map(({ columnId: t, direction: n = "asc", compare: r }) => {
479
+ let i = this.columns.get(String(t)), a = i?.values, o = n === "desc" ? -1 : 1;
480
+ if (!i || !a) return {
481
+ order: () => 0,
482
+ sign: o
483
+ };
484
+ if (r) return {
485
+ order: (e, t) => r(a[e.slot], a[t.slot]),
486
+ sign: o
487
+ };
488
+ if (!e) return {
489
+ order: (e, t) => S(a[e.slot], a[t.slot]),
490
+ sign: o
491
+ };
492
+ let { ranks: s, keys: c } = this.defaultKeyOf(i);
493
+ return {
494
+ order: (e, t) => s[e.slot] - s[t.slot] || c[e.slot] - c[t.slot] || 0,
495
+ sign: o
496
+ };
497
+ });
498
+ return (e, t) => {
499
+ for (let { order: n, sign: r } of i) {
500
+ let i = n(e, t);
501
+ if (i !== 0) return i * r;
502
+ }
503
+ return e.slot - t.slot;
504
+ };
388
505
  }
389
506
  sort() {
390
- this.sortDirty = !1;
391
- let e = this.sortModel.map(({ columnId: e, direction: t = "asc", compare: n = b }) => ({
392
- values: this.columns.get(String(e))?.values,
393
- compare: n,
394
- sign: t === "desc" ? -1 : 1
395
- }));
396
- this.rows.sort((t, n) => {
397
- for (let { values: r, compare: i, sign: a } of e) {
398
- let e = r ? i(r[t.slot], r[n.slot]) : 0;
399
- if (e !== 0) return e * a;
400
- }
401
- return t.slot - n.slot;
402
- });
507
+ this.sortDirty = !1, this.sortChanged.clear(), this.rows.sort(this.createComparator(!0));
403
508
  }
404
- }, S = class {
509
+ sortChanges() {
510
+ let e = this.sortChanged;
511
+ e.size * b >= this.rows.length ? this.sort() : (this.sortDirty = !1, this.sortChanged = /* @__PURE__ */ new Set(), this.rows.resort(e, this.createComparator(!1)));
512
+ }
513
+ }, T = class {
405
514
  entries = /* @__PURE__ */ new Map();
406
515
  limit;
407
516
  constructor(e) {
@@ -422,9 +531,9 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
422
531
  get size() {
423
532
  return this.entries.size;
424
533
  }
425
- }, 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) {
534
+ }, 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) {
426
535
  return e(Object.assign({}, t, { children: n }));
427
- } }, O = class {
536
+ } }, j = class {
428
537
  options;
429
538
  constructor(e) {
430
539
  this.options = { ...e };
@@ -435,29 +544,11 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
435
544
  }
436
545
  optionsUpdated(e) {}
437
546
  clearCache() {}
438
- }, k = class extends O {
439
- bitmaps = new S(4096);
440
- cacheKey(e) {}
441
- pixelRatio = 1;
442
- clearCache() {
443
- this.bitmaps.clear();
444
- }
445
- paint(e, t, n, r) {
446
- let i = this.cacheKey(t), a = (i === void 0 ? void 0 : this.bitmaps.get(i)) ?? this.renderBitmap(t, i);
447
- 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);
448
- }
449
- element = T((e, t, n, r) => this.paint(e, t, n, r));
450
- renderBitmap(e, t) {
451
- let n = this.render(e);
452
- if (t === void 0 || n.width <= 0 || n.height <= 0) return n;
453
- let r = document.createElement("canvas");
454
- return r.width = n.width, r.height = n.height, r.getContext("2d")?.drawImage(n, 0, 0), this.bitmaps.set(t, r), r;
455
- }
456
- }, A = (e, t) => {
547
+ }, M = (e, t) => {
457
548
  let n = e.getContext("2d", t);
458
549
  if (!n) throw Error("2D canvas context is not available");
459
550
  return n;
460
- }, j = class extends O {
551
+ }, N = class extends j {
461
552
  ctx;
462
553
  offCanvas = document.createElement("canvas");
463
554
  offCtx;
@@ -465,7 +556,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
465
556
  speed = 6;
466
557
  count = 75;
467
558
  constructor(e) {
468
- 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)";
559
+ super(e), this.ctx = this.options.ctx, this.offCanvas.width = 250, this.offCanvas.height = 250, this.offCtx = M(this.offCanvas), this.offCtx.lineWidth = .5, this.offCtx.strokeStyle = "rgba(255, 255, 255, 0.75)";
469
560
  }
470
561
  updateLoader() {
471
562
  this.rotation += this.speed / 100;
@@ -481,12 +572,12 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
481
572
  let e = Math.round((this.ctx.canvas.width - this.offCanvas.width) / 2), t = Math.round((this.ctx.canvas.height - this.offCanvas.height) / 2);
482
573
  return this.ctx.clearRect(e, t, 250, 250), this.ctx.drawImage(this.offCanvas, e, t), this.offCanvas;
483
574
  }
484
- }, M = class {
575
+ }, P = class {
485
576
  name;
486
577
  canvas = document.createElement("canvas");
487
578
  ctx;
488
579
  constructor(e, { alpha: t = !0, desynchronized: n = !1 } = {}) {
489
- this.name = e, this.canvas.className = `drawingLayer ${e}`, this.ctx = A(this.canvas, {
580
+ this.name = e, this.canvas.className = `drawingLayer ${e}`, this.ctx = M(this.canvas, {
490
581
  alpha: t,
491
582
  desynchronized: n
492
583
  });
@@ -500,7 +591,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
500
591
  clear() {
501
592
  this.ctx.clearRect(0, 0, this.width, this.height);
502
593
  }
503
- }, N = class {
594
+ }, F = class {
504
595
  renderers = /* @__PURE__ */ new Map();
505
596
  warned = /* @__PURE__ */ new Set();
506
597
  register(e, t) {
@@ -520,10 +611,10 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
520
611
  clearCaches() {
521
612
  this.renderers.forEach((e) => e.clearCache?.());
522
613
  }
523
- }, P = T((e, { x: t = 0, y: n = 0, width: r, height: i, color: a, alpha: o = 1 }, s, c) => {
614
+ }, I = O((e, { x: t = 0, y: n = 0, width: r, height: i, color: a, alpha: o = 1 }, s, c) => {
524
615
  let l = Math.trunc(r), u = Math.trunc(i);
525
616
  !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));
526
- }), F = class e extends O {
617
+ }), L = class e extends j {
527
618
  static defaultOptions = {
528
619
  layer: "background",
529
620
  enableFlashing: !0,
@@ -555,67 +646,39 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
555
646
  }
556
647
  draw({ ctx: e, x: t, y: n, width: r, height: i, age: a }) {
557
648
  let { background: o, flashRGB: s, borderRightWidth: c, borderRightColor: l, borderBottomWidth: u, borderBottomColor: d } = this.options, f = this.getFlashAlpha(a);
558
- (/* @__PURE__ */ D.createElement(E, null, /* @__PURE__ */ D.createElement(P, {
649
+ (/* @__PURE__ */ A.createElement(k, null, /* @__PURE__ */ A.createElement(I, {
559
650
  width: r,
560
651
  height: i,
561
652
  color: o
562
- }), f > 0 && /* @__PURE__ */ D.createElement(P, {
653
+ }), f > 0 && /* @__PURE__ */ A.createElement(I, {
563
654
  width: r,
564
655
  height: i,
565
656
  color: `rgb(${s})`,
566
657
  alpha: f
567
- }), c > 0 && /* @__PURE__ */ D.createElement(P, {
658
+ }), c > 0 && /* @__PURE__ */ A.createElement(I, {
568
659
  x: r - c,
569
660
  width: c,
570
661
  height: i,
571
662
  color: l
572
- }), u > 0 && /* @__PURE__ */ D.createElement(P, {
663
+ }), u > 0 && /* @__PURE__ */ A.createElement(I, {
573
664
  y: i - u,
574
665
  width: r,
575
666
  height: u,
576
667
  color: d
577
668
  }))).draw(e, t, n);
578
669
  }
579
- }, I = class e extends k {
580
- static defaultOptions = {
581
- font: "12px monospace",
582
- color: "#fff",
583
- alpha: !0,
584
- clearBeforeRender: !0
585
- };
586
- charWidthCache = new p();
587
- canvas = document.createElement("canvas");
588
- ctx;
589
- constructor(t = {}) {
590
- super({
591
- ...e.defaultOptions,
592
- ...t
593
- }), this.ctx = A(this.canvas, { alpha: this.options.alpha }), this.ctx.textAlign = "left", this.ctx.textBaseline = "top";
594
- }
595
- set font(e) {
596
- this.options.font !== e && (this.options.font = e);
597
- }
598
- set color(e) {
599
- this.options.color = e;
600
- }
601
- clearCache() {
602
- super.clearCache(), this.charWidthCache.clear();
603
- }
604
- cacheKey({ value: e = "", height: t = 0 }) {
605
- return `${this.options.font}|${this.options.color}|${this.pixelRatio}|${t}|${e}`;
606
- }
607
- getCharacterWidth(e = "") {
608
- let t = this.charWidthCache.get(e);
609
- if (t) return t;
610
- this.ctx.font !== this.options.font && (this.ctx.font = this.options.font, this.ctx.textAlign = "left", this.ctx.textBaseline = "top");
611
- let n = Math.ceil(this.ctx.measureText(e).width);
612
- return this.charWidthCache.set(e, n), n;
613
- }
614
- render({ value: e = "", height: t = 0 }) {
615
- let { pixelRatio: n } = this;
616
- 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;
617
- }
618
- }, L = class e extends O {
670
+ }, R = (e, t, n, r) => {
671
+ let i = 0, a = 0;
672
+ for (; a < e.length;) {
673
+ let n = r(e.charAt(a));
674
+ if (i + n > t) break;
675
+ i += n, a++;
676
+ }
677
+ if (a === e.length) return e;
678
+ let o = r(n);
679
+ for (; a > 0 && i + o > t;) a--, i -= r(e.charAt(a));
680
+ return i + o > t ? "" : e.slice(0, a) + n;
681
+ }, z = class e extends j {
619
682
  static defaultOptions = {
620
683
  ellipsis: "…",
621
684
  cellPadding: {
@@ -628,64 +691,49 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
628
691
  color: "#fff",
629
692
  isEllipsisRight: !0
630
693
  };
631
- characterRenderer;
694
+ characterWidths = /* @__PURE__ */ new Map();
695
+ measureContext = M(document.createElement("canvas"));
696
+ measuredFont = "";
632
697
  constructor(t = {}) {
633
698
  super({
634
699
  ...e.defaultOptions,
635
700
  ...t
636
- }), this.characterRenderer = new I({
637
- font: this.options.font,
638
- color: this.options.color
639
701
  });
640
702
  }
641
703
  set font(e) {
642
- this.options.font !== e && (this.options.font = e, this.characterRenderer.font = e, this.clearCache());
704
+ this.options.font !== e && (this.options.font = e, this.clearCache());
643
705
  }
644
706
  get font() {
645
707
  return this.options.font;
646
708
  }
647
709
  set color(e) {
648
- this.options.color = e, this.characterRenderer.color = e;
710
+ this.options.color = e;
649
711
  }
650
712
  get color() {
651
713
  return this.options.color;
652
714
  }
653
- set pixelRatio(e) {
654
- this.characterRenderer.pixelRatio = e;
655
- }
656
- get pixelRatio() {
657
- return this.characterRenderer.pixelRatio;
658
- }
715
+ pixelRatio = 1;
659
716
  clearCache() {
660
- this.characterRenderer.clearCache();
717
+ this.characterWidths.clear(), this.measuredFont = "";
718
+ }
719
+ measureCharacter(e) {
720
+ let t = this.characterWidths.get(e);
721
+ if (t !== void 0) return t;
722
+ this.measuredFont !== this.options.font && (this.measureContext.font = this.options.font, this.measuredFont = this.options.font);
723
+ let n = this.measureContext.measureText(e).width;
724
+ return this.characterWidths.set(e, n), n;
661
725
  }
662
726
  getTextWidth(e) {
663
727
  let t = 0;
664
- for (let n = 0; n < e.length; n++) {
665
- let r = e.charAt(n);
666
- t += this.characterRenderer.getCharacterWidth(r);
667
- }
728
+ for (let n = 0; n < e.length; n++) t += this.measureCharacter(e.charAt(n));
668
729
  return t;
669
730
  }
670
731
  paint(e, { value: t = "", x: n = 0, y: r = 0, width: i = 0, height: a = 0 }, o, s) {
671
- 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;
672
- for (let n = 0; n < t.length; n++) {
673
- let r = t.charAt(n), i = Math.round(this.characterRenderer.getCharacterWidth(r));
674
- if (Math.ceil(h + i) >= u) {
675
- this.characterRenderer.paint(e, {
676
- value: this.options.ellipsis,
677
- height: p
678
- }, d + m, f);
679
- break;
680
- }
681
- this.characterRenderer.paint(e, {
682
- value: r,
683
- height: p
684
- }, d + m, f), h += i, m += i;
685
- }
732
+ let { cellPadding: c } = this.options, l = R(t, i - c.left - c.right, this.options.ellipsis, (e) => this.measureCharacter(e));
733
+ l === "" || a - c.top - c.bottom <= 0 || (e.font = this.options.font, e.fillStyle = this.options.color, e.textAlign = "left", e.textBaseline = "top", e.fillText(l, o + n + c.left, s + r + c.top));
686
734
  }
687
- element = T((e, t, n, r) => this.paint(e, t, n, r));
688
- }, R = class e extends O {
735
+ element = O((e, t, n, r) => this.paint(e, t, n, r));
736
+ }, B = class e extends j {
689
737
  static defaultOptions = {
690
738
  layer: "header",
691
739
  background: "#333",
@@ -701,7 +749,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
701
749
  super({
702
750
  ...e.defaultOptions,
703
751
  ...t
704
- }), this.textRenderer = new L({
752
+ }), this.textRenderer = new z({
705
753
  font: this.options.font,
706
754
  color: this.options.color
707
755
  });
@@ -727,27 +775,27 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
727
775
  draw({ ctx: e, x: t, y: n, width: r, height: i, formatted: a }) {
728
776
  if (r === 0 || i === 0) return;
729
777
  let { background: o, borderRightWidth: s, borderRightColor: c, borderBottomWidth: l, borderBottomColor: u } = this.options;
730
- (/* @__PURE__ */ D.createElement(E, null, /* @__PURE__ */ D.createElement(P, {
778
+ (/* @__PURE__ */ A.createElement(k, null, /* @__PURE__ */ A.createElement(I, {
731
779
  width: r,
732
780
  height: i,
733
781
  color: o
734
- }), s > 0 && /* @__PURE__ */ D.createElement(P, {
782
+ }), s > 0 && /* @__PURE__ */ A.createElement(I, {
735
783
  x: r - s,
736
784
  width: s,
737
785
  height: i,
738
786
  color: c
739
- }), l > 0 && /* @__PURE__ */ D.createElement(P, {
787
+ }), l > 0 && /* @__PURE__ */ A.createElement(I, {
740
788
  y: i - l,
741
789
  width: r,
742
790
  height: l,
743
791
  color: u
744
- }), /* @__PURE__ */ D.createElement(this.textRenderer.element, {
792
+ }), /* @__PURE__ */ A.createElement(this.textRenderer.element, {
745
793
  value: a,
746
794
  width: r,
747
795
  height: i
748
796
  }))).draw(e, t, n);
749
797
  }
750
- }, z = class e extends O {
798
+ }, V = class e extends j {
751
799
  static defaultOptions = {
752
800
  layer: "content",
753
801
  font: "12px monospace",
@@ -758,7 +806,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
758
806
  super({
759
807
  ...e.defaultOptions,
760
808
  ...t
761
- }), this.renderer = new L({
809
+ }), this.renderer = new z({
762
810
  font: this.options.font,
763
811
  color: this.options.color
764
812
  });
@@ -788,24 +836,24 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
788
836
  this.renderer.clearCache();
789
837
  }
790
838
  draw({ ctx: e, x: t, y: n, width: r, height: i, formatted: a, valueChanged: o, forced: s }) {
791
- (o || s) && (e.clearRect(t, n, r, i), (/* @__PURE__ */ D.createElement(this.renderer.element, {
839
+ (o || s) && (e.clearRect(t, n, r, i), (/* @__PURE__ */ A.createElement(this.renderer.element, {
792
840
  value: a,
793
841
  width: r,
794
842
  height: i
795
843
  })).draw(e, t, n));
796
844
  }
797
- }, B = { scrollArea: "scrollArea" }, V = {
845
+ }, H = { scrollArea: "scrollArea" }, U = {
798
846
  background: "background",
799
847
  content: "content",
800
848
  header: "header",
801
849
  backgroundRenderer: "background",
802
850
  valueRenderer: "value",
803
851
  headerRenderer: "header"
804
- }, H = "canvas-grid-styles", U = () => {
805
- if (document.getElementById(H)) return;
852
+ }, W = "canvas-grid-styles", G = () => {
853
+ if (document.getElementById(W)) return;
806
854
  let e = document.createElement("style");
807
- e.id = H, e.textContent = C, document.head.appendChild(e);
808
- }, W = class extends HTMLElement {
855
+ e.id = W, e.textContent = E, document.head.appendChild(e);
856
+ }, K = class extends HTMLElement {
809
857
  fps = 30;
810
858
  _font = "12px monospace";
811
859
  _headerFont = "13px monospace";
@@ -824,13 +872,13 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
824
872
  _backgroundColor = "#333";
825
873
  _headerBackgroundColor = "#222";
826
874
  textColor = "#efefef";
827
- dataStore = new x({
875
+ dataStore = new w({
828
876
  getRowId: (e, t) => this.getRowId(e, t),
829
877
  getCellHeight: () => this.cellHeight
830
878
  });
831
879
  _dataCollection = this.dataStore.rows;
832
- cellEntries = new S(2048);
833
- getRowId = _;
880
+ cellEntries = new T(2048);
881
+ getRowId = v;
834
882
  headers = new p();
835
883
  animationFrame = void 0;
836
884
  scrollYOffset = 0;
@@ -845,10 +893,10 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
845
893
  layers = [];
846
894
  layersByName = /* @__PURE__ */ new Map();
847
895
  warnedLayers = /* @__PURE__ */ new Set();
848
- renderers = new N();
849
- cellRenderers = [V.backgroundRenderer, V.valueRenderer];
850
- cellBackgroundRenderer = new F({
851
- layer: V.background,
896
+ renderers = new F();
897
+ cellRenderers = [U.backgroundRenderer, U.valueRenderer];
898
+ cellBackgroundRenderer = new L({
899
+ layer: U.background,
852
900
  background: this._backgroundColor,
853
901
  enableFlashing: !0,
854
902
  flashInDuration: 200,
@@ -859,13 +907,13 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
859
907
  borderBottomWidth: 1,
860
908
  borderBottomColor: "#3f3f3f"
861
909
  });
862
- cellValueRenderer = new z({
863
- layer: V.content,
910
+ cellValueRenderer = new V({
911
+ layer: U.content,
864
912
  font: this._font,
865
913
  color: this.textColor
866
914
  });
867
- headerCellRenderer = new R({
868
- layer: V.header,
915
+ headerCellRenderer = new B({
916
+ layer: U.header,
869
917
  font: this._headerFont,
870
918
  color: this.textColor,
871
919
  background: "#222",
@@ -883,8 +931,8 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
883
931
  }, 50);
884
932
  agApi;
885
933
  constructor() {
886
- 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 = {
887
- ctx: this.requireLayer(V.content).ctx,
934
+ 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(U.background, { alpha: !1 }), this.addLayer(U.content), this.addLayer(U.header), this.registerRenderer(U.backgroundRenderer, this.cellBackgroundRenderer), this.registerRenderer(U.valueRenderer, this.cellValueRenderer), this.registerRenderer(U.headerRenderer, this.headerCellRenderer), this.loadingIndicator = new N({ ctx: this.requireLayer(U.header).ctx }), this.cell = {
935
+ ctx: this.requireLayer(U.content).ctx,
888
936
  x: 0,
889
937
  y: 0,
890
938
  width: 0,
@@ -916,7 +964,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
916
964
  };
917
965
  }
918
966
  get canvas() {
919
- return this.requireLayer(V.content).canvas;
967
+ return this.requireLayer(U.content).canvas;
920
968
  }
921
969
  get flashInDuration() {
922
970
  return this.cellBackgroundRenderer.options.flashInDuration;
@@ -940,7 +988,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
940
988
  }
941
989
  addLayer(e, { alpha: t = !0, desynchronized: n = !1, before: r } = {}) {
942
990
  if (this.getLayer(e)) throw Error(`canvas-grid: layer "${e}" already exists`);
943
- let i = new M(e, {
991
+ let i = new P(e, {
944
992
  alpha: t,
945
993
  desynchronized: n
946
994
  }), a = r === void 0 ? void 0 : this.requireLayer(r);
@@ -958,7 +1006,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
958
1006
  }
959
1007
  createScrollArea() {
960
1008
  let e = document.createElement("div");
961
- return e.className = B.scrollArea, e.appendChild(this.scrollHelper), e;
1009
+ return e.className = H.scrollArea, e.appendChild(this.scrollHelper), e;
962
1010
  }
963
1011
  set font(e) {
964
1012
  this._font !== e && (this._font = e, this.cellValueRenderer.font = e);
@@ -1103,7 +1151,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
1103
1151
  index: e.index
1104
1152
  },
1105
1153
  formatted: `${e.index + 1}`
1106
- }), this.paint(V.headerRenderer));
1154
+ }), this.paint(U.headerRenderer));
1107
1155
  }
1108
1156
  forceDraw() {
1109
1157
  this.layers.forEach((e) => e.clear()), this.draw(!0);
@@ -1147,7 +1195,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
1147
1195
  return this.dataStore.getSortModel();
1148
1196
  }
1149
1197
  renderBrick() {
1150
- this.showHeaders && this.showRowNumbers && this._headerBackgroundColor && (this.describe(0, 0, this.rowNumbersWidth, this.headerHeight, {}), this.paint(V.headerRenderer));
1198
+ this.showHeaders && this.showRowNumbers && this._headerBackgroundColor && (this.describe(0, 0, this.rowNumbersWidth, this.headerHeight, {}), this.paint(U.headerRenderer));
1151
1199
  }
1152
1200
  renderHeaders() {
1153
1201
  let e = (this.showRowNumbers ? this.rowNumbersWidth : 0) - this.scrollXOffset;
@@ -1196,7 +1244,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
1196
1244
  let t = {
1197
1245
  width: 100,
1198
1246
  valueFormatter: (e) => String(e),
1199
- headerRenderer: V.headerRenderer
1247
+ headerRenderer: U.headerRenderer
1200
1248
  };
1201
1249
  e.forEach((e, n) => {
1202
1250
  if (typeof e == "string" || typeof e == "number") {
@@ -1220,7 +1268,7 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
1220
1268
  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);
1221
1269
  }
1222
1270
  };
1223
- window && window.customElements && (U(), customElements.define("canvas-grid", W));
1224
- export { W as default };
1271
+ window && window.customElements && (G(), customElements.define("canvas-grid", K));
1272
+ export { K as default };
1225
1273
 
1226
1274
  //# sourceMappingURL=web-component.mjs.map