@modelform/react-native 0.3.0 → 0.5.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.
@@ -8347,12 +8347,12 @@ function documentUses(manifest) {
8347
8347
  // node_modules/.pnpm/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
8348
8348
  function createScanner(text, ignoreTrivia = false) {
8349
8349
  const len = text.length;
8350
- 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;
8351
8351
  function scanHexDigits(count, exact) {
8352
8352
  let digits = 0;
8353
8353
  let value2 = 0;
8354
8354
  while (digits < count || !exact) {
8355
- let ch = text.charCodeAt(pos2);
8355
+ let ch = text.charCodeAt(pos3);
8356
8356
  if (ch >= 48 && ch <= 57) {
8357
8357
  value2 = value2 * 16 + ch - 48;
8358
8358
  } else if (ch >= 65 && ch <= 70) {
@@ -8362,7 +8362,7 @@ function createScanner(text, ignoreTrivia = false) {
8362
8362
  } else {
8363
8363
  break;
8364
8364
  }
8365
- pos2++;
8365
+ pos3++;
8366
8366
  digits++;
8367
8367
  }
8368
8368
  if (digits < count) {
@@ -8371,46 +8371,46 @@ function createScanner(text, ignoreTrivia = false) {
8371
8371
  return value2;
8372
8372
  }
8373
8373
  function setPosition(newPosition) {
8374
- pos2 = newPosition;
8374
+ pos3 = newPosition;
8375
8375
  value = "";
8376
8376
  tokenOffset = 0;
8377
8377
  token = 16;
8378
8378
  scanError = 0;
8379
8379
  }
8380
8380
  function scanNumber() {
8381
- let start = pos2;
8382
- if (text.charCodeAt(pos2) === 48) {
8383
- pos2++;
8381
+ let start = pos3;
8382
+ if (text.charCodeAt(pos3) === 48) {
8383
+ pos3++;
8384
8384
  } else {
8385
- pos2++;
8386
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8387
- pos2++;
8385
+ pos3++;
8386
+ while (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8387
+ pos3++;
8388
8388
  }
8389
8389
  }
8390
- if (pos2 < text.length && text.charCodeAt(pos2) === 46) {
8391
- pos2++;
8392
- if (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8393
- pos2++;
8394
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8395
- 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++;
8396
8396
  }
8397
8397
  } else {
8398
8398
  scanError = 3;
8399
- return text.substring(start, pos2);
8399
+ return text.substring(start, pos3);
8400
8400
  }
8401
8401
  }
8402
- let end = pos2;
8403
- if (pos2 < text.length && (text.charCodeAt(pos2) === 69 || text.charCodeAt(pos2) === 101)) {
8404
- pos2++;
8405
- if (pos2 < text.length && text.charCodeAt(pos2) === 43 || text.charCodeAt(pos2) === 45) {
8406
- 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++;
8407
8407
  }
8408
- if (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8409
- pos2++;
8410
- while (pos2 < text.length && isDigit(text.charCodeAt(pos2))) {
8411
- pos2++;
8408
+ if (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8409
+ pos3++;
8410
+ while (pos3 < text.length && isDigit(text.charCodeAt(pos3))) {
8411
+ pos3++;
8412
8412
  }
8413
- end = pos2;
8413
+ end = pos3;
8414
8414
  } else {
8415
8415
  scanError = 3;
8416
8416
  }
@@ -8418,27 +8418,27 @@ function createScanner(text, ignoreTrivia = false) {
8418
8418
  return text.substring(start, end);
8419
8419
  }
8420
8420
  function scanString() {
8421
- let result = "", start = pos2;
8421
+ let result = "", start = pos3;
8422
8422
  while (true) {
8423
- if (pos2 >= len) {
8424
- result += text.substring(start, pos2);
8423
+ if (pos3 >= len) {
8424
+ result += text.substring(start, pos3);
8425
8425
  scanError = 2;
8426
8426
  break;
8427
8427
  }
8428
- const ch = text.charCodeAt(pos2);
8428
+ const ch = text.charCodeAt(pos3);
8429
8429
  if (ch === 34) {
8430
- result += text.substring(start, pos2);
8431
- pos2++;
8430
+ result += text.substring(start, pos3);
8431
+ pos3++;
8432
8432
  break;
8433
8433
  }
8434
8434
  if (ch === 92) {
8435
- result += text.substring(start, pos2);
8436
- pos2++;
8437
- if (pos2 >= len) {
8435
+ result += text.substring(start, pos3);
8436
+ pos3++;
8437
+ if (pos3 >= len) {
8438
8438
  scanError = 2;
8439
8439
  break;
8440
8440
  }
8441
- const ch2 = text.charCodeAt(pos2++);
8441
+ const ch2 = text.charCodeAt(pos3++);
8442
8442
  switch (ch2) {
8443
8443
  case 34:
8444
8444
  result += '"';
@@ -8475,126 +8475,126 @@ function createScanner(text, ignoreTrivia = false) {
8475
8475
  default:
8476
8476
  scanError = 5;
8477
8477
  }
8478
- start = pos2;
8478
+ start = pos3;
8479
8479
  continue;
8480
8480
  }
8481
8481
  if (ch >= 0 && ch <= 31) {
8482
8482
  if (isLineBreak(ch)) {
8483
- result += text.substring(start, pos2);
8483
+ result += text.substring(start, pos3);
8484
8484
  scanError = 2;
8485
8485
  break;
8486
8486
  } else {
8487
8487
  scanError = 6;
8488
8488
  }
8489
8489
  }
8490
- pos2++;
8490
+ pos3++;
8491
8491
  }
8492
8492
  return result;
8493
8493
  }
8494
8494
  function scanNext() {
8495
8495
  value = "";
8496
8496
  scanError = 0;
8497
- tokenOffset = pos2;
8497
+ tokenOffset = pos3;
8498
8498
  lineStartOffset = lineNumber;
8499
8499
  prevTokenLineStartOffset = tokenLineStartOffset;
8500
- if (pos2 >= len) {
8500
+ if (pos3 >= len) {
8501
8501
  tokenOffset = len;
8502
8502
  return token = 17;
8503
8503
  }
8504
- let code = text.charCodeAt(pos2);
8504
+ let code = text.charCodeAt(pos3);
8505
8505
  if (isWhiteSpace(code)) {
8506
8506
  do {
8507
- pos2++;
8507
+ pos3++;
8508
8508
  value += String.fromCharCode(code);
8509
- code = text.charCodeAt(pos2);
8509
+ code = text.charCodeAt(pos3);
8510
8510
  } while (isWhiteSpace(code));
8511
8511
  return token = 15;
8512
8512
  }
8513
8513
  if (isLineBreak(code)) {
8514
- pos2++;
8514
+ pos3++;
8515
8515
  value += String.fromCharCode(code);
8516
- if (code === 13 && text.charCodeAt(pos2) === 10) {
8517
- pos2++;
8516
+ if (code === 13 && text.charCodeAt(pos3) === 10) {
8517
+ pos3++;
8518
8518
  value += "\n";
8519
8519
  }
8520
8520
  lineNumber++;
8521
- tokenLineStartOffset = pos2;
8521
+ tokenLineStartOffset = pos3;
8522
8522
  return token = 14;
8523
8523
  }
8524
8524
  switch (code) {
8525
8525
  // tokens: []{}:,
8526
8526
  case 123:
8527
- pos2++;
8527
+ pos3++;
8528
8528
  return token = 1;
8529
8529
  case 125:
8530
- pos2++;
8530
+ pos3++;
8531
8531
  return token = 2;
8532
8532
  case 91:
8533
- pos2++;
8533
+ pos3++;
8534
8534
  return token = 3;
8535
8535
  case 93:
8536
- pos2++;
8536
+ pos3++;
8537
8537
  return token = 4;
8538
8538
  case 58:
8539
- pos2++;
8539
+ pos3++;
8540
8540
  return token = 6;
8541
8541
  case 44:
8542
- pos2++;
8542
+ pos3++;
8543
8543
  return token = 5;
8544
8544
  // strings
8545
8545
  case 34:
8546
- pos2++;
8546
+ pos3++;
8547
8547
  value = scanString();
8548
8548
  return token = 10;
8549
8549
  // comments
8550
8550
  case 47:
8551
- const start = pos2 - 1;
8552
- if (text.charCodeAt(pos2 + 1) === 47) {
8553
- pos2 += 2;
8554
- while (pos2 < len) {
8555
- 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))) {
8556
8556
  break;
8557
8557
  }
8558
- pos2++;
8558
+ pos3++;
8559
8559
  }
8560
- value = text.substring(start, pos2);
8560
+ value = text.substring(start, pos3);
8561
8561
  return token = 12;
8562
8562
  }
8563
- if (text.charCodeAt(pos2 + 1) === 42) {
8564
- pos2 += 2;
8563
+ if (text.charCodeAt(pos3 + 1) === 42) {
8564
+ pos3 += 2;
8565
8565
  const safeLength = len - 1;
8566
8566
  let commentClosed = false;
8567
- while (pos2 < safeLength) {
8568
- const ch = text.charCodeAt(pos2);
8569
- if (ch === 42 && text.charCodeAt(pos2 + 1) === 47) {
8570
- pos2 += 2;
8567
+ while (pos3 < safeLength) {
8568
+ const ch = text.charCodeAt(pos3);
8569
+ if (ch === 42 && text.charCodeAt(pos3 + 1) === 47) {
8570
+ pos3 += 2;
8571
8571
  commentClosed = true;
8572
8572
  break;
8573
8573
  }
8574
- pos2++;
8574
+ pos3++;
8575
8575
  if (isLineBreak(ch)) {
8576
- if (ch === 13 && text.charCodeAt(pos2) === 10) {
8577
- pos2++;
8576
+ if (ch === 13 && text.charCodeAt(pos3) === 10) {
8577
+ pos3++;
8578
8578
  }
8579
8579
  lineNumber++;
8580
- tokenLineStartOffset = pos2;
8580
+ tokenLineStartOffset = pos3;
8581
8581
  }
8582
8582
  }
8583
8583
  if (!commentClosed) {
8584
- pos2++;
8584
+ pos3++;
8585
8585
  scanError = 1;
8586
8586
  }
8587
- value = text.substring(start, pos2);
8587
+ value = text.substring(start, pos3);
8588
8588
  return token = 13;
8589
8589
  }
8590
8590
  value += String.fromCharCode(code);
8591
- pos2++;
8591
+ pos3++;
8592
8592
  return token = 16;
8593
8593
  // numbers
8594
8594
  case 45:
8595
8595
  value += String.fromCharCode(code);
8596
- pos2++;
8597
- if (pos2 === len || !isDigit(text.charCodeAt(pos2))) {
8596
+ pos3++;
8597
+ if (pos3 === len || !isDigit(text.charCodeAt(pos3))) {
8598
8598
  return token = 16;
8599
8599
  }
8600
8600
  // found a minus, followed by a number so
@@ -8614,12 +8614,12 @@ function createScanner(text, ignoreTrivia = false) {
8614
8614
  return token = 11;
8615
8615
  // literals and unknown symbols
8616
8616
  default:
8617
- while (pos2 < len && isUnknownContentCharacter(code)) {
8618
- pos2++;
8619
- code = text.charCodeAt(pos2);
8617
+ while (pos3 < len && isUnknownContentCharacter(code)) {
8618
+ pos3++;
8619
+ code = text.charCodeAt(pos3);
8620
8620
  }
8621
- if (tokenOffset !== pos2) {
8622
- value = text.substring(tokenOffset, pos2);
8621
+ if (tokenOffset !== pos3) {
8622
+ value = text.substring(tokenOffset, pos3);
8623
8623
  switch (value) {
8624
8624
  case "true":
8625
8625
  return token = 8;
@@ -8631,7 +8631,7 @@ function createScanner(text, ignoreTrivia = false) {
8631
8631
  return token = 16;
8632
8632
  }
8633
8633
  value += String.fromCharCode(code);
8634
- pos2++;
8634
+ pos3++;
8635
8635
  return token = 16;
8636
8636
  }
8637
8637
  }
@@ -8661,12 +8661,12 @@ function createScanner(text, ignoreTrivia = false) {
8661
8661
  }
8662
8662
  return {
8663
8663
  setPosition,
8664
- getPosition: () => pos2,
8664
+ getPosition: () => pos3,
8665
8665
  scan: ignoreTrivia ? scanNextNonTrivia : scanNext,
8666
8666
  getToken: () => token,
8667
8667
  getTokenValue: () => value,
8668
8668
  getTokenOffset: () => tokenOffset,
8669
- getTokenLength: () => pos2 - tokenOffset,
8669
+ getTokenLength: () => pos3 - tokenOffset,
8670
8670
  getTokenStartLine: () => lineStartOffset,
8671
8671
  getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset,
8672
8672
  getTokenError: () => scanError
@@ -10492,7 +10492,8 @@ function collapseRow(panels, index) {
10492
10492
  var bottom = (panels) => Math.max(0, ...panels.map((p) => pos(p).y + pos(p).h));
10493
10493
  var gridBoxes = (panels) => panels.map((p) => isRow(p) ? { ...pos(p), x: 0, w: GRID_COLUMNS, h: 1 } : pos(p));
10494
10494
  function settleBoxes(boxes, fixed2) {
10495
- const made = fixed2 === void 0 ? boxes : compactGrid(boxes, /* @__PURE__ */ new Set([fixed2]));
10495
+ const keep = new Set(fixed2 === void 0 ? [] : [fixed2].flat());
10496
+ const made = keep.size ? compactGrid(boxes, keep) : boxes;
10496
10497
  return compactGrid(made);
10497
10498
  }
10498
10499
  function writeBoxes(panels, boxes) {
@@ -10601,6 +10602,309 @@ function sampleEntries(forms, metrics, now = Date.now(), days = 180) {
10601
10602
  return out;
10602
10603
  }
10603
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
+
10769
+ // src/core/templates.ts
10770
+ var TEMPLATE_VERSION = 1;
10771
+ var isObject5 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
10772
+ var allPanels3 = (d) => (d.panels ?? []).flatMap((p) => [p, ...p.type === "row" ? p.panels ?? [] : []]);
10773
+ var documentMetrics = (d) => [...new Set(allPanels3(d).flatMap(panelMetrics))];
10774
+ var recordedBy = (d) => inputPanels(d).map((p) => p.options.metric);
10775
+ var metricsOf = (documents) => [...new Set(documents.flatMap((d) => documentMetrics(d.dashboard)))];
10776
+ function tabsShowing(tabs, ids) {
10777
+ return tabs.filter((t) => {
10778
+ const shown = screenDocuments(t.screen);
10779
+ return shown.length > 0 && shown.every((d) => ids.has(d.id));
10780
+ });
10781
+ }
10782
+ function makeTemplate(manifest, documents, ids, meta) {
10783
+ const docs = ids.filter((id) => documents[id]).map((id) => ({ kind: inputPanels(documents[id]).length ? "form" : "dashboard", id, dashboard: structuredClone(documents[id]) }));
10784
+ return {
10785
+ $modelform: "template",
10786
+ version: TEMPLATE_VERSION,
10787
+ title: meta.title,
10788
+ description: meta.description ?? "",
10789
+ documents: docs,
10790
+ tabs: structuredClone(tabsShowing(manifest.tabs ?? [], new Set(docs.map((d) => d.id)))),
10791
+ metrics: metricsOf(docs)
10792
+ };
10793
+ }
10794
+ function parseTemplate(text) {
10795
+ let json;
10796
+ try {
10797
+ json = JSON.parse(text);
10798
+ } catch {
10799
+ return { error: "That isn\u2019t JSON" };
10800
+ }
10801
+ if (!isObject5(json) || json.$modelform !== "template") return { error: "That isn\u2019t a template" };
10802
+ if (typeof json.version !== "number" || json.version > TEMPLATE_VERSION) return { error: "That template comes from a newer version of modelform" };
10803
+ if (typeof json.title !== "string" || !json.title.trim()) return { error: "The template has no title" };
10804
+ if (!Array.isArray(json.documents)) return { error: "The template has no documents list" };
10805
+ const documents = [];
10806
+ const seen = /* @__PURE__ */ new Set();
10807
+ for (const [i, d] of json.documents.entries()) {
10808
+ if (!isObject5(d) || d.kind !== "form" && d.kind !== "dashboard" || typeof d.id !== "string" || !/^[A-Za-z0-9_-]+$/.test(d.id))
10809
+ return { error: `Document ${i + 1} needs a kind (form or dashboard) and an id of letters, digits, - or _` };
10810
+ if (seen.has(d.id)) return { error: `Two documents are called "${d.id}"` };
10811
+ seen.add(d.id);
10812
+ const { dashboard, issues } = prepareDashboard(d.dashboard, d.id);
10813
+ if (!dashboard) return { error: `"${d.id}" can\u2019t be shown: ${issues.find((x) => x.severity === "error")?.message ?? "it isn\u2019t a dashboard"}` };
10814
+ documents.push({ kind: d.kind, id: d.id, dashboard });
10815
+ }
10816
+ const tabs = Array.isArray(json.tabs) ? json.tabs.filter((t) => isObject5(t) && typeof t.id === "string" && typeof t.title === "string" && isObject5(t.screen)) : [];
10817
+ return {
10818
+ template: {
10819
+ $modelform: "template",
10820
+ version: TEMPLATE_VERSION,
10821
+ title: json.title.trim(),
10822
+ description: typeof json.description === "string" ? json.description : "",
10823
+ documents,
10824
+ tabs: tabsShowing(tabs, seen),
10825
+ metrics: metricsOf(documents)
10826
+ }
10827
+ };
10828
+ }
10829
+ function templateNeeds(template, recorded, chosen, metrics = {}) {
10830
+ const config = new Set(recorded);
10831
+ const docs = template.documents.filter((d) => !chosen || chosen.includes(d.id));
10832
+ const ownForms = new Set(docs.filter((d) => d.kind === "form").flatMap((d) => recordedBy(d.dashboard)));
10833
+ const unknown = [...new Set(docs.flatMap((d) => documentMetrics(d.dashboard)))].filter((m) => !config.has(m) && !ownForms.has(m) && !(m in metrics));
10834
+ const covered = template.documents.filter((d) => d.kind === "form").filter((d) => {
10835
+ const recordedHere = recordedBy(d.dashboard);
10836
+ return recordedHere.length > 0 && recordedHere.every((m) => config.has(metrics[m] ?? m));
10837
+ }).map((d) => d.id);
10838
+ return { unknown, covered };
10839
+ }
10840
+ function unique(base, free) {
10841
+ let id = base;
10842
+ for (let n = 2; !free(id); n++) id = `${base}-${n}`;
10843
+ return id;
10844
+ }
10845
+ function retarget(screen, ids) {
10846
+ switch (screen.type) {
10847
+ case "dashboard":
10848
+ return ids[screen.dashboard] ? { ...screen, dashboard: ids[screen.dashboard] } : null;
10849
+ case "forms": {
10850
+ const forms = screen.forms.filter((f) => ids[f]).map((f) => ids[f]);
10851
+ return forms.length ? { ...screen, forms } : null;
10852
+ }
10853
+ case "menu": {
10854
+ const items = screen.items.filter((i) => i.dashboard && ids[i.dashboard] || i.form && ids[i.form]).map((i) => ({ ...i, ...i.dashboard && { dashboard: ids[i.dashboard] }, ...i.form && { form: ids[i.form] } }));
10855
+ return items.length ? { ...screen, items } : null;
10856
+ }
10857
+ }
10858
+ }
10859
+ function applyTemplate(manifestText, paths, template, options = {}) {
10860
+ let manifest;
10861
+ try {
10862
+ manifest = JSON.parse(manifestText);
10863
+ } catch {
10864
+ throw new Error("The manifest isn\u2019t valid JSON");
10865
+ }
10866
+ const taken = new Set(Object.keys(manifest.documents ?? {}));
10867
+ const files = new Set(paths);
10868
+ const folder = (kind) => kind === "form" ? "forms" : "dashboards";
10869
+ const edits = [];
10870
+ const out = {};
10871
+ const ids = {};
10872
+ for (const doc of template.documents) {
10873
+ if (options.documents && !options.documents.includes(doc.id)) continue;
10874
+ const id = unique(doc.id, (c) => !taken.has(c) && !files.has(`${folder(doc.kind)}/${c}.json`));
10875
+ const path = `${folder(doc.kind)}/${id}.json`;
10876
+ taken.add(id);
10877
+ files.add(path);
10878
+ ids[doc.id] = id;
10879
+ const dashboard = options.metrics ? { ...doc.dashboard, panels: renameMetrics(doc.dashboard.panels ?? [], options.metrics) } : doc.dashboard;
10880
+ out[path] = JSON.stringify(dashboard, null, 2) + "\n";
10881
+ if (manifest.documents) edits.push({ path: ["documents", id], value: path });
10882
+ else {
10883
+ edits.push({ path: ["documents"], value: { [id]: path } });
10884
+ manifest.documents = {};
10885
+ }
10886
+ }
10887
+ const tabIds = (manifest.tabs ?? []).map((t) => t.id);
10888
+ const added = [];
10889
+ let at = manifest.tabs?.length ?? 0;
10890
+ for (const tab of template.tabs) {
10891
+ if (!options.tabs?.includes(tab.id)) continue;
10892
+ const screen = retarget(tab.screen, ids);
10893
+ if (!screen) continue;
10894
+ const id = tabIdFor(tab.id, tabIds);
10895
+ tabIds.push(id);
10896
+ added.push(id);
10897
+ const value = { ...tab, id, screen };
10898
+ if (manifest.tabs) edits.push({ path: ["tabs", at++], value, insert: true });
10899
+ else {
10900
+ edits.push({ path: ["tabs"], value: [value] });
10901
+ manifest.tabs = [value];
10902
+ at = 1;
10903
+ }
10904
+ }
10905
+ return { manifest: applyJsonEdits(manifestText, edits), files: out, ids, tabs: added };
10906
+ }
10907
+
10604
10908
  export {
10605
10909
  frameLength,
10606
10910
  timeField,
@@ -10797,5 +11101,17 @@ export {
10797
11101
  placePanel,
10798
11102
  snapToGrid,
10799
11103
  panelEdits,
10800
- sampleEntries
11104
+ sampleEntries,
11105
+ SNIPPET_VERSION,
11106
+ panelMetrics,
11107
+ renameMetrics,
11108
+ copyPanels,
11109
+ parseSnippet,
11110
+ previewPaste,
11111
+ pasteSnippet,
11112
+ TEMPLATE_VERSION,
11113
+ makeTemplate,
11114
+ parseTemplate,
11115
+ templateNeeds,
11116
+ applyTemplate
10801
11117
  };