@sofereditor/core 0.7.0 → 0.9.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.
package/dist/index.cjs CHANGED
@@ -42,6 +42,9 @@ __export(index_exports, {
42
42
  EditorHistory: () => EditorHistory,
43
43
  MARGIN_PRESETS: () => MARGIN_PRESETS,
44
44
  MAX_LIST_LEVEL: () => MAX_LIST_LEVEL,
45
+ MAX_TABELA_PCT: () => MAX_TABELA_PCT,
46
+ MIN_LINHA_PX: () => MIN_LINHA_PX,
47
+ MIN_TABELA_PCT: () => MIN_TABELA_PCT,
45
48
  PAGE_PRESETS: () => PAGE_PRESETS,
46
49
  SOFER_MIME: () => SOFER_MIME,
47
50
  TABLE_BORDER_COLOR: () => TABLE_BORDER_COLOR,
@@ -85,6 +88,7 @@ __export(index_exports, {
85
88
  isCollapsed: () => isCollapsed,
86
89
  isCommandOrigin: () => isCommandOrigin,
87
90
  isEmbedAdjacentToCaret: () => isEmbedAdjacentToCaret,
91
+ isFormulaEmbed: () => isFormulaEmbed,
88
92
  isImageEmbed: () => isImageEmbed,
89
93
  isKnownBlockType: () => isKnownBlockType,
90
94
  isLegacySerializedDocument: () => isLegacySerializedDocument,
@@ -97,6 +101,7 @@ __export(index_exports, {
97
101
  moveToNextCell: () => moveToNextCell,
98
102
  moveToPrevCell: () => moveToPrevCell,
99
103
  newBlockId: () => newBlockId,
104
+ normalizarLarguras: () => normalizarLarguras,
100
105
  orderedRange: () => orderedRange,
101
106
  positionsEqual: () => positionsEqual,
102
107
  pxToMm: () => pxToMm,
@@ -107,9 +112,12 @@ __export(index_exports, {
107
112
  setBlockAttrAtIndex: () => setBlockAttrAtIndex,
108
113
  setBlockType: () => setBlockType,
109
114
  setCellAttr: () => setCellAttr,
110
- setColumnWidth: () => setColumnWidth,
115
+ setColumnBoundary: () => setColumnBoundary,
111
116
  setImageAttrs: () => setImageAttrs,
112
117
  setMark: () => setMark,
118
+ setRowHeight: () => setRowHeight,
119
+ setRowHeights: () => setRowHeights,
120
+ setTableWidth: () => setTableWidth,
113
121
  sliceDelta: () => sliceDelta,
114
122
  sliceToInlineDelta: () => sliceToInlineDelta,
115
123
  sliceToText: () => sliceToText,
@@ -122,6 +130,7 @@ __export(index_exports, {
122
130
  tableRectSelection: () => tableRectSelection,
123
131
  toggleList: () => toggleList,
124
132
  toggleMark: () => toggleMark,
133
+ travaLarguraTabela: () => travaLarguraTabela,
125
134
  withDefaults: () => withDefaults
126
135
  });
127
136
  module.exports = __toCommonJS(index_exports);
@@ -131,6 +140,9 @@ var MAX_LIST_LEVEL = 5;
131
140
  function isImageEmbed(v) {
132
141
  return typeof v === "object" && v !== null && v.type === "image" && typeof v.src === "string";
133
142
  }
143
+ function isFormulaEmbed(v) {
144
+ return isImageEmbed(v) && v.formula != null;
145
+ }
134
146
  function isLegacySerializedDocument(v) {
135
147
  return Array.isArray(v);
136
148
  }
@@ -1681,6 +1693,12 @@ function insertTableRow(ctx, blockIndex, row, where) {
1681
1693
  }
1682
1694
  cells.insert(P * cols, fresh);
1683
1695
  attrs.set("rows", rows + 1);
1696
+ const heights = attrs.get("rowHeights");
1697
+ if (Array.isArray(heights) && heights.length === rows) {
1698
+ const next = heights.slice();
1699
+ next.splice(P, 0, MIN_LINHA_PX);
1700
+ attrs.set("rowHeights", next);
1701
+ }
1684
1702
  const firstReal = newRowCovered.findIndex((v) => !v);
1685
1703
  const targetCellIdx = P * cols + (firstReal < 0 ? 0 : firstReal);
1686
1704
  ctx.setSelection(
@@ -1726,6 +1744,12 @@ function deleteTableRow(ctx, blockIndex, row) {
1726
1744
  }
1727
1745
  cells.delete(R * cols, cols);
1728
1746
  attrs.set("rows", rows - 1);
1747
+ const heights = attrs.get("rowHeights");
1748
+ if (Array.isArray(heights) && heights.length === rows) {
1749
+ const next = heights.slice();
1750
+ next.splice(R, 1);
1751
+ attrs.set("rowHeights", next);
1752
+ }
1729
1753
  for (const p of promotions) {
1730
1754
  const m = ctx.doc.getCellAttrsMap(blockIndex, p.flatNew);
1731
1755
  if (!m) continue;
@@ -1785,9 +1809,12 @@ function insertTableColumn(ctx, blockIndex, col, where) {
1785
1809
  attrs.set("cols", cols + 1);
1786
1810
  const widths = attrs.get("colWidths");
1787
1811
  if (Array.isArray(widths) && widths.length === cols) {
1788
- const next = widths.slice();
1789
- next.splice(C, 0, 100);
1790
- attrs.set("colWidths", next);
1812
+ const base = normalizarLarguras(widths, cols);
1813
+ const media = arredonda(100 / (cols + 1));
1814
+ const escala = cols / (cols + 1);
1815
+ const next = base.map((w) => arredonda(w * escala));
1816
+ next.splice(C, 0, media);
1817
+ attrs.set("colWidths", normalizarLarguras(next, cols + 1));
1791
1818
  }
1792
1819
  const newCols = cols + 1;
1793
1820
  const focusRow = 0;
@@ -1860,7 +1887,7 @@ function deleteTableColumn(ctx, blockIndex, col) {
1860
1887
  if (Array.isArray(widths) && widths.length === cols) {
1861
1888
  const next = widths.slice();
1862
1889
  next.splice(C, 1);
1863
- attrs.set("colWidths", next);
1890
+ attrs.set("colWidths", normalizarLarguras(next, cols - 1));
1864
1891
  }
1865
1892
  const newCol = Math.min(C, newCols - 1);
1866
1893
  const target = findNextRealCell(ctx.doc, blockIndex, newCol, rows * newCols) ?? 0;
@@ -2148,20 +2175,81 @@ function splitCell(ctx, blockIndex, row, col) {
2148
2175
  });
2149
2176
  return true;
2150
2177
  }
2151
- function setColumnWidth(ctx, blockIndex, col, widthPx) {
2178
+ var SOMA_OK = (s) => Math.abs(s - 100) < 0.5;
2179
+ var arredonda = (n) => Math.round(n * 1e3) / 1e3;
2180
+ function normalizarLarguras(widths, cols) {
2181
+ const igual = () => new Array(cols).fill(100 / cols);
2182
+ if (!Array.isArray(widths) || widths.length !== cols || cols <= 0) {
2183
+ return igual();
2184
+ }
2185
+ if (!widths.every((w) => typeof w === "number" && isFinite(w) && w > 0)) {
2186
+ return igual();
2187
+ }
2188
+ const soma = widths.reduce((a, b) => a + b, 0);
2189
+ if (SOMA_OK(soma)) return widths.slice();
2190
+ return widths.map((w) => arredonda(w / soma * 100));
2191
+ }
2192
+ var MIN_COLUNA_PCT = 3;
2193
+ function setColumnBoundary(ctx, blockIndex, boundary, deltaPct, base) {
2152
2194
  if (!ctx.doc.isTable(blockIndex)) return;
2153
2195
  const { cols } = ctx.doc.getTableSize(blockIndex);
2154
- if (col < 0 || col >= cols) return;
2155
- const w = Math.max(20, Math.round(widthPx));
2196
+ if (boundary < 0 || boundary >= cols - 1) return;
2156
2197
  transact(ctx.doc, () => {
2157
2198
  const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2158
2199
  if (!attrsMap) return;
2159
- const current = attrsMap.get("colWidths");
2160
- const next = Array.isArray(current) && current.length === cols ? current.slice() : new Array(cols).fill(120);
2161
- next[col] = w;
2200
+ const larguras = normalizarLarguras(
2201
+ base ?? attrsMap.get("colWidths"),
2202
+ cols
2203
+ );
2204
+ const a = larguras[boundary];
2205
+ const b = larguras[boundary + 1];
2206
+ const piso = Math.min(MIN_COLUNA_PCT, (a + b) / 2);
2207
+ const d = Math.max(piso - a, Math.min(deltaPct, b - piso));
2208
+ const next = larguras.slice();
2209
+ next[boundary] = arredonda(a + d);
2210
+ next[boundary + 1] = arredonda(b - d);
2162
2211
  attrsMap.set("colWidths", next);
2163
2212
  });
2164
2213
  }
2214
+ var MIN_TABELA_PCT = 20;
2215
+ var MAX_TABELA_PCT = 100;
2216
+ function travaLarguraTabela(pct) {
2217
+ return Math.max(MIN_TABELA_PCT, Math.min(MAX_TABELA_PCT, pct));
2218
+ }
2219
+ function setTableWidth(ctx, blockIndex, pct) {
2220
+ if (!ctx.doc.isTable(blockIndex)) return;
2221
+ const v = travaLarguraTabela(arredonda(pct));
2222
+ transact(ctx.doc, () => {
2223
+ ctx.doc.getBlockAttrsMap(blockIndex)?.set("tableWidth", v);
2224
+ });
2225
+ }
2226
+ var MIN_LINHA_PX = 16;
2227
+ function setRowHeight(ctx, blockIndex, row, px) {
2228
+ if (!ctx.doc.isTable(blockIndex)) return;
2229
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2230
+ if (row < 0 || row >= rows) return;
2231
+ transact(ctx.doc, () => {
2232
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2233
+ if (!attrsMap) return;
2234
+ const atual = attrsMap.get("rowHeights");
2235
+ const base = Array.isArray(atual) && atual.length === rows ? atual.slice() : new Array(rows).fill(MIN_LINHA_PX);
2236
+ base[row] = Math.max(MIN_LINHA_PX, Math.round(px));
2237
+ attrsMap.set("rowHeights", base);
2238
+ });
2239
+ }
2240
+ function setRowHeights(ctx, blockIndex, heights) {
2241
+ if (!ctx.doc.isTable(blockIndex)) return;
2242
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2243
+ if (heights.length !== rows) return;
2244
+ transact(ctx.doc, () => {
2245
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2246
+ if (!attrsMap) return;
2247
+ attrsMap.set(
2248
+ "rowHeights",
2249
+ heights.map((h) => Math.max(MIN_LINHA_PX, Math.round(h)))
2250
+ );
2251
+ });
2252
+ }
2165
2253
  function moveToNextCell(ctx) {
2166
2254
  const sel = ctx.getSelection();
2167
2255
  const loc = tableLocationOf(ctx.doc, sel.focus);
@@ -2354,6 +2442,9 @@ var EditorHistory = class {
2354
2442
  EditorHistory,
2355
2443
  MARGIN_PRESETS,
2356
2444
  MAX_LIST_LEVEL,
2445
+ MAX_TABELA_PCT,
2446
+ MIN_LINHA_PX,
2447
+ MIN_TABELA_PCT,
2357
2448
  PAGE_PRESETS,
2358
2449
  SOFER_MIME,
2359
2450
  TABLE_BORDER_COLOR,
@@ -2397,6 +2488,7 @@ var EditorHistory = class {
2397
2488
  isCollapsed,
2398
2489
  isCommandOrigin,
2399
2490
  isEmbedAdjacentToCaret,
2491
+ isFormulaEmbed,
2400
2492
  isImageEmbed,
2401
2493
  isKnownBlockType,
2402
2494
  isLegacySerializedDocument,
@@ -2409,6 +2501,7 @@ var EditorHistory = class {
2409
2501
  moveToNextCell,
2410
2502
  moveToPrevCell,
2411
2503
  newBlockId,
2504
+ normalizarLarguras,
2412
2505
  orderedRange,
2413
2506
  positionsEqual,
2414
2507
  pxToMm,
@@ -2419,9 +2512,12 @@ var EditorHistory = class {
2419
2512
  setBlockAttrAtIndex,
2420
2513
  setBlockType,
2421
2514
  setCellAttr,
2422
- setColumnWidth,
2515
+ setColumnBoundary,
2423
2516
  setImageAttrs,
2424
2517
  setMark,
2518
+ setRowHeight,
2519
+ setRowHeights,
2520
+ setTableWidth,
2425
2521
  sliceDelta,
2426
2522
  sliceToInlineDelta,
2427
2523
  sliceToText,
@@ -2434,6 +2530,7 @@ var EditorHistory = class {
2434
2530
  tableRectSelection,
2435
2531
  toggleList,
2436
2532
  toggleMark,
2533
+ travaLarguraTabela,
2437
2534
  withDefaults
2438
2535
  });
2439
2536
  //# sourceMappingURL=index.cjs.map