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