@pixerate/editor 0.26.1 → 0.28.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.
Files changed (90) hide show
  1. package/README.md +32 -3
  2. package/dist/agent/index.cjs +56 -6
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts +6 -4
  5. package/dist/agent/index.d.ts +6 -4
  6. package/dist/agent/index.js +51 -1
  7. package/dist/agent/index.js.map +1 -1
  8. package/dist/animation/index.cjs +62 -0
  9. package/dist/animation/index.cjs.map +1 -0
  10. package/dist/animation/index.d.cts +296 -0
  11. package/dist/animation/index.d.ts +296 -0
  12. package/dist/animation/index.js +62 -0
  13. package/dist/animation/index.js.map +1 -0
  14. package/dist/{builtins-Chj6zv_O.d.ts → builtins-haN82vjT.d.ts} +33 -3
  15. package/dist/{builtins-CdPcLwwF.d.cts → builtins-nMXh9sT3.d.cts} +33 -3
  16. package/dist/canvas/index.cjs +535 -737
  17. package/dist/canvas/index.cjs.map +1 -1
  18. package/dist/canvas/index.d.cts +74 -291
  19. package/dist/canvas/index.d.ts +74 -291
  20. package/dist/canvas/index.js +417 -619
  21. package/dist/canvas/index.js.map +1 -1
  22. package/dist/{chunk-X7HXL2SB.js → chunk-3RPOW4CS.js} +342 -16
  23. package/dist/chunk-3RPOW4CS.js.map +1 -0
  24. package/dist/{chunk-UBCFIABU.js → chunk-5H2OPMXZ.js} +183 -6
  25. package/dist/chunk-5H2OPMXZ.js.map +1 -0
  26. package/dist/chunk-A6PHCAGX.cjs +87 -0
  27. package/dist/chunk-A6PHCAGX.cjs.map +1 -0
  28. package/dist/{chunk-HRMEDT2L.js → chunk-D6AVOTSD.js} +6 -85
  29. package/dist/chunk-D6AVOTSD.js.map +1 -0
  30. package/dist/chunk-HXDMZQTD.cjs +497 -0
  31. package/dist/chunk-HXDMZQTD.cjs.map +1 -0
  32. package/dist/chunk-IB3ZHQAX.js +497 -0
  33. package/dist/chunk-IB3ZHQAX.js.map +1 -0
  34. package/dist/{chunk-4BVP64JJ.cjs → chunk-K2UYKJ6M.cjs} +24 -103
  35. package/dist/chunk-K2UYKJ6M.cjs.map +1 -0
  36. package/dist/{chunk-HDYMLS3X.cjs → chunk-MC2LQN5S.cjs} +196 -19
  37. package/dist/chunk-MC2LQN5S.cjs.map +1 -0
  38. package/dist/{chunk-XQFHLT53.js → chunk-MXZJ4PKN.js} +24 -10
  39. package/dist/chunk-MXZJ4PKN.js.map +1 -0
  40. package/dist/{chunk-LN2POBHT.cjs → chunk-OYEFRO3D.cjs} +28 -14
  41. package/dist/chunk-OYEFRO3D.cjs.map +1 -0
  42. package/dist/chunk-PWJARCI4.js +637 -0
  43. package/dist/chunk-PWJARCI4.js.map +1 -0
  44. package/dist/chunk-TGD52FEZ.js +87 -0
  45. package/dist/chunk-TGD52FEZ.js.map +1 -0
  46. package/dist/chunk-TTYCN6ZH.cjs +637 -0
  47. package/dist/chunk-TTYCN6ZH.cjs.map +1 -0
  48. package/dist/{chunk-NQF32I6Z.cjs → chunk-U7VGDSNY.cjs} +463 -137
  49. package/dist/chunk-U7VGDSNY.cjs.map +1 -0
  50. package/dist/commands/index.cjs +30 -410
  51. package/dist/commands/index.cjs.map +1 -1
  52. package/dist/commands/index.d.cts +1 -1
  53. package/dist/commands/index.d.ts +1 -1
  54. package/dist/commands/index.js +17 -397
  55. package/dist/commands/index.js.map +1 -1
  56. package/dist/{controller-CxXGOEmy.d.ts → controller-w6eRTjF8.d.cts} +67 -2
  57. package/dist/{controller-CxXGOEmy.d.cts → controller-w6eRTjF8.d.ts} +67 -2
  58. package/dist/extensions/index.cjs +25 -7
  59. package/dist/extensions/index.cjs.map +1 -1
  60. package/dist/extensions/index.d.cts +1 -1
  61. package/dist/extensions/index.d.ts +1 -1
  62. package/dist/extensions/index.js +32 -14
  63. package/dist/generation/index.cjs +3 -3
  64. package/dist/generation/index.cjs.map +1 -1
  65. package/dist/generation/index.js +3 -3
  66. package/dist/{index-jcJwmBYt.d.cts → index-C6gaQRH0.d.cts} +53 -4
  67. package/dist/{index-B273Zllk.d.ts → index-DXAv_R08.d.ts} +53 -4
  68. package/dist/index.cjs +57 -19
  69. package/dist/index.cjs.map +1 -1
  70. package/dist/index.d.cts +7 -5
  71. package/dist/index.d.ts +7 -5
  72. package/dist/index.js +132 -94
  73. package/dist/index.js.map +1 -1
  74. package/dist/spreadsheet/index.cjs +23 -3
  75. package/dist/spreadsheet/index.cjs.map +1 -1
  76. package/dist/spreadsheet/index.d.cts +76 -3
  77. package/dist/spreadsheet/index.d.ts +76 -3
  78. package/dist/spreadsheet/index.js +28 -8
  79. package/dist/tokens/index.cjs +4 -2
  80. package/dist/tokens/index.cjs.map +1 -1
  81. package/dist/tokens/index.js +6 -4
  82. package/package.json +6 -1
  83. package/dist/chunk-4BVP64JJ.cjs.map +0 -1
  84. package/dist/chunk-HDYMLS3X.cjs.map +0 -1
  85. package/dist/chunk-HRMEDT2L.js.map +0 -1
  86. package/dist/chunk-LN2POBHT.cjs.map +0 -1
  87. package/dist/chunk-NQF32I6Z.cjs.map +0 -1
  88. package/dist/chunk-UBCFIABU.js.map +0 -1
  89. package/dist/chunk-X7HXL2SB.js.map +0 -1
  90. package/dist/chunk-XQFHLT53.js.map +0 -1
@@ -1,9 +1,3 @@
1
- import {
2
- parseCsv,
3
- parseTsv,
4
- serializeCsv,
5
- serializeTsv
6
- } from "./chunk-664LQYFJ.js";
7
1
  import {
8
2
  FormulaEvaluator,
9
3
  NumberFormat,
@@ -32,6 +26,12 @@ import {
32
26
  toText,
33
27
  tokenizeFormula
34
28
  } from "./chunk-ZRNHGAQT.js";
29
+ import {
30
+ parseCsv,
31
+ parseTsv,
32
+ serializeCsv,
33
+ serializeTsv
34
+ } from "./chunk-664LQYFJ.js";
35
35
 
36
36
  // src/spreadsheet/formula/dependency-graph.ts
37
37
  function extractNameDependencies(node) {
@@ -863,6 +863,27 @@ function remapFormulaReferences(formula, moveCell, options = {}) {
863
863
  return `${formatPart({ ...start, row: minR, col: minC })}:${formatPart({ ...end, row: maxR, col: maxC })}`;
864
864
  });
865
865
  }
866
+ function transposeFormulaReferences(formula, anchor, block) {
867
+ const t = { row: block.startRow + (anchor.col - block.startCol), col: block.startCol + (anchor.row - block.startRow) };
868
+ return mapFormulaReferences(formula, (text, info) => {
869
+ const parts = text.split(":").map(parsePart);
870
+ if (parts.some((p) => p === null || p.row === null)) return text;
871
+ const moved = [];
872
+ for (const part of parts) {
873
+ const row = part.row;
874
+ const colAbs = !!part.colAbs;
875
+ const rowAbs = !!part.rowAbs;
876
+ if ((colAbs || rowAbs) && info.sheet !== void 0) return text;
877
+ if (colAbs && (part.col < block.startCol || part.col > block.endCol)) return text;
878
+ if (rowAbs && (row < block.startRow || row > block.endRow)) return text;
879
+ const newRow = colAbs ? block.startRow + (part.col - block.startCol) : t.row + (part.col - anchor.col);
880
+ const newCol = rowAbs ? block.startCol + (row - block.startRow) : t.col + (row - anchor.row);
881
+ if (newRow < 0 || newCol < 0) return "#REF!";
882
+ moved.push({ colAbs: part.rowAbs, col: newCol, rowAbs: part.colAbs, row: newRow });
883
+ }
884
+ return moved.map(formatPart).join(":");
885
+ });
886
+ }
866
887
  var CELL_LIKE = /^[A-Za-z]{1,3}\d{1,7}$/;
867
888
  var isCellLike = (name) => {
868
889
  if (!CELL_LIKE.test(name)) return false;
@@ -1781,6 +1802,175 @@ function parseRuleRanges(text) {
1781
1802
  }
1782
1803
  return out;
1783
1804
  }
1805
+ function mergeRuleRanges(ranges) {
1806
+ const join = (list, across) => {
1807
+ const [lo, hi, fixedLo, fixedHi] = across === "row" ? ["startRow", "endRow", "startCol", "endCol"] : ["startCol", "endCol", "startRow", "endRow"];
1808
+ const sorted = list.map(normalizeRange).sort((a, b) => a[fixedLo] - b[fixedLo] || a[fixedHi] - b[fixedHi] || a[lo] - b[lo]);
1809
+ const out = [];
1810
+ for (const r of sorted) {
1811
+ const last = out[out.length - 1];
1812
+ if (last && last[fixedLo] === r[fixedLo] && last[fixedHi] === r[fixedHi] && r[lo] <= last[hi] + 1) {
1813
+ last[hi] = Math.max(last[hi], r[hi]);
1814
+ } else {
1815
+ out.push({ ...r });
1816
+ }
1817
+ }
1818
+ return out;
1819
+ };
1820
+ return join(join(ranges, "row"), "col").sort((a, b) => a.startRow - b.startRow || a.startCol - b.startCol);
1821
+ }
1822
+ function sortRuleRanges(ranges, block, newRowOf) {
1823
+ const b = normalizeRange(block);
1824
+ let changed = false;
1825
+ const pieces = [];
1826
+ const moved = /* @__PURE__ */ new Map();
1827
+ for (const range of ranges) {
1828
+ const r = normalizeRange(range);
1829
+ const top = Math.max(r.startRow, b.startRow);
1830
+ const bottom = Math.min(r.endRow, b.endRow);
1831
+ const left = Math.max(r.startCol, b.startCol);
1832
+ const right = Math.min(r.endCol, b.endCol);
1833
+ if (top > bottom || left > right || top === b.startRow && bottom === b.endRow) {
1834
+ pieces.push(r);
1835
+ continue;
1836
+ }
1837
+ changed = true;
1838
+ pieces.push(...subtractRange(r, b));
1839
+ const key = `${left}:${right}`;
1840
+ let entry = moved.get(key);
1841
+ if (!entry) moved.set(key, entry = { startCol: left, endCol: right, rows: [] });
1842
+ for (let row = top; row <= bottom; row++) entry.rows.push(newRowOf(row));
1843
+ }
1844
+ if (!changed) return null;
1845
+ for (const { startCol, endCol, rows } of moved.values()) {
1846
+ rows.sort((x, y) => x - y);
1847
+ let start = rows[0];
1848
+ for (let i = 1; i <= rows.length; i++) {
1849
+ if (i < rows.length && rows[i] <= rows[i - 1] + 1) continue;
1850
+ pieces.push({ startRow: start, startCol, endRow: rows[i - 1], endCol });
1851
+ start = rows[i];
1852
+ }
1853
+ }
1854
+ return mergeRuleRanges(pieces);
1855
+ }
1856
+ function transposeRuleRanges(ranges, block, target) {
1857
+ const s = normalizeRange(block);
1858
+ const t = normalizeRange(target);
1859
+ if (!ranges.some((r) => rangesIntersect(r, s) || rangesIntersect(r, t))) return null;
1860
+ const inside = [];
1861
+ for (const range of ranges) {
1862
+ const r = normalizeRange(range);
1863
+ if (!rangesIntersect(r, s)) continue;
1864
+ const top = Math.max(r.startRow, s.startRow);
1865
+ const bottom = Math.min(r.endRow, s.endRow);
1866
+ const left = Math.max(r.startCol, s.startCol);
1867
+ const right = Math.min(r.endCol, s.endCol);
1868
+ inside.push({
1869
+ startRow: s.startRow + (left - s.startCol),
1870
+ endRow: s.startRow + (right - s.startCol),
1871
+ startCol: s.startCol + (top - s.startRow),
1872
+ endCol: s.startCol + (bottom - s.startRow)
1873
+ });
1874
+ }
1875
+ const outside = subtractRangeFromRanges(subtractRangeFromRanges(ranges, s), t);
1876
+ return { inside: mergeRuleRanges(inside), outside: mergeRuleRanges(outside) };
1877
+ }
1878
+ function reanchorRuleFormula(formula, from, to) {
1879
+ if (from.row === to.row && from.col === to.col) return formula;
1880
+ const hadEquals = formula.trimStart().startsWith("=");
1881
+ const moved = ruleFormulaAt(formula, from, to.row, to.col);
1882
+ return hadEquals ? moved : moved.slice(1);
1883
+ }
1884
+ function transposeRuleFormula(formula, anchor, block) {
1885
+ const hadEquals = formula.trimStart().startsWith("=");
1886
+ const out = transposeFormulaReferences(withEquals(formula), anchor, normalizeRange(block));
1887
+ return hadEquals ? out : out.slice(1);
1888
+ }
1889
+ var isFormulaText = (text) => text !== void 0 && text.trimStart().startsWith("=");
1890
+ function mapConditionalFormatFormulas(rule, fn) {
1891
+ const anchor = (value) => value.type === "formula" ? { ...value, formula: fn(value.formula) } : value;
1892
+ const next = { ...rule };
1893
+ if (next.type === "expression") next.formula = fn(next.formula);
1894
+ if (next.type === "cellIs") {
1895
+ if (isFormulaText(next.value)) next.value = fn(next.value);
1896
+ if (isFormulaText(next.value2)) next.value2 = fn(next.value2);
1897
+ }
1898
+ if (next.type === "colorScale") next.stops = next.stops.map((s) => ({ ...s, value: anchor(s.value) }));
1899
+ if (next.type === "dataBar") {
1900
+ if (next.min) next.min = anchor(next.min);
1901
+ if (next.max) next.max = anchor(next.max);
1902
+ }
1903
+ if (next.type === "iconSet" && next.thresholds) next.thresholds = next.thresholds.map(anchor);
1904
+ return next;
1905
+ }
1906
+ function mapDataValidationFormulas(rule, fn) {
1907
+ const next = { ...rule };
1908
+ if (isFormulaText(next.formula1)) next.formula1 = fn(next.formula1);
1909
+ if (isFormulaText(next.formula2)) next.formula2 = fn(next.formula2);
1910
+ return next;
1911
+ }
1912
+ function hasRuleFormulas(rule, mapFormulas) {
1913
+ let found = false;
1914
+ mapFormulas(rule, (f) => {
1915
+ found = true;
1916
+ return f;
1917
+ });
1918
+ return found;
1919
+ }
1920
+ var sameCell = (a, b) => a.row === b.row && a.col === b.col;
1921
+ function sortRules(rules, block, newRowOf, mapFormulas) {
1922
+ let changed = false;
1923
+ const out = rules.map((rule) => {
1924
+ const ranges = sortRuleRanges(rule.ranges, block, newRowOf);
1925
+ if (!ranges) return rule;
1926
+ changed = true;
1927
+ const from = ruleAnchor(rule.ranges);
1928
+ const to = ruleAnchor(ranges);
1929
+ const next = { ...rule, ranges };
1930
+ return sameCell(from, to) ? next : mapFormulas(next, (f) => reanchorRuleFormula(f, from, to));
1931
+ });
1932
+ return changed ? out : rules;
1933
+ }
1934
+ function transposeRules(rules, block, target, mapFormulas, newId) {
1935
+ const s = normalizeRange(block);
1936
+ let changed = false;
1937
+ const out = [];
1938
+ for (const rule of rules) {
1939
+ const moved = transposeRuleRanges(rule.ranges, s, target);
1940
+ if (!moved) {
1941
+ out.push(rule);
1942
+ continue;
1943
+ }
1944
+ changed = true;
1945
+ const { inside, outside } = moved;
1946
+ const from = ruleAnchor(rule.ranges);
1947
+ const withFormulas = hasRuleFormulas(rule, mapFormulas);
1948
+ const stayed = () => {
1949
+ const to = ruleAnchor(outside);
1950
+ const next = { ...rule, ranges: outside };
1951
+ return withFormulas && !sameCell(from, to) ? mapFormulas(next, (f) => reanchorRuleFormula(f, from, to)) : next;
1952
+ };
1953
+ const transposed = () => {
1954
+ const next = { ...rule, ranges: inside };
1955
+ if (!withFormulas) return next;
1956
+ const first2 = rule.ranges.map(normalizeRange).find((r) => rangesIntersect(r, s));
1957
+ const at = { row: Math.max(first2.startRow, s.startRow), col: Math.max(first2.startCol, s.startCol) };
1958
+ const atT = { row: s.startRow + (at.col - s.startCol), col: s.startCol + (at.row - s.startRow) };
1959
+ const to = ruleAnchor(inside);
1960
+ return mapFormulas(next, (f) => reanchorRuleFormula(transposeRuleFormula(reanchorRuleFormula(f, from, at), at, s), atT, to));
1961
+ };
1962
+ if (inside.length === 0) {
1963
+ if (outside.length > 0) out.push(stayed());
1964
+ } else if (outside.length === 0) {
1965
+ out.push(transposed());
1966
+ } else if (!withFormulas) {
1967
+ out.push({ ...rule, ranges: mergeRuleRanges([...outside, ...inside]) });
1968
+ } else {
1969
+ out.push(stayed(), { ...transposed(), id: newId() });
1970
+ }
1971
+ }
1972
+ return changed ? out : rules;
1973
+ }
1784
1974
 
1785
1975
  // src/spreadsheet/conditional-format/icons.ts
1786
1976
  var RED = "#d9534f";
@@ -1877,13 +2067,13 @@ function defaultIconThresholds(set) {
1877
2067
  return Array.from({ length: k }, (_, i) => ({ type: "percent", value: Math.round(i * 100 / k) }));
1878
2068
  }
1879
2069
  var usesStats = (rule) => rule.type === "colorScale" || rule.type === "dataBar" || rule.type === "iconSet" || rule.type === "top10" || rule.type === "aboveAverage" || rule.type === "duplicate" || rule.type === "unique";
1880
- var isFormulaText = (text) => !!text && text.trimStart().startsWith("=");
2070
+ var isFormulaText2 = (text) => !!text && text.trimStart().startsWith("=");
1881
2071
  function usesFormulas(rule) {
1882
2072
  switch (rule.type) {
1883
2073
  case "expression":
1884
2074
  return true;
1885
2075
  case "cellIs":
1886
- return isFormulaText(rule.value) || isFormulaText(rule.value2);
2076
+ return isFormulaText2(rule.value) || isFormulaText2(rule.value2);
1887
2077
  case "colorScale":
1888
2078
  return rule.stops.some((s) => s.value.type === "formula");
1889
2079
  case "dataBar":
@@ -2056,7 +2246,7 @@ var ConditionalFormatEngine = class {
2056
2246
  /** The operand of a `cellIs` rule: a formula result, or typed input. */
2057
2247
  operand(text, rule, row, col) {
2058
2248
  if (text === void 0) return "";
2059
- if (isFormulaText(text)) return this.evalFormula(text, rule, row, col);
2249
+ if (isFormulaText2(text)) return this.evalFormula(text, rule, row, col);
2060
2250
  return parseCellInput(text).value;
2061
2251
  }
2062
2252
  /** Combines every rule that applies to (row, col). */
@@ -2826,6 +3016,33 @@ var createRuntime = (doc) => ({
2826
3016
  selectedRange: null
2827
3017
  });
2828
3018
  var sameName3 = (a, b) => a.toUpperCase() === b.toUpperCase();
3019
+ var nameKey = (n) => `${n.sheetId ?? ""}!${n.name.toUpperCase()}`;
3020
+ function normalizeSavedName(n, sheetId) {
3021
+ if (!n || typeof n.name !== "string" || typeof n.refersTo !== "string" || n.name === "") return null;
3022
+ const { sheetId: _ignored, ...rest } = n;
3023
+ return {
3024
+ ...rest,
3025
+ refersTo: n.refersTo.startsWith("=") ? n.refersTo : `=${n.refersTo}`,
3026
+ ...sheetId !== void 0 ? { sheetId } : {}
3027
+ };
3028
+ }
3029
+ function namesFromDocument(doc, sheetId) {
3030
+ if (!Array.isArray(doc?.names)) return [];
3031
+ const out = [];
3032
+ const seen = /* @__PURE__ */ new Set();
3033
+ for (const saved of doc.names) {
3034
+ const n = normalizeSavedName(saved, saved?.sheetId !== void 0 ? sheetId : void 0);
3035
+ if (!n || seen.has(nameKey(n))) continue;
3036
+ seen.add(nameKey(n));
3037
+ out.push(n);
3038
+ }
3039
+ return out;
3040
+ }
3041
+ function withoutNames(doc) {
3042
+ if (!("names" in doc)) return doc;
3043
+ const { names: _names, ...rest } = doc;
3044
+ return rest;
3045
+ }
2829
3046
  var DEFAULT_MAX_HISTORY_BYTES = 64 * 1024 * 1024;
2830
3047
  var AST_CACHE_SIZE = 1e5;
2831
3048
  var RANGE_CACHE_MIN_CELLS = 256;
@@ -2952,11 +3169,13 @@ var SpreadsheetController = class {
2952
3169
  ...options.document?.table ? { table: options.document.table } : {}
2953
3170
  };
2954
3171
  if (options.workbook && options.workbook.sheets.length > 0) {
2955
- this.setWorkbookState(options.workbook, false);
3172
+ this.setWorkbookState({ ...options.workbook, sheets: options.workbook.sheets.map((doc) => ({ ...doc })) }, false);
2956
3173
  } else {
2957
3174
  this.runtimes = [createRuntime(first2)];
2958
3175
  this.active = this.ctx = this.runtimes[0];
3176
+ this.names = namesFromDocument(options.document, first2.id);
2959
3177
  }
3178
+ this.syncDocumentNames();
2960
3179
  this.boundSheetId = this.runtimes[0].doc.id;
2961
3180
  this.primaryDataSourceId = options.primaryDataSourceId;
2962
3181
  this.maxHistoryBytes = Math.max(0, options.maxHistoryBytes ?? DEFAULT_MAX_HISTORY_BYTES);
@@ -3068,6 +3287,7 @@ var SpreadsheetController = class {
3068
3287
  for (const n of this.names) if (n.sheetId === previousId) n.sheetId = loaded.id;
3069
3288
  if (this.boundSheetId === previousId) this.boundSheetId = loaded.id;
3070
3289
  }
3290
+ if (Array.isArray(document.names)) this.loadDocumentNames(document, loaded.id);
3071
3291
  this.activeCell = null;
3072
3292
  this.selectedRange = null;
3073
3293
  this.editingCell = null;
@@ -3080,9 +3300,40 @@ var SpreadsheetController = class {
3080
3300
  this.notifyChange();
3081
3301
  }
3082
3302
  // --- Workbook: loading and saving ---
3083
- /** A deep copy of the workbook: every sheet, the active sheet and the defined names. */
3303
+ /**
3304
+ * Installs the names saved in a document loaded into sheet `sheetId`: they
3305
+ * replace that sheet's own names and, when it is the only sheet, the
3306
+ * workbook names; with other sheets, its workbook names are added (or
3307
+ * replace the same name) and the others are kept.
3308
+ */
3309
+ loadDocumentNames(document, sheetId) {
3310
+ const saved = namesFromDocument(document, sheetId);
3311
+ const onlySheet = this.runtimes.length === 1;
3312
+ const keys = new Set(saved.map(nameKey));
3313
+ this.names = this.names.filter(
3314
+ (n) => n.sheetId !== sheetId && !(n.sheetId === void 0 && (onlySheet || keys.has(nameKey(n))))
3315
+ );
3316
+ this.names.push(...saved);
3317
+ }
3318
+ /**
3319
+ * Writes every sheet's current names into its document (`doc.names`), so
3320
+ * documents passed to `onDocumentChange` or read from `document` carry them.
3321
+ */
3322
+ syncDocumentNames() {
3323
+ for (const rt of this.runtimes) {
3324
+ const names = this.names.filter((n) => n.sheetId === void 0 || n.sheetId === rt.doc.id).map((n) => ({ ...n }));
3325
+ if (names.length > 0) rt.doc.names = names;
3326
+ else delete rt.doc.names;
3327
+ }
3328
+ }
3329
+ /**
3330
+ * A deep copy of the workbook: every sheet, the active sheet and the
3331
+ * defined names. The names are in `names` only (not repeated in each
3332
+ * sheet's `names`).
3333
+ */
3084
3334
  getWorkbook() {
3085
- return JSON.parse(JSON.stringify(this.workbookSnapshot()));
3335
+ const snapshot = this.workbookSnapshot();
3336
+ return JSON.parse(JSON.stringify({ ...snapshot, sheets: snapshot.sheets.map(withoutNames) }));
3086
3337
  }
3087
3338
  /** Replaces the whole workbook (sheets, names, active sheet). Clears undo history. */
3088
3339
  loadWorkbook(workbook) {
@@ -3099,6 +3350,11 @@ var SpreadsheetController = class {
3099
3350
  this.notifyChange();
3100
3351
  this.onSelectionChange?.(this.activeCell, this.selectedRange);
3101
3352
  }
3353
+ /** The workbook as undo data; sheets leave out their copies of the names. */
3354
+ serializeWorkbookSnapshot() {
3355
+ const snapshot = this.workbookSnapshot();
3356
+ return JSON.stringify({ ...snapshot, sheets: snapshot.sheets.map(withoutNames) });
3357
+ }
3102
3358
  workbookSnapshot() {
3103
3359
  return {
3104
3360
  sheets: this.runtimes.map((rt) => rt.doc),
@@ -3127,7 +3383,19 @@ var SpreadsheetController = class {
3127
3383
  });
3128
3384
  this.runtimes = runtimes;
3129
3385
  this.active = this.ctx = runtimes.find((rt) => rt.doc.id === workbook.activeSheetId) ?? runtimes[0];
3130
- this.names = (workbook.names ?? []).filter((n) => n.sheetId === void 0 || ids.has(n.sheetId)).map((n) => ({ ...n, refersTo: n.refersTo.startsWith("=") ? n.refersTo : `=${n.refersTo}` }));
3386
+ const seen = /* @__PURE__ */ new Set();
3387
+ this.names = [];
3388
+ const add = (n) => {
3389
+ if (!n || seen.has(nameKey(n))) return;
3390
+ seen.add(nameKey(n));
3391
+ this.names.push(n);
3392
+ };
3393
+ for (const n of workbook.names ?? []) {
3394
+ if (n?.sheetId === void 0 || ids.has(n.sheetId)) add(normalizeSavedName(n, n?.sheetId));
3395
+ }
3396
+ for (const rt of runtimes) {
3397
+ for (const n of namesFromDocument(rt.doc, rt.doc.id)) add(n);
3398
+ }
3131
3399
  if (resetSelection) {
3132
3400
  this.activeCell = null;
3133
3401
  this.selectedRange = null;
@@ -4464,6 +4732,10 @@ var SpreadsheetController = class {
4464
4732
  if (target === void 0 || col < startCol || col > endCol) return null;
4465
4733
  return { row: target, col };
4466
4734
  });
4735
+ this.moveRulesForSort(
4736
+ { startRow, startCol, endRow, endCol: wholeRows ? Number.MAX_SAFE_INTEGER : endCol },
4737
+ (row) => newIndexOf.get(row) ?? row
4738
+ );
4467
4739
  if (wholeRows) {
4468
4740
  const moved = sorted.map((r) => this.document.rows[r]);
4469
4741
  this.document.rows.splice(startRow, moved.length, ...moved);
@@ -4890,6 +5162,7 @@ var SpreadsheetController = class {
4890
5162
  if (!rangeContains(s, row, col)) return null;
4891
5163
  return { row: s.startRow + (col - s.startCol), col: s.startCol + (row - s.startRow) };
4892
5164
  });
5165
+ this.moveRulesForTranspose(s, t);
4893
5166
  const rowId = (r) => this.document.rows[r].id;
4894
5167
  const colId = (c) => this.document.columns[c].id;
4895
5168
  const moved = [];
@@ -4997,6 +5270,10 @@ var SpreadsheetController = class {
4997
5270
  id,
4998
5271
  name
4999
5272
  };
5273
+ const existingNames = new Set(this.names.map(nameKey));
5274
+ for (const n of namesFromDocument(options.document, id)) {
5275
+ if (!existingNames.has(nameKey(n))) this.names.push(n);
5276
+ }
5000
5277
  const rt = createRuntime(doc);
5001
5278
  const index = Math.max(0, Math.min(options.index ?? this.runtimes.length, this.runtimes.length));
5002
5279
  this.runtimes.splice(index, 0, rt);
@@ -5084,6 +5361,7 @@ var SpreadsheetController = class {
5084
5361
  doc.id = this.generateSheetId();
5085
5362
  doc.name = name;
5086
5363
  doc.createdAt = doc.updatedAt = Date.now();
5364
+ delete doc.names;
5087
5365
  const copies = this.names.filter((n) => n.sheetId === source.doc.id).map((n) => ({ ...n, sheetId: doc.id, refersTo: renameSheetInFormula(n.refersTo, source.doc.name, name) }));
5088
5366
  this.names.push(...copies);
5089
5367
  const rt = createRuntime(doc);
@@ -5283,7 +5561,7 @@ var SpreadsheetController = class {
5283
5561
  }
5284
5562
  recordWorkbookSnapshot() {
5285
5563
  if (this.isApplyingHistory || this.batching) return;
5286
- this.pushUndo({ kind: "workbook", data: JSON.stringify(this.workbookSnapshot()) });
5564
+ this.pushUndo({ kind: "workbook", data: this.serializeWorkbookSnapshot() });
5287
5565
  }
5288
5566
  pushUndo(entry) {
5289
5567
  this.undoStack.push(entry);
@@ -5363,7 +5641,7 @@ var SpreadsheetController = class {
5363
5641
  this.activateRuntime(rt);
5364
5642
  }
5365
5643
  } else {
5366
- to.push({ kind: "workbook", data: JSON.stringify(this.workbookSnapshot()) });
5644
+ to.push({ kind: "workbook", data: this.serializeWorkbookSnapshot() });
5367
5645
  const previousActive = this.active.doc.id;
5368
5646
  const selection = { activeCell: this.activeCell, selectedRange: this.selectedRange };
5369
5647
  const saved = new Map(this.runtimes.map((rt) => [rt.doc.id, rt]));
@@ -5394,6 +5672,7 @@ var SpreadsheetController = class {
5394
5672
  notifyChange() {
5395
5673
  if (!this.cfIncremental) this.cfDirty = "all";
5396
5674
  this.invalidCircles = null;
5675
+ this.syncDocumentNames();
5397
5676
  this.onDocumentChange?.(this.active.doc);
5398
5677
  if (this.onWorkbookChange) {
5399
5678
  const snapshot = this.workbookSnapshot();
@@ -5477,6 +5756,43 @@ var SpreadsheetController = class {
5477
5756
  }).filter((rule) => rule.ranges.length > 0);
5478
5757
  }
5479
5758
  }
5759
+ /**
5760
+ * Moves conditional formats and validations with sorted rows: per-row
5761
+ * parts follow their rows, parts spanning every sorted row stay (Excel).
5762
+ */
5763
+ moveRulesForSort(block, newRowOf) {
5764
+ const table = this.document.table;
5765
+ if (!table) return;
5766
+ if (table.conditionalFormats?.length) {
5767
+ table.conditionalFormats = sortRules(table.conditionalFormats, block, newRowOf, mapConditionalFormatFormulas);
5768
+ }
5769
+ if (table.dataValidations?.length) {
5770
+ table.dataValidations = sortRules(table.dataValidations, block, newRowOf, mapDataValidationFormulas);
5771
+ }
5772
+ }
5773
+ /** Moves conditional formats and validations of a transposed block with its cells. */
5774
+ moveRulesForTranspose(block, target) {
5775
+ const table = this.document.table;
5776
+ if (!table) return;
5777
+ if (table.conditionalFormats?.length) {
5778
+ table.conditionalFormats = transposeRules(
5779
+ table.conditionalFormats,
5780
+ block,
5781
+ target,
5782
+ mapConditionalFormatFormulas,
5783
+ () => this.generateRuleId("cf")
5784
+ );
5785
+ }
5786
+ if (table.dataValidations?.length) {
5787
+ table.dataValidations = transposeRules(
5788
+ table.dataValidations,
5789
+ block,
5790
+ target,
5791
+ mapDataValidationFormulas,
5792
+ () => this.generateRuleId("dv")
5793
+ );
5794
+ }
5795
+ }
5480
5796
  /** Conditional formats of the sheet, highest priority first. */
5481
5797
  getConditionalFormats() {
5482
5798
  return this.document.table?.conditionalFormats ?? [];
@@ -6355,6 +6671,7 @@ export {
6355
6671
  mapFormulaReferences,
6356
6672
  offsetFormulaReferences,
6357
6673
  remapFormulaReferences,
6674
+ transposeFormulaReferences,
6358
6675
  sheetNameNeedsQuotes,
6359
6676
  quoteSheetName,
6360
6677
  formatSheetPrefix,
@@ -6411,6 +6728,15 @@ export {
6411
6728
  forEachRangeCell,
6412
6729
  formatRuleRanges,
6413
6730
  parseRuleRanges,
6731
+ mergeRuleRanges,
6732
+ sortRuleRanges,
6733
+ transposeRuleRanges,
6734
+ reanchorRuleFormula,
6735
+ transposeRuleFormula,
6736
+ mapConditionalFormatFormulas,
6737
+ mapDataValidationFormulas,
6738
+ sortRules,
6739
+ transposeRules,
6414
6740
  CONDITIONAL_FORMAT_ICON_SETS,
6415
6741
  conditionalFormatIconCount,
6416
6742
  getConditionalFormatIcon,
@@ -6475,4 +6801,4 @@ export {
6475
6801
  getSpreadsheetScrollToCell,
6476
6802
  sameSpreadsheetViewWindow
6477
6803
  };
6478
- //# sourceMappingURL=chunk-X7HXL2SB.js.map
6804
+ //# sourceMappingURL=chunk-3RPOW4CS.js.map