@dolusoft/query-table 3.7.0 → 3.7.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.
@@ -1257,6 +1257,68 @@ var measureColumnWidths = (table, fields) => {
1257
1257
  return widths;
1258
1258
  };
1259
1259
  //#endregion
1260
+ //#region src/resize/width-scale.ts
1261
+ var same = (width) => width;
1262
+ var sum = (widths) => widths.reduce((a, b) => a + b, 0);
1263
+ /** The declared width is the drawn one. */
1264
+ var identityScale = {
1265
+ declared: same,
1266
+ rendered: same
1267
+ };
1268
+ /**
1269
+ * A table whose cells take `space` (a container's width, with
1270
+ * `min-width: 100%`) and whose columns are declared narrower: the fixed
1271
+ * layout spreads the rest over the columns by their declared widths, so a
1272
+ * column declared `w` next to `others` is drawn `w * space / (w + others)`
1273
+ * wide until the columns fill the space, and `w` from there on. With no other
1274
+ * column the one column takes the space whatever its width: the identity.
1275
+ */
1276
+ var stretchedScale = (space, others) => others > 0 && space > others ? {
1277
+ declared: (rendered) => rendered >= space - others ? rendered : rendered * others / (space - rendered),
1278
+ rendered: (declared) => declared + others >= space ? declared : declared * space / (declared + others)
1279
+ } : identityScale;
1280
+ /**
1281
+ * How the declared width of the column of `th` maps to its drawn width, read
1282
+ * from the layout. The column is drawn once at the width it is drawn at now.
1283
+ * Wider there, the table stretches it: its row's cells take the whole space,
1284
+ * and the other columns' declared widths follow from the stretch; the fit
1285
+ * must then give back the width the column is drawn at now. Not wider, the
1286
+ * column is drawn once more at `low`, the narrowest width a resize writes. A
1287
+ * table that stretches it there widens every cell of the row by one factor,
1288
+ * the column at least as much (content the fixed layout keeps wider than
1289
+ * `low` only adds to it), and its cells then take the space the columns can
1290
+ * shrink into before the table stretches them. Any other layout gets the
1291
+ * identity: one that never stretches, a column without a width that takes
1292
+ * what is left, one that spreads the space otherwise. Every style is put back before the
1293
+ * function returns (C-31), so nothing is painted in between.
1294
+ */
1295
+ var measureWidthScale = (th, low) => {
1296
+ const now = th.getBoundingClientRect().width;
1297
+ const others = Array.from(th.parentElement?.children ?? []).filter((cell) => cell !== th);
1298
+ const widths = () => others.map((cell) => cell.getBoundingClientRect().width);
1299
+ const swap = styleSwap();
1300
+ /** Draws the column `width` wide; the width it takes. */
1301
+ const draw = (width) => {
1302
+ swap.set(th, "width", `${width}px`);
1303
+ return th.getBoundingClientRect().width;
1304
+ };
1305
+ try {
1306
+ const drawn = draw(now);
1307
+ const before = widths();
1308
+ const rest = sum(before);
1309
+ if (drawn > now + .5) {
1310
+ const scale = stretchedScale(drawn + rest, now * rest / drawn);
1311
+ return Math.abs(draw(scale.declared(now)) - now) <= 1 ? scale : identityScale;
1312
+ }
1313
+ const narrow = draw(low);
1314
+ const after = widths();
1315
+ const factor = sum(after) / rest;
1316
+ return factor > 1 && narrow >= low * factor - 1 && after.every((width, i) => Math.abs(width - before[i] * factor) <= 1) ? stretchedScale(narrow + sum(after), rest) : identityScale;
1317
+ } finally {
1318
+ swap.restore();
1319
+ }
1320
+ };
1321
+ //#endregion
1260
1322
  //#region src/resize/use-column-resize.ts
1261
1323
  /** Default `Column.minWidth`, in pixels. */
1262
1324
  var defaultMinWidth = 40;
@@ -1286,18 +1348,27 @@ var cellOf = (event) => event.currentTarget.closest("th");
1286
1348
  */
1287
1349
  var edgeOf = (column) => column.pinned === "right" ? -1 : 1;
1288
1350
  /**
1351
+ * The scale of a column's widths, read from the layout once per drag or key:
1352
+ * a table stretched to its container draws a column wider than declared.
1353
+ */
1354
+ var scaleOf = (th, column) => measureWidthScale(th, clampWidth(column, 0));
1355
+ /**
1289
1356
  * Resizing a column (C-48 to C-50). The table holds a width only while a
1290
1357
  * drag is under way (the preview, P2); every committed width goes out as a
1291
1358
  * `columnResize` event and comes back, if the consumer wants it, as
1292
1359
  * `Column.width`. The handle captures the pointer, so all drag events arrive
1293
- * at the handle itself, and it has the focus, so Escape does too.
1360
+ * at the handle itself, and it has the focus, so Escape does too. The pointer
1361
+ * and the arrow keys move the drawn width; what the table writes and emits is
1362
+ * the declared width that draws it (`measureWidthScale`).
1294
1363
  */
1295
1364
  var useColumnResize = (options) => {
1365
+ /** `width` is declared (the inline style), `rendered` drawn. */
1296
1366
  const preview = shallowRef(null);
1297
1367
  let drag = null;
1298
1368
  const isResizable = (column) => options.resizable() && column.resizable !== false;
1299
1369
  /** The width the header shows: the preview while dragging, else measured. */
1300
- const widthOf = (column) => preview.value?.field === column.field ? preview.value.width : Math.round(options.measuredWidth(column.field) ?? 0);
1370
+ const widthOf = (column) => preview.value?.field === column.field ? preview.value.rendered : Math.round(options.measuredWidth(column.field) ?? 0);
1371
+ /** Emits the declared width `width`, unless it is the declared `from`. */
1301
1372
  const commit = (column, from, width) => {
1302
1373
  const next = clampWidth(column, width);
1303
1374
  if (next !== Math.round(from)) options.emit({
@@ -1305,14 +1376,15 @@ var useColumnResize = (options) => {
1305
1376
  width: next
1306
1377
  });
1307
1378
  };
1308
- const dragged = (current) => current.startWidth + edgeOf(current.column) * (current.lastX - current.startX);
1379
+ /** The declared width that draws the column where the pointer is. */
1380
+ const dragged = (current) => current.scale.declared(current.startWidth + edgeOf(current.column) * (current.lastX - current.startX));
1309
1381
  const end = (apply) => {
1310
1382
  const current = drag;
1311
1383
  if (current) {
1312
1384
  drag = null;
1313
1385
  cancelAnimationFrame(current.frame);
1314
1386
  preview.value = null;
1315
- if (apply) commit(current.column, current.startWidth, dragged(current));
1387
+ if (apply) commit(current.column, current.scale.declared(current.startWidth), dragged(current));
1316
1388
  }
1317
1389
  };
1318
1390
  /** Every pointer event of a handle; one listener for the whole drag. */
@@ -1329,6 +1401,7 @@ var useColumnResize = (options) => {
1329
1401
  pointerId: event.pointerId,
1330
1402
  startX: event.clientX,
1331
1403
  startWidth: th.getBoundingClientRect().width,
1404
+ scale: scaleOf(th, column),
1332
1405
  lastX: event.clientX,
1333
1406
  frame: 0
1334
1407
  };
@@ -1339,15 +1412,20 @@ var useColumnResize = (options) => {
1339
1412
  if (event.type === "pointermove") drag.frame ||= requestAnimationFrame(() => {
1340
1413
  if (drag) {
1341
1414
  drag.frame = 0;
1415
+ const width = clampWidth(drag.column, dragged(drag));
1342
1416
  preview.value = {
1343
1417
  field: drag.column.field,
1344
- width: clampWidth(drag.column, dragged(drag))
1418
+ width,
1419
+ rendered: Math.round(drag.scale.rendered(width))
1345
1420
  };
1346
1421
  }
1347
1422
  });
1348
1423
  else end(event.type === "pointerup");
1349
1424
  };
1350
- /** Arrow keys step the width, Enter fits it; both from the rendered width. */
1425
+ /**
1426
+ * Arrow keys step the drawn width, Enter fits it to the content (the
1427
+ * declared width `measureColumnWidths` gives, as it is).
1428
+ */
1351
1429
  const onKey = (event, column) => {
1352
1430
  const { key } = event;
1353
1431
  if (drag) {
@@ -1362,8 +1440,11 @@ var useColumnResize = (options) => {
1362
1440
  const th = cellOf(event);
1363
1441
  if (th && (sign || key === "Enter" || event.type === "dblclick")) {
1364
1442
  event.preventDefault();
1365
- const from = th.getBoundingClientRect().width;
1366
- const to = sign ? Math.round(from) + sign * (event.shiftKey ? 50 : 10) : measureColumnWidths(th.closest("table"), [column.field])[column.field];
1443
+ const drawn = th.getBoundingClientRect().width;
1444
+ const scale = scaleOf(th, column);
1445
+ const from = scale.declared(drawn);
1446
+ const step = scale.declared(Math.round(drawn) + sign * (event.shiftKey ? 50 : 10));
1447
+ const to = sign ? from + sign * Math.max(1, Math.abs(step - from)) : measureColumnWidths(th.closest("table"), [column.field])[column.field];
1367
1448
  if (to !== void 0) commit(column, from, to);
1368
1449
  }
1369
1450
  };