@seliseblocks/mailcraft 0.1.0 → 0.2.0
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/DOCS.md +240 -12
- package/README.md +11 -3
- package/README.md.txt +134 -0
- package/dist/mailcraft-editor.bundle.js +51 -39
- package/dist/mailcraft-editor.bundle.js.map +3 -3
- package/examples/templates/order-confirmed.html +80 -80
- package/examples/vanilla.html +242 -23
- package/package.json +7 -2
- package/src/core/assets.js +10 -15
- package/src/core/binder.js +120 -118
- package/src/core/blocks.js +14 -1
- package/src/core/css-cascade.js +117 -117
- package/src/core/editor-core.js +1530 -1492
- package/src/core/export.js +142 -55
- package/src/core/i18n/ar.js +219 -177
- package/src/core/i18n/bg.js +196 -152
- package/src/core/i18n/bn.js +218 -176
- package/src/core/i18n/ca.js +196 -152
- package/src/core/i18n/cs.js +196 -152
- package/src/core/i18n/da.js +196 -152
- package/src/core/i18n/de-CH.js +196 -152
- package/src/core/i18n/de.js +196 -152
- package/src/core/i18n/dz.js +221 -179
- package/src/core/i18n/el.js +196 -152
- package/src/core/i18n/en.js +3 -10
- package/src/core/i18n/es.js +196 -152
- package/src/core/i18n/et.js +196 -152
- package/src/core/i18n/fi.js +196 -152
- package/src/core/i18n/fr.js +196 -152
- package/src/core/i18n/hr.js +196 -152
- package/src/core/i18n/hu.js +196 -152
- package/src/core/i18n/index.js +83 -83
- package/src/core/i18n/it.js +196 -152
- package/src/core/i18n/lt.js +196 -152
- package/src/core/i18n/lv.js +196 -152
- package/src/core/i18n/nb.js +196 -152
- package/src/core/i18n/nl.js +196 -152
- package/src/core/i18n/pl.js +196 -152
- package/src/core/i18n/pt.js +196 -152
- package/src/core/i18n/ro.js +196 -152
- package/src/core/i18n/ru.js +196 -152
- package/src/core/i18n/sk.js +196 -152
- package/src/core/i18n/sl.js +196 -152
- package/src/core/i18n/sv.js +196 -152
- package/src/core/i18n/tables.js +50 -50
- package/src/core/i18n/tr.js +196 -152
- package/src/core/i18n/uk.js +196 -152
- package/src/core/icons.js +237 -235
- package/src/core/ids.js +1 -1
- package/src/core/import-html.js +1025 -959
- package/src/core/layout-style.js +100 -100
- package/src/core/parse.js +10 -10
- package/src/core/placeholder.js +15 -15
- package/src/core/sanitize.js +141 -141
- package/src/core/storage-limits.js +184 -184
- package/src/core/storage.js +85 -85
- package/src/core/theme.js +1 -1
- package/src/core/variables.js +11 -11
- package/src/index.js +9 -9
- package/src/mailcraft-editor.js +26 -14
- package/src/render/block-body.js +49 -6
- package/src/render/canvas.js +31 -2
- package/src/render/fields.js +602 -588
- package/src/render/focus-preserve.js +158 -158
- package/src/render/rte.js +241 -212
- package/src/render/screenshot.js +132 -132
- package/src/render/story.js +415 -415
- package/src/render/style.js +8 -0
- package/types/index.d.ts +419 -0
|
@@ -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
|
+
}
|