@seliseblocks/mailcraft 0.2.11 → 0.2.13

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS.md +8 -0
  3. package/dist/mailcraft-editor.bundle.js +69 -57
  4. package/dist/mailcraft-editor.bundle.js.map +3 -3
  5. package/package.json +2 -2
  6. package/src/core/code-tools.js +302 -0
  7. package/src/core/editor-core.js +61 -0
  8. package/src/core/export.js +5 -2
  9. package/src/core/i18n/ar.js +8 -0
  10. package/src/core/i18n/bg.js +8 -0
  11. package/src/core/i18n/bn.js +8 -0
  12. package/src/core/i18n/ca.js +8 -0
  13. package/src/core/i18n/cs.js +8 -0
  14. package/src/core/i18n/da.js +8 -0
  15. package/src/core/i18n/de-CH.js +8 -0
  16. package/src/core/i18n/de.js +8 -0
  17. package/src/core/i18n/dz.js +8 -0
  18. package/src/core/i18n/el.js +8 -0
  19. package/src/core/i18n/en.js +8 -0
  20. package/src/core/i18n/es.js +8 -0
  21. package/src/core/i18n/et.js +8 -0
  22. package/src/core/i18n/fi.js +8 -0
  23. package/src/core/i18n/fr.js +8 -0
  24. package/src/core/i18n/hr.js +8 -0
  25. package/src/core/i18n/hu.js +8 -0
  26. package/src/core/i18n/index.js +83 -83
  27. package/src/core/i18n/it.js +8 -0
  28. package/src/core/i18n/lt.js +8 -0
  29. package/src/core/i18n/lv.js +8 -0
  30. package/src/core/i18n/nb.js +8 -0
  31. package/src/core/i18n/nl.js +8 -0
  32. package/src/core/i18n/pl.js +8 -0
  33. package/src/core/i18n/pt.js +8 -0
  34. package/src/core/i18n/ro.js +8 -0
  35. package/src/core/i18n/ru.js +8 -0
  36. package/src/core/i18n/sk.js +8 -0
  37. package/src/core/i18n/sl.js +8 -0
  38. package/src/core/i18n/sv.js +8 -0
  39. package/src/core/i18n/tr.js +8 -0
  40. package/src/core/i18n/uk.js +8 -0
  41. package/src/core/icons.js +2 -0
  42. package/src/core/ids.js +1 -1
  43. package/src/core/layout-style.js +100 -100
  44. package/src/core/parse.js +10 -10
  45. package/src/core/placeholder.js +15 -15
  46. package/src/core/variables.js +11 -11
  47. package/src/mailcraft-editor.js +468 -16
  48. package/src/render/focus-preserve.js +158 -158
  49. package/src/render/rte.js +27 -2
  50. package/src/render/story.js +415 -415
@@ -1,158 +1,158 @@
1
- /**
2
- * The original relies on React's reconciliation to keep a focused `<input>`'s
3
- * DOM node alive across a per-keystroke re-render (Design-tab fields, the
4
- * campaign title, AI brief/goal/tone, search boxes, the code textarea all
5
- * commit on every `input` event, not just on blur). This renderer has no
6
- * VDOM -- it rebuilds DOM from scratch -- so without this, typing a single
7
- * character into any of those fields would lose focus immediately after.
8
- *
9
- * The fix: every such input (and every RTE-edited block: text, heading, box,
10
- * html) carries a stable `data-focus-key`. Before a rebuild, capture the
11
- * focused element's key + selection range; after, find the new element with
12
- * the same key and restore focus and the caret position, so the net effect
13
- * matches React's outcome even though the DOM node identity changed.
14
- *
15
- * Contenteditable blocks need this just as much as plain inputs: focusing
16
- * one sets `core.state.editing`, and `EditorCore.mountKeyboard`'s
17
- * `selectionchange` listener refreshes the toolbar's active states on every
18
- * caret move. Without
19
- * caret-position restoration here, that would blow away and recreate the
20
- * focused div each time, so the very first keystroke would silently drop
21
- * focus and the RTE toolbar would appear to do nothing.
22
- */
23
- export function withFocusPreserved(root, rebuild) {
24
- const active = root.activeElement;
25
- const key = active && active.dataset ? active.dataset.focusKey : null;
26
- let selStart = null; let selEnd = null; let scrollTop = null; let editable = false;
27
- // A range slider mid-drag is the one focus-preservation case where
28
- // restoring focus on a rebuilt node isn't enough: dragging its thumb is a
29
- // native, implicit mouse capture tied to that exact element, and replacing
30
- // the element (as every other rebuilt input does here) silently drops that
31
- // capture -- the thumb stops tracking the mouse and the slider feels like
32
- // it's snapping/jumping instead of gliding. So this one case skips
33
- // rebuilding the node entirely: the live element is pulled out before
34
- // `rebuild()` and spliced back into the freshly-built tree afterward.
35
- const rangeNode = key && active.tagName === 'INPUT' && active.type === 'range' ? active : null;
36
- if (rangeNode) {
37
- // nothing to capture -- the node itself is preserved below, after rebuild()
38
- } else if (key && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA')) {
39
- try { selStart = active.selectionStart; selEnd = active.selectionEnd; } catch { /* some input types don't support selection */ }
40
- scrollTop = active.scrollTop;
41
- } else if (key && active.isContentEditable) {
42
- editable = true;
43
- const sel = shadowSelection(root);
44
- if (sel && sel.rangeCount && active.contains(sel.anchorNode)) {
45
- const range = sel.getRangeAt(0);
46
- selStart = textOffset(active, range.startContainer, range.startOffset);
47
- selEnd = textOffset(active, range.endContainer, range.endOffset);
48
- }
49
- scrollTop = active.scrollTop;
50
- }
51
-
52
- rebuild();
53
-
54
- if (!key) return;
55
- const next = root.querySelector(`[data-focus-key="${cssEscape(key)}"]`);
56
- if (!next) return;
57
- if (rangeNode) {
58
- for (const attr of ['min', 'max', 'step']) {
59
- const v = next.getAttribute(attr);
60
- if (v === null) rangeNode.removeAttribute(attr); else rangeNode.setAttribute(attr, v);
61
- }
62
- next.replaceWith(rangeNode);
63
- return;
64
- }
65
- if (editable) {
66
- // Set the Range *before* focusing: focusing a contenteditable establishes
67
- // its own default collapsed selection as a side effect, and doing that
68
- // after we've placed the real caret fires a second, out-of-order
69
- // `selectionchange` notification for that now-stale default -- which
70
- // arrives *after* the one for our real restore, so it looks like a fresh,
71
- // later selection change and clobbers the correct caret right back to
72
- // collapsed. Setting the range first means `.focus()` adopts the
73
- // selection that's already there instead of replacing it.
74
- if (selStart != null) {
75
- try {
76
- const range = document.createRange();
77
- setPointAtOffset(range, next, selStart, true);
78
- setPointAtOffset(range, next, selEnd, false);
79
- const sel = shadowSelection(root);
80
- sel.removeAllRanges();
81
- sel.addRange(range);
82
- } catch { /* ignore -- element structure changed under the caret */ }
83
- }
84
- // `preventScroll` matters a lot here: this `.focus()` fires on every
85
- // re-render of a block mid-edit (every keystroke), not just once. Without
86
- // it, the browser's default focus-scroll-into-view runs every time --
87
- // harmless on a short template, but on a long one it yanks the canvas
88
- // back toward the focused block on every keystroke, fighting whatever
89
- // scroll position the user actually had.
90
- next.focus({ preventScroll: true });
91
- } else {
92
- next.focus({ preventScroll: true });
93
- if (selStart != null) {
94
- try { next.setSelectionRange(selStart, selEnd); } catch { /* ignore */ }
95
- }
96
- }
97
- if (scrollTop != null) next.scrollTop = scrollTop;
98
- }
99
-
100
- /**
101
- * `document.getSelection()`/`window.getSelection()` is redacted to the host
102
- * document when the real selection lives inside an open shadow root -- in
103
- * Chrome its `anchorNode` reports as `<body>` rather than the actual text
104
- * node being edited, even though the visible caret and `execCommand` both
105
- * still operate on the real position. Reading through that redacted object
106
- * (as this file needs to, to compute/restore a caret offset) silently gives
107
- * back garbage -- not an error, just always "position 0" -- which is what
108
- * made every re-render-while-typing reset the caret to the start and type
109
- * new characters in reverse. `ShadowRoot.getSelection()` (Chromium-only; no
110
- * standard equivalent yet) reports the real node/offset.
111
- */
112
- function shadowSelection(root) {
113
- return typeof root.getSelection === 'function' ? root.getSelection() : window.getSelection();
114
- }
115
-
116
- /** Character offset of (node, offset) counting only text within `root`, walking in document order. */
117
- export function textOffset(root, node, offset) {
118
- if (node.nodeType !== Node.TEXT_NODE) {
119
- // A range boundary can land on an element (e.g. offset counts child nodes) --
120
- // resolve it to the text position right before its `offset`-th child.
121
- let n = 0;
122
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
123
- let cur; let target = node.childNodes[offset];
124
- if (!target) { while (walker.nextNode()) n += walker.currentNode.nodeValue.length; return n; }
125
- while ((cur = walker.nextNode())) { if (cur === target || target.contains(cur)) return n; n += cur.nodeValue.length; }
126
- return n;
127
- }
128
- let n = 0;
129
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
130
- let cur;
131
- while ((cur = walker.nextNode())) {
132
- if (cur === node) return n + offset;
133
- n += cur.nodeValue.length;
134
- }
135
- return n;
136
- }
137
-
138
- /** Sets the start or end point of `range` to the text-offset position inside `root`. */
139
- function setPointAtOffset(range, root, targetOffset, isStart) {
140
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
141
- let n = 0; let cur; let last = null;
142
- while ((cur = walker.nextNode())) {
143
- last = cur;
144
- const len = cur.nodeValue.length;
145
- if (n + len >= targetOffset) {
146
- const point = targetOffset - n;
147
- if (isStart) range.setStart(cur, point); else range.setEnd(cur, point);
148
- return;
149
- }
150
- n += len;
151
- }
152
- if (last) { if (isStart) range.setStart(last, last.nodeValue.length); else range.setEnd(last, last.nodeValue.length); }
153
- else { if (isStart) range.setStart(root, 0); else range.setEnd(root, 0); }
154
- }
155
-
156
- function cssEscape(s) {
157
- return String(s).replace(/["\\]/g, '\\$&');
158
- }
1
+ /**
2
+ * The original relies on React's reconciliation to keep a focused `<input>`'s
3
+ * DOM node alive across a per-keystroke re-render (Design-tab fields, the
4
+ * campaign title, AI brief/goal/tone, search boxes, the code textarea all
5
+ * commit on every `input` event, not just on blur). This renderer has no
6
+ * VDOM -- it rebuilds DOM from scratch -- so without this, typing a single
7
+ * character into any of those fields would lose focus immediately after.
8
+ *
9
+ * The fix: every such input (and every RTE-edited block: text, heading, box,
10
+ * html) carries a stable `data-focus-key`. Before a rebuild, capture the
11
+ * focused element's key + selection range; after, find the new element with
12
+ * the same key and restore focus and the caret position, so the net effect
13
+ * matches React's outcome even though the DOM node identity changed.
14
+ *
15
+ * Contenteditable blocks need this just as much as plain inputs: focusing
16
+ * one sets `core.state.editing`, and `EditorCore.mountKeyboard`'s
17
+ * `selectionchange` listener refreshes the toolbar's active states on every
18
+ * caret move. Without
19
+ * caret-position restoration here, that would blow away and recreate the
20
+ * focused div each time, so the very first keystroke would silently drop
21
+ * focus and the RTE toolbar would appear to do nothing.
22
+ */
23
+ export function withFocusPreserved(root, rebuild) {
24
+ const active = root.activeElement;
25
+ const key = active && active.dataset ? active.dataset.focusKey : null;
26
+ let selStart = null; let selEnd = null; let scrollTop = null; let editable = false;
27
+ // A range slider mid-drag is the one focus-preservation case where
28
+ // restoring focus on a rebuilt node isn't enough: dragging its thumb is a
29
+ // native, implicit mouse capture tied to that exact element, and replacing
30
+ // the element (as every other rebuilt input does here) silently drops that
31
+ // capture -- the thumb stops tracking the mouse and the slider feels like
32
+ // it's snapping/jumping instead of gliding. So this one case skips
33
+ // rebuilding the node entirely: the live element is pulled out before
34
+ // `rebuild()` and spliced back into the freshly-built tree afterward.
35
+ const rangeNode = key && active.tagName === 'INPUT' && active.type === 'range' ? active : null;
36
+ if (rangeNode) {
37
+ // nothing to capture -- the node itself is preserved below, after rebuild()
38
+ } else if (key && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA')) {
39
+ try { selStart = active.selectionStart; selEnd = active.selectionEnd; } catch { /* some input types don't support selection */ }
40
+ scrollTop = active.scrollTop;
41
+ } else if (key && active.isContentEditable) {
42
+ editable = true;
43
+ const sel = shadowSelection(root);
44
+ if (sel && sel.rangeCount && active.contains(sel.anchorNode)) {
45
+ const range = sel.getRangeAt(0);
46
+ selStart = textOffset(active, range.startContainer, range.startOffset);
47
+ selEnd = textOffset(active, range.endContainer, range.endOffset);
48
+ }
49
+ scrollTop = active.scrollTop;
50
+ }
51
+
52
+ rebuild();
53
+
54
+ if (!key) return;
55
+ const next = root.querySelector(`[data-focus-key="${cssEscape(key)}"]`);
56
+ if (!next) return;
57
+ if (rangeNode) {
58
+ for (const attr of ['min', 'max', 'step']) {
59
+ const v = next.getAttribute(attr);
60
+ if (v === null) rangeNode.removeAttribute(attr); else rangeNode.setAttribute(attr, v);
61
+ }
62
+ next.replaceWith(rangeNode);
63
+ return;
64
+ }
65
+ if (editable) {
66
+ // Set the Range *before* focusing: focusing a contenteditable establishes
67
+ // its own default collapsed selection as a side effect, and doing that
68
+ // after we've placed the real caret fires a second, out-of-order
69
+ // `selectionchange` notification for that now-stale default -- which
70
+ // arrives *after* the one for our real restore, so it looks like a fresh,
71
+ // later selection change and clobbers the correct caret right back to
72
+ // collapsed. Setting the range first means `.focus()` adopts the
73
+ // selection that's already there instead of replacing it.
74
+ if (selStart != null) {
75
+ try {
76
+ const range = document.createRange();
77
+ setPointAtOffset(range, next, selStart, true);
78
+ setPointAtOffset(range, next, selEnd, false);
79
+ const sel = shadowSelection(root);
80
+ sel.removeAllRanges();
81
+ sel.addRange(range);
82
+ } catch { /* ignore -- element structure changed under the caret */ }
83
+ }
84
+ // `preventScroll` matters a lot here: this `.focus()` fires on every
85
+ // re-render of a block mid-edit (every keystroke), not just once. Without
86
+ // it, the browser's default focus-scroll-into-view runs every time --
87
+ // harmless on a short template, but on a long one it yanks the canvas
88
+ // back toward the focused block on every keystroke, fighting whatever
89
+ // scroll position the user actually had.
90
+ next.focus({ preventScroll: true });
91
+ } else {
92
+ next.focus({ preventScroll: true });
93
+ if (selStart != null) {
94
+ try { next.setSelectionRange(selStart, selEnd); } catch { /* ignore */ }
95
+ }
96
+ }
97
+ if (scrollTop != null) next.scrollTop = scrollTop;
98
+ }
99
+
100
+ /**
101
+ * `document.getSelection()`/`window.getSelection()` is redacted to the host
102
+ * document when the real selection lives inside an open shadow root -- in
103
+ * Chrome its `anchorNode` reports as `<body>` rather than the actual text
104
+ * node being edited, even though the visible caret and `execCommand` both
105
+ * still operate on the real position. Reading through that redacted object
106
+ * (as this file needs to, to compute/restore a caret offset) silently gives
107
+ * back garbage -- not an error, just always "position 0" -- which is what
108
+ * made every re-render-while-typing reset the caret to the start and type
109
+ * new characters in reverse. `ShadowRoot.getSelection()` (Chromium-only; no
110
+ * standard equivalent yet) reports the real node/offset.
111
+ */
112
+ function shadowSelection(root) {
113
+ return typeof root.getSelection === 'function' ? root.getSelection() : window.getSelection();
114
+ }
115
+
116
+ /** Character offset of (node, offset) counting only text within `root`, walking in document order. */
117
+ export function textOffset(root, node, offset) {
118
+ if (node.nodeType !== Node.TEXT_NODE) {
119
+ // A range boundary can land on an element (e.g. offset counts child nodes) --
120
+ // resolve it to the text position right before its `offset`-th child.
121
+ let n = 0;
122
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
123
+ let cur; let target = node.childNodes[offset];
124
+ if (!target) { while (walker.nextNode()) n += walker.currentNode.nodeValue.length; return n; }
125
+ while ((cur = walker.nextNode())) { if (cur === target || target.contains(cur)) return n; n += cur.nodeValue.length; }
126
+ return n;
127
+ }
128
+ let n = 0;
129
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
130
+ let cur;
131
+ while ((cur = walker.nextNode())) {
132
+ if (cur === node) return n + offset;
133
+ n += cur.nodeValue.length;
134
+ }
135
+ return n;
136
+ }
137
+
138
+ /** Sets the start or end point of `range` to the text-offset position inside `root`. */
139
+ function setPointAtOffset(range, root, targetOffset, isStart) {
140
+ const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
141
+ let n = 0; let cur; let last = null;
142
+ while ((cur = walker.nextNode())) {
143
+ last = cur;
144
+ const len = cur.nodeValue.length;
145
+ if (n + len >= targetOffset) {
146
+ const point = targetOffset - n;
147
+ if (isStart) range.setStart(cur, point); else range.setEnd(cur, point);
148
+ return;
149
+ }
150
+ n += len;
151
+ }
152
+ if (last) { if (isStart) range.setStart(last, last.nodeValue.length); else range.setEnd(last, last.nodeValue.length); }
153
+ else { if (isStart) range.setStart(root, 0); else range.setEnd(root, 0); }
154
+ }
155
+
156
+ function cssEscape(s) {
157
+ return String(s).replace(/["\\]/g, '\\$&');
158
+ }
package/src/render/rte.js CHANGED
@@ -89,7 +89,27 @@ export function renderRte(core, b) {
89
89
  position: 'absolute', top: '-82px', left: '0', zIndex: '30', display: 'grid', gap: '5px',
90
90
  background: 'var(--rte-bg)', border: '1px solid var(--rte-border)', borderRadius: '8px', padding: '7px 8px', boxShadow: 'var(--rte-shadow)',
91
91
  }, { class: 'mc-rte', 'data-mc-rte': '1', 'data-rte-root': '1' });
92
- root.addEventListener('pointerdown', () => { core.rteActive = true; }, true);
92
+ // `rteActive` guards `blockCtx.onBlur` (canvas.js): the blur the edited
93
+ // block fires when a toolbar control takes focus (a select opening, a color
94
+ // input, the link field) must not end the edit. It used to be a one-way
95
+ // latch here -- only `core.exec()` ever reset it -- so any toolbar
96
+ // interaction that never reached exec (a dismissed dropdown, a cancelled
97
+ // color dialog, a click on the toolbar's padding) left it stuck true, and
98
+ // every later genuine blur was swallowed: the toolbar could no longer be
99
+ // closed by clicking outside. The swallow is only needed for the blur fired
100
+ // during this press's own mousedown, so release on the gesture's pointerup
101
+ // (blur always precedes it); controls that need the guard past the gesture
102
+ // re-arm it themselves (`exec`, `openLink`).
103
+ root.addEventListener('pointerdown', () => {
104
+ core.rteActive = true;
105
+ const release = () => {
106
+ core.rteActive = false;
107
+ window.removeEventListener('pointerup', release, true);
108
+ window.removeEventListener('pointercancel', release, true);
109
+ };
110
+ window.addEventListener('pointerup', release, true);
111
+ window.addEventListener('pointercancel', release, true);
112
+ }, true);
93
113
  root.addEventListener('mousedown', (e) => e.stopPropagation());
94
114
  root.addEventListener('click', (e) => e.stopPropagation());
95
115
 
@@ -208,7 +228,12 @@ function linkPopover(core, b, vars) {
208
228
  const r = el('div', { display: 'flex', alignItems: 'center', gap: '5px' });
209
229
  const input = el('input', inputStyle, { placeholder: 'https://example.com' });
210
230
  input.value = d.href;
211
- setTimeout(() => input.focus(), 0);
231
+ // `openLink()` (editor-core.js) arms `rteActive` so the block blur this
232
+ // focus steal fires is swallowed; the blur is synchronous inside `focus()`,
233
+ // so releasing right after keeps the guard from outliving its one job
234
+ // (stuck true, it made every later blur -- and so click-outside-to-close --
235
+ // dead, e.g. after cancelling the popover with Escape).
236
+ setTimeout(() => { input.focus(); core.rteActive = false; }, 0);
212
237
  // Debounced (typeCommit): each draft update re-renders everything. Flushed
213
238
  // explicitly before applyLink -- both Enter and the Apply button (which
214
239
  // suppresses blur on mousedown to keep the text selection) read the draft