@sofereditor/core 0.6.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,
@@ -84,6 +87,8 @@ __export(index_exports, {
84
87
  insertText: () => insertText,
85
88
  isCollapsed: () => isCollapsed,
86
89
  isCommandOrigin: () => isCommandOrigin,
90
+ isEmbedAdjacentToCaret: () => isEmbedAdjacentToCaret,
91
+ isFormulaEmbed: () => isFormulaEmbed,
87
92
  isImageEmbed: () => isImageEmbed,
88
93
  isKnownBlockType: () => isKnownBlockType,
89
94
  isLegacySerializedDocument: () => isLegacySerializedDocument,
@@ -96,6 +101,7 @@ __export(index_exports, {
96
101
  moveToNextCell: () => moveToNextCell,
97
102
  moveToPrevCell: () => moveToPrevCell,
98
103
  newBlockId: () => newBlockId,
104
+ normalizarLarguras: () => normalizarLarguras,
99
105
  orderedRange: () => orderedRange,
100
106
  positionsEqual: () => positionsEqual,
101
107
  pxToMm: () => pxToMm,
@@ -106,9 +112,12 @@ __export(index_exports, {
106
112
  setBlockAttrAtIndex: () => setBlockAttrAtIndex,
107
113
  setBlockType: () => setBlockType,
108
114
  setCellAttr: () => setCellAttr,
109
- setColumnWidth: () => setColumnWidth,
115
+ setColumnBoundary: () => setColumnBoundary,
110
116
  setImageAttrs: () => setImageAttrs,
111
117
  setMark: () => setMark,
118
+ setRowHeight: () => setRowHeight,
119
+ setRowHeights: () => setRowHeights,
120
+ setTableWidth: () => setTableWidth,
112
121
  sliceDelta: () => sliceDelta,
113
122
  sliceToInlineDelta: () => sliceToInlineDelta,
114
123
  sliceToText: () => sliceToText,
@@ -121,6 +130,7 @@ __export(index_exports, {
121
130
  tableRectSelection: () => tableRectSelection,
122
131
  toggleList: () => toggleList,
123
132
  toggleMark: () => toggleMark,
133
+ travaLarguraTabela: () => travaLarguraTabela,
124
134
  withDefaults: () => withDefaults
125
135
  });
126
136
  module.exports = __toCommonJS(index_exports);
@@ -130,6 +140,9 @@ var MAX_LIST_LEVEL = 5;
130
140
  function isImageEmbed(v) {
131
141
  return typeof v === "object" && v !== null && v.type === "image" && typeof v.src === "string";
132
142
  }
143
+ function isFormulaEmbed(v) {
144
+ return isImageEmbed(v) && v.formula != null;
145
+ }
133
146
  function isLegacySerializedDocument(v) {
134
147
  return Array.isArray(v);
135
148
  }
@@ -1171,6 +1184,24 @@ function deleteForward(ctx) {
1171
1184
  }
1172
1185
  });
1173
1186
  }
1187
+ function isEmbedAdjacentToCaret(ctx, direction) {
1188
+ const sel = ctx.getSelection();
1189
+ if (!isCollapsed(sel)) return false;
1190
+ const pos = sel.focus;
1191
+ const yText = ctx.doc.textAt(pos.blockIndex, pos.cellIndex);
1192
+ if (!yText) return false;
1193
+ const delta = yText.toDelta();
1194
+ let cursor = 0;
1195
+ for (const op of delta) {
1196
+ const len = typeof op.insert === "string" ? op.insert.length : 1;
1197
+ const opStart = cursor;
1198
+ const opEnd = cursor + len;
1199
+ cursor = opEnd;
1200
+ const matches = direction === "backward" ? opEnd === pos.offset : opStart === pos.offset;
1201
+ if (matches) return isImageEmbed(op.insert);
1202
+ }
1203
+ return false;
1204
+ }
1174
1205
  var booleanMarks = /* @__PURE__ */ new Set(["bold", "italic", "underline", "strike"]);
1175
1206
  function toggleMark(ctx, name, value) {
1176
1207
  const sel = ctx.getSelection();
@@ -1662,6 +1693,12 @@ function insertTableRow(ctx, blockIndex, row, where) {
1662
1693
  }
1663
1694
  cells.insert(P * cols, fresh);
1664
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
+ }
1665
1702
  const firstReal = newRowCovered.findIndex((v) => !v);
1666
1703
  const targetCellIdx = P * cols + (firstReal < 0 ? 0 : firstReal);
1667
1704
  ctx.setSelection(
@@ -1707,6 +1744,12 @@ function deleteTableRow(ctx, blockIndex, row) {
1707
1744
  }
1708
1745
  cells.delete(R * cols, cols);
1709
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
+ }
1710
1753
  for (const p of promotions) {
1711
1754
  const m = ctx.doc.getCellAttrsMap(blockIndex, p.flatNew);
1712
1755
  if (!m) continue;
@@ -1766,9 +1809,12 @@ function insertTableColumn(ctx, blockIndex, col, where) {
1766
1809
  attrs.set("cols", cols + 1);
1767
1810
  const widths = attrs.get("colWidths");
1768
1811
  if (Array.isArray(widths) && widths.length === cols) {
1769
- const next = widths.slice();
1770
- next.splice(C, 0, 100);
1771
- 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));
1772
1818
  }
1773
1819
  const newCols = cols + 1;
1774
1820
  const focusRow = 0;
@@ -1841,7 +1887,7 @@ function deleteTableColumn(ctx, blockIndex, col) {
1841
1887
  if (Array.isArray(widths) && widths.length === cols) {
1842
1888
  const next = widths.slice();
1843
1889
  next.splice(C, 1);
1844
- attrs.set("colWidths", next);
1890
+ attrs.set("colWidths", normalizarLarguras(next, cols - 1));
1845
1891
  }
1846
1892
  const newCol = Math.min(C, newCols - 1);
1847
1893
  const target = findNextRealCell(ctx.doc, blockIndex, newCol, rows * newCols) ?? 0;
@@ -2129,20 +2175,81 @@ function splitCell(ctx, blockIndex, row, col) {
2129
2175
  });
2130
2176
  return true;
2131
2177
  }
2132
- 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) {
2133
2194
  if (!ctx.doc.isTable(blockIndex)) return;
2134
2195
  const { cols } = ctx.doc.getTableSize(blockIndex);
2135
- if (col < 0 || col >= cols) return;
2136
- const w = Math.max(20, Math.round(widthPx));
2196
+ if (boundary < 0 || boundary >= cols - 1) return;
2137
2197
  transact(ctx.doc, () => {
2138
2198
  const attrsMap = ctx.doc.getBlockAttrsMap(blockIndex);
2139
2199
  if (!attrsMap) return;
2140
- const current = attrsMap.get("colWidths");
2141
- const next = Array.isArray(current) && current.length === cols ? current.slice() : new Array(cols).fill(120);
2142
- 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);
2143
2211
  attrsMap.set("colWidths", next);
2144
2212
  });
2145
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
+ }
2146
2253
  function moveToNextCell(ctx) {
2147
2254
  const sel = ctx.getSelection();
2148
2255
  const loc = tableLocationOf(ctx.doc, sel.focus);
@@ -2335,6 +2442,9 @@ var EditorHistory = class {
2335
2442
  EditorHistory,
2336
2443
  MARGIN_PRESETS,
2337
2444
  MAX_LIST_LEVEL,
2445
+ MAX_TABELA_PCT,
2446
+ MIN_LINHA_PX,
2447
+ MIN_TABELA_PCT,
2338
2448
  PAGE_PRESETS,
2339
2449
  SOFER_MIME,
2340
2450
  TABLE_BORDER_COLOR,
@@ -2377,6 +2487,8 @@ var EditorHistory = class {
2377
2487
  insertText,
2378
2488
  isCollapsed,
2379
2489
  isCommandOrigin,
2490
+ isEmbedAdjacentToCaret,
2491
+ isFormulaEmbed,
2380
2492
  isImageEmbed,
2381
2493
  isKnownBlockType,
2382
2494
  isLegacySerializedDocument,
@@ -2389,6 +2501,7 @@ var EditorHistory = class {
2389
2501
  moveToNextCell,
2390
2502
  moveToPrevCell,
2391
2503
  newBlockId,
2504
+ normalizarLarguras,
2392
2505
  orderedRange,
2393
2506
  positionsEqual,
2394
2507
  pxToMm,
@@ -2399,9 +2512,12 @@ var EditorHistory = class {
2399
2512
  setBlockAttrAtIndex,
2400
2513
  setBlockType,
2401
2514
  setCellAttr,
2402
- setColumnWidth,
2515
+ setColumnBoundary,
2403
2516
  setImageAttrs,
2404
2517
  setMark,
2518
+ setRowHeight,
2519
+ setRowHeights,
2520
+ setTableWidth,
2405
2521
  sliceDelta,
2406
2522
  sliceToInlineDelta,
2407
2523
  sliceToText,
@@ -2414,6 +2530,7 @@ var EditorHistory = class {
2414
2530
  tableRectSelection,
2415
2531
  toggleList,
2416
2532
  toggleMark,
2533
+ travaLarguraTabela,
2417
2534
  withDefaults
2418
2535
  });
2419
2536
  //# sourceMappingURL=index.cjs.map