@bobfrankston/rmfmail 1.1.150 → 1.1.152

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.
@@ -777,30 +777,47 @@ async function createTinyMceEditor(container2, opts = {}) {
777
777
  applyZoom();
778
778
  }
779
779
  });
780
- ed.on("keypress", (e) => {
781
- if (e.ctrlKey || e.altKey || e.metaKey)
780
+ const installLinkEscape = () => {
781
+ const doc = ed.getDoc();
782
+ if (!doc || doc.__rmfLinkEscapeInstalled)
782
783
  return;
783
- const sel = ed.selection;
784
- const rng = sel.getRng();
785
- if (!rng || !rng.collapsed)
786
- return;
787
- const a = ed.dom.getParent(sel.getNode(), "a");
788
- if (!a)
789
- return;
790
- let tail;
791
- try {
792
- tail = rng.cloneRange();
793
- tail.setEndAfter(a);
794
- } catch {
795
- return;
796
- }
797
- if (tail.toString().length > 0)
798
- return;
799
- const after = ed.getDoc().createRange();
800
- after.setStartAfter(a);
801
- after.collapse(true);
802
- sel.setRng(after);
803
- });
784
+ doc.__rmfLinkEscapeInstalled = true;
785
+ doc.addEventListener("beforeinput", (e) => {
786
+ const kind = e.inputType || "";
787
+ const isInsert = kind === "insertText" || kind === "insertCompositionText" || kind === "insertFromPaste" || kind === "insertFromDrop" || kind === "insertFromComposition";
788
+ if (!isInsert)
789
+ return;
790
+ const sel = doc.getSelection();
791
+ if (!sel || sel.rangeCount === 0)
792
+ return;
793
+ const rng = sel.getRangeAt(0);
794
+ if (!rng.collapsed)
795
+ return;
796
+ const node = rng.startContainer;
797
+ const a = node.nodeType === Node.ELEMENT_NODE ? node : node.parentNode;
798
+ if (!a)
799
+ return;
800
+ const link = a.closest?.("a");
801
+ if (!link)
802
+ return;
803
+ let tail;
804
+ try {
805
+ tail = rng.cloneRange();
806
+ tail.setEndAfter(link);
807
+ } catch {
808
+ return;
809
+ }
810
+ if (tail.toString().length > 0)
811
+ return;
812
+ const after = doc.createRange();
813
+ after.setStartAfter(link);
814
+ after.collapse(true);
815
+ sel.removeAllRanges();
816
+ sel.addRange(after);
817
+ }, true);
818
+ };
819
+ ed.on("init", installLinkEscape);
820
+ ed.on("SetContent", installLinkEscape);
804
821
  ed.on("init", () => {
805
822
  try {
806
823
  const body = ed.getBody();
@@ -1947,6 +1964,13 @@ function decorate(editor2, sp) {
1947
1964
  const doc = editor2.getDoc?.();
1948
1965
  if (!body || !doc)
1949
1966
  return;
1967
+ let savedFocusNode = null;
1968
+ let savedFocusOffset = 0;
1969
+ const _preSel = doc.getSelection();
1970
+ if (_preSel && _preSel.rangeCount > 0) {
1971
+ savedFocusNode = _preSel.focusNode;
1972
+ savedFocusOffset = _preSel.focusOffset;
1973
+ }
1950
1974
  const savedAbs = caretAbsOffsetFromBody(body);
1951
1975
  const scroller = doc.scrollingElement || doc.documentElement;
1952
1976
  const savedScrollTop = scroller?.scrollTop ?? 0;
@@ -2030,7 +2054,23 @@ function decorate(editor2, sp) {
2030
2054
  }
2031
2055
  });
2032
2056
  } finally {
2033
- if (savedAbs != null)
2057
+ let restored = false;
2058
+ if (savedFocusNode && body.contains(savedFocusNode)) {
2059
+ try {
2060
+ const range = doc.createRange();
2061
+ const maxOffset = savedFocusNode.nodeType === Node.TEXT_NODE ? savedFocusNode.data.length : savedFocusNode.childNodes.length;
2062
+ range.setStart(savedFocusNode, Math.min(savedFocusOffset, maxOffset));
2063
+ range.collapse(true);
2064
+ const sel = doc.getSelection();
2065
+ if (sel) {
2066
+ sel.removeAllRanges();
2067
+ sel.addRange(range);
2068
+ restored = true;
2069
+ }
2070
+ } catch {
2071
+ }
2072
+ }
2073
+ if (!restored && savedAbs != null)
2034
2074
  restoreCaretFromAbsOffset(body, savedAbs);
2035
2075
  if (scroller && scroller.scrollTop !== savedScrollTop)
2036
2076
  scroller.scrollTop = savedScrollTop;