@sofereditor/react 0.9.0 → 0.10.1

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
@@ -139,6 +139,11 @@ function isWrapAnchorImg(n) {
139
139
  function isPhantomEmbed(n) {
140
140
  return n.nodeType === Node.ELEMENT_NODE && n.dataset.embedPhantom === "true";
141
141
  }
142
+ function isCellLineBoundary(n) {
143
+ if (n.nodeType !== Node.ELEMENT_NODE) return false;
144
+ const line = n.dataset.cellLine;
145
+ return line != null && Number(line) > 0;
146
+ }
142
147
  function indexInParent(n) {
143
148
  const parent = n.parentNode;
144
149
  if (!parent) return 0;
@@ -166,6 +171,7 @@ function textOffsetWithin(container, node, offsetInNode) {
166
171
  let done = false;
167
172
  function visit(n) {
168
173
  if (done) return;
174
+ if (isCellLineBoundary(n)) offset += 1;
169
175
  if (n === target) {
170
176
  if (n.nodeType === Node.TEXT_NODE) {
171
177
  offset += targetOff;
@@ -266,6 +272,7 @@ function locatePoint(root, pos) {
266
272
  let remaining = Math.max(0, pos.offset - containerStart);
267
273
  let result = null;
268
274
  let lastImg = null;
275
+ let lastBoundary = null;
269
276
  let lastText = null;
270
277
  function visit(n) {
271
278
  if (result) return;
@@ -277,6 +284,7 @@ function locatePoint(root, pos) {
277
284
  }
278
285
  remaining -= len;
279
286
  lastImg = null;
287
+ lastBoundary = null;
280
288
  lastText = n;
281
289
  return;
282
290
  }
@@ -284,6 +292,25 @@ function locatePoint(root, pos) {
284
292
  const el = n;
285
293
  if (isRejectedSubtree(el, container)) return;
286
294
  if (isImgEmbed(el) && isWrapAnchorImg(el)) return;
295
+ if (isCellLineBoundary(el)) {
296
+ if (remaining === 0) {
297
+ if (lastImg) {
298
+ const parent = lastImg.parentNode;
299
+ if (parent) {
300
+ result = { node: parent, offset: indexInParent(lastImg) + 1 };
301
+ return;
302
+ }
303
+ }
304
+ const prevLine = lastBoundary ?? el.previousElementSibling;
305
+ if (prevLine) {
306
+ result = { node: prevLine, offset: 0 };
307
+ return;
308
+ }
309
+ }
310
+ remaining -= 1;
311
+ lastBoundary = el;
312
+ lastImg = null;
313
+ }
287
314
  if (isImgEmbed(el) || isPhantomEmbed(el)) {
288
315
  if (remaining === 0) {
289
316
  const parent = el.parentNode;
@@ -294,6 +321,7 @@ function locatePoint(root, pos) {
294
321
  }
295
322
  remaining -= 1;
296
323
  lastImg = el;
324
+ lastBoundary = null;
297
325
  return;
298
326
  }
299
327
  for (const child of Array.from(el.childNodes)) {
@@ -303,7 +331,10 @@ function locatePoint(root, pos) {
303
331
  }
304
332
  visit(container);
305
333
  if (result) return result;
306
- if (lastImg && remaining === 0) {
334
+ if (lastBoundary) {
335
+ return { node: lastBoundary, offset: 0 };
336
+ }
337
+ if (lastImg) {
307
338
  const parent = lastImg.parentNode;
308
339
  if (parent) return { node: parent, offset: indexInParent(lastImg) + 1 };
309
340
  }
@@ -2835,11 +2866,11 @@ var import_core8 = require("@sofereditor/core");
2835
2866
  var import_react9 = require("react");
2836
2867
  var import_core6 = require("@sofereditor/core");
2837
2868
  var import_jsx_runtime10 = require("react/jsx-runtime");
2838
- function renderInline(delta, keyPrefix) {
2869
+ function renderInline(delta, keyPrefix, baseOffset = 0) {
2839
2870
  if (delta.length === 0 || delta.length === 1 && typeof delta[0].insert === "string" && delta[0].insert === "") {
2840
2871
  return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("br", { "data-empty": "true" });
2841
2872
  }
2842
- let offset = 0;
2873
+ let offset = baseOffset;
2843
2874
  const wrapAnchors = [];
2844
2875
  const body = [];
2845
2876
  for (const op of delta) {
@@ -3103,6 +3134,12 @@ function sliceDelta(delta, start, end) {
3103
3134
  var import_react10 = require("react");
3104
3135
  var import_core7 = require("@sofereditor/core");
3105
3136
  var import_jsx_runtime11 = require("react/jsx-runtime");
3137
+ function posicaoVisualDaDivisa(xLogico, larguraPx, rtl) {
3138
+ return rtl ? larguraPx - xLogico : xLogico;
3139
+ }
3140
+ function sinalDoArrasto(rtl) {
3141
+ return rtl ? -1 : 1;
3142
+ }
3106
3143
  function deltaPctDoArrasto(dxPx, larguraTabelaPx) {
3107
3144
  if (!(larguraTabelaPx > 0)) return 0;
3108
3145
  return dxPx / larguraTabelaPx * 100;
@@ -3112,7 +3149,7 @@ function distribuirAltura(base, deltaPx) {
3112
3149
  const fatia = deltaPx / base.length;
3113
3150
  return base.map((h) => Math.max(import_core7.MIN_LINHA_PX, Math.round(h + fatia)));
3114
3151
  }
3115
- function TableResizeOverlay({ tableRef, blockIndex, cols, rows }) {
3152
+ function TableResizeOverlay({ tableRef, blockIndex, cols, rows, rtl = false }) {
3116
3153
  const editor = useEditorContext();
3117
3154
  const [layout, setLayout] = (0, import_react10.useState)(null);
3118
3155
  const arrastoRef = (0, import_react10.useRef)(null);
@@ -3187,7 +3224,8 @@ function TableResizeOverlay({ tableRef, blockIndex, cols, rows }) {
3187
3224
  if (!d) return;
3188
3225
  switch (d.tipo) {
3189
3226
  case "coluna": {
3190
- const dPct = deltaPctDoArrasto(e.clientX - d.startX, d.tableWidthPx);
3227
+ const dx = (e.clientX - d.startX) * sinalDoArrasto(rtl);
3228
+ const dPct = deltaPctDoArrasto(dx, d.tableWidthPx);
3191
3229
  editor.setColumnBoundary(blockIndex, d.boundary, dPct, d.baseWidths);
3192
3230
  break;
3193
3231
  }
@@ -3212,7 +3250,7 @@ function TableResizeOverlay({ tableRef, blockIndex, cols, rows }) {
3212
3250
  }
3213
3251
  }
3214
3252
  },
3215
- [blockIndex, editor]
3253
+ [blockIndex, editor, rtl]
3216
3254
  );
3217
3255
  const containerWidthPxOf = (0, import_react10.useCallback)((table, tableWidthPx) => {
3218
3256
  return table.parentElement?.getBoundingClientRect().width || tableWidthPx;
@@ -3328,7 +3366,7 @@ function TableResizeOverlay({ tableRef, blockIndex, cols, rows }) {
3328
3366
  {
3329
3367
  contentEditable: false,
3330
3368
  className: "ed-col-resize-handle",
3331
- style: colHandleStyle(x),
3369
+ style: colHandleStyle(posicaoVisualDaDivisa(x, layout.width, rtl)),
3332
3370
  onPointerDown: onColumnPointerDown(c),
3333
3371
  onPointerMove,
3334
3372
  onPointerUp,
@@ -3501,6 +3539,27 @@ function clampListLevel(l) {
3501
3539
  if (typeof l !== "number" || !Number.isFinite(l)) return 0;
3502
3540
  return Math.max(0, Math.min(5, Math.trunc(l)));
3503
3541
  }
3542
+ function renderCellContent(attrs, delta, keyPrefix) {
3543
+ const kind = attrs?.listKind;
3544
+ if (!kind) return renderInline(delta, keyPrefix);
3545
+ const linhas = (0, import_core8.splitDeltaByLines)(delta);
3546
+ const Tag = kind === "ordered" ? "ol" : "ul";
3547
+ let baseOffset = 0;
3548
+ return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
3549
+ Tag,
3550
+ {
3551
+ className: `ed-list ed-list-${kind}`,
3552
+ "data-list-kind": kind,
3553
+ start: kind === "ordered" && typeof attrs?.listStart === "number" ? attrs.listStart : void 0,
3554
+ style: attrs?.listStyle ? { listStyleType: attrs.listStyle } : void 0,
3555
+ children: linhas.map((linha, i) => {
3556
+ const li = /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("li", { className: "ed-listitem", "data-cell-line": i, children: renderInline(linha, `${keyPrefix}-l${i}`, baseOffset) }, i);
3557
+ baseOffset += (0, import_core8.deltaLength)(linha) + 1;
3558
+ return li;
3559
+ })
3560
+ }
3561
+ );
3562
+ }
3504
3563
  function TableView({ block, index, tableFragment }) {
3505
3564
  const rows = clampPositive(block.attrs.rows);
3506
3565
  const cols = clampPositive(block.attrs.cols);
@@ -3525,6 +3584,7 @@ function TableView({ block, index, tableFragment }) {
3525
3584
  ref: tableRef,
3526
3585
  "data-block-index": index,
3527
3586
  "data-block-type": "table",
3587
+ dir: block.attrs.dir === "rtl" || block.attrs.dir === "ltr" ? block.attrs.dir : void 0,
3528
3588
  className: "ed-block ed-table",
3529
3589
  style: typeof tableWidth === "number" ? { width: `${tableWidth}%` } : void 0,
3530
3590
  children: [
@@ -3566,7 +3626,7 @@ function TableView({ block, index, tableFragment }) {
3566
3626
  )
3567
3627
  ),
3568
3628
  className: inRect ? "ed-cell ed-cell--selected" : "ed-cell",
3569
- children: renderInline(delta, `t${index}-c${flat}`)
3629
+ children: renderCellContent(cell?.attrs, delta, `t${index}-c${flat}`)
3570
3630
  },
3571
3631
  c
3572
3632
  );
@@ -3578,7 +3638,16 @@ function TableView({ block, index, tableFragment }) {
3578
3638
  ]
3579
3639
  }
3580
3640
  ),
3581
- isFirstFragment && /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(TableResizeOverlay, { tableRef, blockIndex: index, cols, rows })
3641
+ isFirstFragment && /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(
3642
+ TableResizeOverlay,
3643
+ {
3644
+ tableRef,
3645
+ blockIndex: index,
3646
+ cols,
3647
+ rows,
3648
+ rtl: block.attrs.dir === "rtl"
3649
+ }
3650
+ )
3582
3651
  ] });
3583
3652
  }
3584
3653
  function spanAttr(v) {
@@ -4363,6 +4432,10 @@ function useEditor(opts = {}) {
4363
4432
  const isListActive = (0, import_react14.useCallback)(
4364
4433
  (kind) => {
4365
4434
  const sel = selectionRef.current;
4435
+ if (sel.focus.cellIndex != null && doc.isTable(sel.focus.blockIndex)) {
4436
+ const real = doc.realCellIndex(sel.focus.blockIndex, sel.focus.cellIndex);
4437
+ return real != null && doc.getCellAttrs(sel.focus.blockIndex, real).listKind === kind;
4438
+ }
4366
4439
  const { start, end } = (0, import_core9.orderedRange)(sel);
4367
4440
  for (let i = start.blockIndex; i <= end.blockIndex; i++) {
4368
4441
  if (doc.getBlockType(i) !== "listItem") return false;
@@ -5799,7 +5872,6 @@ function TableBorderPanel({
5799
5872
 
5800
5873
  // src/Toolbar.tsx
5801
5874
  var import_jsx_runtime15 = require("react/jsx-runtime");
5802
- var FONT_FAMILIES = ["Arial"];
5803
5875
  var FONT_SIZES = ["8pt", "10pt", "11pt", "12pt", "14pt", "16pt", "18pt", "24pt", "32pt", "48pt"];
5804
5876
  var BLOCK_OPTIONS = [
5805
5877
  { value: "paragraph", label: "Par\xE1grafo", type: "paragraph" },
@@ -5873,11 +5945,6 @@ function Toolbar({ className }) {
5873
5945
  const onHighlightChange = (e) => {
5874
5946
  setMark("highlight", e.target.value);
5875
5947
  };
5876
- const onFontFamilyChange = (e) => {
5877
- const v = e.target.value;
5878
- if (v === "") removeMark("fontFamily");
5879
- else setMark("fontFamily", v);
5880
- };
5881
5948
  const onFontSizeChange = (e) => {
5882
5949
  const v = e.target.value;
5883
5950
  if (v === "") removeMark("fontSize");
@@ -5938,7 +6005,6 @@ function Toolbar({ className }) {
5938
6005
  );
5939
6006
  const colorValue = typeof active.color === "string" ? active.color : "#000000";
5940
6007
  const highlightValue = typeof active.highlight === "string" ? active.highlight : "#fff176";
5941
- const fontFamilyValue = typeof active.fontFamily === "string" ? active.fontFamily : "";
5942
6008
  const fontSizeValue = typeof active.fontSize === "string" ? active.fontSize : "";
5943
6009
  const blockValue = blockSelectValue(blockType, blockLevel);
5944
6010
  const selectedEmbed = editor.getSelectedEmbed();
@@ -6037,36 +6103,20 @@ function Toolbar({ className }) {
6037
6103
  }
6038
6104
  )
6039
6105
  ] }),
6040
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(Group, { children: [
6041
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(
6042
- "select",
6043
- {
6044
- className: "ed-toolbar-select",
6045
- value: fontFamilyValue,
6046
- onChange: onFontFamilyChange,
6047
- onMouseDown: stop,
6048
- "aria-label": "Fam\xEDlia da fonte",
6049
- children: [
6050
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: "", children: "Fonte\u2026" }),
6051
- FONT_FAMILIES.map((f) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: f, children: f }, f))
6052
- ]
6053
- }
6054
- ),
6055
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(
6056
- "select",
6057
- {
6058
- className: "ed-toolbar-select",
6059
- value: fontSizeValue,
6060
- onChange: onFontSizeChange,
6061
- onMouseDown: stop,
6062
- "aria-label": "Tamanho da fonte",
6063
- children: [
6064
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: "", children: "Tamanho\u2026" }),
6065
- FONT_SIZES.map((s) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: s, children: s }, s))
6066
- ]
6067
- }
6068
- )
6069
- ] }),
6106
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Group, { children: /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(
6107
+ "select",
6108
+ {
6109
+ className: "ed-toolbar-select",
6110
+ value: fontSizeValue,
6111
+ onChange: onFontSizeChange,
6112
+ onMouseDown: stop,
6113
+ "aria-label": "Tamanho da fonte",
6114
+ children: [
6115
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: "", children: "Tamanho\u2026" }),
6116
+ FONT_SIZES.map((s) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("option", { value: s, children: s }, s))
6117
+ ]
6118
+ }
6119
+ ) }),
6070
6120
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Group, { children: ALIGN_OPTIONS.map((a) => /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(
6071
6121
  "button",
6072
6122
  {