@sofereditor/core 0.7.0 → 0.10.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,21 +112,27 @@ __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,
116
124
  spanOf: () => spanOf,
117
125
  splitCell: () => splitCell,
126
+ splitDeltaByLines: () => splitDeltaByLines,
118
127
  splitListItem: () => splitListItem,
119
128
  splitUnderscoreRuns: () => splitUnderscoreRuns,
129
+ stripFontFamilyMarks: () => stripFontFamilyMarks,
120
130
  styleToCssText: () => styleToCssText,
121
131
  tableLocationOf: () => tableLocationOf,
122
132
  tableRectSelection: () => tableRectSelection,
123
133
  toggleList: () => toggleList,
124
134
  toggleMark: () => toggleMark,
135
+ travaLarguraTabela: () => travaLarguraTabela,
125
136
  withDefaults: () => withDefaults
126
137
  });
127
138
  module.exports = __toCommonJS(index_exports);
@@ -131,6 +142,9 @@ var MAX_LIST_LEVEL = 5;
131
142
  function isImageEmbed(v) {
132
143
  return typeof v === "object" && v !== null && v.type === "image" && typeof v.src === "string";
133
144
  }
145
+ function isFormulaEmbed(v) {
146
+ return isImageEmbed(v) && v.formula != null;
147
+ }
134
148
  function isLegacySerializedDocument(v) {
135
149
  return Array.isArray(v);
136
150
  }
@@ -850,6 +864,25 @@ function sameMarkValue(a, b) {
850
864
  }
851
865
  return false;
852
866
  }
867
+ function splitDeltaByLines(delta) {
868
+ const lines = [[]];
869
+ for (const op of delta) {
870
+ if (typeof op.insert !== "string") {
871
+ lines[lines.length - 1].push(op);
872
+ continue;
873
+ }
874
+ const parts = op.insert.split("\n");
875
+ for (let i = 0; i < parts.length; i++) {
876
+ if (i > 0) lines.push([]);
877
+ const part = parts[i];
878
+ if (part.length === 0) continue;
879
+ lines[lines.length - 1].push(
880
+ op.attributes ? { insert: part, attributes: op.attributes } : { insert: part }
881
+ );
882
+ }
883
+ }
884
+ return lines;
885
+ }
853
886
 
854
887
  // src/decorations.ts
855
888
  var BLANK_MIN_RUN = 3;
@@ -1473,7 +1506,12 @@ function mergeWithPrevious(ctx, blockIndex) {
1473
1506
  function toggleList(ctx, kind) {
1474
1507
  transact(ctx.doc, () => {
1475
1508
  const sel = ctx.getSelection();
1476
- if (sel.anchor.cellIndex != null || sel.focus.cellIndex != null) return;
1509
+ if (sel.anchor.cellIndex != null || sel.focus.cellIndex != null) {
1510
+ const real = sel.focus.cellIndex != null ? ctx.doc.realCellIndex(sel.focus.blockIndex, sel.focus.cellIndex) : void 0;
1511
+ const atual = real != null ? ctx.doc.getCellAttrs(sel.focus.blockIndex, real).listKind : void 0;
1512
+ setCellAttr(ctx, "listKind", atual === kind ? null : kind);
1513
+ return;
1514
+ }
1477
1515
  const indices = selectedBlockIndices(sel, ctx.doc.blockCount());
1478
1516
  const allMatching = indices.every((i) => {
1479
1517
  const block = ctx.doc.getBlock(i);
@@ -1681,6 +1719,12 @@ function insertTableRow(ctx, blockIndex, row, where) {
1681
1719
  }
1682
1720
  cells.insert(P * cols, fresh);
1683
1721
  attrs.set("rows", rows + 1);
1722
+ const heights = attrs.get("rowHeights");
1723
+ if (Array.isArray(heights) && heights.length === rows) {
1724
+ const next = heights.slice();
1725
+ next.splice(P, 0, MIN_LINHA_PX);
1726
+ attrs.set("rowHeights", next);
1727
+ }
1684
1728
  const firstReal = newRowCovered.findIndex((v) => !v);
1685
1729
  const targetCellIdx = P * cols + (firstReal < 0 ? 0 : firstReal);
1686
1730
  ctx.setSelection(
@@ -1726,6 +1770,12 @@ function deleteTableRow(ctx, blockIndex, row) {
1726
1770
  }
1727
1771
  cells.delete(R * cols, cols);
1728
1772
  attrs.set("rows", rows - 1);
1773
+ const heights = attrs.get("rowHeights");
1774
+ if (Array.isArray(heights) && heights.length === rows) {
1775
+ const next = heights.slice();
1776
+ next.splice(R, 1);
1777
+ attrs.set("rowHeights", next);
1778
+ }
1729
1779
  for (const p of promotions) {
1730
1780
  const m = ctx.doc.getCellAttrsMap(blockIndex, p.flatNew);
1731
1781
  if (!m) continue;
@@ -1785,9 +1835,12 @@ function insertTableColumn(ctx, blockIndex, col, where) {
1785
1835
  attrs.set("cols", cols + 1);
1786
1836
  const widths = attrs.get("colWidths");
1787
1837
  if (Array.isArray(widths) && widths.length === cols) {
1788
- const next = widths.slice();
1789
- next.splice(C, 0, 100);
1790
- attrs.set("colWidths", next);
1838
+ const base = normalizarLarguras(widths, cols);
1839
+ const media = arredonda(100 / (cols + 1));
1840
+ const escala = cols / (cols + 1);
1841
+ const next = base.map((w) => arredonda(w * escala));
1842
+ next.splice(C, 0, media);
1843
+ attrs.set("colWidths", normalizarLarguras(next, cols + 1));
1791
1844
  }
1792
1845
  const newCols = cols + 1;
1793
1846
  const focusRow = 0;
@@ -1860,7 +1913,7 @@ function deleteTableColumn(ctx, blockIndex, col) {
1860
1913
  if (Array.isArray(widths) && widths.length === cols) {
1861
1914
  const next = widths.slice();
1862
1915
  next.splice(C, 1);
1863
- attrs.set("colWidths", next);
1916
+ attrs.set("colWidths", normalizarLarguras(next, cols - 1));
1864
1917
  }
1865
1918
  const newCol = Math.min(C, newCols - 1);
1866
1919
  const target = findNextRealCell(ctx.doc, blockIndex, newCol, rows * newCols) ?? 0;
@@ -2148,20 +2201,81 @@ function splitCell(ctx, blockIndex, row, col) {
2148
2201
  });
2149
2202
  return true;
2150
2203
  }
2151
- function setColumnWidth(ctx, blockIndex, col, widthPx) {
2204
+ var SOMA_OK = (s) => Math.abs(s - 100) < 0.5;
2205
+ var arredonda = (n) => Math.round(n * 1e3) / 1e3;
2206
+ function normalizarLarguras(widths, cols) {
2207
+ const igual = () => new Array(cols).fill(100 / cols);
2208
+ if (!Array.isArray(widths) || widths.length !== cols || cols <= 0) {
2209
+ return igual();
2210
+ }
2211
+ if (!widths.every((w) => typeof w === "number" && isFinite(w) && w > 0)) {
2212
+ return igual();
2213
+ }
2214
+ const soma = widths.reduce((a, b) => a + b, 0);
2215
+ if (SOMA_OK(soma)) return widths.slice();
2216
+ return widths.map((w) => arredonda(w / soma * 100));
2217
+ }
2218
+ var MIN_COLUNA_PCT = 3;
2219
+ function setColumnBoundary(ctx, blockIndex, boundary, deltaPct, base) {
2152
2220
  if (!ctx.doc.isTable(blockIndex)) return;
2153
2221
  const { cols } = ctx.doc.getTableSize(blockIndex);
2154
- if (col < 0 || col >= cols) return;
2155
- const w = Math.max(20, Math.round(widthPx));
2222
+ if (boundary < 0 || boundary >= cols - 1) return;
2156
2223
  transact(ctx.doc, () => {
2157
2224
  const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2158
2225
  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;
2226
+ const larguras = normalizarLarguras(
2227
+ base ?? attrsMap.get("colWidths"),
2228
+ cols
2229
+ );
2230
+ const a = larguras[boundary];
2231
+ const b = larguras[boundary + 1];
2232
+ const piso = Math.min(MIN_COLUNA_PCT, (a + b) / 2);
2233
+ const d = Math.max(piso - a, Math.min(deltaPct, b - piso));
2234
+ const next = larguras.slice();
2235
+ next[boundary] = arredonda(a + d);
2236
+ next[boundary + 1] = arredonda(b - d);
2162
2237
  attrsMap.set("colWidths", next);
2163
2238
  });
2164
2239
  }
2240
+ var MIN_TABELA_PCT = 20;
2241
+ var MAX_TABELA_PCT = 100;
2242
+ function travaLarguraTabela(pct) {
2243
+ return Math.max(MIN_TABELA_PCT, Math.min(MAX_TABELA_PCT, pct));
2244
+ }
2245
+ function setTableWidth(ctx, blockIndex, pct) {
2246
+ if (!ctx.doc.isTable(blockIndex)) return;
2247
+ const v = travaLarguraTabela(arredonda(pct));
2248
+ transact(ctx.doc, () => {
2249
+ ctx.doc.getBlockAttrsMap(blockIndex)?.set("tableWidth", v);
2250
+ });
2251
+ }
2252
+ var MIN_LINHA_PX = 16;
2253
+ function setRowHeight(ctx, blockIndex, row, px) {
2254
+ if (!ctx.doc.isTable(blockIndex)) return;
2255
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2256
+ if (row < 0 || row >= rows) return;
2257
+ transact(ctx.doc, () => {
2258
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2259
+ if (!attrsMap) return;
2260
+ const atual = attrsMap.get("rowHeights");
2261
+ const base = Array.isArray(atual) && atual.length === rows ? atual.slice() : new Array(rows).fill(MIN_LINHA_PX);
2262
+ base[row] = Math.max(MIN_LINHA_PX, Math.round(px));
2263
+ attrsMap.set("rowHeights", base);
2264
+ });
2265
+ }
2266
+ function setRowHeights(ctx, blockIndex, heights) {
2267
+ if (!ctx.doc.isTable(blockIndex)) return;
2268
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2269
+ if (heights.length !== rows) return;
2270
+ transact(ctx.doc, () => {
2271
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2272
+ if (!attrsMap) return;
2273
+ attrsMap.set(
2274
+ "rowHeights",
2275
+ heights.map((h) => Math.max(MIN_LINHA_PX, Math.round(h)))
2276
+ );
2277
+ });
2278
+ }
2165
2279
  function moveToNextCell(ctx) {
2166
2280
  const sel = ctx.getSelection();
2167
2281
  const loc = tableLocationOf(ctx.doc, sel.focus);
@@ -2340,6 +2454,45 @@ var EditorHistory = class {
2340
2454
  this.undoManager.destroy();
2341
2455
  }
2342
2456
  };
2457
+
2458
+ // src/migrations.ts
2459
+ function coletar(yText, alvos) {
2460
+ if (!yText) return 0;
2461
+ const delta = yText.toDelta();
2462
+ const ranges = [];
2463
+ let index = 0;
2464
+ for (const op of delta) {
2465
+ const len = typeof op.insert === "string" ? op.insert.length : 1;
2466
+ if (typeof op.insert === "string" && op.attributes && "fontFamily" in op.attributes) {
2467
+ ranges.push([index, len]);
2468
+ }
2469
+ index += len;
2470
+ }
2471
+ if (ranges.length > 0) alvos.push({ yText, ranges });
2472
+ return ranges.length;
2473
+ }
2474
+ function stripFontFamilyMarks(doc, opts) {
2475
+ const alvos = [];
2476
+ let total = 0;
2477
+ for (let i = 0; i < doc.blockCount(); i++) {
2478
+ total += coletar(doc.getBlockText(i), alvos);
2479
+ const cells = doc.getCells(i);
2480
+ if (cells) {
2481
+ for (let c = 0; c < cells.length; c++) {
2482
+ total += coletar(doc.getCellText(i, c), alvos);
2483
+ }
2484
+ }
2485
+ }
2486
+ if (total === 0 || opts?.dryRun) return total;
2487
+ doc.ydoc.transact(() => {
2488
+ for (const { yText, ranges } of alvos) {
2489
+ for (const [index, length] of ranges) {
2490
+ yText.format(index, length, { fontFamily: null });
2491
+ }
2492
+ }
2493
+ }, "migration");
2494
+ return total;
2495
+ }
2343
2496
  // Annotate the CommonJS export names for ESM import in node:
2344
2497
  0 && (module.exports = {
2345
2498
  ANSWER_LINE_COLOR,
@@ -2354,6 +2507,9 @@ var EditorHistory = class {
2354
2507
  EditorHistory,
2355
2508
  MARGIN_PRESETS,
2356
2509
  MAX_LIST_LEVEL,
2510
+ MAX_TABELA_PCT,
2511
+ MIN_LINHA_PX,
2512
+ MIN_TABELA_PCT,
2357
2513
  PAGE_PRESETS,
2358
2514
  SOFER_MIME,
2359
2515
  TABLE_BORDER_COLOR,
@@ -2397,6 +2553,7 @@ var EditorHistory = class {
2397
2553
  isCollapsed,
2398
2554
  isCommandOrigin,
2399
2555
  isEmbedAdjacentToCaret,
2556
+ isFormulaEmbed,
2400
2557
  isImageEmbed,
2401
2558
  isKnownBlockType,
2402
2559
  isLegacySerializedDocument,
@@ -2409,6 +2566,7 @@ var EditorHistory = class {
2409
2566
  moveToNextCell,
2410
2567
  moveToPrevCell,
2411
2568
  newBlockId,
2569
+ normalizarLarguras,
2412
2570
  orderedRange,
2413
2571
  positionsEqual,
2414
2572
  pxToMm,
@@ -2419,21 +2577,27 @@ var EditorHistory = class {
2419
2577
  setBlockAttrAtIndex,
2420
2578
  setBlockType,
2421
2579
  setCellAttr,
2422
- setColumnWidth,
2580
+ setColumnBoundary,
2423
2581
  setImageAttrs,
2424
2582
  setMark,
2583
+ setRowHeight,
2584
+ setRowHeights,
2585
+ setTableWidth,
2425
2586
  sliceDelta,
2426
2587
  sliceToInlineDelta,
2427
2588
  sliceToText,
2428
2589
  spanOf,
2429
2590
  splitCell,
2591
+ splitDeltaByLines,
2430
2592
  splitListItem,
2431
2593
  splitUnderscoreRuns,
2594
+ stripFontFamilyMarks,
2432
2595
  styleToCssText,
2433
2596
  tableLocationOf,
2434
2597
  tableRectSelection,
2435
2598
  toggleList,
2436
2599
  toggleMark,
2600
+ travaLarguraTabela,
2437
2601
  withDefaults
2438
2602
  });
2439
2603
  //# sourceMappingURL=index.cjs.map