@trafica/editor 1.0.38 → 1.0.40

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
@@ -1639,23 +1639,27 @@ function serializeBlock(node, idCounts = /* @__PURE__ */ new Map(), inCell = fal
1639
1639
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
1640
1640
  switch (node.type) {
1641
1641
  case "paragraph": {
1642
- const align = ((_a = node.attrs) == null ? void 0 : _a.align) ? ` data-align="${node.attrs.align}"` : "";
1643
- const style = inCell ? ` style="margin:0;min-height:1.2em;"` : "";
1644
- return `<p${align}${style}>${serializeChildren(node.children)}</p>`;
1642
+ const alignVal = (_a = node.attrs) == null ? void 0 : _a.align;
1643
+ const alignAttrs = alignVal ? ` data-align="${alignVal}" style="text-align:${alignVal};"` : "";
1644
+ const cellStyle = inCell ? ` style="margin:0;min-height:1.2em;${alignVal ? `text-align:${alignVal};` : ""}"` : "";
1645
+ const attrs = inCell ? alignVal ? ` data-align="${alignVal}"${cellStyle}` : cellStyle : alignAttrs;
1646
+ return `<p${attrs}>${serializeChildren(node.children)}</p>`;
1645
1647
  }
1646
1648
  case "heading": {
1647
1649
  const l = (_c = (_b = node.attrs) == null ? void 0 : _b.level) != null ? _c : 1;
1648
- const align = ((_d = node.attrs) == null ? void 0 : _d.align) ? ` data-align="${node.attrs.align}"` : "";
1650
+ const alignVal = (_d = node.attrs) == null ? void 0 : _d.align;
1651
+ const alignAttrs = alignVal ? ` data-align="${alignVal}" style="text-align:${alignVal};"` : "";
1649
1652
  const rawText = node.children.filter((c) => "text" in c).map((c) => c.text).join("");
1650
1653
  const base = slugify(rawText) || `heading-${l}`;
1651
1654
  const count = (_e = idCounts.get(base)) != null ? _e : 0;
1652
1655
  idCounts.set(base, count + 1);
1653
1656
  const id = count === 0 ? base : `${base}-${count}`;
1654
- return `<h${l} id="${id}"${align}>${serializeChildren(node.children)}</h${l}>`;
1657
+ return `<h${l} id="${id}"${alignAttrs}>${serializeChildren(node.children)}</h${l}>`;
1655
1658
  }
1656
1659
  case "blockquote": {
1657
- const align = ((_f = node.attrs) == null ? void 0 : _f.align) ? ` data-align="${node.attrs.align}"` : "";
1658
- return `<blockquote${align}>${serializeChildren(node.children)}</blockquote>`;
1660
+ const alignVal = (_f = node.attrs) == null ? void 0 : _f.align;
1661
+ const alignAttrs = alignVal ? ` data-align="${alignVal}" style="text-align:${alignVal};"` : "";
1662
+ return `<blockquote${alignAttrs}>${serializeChildren(node.children)}</blockquote>`;
1659
1663
  }
1660
1664
  case "bullet_list":
1661
1665
  return `<ul>${serializeChildren(node.children)}</ul>`;
@@ -1771,6 +1775,14 @@ function escapeHTML(str) {
1771
1775
  function escapeAttr(str) {
1772
1776
  return str.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
1773
1777
  }
1778
+ var VALID_ALIGNS = /* @__PURE__ */ new Set(["left", "center", "right", "justify"]);
1779
+ function getAlign(el) {
1780
+ const da = el.getAttribute("data-align");
1781
+ if (da && VALID_ALIGNS.has(da)) return da;
1782
+ const ta = el.style.textAlign;
1783
+ if (ta && VALID_ALIGNS.has(ta)) return ta;
1784
+ return null;
1785
+ }
1774
1786
  function parseHTMLBody(body) {
1775
1787
  const children = [];
1776
1788
  for (const child of Array.from(body.childNodes)) {
@@ -1791,35 +1803,35 @@ function parseHTMLNode(node) {
1791
1803
  switch (tag) {
1792
1804
  case "p":
1793
1805
  case "div": {
1794
- const align = el.getAttribute("data-align");
1806
+ const align = getAlign(el);
1795
1807
  return { type: "paragraph", attrs: align ? { align } : {}, children: parseInlineChildren(el) };
1796
1808
  }
1797
1809
  case "h1": {
1798
- const align = el.getAttribute("data-align");
1810
+ const align = getAlign(el);
1799
1811
  return { type: "heading", attrs: { level: 1, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1800
1812
  }
1801
1813
  case "h2": {
1802
- const align = el.getAttribute("data-align");
1814
+ const align = getAlign(el);
1803
1815
  return { type: "heading", attrs: { level: 2, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1804
1816
  }
1805
1817
  case "h3": {
1806
- const align = el.getAttribute("data-align");
1818
+ const align = getAlign(el);
1807
1819
  return { type: "heading", attrs: { level: 3, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1808
1820
  }
1809
1821
  case "h4": {
1810
- const align = el.getAttribute("data-align");
1822
+ const align = getAlign(el);
1811
1823
  return { type: "heading", attrs: { level: 4, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1812
1824
  }
1813
1825
  case "h5": {
1814
- const align = el.getAttribute("data-align");
1826
+ const align = getAlign(el);
1815
1827
  return { type: "heading", attrs: { level: 5, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1816
1828
  }
1817
1829
  case "h6": {
1818
- const align = el.getAttribute("data-align");
1830
+ const align = getAlign(el);
1819
1831
  return { type: "heading", attrs: { level: 6, ...align ? { align } : {} }, children: parseInlineChildren(el) };
1820
1832
  }
1821
1833
  case "blockquote": {
1822
- const align = el.getAttribute("data-align");
1834
+ const align = getAlign(el);
1823
1835
  return { type: "blockquote", attrs: align ? { align } : {}, children: parseInlineChildren(el) };
1824
1836
  }
1825
1837
  case "ul": {
@@ -7849,6 +7861,7 @@ function EditorCore({
7849
7861
  }) {
7850
7862
  const containerRef = react.useRef(null);
7851
7863
  const isRenderingRef = react.useRef(false);
7864
+ const skipRestoreSelectionRef = react.useRef(false);
7852
7865
  const scrollCaretIntoView = react.useCallback(() => {
7853
7866
  var _a, _b;
7854
7867
  const sel = window.getSelection();
@@ -7961,8 +7974,14 @@ function EditorCore({
7961
7974
  react.useLayoutEffect(() => {
7962
7975
  const container = containerRef.current;
7963
7976
  if (!container || !state.selection) return;
7977
+ if (skipRestoreSelectionRef.current) {
7978
+ skipRestoreSelectionRef.current = false;
7979
+ return;
7980
+ }
7981
+ isRenderingRef.current = true;
7964
7982
  restoreSelection(container, state.selection);
7965
7983
  scrollCaretIntoView();
7984
+ isRenderingRef.current = false;
7966
7985
  }, [state.selection]);
7967
7986
  react.useEffect(() => {
7968
7987
  const container = containerRef.current;
@@ -8027,6 +8046,7 @@ function EditorCore({
8027
8046
  if (currentSelection && JSON.stringify(currentSelection.anchor) === JSON.stringify(captured.anchor) && JSON.stringify(currentSelection.focus) === JSON.stringify(captured.focus)) {
8028
8047
  return;
8029
8048
  }
8049
+ skipRestoreSelectionRef.current = true;
8030
8050
  const tr = createTransaction();
8031
8051
  tr.steps.push(tr_setSelection(captured));
8032
8052
  engine.dispatch(tr);
@@ -8409,10 +8429,9 @@ function EditorCore({
8409
8429
  if (!container) return;
8410
8430
  const selection = captureSelection(container);
8411
8431
  if (!selection) return;
8432
+ skipRestoreSelectionRef.current = true;
8412
8433
  const tr = createTransaction();
8413
- tr.steps.push(
8414
- tr_setSelection(selection)
8415
- );
8434
+ tr.steps.push(tr_setSelection(selection));
8416
8435
  engine.dispatch(tr);
8417
8436
  }, [engine]);
8418
8437
  const imageResizeRef = react.useRef(null);