@seliseblocks/mailcraft 0.2.18 → 0.2.19

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.
@@ -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
+ }
@@ -8,10 +8,15 @@
8
8
  * since a Web Component sizes to its host element, not the viewport.
9
9
  */
10
10
  export const STYLE = `
11
- @import url('https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700&display=swap');
12
11
  :host { display: block; height: 100%; }
13
12
  #mc, #mc *, #mc *::before, #mc *::after { box-sizing: border-box; }
14
13
  #mc {
14
+ /* No webfont is fetched here. The component makes no network requests of
15
+ its own -- it has to work from a \`file://\` page with the network off --
16
+ so the chrome renders in whatever of these the machine already has.
17
+ 'Manrope' stays at the head of the stack because a host that wants it
18
+ can load it itself; otherwise the platform UI face takes over. A host
19
+ that wants a different face sets \`ui-font\`. */
15
20
  --ed-font: 'Manrope', 'Segoe UI Variable', 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
16
21
  --ed-bg: #f5f7fa; --ed-panel: #ffffff; --ed-panel-2: #f8fafc; --ed-work: #f1f4f8;
17
22
  --ed-line: rgba(15,23,42,0.09); --ed-line-2: rgba(15,23,42,0.16);