@stramien/core 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -22,6 +22,44 @@ function humanizeField(field) {
22
22
  if (!field) return "";
23
23
  return field.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_.-]+/g, " ").replace(/\b\w/g, (ch) => ch.toUpperCase()).trim();
24
24
  }
25
+ function distributeWidths(cols, total) {
26
+ const out = new Array(cols.length).fill(0);
27
+ let free = cols.map((_, i) => i);
28
+ let space = total;
29
+ for (; ; ) {
30
+ const weight = free.reduce((sum, i) => sum + Math.max(0, cols[i].weight), 0);
31
+ const share = (i) => weight > 0 ? Math.max(0, cols[i].weight) / weight * space : space / free.length;
32
+ const clamped = free.filter((i) => {
33
+ const ideal = share(i);
34
+ return ideal < cols[i].min || ideal > cols[i].max;
35
+ });
36
+ if (clamped.length === 0) break;
37
+ for (const i of clamped) {
38
+ out[i] = share(i) < cols[i].min ? cols[i].min : cols[i].max;
39
+ space -= out[i];
40
+ }
41
+ free = free.filter((i) => !clamped.includes(i));
42
+ if (free.length === 0) return out;
43
+ }
44
+ const exact = free.map((i) => {
45
+ const weight = free.reduce((sum, j) => sum + Math.max(0, cols[j].weight), 0);
46
+ return weight > 0 ? Math.max(0, cols[i].weight) / weight * space : space / free.length;
47
+ });
48
+ const floors = exact.map((x) => Math.floor(x + 1e-6));
49
+ let leftover = Math.round(space - floors.reduce((a, b) => a + b, 0));
50
+ const byRemainder = exact.map((x, k) => ({ k, rest: x - floors[k] })).sort((a, b) => b.rest - a.rest || a.k - b.k);
51
+ for (const { k } of byRemainder) {
52
+ if (leftover <= 0) break;
53
+ const i = free[k];
54
+ if (floors[k] + 1 > cols[i].max) continue;
55
+ floors[k]++;
56
+ leftover--;
57
+ }
58
+ free.forEach((i, k) => {
59
+ out[i] = floors[k];
60
+ });
61
+ return out;
62
+ }
25
63
  var ColumnController = class {
26
64
  columns = [];
27
65
  manualWidths = /* @__PURE__ */ new Set();
@@ -536,15 +574,14 @@ var ColumnController = class {
536
574
  const targetSpace = Math.max(0, availableWidth - fixedWidth);
537
575
  const currentTotal = resizableCols.reduce((sum, c) => sum + c.width, 0);
538
576
  if (currentTotal <= 0) return false;
539
- let allocated = 0;
577
+ const widths = distributeWidths(
578
+ resizableCols.map((c) => ({ weight: c.width, min: c.minWidth, max: c.maxWidth })),
579
+ targetSpace
580
+ );
540
581
  resizableCols.forEach((col, index) => {
541
- const isLast = index === resizableCols.length - 1;
542
- const ideal = isLast ? targetSpace - allocated : Math.round(col.width / currentTotal * targetSpace);
543
- const clamped = Math.min(col.maxWidth, Math.max(col.minWidth, ideal));
544
- col.width = clamped;
545
- allocated += clamped;
582
+ col.width = widths[index];
546
583
  this.manualWidths.add(col.colId);
547
- this.setOverride(col.colId, { width: clamped });
584
+ this.setOverride(col.colId, { width: col.width });
548
585
  });
549
586
  this.updatePinningCollapse();
550
587
  return true;
@@ -3255,6 +3292,18 @@ var DomRenderer = class {
3255
3292
  getViewportWidth() {
3256
3293
  return Math.floor(this.surfaceEl.clientWidth);
3257
3294
  }
3295
+ /**
3296
+ * Width all columns can take without a horizontal scrollbar: the visible width of the
3297
+ * unpinned area plus the pinned zones' columns. Unlike `getViewportWidth`, this leaves out
3298
+ * the pinned zones' borders and a vertical scrollbar.
3299
+ */
3300
+ getColumnFitWidth() {
3301
+ const center = this.bodyCenterEl.clientWidth;
3302
+ if (center <= 0) return this.getViewportWidth();
3303
+ const zones = this.callbacks.getColumnsByZone();
3304
+ const pinned = [...zones.left, ...zones.right].reduce((sum, c) => sum + c.width, 0);
3305
+ return Math.floor(center + pinned);
3306
+ }
3258
3307
  renderAll() {
3259
3308
  this.syncGridRole();
3260
3309
  this.renderToolbar();
@@ -9198,7 +9247,7 @@ var StramienGridCore = class {
9198
9247
  this.emitStateChanged();
9199
9248
  },
9200
9249
  sizeColumnsToFit: () => {
9201
- const width = this.renderer.getViewportWidth();
9250
+ const width = this.renderer.getColumnFitWidth();
9202
9251
  const wasCollapsed = this.columnController.isPinningCollapsed();
9203
9252
  if (this.columnController.sizeColumnsToFit(width)) {
9204
9253
  if (this.columnController.isPinningCollapsed() !== wasCollapsed) {