@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.js CHANGED
@@ -3,6 +3,9 @@ var MAX_LIST_LEVEL = 5;
3
3
  function isImageEmbed(v) {
4
4
  return typeof v === "object" && v !== null && v.type === "image" && typeof v.src === "string";
5
5
  }
6
+ function isFormulaEmbed(v) {
7
+ return isImageEmbed(v) && v.formula != null;
8
+ }
6
9
  function isLegacySerializedDocument(v) {
7
10
  return Array.isArray(v);
8
11
  }
@@ -722,6 +725,25 @@ function sameMarkValue(a, b) {
722
725
  }
723
726
  return false;
724
727
  }
728
+ function splitDeltaByLines(delta) {
729
+ const lines = [[]];
730
+ for (const op of delta) {
731
+ if (typeof op.insert !== "string") {
732
+ lines[lines.length - 1].push(op);
733
+ continue;
734
+ }
735
+ const parts = op.insert.split("\n");
736
+ for (let i = 0; i < parts.length; i++) {
737
+ if (i > 0) lines.push([]);
738
+ const part = parts[i];
739
+ if (part.length === 0) continue;
740
+ lines[lines.length - 1].push(
741
+ op.attributes ? { insert: part, attributes: op.attributes } : { insert: part }
742
+ );
743
+ }
744
+ }
745
+ return lines;
746
+ }
725
747
 
726
748
  // src/decorations.ts
727
749
  var BLANK_MIN_RUN = 3;
@@ -1345,7 +1367,12 @@ function mergeWithPrevious(ctx, blockIndex) {
1345
1367
  function toggleList(ctx, kind) {
1346
1368
  transact(ctx.doc, () => {
1347
1369
  const sel = ctx.getSelection();
1348
- if (sel.anchor.cellIndex != null || sel.focus.cellIndex != null) return;
1370
+ if (sel.anchor.cellIndex != null || sel.focus.cellIndex != null) {
1371
+ const real = sel.focus.cellIndex != null ? ctx.doc.realCellIndex(sel.focus.blockIndex, sel.focus.cellIndex) : void 0;
1372
+ const atual = real != null ? ctx.doc.getCellAttrs(sel.focus.blockIndex, real).listKind : void 0;
1373
+ setCellAttr(ctx, "listKind", atual === kind ? null : kind);
1374
+ return;
1375
+ }
1349
1376
  const indices = selectedBlockIndices(sel, ctx.doc.blockCount());
1350
1377
  const allMatching = indices.every((i) => {
1351
1378
  const block = ctx.doc.getBlock(i);
@@ -1553,6 +1580,12 @@ function insertTableRow(ctx, blockIndex, row, where) {
1553
1580
  }
1554
1581
  cells.insert(P * cols, fresh);
1555
1582
  attrs.set("rows", rows + 1);
1583
+ const heights = attrs.get("rowHeights");
1584
+ if (Array.isArray(heights) && heights.length === rows) {
1585
+ const next = heights.slice();
1586
+ next.splice(P, 0, MIN_LINHA_PX);
1587
+ attrs.set("rowHeights", next);
1588
+ }
1556
1589
  const firstReal = newRowCovered.findIndex((v) => !v);
1557
1590
  const targetCellIdx = P * cols + (firstReal < 0 ? 0 : firstReal);
1558
1591
  ctx.setSelection(
@@ -1598,6 +1631,12 @@ function deleteTableRow(ctx, blockIndex, row) {
1598
1631
  }
1599
1632
  cells.delete(R * cols, cols);
1600
1633
  attrs.set("rows", rows - 1);
1634
+ const heights = attrs.get("rowHeights");
1635
+ if (Array.isArray(heights) && heights.length === rows) {
1636
+ const next = heights.slice();
1637
+ next.splice(R, 1);
1638
+ attrs.set("rowHeights", next);
1639
+ }
1601
1640
  for (const p of promotions) {
1602
1641
  const m = ctx.doc.getCellAttrsMap(blockIndex, p.flatNew);
1603
1642
  if (!m) continue;
@@ -1657,9 +1696,12 @@ function insertTableColumn(ctx, blockIndex, col, where) {
1657
1696
  attrs.set("cols", cols + 1);
1658
1697
  const widths = attrs.get("colWidths");
1659
1698
  if (Array.isArray(widths) && widths.length === cols) {
1660
- const next = widths.slice();
1661
- next.splice(C, 0, 100);
1662
- attrs.set("colWidths", next);
1699
+ const base = normalizarLarguras(widths, cols);
1700
+ const media = arredonda(100 / (cols + 1));
1701
+ const escala = cols / (cols + 1);
1702
+ const next = base.map((w) => arredonda(w * escala));
1703
+ next.splice(C, 0, media);
1704
+ attrs.set("colWidths", normalizarLarguras(next, cols + 1));
1663
1705
  }
1664
1706
  const newCols = cols + 1;
1665
1707
  const focusRow = 0;
@@ -1732,7 +1774,7 @@ function deleteTableColumn(ctx, blockIndex, col) {
1732
1774
  if (Array.isArray(widths) && widths.length === cols) {
1733
1775
  const next = widths.slice();
1734
1776
  next.splice(C, 1);
1735
- attrs.set("colWidths", next);
1777
+ attrs.set("colWidths", normalizarLarguras(next, cols - 1));
1736
1778
  }
1737
1779
  const newCol = Math.min(C, newCols - 1);
1738
1780
  const target = findNextRealCell(ctx.doc, blockIndex, newCol, rows * newCols) ?? 0;
@@ -2020,20 +2062,81 @@ function splitCell(ctx, blockIndex, row, col) {
2020
2062
  });
2021
2063
  return true;
2022
2064
  }
2023
- function setColumnWidth(ctx, blockIndex, col, widthPx) {
2065
+ var SOMA_OK = (s) => Math.abs(s - 100) < 0.5;
2066
+ var arredonda = (n) => Math.round(n * 1e3) / 1e3;
2067
+ function normalizarLarguras(widths, cols) {
2068
+ const igual = () => new Array(cols).fill(100 / cols);
2069
+ if (!Array.isArray(widths) || widths.length !== cols || cols <= 0) {
2070
+ return igual();
2071
+ }
2072
+ if (!widths.every((w) => typeof w === "number" && isFinite(w) && w > 0)) {
2073
+ return igual();
2074
+ }
2075
+ const soma = widths.reduce((a, b) => a + b, 0);
2076
+ if (SOMA_OK(soma)) return widths.slice();
2077
+ return widths.map((w) => arredonda(w / soma * 100));
2078
+ }
2079
+ var MIN_COLUNA_PCT = 3;
2080
+ function setColumnBoundary(ctx, blockIndex, boundary, deltaPct, base) {
2024
2081
  if (!ctx.doc.isTable(blockIndex)) return;
2025
2082
  const { cols } = ctx.doc.getTableSize(blockIndex);
2026
- if (col < 0 || col >= cols) return;
2027
- const w = Math.max(20, Math.round(widthPx));
2083
+ if (boundary < 0 || boundary >= cols - 1) return;
2028
2084
  transact(ctx.doc, () => {
2029
2085
  const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2030
2086
  if (!attrsMap) return;
2031
- const current = attrsMap.get("colWidths");
2032
- const next = Array.isArray(current) && current.length === cols ? current.slice() : new Array(cols).fill(120);
2033
- next[col] = w;
2087
+ const larguras = normalizarLarguras(
2088
+ base ?? attrsMap.get("colWidths"),
2089
+ cols
2090
+ );
2091
+ const a = larguras[boundary];
2092
+ const b = larguras[boundary + 1];
2093
+ const piso = Math.min(MIN_COLUNA_PCT, (a + b) / 2);
2094
+ const d = Math.max(piso - a, Math.min(deltaPct, b - piso));
2095
+ const next = larguras.slice();
2096
+ next[boundary] = arredonda(a + d);
2097
+ next[boundary + 1] = arredonda(b - d);
2034
2098
  attrsMap.set("colWidths", next);
2035
2099
  });
2036
2100
  }
2101
+ var MIN_TABELA_PCT = 20;
2102
+ var MAX_TABELA_PCT = 100;
2103
+ function travaLarguraTabela(pct) {
2104
+ return Math.max(MIN_TABELA_PCT, Math.min(MAX_TABELA_PCT, pct));
2105
+ }
2106
+ function setTableWidth(ctx, blockIndex, pct) {
2107
+ if (!ctx.doc.isTable(blockIndex)) return;
2108
+ const v = travaLarguraTabela(arredonda(pct));
2109
+ transact(ctx.doc, () => {
2110
+ ctx.doc.getBlockAttrsMap(blockIndex)?.set("tableWidth", v);
2111
+ });
2112
+ }
2113
+ var MIN_LINHA_PX = 16;
2114
+ function setRowHeight(ctx, blockIndex, row, px) {
2115
+ if (!ctx.doc.isTable(blockIndex)) return;
2116
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2117
+ if (row < 0 || row >= rows) return;
2118
+ transact(ctx.doc, () => {
2119
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2120
+ if (!attrsMap) return;
2121
+ const atual = attrsMap.get("rowHeights");
2122
+ const base = Array.isArray(atual) && atual.length === rows ? atual.slice() : new Array(rows).fill(MIN_LINHA_PX);
2123
+ base[row] = Math.max(MIN_LINHA_PX, Math.round(px));
2124
+ attrsMap.set("rowHeights", base);
2125
+ });
2126
+ }
2127
+ function setRowHeights(ctx, blockIndex, heights) {
2128
+ if (!ctx.doc.isTable(blockIndex)) return;
2129
+ const { rows } = ctx.doc.getTableSize(blockIndex);
2130
+ if (heights.length !== rows) return;
2131
+ transact(ctx.doc, () => {
2132
+ const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2133
+ if (!attrsMap) return;
2134
+ attrsMap.set(
2135
+ "rowHeights",
2136
+ heights.map((h) => Math.max(MIN_LINHA_PX, Math.round(h)))
2137
+ );
2138
+ });
2139
+ }
2037
2140
  function moveToNextCell(ctx) {
2038
2141
  const sel = ctx.getSelection();
2039
2142
  const loc = tableLocationOf(ctx.doc, sel.focus);
@@ -2212,6 +2315,45 @@ var EditorHistory = class {
2212
2315
  this.undoManager.destroy();
2213
2316
  }
2214
2317
  };
2318
+
2319
+ // src/migrations.ts
2320
+ function coletar(yText, alvos) {
2321
+ if (!yText) return 0;
2322
+ const delta = yText.toDelta();
2323
+ const ranges = [];
2324
+ let index = 0;
2325
+ for (const op of delta) {
2326
+ const len = typeof op.insert === "string" ? op.insert.length : 1;
2327
+ if (typeof op.insert === "string" && op.attributes && "fontFamily" in op.attributes) {
2328
+ ranges.push([index, len]);
2329
+ }
2330
+ index += len;
2331
+ }
2332
+ if (ranges.length > 0) alvos.push({ yText, ranges });
2333
+ return ranges.length;
2334
+ }
2335
+ function stripFontFamilyMarks(doc, opts) {
2336
+ const alvos = [];
2337
+ let total = 0;
2338
+ for (let i = 0; i < doc.blockCount(); i++) {
2339
+ total += coletar(doc.getBlockText(i), alvos);
2340
+ const cells = doc.getCells(i);
2341
+ if (cells) {
2342
+ for (let c = 0; c < cells.length; c++) {
2343
+ total += coletar(doc.getCellText(i, c), alvos);
2344
+ }
2345
+ }
2346
+ }
2347
+ if (total === 0 || opts?.dryRun) return total;
2348
+ doc.ydoc.transact(() => {
2349
+ for (const { yText, ranges } of alvos) {
2350
+ for (const [index, length] of ranges) {
2351
+ yText.format(index, length, { fontFamily: null });
2352
+ }
2353
+ }
2354
+ }, "migration");
2355
+ return total;
2356
+ }
2215
2357
  export {
2216
2358
  ANSWER_LINE_COLOR,
2217
2359
  ANSWER_LINE_MAX,
@@ -2225,6 +2367,9 @@ export {
2225
2367
  EditorHistory,
2226
2368
  MARGIN_PRESETS,
2227
2369
  MAX_LIST_LEVEL,
2370
+ MAX_TABELA_PCT,
2371
+ MIN_LINHA_PX,
2372
+ MIN_TABELA_PCT,
2228
2373
  PAGE_PRESETS,
2229
2374
  SOFER_MIME,
2230
2375
  TABLE_BORDER_COLOR,
@@ -2268,6 +2413,7 @@ export {
2268
2413
  isCollapsed,
2269
2414
  isCommandOrigin,
2270
2415
  isEmbedAdjacentToCaret,
2416
+ isFormulaEmbed,
2271
2417
  isImageEmbed,
2272
2418
  isKnownBlockType,
2273
2419
  isLegacySerializedDocument,
@@ -2280,6 +2426,7 @@ export {
2280
2426
  moveToNextCell,
2281
2427
  moveToPrevCell,
2282
2428
  newBlockId,
2429
+ normalizarLarguras,
2283
2430
  orderedRange,
2284
2431
  positionsEqual,
2285
2432
  pxToMm,
@@ -2290,21 +2437,27 @@ export {
2290
2437
  setBlockAttrAtIndex,
2291
2438
  setBlockType,
2292
2439
  setCellAttr,
2293
- setColumnWidth,
2440
+ setColumnBoundary,
2294
2441
  setImageAttrs,
2295
2442
  setMark,
2443
+ setRowHeight,
2444
+ setRowHeights,
2445
+ setTableWidth,
2296
2446
  sliceDelta,
2297
2447
  sliceToInlineDelta,
2298
2448
  sliceToText,
2299
2449
  spanOf,
2300
2450
  splitCell,
2451
+ splitDeltaByLines,
2301
2452
  splitListItem,
2302
2453
  splitUnderscoreRuns,
2454
+ stripFontFamilyMarks,
2303
2455
  styleToCssText,
2304
2456
  tableLocationOf,
2305
2457
  tableRectSelection,
2306
2458
  toggleList,
2307
2459
  toggleMark,
2460
+ travaLarguraTabela,
2308
2461
  withDefaults
2309
2462
  };
2310
2463
  //# sourceMappingURL=index.js.map