@modelform/react-native 0.2.0 → 0.4.0

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.
@@ -483,6 +483,32 @@ var GRID_COLUMNS = 24;
483
483
  var CELL_HEIGHT = 30;
484
484
  var CELL_MARGIN = 8;
485
485
  var gridUnitsToPx = (h) => h * CELL_HEIGHT + (h - 1) * CELL_MARGIN;
486
+ var gridOffsetPx = (y) => y * (CELL_HEIGHT + CELL_MARGIN);
487
+ var overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
488
+ function compactGrid(boxes, fixed2 = /* @__PURE__ */ new Set()) {
489
+ const snapped = boxes.map((b) => {
490
+ const w = Math.min(GRID_COLUMNS, Math.max(1, Math.round(b.x + b.w) - Math.round(b.x)));
491
+ const x = Math.min(GRID_COLUMNS - w, Math.max(0, Math.round(b.x)));
492
+ return { x, y: Math.max(0, b.y), w, h: Math.max(1, Math.round(b.h)) };
493
+ });
494
+ const out = new Array(boxes.length);
495
+ const placed = [];
496
+ for (const i of fixed2) {
497
+ if (!snapped[i]) continue;
498
+ out[i] = { ...snapped[i], y: Math.round(snapped[i].y) };
499
+ placed.push(out[i]);
500
+ }
501
+ const order = snapped.map((_, i) => i).filter((i) => !fixed2.has(i)).sort((a, b) => snapped[a].y - snapped[b].y || snapped[a].x - snapped[b].x || a - b);
502
+ for (const i of order) {
503
+ const box = { ...snapped[i], y: Math.floor(snapped[i].y) };
504
+ box.y = Math.min(box.y, Math.max(0, ...placed.map((p) => p.y + p.h)));
505
+ while (box.y > 0 && !placed.some((p) => overlaps({ ...box, y: box.y - 1 }, p))) box.y--;
506
+ for (let hit = placed.find((p) => overlaps(box, p)); hit; hit = placed.find((p) => overlaps(box, p))) box.y = hit.y + hit.h;
507
+ out[i] = box;
508
+ placed.push(box);
509
+ }
510
+ return out;
511
+ }
486
512
  var isRow = (p) => p.type === "row";
487
513
  var byPosition = (a, b) => a.gridPos.y - b.gridPos.y || a.gridPos.x - b.gridPos.x;
488
514
  function layoutDashboard(dashboard, mode = "phone", repeat2) {
@@ -506,12 +532,15 @@ function layoutDashboard(dashboard, mode = "phone", repeat2) {
506
532
  children: s.children.map((c) => ({ ...c, scopedVars: { ...c.scopedVars, ...scoped } }))
507
533
  };
508
534
  });
509
- }).map((s) => ({
510
- key: s.key,
511
- row: s.row,
512
- collapsed: !!s.row?.collapsed,
513
- rows: packRows(expandPanelRepeats(s.children, repeat2).sort(byPosition), mode)
514
- }));
535
+ }).map((s) => {
536
+ const panels = expandPanelRepeats(s.children, repeat2).sort(byPosition);
537
+ return { key: s.key, row: s.row, collapsed: !!s.row?.collapsed, rows: packRows(panels, mode), grid: placeOnGrid(panels) };
538
+ });
539
+ }
540
+ function placeOnGrid(panels) {
541
+ const boxes = compactGrid(panels.map((p) => p.gridPos));
542
+ const cells = panels.map((panel, i) => ({ key: panelKey(panel), panel, span: boxes[i].w, ...boxes[i] }));
543
+ return { cells, h: Math.max(0, ...boxes.map((b) => b.y + b.h)) };
515
544
  }
516
545
  function repeatOptions(name, ctx) {
517
546
  const value = ctx.values[name];
@@ -8318,12 +8347,12 @@ function documentUses(manifest) {
8318
8347
  // node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
8319
8348
  function createScanner(text, ignoreTrivia = false) {
8320
8349
  const len = text.length;
8321
- let pos2 = 0, value = "", tokenOffset = 0, token = 16, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0;
8350
+ let pos3 = 0, value = "", tokenOffset = 0, token = 16, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0;
8322
8351
  function scanHexDigits(count, exact) {
8323
8352
  let digits = 0;
8324
8353
  let value2 = 0;
8325
8354
  while (digits < count || !exact) {
8326
- let ch = text.charCodeAt(pos2);
8355
+ let ch = text.charCodeAt(pos3);
8327
8356
  if (ch >= 48 && ch <= 57) {
8328
8357
  value2 = value2 * 16 + ch - 48;
8329
8358
  } else if (ch >= 65 && ch <= 70) {
@@ -8333,7 +8362,7 @@ function createScanner(text, ignoreTrivia = false) {
8333
8362
  } else {
8334
8363
  break;
8335
8364
  }
8336
- pos2++;
8365
+ pos3++;
8337
8366
  digits++;
8338
8367
  }
8339
8368
  if (digits < count) {
@@ -8342,46 +8371,46 @@ function createScanner(text, ignoreTrivia = false) {
8342
8371
  return value2;
8343
8372
  }
8344
8373
  function setPosition(newPosition) {
8345
- pos2 = newPosition;
8374
+ pos3 = newPosition;
8346
8375
  value = "";
8347
8376
  tokenOffset = 0;
8348
8377
  token = 16;
8349
8378
  scanError = 0;
8350
8379
  }
8351
8380
  function scanNumber() {
8352
- let start = pos2;
8353
- if (text.charCodeAt(pos2) === 48) {
8354
- pos2++;
8381
+ let start = pos3;
8382
+ if (text.charCodeAt(pos3) === 48) {
8383
+ pos3++;
8355
8384
  } else {
8356
- pos2++;
8357
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8358
- pos2++;
8385
+ pos3++;
8386
+ while (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8387
+ pos3++;
8359
8388
  }
8360
8389
  }
8361
- if (pos2 < text.length && text.charCodeAt(pos2) === 46) {
8362
- pos2++;
8363
- if (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8364
- pos2++;
8365
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8366
- pos2++;
8390
+ if (pos3 < text.length && text.charCodeAt(pos3) === 46) {
8391
+ pos3++;
8392
+ if (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8393
+ pos3++;
8394
+ while (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8395
+ pos3++;
8367
8396
  }
8368
8397
  } else {
8369
8398
  scanError = 3;
8370
- return text.substring(start, pos2);
8399
+ return text.substring(start, pos3);
8371
8400
  }
8372
8401
  }
8373
- let end = pos2;
8374
- if (pos2 < text.length && (text.charCodeAt(pos2) === 69 || text.charCodeAt(pos2) === 101)) {
8375
- pos2++;
8376
- if (pos2 < text.length && text.charCodeAt(pos2) === 43 || text.charCodeAt(pos2) === 45) {
8377
- pos2++;
8402
+ let end = pos3;
8403
+ if (pos3 < text.length && (text.charCodeAt(pos3) === 69 || text.charCodeAt(pos3) === 101)) {
8404
+ pos3++;
8405
+ if (pos3 < text.length && text.charCodeAt(pos3) === 43 || text.charCodeAt(pos3) === 45) {
8406
+ pos3++;
8378
8407
  }
8379
- if (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8380
- pos2++;
8381
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8382
- pos2++;
8408
+ if (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8409
+ pos3++;
8410
+ while (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8411
+ pos3++;
8383
8412
  }
8384
- end = pos2;
8413
+ end = pos3;
8385
8414
  } else {
8386
8415
  scanError = 3;
8387
8416
  }
@@ -8389,27 +8418,27 @@ function createScanner(text, ignoreTrivia = false) {
8389
8418
  return text.substring(start, end);
8390
8419
  }
8391
8420
  function scanString() {
8392
- let result = "", start = pos2;
8421
+ let result = "", start = pos3;
8393
8422
  while (true) {
8394
- if (pos2 >= len) {
8395
- result += text.substring(start, pos2);
8423
+ if (pos3 >= len) {
8424
+ result += text.substring(start, pos3);
8396
8425
  scanError = 2;
8397
8426
  break;
8398
8427
  }
8399
- const ch = text.charCodeAt(pos2);
8428
+ const ch = text.charCodeAt(pos3);
8400
8429
  if (ch === 34) {
8401
- result += text.substring(start, pos2);
8402
- pos2++;
8430
+ result += text.substring(start, pos3);
8431
+ pos3++;
8403
8432
  break;
8404
8433
  }
8405
8434
  if (ch === 92) {
8406
- result += text.substring(start, pos2);
8407
- pos2++;
8408
- if (pos2 >= len) {
8435
+ result += text.substring(start, pos3);
8436
+ pos3++;
8437
+ if (pos3 >= len) {
8409
8438
  scanError = 2;
8410
8439
  break;
8411
8440
  }
8412
- const ch2 = text.charCodeAt(pos2++);
8441
+ const ch2 = text.charCodeAt(pos3++);
8413
8442
  switch (ch2) {
8414
8443
  case 34:
8415
8444
  result += '"';
@@ -8446,126 +8475,126 @@ function createScanner(text, ignoreTrivia = false) {
8446
8475
  default:
8447
8476
  scanError = 5;
8448
8477
  }
8449
- start = pos2;
8478
+ start = pos3;
8450
8479
  continue;
8451
8480
  }
8452
8481
  if (ch >= 0 && ch <= 31) {
8453
8482
  if (isLineBreak(ch)) {
8454
- result += text.substring(start, pos2);
8483
+ result += text.substring(start, pos3);
8455
8484
  scanError = 2;
8456
8485
  break;
8457
8486
  } else {
8458
8487
  scanError = 6;
8459
8488
  }
8460
8489
  }
8461
- pos2++;
8490
+ pos3++;
8462
8491
  }
8463
8492
  return result;
8464
8493
  }
8465
8494
  function scanNext() {
8466
8495
  value = "";
8467
8496
  scanError = 0;
8468
- tokenOffset = pos2;
8497
+ tokenOffset = pos3;
8469
8498
  lineStartOffset = lineNumber;
8470
8499
  prevTokenLineStartOffset = tokenLineStartOffset;
8471
- if (pos2 >= len) {
8500
+ if (pos3 >= len) {
8472
8501
  tokenOffset = len;
8473
8502
  return token = 17;
8474
8503
  }
8475
- let code = text.charCodeAt(pos2);
8504
+ let code = text.charCodeAt(pos3);
8476
8505
  if (isWhiteSpace(code)) {
8477
8506
  do {
8478
- pos2++;
8507
+ pos3++;
8479
8508
  value += String.fromCharCode(code);
8480
- code = text.charCodeAt(pos2);
8509
+ code = text.charCodeAt(pos3);
8481
8510
  } while (isWhiteSpace(code));
8482
8511
  return token = 15;
8483
8512
  }
8484
8513
  if (isLineBreak(code)) {
8485
- pos2++;
8514
+ pos3++;
8486
8515
  value += String.fromCharCode(code);
8487
- if (code === 13 && text.charCodeAt(pos2) === 10) {
8488
- pos2++;
8516
+ if (code === 13 && text.charCodeAt(pos3) === 10) {
8517
+ pos3++;
8489
8518
  value += "\n";
8490
8519
  }
8491
8520
  lineNumber++;
8492
- tokenLineStartOffset = pos2;
8521
+ tokenLineStartOffset = pos3;
8493
8522
  return token = 14;
8494
8523
  }
8495
8524
  switch (code) {
8496
8525
  // tokens: []{}:,
8497
8526
  case 123:
8498
- pos2++;
8527
+ pos3++;
8499
8528
  return token = 1;
8500
8529
  case 125:
8501
- pos2++;
8530
+ pos3++;
8502
8531
  return token = 2;
8503
8532
  case 91:
8504
- pos2++;
8533
+ pos3++;
8505
8534
  return token = 3;
8506
8535
  case 93:
8507
- pos2++;
8536
+ pos3++;
8508
8537
  return token = 4;
8509
8538
  case 58:
8510
- pos2++;
8539
+ pos3++;
8511
8540
  return token = 6;
8512
8541
  case 44:
8513
- pos2++;
8542
+ pos3++;
8514
8543
  return token = 5;
8515
8544
  // strings
8516
8545
  case 34:
8517
- pos2++;
8546
+ pos3++;
8518
8547
  value = scanString();
8519
8548
  return token = 10;
8520
8549
  // comments
8521
8550
  case 47:
8522
- const start = pos2 - 1;
8523
- if (text.charCodeAt(pos2 + 1) === 47) {
8524
- pos2 += 2;
8525
- while (pos2 < len) {
8526
- if (isLineBreak(text.charCodeAt(pos2))) {
8551
+ const start = pos3 - 1;
8552
+ if (text.charCodeAt(pos3 + 1) === 47) {
8553
+ pos3 += 2;
8554
+ while (pos3 < len) {
8555
+ if (isLineBreak(text.charCodeAt(pos3))) {
8527
8556
  break;
8528
8557
  }
8529
- pos2++;
8558
+ pos3++;
8530
8559
  }
8531
- value = text.substring(start, pos2);
8560
+ value = text.substring(start, pos3);
8532
8561
  return token = 12;
8533
8562
  }
8534
- if (text.charCodeAt(pos2 + 1) === 42) {
8535
- pos2 += 2;
8563
+ if (text.charCodeAt(pos3 + 1) === 42) {
8564
+ pos3 += 2;
8536
8565
  const safeLength = len - 1;
8537
8566
  let commentClosed = false;
8538
- while (pos2 < safeLength) {
8539
- const ch = text.charCodeAt(pos2);
8540
- if (ch === 42 && text.charCodeAt(pos2 + 1) === 47) {
8541
- pos2 += 2;
8567
+ while (pos3 < safeLength) {
8568
+ const ch = text.charCodeAt(pos3);
8569
+ if (ch === 42 && text.charCodeAt(pos3 + 1) === 47) {
8570
+ pos3 += 2;
8542
8571
  commentClosed = true;
8543
8572
  break;
8544
8573
  }
8545
- pos2++;
8574
+ pos3++;
8546
8575
  if (isLineBreak(ch)) {
8547
- if (ch === 13 && text.charCodeAt(pos2) === 10) {
8548
- pos2++;
8576
+ if (ch === 13 && text.charCodeAt(pos3) === 10) {
8577
+ pos3++;
8549
8578
  }
8550
8579
  lineNumber++;
8551
- tokenLineStartOffset = pos2;
8580
+ tokenLineStartOffset = pos3;
8552
8581
  }
8553
8582
  }
8554
8583
  if (!commentClosed) {
8555
- pos2++;
8584
+ pos3++;
8556
8585
  scanError = 1;
8557
8586
  }
8558
- value = text.substring(start, pos2);
8587
+ value = text.substring(start, pos3);
8559
8588
  return token = 13;
8560
8589
  }
8561
8590
  value += String.fromCharCode(code);
8562
- pos2++;
8591
+ pos3++;
8563
8592
  return token = 16;
8564
8593
  // numbers
8565
8594
  case 45:
8566
8595
  value += String.fromCharCode(code);
8567
- pos2++;
8568
- if (pos2 === len || !isDigit(text.charCodeAt(pos2))) {
8596
+ pos3++;
8597
+ if (pos3 === len || !isDigit(text.charCodeAt(pos3))) {
8569
8598
  return token = 16;
8570
8599
  }
8571
8600
  // found a minus, followed by a number so
@@ -8585,12 +8614,12 @@ function createScanner(text, ignoreTrivia = false) {
8585
8614
  return token = 11;
8586
8615
  // literals and unknown symbols
8587
8616
  default:
8588
- while (pos2 < len && isUnknownContentCharacter(code)) {
8589
- pos2++;
8590
- code = text.charCodeAt(pos2);
8617
+ while (pos3 < len && isUnknownContentCharacter(code)) {
8618
+ pos3++;
8619
+ code = text.charCodeAt(pos3);
8591
8620
  }
8592
- if (tokenOffset !== pos2) {
8593
- value = text.substring(tokenOffset, pos2);
8621
+ if (tokenOffset !== pos3) {
8622
+ value = text.substring(tokenOffset, pos3);
8594
8623
  switch (value) {
8595
8624
  case "true":
8596
8625
  return token = 8;
@@ -8602,7 +8631,7 @@ function createScanner(text, ignoreTrivia = false) {
8602
8631
  return token = 16;
8603
8632
  }
8604
8633
  value += String.fromCharCode(code);
8605
- pos2++;
8634
+ pos3++;
8606
8635
  return token = 16;
8607
8636
  }
8608
8637
  }
@@ -8632,12 +8661,12 @@ function createScanner(text, ignoreTrivia = false) {
8632
8661
  }
8633
8662
  return {
8634
8663
  setPosition,
8635
- getPosition: () => pos2,
8664
+ getPosition: () => pos3,
8636
8665
  scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
8637
8666
  getToken: () => token,
8638
8667
  getTokenValue: () => value,
8639
8668
  getTokenOffset: () => tokenOffset,
8640
- getTokenLength: () => pos2 - tokenOffset,
8669
+ getTokenLength: () => pos3 - tokenOffset,
8641
8670
  getTokenStartLine: () => lineStartOffset,
8642
8671
  getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
8643
8672
  getTokenError: () => scanError
@@ -10306,6 +10335,33 @@ function panelSize(panel, mode) {
10306
10335
  return { span: panelSpan(panel, "phone"), h: clampH(panel.mobile?.h ?? p.h) };
10307
10336
  }
10308
10337
  function layoutCanvas(panels, order, mode, width, override) {
10338
+ return mode === "grid" ? layoutGridCanvas(panels, width, override) : layoutPhoneCanvas(panels, order, width, override);
10339
+ }
10340
+ function layoutGridCanvas(panels, width, override) {
10341
+ const colW = (width + CANVAS_GAP) / GRID_COLUMNS;
10342
+ let boxes = gridBoxes(panels);
10343
+ if (override && boxes[override.index]) {
10344
+ const b = { ...boxes[override.index] };
10345
+ if (!isRow(panels[override.index])) {
10346
+ if (override.span !== void 0) b.w = clampW(override.span);
10347
+ if (override.h !== void 0) b.h = clampH(override.h);
10348
+ }
10349
+ if (override.x !== void 0) b.x = isRow(panels[override.index]) ? 0 : Math.min(GRID_COLUMNS - b.w, Math.max(0, Math.round(override.x)));
10350
+ if (override.y !== void 0) b.y = Math.max(0, Math.round(override.y));
10351
+ b.x = Math.min(b.x, GRID_COLUMNS - b.w);
10352
+ boxes = [...boxes];
10353
+ boxes[override.index] = b;
10354
+ }
10355
+ const settled = settleBoxes(boxes, override?.index);
10356
+ const items = settled.map((b, index) => {
10357
+ const row = isRow(panels[index]);
10358
+ return { index, row, x: b.x * colW, y: gridOffsetPx(b.y), width: b.w * colW - CANVAS_GAP, height: row ? gridUnitsToPx(1) : gridUnitsToPx(b.h), span: b.w, h: b.h };
10359
+ });
10360
+ items.sort((a, b) => a.y - b.y || a.x - b.x);
10361
+ return { items, height: Math.max(0, ...items.map((c) => c.y + c.height)), hidden: [] };
10362
+ }
10363
+ function layoutPhoneCanvas(panels, order, width, override) {
10364
+ const mode = "phone";
10309
10365
  const colW = (width + CANVAS_GAP) / GRID_COLUMNS;
10310
10366
  const items = [];
10311
10367
  const hidden = [];
@@ -10344,7 +10400,8 @@ function layoutCanvas(panels, order, mode, width, override) {
10344
10400
  hidden.push(index);
10345
10401
  continue;
10346
10402
  }
10347
- const size = override?.index === index ? { span: clampW(override.span), h: clampH(override.h) } : panelSize(panel, mode);
10403
+ const natural = panelSize(panel, mode);
10404
+ const size = override?.index === index ? { span: clampW(override.span ?? natural.span), h: clampH(override.h ?? natural.h) } : natural;
10348
10405
  if (used + size.span > GRID_COLUMNS) flush();
10349
10406
  line.push({ index, row: false, x: 0, y: 0, width: 0, height: 0, ...size });
10350
10407
  used += size.span;
@@ -10365,16 +10422,21 @@ function dropTarget(canvas, x, y, dragging) {
10365
10422
  return items[items.indexOf(best) + 1]?.index ?? null;
10366
10423
  }
10367
10424
  function movePanel(panels, index, before) {
10368
- const order = readingOrder(panels).filter((i) => i !== index);
10369
- const at = before === null ? order.length : order.indexOf(before);
10370
- order.splice(at === -1 ? order.length : at, 0, index);
10371
- return repack(panels, order);
10425
+ if (before === index) return panels;
10426
+ const others = panels.filter((_, i) => i !== index);
10427
+ const at = before === null || !panels[before] ? { x: 0, y: bottom(others) } : pos(panels[before]);
10428
+ return placePanel(panels, index, at.x, at.y);
10372
10429
  }
10373
10430
  function resizePanel(panels, index, mode, span, h) {
10374
10431
  const out = [...panels];
10375
10432
  const p = panels[index];
10376
- out[index] = mode === "grid" ? { ...p, gridPos: { ...pos(p), w: clampW(span), h: clampH(h) } } : { ...p, mobile: { ...p.mobile, span: clampW(span), h: clampH(h) } };
10377
- return repack(out, readingOrder(out));
10433
+ if (mode === "phone") {
10434
+ out[index] = { ...p, mobile: { ...p.mobile, span: clampW(span), h: clampH(h) } };
10435
+ return out;
10436
+ }
10437
+ const w = clampW(span);
10438
+ out[index] = { ...p, gridPos: { ...pos(p), x: Math.min(pos(p).x, GRID_COLUMNS - w), w, h: clampH(h) } };
10439
+ return settleLayout(out, index);
10378
10440
  }
10379
10441
  function setHiddenOnPhones(panels, index, hidden) {
10380
10442
  const out = [...panels];
@@ -10389,34 +10451,78 @@ function nextPanelId(panels) {
10389
10451
  }
10390
10452
  function insertPanel(panels, panel, after) {
10391
10453
  const index = panels.length;
10392
- const out = [...panels, { ...panel, id: nextPanelId(panels), gridPos: { ...DEFAULT_POS, ...panel.gridPos } }];
10393
- const order = readingOrder(panels);
10394
- order.splice(after === null ? order.length : order.indexOf(after) + 1, 0, index);
10395
- return { panels: repack(out, order), index };
10454
+ const p = { ...DEFAULT_POS, ...panel.gridPos };
10455
+ const w = isRow(panel) ? GRID_COLUMNS : clampW(p.w);
10456
+ let at;
10457
+ if (after === null || !panels[after]) at = { x: 0, y: bottom(panels) };
10458
+ else {
10459
+ const a = pos(panels[after]);
10460
+ at = !isRow(panel) && !isRow(panels[after]) && a.x + a.w + w <= GRID_COLUMNS ? { x: a.x + a.w, y: a.y } : { x: isRow(panel) ? 0 : Math.min(a.x, GRID_COLUMNS - w), y: a.y + a.h };
10461
+ }
10462
+ const out = [...panels, { ...panel, id: nextPanelId(panels), gridPos: { ...p, ...at, w } }];
10463
+ return { panels: settleLayout(out, index), index };
10396
10464
  }
10397
10465
  function duplicatePanel(panels, index) {
10398
10466
  return insertPanel(panels, structuredClone(panels[index]), index);
10399
10467
  }
10400
10468
  function removePanel(panels, index) {
10401
- const out = panels.filter((_, i) => i !== index);
10402
- return repack(out, readingOrder(out));
10469
+ return settleLayout(panels.filter((_, i) => i !== index));
10403
10470
  }
10404
10471
  function expandRow(panels, index) {
10405
10472
  const row = panels[index];
10406
10473
  const children = row.panels ?? [];
10407
- const out = [...panels, ...children];
10474
+ const top = pos(row).y + 1;
10475
+ const first = Math.min(...children.map((c) => pos(c).y));
10476
+ const height = Math.max(0, ...children.map((c) => pos(c).y - first + pos(c).h));
10477
+ const out = panels.map((p, i) => i !== index && pos(p).y > pos(row).y ? { ...p, gridPos: { ...pos(p), y: pos(p).y + height } } : p);
10408
10478
  out[index] = { ...row, collapsed: false, panels: [] };
10479
+ out.push(...children.map((c) => ({ ...c, gridPos: { ...pos(c), y: top + pos(c).y - first } })));
10480
+ return settleLayout(out);
10481
+ }
10482
+ function collapseRow(panels, index) {
10409
10483
  const order = readingOrder(panels);
10410
- order.splice(order.indexOf(index) + 1, 0, ...children.map((_, i) => panels.length + i));
10411
- return repack(out, order);
10484
+ const at = order.indexOf(index);
10485
+ const next = order.findIndex((i, n) => n > at && isRow(panels[i]));
10486
+ const removed = order.slice(at + 1, next === -1 ? order.length : next).sort((a, b) => a - b);
10487
+ const out = panels.filter((_, i) => !removed.includes(i));
10488
+ const rowAt = index - removed.filter((i) => i < index).length;
10489
+ out[rowAt] = { ...panels[index], collapsed: true, panels: removed.map((i) => panels[i]) };
10490
+ return { panels: settleLayout(out), removed };
10491
+ }
10492
+ var bottom = (panels) => Math.max(0, ...panels.map((p) => pos(p).y + pos(p).h));
10493
+ var gridBoxes = (panels) => panels.map((p) => isRow(p) ? { ...pos(p), x: 0, w: GRID_COLUMNS, h: 1 } : pos(p));
10494
+ function settleBoxes(boxes, fixed2) {
10495
+ const keep = new Set(fixed2 === void 0 ? [] : [fixed2].flat());
10496
+ const made = keep.size ? compactGrid(boxes, keep) : boxes;
10497
+ return compactGrid(made);
10498
+ }
10499
+ function writeBoxes(panels, boxes) {
10500
+ return panels.map((p, i) => {
10501
+ const before = p.gridPos;
10502
+ const b = boxes[i];
10503
+ return before && before.x === b.x && before.y === b.y && before.w === b.w && before.h === b.h ? p : { ...p, gridPos: { ...pos(p), ...b } };
10504
+ });
10505
+ }
10506
+ function settleLayout(panels, fixed2) {
10507
+ return writeBoxes(panels, settleBoxes(gridBoxes(panels), fixed2));
10508
+ }
10509
+ function placePanel(panels, index, x, y) {
10510
+ const out = [...panels];
10511
+ const p = pos(panels[index]);
10512
+ const w = isRow(panels[index]) ? GRID_COLUMNS : clampW(p.w);
10513
+ out[index] = { ...panels[index], gridPos: { ...p, x: Math.min(GRID_COLUMNS - w, Math.max(0, Math.round(x))), y: Math.max(0, Math.round(y)) } };
10514
+ return settleLayout(out, index);
10515
+ }
10516
+ function snapToGrid(width, x, y, w) {
10517
+ const colW = (width + CANVAS_GAP) / GRID_COLUMNS;
10518
+ return { x: Math.min(GRID_COLUMNS - w, Math.max(0, Math.round(x / colW))), y: Math.max(0, Math.round(y / gridOffsetPx(1))) };
10412
10519
  }
10413
10520
  function panelEdits(before, after, removed) {
10414
10521
  const edits = [];
10415
10522
  let base = before;
10416
- if (removed !== void 0) {
10417
- edits.push({ path: ["panels", removed], remove: true });
10418
- base = before.filter((_, i) => i !== removed);
10419
- }
10523
+ const gone = [...new Set(removed === void 0 ? [] : [removed].flat())].sort((a, b) => b - a);
10524
+ for (const i of gone) edits.push({ path: ["panels", i], remove: true });
10525
+ if (gone.length) base = before.filter((_, i) => !gone.includes(i));
10420
10526
  base.forEach((p, i) => edits.push(...diffJson(p, after[i], ["panels", i])));
10421
10527
  for (let i = base.length; i < after.length; i++) edits.push({ path: ["panels", i], value: after[i], insert: true });
10422
10528
  return edits;
@@ -10496,6 +10602,170 @@ function sampleEntries(forms, metrics, now = Date.now(), days = 180) {
10496
10602
  return out;
10497
10603
  }
10498
10604
 
10605
+ // src/core/snippets.ts
10606
+ var SNIPPET_VERSION = 1;
10607
+ var DEFAULT_POS2 = { x: 0, y: 0, w: GRID_COLUMNS, h: 4 };
10608
+ var pos2 = (p) => ({ ...DEFAULT_POS2, ...p.gridPos });
10609
+ var isObject4 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
10610
+ var refers2 = (name) => new RegExp(`\\$${name}\\b|\\$\\{${name}(:\\w+)?\\}|\\[\\[${name}(:\\w+)?\\]\\]`);
10611
+ var withChildren = (p) => [p, ...isRow(p) ? p.panels ?? [] : []];
10612
+ function panelMetrics(p) {
10613
+ const found = [];
10614
+ if (isInputType(p.type) && typeof p.options?.metric === "string") found.push(p.options.metric);
10615
+ for (const t of p.targets ?? []) if (typeof t.metric === "string" && t.metric && !t.metric.startsWith("$")) found.push(t.metric);
10616
+ return found;
10617
+ }
10618
+ function renameMetrics(panels, map) {
10619
+ const rename = (p) => {
10620
+ let out = p;
10621
+ if (isInputType(p.type) && typeof p.options?.metric === "string" && map[p.options.metric]) out = { ...out, options: { ...out.options, metric: map[p.options.metric] } };
10622
+ if (p.targets?.some((t) => typeof t.metric === "string" && map[t.metric])) out = { ...out, targets: p.targets.map((t) => typeof t.metric === "string" && map[t.metric] ? { ...t, metric: map[t.metric] } : t) };
10623
+ if (p.panels?.length) out = { ...out, panels: p.panels.map(rename) };
10624
+ return out;
10625
+ };
10626
+ return panels.map(rename);
10627
+ }
10628
+ function copyPanels(dashboard, indices) {
10629
+ const panels = dashboard.panels ?? [];
10630
+ const order = readingOrder(panels);
10631
+ const picked = /* @__PURE__ */ new Set();
10632
+ for (const i of indices) {
10633
+ if (!panels[i]) continue;
10634
+ picked.add(i);
10635
+ if (isRow(panels[i]) && !panels[i].collapsed) {
10636
+ for (const j of order.slice(order.indexOf(i) + 1)) {
10637
+ if (isRow(panels[j])) break;
10638
+ picked.add(j);
10639
+ }
10640
+ }
10641
+ }
10642
+ const chosen = order.filter((i) => picked.has(i)).map((i) => panels[i]);
10643
+ return snippetOf(chosen, dashboard.templating?.list ?? []);
10644
+ }
10645
+ function snippetOf(panels, available) {
10646
+ const left = Math.min(...panels.map((p) => pos2(p).x));
10647
+ const top = Math.min(...panels.map((p) => pos2(p).y));
10648
+ const shift = (p) => {
10649
+ const { id: _, ...rest } = p;
10650
+ const g = pos2(p);
10651
+ return { ...rest, gridPos: { ...g, x: g.x - (isRow(p) ? 0 : left), y: g.y - top }, ...p.panels && { panels: p.panels.map(shift) } };
10652
+ };
10653
+ const out = panels.map(shift);
10654
+ const text = JSON.stringify(panels);
10655
+ const names = available.map((v) => v.name);
10656
+ const used = new Set(names.filter((n) => panels.some((p) => withChildren(p).some((c) => c.repeat === n)) || refers2(n).test(text)));
10657
+ for (let grew = true; grew; ) {
10658
+ grew = false;
10659
+ for (const v of available) {
10660
+ if (!used.has(v.name)) continue;
10661
+ for (const r of variableReferences(v, names)) {
10662
+ if (used.has(r)) continue;
10663
+ used.add(r);
10664
+ grew = true;
10665
+ }
10666
+ }
10667
+ }
10668
+ const all = panels.flatMap(withChildren);
10669
+ return {
10670
+ $modelform: "panels",
10671
+ version: SNIPPET_VERSION,
10672
+ kind: all.some((p) => isInputType(p.type)) ? "form" : "dashboard",
10673
+ panels: out,
10674
+ variables: available.filter((v) => used.has(v.name)),
10675
+ metrics: [...new Set(all.flatMap(panelMetrics))]
10676
+ };
10677
+ }
10678
+ var looksLikePanel = (v) => isObject4(v) && typeof v.type === "string";
10679
+ function parseSnippet(text) {
10680
+ let json;
10681
+ try {
10682
+ json = JSON.parse(text);
10683
+ } catch {
10684
+ return { error: "That isn\u2019t JSON" };
10685
+ }
10686
+ let panels;
10687
+ let variables = [];
10688
+ if (isObject4(json) && json.$modelform === "panels") {
10689
+ if (typeof json.version !== "number" || json.version > SNIPPET_VERSION) return { error: "Those panels come from a newer version of modelform" };
10690
+ if (!Array.isArray(json.panels)) return { error: "The copied panels are missing their panels" };
10691
+ panels = json.panels;
10692
+ variables = Array.isArray(json.variables) ? json.variables : [];
10693
+ } else if (Array.isArray(json)) panels = json;
10694
+ else if (isObject4(json) && Array.isArray(json.panels) && !looksLikePanel(json)) {
10695
+ panels = json.panels;
10696
+ const templating = json.templating;
10697
+ variables = isObject4(templating) && Array.isArray(templating.list) ? templating.list : [];
10698
+ } else if (looksLikePanel(json)) panels = [json];
10699
+ else return { error: "That isn\u2019t a panel, a list of panels or a dashboard" };
10700
+ const usable = panels.filter(looksLikePanel);
10701
+ if (!usable.length) return { error: "There are no panels in it" };
10702
+ const vars = variables.filter((v) => isObject4(v) && typeof v.name === "string" && typeof v.type === "string");
10703
+ return { snippet: snippetOf(usable, vars) };
10704
+ }
10705
+ var stable = (v) => Array.isArray(v) ? `[${v.map(stable).join(",")}]` : isObject4(v) ? `{${Object.keys(v).sort().map((k) => `${JSON.stringify(k)}:${stable(v[k])}`).join(",")}}` : JSON.stringify(v);
10706
+ var sameVariable = (a, b) => {
10707
+ const strip = ({ current: _, options: __, ...rest }) => stable(rest);
10708
+ return strip(a) === strip(b);
10709
+ };
10710
+ function previewPaste(dashboard, snippet) {
10711
+ const { added, renamed } = mergeVariables(dashboard.templating?.list ?? [], snippet.variables);
10712
+ return { added, renamed };
10713
+ }
10714
+ function mergeVariables(existing, incoming) {
10715
+ const taken = new Set(existing.map((v) => v.name));
10716
+ const added = [];
10717
+ const renamed = {};
10718
+ const out = [...existing];
10719
+ for (const v of incoming) {
10720
+ const same = existing.find((e) => e.name === v.name);
10721
+ if (same && sameVariable(same, v)) continue;
10722
+ let name = v.name;
10723
+ if (same) {
10724
+ let n = 2;
10725
+ while (taken.has(`${v.name}_${n}`)) n++;
10726
+ name = `${v.name}_${n}`;
10727
+ renamed[v.name] = name;
10728
+ }
10729
+ taken.add(name);
10730
+ added.push(name);
10731
+ out.push({ ...v, name });
10732
+ }
10733
+ const follow = (v) => Object.entries(renamed).reduce((acc, [from, to]) => renameVariableInPanels([acc], from, to)[0], v);
10734
+ return { variables: out.map((v, i) => i < existing.length ? v : follow(v)), added, renamed };
10735
+ }
10736
+ function pasteSnippet(dashboard, snippet, options = {}) {
10737
+ const existing = dashboard.panels ?? [];
10738
+ const kind = options.kind ?? (existing.flatMap(withChildren).some((p) => isInputType(p.type)) ? "form" : "dashboard");
10739
+ const skipped = [];
10740
+ const keep = (p) => {
10741
+ if (kind === "dashboard" && isInputType(p.type)) {
10742
+ skipped.push(p.title || p.type);
10743
+ return false;
10744
+ }
10745
+ return true;
10746
+ };
10747
+ let incoming = snippet.panels.filter(keep).map((p) => p.panels ? { ...p, panels: p.panels.filter(keep) } : p);
10748
+ const { variables, added, renamed } = mergeVariables(dashboard.templating?.list ?? [], snippet.variables);
10749
+ for (const [from, to] of Object.entries(renamed)) incoming = renameVariableInPanels(incoming, from, to);
10750
+ if (options.metrics) incoming = renameMetrics(incoming, options.metrics);
10751
+ const after = options.after != null ? existing[options.after] : void 0;
10752
+ const top = after ? pos2(after).y + pos2(after).h : Math.max(0, ...existing.map((p) => pos2(p).y + pos2(p).h));
10753
+ let id = nextPanelId(existing);
10754
+ const place = (p) => ({ ...p, id: id++, gridPos: { ...pos2(p), y: pos2(p).y + top }, ...p.panels && { panels: p.panels.map(place) } });
10755
+ const pasted = incoming.map(place);
10756
+ const index = existing.length;
10757
+ const all = [...existing, ...pasted];
10758
+ return {
10759
+ panels: settleLayout(all, pasted.map((_, i) => index + i)),
10760
+ variables,
10761
+ index,
10762
+ count: pasted.length,
10763
+ added,
10764
+ renamed,
10765
+ skipped
10766
+ };
10767
+ }
10768
+
10499
10769
  export {
10500
10770
  frameLength,
10501
10771
  timeField,
@@ -10536,6 +10806,8 @@ export {
10536
10806
  valueText,
10537
10807
  GRID_COLUMNS,
10538
10808
  gridUnitsToPx,
10809
+ gridOffsetPx,
10810
+ compactGrid,
10539
10811
  isRow,
10540
10812
  layoutDashboard,
10541
10813
  repeatOptions,
@@ -10685,6 +10957,15 @@ export {
10685
10957
  duplicatePanel,
10686
10958
  removePanel,
10687
10959
  expandRow,
10960
+ collapseRow,
10961
+ settleLayout,
10962
+ placePanel,
10963
+ snapToGrid,
10688
10964
  panelEdits,
10689
- sampleEntries
10965
+ sampleEntries,
10966
+ SNIPPET_VERSION,
10967
+ copyPanels,
10968
+ parseSnippet,
10969
+ previewPaste,
10970
+ pasteSnippet
10690
10971
  };