@seliseblocks/mailcraft 0.1.1 → 0.2.1

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 (86) hide show
  1. package/DOCS.md +243 -16
  2. package/README.md +15 -6
  3. package/README.md.txt +135 -0
  4. package/dist/mailcraft-editor.bundle.js +87 -47
  5. package/dist/mailcraft-editor.bundle.js.map +3 -3
  6. package/examples/templates/activate-your-account.html +1 -1
  7. package/examples/templates/back-in-stock.html +1 -1
  8. package/examples/templates/cart-left-behind.html +1 -1
  9. package/examples/templates/community-giveaway.html +1 -1
  10. package/examples/templates/frontend-futures-invite.html +1 -1
  11. package/examples/templates/give-25-get-25.html +1 -1
  12. package/examples/templates/invoice-paid.html +1 -1
  13. package/examples/templates/meet-nova-launch.html +1 -1
  14. package/examples/templates/mega-weekend-sale.html +1 -1
  15. package/examples/templates/order-confirmed.html +80 -80
  16. package/examples/templates/rate-your-headphones.html +1 -1
  17. package/examples/templates/reset-your-password.html +1 -1
  18. package/examples/templates/thankyou-promo-code.html +1 -1
  19. package/examples/templates/the-sunday-brief.html +1 -1
  20. package/examples/templates/welcome-to-your-workspace.html +1 -1
  21. package/examples/templates/your-order-shipped.html +1 -1
  22. package/examples/templates/your-password-was-changed.html +1 -1
  23. package/examples/templates/your-signin-code.html +1 -1
  24. package/examples/vanilla.html +261 -47
  25. package/package.json +7 -2
  26. package/src/core/assets.js +10 -15
  27. package/src/core/binder.js +140 -118
  28. package/src/core/blocks.js +14 -1
  29. package/src/core/css-cascade.js +117 -117
  30. package/src/core/editor-core.js +1540 -1492
  31. package/src/core/export.js +158 -55
  32. package/src/core/i18n/ar.js +218 -177
  33. package/src/core/i18n/bg.js +195 -152
  34. package/src/core/i18n/bn.js +217 -176
  35. package/src/core/i18n/ca.js +195 -152
  36. package/src/core/i18n/cs.js +195 -152
  37. package/src/core/i18n/da.js +195 -152
  38. package/src/core/i18n/de-CH.js +195 -152
  39. package/src/core/i18n/de.js +195 -152
  40. package/src/core/i18n/dz.js +220 -179
  41. package/src/core/i18n/el.js +195 -152
  42. package/src/core/i18n/en.js +3 -11
  43. package/src/core/i18n/es.js +195 -152
  44. package/src/core/i18n/et.js +195 -152
  45. package/src/core/i18n/fi.js +195 -152
  46. package/src/core/i18n/fr.js +195 -152
  47. package/src/core/i18n/hr.js +195 -152
  48. package/src/core/i18n/hu.js +195 -152
  49. package/src/core/i18n/index.js +83 -83
  50. package/src/core/i18n/it.js +195 -152
  51. package/src/core/i18n/lt.js +195 -152
  52. package/src/core/i18n/lv.js +195 -152
  53. package/src/core/i18n/nb.js +195 -152
  54. package/src/core/i18n/nl.js +195 -152
  55. package/src/core/i18n/pl.js +195 -152
  56. package/src/core/i18n/pt.js +195 -152
  57. package/src/core/i18n/ro.js +195 -152
  58. package/src/core/i18n/ru.js +195 -152
  59. package/src/core/i18n/sk.js +195 -152
  60. package/src/core/i18n/sl.js +195 -152
  61. package/src/core/i18n/sv.js +195 -152
  62. package/src/core/i18n/tables.js +50 -50
  63. package/src/core/i18n/tr.js +195 -152
  64. package/src/core/i18n/uk.js +195 -152
  65. package/src/core/icons.js +237 -235
  66. package/src/core/ids.js +1 -1
  67. package/src/core/import-html.js +1047 -959
  68. package/src/core/layout-style.js +100 -100
  69. package/src/core/parse.js +10 -10
  70. package/src/core/placeholder.js +15 -15
  71. package/src/core/sanitize.js +141 -141
  72. package/src/core/storage-limits.js +189 -184
  73. package/src/core/storage.js +85 -85
  74. package/src/core/theme.js +15 -1
  75. package/src/core/variables.js +11 -11
  76. package/src/index.js +9 -9
  77. package/src/mailcraft-editor.js +64 -25
  78. package/src/render/block-body.js +49 -6
  79. package/src/render/canvas.js +74 -8
  80. package/src/render/fields.js +619 -588
  81. package/src/render/focus-preserve.js +158 -158
  82. package/src/render/rte.js +241 -212
  83. package/src/render/screenshot.js +138 -132
  84. package/src/render/story.js +415 -415
  85. package/src/render/style.js +44 -8
  86. package/types/index.d.ts +422 -0
@@ -1,132 +1,138 @@
1
- import { renderDoc } from './canvas.js';
2
-
3
- /**
4
- * Full-template screenshot, zero dependencies: render the same static tree
5
- * the preview modal uses (inline styles only), wrap it in an SVG
6
- * `<foreignObject>`, rasterize that through an `<img>` onto a canvas, and
7
- * hand back a PNG Blob.
8
- *
9
- * Two hard rules of SVG-loaded-as-image. The first is that the SVG has to
10
- * reach the `<img>` as a `data:` URI, never a `blob:` one: Chromium treats a
11
- * blob-loaded SVG that contains a `<foreignObject>` as cross-origin content,
12
- * so the canvas comes out tainted and `toBlob()` throws
13
- * ("Tainted canvases may not be exported") -- i.e. every capture fails, with
14
- * nothing in the console to say why. A `data:` URI of the same bytes is
15
- * origin-clean and rasterizes identically.
16
- *
17
- * The second is that it may not fetch ANY
18
- * external resource -- not "CORS applies", but a total network ban -- so
19
- * every image has to already be a data: URI when the SVG is built. Uploads
20
- * and the seeded placeholders are data: URIs by construction
21
- * (core/assets.js, core/placeholder.js); anything else (a hand-typed image
22
- * URL) is inlined here via a best-effort fetch, and a fetch the remote
23
- * server refuses (no CORS header) degrades to a blank pixel instead of
24
- * silently producing a tainted, unexportable canvas.
25
- *
26
- * Known limits, accepted: `<iframe>` embeds never paint inside foreignObject
27
- * (they render as their reserved box), and web fonts are unavailable, so
28
- * text falls back through the theme's system font stack -- which is what an
29
- * email client would show anyway.
30
- */
31
-
32
- const BLANK_PIXEL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
33
-
34
- function toDataUri(url) {
35
- return fetch(url, { mode: 'cors' }).then((res) => {
36
- if (!res.ok) throw new Error('fetch failed: ' + res.status);
37
- return res.blob();
38
- }).then((blob) => new Promise((resolve, reject) => {
39
- const fr = new FileReader();
40
- fr.onload = () => resolve(fr.result);
41
- fr.onerror = reject;
42
- fr.readAsDataURL(blob);
43
- }));
44
- }
45
-
46
- async function inlineExternalImages(root) {
47
- const jobs = [];
48
- root.querySelectorAll('img').forEach((img) => {
49
- const src = img.getAttribute('src') || '';
50
- if (!src || src.startsWith('data:')) return;
51
- jobs.push(toDataUri(src).then(
52
- (uri) => { img.setAttribute('src', uri); },
53
- () => { img.setAttribute('src', BLANK_PIXEL); },
54
- ));
55
- });
56
- root.querySelectorAll('[style]').forEach((el) => {
57
- const m = (el.style.backgroundImage || '').match(/url\(["']?(?!data:)([^"')]+)["']?\)/);
58
- if (!m) return;
59
- jobs.push(toDataUri(m[1]).then(
60
- (uri) => { el.style.backgroundImage = 'url("' + uri + '")'; },
61
- () => { el.style.backgroundImage = 'none'; },
62
- ));
63
- });
64
- await Promise.all(jobs);
65
- }
66
-
67
- /**
68
- * Renders the current document full-length (desktop width, independent of
69
- * the device toggle and zoom) and returns a PNG Blob at `scale`x resolution.
70
- * `mountInto` is any attached container inside the editor's shadow root --
71
- * the tree must live in the live document briefly, off-screen, to lay out
72
- * and be measured before serialization.
73
- *
74
- * (`export const` + async function expression, not `export async function`:
75
- * build.js's transform only recognizes `export (const|function|class)`.)
76
- */
77
- export const captureTemplatePng = async function (core, mountInto, scale = 2) {
78
- const theme = core.state.doc.theme;
79
- const pad = 32;
80
- const wrapper = document.createElement('div');
81
- // Off-screen but attached (fixed keeps it out of any scroll container's
82
- // scrollHeight, so capturing never moves the user's canvas position).
83
- wrapper.style.cssText = `position: fixed; left: -100000px; top: 0; width: ${theme.width + pad * 2}px; background: ${theme.bg}; padding: ${pad}px; box-sizing: border-box; display: flex; justify-content: center;`;
84
- wrapper.setAttribute('aria-hidden', 'true');
85
- // renderDoc sizes the sheet from `state.device`, so with the mobile toggle
86
- // on it would return a 375px sheet floating inside this desktop-width
87
- // wrapper. Borrow the state for the length of one synchronous render --
88
- // nothing observes it in between, and no re-render is triggered.
89
- const device = core.state.device;
90
- core.state.device = 'desktop';
91
- try {
92
- wrapper.appendChild(renderDoc(core, false));
93
- } finally {
94
- core.state.device = device;
95
- }
96
- mountInto.appendChild(wrapper);
97
- try {
98
- await inlineExternalImages(wrapper);
99
- // Heights depend on images' intrinsic ratios (blocks set width, not
100
- // height) -- decode everything before measuring or tall images collapse.
101
- await Promise.all(Array.from(wrapper.querySelectorAll('img')).map(
102
- (img) => (img.decode ? img.decode().catch(() => {}) : Promise.resolve()),
103
- ));
104
- const w = wrapper.offsetWidth;
105
- const h = wrapper.offsetHeight;
106
- // Serialize a *clone* with the off-screen positioning stripped -- the
107
- // wrapper's own `fixed; left: -100000px` would carry into the SVG and
108
- // place the content outside the drawable area.
109
- const copy = wrapper.cloneNode(true);
110
- copy.style.position = 'static';
111
- copy.style.left = 'auto';
112
- const xml = new XMLSerializer().serializeToString(copy);
113
- const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}"><foreignObject width="100%" height="100%">${xml}</foreignObject></svg>`;
114
-
115
- const img = new Image();
116
- await new Promise((resolve, reject) => {
117
- img.onload = resolve;
118
- img.onerror = () => reject(new Error('SVG rasterization failed'));
119
- img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
120
- });
121
- const canvas = document.createElement('canvas');
122
- canvas.width = Math.max(1, Math.round(w * scale));
123
- canvas.height = Math.max(1, Math.round(h * scale));
124
- const ctx = canvas.getContext('2d');
125
- ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
126
- return await new Promise((resolve, reject) => {
127
- canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('PNG encoding failed'))), 'image/png');
128
- });
129
- } finally {
130
- wrapper.remove();
131
- }
132
- };
1
+ import { renderDoc } from './canvas.js';
2
+
3
+ /**
4
+ * Full-template screenshot, zero dependencies: render the same static tree
5
+ * the preview modal uses (inline styles only), wrap it in an SVG
6
+ * `<foreignObject>`, rasterize that through an `<img>` onto a canvas, and
7
+ * hand back a PNG Blob.
8
+ *
9
+ * Two hard rules of SVG-loaded-as-image. The first is that the SVG has to
10
+ * reach the `<img>` as a `data:` URI, never a `blob:` one: Chromium treats a
11
+ * blob-loaded SVG that contains a `<foreignObject>` as cross-origin content,
12
+ * so the canvas comes out tainted and `toBlob()` throws
13
+ * ("Tainted canvases may not be exported") -- i.e. every capture fails, with
14
+ * nothing in the console to say why. A `data:` URI of the same bytes is
15
+ * origin-clean and rasterizes identically.
16
+ *
17
+ * The second is that it may not fetch ANY
18
+ * external resource -- not "CORS applies", but a total network ban -- so
19
+ * every image has to already be a data: URI when the SVG is built. A block
20
+ * placeholder is one by construction (core/placeholder.js), and so is a file
21
+ * dropped into the local library; anything else (a provider URL, a hand-typed
22
+ * URL) is inlined here via a best-effort fetch, and a fetch the remote
23
+ * server refuses (no CORS header) degrades to a blank pixel instead of
24
+ * silently producing a tainted, unexportable canvas.
25
+ *
26
+ * Known limits, accepted: `<iframe>` embeds never paint inside foreignObject
27
+ * (they render as their reserved box), and web fonts are unavailable, so
28
+ * text falls back through the theme's system font stack -- which is what an
29
+ * email client would show anyway.
30
+ */
31
+
32
+ const BLANK_PIXEL = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
33
+
34
+ function toDataUri(url) {
35
+ return fetch(url, { mode: 'cors' }).then((res) => {
36
+ if (!res.ok) throw new Error('fetch failed: ' + res.status);
37
+ return res.blob();
38
+ }).then((blob) => new Promise((resolve, reject) => {
39
+ const fr = new FileReader();
40
+ fr.onload = () => resolve(fr.result);
41
+ fr.onerror = reject;
42
+ fr.readAsDataURL(blob);
43
+ }));
44
+ }
45
+
46
+ async function inlineExternalImages(root) {
47
+ const jobs = [];
48
+ root.querySelectorAll('img').forEach((img) => {
49
+ const src = img.getAttribute('src') || '';
50
+ if (!src || src.startsWith('data:')) return;
51
+ jobs.push(toDataUri(src).then(
52
+ (uri) => { img.setAttribute('src', uri); },
53
+ () => { img.setAttribute('src', BLANK_PIXEL); },
54
+ ));
55
+ });
56
+ root.querySelectorAll('[style]').forEach((el) => {
57
+ const m = (el.style.backgroundImage || '').match(/url\(["']?(?!data:)([^"')]+)["']?\)/);
58
+ if (!m) return;
59
+ jobs.push(toDataUri(m[1]).then(
60
+ (uri) => { el.style.backgroundImage = 'url("' + uri + '")'; },
61
+ () => { el.style.backgroundImage = 'none'; },
62
+ ));
63
+ });
64
+ await Promise.all(jobs);
65
+ }
66
+
67
+ /**
68
+ * Renders the current document full-length (desktop width, independent of
69
+ * the device toggle and zoom) and returns a PNG Blob at `scale`x resolution.
70
+ * `mountInto` is any attached container inside the editor's shadow root --
71
+ * the tree must live in the live document briefly, off-screen, to lay out
72
+ * and be measured before serialization.
73
+ *
74
+ * (`export const` + async function expression, not `export async function`:
75
+ * build.js's transform only recognizes `export (const|function|class)`.)
76
+ */
77
+ export const captureTemplatePng = async function (core, mountInto, scale = 2) {
78
+ const theme = core.state.doc.theme;
79
+ const pad = 32;
80
+ const wrapper = document.createElement('div');
81
+ // Off-screen but attached (fixed keeps it out of any scroll container's
82
+ // scrollHeight, so capturing never moves the user's canvas position).
83
+ // The document's own page padding sits inside renderDoc's page element, so
84
+ // the wrapper has to be wide enough for it -- sized off the content width
85
+ // alone, `max-width:100%` would shrink the sheet below `theme.width` by
86
+ // exactly the padding and the capture would come out narrower than the
87
+ // template really is.
88
+ const pageW = theme.width + (Number(theme.padX) || 0) * 2;
89
+ wrapper.style.cssText = `position: fixed; left: -100000px; top: 0; width: ${pageW + pad * 2}px; background: ${theme.bg}; padding: ${pad}px; box-sizing: border-box; display: flex; justify-content: center;`;
90
+ wrapper.setAttribute('aria-hidden', 'true');
91
+ // renderDoc sizes the sheet from `state.device`, so with the mobile toggle
92
+ // on it would return a 375px sheet floating inside this desktop-width
93
+ // wrapper. Borrow the state for the length of one synchronous render --
94
+ // nothing observes it in between, and no re-render is triggered.
95
+ const device = core.state.device;
96
+ core.state.device = 'desktop';
97
+ try {
98
+ wrapper.appendChild(renderDoc(core, false));
99
+ } finally {
100
+ core.state.device = device;
101
+ }
102
+ mountInto.appendChild(wrapper);
103
+ try {
104
+ await inlineExternalImages(wrapper);
105
+ // Heights depend on images' intrinsic ratios (blocks set width, not
106
+ // height) -- decode everything before measuring or tall images collapse.
107
+ await Promise.all(Array.from(wrapper.querySelectorAll('img')).map(
108
+ (img) => (img.decode ? img.decode().catch(() => {}) : Promise.resolve()),
109
+ ));
110
+ const w = wrapper.offsetWidth;
111
+ const h = wrapper.offsetHeight;
112
+ // Serialize a *clone* with the off-screen positioning stripped -- the
113
+ // wrapper's own `fixed; left: -100000px` would carry into the SVG and
114
+ // place the content outside the drawable area.
115
+ const copy = wrapper.cloneNode(true);
116
+ copy.style.position = 'static';
117
+ copy.style.left = 'auto';
118
+ const xml = new XMLSerializer().serializeToString(copy);
119
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}"><foreignObject width="100%" height="100%">${xml}</foreignObject></svg>`;
120
+
121
+ const img = new Image();
122
+ await new Promise((resolve, reject) => {
123
+ img.onload = resolve;
124
+ img.onerror = () => reject(new Error('SVG rasterization failed'));
125
+ img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
126
+ });
127
+ const canvas = document.createElement('canvas');
128
+ canvas.width = Math.max(1, Math.round(w * scale));
129
+ canvas.height = Math.max(1, Math.round(h * scale));
130
+ const ctx = canvas.getContext('2d');
131
+ ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
132
+ return await new Promise((resolve, reject) => {
133
+ canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('PNG encoding failed'))), 'image/png');
134
+ });
135
+ } finally {
136
+ wrapper.remove();
137
+ }
138
+ };